atom_browser_context.h 6.5 KB

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