electron_api_session.h 8.5 KB

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