browser.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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_MAC)
  106. std::string type = "mainAppService";
  107. std::string service_name;
  108. std::string status;
  109. #elif BUILDFLAG(IS_WIN)
  110. // used in browser::setLoginItemSettings
  111. bool enabled = true;
  112. std::wstring name;
  113. // used in browser::getLoginItemSettings
  114. bool executable_will_launch_at_login = false;
  115. std::vector<LaunchItem> launch_items;
  116. #endif
  117. LoginItemSettings();
  118. ~LoginItemSettings();
  119. LoginItemSettings(const LoginItemSettings&);
  120. };
  121. void SetLoginItemSettings(LoginItemSettings settings);
  122. LoginItemSettings GetLoginItemSettings(const LoginItemSettings& options);
  123. #if BUILDFLAG(IS_MAC)
  124. // Set the handler which decides whether to shutdown.
  125. void SetShutdownHandler(base::RepeatingCallback<bool()> handler);
  126. // Hide the application.
  127. void Hide();
  128. bool IsHidden();
  129. // Show the application.
  130. void Show();
  131. // Creates an activity and sets it as the one currently in use.
  132. void SetUserActivity(const std::string& type,
  133. base::Value::Dict user_info,
  134. gin::Arguments* args);
  135. // Returns the type name of the current user activity.
  136. std::string GetCurrentActivityType();
  137. // Invalidates an activity and marks it as no longer eligible for
  138. // continuation
  139. void InvalidateCurrentActivity();
  140. // Marks this activity object as inactive without invalidating it.
  141. void ResignCurrentActivity();
  142. // Updates the current user activity
  143. void UpdateCurrentActivity(const std::string& type,
  144. base::Value::Dict user_info);
  145. // Indicates that an user activity is about to be resumed.
  146. bool WillContinueUserActivity(const std::string& type);
  147. // Indicates a failure to resume a Handoff activity.
  148. void DidFailToContinueUserActivity(const std::string& type,
  149. const std::string& error);
  150. // Resumes an activity via hand-off.
  151. bool ContinueUserActivity(const std::string& type,
  152. base::Value::Dict user_info,
  153. base::Value::Dict details);
  154. // Indicates that an activity was continued on another device.
  155. void UserActivityWasContinued(const std::string& type,
  156. base::Value::Dict user_info);
  157. // Gives an opportunity to update the Handoff payload.
  158. bool UpdateUserActivityState(const std::string& type,
  159. base::Value::Dict user_info);
  160. void ApplyForcedRTL();
  161. // Bounce the dock icon.
  162. enum class BounceType{
  163. kCritical = 0, // NSCriticalRequest
  164. kInformational = 10, // NSInformationalRequest
  165. };
  166. int DockBounce(BounceType type);
  167. void DockCancelBounce(int request_id);
  168. // Bounce the Downloads stack.
  169. void DockDownloadFinished(const std::string& filePath);
  170. // Set/Get dock's badge text.
  171. void DockSetBadgeText(const std::string& label);
  172. std::string DockGetBadgeText();
  173. // Hide/Show dock.
  174. void DockHide();
  175. v8::Local<v8::Promise> DockShow(v8::Isolate* isolate);
  176. bool DockIsVisible();
  177. // Set docks' menu.
  178. void DockSetMenu(ElectronMenuModel* model);
  179. // Set docks' icon.
  180. void DockSetIcon(v8::Isolate* isolate, v8::Local<v8::Value> icon);
  181. #endif // BUILDFLAG(IS_MAC)
  182. void ShowAboutPanel();
  183. void SetAboutPanelOptions(base::Value::Dict options);
  184. #if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
  185. void ShowEmojiPanel();
  186. #endif
  187. #if BUILDFLAG(IS_WIN)
  188. struct UserTask {
  189. base::FilePath program;
  190. std::wstring arguments;
  191. std::wstring title;
  192. std::wstring description;
  193. base::FilePath working_dir;
  194. base::FilePath icon_path;
  195. int icon_index;
  196. UserTask();
  197. UserTask(const UserTask&);
  198. ~UserTask();
  199. };
  200. // Add a custom task to jump list.
  201. bool SetUserTasks(const std::vector<UserTask>& tasks);
  202. // Returns the application user model ID, if there isn't one, then create
  203. // one from app's name.
  204. // The returned string managed by Browser, and should not be modified.
  205. PCWSTR GetAppUserModelID();
  206. #endif // BUILDFLAG(IS_WIN)
  207. #if BUILDFLAG(IS_LINUX)
  208. // Whether Unity launcher is running.
  209. bool IsUnityRunning();
  210. #endif // BUILDFLAG(IS_LINUX)
  211. // Tell the application to open a file.
  212. bool OpenFile(const std::string& file_path);
  213. // Tell the application to open a url.
  214. void OpenURL(const std::string& url);
  215. #if BUILDFLAG(IS_MAC)
  216. // Tell the application to create a new window for a tab.
  217. void NewWindowForTab();
  218. // Indicate that the app is now active.
  219. void DidBecomeActive();
  220. // Indicate that the app is no longer active and doesn’t have focus.
  221. void DidResignActive();
  222. #endif // BUILDFLAG(IS_MAC)
  223. // Tell the application that application is activated with visible/invisible
  224. // windows.
  225. void Activate(bool has_visible_windows);
  226. bool IsEmojiPanelSupported();
  227. // Tell the application the loading has been done.
  228. void WillFinishLaunching();
  229. void DidFinishLaunching(base::Value::Dict launch_info);
  230. void OnAccessibilitySupportChanged();
  231. void PreMainMessageLoopRun();
  232. void PreCreateThreads();
  233. // Stores the supplied |quit_closure|, to be run when the last Browser
  234. // instance is destroyed.
  235. void SetMainMessageLoopQuitClosure(base::OnceClosure quit_closure);
  236. void AddObserver(BrowserObserver* obs) { observers_.AddObserver(obs); }
  237. void RemoveObserver(BrowserObserver* obs) { observers_.RemoveObserver(obs); }
  238. #if BUILDFLAG(IS_MAC)
  239. // Returns whether secure input is enabled
  240. bool IsSecureKeyboardEntryEnabled();
  241. void SetSecureKeyboardEntryEnabled(bool enabled);
  242. #endif
  243. bool is_shutting_down() const { return is_shutdown_; }
  244. bool is_quitting() const { return is_quitting_; }
  245. bool is_ready() const { return is_ready_; }
  246. v8::Local<v8::Value> WhenReady(v8::Isolate* isolate);
  247. protected:
  248. // Returns the version of application bundle or executable file.
  249. std::string GetExecutableFileVersion() const;
  250. // Returns the name of application bundle or executable file.
  251. std::string GetExecutableFileProductName() const;
  252. // Send the will-quit message and then shutdown the application.
  253. void NotifyAndShutdown();
  254. // Send the before-quit message and start closing windows.
  255. bool HandleBeforeQuit();
  256. bool is_quitting_ = false;
  257. private:
  258. // WindowListObserver implementations:
  259. void OnWindowCloseCancelled(NativeWindow* window) override;
  260. void OnWindowAllClosed() override;
  261. // Observers of the browser.
  262. base::ObserverList<BrowserObserver> observers_;
  263. // Tracks tasks requesting file icons.
  264. base::CancelableTaskTracker cancelable_task_tracker_;
  265. // Whether `app.exit()` has been called
  266. bool is_exiting_ = false;
  267. // Whether "ready" event has been emitted.
  268. bool is_ready_ = false;
  269. // The browser is being shutdown.
  270. bool is_shutdown_ = false;
  271. // Null until/unless the default main message loop is running.
  272. base::OnceClosure quit_main_message_loop_;
  273. int badge_count_ = 0;
  274. std::unique_ptr<gin_helper::Promise<void>> ready_promise_;
  275. #if BUILDFLAG(IS_MAC)
  276. std::unique_ptr<ui::ScopedPasswordInputEnabler> password_input_enabler_;
  277. base::Time last_dock_show_;
  278. #endif
  279. base::Value::Dict about_panel_options_;
  280. #if BUILDFLAG(IS_WIN)
  281. void UpdateBadgeContents(HWND hwnd,
  282. const absl::optional<std::string>& badge_content,
  283. const std::string& badge_alt_string);
  284. // In charge of running taskbar related APIs.
  285. TaskbarHost taskbar_host_;
  286. #endif
  287. };
  288. } // namespace electron
  289. #endif // ELECTRON_SHELL_BROWSER_BROWSER_H_