browser.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. #ifndef ELECTRON_SHELL_BROWSER_BROWSER_H_
  5. #define ELECTRON_SHELL_BROWSER_BROWSER_H_
  6. #include <memory>
  7. #include <optional>
  8. #include <string>
  9. #include <vector>
  10. #include "base/compiler_specific.h"
  11. #include "base/observer_list.h"
  12. #include "base/task/cancelable_task_tracker.h"
  13. #include "base/values.h"
  14. #include "gin/dictionary.h"
  15. #include "shell/browser/browser_observer.h"
  16. #include "shell/browser/window_list_observer.h"
  17. #include "shell/common/gin_converters/login_item_settings_converter.h"
  18. #include "shell/common/gin_helper/promise.h"
  19. #if BUILDFLAG(IS_WIN)
  20. #include <windows.h>
  21. #include "base/files/file_path.h"
  22. #include "shell/browser/ui/win/taskbar_host.h"
  23. #endif
  24. #if BUILDFLAG(IS_MAC)
  25. #include "ui/base/cocoa/secure_password_input.h"
  26. #endif
  27. namespace base {
  28. class FilePath;
  29. }
  30. namespace gin_helper {
  31. class Arguments;
  32. }
  33. namespace electron {
  34. class ElectronMenuModel;
  35. #if BUILDFLAG(IS_WIN)
  36. struct LaunchItem {
  37. std::wstring name;
  38. std::wstring path;
  39. std::wstring scope;
  40. std::vector<std::wstring> args;
  41. bool enabled = true;
  42. LaunchItem();
  43. ~LaunchItem();
  44. LaunchItem(const LaunchItem&);
  45. };
  46. #endif
  47. struct LoginItemSettings {
  48. bool open_at_login = false;
  49. bool open_as_hidden = false;
  50. bool restore_state = false;
  51. bool opened_at_login = false;
  52. bool opened_as_hidden = false;
  53. std::u16string path;
  54. std::vector<std::u16string> args;
  55. #if BUILDFLAG(IS_MAC)
  56. std::string type = "mainAppService";
  57. std::string service_name;
  58. std::string status;
  59. #elif BUILDFLAG(IS_WIN)
  60. // used in browser::setLoginItemSettings
  61. bool enabled = true;
  62. std::wstring name;
  63. // used in browser::getLoginItemSettings
  64. bool executable_will_launch_at_login = false;
  65. std::vector<LaunchItem> launch_items;
  66. #endif
  67. LoginItemSettings();
  68. ~LoginItemSettings();
  69. LoginItemSettings(const LoginItemSettings&);
  70. };
  71. // This class is used for control application-wide operations.
  72. class Browser : private WindowListObserver {
  73. public:
  74. Browser();
  75. ~Browser() override;
  76. // disable copy
  77. Browser(const Browser&) = delete;
  78. Browser& operator=(const Browser&) = delete;
  79. static Browser* Get();
  80. // Try to close all windows and quit the application.
  81. void Quit();
  82. // Exit the application immediately and set exit code.
  83. void Exit(gin::Arguments* args);
  84. // Cleanup everything and shutdown the application gracefully.
  85. void Shutdown();
  86. // Focus the application.
  87. void Focus(gin::Arguments* args);
  88. // Returns the version of the executable (or bundle).
  89. std::string GetVersion() const;
  90. // Overrides the application version.
  91. void SetVersion(const std::string& version);
  92. // Returns the application's name, default is just Electron.
  93. std::string GetName() const;
  94. // Overrides the application name.
  95. void SetName(const std::string& name);
  96. // Add the |path| to recent documents list.
  97. void AddRecentDocument(const base::FilePath& path);
  98. // Clear the recent documents list.
  99. void ClearRecentDocuments();
  100. #if BUILDFLAG(IS_WIN)
  101. // Set the application user model ID.
  102. void SetAppUserModelID(const std::wstring& name);
  103. #endif
  104. // Remove the default protocol handler registry key
  105. bool RemoveAsDefaultProtocolClient(const std::string& protocol,
  106. gin::Arguments* args);
  107. // Set as default handler for a protocol.
  108. bool SetAsDefaultProtocolClient(const std::string& protocol,
  109. gin::Arguments* args);
  110. // Query the current state of default handler for a protocol.
  111. bool IsDefaultProtocolClient(const std::string& protocol,
  112. gin::Arguments* args);
  113. std::u16string GetApplicationNameForProtocol(const GURL& url);
  114. #if !BUILDFLAG(IS_LINUX)
  115. // get the name, icon and path for an application
  116. v8::Local<v8::Promise> GetApplicationInfoForProtocol(v8::Isolate* isolate,
  117. const GURL& url);
  118. #endif
  119. // Set/Get the badge count.
  120. bool SetBadgeCount(std::optional<int> count);
  121. [[nodiscard]] int badge_count() const { return badge_count_; }
  122. void SetLoginItemSettings(LoginItemSettings settings);
  123. v8::Local<v8::Value> GetLoginItemSettings(const LoginItemSettings& options);
  124. #if BUILDFLAG(IS_MAC)
  125. // Set the handler which decides whether to shutdown.
  126. void SetShutdownHandler(base::RepeatingCallback<bool()> handler);
  127. // Hide the application.
  128. void Hide();
  129. bool IsHidden();
  130. // Show the application.
  131. void Show();
  132. // Creates an activity and sets it as the one currently in use.
  133. void SetUserActivity(const std::string& type,
  134. base::Value::Dict user_info,
  135. gin::Arguments* args);
  136. // Returns the type name of the current user activity.
  137. std::string GetCurrentActivityType();
  138. // Invalidates an activity and marks it as no longer eligible for
  139. // continuation
  140. void InvalidateCurrentActivity();
  141. // Marks this activity object as inactive without invalidating it.
  142. void ResignCurrentActivity();
  143. // Updates the current user activity
  144. void UpdateCurrentActivity(const std::string& type,
  145. base::Value::Dict user_info);
  146. // Indicates that an user activity is about to be resumed.
  147. bool WillContinueUserActivity(const std::string& type);
  148. // Indicates a failure to resume a Handoff activity.
  149. void DidFailToContinueUserActivity(const std::string& type,
  150. const std::string& error);
  151. // Resumes an activity via hand-off.
  152. bool ContinueUserActivity(const std::string& type,
  153. base::Value::Dict user_info,
  154. base::Value::Dict details);
  155. // Indicates that an activity was continued on another device.
  156. void UserActivityWasContinued(const std::string& type,
  157. base::Value::Dict user_info);
  158. // Gives an opportunity to update the Handoff payload.
  159. bool UpdateUserActivityState(const std::string& type,
  160. base::Value::Dict user_info);
  161. void ApplyForcedRTL();
  162. // Bounce the dock icon.
  163. enum class BounceType{
  164. kCritical = 0, // NSCriticalRequest
  165. kInformational = 10, // NSInformationalRequest
  166. };
  167. int DockBounce(BounceType type);
  168. void DockCancelBounce(int request_id);
  169. // Bounce the Downloads stack.
  170. void DockDownloadFinished(const std::string& filePath);
  171. // Set/Get dock's badge text.
  172. void DockSetBadgeText(const std::string& label);
  173. std::string DockGetBadgeText();
  174. // Hide/Show dock.
  175. void DockHide();
  176. v8::Local<v8::Promise> DockShow(v8::Isolate* isolate);
  177. bool DockIsVisible();
  178. // Set docks' menu.
  179. void DockSetMenu(ElectronMenuModel* model);
  180. // Set docks' icon.
  181. void DockSetIcon(v8::Isolate* isolate, v8::Local<v8::Value> icon);
  182. void SetLaunchedAtLogin(bool launched_at_login) {
  183. was_launched_at_login_ = launched_at_login;
  184. }
  185. #endif // BUILDFLAG(IS_MAC)
  186. void ShowAboutPanel();
  187. void SetAboutPanelOptions(base::Value::Dict options);
  188. #if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
  189. void ShowEmojiPanel();
  190. #endif
  191. #if BUILDFLAG(IS_WIN)
  192. struct UserTask {
  193. base::FilePath program;
  194. std::wstring arguments;
  195. std::wstring title;
  196. std::wstring description;
  197. base::FilePath working_dir;
  198. base::FilePath icon_path;
  199. int icon_index;
  200. UserTask();
  201. UserTask(const UserTask&);
  202. ~UserTask();
  203. };
  204. // Add a custom task to jump list.
  205. bool SetUserTasks(const std::vector<UserTask>& tasks);
  206. // Returns the application user model ID, if there isn't one, then create
  207. // one from app's name.
  208. // The returned string managed by Browser, and should not be modified.
  209. PCWSTR GetAppUserModelID();
  210. #endif // BUILDFLAG(IS_WIN)
  211. #if BUILDFLAG(IS_LINUX)
  212. // Whether Unity launcher is running.
  213. bool IsUnityRunning();
  214. #endif // BUILDFLAG(IS_LINUX)
  215. // Tell the application to open a file.
  216. bool OpenFile(const std::string& file_path);
  217. // Tell the application to open a url.
  218. void OpenURL(const std::string& url);
  219. #if BUILDFLAG(IS_MAC)
  220. // Tell the application to create a new window for a tab.
  221. void NewWindowForTab();
  222. // Indicate that the app is now active.
  223. void DidBecomeActive();
  224. // Indicate that the app is no longer active and doesn’t have focus.
  225. void DidResignActive();
  226. #endif // BUILDFLAG(IS_MAC)
  227. // Tell the application that application is activated with visible/invisible
  228. // windows.
  229. void Activate(bool has_visible_windows);
  230. bool IsEmojiPanelSupported();
  231. // Tell the application the loading has been done.
  232. void WillFinishLaunching();
  233. void DidFinishLaunching(base::Value::Dict launch_info);
  234. void OnAccessibilitySupportChanged();
  235. void PreMainMessageLoopRun();
  236. void PreCreateThreads();
  237. // Stores the supplied |quit_closure|, to be run when the last Browser
  238. // instance is destroyed.
  239. void SetMainMessageLoopQuitClosure(base::OnceClosure quit_closure);
  240. void AddObserver(BrowserObserver* obs) { observers_.AddObserver(obs); }
  241. void RemoveObserver(BrowserObserver* obs) { observers_.RemoveObserver(obs); }
  242. #if BUILDFLAG(IS_MAC)
  243. // Returns whether secure input is enabled
  244. bool IsSecureKeyboardEntryEnabled();
  245. void SetSecureKeyboardEntryEnabled(bool enabled);
  246. #endif
  247. bool is_shutting_down() const { return is_shutdown_; }
  248. bool is_quitting() const { return is_quitting_; }
  249. bool is_ready() const { return is_ready_; }
  250. v8::Local<v8::Value> WhenReady(v8::Isolate* isolate);
  251. protected:
  252. // Returns the version of application bundle or executable file.
  253. std::string GetExecutableFileVersion() const;
  254. // Returns the name of application bundle or executable file.
  255. std::string GetExecutableFileProductName() const;
  256. // Send the will-quit message and then shutdown the application.
  257. void NotifyAndShutdown();
  258. // Send the before-quit message and start closing windows.
  259. bool HandleBeforeQuit();
  260. bool is_quitting_ = false;
  261. private:
  262. // WindowListObserver implementations:
  263. void OnWindowCloseCancelled(NativeWindow* window) override;
  264. void OnWindowAllClosed() override;
  265. // Observers of the browser.
  266. base::ObserverList<BrowserObserver> observers_;
  267. // Tracks tasks requesting file icons.
  268. base::CancelableTaskTracker cancelable_task_tracker_;
  269. // Whether `app.exit()` has been called
  270. bool is_exiting_ = false;
  271. // Whether "ready" event has been emitted.
  272. bool is_ready_ = false;
  273. // The browser is being shutdown.
  274. bool is_shutdown_ = false;
  275. // Null until/unless the default main message loop is running.
  276. base::OnceClosure quit_main_message_loop_;
  277. int badge_count_ = 0;
  278. std::unique_ptr<gin_helper::Promise<void>> ready_promise_;
  279. #if BUILDFLAG(IS_MAC)
  280. std::unique_ptr<ui::ScopedPasswordInputEnabler> password_input_enabler_;
  281. base::Time last_dock_show_;
  282. bool was_launched_at_login_;
  283. #endif
  284. base::Value::Dict about_panel_options_;
  285. #if BUILDFLAG(IS_WIN)
  286. void UpdateBadgeContents(HWND hwnd,
  287. const std::optional<std::string>& badge_content,
  288. const std::string& badge_alt_string);
  289. // In charge of running taskbar related APIs.
  290. TaskbarHost taskbar_host_;
  291. #endif
  292. };
  293. } // namespace electron
  294. #endif // ELECTRON_SHELL_BROWSER_BROWSER_H_