browser.h 8.0 KB

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