browser.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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_helper/promise.h"
  18. #if BUILDFLAG(IS_WIN)
  19. #include <windows.h>
  20. #include "base/files/file_path.h"
  21. #include "shell/browser/ui/win/taskbar_host.h"
  22. #endif
  23. #if BUILDFLAG(IS_MAC)
  24. #include "ui/base/cocoa/secure_password_input.h"
  25. #endif
  26. namespace base {
  27. class FilePath;
  28. }
  29. namespace gin_helper {
  30. class Arguments;
  31. }
  32. namespace electron {
  33. class ElectronMenuModel;
  34. // This class is used for control application-wide operations.
  35. class Browser : public WindowListObserver {
  36. public:
  37. Browser();
  38. ~Browser() override;
  39. // disable copy
  40. Browser(const Browser&) = delete;
  41. Browser& operator=(const Browser&) = delete;
  42. static Browser* Get();
  43. // Try to close all windows and quit the application.
  44. void Quit();
  45. // Exit the application immediately and set exit code.
  46. void Exit(gin::Arguments* args);
  47. // Cleanup everything and shutdown the application gracefully.
  48. void Shutdown();
  49. // Focus the application.
  50. void Focus(gin::Arguments* args);
  51. // Returns the version of the executable (or bundle).
  52. std::string GetVersion() const;
  53. // Overrides the application version.
  54. void SetVersion(const std::string& version);
  55. // Returns the application's name, default is just Electron.
  56. std::string GetName() const;
  57. // Overrides the application name.
  58. void SetName(const std::string& name);
  59. // Add the |path| to recent documents list.
  60. void AddRecentDocument(const base::FilePath& path);
  61. // Clear the recent documents list.
  62. void ClearRecentDocuments();
  63. #if BUILDFLAG(IS_WIN)
  64. // Set the application user model ID.
  65. void SetAppUserModelID(const std::wstring& name);
  66. #endif
  67. // Remove the default protocol handler registry key
  68. bool RemoveAsDefaultProtocolClient(const std::string& protocol,
  69. gin::Arguments* args);
  70. // Set as default handler for a protocol.
  71. bool SetAsDefaultProtocolClient(const std::string& protocol,
  72. gin::Arguments* args);
  73. // Query the current state of default handler for a protocol.
  74. bool IsDefaultProtocolClient(const std::string& protocol,
  75. gin::Arguments* args);
  76. std::u16string GetApplicationNameForProtocol(const GURL& url);
  77. #if !BUILDFLAG(IS_LINUX)
  78. // get the name, icon and path for an application
  79. v8::Local<v8::Promise> GetApplicationInfoForProtocol(v8::Isolate* isolate,
  80. const GURL& url);
  81. #endif
  82. // Set/Get the badge count.
  83. bool SetBadgeCount(std::optional<int> count);
  84. [[nodiscard]] int badge_count() const { return badge_count_; }
  85. #if BUILDFLAG(IS_WIN)
  86. struct LaunchItem {
  87. std::wstring name;
  88. std::wstring path;
  89. std::wstring scope;
  90. std::vector<std::wstring> args;
  91. bool enabled = true;
  92. LaunchItem();
  93. ~LaunchItem();
  94. LaunchItem(const LaunchItem&);
  95. };
  96. #endif
  97. // Set/Get the login item settings of the app
  98. struct LoginItemSettings {
  99. bool open_at_login = false;
  100. bool open_as_hidden = false;
  101. bool restore_state = false;
  102. bool opened_at_login = false;
  103. bool opened_as_hidden = false;
  104. std::u16string path;
  105. std::vector<std::u16string> args;
  106. #if BUILDFLAG(IS_MAC)
  107. std::string type = "mainAppService";
  108. std::string service_name;
  109. std::string status;
  110. #elif BUILDFLAG(IS_WIN)
  111. // used in browser::setLoginItemSettings
  112. bool enabled = true;
  113. std::wstring name;
  114. // used in browser::getLoginItemSettings
  115. bool executable_will_launch_at_login = false;
  116. std::vector<LaunchItem> launch_items;
  117. #endif
  118. LoginItemSettings();
  119. ~LoginItemSettings();
  120. LoginItemSettings(const LoginItemSettings&);
  121. };
  122. void SetLoginItemSettings(LoginItemSettings settings);
  123. LoginItemSettings 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. #endif // BUILDFLAG(IS_MAC)
  183. void ShowAboutPanel();
  184. void SetAboutPanelOptions(base::Value::Dict options);
  185. #if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
  186. void ShowEmojiPanel();
  187. #endif
  188. #if BUILDFLAG(IS_WIN)
  189. struct UserTask {
  190. base::FilePath program;
  191. std::wstring arguments;
  192. std::wstring title;
  193. std::wstring description;
  194. base::FilePath working_dir;
  195. base::FilePath icon_path;
  196. int icon_index;
  197. UserTask();
  198. UserTask(const UserTask&);
  199. ~UserTask();
  200. };
  201. // Add a custom task to jump list.
  202. bool SetUserTasks(const std::vector<UserTask>& tasks);
  203. // Returns the application user model ID, if there isn't one, then create
  204. // one from app's name.
  205. // The returned string managed by Browser, and should not be modified.
  206. PCWSTR GetAppUserModelID();
  207. #endif // BUILDFLAG(IS_WIN)
  208. #if BUILDFLAG(IS_LINUX)
  209. // Whether Unity launcher is running.
  210. bool IsUnityRunning();
  211. #endif // BUILDFLAG(IS_LINUX)
  212. // Tell the application to open a file.
  213. bool OpenFile(const std::string& file_path);
  214. // Tell the application to open a url.
  215. void OpenURL(const std::string& url);
  216. #if BUILDFLAG(IS_MAC)
  217. // Tell the application to create a new window for a tab.
  218. void NewWindowForTab();
  219. // Indicate that the app is now active.
  220. void DidBecomeActive();
  221. // Indicate that the app is no longer active and doesn’t have focus.
  222. void DidResignActive();
  223. #endif // BUILDFLAG(IS_MAC)
  224. // Tell the application that application is activated with visible/invisible
  225. // windows.
  226. void Activate(bool has_visible_windows);
  227. bool IsEmojiPanelSupported();
  228. // Tell the application the loading has been done.
  229. void WillFinishLaunching();
  230. void DidFinishLaunching(base::Value::Dict launch_info);
  231. void OnAccessibilitySupportChanged();
  232. void PreMainMessageLoopRun();
  233. void PreCreateThreads();
  234. // Stores the supplied |quit_closure|, to be run when the last Browser
  235. // instance is destroyed.
  236. void SetMainMessageLoopQuitClosure(base::OnceClosure quit_closure);
  237. void AddObserver(BrowserObserver* obs) { observers_.AddObserver(obs); }
  238. void RemoveObserver(BrowserObserver* obs) { observers_.RemoveObserver(obs); }
  239. #if BUILDFLAG(IS_MAC)
  240. // Returns whether secure input is enabled
  241. bool IsSecureKeyboardEntryEnabled();
  242. void SetSecureKeyboardEntryEnabled(bool enabled);
  243. #endif
  244. bool is_shutting_down() const { return is_shutdown_; }
  245. bool is_quitting() const { return is_quitting_; }
  246. bool is_ready() const { return is_ready_; }
  247. v8::Local<v8::Value> WhenReady(v8::Isolate* isolate);
  248. protected:
  249. // Returns the version of application bundle or executable file.
  250. std::string GetExecutableFileVersion() const;
  251. // Returns the name of application bundle or executable file.
  252. std::string GetExecutableFileProductName() const;
  253. // Send the will-quit message and then shutdown the application.
  254. void NotifyAndShutdown();
  255. // Send the before-quit message and start closing windows.
  256. bool HandleBeforeQuit();
  257. bool is_quitting_ = false;
  258. private:
  259. // WindowListObserver implementations:
  260. void OnWindowCloseCancelled(NativeWindow* window) override;
  261. void OnWindowAllClosed() override;
  262. // Observers of the browser.
  263. base::ObserverList<BrowserObserver> observers_;
  264. // Tracks tasks requesting file icons.
  265. base::CancelableTaskTracker cancelable_task_tracker_;
  266. // Whether `app.exit()` has been called
  267. bool is_exiting_ = false;
  268. // Whether "ready" event has been emitted.
  269. bool is_ready_ = false;
  270. // The browser is being shutdown.
  271. bool is_shutdown_ = false;
  272. // Null until/unless the default main message loop is running.
  273. base::OnceClosure quit_main_message_loop_;
  274. int badge_count_ = 0;
  275. std::unique_ptr<gin_helper::Promise<void>> ready_promise_;
  276. #if BUILDFLAG(IS_MAC)
  277. std::unique_ptr<ui::ScopedPasswordInputEnabler> password_input_enabler_;
  278. base::Time last_dock_show_;
  279. #endif
  280. base::Value::Dict about_panel_options_;
  281. #if BUILDFLAG(IS_WIN)
  282. void UpdateBadgeContents(HWND hwnd,
  283. const std::optional<std::string>& badge_content,
  284. const std::string& badge_alt_string);
  285. // In charge of running taskbar related APIs.
  286. TaskbarHost taskbar_host_;
  287. #endif
  288. };
  289. } // namespace electron
  290. #endif // ELECTRON_SHELL_BROWSER_BROWSER_H_