atom_browser_context.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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_ATOM_BROWSER_CONTEXT_H_
  5. #define SHELL_BROWSER_ATOM_BROWSER_CONTEXT_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/memory/ref_counted_delete_on_sequence.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "chrome/browser/net/proxy_config_monitor.h"
  13. #include "chrome/browser/predictors/preconnect_manager.h"
  14. #include "content/public/browser/browser_context.h"
  15. #include "content/public/browser/resource_context.h"
  16. #include "electron/buildflags/buildflags.h"
  17. #include "mojo/public/cpp/bindings/receiver.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. class PrefRegistrySimple;
  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 AtomExtensionSystem;
  33. }
  34. #endif
  35. namespace electron {
  36. class AtomBrowserContext;
  37. class AtomDownloadManagerDelegate;
  38. class AtomPermissionManager;
  39. class CookieChangeNotifier;
  40. class ResolveProxyHelper;
  41. class SpecialStoragePolicy;
  42. class WebViewManager;
  43. class AtomBrowserContext
  44. : public base::RefCountedDeleteOnSequence<AtomBrowserContext>,
  45. public content::BrowserContext,
  46. public network::mojom::TrustedURLLoaderAuthClient {
  47. public:
  48. // partition_id => browser_context
  49. struct PartitionKey {
  50. std::string partition;
  51. bool in_memory;
  52. PartitionKey(const std::string& partition, bool in_memory)
  53. : partition(partition), in_memory(in_memory) {}
  54. bool operator<(const PartitionKey& other) const {
  55. if (partition == other.partition)
  56. return in_memory < other.in_memory;
  57. return partition < other.partition;
  58. }
  59. bool operator==(const PartitionKey& other) const {
  60. return (partition == other.partition) && (in_memory == other.in_memory);
  61. }
  62. };
  63. using BrowserContextMap =
  64. std::map<PartitionKey, base::WeakPtr<AtomBrowserContext>>;
  65. // Get or create the BrowserContext according to its |partition| and
  66. // |in_memory|. The |options| will be passed to constructor when there is no
  67. // existing BrowserContext.
  68. static scoped_refptr<AtomBrowserContext> From(
  69. const std::string& partition,
  70. bool in_memory,
  71. const base::DictionaryValue& options = base::DictionaryValue());
  72. static BrowserContextMap browser_context_map() {
  73. return browser_context_map_;
  74. }
  75. void SetUserAgent(const std::string& user_agent);
  76. std::string GetUserAgent() const;
  77. bool CanUseHttpCache() const;
  78. int GetMaxCacheSize() const;
  79. ResolveProxyHelper* GetResolveProxyHelper();
  80. predictors::PreconnectManager* GetPreconnectManager();
  81. scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory();
  82. // content::BrowserContext:
  83. base::FilePath GetPath() override;
  84. bool IsOffTheRecord() override;
  85. content::ResourceContext* GetResourceContext() override;
  86. std::unique_ptr<content::ZoomLevelDelegate> CreateZoomLevelDelegate(
  87. const base::FilePath& partition_path) override;
  88. content::PushMessagingService* GetPushMessagingService() override;
  89. content::SSLHostStateDelegate* GetSSLHostStateDelegate() override;
  90. content::BackgroundFetchDelegate* GetBackgroundFetchDelegate() override;
  91. content::BackgroundSyncController* GetBackgroundSyncController() override;
  92. content::BrowsingDataRemoverDelegate* GetBrowsingDataRemoverDelegate()
  93. override;
  94. std::string GetMediaDeviceIDSalt() override;
  95. content::DownloadManagerDelegate* GetDownloadManagerDelegate() override;
  96. content::BrowserPluginGuestManager* GetGuestManager() override;
  97. content::PermissionControllerDelegate* GetPermissionControllerDelegate()
  98. override;
  99. storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override;
  100. content::ClientHintsControllerDelegate* GetClientHintsControllerDelegate()
  101. override;
  102. // extensions deps
  103. void SetCorsOriginAccessListForOrigin(
  104. const url::Origin& source_origin,
  105. std::vector<network::mojom::CorsOriginPatternPtr> allow_patterns,
  106. std::vector<network::mojom::CorsOriginPatternPtr> block_patterns,
  107. base::OnceClosure closure) override;
  108. CookieChangeNotifier* cookie_change_notifier() const {
  109. return cookie_change_notifier_.get();
  110. }
  111. ProxyConfigMonitor* proxy_config_monitor() {
  112. return proxy_config_monitor_.get();
  113. }
  114. PrefService* prefs() const { return prefs_.get(); }
  115. void set_in_memory_pref_store(ValueMapPrefStore* pref_store) {
  116. in_memory_pref_store_ = pref_store;
  117. }
  118. ValueMapPrefStore* in_memory_pref_store() const {
  119. return in_memory_pref_store_;
  120. }
  121. base::WeakPtr<AtomBrowserContext> GetWeakPtr() {
  122. return weak_factory_.GetWeakPtr();
  123. }
  124. protected:
  125. AtomBrowserContext(const std::string& partition,
  126. bool in_memory,
  127. const base::DictionaryValue& options);
  128. ~AtomBrowserContext() override;
  129. private:
  130. friend class base::RefCountedDeleteOnSequence<AtomBrowserContext>;
  131. friend class base::DeleteHelper<AtomBrowserContext>;
  132. void OnLoaderCreated(int32_t request_id,
  133. mojo::PendingReceiver<network::mojom::TrustedAuthClient>
  134. header_client) override;
  135. // Initialize pref registry.
  136. void InitPrefs();
  137. static BrowserContextMap browser_context_map_;
  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<AtomDownloadManagerDelegate> download_manager_delegate_;
  143. std::unique_ptr<WebViewManager> guest_manager_;
  144. std::unique_ptr<AtomPermissionManager> 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. // Tracks the ProxyConfig to use, and passes any updates to a NetworkContext
  149. // ProxyConfigClient.
  150. std::unique_ptr<ProxyConfigMonitor> proxy_config_monitor_;
  151. std::unique_ptr<predictors::PreconnectManager> preconnect_manager_;
  152. std::string user_agent_;
  153. base::FilePath path_;
  154. bool in_memory_ = false;
  155. bool use_cache_ = true;
  156. int max_cache_size_ = 0;
  157. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  158. // Owned by the KeyedService system.
  159. extensions::AtomExtensionSystem* extension_system_;
  160. #endif
  161. // Shared URLLoaderFactory.
  162. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  163. mojo::Receiver<network::mojom::TrustedURLLoaderAuthClient> auth_client_{this};
  164. base::WeakPtrFactory<AtomBrowserContext> weak_factory_;
  165. DISALLOW_COPY_AND_ASSIGN(AtomBrowserContext);
  166. };
  167. } // namespace electron
  168. #endif // SHELL_BROWSER_ATOM_BROWSER_CONTEXT_H_