atom_browser_context.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 ATOM_BROWSER_ATOM_BROWSER_CONTEXT_H_
  5. #define ATOM_BROWSER_ATOM_BROWSER_CONTEXT_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "atom/browser/media/media_device_id_salt.h"
  11. #include "atom/browser/net/url_request_context_getter.h"
  12. #include "base/memory/ref_counted_delete_on_sequence.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "chrome/browser/net/proxy_config_monitor.h"
  15. #include "content/public/browser/browser_context.h"
  16. class PrefRegistrySimple;
  17. class PrefService;
  18. class ValueMapPrefStore;
  19. namespace storage {
  20. class SpecialStoragePolicy;
  21. }
  22. namespace atom {
  23. class AtomBlobReader;
  24. class AtomBrowserContext;
  25. class AtomDownloadManagerDelegate;
  26. class AtomPermissionManager;
  27. class CookieChangeNotifier;
  28. class ResolveProxyHelper;
  29. class SpecialStoragePolicy;
  30. class WebViewManager;
  31. class AtomBrowserContext
  32. : public base::RefCountedDeleteOnSequence<AtomBrowserContext>,
  33. public content::BrowserContext {
  34. public:
  35. // Get or create the BrowserContext according to its |partition| and
  36. // |in_memory|. The |options| will be passed to constructor when there is no
  37. // existing BrowserContext.
  38. static scoped_refptr<AtomBrowserContext> From(
  39. const std::string& partition,
  40. bool in_memory,
  41. const base::DictionaryValue& options = base::DictionaryValue());
  42. void SetUserAgent(const std::string& user_agent);
  43. std::string GetUserAgent() const;
  44. bool CanUseHttpCache() const;
  45. int GetMaxCacheSize() const;
  46. AtomBlobReader* GetBlobReader();
  47. network::mojom::NetworkContextPtr GetNetworkContext();
  48. // Get the request context, if there is none, create it.
  49. net::URLRequestContextGetter* GetRequestContext();
  50. ResolveProxyHelper* GetResolveProxyHelper();
  51. // content::BrowserContext:
  52. base::FilePath GetPath() const override;
  53. bool IsOffTheRecord() const override;
  54. content::ResourceContext* GetResourceContext() override;
  55. std::unique_ptr<content::ZoomLevelDelegate> CreateZoomLevelDelegate(
  56. const base::FilePath& partition_path) override;
  57. content::PushMessagingService* GetPushMessagingService() override;
  58. content::SSLHostStateDelegate* GetSSLHostStateDelegate() override;
  59. content::BackgroundFetchDelegate* GetBackgroundFetchDelegate() override;
  60. content::BackgroundSyncController* GetBackgroundSyncController() override;
  61. content::BrowsingDataRemoverDelegate* GetBrowsingDataRemoverDelegate()
  62. override;
  63. net::URLRequestContextGetter* CreateRequestContextForStoragePartition(
  64. const base::FilePath& partition_path,
  65. bool in_memory,
  66. content::ProtocolHandlerMap* protocol_handlers,
  67. content::URLRequestInterceptorScopedVector request_interceptors) override;
  68. net::URLRequestContextGetter* CreateMediaRequestContextForStoragePartition(
  69. const base::FilePath& partition_path,
  70. bool in_memory) override;
  71. std::string GetMediaDeviceIDSalt() override;
  72. content::DownloadManagerDelegate* GetDownloadManagerDelegate() override;
  73. content::BrowserPluginGuestManager* GetGuestManager() override;
  74. content::PermissionControllerDelegate* GetPermissionControllerDelegate()
  75. override;
  76. storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override;
  77. net::URLRequestContextGetter* CreateRequestContext(
  78. content::ProtocolHandlerMap* protocol_handlers,
  79. content::URLRequestInterceptorScopedVector request_interceptors) override;
  80. net::URLRequestContextGetter* CreateMediaRequestContext() override;
  81. content::ClientHintsControllerDelegate* GetClientHintsControllerDelegate()
  82. override;
  83. CookieChangeNotifier* cookie_change_notifier() const {
  84. return cookie_change_notifier_.get();
  85. }
  86. ProxyConfigMonitor* proxy_config_monitor() {
  87. return proxy_config_monitor_.get();
  88. }
  89. PrefService* prefs() const { return prefs_.get(); }
  90. void set_in_memory_pref_store(ValueMapPrefStore* pref_store) {
  91. in_memory_pref_store_ = pref_store;
  92. }
  93. ValueMapPrefStore* in_memory_pref_store() const {
  94. return in_memory_pref_store_;
  95. }
  96. base::WeakPtr<AtomBrowserContext> GetWeakPtr() {
  97. return weak_factory_.GetWeakPtr();
  98. }
  99. protected:
  100. AtomBrowserContext(const std::string& partition,
  101. bool in_memory,
  102. const base::DictionaryValue& options);
  103. ~AtomBrowserContext() override;
  104. private:
  105. friend class base::RefCountedDeleteOnSequence<AtomBrowserContext>;
  106. friend class base::DeleteHelper<AtomBrowserContext>;
  107. // Initialize pref registry.
  108. void InitPrefs();
  109. // partition_id => browser_context
  110. struct PartitionKey {
  111. std::string partition;
  112. bool in_memory;
  113. PartitionKey(const std::string& partition, bool in_memory)
  114. : partition(partition), in_memory(in_memory) {}
  115. bool operator<(const PartitionKey& other) const {
  116. if (partition == other.partition)
  117. return in_memory < other.in_memory;
  118. return partition < other.partition;
  119. }
  120. bool operator==(const PartitionKey& other) const {
  121. return (partition == other.partition) && (in_memory == other.in_memory);
  122. }
  123. };
  124. using BrowserContextMap =
  125. std::map<PartitionKey, base::WeakPtr<AtomBrowserContext>>;
  126. static BrowserContextMap browser_context_map_;
  127. // Self-destructing class responsible for creating URLRequestContextGetter
  128. // on the UI thread and deletes itself on the IO thread.
  129. URLRequestContextGetter::Handle* io_handle_;
  130. ValueMapPrefStore* in_memory_pref_store_;
  131. std::unique_ptr<CookieChangeNotifier> cookie_change_notifier_;
  132. std::unique_ptr<PrefService> prefs_;
  133. std::unique_ptr<AtomDownloadManagerDelegate> download_manager_delegate_;
  134. std::unique_ptr<WebViewManager> guest_manager_;
  135. std::unique_ptr<AtomPermissionManager> permission_manager_;
  136. std::unique_ptr<AtomBlobReader> blob_reader_;
  137. std::unique_ptr<MediaDeviceIDSalt> media_device_id_salt_;
  138. scoped_refptr<ResolveProxyHelper> resolve_proxy_helper_;
  139. scoped_refptr<storage::SpecialStoragePolicy> storage_policy_;
  140. // Tracks the ProxyConfig to use, and passes any updates to a NetworkContext
  141. // ProxyConfigClient.
  142. std::unique_ptr<ProxyConfigMonitor> proxy_config_monitor_;
  143. std::string user_agent_;
  144. base::FilePath path_;
  145. bool in_memory_ = false;
  146. bool use_cache_ = true;
  147. int max_cache_size_ = 0;
  148. base::WeakPtrFactory<AtomBrowserContext> weak_factory_;
  149. DISALLOW_COPY_AND_ASSIGN(AtomBrowserContext);
  150. };
  151. } // namespace atom
  152. #endif // ATOM_BROWSER_ATOM_BROWSER_CONTEXT_H_