browser_linux.cc 6.9 KB

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