electron_browser_context.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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_ELECTRON_BROWSER_CONTEXT_H_
  5. #define ELECTRON_SHELL_BROWSER_ELECTRON_BROWSER_CONTEXT_H_
  6. #include <map>
  7. #include <memory>
  8. #include <optional>
  9. #include <string>
  10. #include <string_view>
  11. #include <variant>
  12. #include <vector>
  13. #include "base/memory/raw_ptr.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "chrome/browser/predictors/preconnect_manager.h"
  16. #include "content/public/browser/browser_context.h"
  17. #include "content/public/browser/media_stream_request.h"
  18. #include "content/public/browser/resource_context.h"
  19. #include "electron/buildflags/buildflags.h"
  20. #include "gin/arguments.h"
  21. #include "mojo/public/cpp/bindings/remote.h"
  22. #include "services/network/public/mojom/network_context.mojom.h"
  23. #include "services/network/public/mojom/url_loader_factory.mojom.h"
  24. #include "shell/browser/media/media_device_id_salt.h"
  25. #include "third_party/blink/public/common/permissions/permission_utils.h"
  26. class PrefService;
  27. class ValueMapPrefStore;
  28. namespace network {
  29. class SharedURLLoaderFactory;
  30. }
  31. namespace storage {
  32. class SpecialStoragePolicy;
  33. }
  34. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  35. namespace extensions {
  36. class ElectronExtensionSystem;
  37. }
  38. #endif
  39. namespace v8 {
  40. template <typename T>
  41. class Local;
  42. class Isolate;
  43. class Value;
  44. } // namespace v8
  45. namespace electron {
  46. using DevicePermissionMap =
  47. std::map<blink::PermissionType,
  48. std::map<url::Origin, std::vector<std::unique_ptr<base::Value>>>>;
  49. class ElectronDownloadManagerDelegate;
  50. class ElectronPermissionManager;
  51. class CookieChangeNotifier;
  52. class ResolveProxyHelper;
  53. class WebViewManager;
  54. class ProtocolRegistry;
  55. using DisplayMediaResponseCallbackJs =
  56. base::OnceCallback<void(gin::Arguments* args)>;
  57. using DisplayMediaRequestHandler =
  58. base::RepeatingCallback<void(const content::MediaStreamRequest&,
  59. DisplayMediaResponseCallbackJs)>;
  60. using PartitionOrPath =
  61. std::variant<std::reference_wrapper<const std::string>,
  62. std::reference_wrapper<const base::FilePath>>;
  63. class ElectronBrowserContext : public content::BrowserContext {
  64. public:
  65. // disable copy
  66. ElectronBrowserContext(const ElectronBrowserContext&) = delete;
  67. ElectronBrowserContext& operator=(const ElectronBrowserContext&) = delete;
  68. // partition_id => browser_context
  69. struct PartitionKey {
  70. PartitionKey(const std::string_view partition, bool in_memory)
  71. : type_{Type::Partition}, location_{partition}, in_memory_{in_memory} {}
  72. explicit PartitionKey(const base::FilePath& file_path)
  73. : type_{Type::Path},
  74. location_{file_path.AsUTF8Unsafe()},
  75. in_memory_{false} {}
  76. friend auto operator<=>(const PartitionKey&, const PartitionKey&) = default;
  77. private:
  78. enum class Type { Partition, Path };
  79. Type type_;
  80. std::string location_;
  81. bool in_memory_;
  82. };
  83. using BrowserContextMap =
  84. std::map<PartitionKey, std::unique_ptr<ElectronBrowserContext>>;
  85. // Get or create the BrowserContext according to its |partition| and
  86. // |in_memory|. The |options| will be passed to constructor when there is no
  87. // existing BrowserContext.
  88. static ElectronBrowserContext* From(const std::string& partition,
  89. bool in_memory,
  90. base::Value::Dict options = {});
  91. // Get or create the BrowserContext using the |path|.
  92. // The |options| will be passed to constructor when there is no
  93. // existing BrowserContext.
  94. static ElectronBrowserContext* FromPath(const base::FilePath& path,
  95. base::Value::Dict options = {});
  96. static BrowserContextMap& browser_context_map();
  97. void SetUserAgent(const std::string& user_agent);
  98. std::string GetUserAgent() const;
  99. bool can_use_http_cache() const { return use_cache_; }
  100. int max_cache_size() const { return max_cache_size_; }
  101. ResolveProxyHelper* GetResolveProxyHelper();
  102. predictors::PreconnectManager* GetPreconnectManager();
  103. scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory();
  104. std::string GetMediaDeviceIDSalt();
  105. // content::BrowserContext:
  106. base::FilePath GetPath() override;
  107. bool IsOffTheRecord() override;
  108. std::unique_ptr<content::ZoomLevelDelegate> CreateZoomLevelDelegate(
  109. const base::FilePath& partition_path) override;
  110. content::PushMessagingService* GetPushMessagingService() override;
  111. content::SSLHostStateDelegate* GetSSLHostStateDelegate() override;
  112. content::BackgroundFetchDelegate* GetBackgroundFetchDelegate() override;
  113. content::BackgroundSyncController* GetBackgroundSyncController() override;
  114. content::BrowsingDataRemoverDelegate* GetBrowsingDataRemoverDelegate()
  115. override;
  116. content::DownloadManagerDelegate* GetDownloadManagerDelegate() override;
  117. content::BrowserPluginGuestManager* GetGuestManager() override;
  118. content::PlatformNotificationService* GetPlatformNotificationService()
  119. override;
  120. content::PermissionControllerDelegate* GetPermissionControllerDelegate()
  121. override;
  122. storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override;
  123. content::ClientHintsControllerDelegate* GetClientHintsControllerDelegate()
  124. override;
  125. content::StorageNotificationService* GetStorageNotificationService() override;
  126. content::ReduceAcceptLanguageControllerDelegate*
  127. GetReduceAcceptLanguageControllerDelegate() override;
  128. content::FileSystemAccessPermissionContext*
  129. GetFileSystemAccessPermissionContext() override;
  130. CookieChangeNotifier* cookie_change_notifier() const {
  131. return cookie_change_notifier_.get();
  132. }
  133. PrefService* prefs() const { return prefs_.get(); }
  134. ValueMapPrefStore* in_memory_pref_store() const {
  135. return in_memory_pref_store_.get();
  136. }
  137. base::WeakPtr<ElectronBrowserContext> GetWeakPtr() {
  138. return weak_factory_.GetWeakPtr();
  139. }
  140. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  141. extensions::ElectronExtensionSystem* extension_system() {
  142. // Guard usages of extension_system() with !IsOffTheRecord()
  143. // There is no extension system for in-memory sessions
  144. DCHECK(!IsOffTheRecord());
  145. return extension_system_;
  146. }
  147. #endif
  148. ProtocolRegistry* protocol_registry() const {
  149. return protocol_registry_.get();
  150. }
  151. void SetSSLConfig(network::mojom::SSLConfigPtr config);
  152. network::mojom::SSLConfigPtr GetSSLConfig();
  153. void SetSSLConfigClient(mojo::Remote<network::mojom::SSLConfigClient> client);
  154. bool ChooseDisplayMediaDevice(const content::MediaStreamRequest& request,
  155. content::MediaResponseCallback callback);
  156. void SetDisplayMediaRequestHandler(DisplayMediaRequestHandler handler);
  157. ~ElectronBrowserContext() override;
  158. // Grants |origin| access to |device|.
  159. // To be used in place of ObjectPermissionContextBase::GrantObjectPermission.
  160. void GrantDevicePermission(const url::Origin& origin,
  161. const base::Value& device,
  162. blink::PermissionType permissionType);
  163. // Revokes |origin| access to |device|.
  164. // To be used in place of ObjectPermissionContextBase::RevokeObjectPermission.
  165. void RevokeDevicePermission(const url::Origin& origin,
  166. const base::Value& device,
  167. blink::PermissionType permission_type);
  168. // Returns the list of devices that |origin| has been granted permission to
  169. // access. To be used in place of
  170. // ObjectPermissionContextBase::GetGrantedObjects.
  171. bool CheckDevicePermission(const url::Origin& origin,
  172. const base::Value& device,
  173. blink::PermissionType permissionType);
  174. private:
  175. ElectronBrowserContext(const PartitionOrPath partition_location,
  176. bool in_memory,
  177. base::Value::Dict options);
  178. ElectronBrowserContext(base::FilePath partition, base::Value::Dict options);
  179. static void DisplayMediaDeviceChosen(
  180. const content::MediaStreamRequest& request,
  181. content::MediaResponseCallback callback,
  182. gin::Arguments* args);
  183. // Initialize pref registry.
  184. void InitPrefs();
  185. bool DoesDeviceMatch(const base::Value& device,
  186. const base::Value* device_to_compare,
  187. blink::PermissionType permission_type);
  188. scoped_refptr<ValueMapPrefStore> in_memory_pref_store_;
  189. std::unique_ptr<CookieChangeNotifier> cookie_change_notifier_;
  190. std::unique_ptr<PrefService> prefs_;
  191. std::unique_ptr<ElectronDownloadManagerDelegate> download_manager_delegate_;
  192. std::unique_ptr<WebViewManager> guest_manager_;
  193. std::unique_ptr<ElectronPermissionManager> permission_manager_;
  194. std::unique_ptr<MediaDeviceIDSalt> media_device_id_salt_;
  195. scoped_refptr<ResolveProxyHelper> resolve_proxy_helper_;
  196. scoped_refptr<storage::SpecialStoragePolicy> storage_policy_;
  197. std::unique_ptr<predictors::PreconnectManager> preconnect_manager_;
  198. std::unique_ptr<ProtocolRegistry> protocol_registry_;
  199. std::optional<std::string> user_agent_;
  200. base::FilePath path_;
  201. bool in_memory_ = false;
  202. bool use_cache_ = true;
  203. int max_cache_size_ = 0;
  204. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  205. // Owned by the KeyedService system.
  206. raw_ptr<extensions::ElectronExtensionSystem> extension_system_;
  207. #endif
  208. // Shared URLLoaderFactory.
  209. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  210. network::mojom::SSLConfigPtr ssl_config_;
  211. mojo::Remote<network::mojom::SSLConfigClient> ssl_config_client_;
  212. DisplayMediaRequestHandler display_media_request_handler_;
  213. // In-memory cache that holds objects that have been granted permissions.
  214. DevicePermissionMap granted_devices_;
  215. base::WeakPtrFactory<ElectronBrowserContext> weak_factory_{this};
  216. };
  217. } // namespace electron
  218. #endif // ELECTRON_SHELL_BROWSER_ELECTRON_BROWSER_CONTEXT_H_