browser_linux.cc 6.9 KB

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