platform_util_linux.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "atom/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. #define ELECTRON_DEFAULT_TRASH "gio"
  16. namespace {
  17. bool XDGUtilV(const std::vector<std::string>& argv, const bool wait_for_exit) {
  18. base::LaunchOptions options;
  19. options.allow_new_privs = true;
  20. // xdg-open can fall back on mailcap which eventually might plumb through
  21. // to a command that needs a terminal. Set the environment variable telling
  22. // it that we definitely don't have a terminal available and that it should
  23. // bring up a new terminal if necessary. See "man mailcap".
  24. options.environ["MM_NOTTTY"] = "1";
  25. base::Process process = base::LaunchProcess(argv, options);
  26. if (!process.IsValid())
  27. return false;
  28. if (wait_for_exit) {
  29. int exit_code = -1;
  30. if (!process.WaitForExit(&exit_code))
  31. return false;
  32. return (exit_code == 0);
  33. }
  34. base::EnsureProcessGetsReaped(std::move(process));
  35. return true;
  36. }
  37. bool XDGUtil(const std::string& util,
  38. const std::string& arg,
  39. const bool wait_for_exit) {
  40. std::vector<std::string> argv;
  41. argv.push_back(util);
  42. argv.push_back(arg);
  43. return XDGUtilV(argv, wait_for_exit);
  44. }
  45. bool XDGOpen(const std::string& path, const bool wait_for_exit) {
  46. return XDGUtil("xdg-open", path, wait_for_exit);
  47. }
  48. bool XDGEmail(const std::string& email, const bool wait_for_exit) {
  49. return XDGUtil("xdg-email", email, wait_for_exit);
  50. }
  51. } // namespace
  52. namespace platform_util {
  53. // TODO(estade): It would be nice to be able to select the file in the file
  54. // manager, but that probably requires extending xdg-open. For now just
  55. // show the folder.
  56. bool ShowItemInFolder(const base::FilePath& full_path) {
  57. base::FilePath dir = full_path.DirName();
  58. if (!base::DirectoryExists(dir))
  59. return false;
  60. return XDGOpen(dir.value(), false);
  61. }
  62. bool OpenItem(const base::FilePath& full_path) {
  63. return XDGOpen(full_path.value(), false);
  64. }
  65. bool OpenExternal(const GURL& url, const OpenExternalOptions& options) {
  66. // Don't wait for exit, since we don't want to wait for the browser/email
  67. // client window to close before returning
  68. if (url.SchemeIs("mailto"))
  69. return XDGEmail(url.spec(), false);
  70. else
  71. return XDGOpen(url.spec(), false);
  72. }
  73. void OpenExternal(const GURL& url,
  74. const OpenExternalOptions& options,
  75. OpenExternalCallback callback) {
  76. // TODO(gabriel): Implement async open if callback is specified
  77. std::move(callback).Run(OpenExternal(url, options) ? "" : "Failed to open");
  78. }
  79. bool MoveItemToTrash(const base::FilePath& full_path) {
  80. std::string trash;
  81. if (getenv(ELECTRON_TRASH) != NULL) {
  82. trash = getenv(ELECTRON_TRASH);
  83. } else {
  84. // Determine desktop environment and set accordingly.
  85. std::unique_ptr<base::Environment> env(base::Environment::Create());
  86. base::nix::DesktopEnvironment desktop_env(
  87. base::nix::GetDesktopEnvironment(env.get()));
  88. if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE4 ||
  89. desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE5) {
  90. trash = "kioclient5";
  91. } else if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE3) {
  92. trash = "kioclient";
  93. } else {
  94. trash = ELECTRON_DEFAULT_TRASH;
  95. }
  96. }
  97. std::vector<std::string> argv;
  98. if (trash.compare("kioclient5") == 0 || trash.compare("kioclient") == 0) {
  99. argv.push_back(trash);
  100. argv.push_back("move");
  101. argv.push_back(full_path.value());
  102. argv.push_back("trash:/");
  103. } else if (trash.compare("trash-cli") == 0) {
  104. argv.push_back("trash-put");
  105. argv.push_back(full_path.value());
  106. } else if (trash.compare("gvfs-trash") == 0) {
  107. // retain support for deprecated gvfs-trash
  108. argv.push_back("gvfs-trash");
  109. argv.push_back(full_path.value());
  110. } else {
  111. argv.push_back(ELECTRON_DEFAULT_TRASH);
  112. argv.push_back("trash");
  113. argv.push_back(full_path.value());
  114. }
  115. return XDGUtilV(argv, true);
  116. }
  117. void Beep() {
  118. // echo '\a' > /dev/console
  119. FILE* fp = fopen("/dev/console", "a");
  120. if (fp == nullptr) {
  121. fp = fopen("/dev/tty", "a");
  122. }
  123. if (fp != nullptr) {
  124. fprintf(fp, "\a");
  125. fclose(fp);
  126. }
  127. }
  128. bool GetDesktopName(std::string* setme) {
  129. bool found = false;
  130. std::unique_ptr<base::Environment> env(base::Environment::Create());
  131. std::string desktop_id = libgtkui::GetDesktopName(env.get());
  132. constexpr char const* libcc_default_id = "chromium-browser.desktop";
  133. if (!desktop_id.empty() && (desktop_id != libcc_default_id)) {
  134. *setme = desktop_id;
  135. found = true;
  136. }
  137. return found;
  138. }
  139. } // namespace platform_util