platform_util_linux.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/common/platform_util.h"
  5. #include <stdio.h>
  6. #include "base/cancelable_callback.h"
  7. #include "base/environment.h"
  8. #include "base/files/file_util.h"
  9. #include "base/nix/xdg_util.h"
  10. #include "base/process/kill.h"
  11. #include "base/process/launch.h"
  12. #include "chrome/browser/ui/libgtkui/gtk_util.h"
  13. #include "url/gurl.h"
  14. #define ELECTRON_TRASH "ELECTRON_TRASH"
  15. namespace {
  16. bool XDGUtil(const std::vector<std::string>& argv, const bool wait_for_exit) {
  17. base::LaunchOptions options;
  18. options.allow_new_privs = true;
  19. // xdg-open can fall back on mailcap which eventually might plumb through
  20. // to a command that needs a terminal. Set the environment variable telling
  21. // it that we definitely don't have a terminal available and that it should
  22. // bring up a new terminal if necessary. See "man mailcap".
  23. options.environment["MM_NOTTTY"] = "1";
  24. base::Process process = base::LaunchProcess(argv, options);
  25. if (!process.IsValid())
  26. return false;
  27. if (wait_for_exit) {
  28. int exit_code = -1;
  29. if (!process.WaitForExit(&exit_code))
  30. return false;
  31. return (exit_code == 0);
  32. }
  33. base::EnsureProcessGetsReaped(std::move(process));
  34. return true;
  35. }
  36. bool XDGOpen(const std::string& path, const bool wait_for_exit) {
  37. return XDGUtil({"xdg-open", path}, wait_for_exit);
  38. }
  39. bool XDGEmail(const std::string& email, const bool wait_for_exit) {
  40. return XDGUtil({"xdg-email", email}, wait_for_exit);
  41. }
  42. } // namespace
  43. namespace platform_util {
  44. // TODO(estade): It would be nice to be able to select the file in the file
  45. // manager, but that probably requires extending xdg-open. For now just
  46. // show the folder.
  47. void ShowItemInFolder(const base::FilePath& full_path) {
  48. base::FilePath dir = full_path.DirName();
  49. if (!base::DirectoryExists(dir))
  50. return;
  51. XDGOpen(dir.value(), false);
  52. }
  53. bool OpenItem(const base::FilePath& full_path) {
  54. return XDGOpen(full_path.value(), false);
  55. }
  56. void OpenExternal(const GURL& url,
  57. const OpenExternalOptions& options,
  58. OpenExternalCallback callback) {
  59. // Don't wait for exit, since we don't want to wait for the browser/email
  60. // client window to close before returning
  61. if (url.SchemeIs("mailto"))
  62. std::move(callback).Run(XDGEmail(url.spec(), false) ? ""
  63. : "Failed to open");
  64. else
  65. std::move(callback).Run(XDGOpen(url.spec(), false) ? "" : "Failed to open");
  66. }
  67. bool MoveItemToTrash(const base::FilePath& full_path, bool delete_on_fail) {
  68. std::unique_ptr<base::Environment> env(base::Environment::Create());
  69. // find the trash method
  70. std::string trash;
  71. if (!env->GetVar(ELECTRON_TRASH, &trash)) {
  72. // Determine desktop environment and set accordingly.
  73. const auto desktop_env(base::nix::GetDesktopEnvironment(env.get()));
  74. if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE4 ||
  75. desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE5) {
  76. trash = "kioclient5";
  77. } else if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE3) {
  78. trash = "kioclient";
  79. }
  80. }
  81. // build the invocation
  82. std::vector<std::string> argv;
  83. const auto& filename = full_path.value();
  84. if (trash == "kioclient5" || trash == "kioclient") {
  85. argv = {trash, "move", filename, "trash:/"};
  86. } else if (trash == "trash-cli") {
  87. argv = {"trash-put", filename};
  88. } else if (trash == "gvfs-trash") {
  89. argv = {"gvfs-trash", filename}; // deprecated, but still exists
  90. } else {
  91. argv = {"gio", "trash", filename};
  92. }
  93. return XDGUtil(argv, true);
  94. }
  95. void Beep() {
  96. // echo '\a' > /dev/console
  97. FILE* fp = fopen("/dev/console", "a");
  98. if (fp == nullptr) {
  99. fp = fopen("/dev/tty", "a");
  100. }
  101. if (fp != nullptr) {
  102. fprintf(fp, "\a");
  103. fclose(fp);
  104. }
  105. }
  106. bool GetDesktopName(std::string* setme) {
  107. return base::Environment::Create()->GetVar("CHROME_DESKTOP", setme);
  108. }
  109. } // namespace platform_util