browser.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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();
  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. };
  76. void SetLoginItemSettings(LoginItemSettings settings);
  77. LoginItemSettings GetLoginItemSettings();
  78. #if defined(OS_MACOSX)
  79. // Hide the application.
  80. void Hide();
  81. // Show the application.
  82. void Show();
  83. // Creates an activity and sets it as the one currently in use.
  84. void SetUserActivity(const std::string& type,
  85. const base::DictionaryValue& user_info,
  86. mate::Arguments* args);
  87. // Returns the type name of the current user activity.
  88. std::string GetCurrentActivityType();
  89. // Resumes an activity via hand-off.
  90. bool ContinueUserActivity(const std::string& type,
  91. const base::DictionaryValue& user_info);
  92. // Bounce the dock icon.
  93. enum BounceType {
  94. BOUNCE_CRITICAL = 0,
  95. BOUNCE_INFORMATIONAL = 10,
  96. };
  97. int DockBounce(BounceType type);
  98. void DockCancelBounce(int request_id);
  99. // Bounce the Downloads stack.
  100. void DockDownloadFinished(const std::string& filePath);
  101. // Set/Get dock's badge text.
  102. void DockSetBadgeText(const std::string& label);
  103. std::string DockGetBadgeText();
  104. // Hide/Show dock.
  105. void DockHide();
  106. void DockShow();
  107. bool DockIsVisible();
  108. // Set docks' menu.
  109. void DockSetMenu(AtomMenuModel* model);
  110. // Set docks' icon.
  111. void DockSetIcon(const gfx::Image& image);
  112. void ShowAboutPanel();
  113. void SetAboutPanelOptions(const base::DictionaryValue& options);
  114. #endif // defined(OS_MACOSX)
  115. #if defined(OS_WIN)
  116. struct UserTask {
  117. base::FilePath program;
  118. base::string16 arguments;
  119. base::string16 title;
  120. base::string16 description;
  121. base::FilePath icon_path;
  122. int icon_index;
  123. };
  124. // Add a custom task to jump list.
  125. bool SetUserTasks(const std::vector<UserTask>& tasks);
  126. // Returns the application user model ID, if there isn't one, then create
  127. // one from app's name.
  128. // The returned string managed by Browser, and should not be modified.
  129. PCWSTR GetAppUserModelID();
  130. #endif // defined(OS_WIN)
  131. #if defined(OS_LINUX)
  132. // Whether Unity launcher is running.
  133. bool IsUnityRunning();
  134. #endif // defined(OS_LINUX)
  135. // Tell the application to open a file.
  136. bool OpenFile(const std::string& file_path);
  137. // Tell the application to open a url.
  138. void OpenURL(const std::string& url);
  139. // Tell the application that application is activated with visible/invisible
  140. // windows.
  141. void Activate(bool has_visible_windows);
  142. // Tell the application the loading has been done.
  143. void WillFinishLaunching();
  144. void DidFinishLaunching(const base::DictionaryValue& launch_info);
  145. void OnAccessibilitySupportChanged();
  146. // Request basic auth login.
  147. void RequestLogin(LoginHandler* login_handler,
  148. std::unique_ptr<base::DictionaryValue> request_details);
  149. void AddObserver(BrowserObserver* obs) {
  150. observers_.AddObserver(obs);
  151. }
  152. void RemoveObserver(BrowserObserver* obs) {
  153. observers_.RemoveObserver(obs);
  154. }
  155. bool is_shutting_down() const { return is_shutdown_; }
  156. bool is_quiting() const { return is_quiting_; }
  157. bool is_ready() const { return is_ready_; }
  158. protected:
  159. // Returns the version of application bundle or executable file.
  160. std::string GetExecutableFileVersion() const;
  161. // Returns the name of application bundle or executable file.
  162. std::string GetExecutableFileProductName() const;
  163. // Send the will-quit message and then shutdown the application.
  164. void NotifyAndShutdown();
  165. // Send the before-quit message and start closing windows.
  166. bool HandleBeforeQuit();
  167. bool is_quiting_;
  168. private:
  169. // WindowListObserver implementations:
  170. void OnWindowCloseCancelled(NativeWindow* window) override;
  171. void OnWindowAllClosed() override;
  172. // Observers of the browser.
  173. base::ObserverList<BrowserObserver> observers_;
  174. // Whether `app.exit()` has been called
  175. bool is_exiting_;
  176. // Whether "ready" event has been emitted.
  177. bool is_ready_;
  178. // The browser is being shutdown.
  179. bool is_shutdown_;
  180. std::string version_override_;
  181. std::string name_override_;
  182. int badge_count_ = 0;
  183. #if defined(OS_WIN)
  184. base::string16 app_user_model_id_;
  185. #endif
  186. #if defined(OS_MACOSX)
  187. base::DictionaryValue about_panel_options_;
  188. #endif
  189. DISALLOW_COPY_AND_ASSIGN(Browser);
  190. };
  191. } // namespace atom
  192. #endif // ATOM_BROWSER_BROWSER_H_