browser_linux.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/application_info.h"
  10. #include "atom/common/atom_version.h"
  11. #include "base/command_line.h"
  12. #include "base/environment.h"
  13. #include "base/process/launch.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)
  25. return false;
  26. base::LaunchOptions options;
  27. options.fds_to_remap.push_back(std::make_pair(devnull, STDIN_FILENO));
  28. base::Process process = base::LaunchProcess(argv, options);
  29. close(devnull);
  30. if (!process.IsValid())
  31. 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 (auto* const 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. void Browser::ClearRecentDocuments() {}
  59. void Browser::SetAppUserModelID(const base::string16& name) {}
  60. bool Browser::SetAsDefaultProtocolClient(const std::string& protocol,
  61. mate::Arguments* args) {
  62. return SetDefaultWebClient(protocol);
  63. }
  64. bool Browser::IsDefaultProtocolClient(const std::string& protocol,
  65. mate::Arguments* args) {
  66. std::unique_ptr<base::Environment> env(base::Environment::Create());
  67. if (protocol.empty())
  68. return false;
  69. std::vector<std::string> argv;
  70. argv.push_back(kXdgSettings);
  71. argv.push_back("check");
  72. argv.push_back(kXdgSettingsDefaultSchemeHandler);
  73. argv.push_back(protocol);
  74. argv.push_back(libgtkui::GetDesktopName(env.get()));
  75. std::string reply;
  76. int success_code;
  77. bool ran_ok = base::GetAppOutputWithExitCode(base::CommandLine(argv), &reply,
  78. &success_code);
  79. if (!ran_ok || success_code != EXIT_SUCCESS)
  80. return false;
  81. // Allow any reply that starts with "yes".
  82. return base::StartsWith(reply, "yes", base::CompareCase::SENSITIVE) ? true
  83. : false;
  84. }
  85. // Todo implement
  86. bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol,
  87. mate::Arguments* args) {
  88. return false;
  89. }
  90. bool Browser::SetBadgeCount(int count) {
  91. if (IsUnityRunning()) {
  92. unity::SetDownloadCount(count);
  93. badge_count_ = count;
  94. return true;
  95. } else {
  96. return false;
  97. }
  98. }
  99. void Browser::SetLoginItemSettings(LoginItemSettings settings) {}
  100. Browser::LoginItemSettings Browser::GetLoginItemSettings(
  101. const LoginItemSettings& options) {
  102. return LoginItemSettings();
  103. }
  104. std::string Browser::GetExecutableFileVersion() const {
  105. return GetApplicationVersion();
  106. }
  107. std::string Browser::GetExecutableFileProductName() const {
  108. return GetApplicationName();
  109. }
  110. bool Browser::IsUnityRunning() {
  111. return unity::IsRunning();
  112. }
  113. bool Browser::IsEmojiPanelSupported() {
  114. return false;
  115. }
  116. void Browser::ShowAboutPanel() {
  117. std::string app_name, version, copyright, icon_path, website;
  118. GtkWidget* dialogWidget = gtk_about_dialog_new();
  119. GtkAboutDialog* dialog = GTK_ABOUT_DIALOG(dialogWidget);
  120. if (about_panel_options_.GetString("applicationName", &app_name))
  121. gtk_about_dialog_set_program_name(dialog, app_name.c_str());
  122. if (about_panel_options_.GetString("applicationVersion", &version))
  123. gtk_about_dialog_set_version(dialog, version.c_str());
  124. if (about_panel_options_.GetString("copyright", &copyright))
  125. gtk_about_dialog_set_copyright(dialog, copyright.c_str());
  126. if (about_panel_options_.GetString("website", &website))
  127. gtk_about_dialog_set_website(dialog, website.c_str());
  128. if (about_panel_options_.GetString("iconPath", &icon_path)) {
  129. GError* error = nullptr;
  130. constexpr int width = 64; // width of about panel icon in pixels
  131. constexpr int height = 64; // height of about panel icon in pixels
  132. // set preserve_aspect_ratio to true
  133. GdkPixbuf* icon = gdk_pixbuf_new_from_file_at_size(icon_path.c_str(), width,
  134. height, &error);
  135. if (error != nullptr) {
  136. g_warning("%s", error->message);
  137. g_clear_error(&error);
  138. } else {
  139. gtk_about_dialog_set_logo(dialog, icon);
  140. g_clear_object(&icon);
  141. }
  142. }
  143. gtk_dialog_run(GTK_DIALOG(dialog));
  144. gtk_widget_destroy(dialogWidget);
  145. }
  146. void Browser::SetAboutPanelOptions(const base::DictionaryValue& options) {
  147. about_panel_options_.Clear();
  148. for (const auto& pair : options) {
  149. const std::string& key = pair.first;
  150. const auto& val = pair.second;
  151. if (!key.empty() && val->is_string())
  152. about_panel_options_.SetString(key, val->GetString());
  153. }
  154. }
  155. } // namespace atom