electron_browser_context.h 9.0 KB

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