browser.h 8.8 KB

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