browser_linux.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/browser/browser.h"
  5. #include <fcntl.h>
  6. #include <stdlib.h>
  7. #include "base/command_line.h"
  8. #include "base/environment.h"
  9. #include "base/process/launch.h"
  10. #include "electron/electron_version.h"
  11. #include "shell/browser/native_window.h"
  12. #include "shell/browser/window_list.h"
  13. #include "shell/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 electron {
  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. const auto& opts = about_panel_options_;
  118. if (!opts.is_dict()) {
  119. LOG(WARNING) << "Called showAboutPanel(), but didn't use "
  120. "setAboutPanelSettings() first";
  121. return;
  122. }
  123. GtkWidget* dialogWidget = gtk_about_dialog_new();
  124. GtkAboutDialog* dialog = GTK_ABOUT_DIALOG(dialogWidget);
  125. const std::string* str;
  126. const base::Value* val;
  127. if ((str = opts.FindStringKey("applicationName"))) {
  128. gtk_about_dialog_set_program_name(dialog, str->c_str());
  129. }
  130. if ((str = opts.FindStringKey("applicationVersion"))) {
  131. gtk_about_dialog_set_version(dialog, str->c_str());
  132. }
  133. if ((str = opts.FindStringKey("copyright"))) {
  134. gtk_about_dialog_set_copyright(dialog, str->c_str());
  135. }
  136. if ((str = opts.FindStringKey("website"))) {
  137. gtk_about_dialog_set_website(dialog, str->c_str());
  138. }
  139. if ((str = opts.FindStringKey("iconPath"))) {
  140. GError* error = nullptr;
  141. constexpr int width = 64; // width of about panel icon in pixels
  142. constexpr int height = 64; // height of about panel icon in pixels
  143. // set preserve_aspect_ratio to true
  144. GdkPixbuf* icon =
  145. gdk_pixbuf_new_from_file_at_size(str->c_str(), width, height, &error);
  146. if (error != nullptr) {
  147. g_warning("%s", error->message);
  148. g_clear_error(&error);
  149. } else {
  150. gtk_about_dialog_set_logo(dialog, icon);
  151. g_clear_object(&icon);
  152. }
  153. }
  154. if ((val = opts.FindListKey("authors"))) {
  155. std::vector<const char*> cstrs;
  156. for (const auto& authorVal : val->GetList()) {
  157. if (authorVal.is_string()) {
  158. cstrs.push_back(authorVal.GetString().c_str());
  159. }
  160. }
  161. if (cstrs.empty()) {
  162. LOG(WARNING) << "No author strings found in 'authors' array";
  163. } else {
  164. cstrs.push_back(nullptr); // null-terminated char* array
  165. gtk_about_dialog_set_authors(dialog, cstrs.data());
  166. }
  167. }
  168. gtk_dialog_run(GTK_DIALOG(dialog));
  169. gtk_widget_destroy(dialogWidget);
  170. }
  171. void Browser::SetAboutPanelOptions(const base::DictionaryValue& options) {
  172. about_panel_options_ = options.Clone();
  173. }
  174. } // namespace electron