electron_browser_context.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 SHELL_BROWSER_ELECTRON_BROWSER_CONTEXT_H_
  5. #define 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/resource_context.h"
  14. #include "electron/buildflags/buildflags.h"
  15. #include "services/network/public/mojom/network_context.mojom.h"
  16. #include "services/network/public/mojom/url_loader_factory.mojom.h"
  17. #include "shell/browser/media/media_device_id_salt.h"
  18. class PrefRegistrySimple;
  19. class PrefService;
  20. class ValueMapPrefStore;
  21. namespace network {
  22. class SharedURLLoaderFactory;
  23. }
  24. namespace storage {
  25. class SpecialStoragePolicy;
  26. }
  27. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  28. namespace extensions {
  29. class ElectronExtensionSystem;
  30. }
  31. #endif
  32. namespace electron {
  33. class ElectronBrowserContext;
  34. class ElectronDownloadManagerDelegate;
  35. class ElectronPermissionManager;
  36. class CookieChangeNotifier;
  37. class ResolveProxyHelper;
  38. class SpecialStoragePolicy;
  39. class WebViewManager;
  40. class ProtocolRegistry;
  41. class ElectronBrowserContext
  42. : public content::BrowserContext,
  43. public network::mojom::TrustedURLLoaderAuthClient {
  44. public:
  45. // partition_id => browser_context
  46. struct PartitionKey {
  47. std::string partition;
  48. bool in_memory;
  49. PartitionKey(const std::string& partition, bool in_memory)
  50. : partition(partition), in_memory(in_memory) {}
  51. bool operator<(const PartitionKey& other) const {
  52. if (partition == other.partition)
  53. return in_memory < other.in_memory;
  54. return partition < other.partition;
  55. }
  56. bool operator==(const PartitionKey& other) const {
  57. return (partition == other.partition) && (in_memory == other.in_memory);
  58. }
  59. };
  60. using BrowserContextMap =
  61. std::map<PartitionKey, std::unique_ptr<ElectronBrowserContext>>;
  62. // Get or create the BrowserContext according to its |partition| and
  63. // |in_memory|. The |options| will be passed to constructor when there is no
  64. // existing BrowserContext.
  65. static ElectronBrowserContext* From(
  66. const std::string& partition,
  67. bool in_memory,
  68. base::DictionaryValue options = base::DictionaryValue());
  69. static BrowserContextMap& browser_context_map();
  70. void SetUserAgent(const std::string& user_agent);
  71. std::string GetUserAgent() const;
  72. bool CanUseHttpCache() const;
  73. int GetMaxCacheSize() const;
  74. ResolveProxyHelper* GetResolveProxyHelper();
  75. predictors::PreconnectManager* GetPreconnectManager();
  76. scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory();
  77. // content::BrowserContext:
  78. base::FilePath GetPath() override;
  79. bool IsOffTheRecord() override;
  80. content::ResourceContext* GetResourceContext() override;
  81. std::unique_ptr<content::ZoomLevelDelegate> CreateZoomLevelDelegate(
  82. const base::FilePath& partition_path) override;
  83. content::PushMessagingService* GetPushMessagingService() override;
  84. content::SSLHostStateDelegate* GetSSLHostStateDelegate() override;
  85. content::BackgroundFetchDelegate* GetBackgroundFetchDelegate() override;
  86. content::BackgroundSyncController* GetBackgroundSyncController() override;
  87. content::BrowsingDataRemoverDelegate* GetBrowsingDataRemoverDelegate()
  88. override;
  89. std::string GetMediaDeviceIDSalt() override;
  90. content::DownloadManagerDelegate* GetDownloadManagerDelegate() override;
  91. content::BrowserPluginGuestManager* GetGuestManager() override;
  92. content::PermissionControllerDelegate* GetPermissionControllerDelegate()
  93. override;
  94. storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override;
  95. content::ClientHintsControllerDelegate* GetClientHintsControllerDelegate()
  96. override;
  97. content::StorageNotificationService* GetStorageNotificationService() override;
  98. // extensions deps
  99. void SetCorsOriginAccessListForOrigin(
  100. const url::Origin& source_origin,
  101. std::vector<network::mojom::CorsOriginPatternPtr> allow_patterns,
  102. std::vector<network::mojom::CorsOriginPatternPtr> block_patterns,
  103. base::OnceClosure closure) override;
  104. CookieChangeNotifier* cookie_change_notifier() const {
  105. return cookie_change_notifier_.get();
  106. }
  107. PrefService* prefs() const { return prefs_.get(); }
  108. void set_in_memory_pref_store(ValueMapPrefStore* pref_store) {
  109. in_memory_pref_store_ = pref_store;
  110. }
  111. ValueMapPrefStore* in_memory_pref_store() const {
  112. return in_memory_pref_store_;
  113. }
  114. base::WeakPtr<ElectronBrowserContext> GetWeakPtr() {
  115. return weak_factory_.GetWeakPtr();
  116. }
  117. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  118. extensions::ElectronExtensionSystem* extension_system() {
  119. // Guard usages of extension_system() with !IsOffTheRecord()
  120. // There is no extension system for in-memory sessions
  121. DCHECK(!IsOffTheRecord());
  122. return extension_system_;
  123. }
  124. #endif
  125. ProtocolRegistry* protocol_registry() const {
  126. return protocol_registry_.get();
  127. }
  128. ~ElectronBrowserContext() override;
  129. private:
  130. ElectronBrowserContext(const std::string& partition,
  131. bool in_memory,
  132. base::DictionaryValue options);
  133. void OnLoaderCreated(int32_t request_id,
  134. mojo::PendingReceiver<network::mojom::TrustedAuthClient>
  135. header_client) override;
  136. // Initialize pref registry.
  137. void InitPrefs();
  138. ValueMapPrefStore* in_memory_pref_store_;
  139. std::unique_ptr<content::ResourceContext> resource_context_;
  140. std::unique_ptr<CookieChangeNotifier> cookie_change_notifier_;
  141. std::unique_ptr<PrefService> prefs_;
  142. std::unique_ptr<ElectronDownloadManagerDelegate> download_manager_delegate_;
  143. std::unique_ptr<WebViewManager> guest_manager_;
  144. std::unique_ptr<ElectronPermissionManager> permission_manager_;
  145. std::unique_ptr<MediaDeviceIDSalt> media_device_id_salt_;
  146. scoped_refptr<ResolveProxyHelper> resolve_proxy_helper_;
  147. scoped_refptr<storage::SpecialStoragePolicy> storage_policy_;
  148. std::unique_ptr<predictors::PreconnectManager> preconnect_manager_;
  149. std::unique_ptr<ProtocolRegistry> protocol_registry_;
  150. std::string user_agent_;
  151. base::FilePath path_;
  152. bool in_memory_ = false;
  153. bool use_cache_ = true;
  154. int max_cache_size_ = 0;
  155. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  156. // Owned by the KeyedService system.
  157. extensions::ElectronExtensionSystem* extension_system_;
  158. #endif
  159. // Shared URLLoaderFactory.
  160. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  161. mojo::Receiver<network::mojom::TrustedURLLoaderAuthClient> auth_client_{this};
  162. base::WeakPtrFactory<ElectronBrowserContext> weak_factory_;
  163. DISALLOW_COPY_AND_ASSIGN(ElectronBrowserContext);
  164. };
  165. } // namespace electron
  166. #endif // SHELL_BROWSER_ELECTRON_BROWSER_CONTEXT_H_