browser_linux.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #include "shell/common/thread_restrictions.h"
  15. #if BUILDFLAG(IS_LINUX)
  16. #include "shell/browser/linux/unity_service.h"
  17. #include "ui/gtk/gtk_util.h" // nogncheck
  18. #endif
  19. namespace electron {
  20. const char kXdgSettings[] = "xdg-settings";
  21. const char kXdgSettingsDefaultSchemeHandler[] = "default-url-scheme-handler";
  22. // The use of the ForTesting flavors is a hack workaround to avoid having to
  23. // patch these as friends into the associated guard classes.
  24. class LaunchXdgUtilityScopedAllowBaseSyncPrimitives
  25. : public base::ScopedAllowBaseSyncPrimitivesForTesting {};
  26. bool LaunchXdgUtility(const std::vector<std::string>& argv, int* exit_code) {
  27. *exit_code = EXIT_FAILURE;
  28. int devnull = open("/dev/null", O_RDONLY);
  29. if (devnull < 0)
  30. return false;
  31. base::LaunchOptions options;
  32. options.fds_to_remap.emplace_back(devnull, STDIN_FILENO);
  33. base::Process process = base::LaunchProcess(argv, options);
  34. close(devnull);
  35. if (!process.IsValid())
  36. return false;
  37. LaunchXdgUtilityScopedAllowBaseSyncPrimitives allow_base_sync_primitives;
  38. return process.WaitForExit(exit_code);
  39. }
  40. absl::optional<std::string> GetXdgAppOutput(
  41. const std::vector<std::string>& argv) {
  42. std::string reply;
  43. int success_code;
  44. ScopedAllowBlockingForElectron allow_blocking;
  45. bool ran_ok = base::GetAppOutputWithExitCode(base::CommandLine(argv), &reply,
  46. &success_code);
  47. if (!ran_ok || success_code != EXIT_SUCCESS)
  48. return absl::optional<std::string>();
  49. return absl::make_optional(reply);
  50. }
  51. bool SetDefaultWebClient(const std::string& protocol) {
  52. auto env = base::Environment::Create();
  53. std::vector<std::string> argv = {kXdgSettings, "set"};
  54. if (!protocol.empty()) {
  55. argv.emplace_back(kXdgSettingsDefaultSchemeHandler);
  56. argv.emplace_back(protocol);
  57. }
  58. std::string desktop_name;
  59. if (!env->GetVar("CHROME_DESKTOP", &desktop_name)) {
  60. return false;
  61. }
  62. argv.emplace_back(desktop_name);
  63. int exit_code;
  64. bool ran_ok = LaunchXdgUtility(argv, &exit_code);
  65. return ran_ok && exit_code == EXIT_SUCCESS;
  66. }
  67. void Browser::AddRecentDocument(const base::FilePath& path) {}
  68. void Browser::ClearRecentDocuments() {}
  69. bool Browser::SetAsDefaultProtocolClient(const std::string& protocol,
  70. gin::Arguments* args) {
  71. return SetDefaultWebClient(protocol);
  72. }
  73. bool Browser::IsDefaultProtocolClient(const std::string& protocol,
  74. gin::Arguments* args) {
  75. auto env = base::Environment::Create();
  76. if (protocol.empty())
  77. return false;
  78. std::string desktop_name;
  79. if (!env->GetVar("CHROME_DESKTOP", &desktop_name))
  80. return false;
  81. const std::vector<std::string> argv = {kXdgSettings, "check",
  82. kXdgSettingsDefaultSchemeHandler,
  83. protocol, desktop_name};
  84. const auto output = GetXdgAppOutput(argv);
  85. if (!output)
  86. return false;
  87. // Allow any reply that starts with "yes".
  88. return base::StartsWith(output.value(), "yes", base::CompareCase::SENSITIVE);
  89. }
  90. // Todo implement
  91. bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol,
  92. gin::Arguments* args) {
  93. return false;
  94. }
  95. std::u16string Browser::GetApplicationNameForProtocol(const GURL& url) {
  96. const std::vector<std::string> argv = {
  97. "xdg-mime", "query", "default",
  98. std::string("x-scheme-handler/") + url.scheme()};
  99. return base::ASCIIToUTF16(GetXdgAppOutput(argv).value_or(std::string()));
  100. }
  101. bool Browser::SetBadgeCount(absl::optional<int> count) {
  102. if (IsUnityRunning() && count.has_value()) {
  103. unity::SetDownloadCount(count.value());
  104. badge_count_ = count.value();
  105. return true;
  106. } else {
  107. return false;
  108. }
  109. }
  110. void Browser::SetLoginItemSettings(LoginItemSettings settings) {}
  111. Browser::LoginItemSettings Browser::GetLoginItemSettings(
  112. const LoginItemSettings& options) {
  113. return LoginItemSettings();
  114. }
  115. std::string Browser::GetExecutableFileVersion() const {
  116. return GetApplicationVersion();
  117. }
  118. std::string Browser::GetExecutableFileProductName() const {
  119. return GetApplicationName();
  120. }
  121. bool Browser::IsUnityRunning() {
  122. return unity::IsRunning();
  123. }
  124. bool Browser::IsEmojiPanelSupported() {
  125. return false;
  126. }
  127. void Browser::ShowAboutPanel() {
  128. const auto& opts = about_panel_options_;
  129. GtkWidget* dialogWidget = gtk_about_dialog_new();
  130. GtkAboutDialog* dialog = GTK_ABOUT_DIALOG(dialogWidget);
  131. const std::string* str;
  132. const base::Value::List* list;
  133. if ((str = opts.FindString("applicationName"))) {
  134. gtk_about_dialog_set_program_name(dialog, str->c_str());
  135. }
  136. if ((str = opts.FindString("applicationVersion"))) {
  137. gtk_about_dialog_set_version(dialog, str->c_str());
  138. }
  139. if ((str = opts.FindString("copyright"))) {
  140. gtk_about_dialog_set_copyright(dialog, str->c_str());
  141. }
  142. if ((str = opts.FindString("website"))) {
  143. gtk_about_dialog_set_website(dialog, str->c_str());
  144. }
  145. if ((str = opts.FindString("iconPath"))) {
  146. GError* error = nullptr;
  147. constexpr int width = 64; // width of about panel icon in pixels
  148. constexpr int height = 64; // height of about panel icon in pixels
  149. // set preserve_aspect_ratio to true
  150. GdkPixbuf* icon =
  151. gdk_pixbuf_new_from_file_at_size(str->c_str(), width, height, &error);
  152. if (error != nullptr) {
  153. g_warning("%s", error->message);
  154. g_clear_error(&error);
  155. } else {
  156. gtk_about_dialog_set_logo(dialog, icon);
  157. g_clear_object(&icon);
  158. }
  159. }
  160. if ((list = opts.FindList("authors"))) {
  161. std::vector<const char*> cstrs;
  162. for (const auto& authorVal : *list) {
  163. if (authorVal.is_string()) {
  164. cstrs.push_back(authorVal.GetString().c_str());
  165. }
  166. }
  167. if (cstrs.empty()) {
  168. LOG(WARNING) << "No author strings found in 'authors' array";
  169. } else {
  170. cstrs.push_back(nullptr); // null-terminated char* array
  171. gtk_about_dialog_set_authors(dialog, cstrs.data());
  172. }
  173. }
  174. // destroy the widget when it closes
  175. g_signal_connect_swapped(dialogWidget, "response",
  176. G_CALLBACK(gtk_widget_destroy), dialogWidget);
  177. gtk_widget_show_all(dialogWidget);
  178. }
  179. void Browser::SetAboutPanelOptions(base::Value::Dict options) {
  180. about_panel_options_ = std::move(options);
  181. }
  182. } // namespace electron