browser.h 8.9 KB

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