browser_linux.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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(OS_LINUX)
  15. #include "shell/browser/linux/unity_service.h"
  16. #include "ui/gtk/gtk_util.h"
  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. base::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 base::Optional<std::string>();
  50. return base::make_optional(reply);
  51. }
  52. bool SetDefaultWebClient(const std::string& protocol) {
  53. std::unique_ptr<base::Environment> 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::Focus(gin::Arguments* args) {
  69. // Focus on the first visible window.
  70. for (auto* const window : WindowList::GetWindows()) {
  71. if (window->IsVisible()) {
  72. window->Focus(true);
  73. break;
  74. }
  75. }
  76. }
  77. void Browser::AddRecentDocument(const base::FilePath& path) {}
  78. void Browser::ClearRecentDocuments() {}
  79. void Browser::SetAppUserModelID(const base::string16& name) {}
  80. bool Browser::SetAsDefaultProtocolClient(const std::string& protocol,
  81. gin::Arguments* args) {
  82. return SetDefaultWebClient(protocol);
  83. }
  84. bool Browser::IsDefaultProtocolClient(const std::string& protocol,
  85. gin::Arguments* args) {
  86. std::unique_ptr<base::Environment> env(base::Environment::Create());
  87. if (protocol.empty())
  88. return false;
  89. std::string desktop_name;
  90. if (!env->GetVar("CHROME_DESKTOP", &desktop_name))
  91. return false;
  92. const std::vector<std::string> argv = {kXdgSettings, "check",
  93. kXdgSettingsDefaultSchemeHandler,
  94. protocol, desktop_name};
  95. const auto output = GetXdgAppOutput(argv);
  96. if (!output)
  97. return false;
  98. // Allow any reply that starts with "yes".
  99. return base::StartsWith(output.value(), "yes", base::CompareCase::SENSITIVE);
  100. }
  101. // Todo implement
  102. bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol,
  103. gin::Arguments* args) {
  104. return false;
  105. }
  106. base::string16 Browser::GetApplicationNameForProtocol(const GURL& url) {
  107. const std::vector<std::string> argv = {
  108. "xdg-mime", "query", "default",
  109. std::string("x-scheme-handler/") + url.scheme()};
  110. return base::ASCIIToUTF16(GetXdgAppOutput(argv).value_or(std::string()));
  111. }
  112. bool Browser::SetBadgeCount(base::Optional<int> count) {
  113. if (IsUnityRunning() && count.has_value()) {
  114. unity::SetDownloadCount(count.value());
  115. badge_count_ = count.value();
  116. return true;
  117. } else {
  118. return false;
  119. }
  120. }
  121. void Browser::SetLoginItemSettings(LoginItemSettings settings) {}
  122. Browser::LoginItemSettings Browser::GetLoginItemSettings(
  123. const LoginItemSettings& options) {
  124. return LoginItemSettings();
  125. }
  126. std::string Browser::GetExecutableFileVersion() const {
  127. return GetApplicationVersion();
  128. }
  129. std::string Browser::GetExecutableFileProductName() const {
  130. return GetApplicationName();
  131. }
  132. bool Browser::IsUnityRunning() {
  133. return unity::IsRunning();
  134. }
  135. bool Browser::IsEmojiPanelSupported() {
  136. return false;
  137. }
  138. void Browser::ShowAboutPanel() {
  139. const auto& opts = about_panel_options_;
  140. if (!opts.is_dict()) {
  141. LOG(WARNING) << "Called showAboutPanel(), but didn't use "
  142. "setAboutPanelSettings() first";
  143. return;
  144. }
  145. GtkWidget* dialogWidget = gtk_about_dialog_new();
  146. GtkAboutDialog* dialog = GTK_ABOUT_DIALOG(dialogWidget);
  147. const std::string* str;
  148. const base::Value* val;
  149. if ((str = opts.FindStringKey("applicationName"))) {
  150. gtk_about_dialog_set_program_name(dialog, str->c_str());
  151. }
  152. if ((str = opts.FindStringKey("applicationVersion"))) {
  153. gtk_about_dialog_set_version(dialog, str->c_str());
  154. }
  155. if ((str = opts.FindStringKey("copyright"))) {
  156. gtk_about_dialog_set_copyright(dialog, str->c_str());
  157. }
  158. if ((str = opts.FindStringKey("website"))) {
  159. gtk_about_dialog_set_website(dialog, str->c_str());
  160. }
  161. if ((str = opts.FindStringKey("iconPath"))) {
  162. GError* error = nullptr;
  163. constexpr int width = 64; // width of about panel icon in pixels
  164. constexpr int height = 64; // height of about panel icon in pixels
  165. // set preserve_aspect_ratio to true
  166. GdkPixbuf* icon =
  167. gdk_pixbuf_new_from_file_at_size(str->c_str(), width, height, &error);
  168. if (error != nullptr) {
  169. g_warning("%s", error->message);
  170. g_clear_error(&error);
  171. } else {
  172. gtk_about_dialog_set_logo(dialog, icon);
  173. g_clear_object(&icon);
  174. }
  175. }
  176. if ((val = opts.FindListKey("authors"))) {
  177. std::vector<const char*> cstrs;
  178. for (const auto& authorVal : val->GetList()) {
  179. if (authorVal.is_string()) {
  180. cstrs.push_back(authorVal.GetString().c_str());
  181. }
  182. }
  183. if (cstrs.empty()) {
  184. LOG(WARNING) << "No author strings found in 'authors' array";
  185. } else {
  186. cstrs.push_back(nullptr); // null-terminated char* array
  187. gtk_about_dialog_set_authors(dialog, cstrs.data());
  188. }
  189. }
  190. gtk_dialog_run(GTK_DIALOG(dialog));
  191. gtk_widget_destroy(dialogWidget);
  192. }
  193. void Browser::SetAboutPanelOptions(base::DictionaryValue options) {
  194. about_panel_options_ = std::move(options);
  195. }
  196. } // namespace electron