electron_api_app.h 10 KB

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