atom_browser_context.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/net/url_request_context_getter.h"
  11. #include "base/memory/ref_counted_delete_on_sequence.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "brightray/browser/media/media_device_id_salt.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. CookieChangeNotifier* cookie_change_notifier() const {
  82. return cookie_change_notifier_.get();
  83. }
  84. ProxyConfigMonitor* proxy_config_monitor() {
  85. return proxy_config_monitor_.get();
  86. }
  87. PrefService* prefs() const { return prefs_.get(); }
  88. void set_in_memory_pref_store(ValueMapPrefStore* pref_store) {
  89. in_memory_pref_store_ = pref_store;
  90. }
  91. ValueMapPrefStore* in_memory_pref_store() const {
  92. return in_memory_pref_store_;
  93. }
  94. base::WeakPtr<AtomBrowserContext> GetWeakPtr() {
  95. return weak_factory_.GetWeakPtr();
  96. }
  97. protected:
  98. AtomBrowserContext(const std::string& partition,
  99. bool in_memory,
  100. const base::DictionaryValue& options);
  101. ~AtomBrowserContext() override;
  102. private:
  103. friend class base::RefCountedDeleteOnSequence<AtomBrowserContext>;
  104. friend class base::DeleteHelper<AtomBrowserContext>;
  105. // Initialize pref registry.
  106. void InitPrefs();
  107. // partition_id => browser_context
  108. struct PartitionKey {
  109. std::string partition;
  110. bool in_memory;
  111. PartitionKey(const std::string& partition, bool in_memory)
  112. : partition(partition), in_memory(in_memory) {}
  113. bool operator<(const PartitionKey& other) const {
  114. if (partition == other.partition)
  115. return in_memory < other.in_memory;
  116. return partition < other.partition;
  117. }
  118. bool operator==(const PartitionKey& other) const {
  119. return (partition == other.partition) && (in_memory == other.in_memory);
  120. }
  121. };
  122. using BrowserContextMap =
  123. std::map<PartitionKey, base::WeakPtr<AtomBrowserContext>>;
  124. static BrowserContextMap browser_context_map_;
  125. // Self-destructing class responsible for creating URLRequestContextGetter
  126. // on the UI thread and deletes itself on the IO thread.
  127. URLRequestContextGetter::Handle* io_handle_;
  128. ValueMapPrefStore* in_memory_pref_store_;
  129. std::unique_ptr<CookieChangeNotifier> cookie_change_notifier_;
  130. std::unique_ptr<PrefService> prefs_;
  131. std::unique_ptr<AtomDownloadManagerDelegate> download_manager_delegate_;
  132. std::unique_ptr<WebViewManager> guest_manager_;
  133. std::unique_ptr<AtomPermissionManager> permission_manager_;
  134. std::unique_ptr<AtomBlobReader> blob_reader_;
  135. std::unique_ptr<brightray::MediaDeviceIDSalt> media_device_id_salt_;
  136. scoped_refptr<ResolveProxyHelper> resolve_proxy_helper_;
  137. scoped_refptr<storage::SpecialStoragePolicy> storage_policy_;
  138. // Tracks the ProxyConfig to use, and passes any updates to a NetworkContext
  139. // ProxyConfigClient.
  140. std::unique_ptr<ProxyConfigMonitor> proxy_config_monitor_;
  141. std::string user_agent_;
  142. base::FilePath path_;
  143. bool in_memory_ = false;
  144. bool use_cache_ = true;
  145. int max_cache_size_ = 0;
  146. base::WeakPtrFactory<AtomBrowserContext> weak_factory_;
  147. DISALLOW_COPY_AND_ASSIGN(AtomBrowserContext);
  148. };
  149. } // namespace atom
  150. #endif // ATOM_BROWSER_ATOM_BROWSER_CONTEXT_H_