browser.h 6.9 KB

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