atom_api_app.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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_API_ATOM_API_APP_H_
  5. #define SHELL_BROWSER_API_ATOM_API_APP_H_
  6. #include <memory>
  7. #include <string>
  8. #include <unordered_map>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/task/cancelable_task_tracker.h"
  12. #include "chrome/browser/icon_manager.h"
  13. #include "chrome/browser/process_singleton.h"
  14. #include "content/public/browser/browser_child_process_observer.h"
  15. #include "content/public/browser/gpu_data_manager_observer.h"
  16. #include "content/public/browser/render_process_host.h"
  17. #include "native_mate/dictionary.h"
  18. #include "native_mate/handle.h"
  19. #include "net/base/completion_once_callback.h"
  20. #include "net/base/completion_repeating_callback.h"
  21. #include "net/ssl/client_cert_identity.h"
  22. #include "shell/browser/api/event_emitter.h"
  23. #include "shell/browser/api/process_metric.h"
  24. #include "shell/browser/atom_browser_client.h"
  25. #include "shell/browser/browser.h"
  26. #include "shell/browser/browser_observer.h"
  27. #include "shell/common/native_mate_converters/callback.h"
  28. #include "shell/common/promise_util.h"
  29. #if defined(USE_NSS_CERTS)
  30. #include "chrome/browser/certificate_manager_model.h"
  31. #endif
  32. namespace base {
  33. class FilePath;
  34. }
  35. namespace mate {
  36. class Arguments;
  37. } // namespace mate
  38. namespace electron {
  39. #if defined(OS_WIN)
  40. enum class JumpListResult : int;
  41. #endif
  42. namespace api {
  43. class App : public AtomBrowserClient::Delegate,
  44. public mate::EventEmitter<App>,
  45. public BrowserObserver,
  46. public content::GpuDataManagerObserver,
  47. public content::BrowserChildProcessObserver {
  48. public:
  49. using FileIconCallback =
  50. base::RepeatingCallback<void(v8::Local<v8::Value>, const gfx::Image&)>;
  51. static mate::Handle<App> Create(v8::Isolate* isolate);
  52. static void BuildPrototype(v8::Isolate* isolate,
  53. v8::Local<v8::FunctionTemplate> prototype);
  54. #if defined(USE_NSS_CERTS)
  55. void OnCertificateManagerModelCreated(
  56. std::unique_ptr<base::DictionaryValue> options,
  57. net::CompletionOnceCallback callback,
  58. std::unique_ptr<CertificateManagerModel> model);
  59. #endif
  60. base::FilePath GetAppPath() const;
  61. void RenderProcessReady(content::RenderProcessHost* host);
  62. void RenderProcessDisconnected(base::ProcessId host_pid);
  63. void PreMainMessageLoopRun();
  64. protected:
  65. explicit App(v8::Isolate* isolate);
  66. ~App() override;
  67. // BrowserObserver:
  68. void OnBeforeQuit(bool* prevent_default) override;
  69. void OnWillQuit(bool* prevent_default) override;
  70. void OnWindowAllClosed() override;
  71. void OnQuit() override;
  72. void OnOpenFile(bool* prevent_default, const std::string& file_path) override;
  73. void OnOpenURL(const std::string& url) override;
  74. void OnActivate(bool has_visible_windows) override;
  75. void OnWillFinishLaunching() override;
  76. void OnFinishLaunching(const base::DictionaryValue& launch_info) override;
  77. void OnAccessibilitySupportChanged() override;
  78. void OnPreMainMessageLoopRun() override;
  79. #if defined(OS_MACOSX)
  80. void OnWillContinueUserActivity(bool* prevent_default,
  81. const std::string& type) override;
  82. void OnDidFailToContinueUserActivity(const std::string& type,
  83. const std::string& error) override;
  84. void OnContinueUserActivity(bool* prevent_default,
  85. const std::string& type,
  86. const base::DictionaryValue& user_info) override;
  87. void OnUserActivityWasContinued(
  88. const std::string& type,
  89. const base::DictionaryValue& user_info) override;
  90. void OnUpdateUserActivityState(
  91. bool* prevent_default,
  92. const std::string& type,
  93. const base::DictionaryValue& user_info) override;
  94. void OnNewWindowForTab() override;
  95. #endif
  96. // content::ContentBrowserClient:
  97. void AllowCertificateError(
  98. content::WebContents* web_contents,
  99. int cert_error,
  100. const net::SSLInfo& ssl_info,
  101. const GURL& request_url,
  102. bool is_main_frame_request,
  103. bool strict_enforcement,
  104. const base::RepeatingCallback<
  105. void(content::CertificateRequestResultType)>& callback) override;
  106. base::OnceClosure SelectClientCertificate(
  107. content::WebContents* web_contents,
  108. net::SSLCertRequestInfo* cert_request_info,
  109. net::ClientCertIdentityList client_certs,
  110. std::unique_ptr<content::ClientCertificateDelegate> delegate) override;
  111. bool CanCreateWindow(content::RenderFrameHost* opener,
  112. const GURL& opener_url,
  113. const GURL& opener_top_level_frame_url,
  114. const url::Origin& source_origin,
  115. content::mojom::WindowContainerType container_type,
  116. const GURL& target_url,
  117. const content::Referrer& referrer,
  118. const std::string& frame_name,
  119. WindowOpenDisposition disposition,
  120. const blink::mojom::WindowFeatures& features,
  121. const std::vector<std::string>& additional_features,
  122. const scoped_refptr<network::ResourceRequestBody>& body,
  123. bool user_gesture,
  124. bool opener_suppressed,
  125. bool* no_javascript_access) override;
  126. // content::GpuDataManagerObserver:
  127. void OnGpuInfoUpdate() override;
  128. void OnGpuProcessCrashed(base::TerminationStatus status) override;
  129. // content::BrowserChildProcessObserver:
  130. void BrowserChildProcessLaunchedAndConnected(
  131. const content::ChildProcessData& data) override;
  132. void BrowserChildProcessHostDisconnected(
  133. const content::ChildProcessData& data) override;
  134. void BrowserChildProcessCrashed(
  135. const content::ChildProcessData& data,
  136. const content::ChildProcessTerminationInfo& info) override;
  137. void BrowserChildProcessKilled(
  138. const content::ChildProcessData& data,
  139. const content::ChildProcessTerminationInfo& info) override;
  140. private:
  141. void SetAppPath(const base::FilePath& app_path);
  142. void ChildProcessLaunched(int process_type, base::ProcessHandle handle);
  143. void ChildProcessDisconnected(base::ProcessId pid);
  144. void SetAppLogsPath(base::Optional<base::FilePath> custom_path,
  145. mate::Arguments* args);
  146. // Get/Set the pre-defined path in PathService.
  147. base::FilePath GetPath(mate::Arguments* args, const std::string& name);
  148. void SetPath(mate::Arguments* args,
  149. const std::string& name,
  150. const base::FilePath& path);
  151. void SetDesktopName(const std::string& desktop_name);
  152. std::string GetLocale();
  153. std::string GetLocaleCountryCode();
  154. void OnSecondInstance(const base::CommandLine::StringVector& cmd,
  155. const base::FilePath& cwd);
  156. bool HasSingleInstanceLock() const;
  157. bool RequestSingleInstanceLock();
  158. void ReleaseSingleInstanceLock();
  159. bool Relaunch(mate::Arguments* args);
  160. void DisableHardwareAcceleration(mate::Arguments* args);
  161. void DisableDomainBlockingFor3DAPIs(mate::Arguments* args);
  162. bool IsAccessibilitySupportEnabled();
  163. void SetAccessibilitySupportEnabled(bool enabled, mate::Arguments* args);
  164. Browser::LoginItemSettings GetLoginItemSettings(mate::Arguments* args);
  165. #if defined(USE_NSS_CERTS)
  166. void ImportCertificate(const base::DictionaryValue& options,
  167. net::CompletionRepeatingCallback callback);
  168. #endif
  169. v8::Local<v8::Promise> GetFileIcon(const base::FilePath& path,
  170. mate::Arguments* args);
  171. std::vector<mate::Dictionary> GetAppMetrics(v8::Isolate* isolate);
  172. v8::Local<v8::Value> GetGPUFeatureStatus(v8::Isolate* isolate);
  173. v8::Local<v8::Promise> GetGPUInfo(v8::Isolate* isolate,
  174. const std::string& info_type);
  175. void EnableSandbox(mate::Arguments* args);
  176. void SetUserAgentFallback(const std::string& user_agent);
  177. std::string GetUserAgentFallback();
  178. void SetBrowserClientCanUseCustomSiteInstance(bool should_disable);
  179. bool CanBrowserClientUseCustomSiteInstance();
  180. #if defined(OS_MACOSX)
  181. bool MoveToApplicationsFolder(mate::Arguments* args);
  182. bool IsInApplicationsFolder();
  183. v8::Local<v8::Value> GetDockAPI(v8::Isolate* isolate);
  184. v8::Global<v8::Value> dock_;
  185. #endif
  186. #if defined(MAS_BUILD)
  187. base::RepeatingCallback<void()> StartAccessingSecurityScopedResource(
  188. mate::Arguments* args);
  189. #endif
  190. #if defined(OS_WIN)
  191. // Get the current Jump List settings.
  192. v8::Local<v8::Value> GetJumpListSettings();
  193. // Set or remove a custom Jump List for the application.
  194. JumpListResult SetJumpList(v8::Local<v8::Value> val, mate::Arguments* args);
  195. #endif // defined(OS_WIN)
  196. std::unique_ptr<ProcessSingleton> process_singleton_;
  197. #if defined(USE_NSS_CERTS)
  198. std::unique_ptr<CertificateManagerModel> certificate_manager_model_;
  199. #endif
  200. // Tracks tasks requesting file icons.
  201. base::CancelableTaskTracker cancelable_task_tracker_;
  202. base::FilePath app_path_;
  203. using ProcessMetricMap =
  204. std::unordered_map<base::ProcessId,
  205. std::unique_ptr<electron::ProcessMetric>>;
  206. ProcessMetricMap app_metrics_;
  207. DISALLOW_COPY_AND_ASSIGN(App);
  208. };
  209. } // namespace api
  210. } // namespace electron
  211. #endif // SHELL_BROWSER_API_ATOM_API_APP_H_