browser.h 11 KB

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