electron_api_app.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 ELECTRON_SHELL_BROWSER_API_ELECTRON_API_APP_H_
  5. #define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_APP_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/task/cancelable_task_tracker.h"
  11. #include "chrome/browser/icon_manager.h"
  12. #include "chrome/browser/process_singleton.h"
  13. #include "content/public/browser/browser_child_process_observer.h"
  14. #include "content/public/browser/gpu_data_manager_observer.h"
  15. #include "content/public/browser/render_process_host.h"
  16. #include "crypto/crypto_buildflags.h"
  17. #include "gin/handle.h"
  18. #include "net/base/completion_once_callback.h"
  19. #include "net/base/completion_repeating_callback.h"
  20. #include "net/ssl/client_cert_identity.h"
  21. #include "shell/browser/api/process_metric.h"
  22. #include "shell/browser/browser.h"
  23. #include "shell/browser/browser_observer.h"
  24. #include "shell/browser/electron_browser_client.h"
  25. #include "shell/browser/event_emitter_mixin.h"
  26. #include "shell/common/gin_helper/dictionary.h"
  27. #include "shell/common/gin_helper/error_thrower.h"
  28. #include "shell/common/gin_helper/promise.h"
  29. #if BUILDFLAG(USE_NSS_CERTS)
  30. #include "shell/browser/certificate_manager_model.h"
  31. #endif
  32. namespace base {
  33. class FilePath;
  34. }
  35. namespace electron {
  36. #if BUILDFLAG(IS_WIN)
  37. enum class JumpListResult : int;
  38. #endif
  39. namespace api {
  40. class App : public ElectronBrowserClient::Delegate,
  41. public gin::Wrappable<App>,
  42. public gin_helper::EventEmitterMixin<App>,
  43. public BrowserObserver,
  44. public content::GpuDataManagerObserver,
  45. public content::BrowserChildProcessObserver {
  46. public:
  47. using FileIconCallback =
  48. base::RepeatingCallback<void(v8::Local<v8::Value>, const gfx::Image&)>;
  49. static gin::Handle<App> Create(v8::Isolate* isolate);
  50. static App* Get();
  51. // gin::Wrappable
  52. static gin::WrapperInfo kWrapperInfo;
  53. gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
  54. v8::Isolate* isolate) override;
  55. const char* GetTypeName() override;
  56. #if BUILDFLAG(USE_NSS_CERTS)
  57. void OnCertificateManagerModelCreated(
  58. base::Value options,
  59. net::CompletionOnceCallback callback,
  60. std::unique_ptr<CertificateManagerModel> model);
  61. #endif
  62. base::FilePath GetAppPath() const;
  63. void RenderProcessReady(content::RenderProcessHost* host);
  64. void RenderProcessExited(content::RenderProcessHost* host);
  65. static bool IsPackaged();
  66. App();
  67. // disable copy
  68. App(const App&) = delete;
  69. App& operator=(const App&) = delete;
  70. private:
  71. ~App() override;
  72. // BrowserObserver:
  73. void OnBeforeQuit(bool* prevent_default) override;
  74. void OnWillQuit(bool* prevent_default) override;
  75. void OnWindowAllClosed() override;
  76. void OnQuit() override;
  77. void OnOpenFile(bool* prevent_default, const std::string& file_path) override;
  78. void OnOpenURL(const std::string& url) override;
  79. void OnActivate(bool has_visible_windows) override;
  80. void OnWillFinishLaunching() override;
  81. void OnFinishLaunching(base::Value::Dict launch_info) override;
  82. void OnAccessibilitySupportChanged() override;
  83. void OnPreMainMessageLoopRun() override;
  84. void OnPreCreateThreads() override;
  85. #if BUILDFLAG(IS_MAC)
  86. void OnWillContinueUserActivity(bool* prevent_default,
  87. const std::string& type) override;
  88. void OnDidFailToContinueUserActivity(const std::string& type,
  89. const std::string& error) override;
  90. void OnContinueUserActivity(bool* prevent_default,
  91. const std::string& type,
  92. base::Value::Dict user_info,
  93. base::Value::Dict details) override;
  94. void OnUserActivityWasContinued(const std::string& type,
  95. base::Value::Dict user_info) override;
  96. void OnUpdateUserActivityState(bool* prevent_default,
  97. const std::string& type,
  98. base::Value::Dict user_info) override;
  99. void OnNewWindowForTab() override;
  100. void OnDidBecomeActive() override;
  101. void OnDidResignActive() override;
  102. #endif
  103. // content::ContentBrowserClient:
  104. void AllowCertificateError(
  105. content::WebContents* web_contents,
  106. int cert_error,
  107. const net::SSLInfo& ssl_info,
  108. const GURL& request_url,
  109. bool is_main_frame_request,
  110. bool strict_enforcement,
  111. base::OnceCallback<void(content::CertificateRequestResultType)> callback)
  112. override;
  113. base::OnceClosure SelectClientCertificate(
  114. content::BrowserContext* browser_context,
  115. content::WebContents* web_contents,
  116. net::SSLCertRequestInfo* cert_request_info,
  117. net::ClientCertIdentityList identities,
  118. std::unique_ptr<content::ClientCertificateDelegate> delegate) override;
  119. bool CanCreateWindow(content::RenderFrameHost* opener,
  120. const GURL& opener_url,
  121. const GURL& opener_top_level_frame_url,
  122. const url::Origin& source_origin,
  123. content::mojom::WindowContainerType container_type,
  124. const GURL& target_url,
  125. const content::Referrer& referrer,
  126. const std::string& frame_name,
  127. WindowOpenDisposition disposition,
  128. const blink::mojom::WindowFeatures& features,
  129. const std::string& raw_features,
  130. const scoped_refptr<network::ResourceRequestBody>& body,
  131. bool user_gesture,
  132. bool opener_suppressed,
  133. bool* no_javascript_access) override;
  134. // content::GpuDataManagerObserver:
  135. void OnGpuInfoUpdate() override;
  136. void OnGpuProcessCrashed() override;
  137. // content::BrowserChildProcessObserver:
  138. void BrowserChildProcessLaunchedAndConnected(
  139. const content::ChildProcessData& data) override;
  140. void BrowserChildProcessHostDisconnected(
  141. const content::ChildProcessData& data) override;
  142. void BrowserChildProcessCrashed(
  143. const content::ChildProcessData& data,
  144. const content::ChildProcessTerminationInfo& info) override;
  145. void BrowserChildProcessKilled(
  146. const content::ChildProcessData& data,
  147. const content::ChildProcessTerminationInfo& info) override;
  148. private:
  149. void BrowserChildProcessCrashedOrKilled(
  150. const content::ChildProcessData& data,
  151. const content::ChildProcessTerminationInfo& info);
  152. void SetAppPath(const base::FilePath& app_path);
  153. void ChildProcessLaunched(int process_type,
  154. int pid,
  155. base::ProcessHandle handle,
  156. const std::string& service_name = std::string(),
  157. const std::string& name = std::string());
  158. void ChildProcessDisconnected(int pid);
  159. void SetAppLogsPath(gin_helper::ErrorThrower thrower,
  160. absl::optional<base::FilePath> custom_path);
  161. // Get/Set the pre-defined path in PathService.
  162. base::FilePath GetPath(gin_helper::ErrorThrower thrower,
  163. const std::string& name);
  164. void SetPath(gin_helper::ErrorThrower thrower,
  165. const std::string& name,
  166. const base::FilePath& path);
  167. void SetDesktopName(const std::string& desktop_name);
  168. std::string GetLocale();
  169. std::string GetLocaleCountryCode();
  170. std::string GetSystemLocale(gin_helper::ErrorThrower thrower) const;
  171. void OnSecondInstance(const base::CommandLine& cmd,
  172. const base::FilePath& cwd,
  173. const std::vector<const uint8_t> additional_data);
  174. bool HasSingleInstanceLock() const;
  175. bool RequestSingleInstanceLock(gin::Arguments* args);
  176. void ReleaseSingleInstanceLock();
  177. bool Relaunch(gin::Arguments* args);
  178. void DisableHardwareAcceleration(gin_helper::ErrorThrower thrower);
  179. void DisableDomainBlockingFor3DAPIs(gin_helper::ErrorThrower thrower);
  180. bool IsAccessibilitySupportEnabled();
  181. void SetAccessibilitySupportEnabled(gin_helper::ErrorThrower thrower,
  182. bool enabled);
  183. Browser::LoginItemSettings GetLoginItemSettings(gin::Arguments* args);
  184. #if BUILDFLAG(USE_NSS_CERTS)
  185. void ImportCertificate(gin_helper::ErrorThrower thrower,
  186. base::Value options,
  187. net::CompletionOnceCallback callback);
  188. #endif
  189. v8::Local<v8::Promise> GetFileIcon(const base::FilePath& path,
  190. gin::Arguments* args);
  191. std::vector<gin_helper::Dictionary> GetAppMetrics(v8::Isolate* isolate);
  192. v8::Local<v8::Value> GetGPUFeatureStatus(v8::Isolate* isolate);
  193. v8::Local<v8::Promise> GetGPUInfo(v8::Isolate* isolate,
  194. const std::string& info_type);
  195. void EnableSandbox(gin_helper::ErrorThrower thrower);
  196. void SetUserAgentFallback(const std::string& user_agent);
  197. std::string GetUserAgentFallback();
  198. #if BUILDFLAG(IS_MAC)
  199. void SetActivationPolicy(gin_helper::ErrorThrower thrower,
  200. const std::string& policy);
  201. bool MoveToApplicationsFolder(gin_helper::ErrorThrower, gin::Arguments* args);
  202. bool IsInApplicationsFolder();
  203. v8::Local<v8::Value> GetDockAPI(v8::Isolate* isolate);
  204. bool IsRunningUnderRosettaTranslation() const;
  205. v8::Global<v8::Value> dock_;
  206. #endif
  207. #if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
  208. bool IsRunningUnderARM64Translation() const;
  209. #endif
  210. #if IS_MAS_BUILD()
  211. base::RepeatingCallback<void()> StartAccessingSecurityScopedResource(
  212. gin::Arguments* args);
  213. #endif
  214. #if BUILDFLAG(IS_WIN)
  215. // Get the current Jump List settings.
  216. v8::Local<v8::Value> GetJumpListSettings();
  217. // Set or remove a custom Jump List for the application.
  218. JumpListResult SetJumpList(v8::Local<v8::Value> val, gin::Arguments* args);
  219. #endif // BUILDFLAG(IS_WIN)
  220. std::unique_ptr<ProcessSingleton> process_singleton_;
  221. #if BUILDFLAG(USE_NSS_CERTS)
  222. std::unique_ptr<CertificateManagerModel> certificate_manager_model_;
  223. #endif
  224. // Tracks tasks requesting file icons.
  225. base::CancelableTaskTracker cancelable_task_tracker_;
  226. base::FilePath app_path_;
  227. using ProcessMetricMap =
  228. std::map<int, std::unique_ptr<electron::ProcessMetric>>;
  229. ProcessMetricMap app_metrics_;
  230. bool disable_hw_acceleration_ = false;
  231. bool disable_domain_blocking_for_3DAPIs_ = false;
  232. bool watch_singleton_socket_on_ready_ = false;
  233. };
  234. } // namespace api
  235. } // namespace electron
  236. #endif // ELECTRON_SHELL_BROWSER_API_ELECTRON_API_APP_H_