electron_browser_context.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/weak_ptr.h"
  14. #include "content/public/browser/browser_context.h"
  15. #include "content/public/browser/media_stream_request.h"
  16. #include "content/public/browser/resource_context.h"
  17. #include "electron/shell/browser/media/media_device_id_salt.h"
  18. #include "mojo/public/cpp/bindings/remote.h"
  19. #include "services/network/public/mojom/ssl_config.mojom.h"
  20. #include "third_party/blink/public/common/permissions/permission_utils.h"
  21. class PrefService;
  22. class ValueMapPrefStore;
  23. namespace gin {
  24. class Arguments;
  25. }
  26. namespace network {
  27. class SharedURLLoaderFactory;
  28. }
  29. namespace predictors {
  30. class PreconnectManager;
  31. } // namespace predictors
  32. namespace storage {
  33. class SpecialStoragePolicy;
  34. }
  35. namespace electron {
  36. class ElectronDownloadManagerDelegate;
  37. class ElectronPermissionManager;
  38. class CookieChangeNotifier;
  39. class ResolveProxyHelper;
  40. class WebViewManager;
  41. class ProtocolRegistry;
  42. using DisplayMediaResponseCallbackJs =
  43. base::OnceCallback<void(gin::Arguments* args)>;
  44. using DisplayMediaRequestHandler =
  45. base::RepeatingCallback<void(const content::MediaStreamRequest&,
  46. DisplayMediaResponseCallbackJs)>;
  47. class ElectronBrowserContext : public content::BrowserContext {
  48. public:
  49. // disable copy
  50. ElectronBrowserContext(const ElectronBrowserContext&) = delete;
  51. ElectronBrowserContext& operator=(const ElectronBrowserContext&) = delete;
  52. [[nodiscard]] static std::vector<ElectronBrowserContext*> BrowserContexts();
  53. [[nodiscard]] static bool IsValidContext(const void* context);
  54. // Get or create the default BrowserContext.
  55. static ElectronBrowserContext* GetDefaultBrowserContext(
  56. base::Value::Dict options = {});
  57. // Get or create the BrowserContext according to its |partition| and
  58. // |in_memory|. The |options| will be passed to constructor when there is no
  59. // existing BrowserContext.
  60. static ElectronBrowserContext* From(const std::string& partition,
  61. bool in_memory,
  62. base::Value::Dict options = {});
  63. // Get or create the BrowserContext using the |path|.
  64. // The |options| will be passed to constructor when there is no
  65. // existing BrowserContext.
  66. static ElectronBrowserContext* FromPath(const base::FilePath& path,
  67. base::Value::Dict options = {});
  68. static void DestroyAllContexts();
  69. void SetUserAgent(const std::string& user_agent);
  70. std::string GetUserAgent() const;
  71. bool can_use_http_cache() const { return use_cache_; }
  72. int max_cache_size() const { return max_cache_size_; }
  73. ResolveProxyHelper* GetResolveProxyHelper();
  74. predictors::PreconnectManager* GetPreconnectManager();
  75. scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory();
  76. std::string GetMediaDeviceIDSalt();
  77. // content::BrowserContext:
  78. base::FilePath GetPath() override;
  79. bool IsOffTheRecord() override;
  80. std::unique_ptr<content::ZoomLevelDelegate> CreateZoomLevelDelegate(
  81. const base::FilePath& partition_path) override;
  82. content::PushMessagingService* GetPushMessagingService() override;
  83. content::SSLHostStateDelegate* GetSSLHostStateDelegate() override;
  84. content::BackgroundFetchDelegate* GetBackgroundFetchDelegate() override;
  85. content::BackgroundSyncController* GetBackgroundSyncController() override;
  86. content::BrowsingDataRemoverDelegate* GetBrowsingDataRemoverDelegate()
  87. override;
  88. content::DownloadManagerDelegate* GetDownloadManagerDelegate() override;
  89. content::BrowserPluginGuestManager* GetGuestManager() override;
  90. content::PlatformNotificationService* GetPlatformNotificationService()
  91. override;
  92. content::PermissionControllerDelegate* GetPermissionControllerDelegate()
  93. override;
  94. storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override;
  95. content::ClientHintsControllerDelegate* GetClientHintsControllerDelegate()
  96. override;
  97. content::StorageNotificationService* GetStorageNotificationService() override;
  98. content::ReduceAcceptLanguageControllerDelegate*
  99. GetReduceAcceptLanguageControllerDelegate() override;
  100. content::FileSystemAccessPermissionContext*
  101. GetFileSystemAccessPermissionContext() override;
  102. CookieChangeNotifier* cookie_change_notifier() const {
  103. return cookie_change_notifier_.get();
  104. }
  105. PrefService* prefs() const { return prefs_.get(); }
  106. ValueMapPrefStore* in_memory_pref_store() const {
  107. return in_memory_pref_store_.get();
  108. }
  109. base::WeakPtr<ElectronBrowserContext> GetWeakPtr() {
  110. return weak_factory_.GetWeakPtr();
  111. }
  112. ProtocolRegistry* protocol_registry() const {
  113. return protocol_registry_.get();
  114. }
  115. void SetSSLConfig(network::mojom::SSLConfigPtr config);
  116. network::mojom::SSLConfigPtr GetSSLConfig();
  117. void SetSSLConfigClient(mojo::Remote<network::mojom::SSLConfigClient> client);
  118. bool ChooseDisplayMediaDevice(const content::MediaStreamRequest& request,
  119. content::MediaResponseCallback callback);
  120. void SetDisplayMediaRequestHandler(DisplayMediaRequestHandler handler);
  121. ~ElectronBrowserContext() override;
  122. // Grants |origin| access to |device|.
  123. // To be used in place of ObjectPermissionContextBase::GrantObjectPermission.
  124. void GrantDevicePermission(const url::Origin& origin,
  125. const base::Value& device,
  126. blink::PermissionType permissionType);
  127. // Revokes |origin| access to |device|.
  128. // To be used in place of ObjectPermissionContextBase::RevokeObjectPermission.
  129. void RevokeDevicePermission(const url::Origin& origin,
  130. const base::Value& device,
  131. blink::PermissionType permission_type);
  132. // Returns the list of devices that |origin| has been granted permission to
  133. // access. To be used in place of
  134. // ObjectPermissionContextBase::GetGrantedObjects.
  135. bool CheckDevicePermission(const url::Origin& origin,
  136. const base::Value& device,
  137. blink::PermissionType permissionType);
  138. private:
  139. using DevicePermissionMap = std::map<
  140. blink::PermissionType,
  141. std::map<url::Origin, std::vector<std::unique_ptr<base::Value>>>>;
  142. using PartitionOrPath =
  143. std::variant<std::reference_wrapper<const std::string>,
  144. std::reference_wrapper<const base::FilePath>>;
  145. ElectronBrowserContext(const PartitionOrPath partition_location,
  146. bool in_memory,
  147. base::Value::Dict options);
  148. ElectronBrowserContext(base::FilePath partition, base::Value::Dict options);
  149. static void DisplayMediaDeviceChosen(
  150. const content::MediaStreamRequest& request,
  151. content::MediaResponseCallback callback,
  152. gin::Arguments* args);
  153. // Initialize pref registry.
  154. void InitPrefs();
  155. scoped_refptr<ValueMapPrefStore> in_memory_pref_store_;
  156. std::unique_ptr<CookieChangeNotifier> cookie_change_notifier_;
  157. std::unique_ptr<PrefService> prefs_;
  158. std::unique_ptr<ElectronDownloadManagerDelegate> download_manager_delegate_;
  159. std::unique_ptr<WebViewManager> guest_manager_;
  160. std::unique_ptr<ElectronPermissionManager> permission_manager_;
  161. std::unique_ptr<MediaDeviceIDSalt> media_device_id_salt_;
  162. scoped_refptr<ResolveProxyHelper> resolve_proxy_helper_;
  163. scoped_refptr<storage::SpecialStoragePolicy> storage_policy_;
  164. std::unique_ptr<predictors::PreconnectManager> preconnect_manager_;
  165. std::unique_ptr<ProtocolRegistry> protocol_registry_;
  166. std::optional<std::string> user_agent_;
  167. base::FilePath path_;
  168. bool in_memory_ = false;
  169. bool use_cache_ = true;
  170. int max_cache_size_ = 0;
  171. // Shared URLLoaderFactory.
  172. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  173. network::mojom::SSLConfigPtr ssl_config_;
  174. mojo::Remote<network::mojom::SSLConfigClient> ssl_config_client_;
  175. DisplayMediaRequestHandler display_media_request_handler_;
  176. // In-memory cache that holds objects that have been granted permissions.
  177. DevicePermissionMap granted_devices_;
  178. base::WeakPtrFactory<ElectronBrowserContext> weak_factory_{this};
  179. };
  180. } // namespace electron
  181. #endif // ELECTRON_SHELL_BROWSER_ELECTRON_BROWSER_CONTEXT_H_