browser.h 11 KB

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