electron_api_session.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright (c) 2015 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_SESSION_H_
  5. #define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_SESSION_H_
  6. #include <optional>
  7. #include <string>
  8. #include <vector>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/raw_ref.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/values.h"
  13. #include "content/public/browser/download_manager.h"
  14. #include "electron/buildflags/buildflags.h"
  15. #include "gin/wrappable.h"
  16. #include "services/network/public/mojom/host_resolver.mojom-forward.h"
  17. #include "services/network/public/mojom/ssl_config.mojom-forward.h"
  18. #include "shell/browser/api/ipc_dispatcher.h"
  19. #include "shell/browser/event_emitter_mixin.h"
  20. #include "shell/browser/net/resolve_proxy_helper.h"
  21. #include "shell/common/gin_helper/cleaned_up_at_exit.h"
  22. #include "shell/common/gin_helper/constructible.h"
  23. #include "shell/common/gin_helper/pinnable.h"
  24. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  25. #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h" // nogncheck
  26. #endif
  27. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  28. #include "extensions/browser/extension_registry.h"
  29. #include "extensions/browser/extension_registry_observer.h"
  30. #endif
  31. class GURL;
  32. namespace base {
  33. class FilePath;
  34. }
  35. namespace gin {
  36. class Arguments;
  37. template <typename T>
  38. class Handle;
  39. } // namespace gin
  40. namespace gin_helper {
  41. class Dictionary;
  42. class ErrorThrower;
  43. } // namespace gin_helper
  44. namespace net {
  45. class ProxyConfig;
  46. }
  47. namespace electron {
  48. class ElectronBrowserContext;
  49. struct PreloadScript;
  50. namespace api {
  51. class Session final : public gin::Wrappable<Session>,
  52. public gin_helper::Pinnable<Session>,
  53. public gin_helper::Constructible<Session>,
  54. public gin_helper::EventEmitterMixin<Session>,
  55. public gin_helper::CleanedUpAtExit,
  56. public IpcDispatcher<Session>,
  57. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  58. private SpellcheckHunspellDictionary::Observer,
  59. #endif
  60. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  61. private extensions::ExtensionRegistryObserver,
  62. #endif
  63. private content::DownloadManager::Observer {
  64. public:
  65. // Gets or creates Session from the |browser_context|.
  66. static gin::Handle<Session> CreateFrom(
  67. v8::Isolate* isolate,
  68. ElectronBrowserContext* browser_context);
  69. static gin::Handle<Session> New(); // Dummy, do not use!
  70. static Session* FromBrowserContext(content::BrowserContext* context);
  71. // Gets the Session of |partition|.
  72. static gin::Handle<Session> FromPartition(v8::Isolate* isolate,
  73. const std::string& partition,
  74. base::Value::Dict options = {});
  75. // Gets the Session based on |path|.
  76. static std::optional<gin::Handle<Session>> FromPath(
  77. v8::Isolate* isolate,
  78. const base::FilePath& path,
  79. base::Value::Dict options = {});
  80. ElectronBrowserContext* browser_context() const {
  81. return &browser_context_.get();
  82. }
  83. // gin::Wrappable
  84. static gin::WrapperInfo kWrapperInfo;
  85. static void FillObjectTemplate(v8::Isolate*, v8::Local<v8::ObjectTemplate>);
  86. static const char* GetClassName() { return "Session"; }
  87. const char* GetTypeName() override;
  88. // gin_helper::CleanedUpAtExit
  89. void WillBeDestroyed() override;
  90. // Methods.
  91. v8::Local<v8::Promise> ResolveHost(
  92. std::string host,
  93. std::optional<network::mojom::ResolveHostParametersPtr> params);
  94. v8::Local<v8::Promise> ResolveProxy(gin::Arguments* args);
  95. v8::Local<v8::Promise> GetCacheSize();
  96. v8::Local<v8::Promise> ClearCache();
  97. v8::Local<v8::Promise> ClearStorageData(gin::Arguments* args);
  98. void FlushStorageData();
  99. v8::Local<v8::Promise> SetProxy(gin::Arguments* args);
  100. v8::Local<v8::Promise> ForceReloadProxyConfig();
  101. void SetDownloadPath(const base::FilePath& path);
  102. void EnableNetworkEmulation(const gin_helper::Dictionary& options);
  103. void DisableNetworkEmulation();
  104. void SetCertVerifyProc(v8::Local<v8::Value> proc, gin::Arguments* args);
  105. void SetPermissionRequestHandler(v8::Local<v8::Value> val,
  106. gin::Arguments* args);
  107. void SetPermissionCheckHandler(v8::Local<v8::Value> val,
  108. gin::Arguments* args);
  109. void SetDevicePermissionHandler(v8::Local<v8::Value> val,
  110. gin::Arguments* args);
  111. void SetUSBProtectedClassesHandler(v8::Local<v8::Value> val,
  112. gin::Arguments* args);
  113. void SetBluetoothPairingHandler(v8::Local<v8::Value> val,
  114. gin::Arguments* args);
  115. v8::Local<v8::Promise> ClearHostResolverCache(gin::Arguments* args);
  116. v8::Local<v8::Promise> ClearAuthCache();
  117. void AllowNTLMCredentialsForDomains(const std::string& domains);
  118. void SetUserAgent(const std::string& user_agent, gin::Arguments* args);
  119. std::string GetUserAgent();
  120. void SetSSLConfig(network::mojom::SSLConfigPtr config);
  121. bool IsPersistent();
  122. v8::Local<v8::Promise> GetBlobData(v8::Isolate* isolate,
  123. const std::string& uuid);
  124. void DownloadURL(const GURL& url, gin::Arguments* args);
  125. void CreateInterruptedDownload(const gin_helper::Dictionary& options);
  126. std::string RegisterPreloadScript(gin_helper::ErrorThrower thrower,
  127. const PreloadScript& new_preload_script);
  128. void UnregisterPreloadScript(gin_helper::ErrorThrower thrower,
  129. const std::string& script_id);
  130. std::vector<PreloadScript> GetPreloadScripts() const;
  131. v8::Local<v8::Promise> GetSharedDictionaryInfo(
  132. const gin_helper::Dictionary& options);
  133. v8::Local<v8::Promise> GetSharedDictionaryUsageInfo();
  134. v8::Local<v8::Promise> ClearSharedDictionaryCache();
  135. v8::Local<v8::Promise> ClearSharedDictionaryCacheForIsolationKey(
  136. const gin_helper::Dictionary& options);
  137. v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
  138. v8::Local<v8::Value> Protocol(v8::Isolate* isolate);
  139. v8::Local<v8::Value> ServiceWorkerContext(v8::Isolate* isolate);
  140. v8::Local<v8::Value> WebRequest(v8::Isolate* isolate);
  141. v8::Local<v8::Value> NetLog(v8::Isolate* isolate);
  142. void Preconnect(const gin_helper::Dictionary& options, gin::Arguments* args);
  143. v8::Local<v8::Promise> CloseAllConnections();
  144. v8::Local<v8::Value> GetPath(v8::Isolate* isolate);
  145. void SetCodeCachePath(gin::Arguments* args);
  146. v8::Local<v8::Promise> ClearCodeCaches(const gin_helper::Dictionary& options);
  147. v8::Local<v8::Value> ClearData(gin_helper::ErrorThrower thrower,
  148. gin::Arguments* args);
  149. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  150. base::Value GetSpellCheckerLanguages();
  151. void SetSpellCheckerLanguages(gin_helper::ErrorThrower thrower,
  152. const std::vector<std::string>& languages);
  153. v8::Local<v8::Promise> ListWordsInSpellCheckerDictionary();
  154. bool AddWordToSpellCheckerDictionary(const std::string& word);
  155. bool RemoveWordFromSpellCheckerDictionary(const std::string& word);
  156. void SetSpellCheckerEnabled(bool b);
  157. bool IsSpellCheckerEnabled() const;
  158. #endif
  159. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  160. v8::Local<v8::Promise> LoadExtension(const base::FilePath& extension_path,
  161. gin::Arguments* args);
  162. void RemoveExtension(const std::string& extension_id);
  163. v8::Local<v8::Value> GetExtension(const std::string& extension_id);
  164. v8::Local<v8::Value> GetAllExtensions();
  165. // extensions::ExtensionRegistryObserver:
  166. void OnExtensionLoaded(content::BrowserContext* browser_context,
  167. const extensions::Extension* extension) override;
  168. void OnExtensionReady(content::BrowserContext* browser_context,
  169. const extensions::Extension* extension) override;
  170. void OnExtensionUnloaded(content::BrowserContext* browser_context,
  171. const extensions::Extension* extension,
  172. extensions::UnloadedExtensionReason reason) override;
  173. #endif
  174. // disable copy
  175. Session(const Session&) = delete;
  176. Session& operator=(const Session&) = delete;
  177. protected:
  178. Session(v8::Isolate* isolate, ElectronBrowserContext* browser_context);
  179. ~Session() override;
  180. // content::DownloadManager::Observer:
  181. void OnDownloadCreated(content::DownloadManager* manager,
  182. download::DownloadItem* item) override;
  183. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  184. // SpellcheckHunspellDictionary::Observer
  185. void OnHunspellDictionaryInitialized(const std::string& language) override;
  186. void OnHunspellDictionaryDownloadBegin(const std::string& language) override;
  187. void OnHunspellDictionaryDownloadSuccess(
  188. const std::string& language) override;
  189. void OnHunspellDictionaryDownloadFailure(
  190. const std::string& language) override;
  191. #endif
  192. private:
  193. void SetDisplayMediaRequestHandler(v8::Isolate* isolate,
  194. v8::Local<v8::Value> val);
  195. // Cached gin_helper::Wrappable objects.
  196. v8::Global<v8::Value> cookies_;
  197. v8::Global<v8::Value> protocol_;
  198. v8::Global<v8::Value> net_log_;
  199. v8::Global<v8::Value> service_worker_context_;
  200. v8::Global<v8::Value> web_request_;
  201. raw_ptr<v8::Isolate> isolate_;
  202. // The client id to enable the network throttler.
  203. base::UnguessableToken network_emulation_token_;
  204. const raw_ref<ElectronBrowserContext> browser_context_;
  205. base::WeakPtrFactory<Session> weak_factory_{this};
  206. };
  207. } // namespace api
  208. } // namespace electron
  209. #endif // ELECTRON_SHELL_BROWSER_API_ELECTRON_API_SESSION_H_