electron_browser_context.h 6.5 KB

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