browser.h 10 KB

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