browser_linux.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/browser/browser.h"
  5. #include <fcntl.h>
  6. #include <stdlib.h>
  7. #include "atom/browser/native_window.h"
  8. #include "atom/browser/window_list.h"
  9. #include "atom/common/atom_version.h"
  10. #include "base/command_line.h"
  11. #include "base/environment.h"
  12. #include "base/process/launch.h"
  13. #include "brightray/common/application_info.h"
  14. #if defined(USE_X11)
  15. #include "chrome/browser/ui/libgtkui/gtk_util.h"
  16. #include "chrome/browser/ui/libgtkui/unity_service.h"
  17. #endif
  18. namespace atom {
  19. const char kXdgSettings[] = "xdg-settings";
  20. const char kXdgSettingsDefaultSchemeHandler[] = "default-url-scheme-handler";
  21. bool LaunchXdgUtility(const std::vector<std::string>& argv, int* exit_code) {
  22. *exit_code = EXIT_FAILURE;
  23. int devnull = open("/dev/null", O_RDONLY);
  24. if (devnull < 0) return false;
  25. base::LaunchOptions options;
  26. base::FileHandleMappingVector remap;
  27. remap.push_back(std::make_pair(devnull, STDIN_FILENO));
  28. options.fds_to_remap = &remap;
  29. base::Process process = base::LaunchProcess(argv, options);
  30. close(devnull);
  31. if (!process.IsValid()) return false;
  32. return process.WaitForExit(exit_code);
  33. }
  34. bool SetDefaultWebClient(const std::string& protocol) {
  35. std::unique_ptr<base::Environment> env(base::Environment::Create());
  36. std::vector<std::string> argv;
  37. argv.push_back(kXdgSettings);
  38. argv.push_back("set");
  39. if (!protocol.empty()) {
  40. argv.push_back(kXdgSettingsDefaultSchemeHandler);
  41. argv.push_back(protocol);
  42. }
  43. argv.push_back(libgtkui::GetDesktopName(env.get()));
  44. int exit_code;
  45. bool ran_ok = LaunchXdgUtility(argv, &exit_code);
  46. return ran_ok && exit_code == EXIT_SUCCESS;
  47. }
  48. void Browser::Focus() {
  49. // Focus on the first visible window.
  50. for (const auto& window : WindowList::GetWindows()) {
  51. if (window->IsVisible()) {
  52. window->Focus(true);
  53. break;
  54. }
  55. }
  56. }
  57. void Browser::AddRecentDocument(const base::FilePath& path) {
  58. }
  59. void Browser::ClearRecentDocuments() {
  60. }
  61. void Browser::SetAppUserModelID(const base::string16& name) {
  62. }
  63. bool Browser::SetAsDefaultProtocolClient(const std::string& protocol,
  64. mate::Arguments* args) {
  65. return SetDefaultWebClient(protocol);
  66. }
  67. bool Browser::IsDefaultProtocolClient(const std::string& protocol,
  68. mate::Arguments* args) {
  69. std::unique_ptr<base::Environment> env(base::Environment::Create());
  70. if (protocol.empty()) return false;
  71. std::vector<std::string> argv;
  72. argv.push_back(kXdgSettings);
  73. argv.push_back("check");
  74. argv.push_back(kXdgSettingsDefaultSchemeHandler);
  75. argv.push_back(protocol);
  76. argv.push_back(libgtkui::GetDesktopName(env.get()));
  77. std::string reply;
  78. int success_code;
  79. bool ran_ok = base::GetAppOutputWithExitCode(base::CommandLine(argv),
  80. &reply, &success_code);
  81. if (!ran_ok || success_code != EXIT_SUCCESS) return false;
  82. // Allow any reply that starts with "yes".
  83. return base::StartsWith(reply, "yes", base::CompareCase::SENSITIVE)
  84. ? true
  85. : false;
  86. }
  87. // Todo implement
  88. bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol,
  89. mate::Arguments* args) {
  90. return false;
  91. }
  92. bool Browser::SetBadgeCount(int count) {
  93. if (IsUnityRunning()) {
  94. unity::SetDownloadCount(count);
  95. badge_count_ = count;
  96. return true;
  97. } else {
  98. return false;
  99. }
  100. }
  101. void Browser::SetLoginItemSettings(LoginItemSettings settings) {
  102. }
  103. Browser::LoginItemSettings Browser::GetLoginItemSettings(
  104. const LoginItemSettings& options) {
  105. return LoginItemSettings();
  106. }
  107. std::string Browser::GetExecutableFileVersion() const {
  108. return brightray::GetApplicationVersion();
  109. }
  110. std::string Browser::GetExecutableFileProductName() const {
  111. return brightray::GetApplicationName();
  112. }
  113. bool Browser::IsUnityRunning() {
  114. return unity::IsRunning();
  115. }
  116. } // namespace atom