browser.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 "native_mate/arguments.h"
  15. #include "shell/browser/browser_observer.h"
  16. #include "shell/browser/window_list_observer.h"
  17. #include "shell/common/promise_util.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 electron {
  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 an activity and marks it as no longer eligible for
  99. // continuation
  100. void InvalidateCurrentActivity();
  101. // Marks this activity object as inactive without invalidating it.
  102. void ResignCurrentActivity();
  103. // Updates the current user activity
  104. void UpdateCurrentActivity(const std::string& type,
  105. const base::DictionaryValue& user_info);
  106. // Indicates that an user activity is about to be resumed.
  107. bool WillContinueUserActivity(const std::string& type);
  108. // Indicates a failure to resume a Handoff activity.
  109. void DidFailToContinueUserActivity(const std::string& type,
  110. const std::string& error);
  111. // Resumes an activity via hand-off.
  112. bool ContinueUserActivity(const std::string& type,
  113. const base::DictionaryValue& user_info);
  114. // Indicates that an activity was continued on another device.
  115. void UserActivityWasContinued(const std::string& type,
  116. const base::DictionaryValue& user_info);
  117. // Gives an oportunity to update the Handoff payload.
  118. bool UpdateUserActivityState(const std::string& type,
  119. const base::DictionaryValue& user_info);
  120. // Bounce the dock icon.
  121. enum class BounceType{
  122. CRITICAL = 0, // NSCriticalRequest
  123. INFORMATIONAL = 10, // NSInformationalRequest
  124. };
  125. int DockBounce(BounceType type);
  126. void DockCancelBounce(int request_id);
  127. // Bounce the Downloads stack.
  128. void DockDownloadFinished(const std::string& filePath);
  129. // Set/Get dock's badge text.
  130. void DockSetBadgeText(const std::string& label);
  131. std::string DockGetBadgeText();
  132. // Hide/Show dock.
  133. void DockHide();
  134. v8::Local<v8::Promise> DockShow(v8::Isolate* isolate);
  135. bool DockIsVisible();
  136. // Set docks' menu.
  137. void DockSetMenu(AtomMenuModel* model);
  138. // Set docks' icon.
  139. void DockSetIcon(const gfx::Image& image);
  140. #endif // defined(OS_MACOSX)
  141. #if defined(OS_MACOSX) || defined(OS_LINUX)
  142. void ShowAboutPanel();
  143. void SetAboutPanelOptions(const base::DictionaryValue& options);
  144. #endif
  145. #if defined(OS_MACOSX) || defined(OS_WIN)
  146. void ShowEmojiPanel();
  147. #endif
  148. #if defined(OS_WIN)
  149. struct UserTask {
  150. base::FilePath program;
  151. base::string16 arguments;
  152. base::string16 title;
  153. base::string16 description;
  154. base::FilePath working_dir;
  155. base::FilePath icon_path;
  156. int icon_index;
  157. UserTask();
  158. UserTask(const UserTask&);
  159. ~UserTask();
  160. };
  161. // Add a custom task to jump list.
  162. bool SetUserTasks(const std::vector<UserTask>& tasks);
  163. // Returns the application user model ID, if there isn't one, then create
  164. // one from app's name.
  165. // The returned string managed by Browser, and should not be modified.
  166. PCWSTR GetAppUserModelID();
  167. #endif // defined(OS_WIN)
  168. #if defined(OS_LINUX)
  169. // Whether Unity launcher is running.
  170. bool IsUnityRunning();
  171. #endif // defined(OS_LINUX)
  172. // Tell the application to open a file.
  173. bool OpenFile(const std::string& file_path);
  174. // Tell the application to open a url.
  175. void OpenURL(const std::string& url);
  176. #if defined(OS_MACOSX)
  177. // Tell the application to create a new window for a tab.
  178. void NewWindowForTab();
  179. #endif // defined(OS_MACOSX)
  180. // Tell the application that application is activated with visible/invisible
  181. // windows.
  182. void Activate(bool has_visible_windows);
  183. bool IsEmojiPanelSupported();
  184. // Tell the application the loading has been done.
  185. void WillFinishLaunching();
  186. void DidFinishLaunching(const base::DictionaryValue& launch_info);
  187. void OnAccessibilitySupportChanged();
  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)
  225. base::Value about_panel_options_;
  226. #elif defined(OS_MACOSX)
  227. base::DictionaryValue about_panel_options_;
  228. #endif
  229. DISALLOW_COPY_AND_ASSIGN(Browser);
  230. };
  231. } // namespace electron
  232. #endif // SHELL_BROWSER_BROWSER_H_