browser.h 9.3 KB

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