browser_linux.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 BUILDFLAG(IS_LINUX)
  15. #include "shell/browser/linux/unity_service.h"
  16. #include "ui/gtk/gtk_util.h" // nogncheck
  17. #endif
  18. namespace electron {
  19. const char kXdgSettings[] = "xdg-settings";
  20. const char kXdgSettingsDefaultSchemeHandler[] = "default-url-scheme-handler";
  21. // The use of the ForTesting flavors is a hack workaround to avoid having to
  22. // patch these as friends into the associated guard classes.
  23. class LaunchXdgUtilityScopedAllowBaseSyncPrimitives
  24. : public base::ScopedAllowBaseSyncPrimitivesForTesting {};
  25. class GetXdgAppOutputScopedAllowBlocking
  26. : public base::ScopedAllowBlockingForTesting {};
  27. bool LaunchXdgUtility(const std::vector<std::string>& argv, int* exit_code) {
  28. *exit_code = EXIT_FAILURE;
  29. int devnull = open("/dev/null", O_RDONLY);
  30. if (devnull < 0)
  31. return false;
  32. base::LaunchOptions options;
  33. options.fds_to_remap.emplace_back(devnull, STDIN_FILENO);
  34. base::Process process = base::LaunchProcess(argv, options);
  35. close(devnull);
  36. if (!process.IsValid())
  37. return false;
  38. LaunchXdgUtilityScopedAllowBaseSyncPrimitives allow_base_sync_primitives;
  39. return process.WaitForExit(exit_code);
  40. }
  41. absl::optional<std::string> GetXdgAppOutput(
  42. const std::vector<std::string>& argv) {
  43. std::string reply;
  44. int success_code;
  45. GetXdgAppOutputScopedAllowBlocking allow_blocking;
  46. bool ran_ok = base::GetAppOutputWithExitCode(base::CommandLine(argv), &reply,
  47. &success_code);
  48. if (!ran_ok || success_code != EXIT_SUCCESS)
  49. return absl::optional<std::string>();
  50. return absl::make_optional(reply);
  51. }
  52. bool SetDefaultWebClient(const std::string& protocol) {
  53. auto env = base::Environment::Create();
  54. std::vector<std::string> argv = {kXdgSettings, "set"};
  55. if (!protocol.empty()) {
  56. argv.emplace_back(kXdgSettingsDefaultSchemeHandler);
  57. argv.emplace_back(protocol);
  58. }
  59. std::string desktop_name;
  60. if (!env->GetVar("CHROME_DESKTOP", &desktop_name)) {
  61. return false;
  62. }
  63. argv.emplace_back(desktop_name);
  64. int exit_code;
  65. bool ran_ok = LaunchXdgUtility(argv, &exit_code);
  66. return ran_ok && exit_code == EXIT_SUCCESS;
  67. }
  68. void Browser::AddRecentDocument(const base::FilePath& path) {}
  69. void Browser::ClearRecentDocuments() {}
  70. bool Browser::SetAsDefaultProtocolClient(const std::string& protocol,
  71. gin::Arguments* args) {
  72. return SetDefaultWebClient(protocol);
  73. }
  74. bool Browser::IsDefaultProtocolClient(const std::string& protocol,
  75. gin::Arguments* args) {
  76. auto env = base::Environment::Create();
  77. if (protocol.empty())
  78. return false;
  79. std::string desktop_name;
  80. if (!env->GetVar("CHROME_DESKTOP", &desktop_name))
  81. return false;
  82. const std::vector<std::string> argv = {kXdgSettings, "check",
  83. kXdgSettingsDefaultSchemeHandler,
  84. protocol, desktop_name};
  85. const auto output = GetXdgAppOutput(argv);
  86. if (!output)
  87. return false;
  88. // Allow any reply that starts with "yes".
  89. return base::StartsWith(output.value(), "yes", base::CompareCase::SENSITIVE);
  90. }
  91. // Todo implement
  92. bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol,
  93. gin::Arguments* args) {
  94. return false;
  95. }
  96. std::u16string Browser::GetApplicationNameForProtocol(const GURL& url) {
  97. const std::vector<std::string> argv = {
  98. "xdg-mime", "query", "default",
  99. std::string("x-scheme-handler/") + url.scheme()};
  100. return base::ASCIIToUTF16(GetXdgAppOutput(argv).value_or(std::string()));
  101. }
  102. bool Browser::SetBadgeCount(absl::optional<int> count) {
  103. if (IsUnityRunning() && count.has_value()) {
  104. unity::SetDownloadCount(count.value());
  105. badge_count_ = count.value();
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. }
  111. void Browser::SetLoginItemSettings(LoginItemSettings settings) {}
  112. Browser::LoginItemSettings Browser::GetLoginItemSettings(
  113. const LoginItemSettings& options) {
  114. return LoginItemSettings();
  115. }
  116. std::string Browser::GetExecutableFileVersion() const {
  117. return GetApplicationVersion();
  118. }
  119. std::string Browser::GetExecutableFileProductName() const {
  120. return GetApplicationName();
  121. }
  122. bool Browser::IsUnityRunning() {
  123. return unity::IsRunning();
  124. }
  125. bool Browser::IsEmojiPanelSupported() {
  126. return false;
  127. }
  128. void Browser::ShowAboutPanel() {
  129. const auto& opts = about_panel_options_;
  130. if (!opts.is_dict()) {
  131. LOG(WARNING) << "Called showAboutPanel(), but didn't use "
  132. "setAboutPanelSettings() first";
  133. return;
  134. }
  135. GtkWidget* dialogWidget = gtk_about_dialog_new();
  136. GtkAboutDialog* dialog = GTK_ABOUT_DIALOG(dialogWidget);
  137. const std::string* str;
  138. const base::Value* val;
  139. if ((str = opts.FindStringKey("applicationName"))) {
  140. gtk_about_dialog_set_program_name(dialog, str->c_str());
  141. }
  142. if ((str = opts.FindStringKey("applicationVersion"))) {
  143. gtk_about_dialog_set_version(dialog, str->c_str());
  144. }
  145. if ((str = opts.FindStringKey("copyright"))) {
  146. gtk_about_dialog_set_copyright(dialog, str->c_str());
  147. }
  148. if ((str = opts.FindStringKey("website"))) {
  149. gtk_about_dialog_set_website(dialog, str->c_str());
  150. }
  151. if ((str = opts.FindStringKey("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 ((val = opts.FindListKey("authors"))) {
  167. std::vector<const char*> cstrs;
  168. for (const auto& authorVal : val->GetListDeprecated()) {
  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. gtk_dialog_run(GTK_DIALOG(dialog));
  181. gtk_widget_destroy(dialogWidget);
  182. }
  183. void Browser::SetAboutPanelOptions(base::Value::Dict options) {
  184. about_panel_options_ = base::Value(std::move(options));
  185. }
  186. } // namespace electron