browser_context.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE-CHROMIUM file.
  4. #ifndef BRIGHTRAY_BROWSER_BROWSER_CONTEXT_H_
  5. #define BRIGHTRAY_BROWSER_BROWSER_CONTEXT_H_
  6. #include <map>
  7. #include <string>
  8. #include "base/memory/ref_counted.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "brightray/browser/media/media_device_id_salt.h"
  11. #include "brightray/browser/net/devtools_network_controller_handle.h"
  12. #include "brightray/browser/permission_manager.h"
  13. #include "brightray/browser/url_request_context_getter.h"
  14. #include "content/public/browser/browser_context.h"
  15. class PrefRegistrySimple;
  16. class PrefService;
  17. namespace storage {
  18. class SpecialStoragePolicy;
  19. }
  20. namespace brightray {
  21. class PermissionManager;
  22. class BrowserContext : public base::RefCounted<BrowserContext>,
  23. public content::BrowserContext,
  24. public brightray::URLRequestContextGetter::Delegate {
  25. public:
  26. // Get the BrowserContext according to its |partition| and |in_memory|,
  27. // empty pointer when be returned when there is no matching BrowserContext.
  28. static scoped_refptr<BrowserContext> Get(
  29. const std::string& partition, bool in_memory);
  30. base::WeakPtr<BrowserContext> GetWeakPtr() {
  31. return weak_factory_.GetWeakPtr();
  32. }
  33. // Get the request context, if there is no one, create it.
  34. URLRequestContextGetter* GetRequestContext();
  35. // content::BrowserContext:
  36. std::unique_ptr<content::ZoomLevelDelegate> CreateZoomLevelDelegate(
  37. const base::FilePath& partition_path) override;
  38. bool IsOffTheRecord() const override;
  39. content::ResourceContext* GetResourceContext() override;
  40. content::DownloadManagerDelegate* GetDownloadManagerDelegate() override;
  41. content::BrowserPluginGuestManager* GetGuestManager() override;
  42. storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override;
  43. content::PushMessagingService* GetPushMessagingService() override;
  44. content::SSLHostStateDelegate* GetSSLHostStateDelegate() override;
  45. content::PermissionManager* GetPermissionManager() override;
  46. content::BackgroundSyncController* GetBackgroundSyncController() override;
  47. content::BrowsingDataRemoverDelegate* GetBrowsingDataRemoverDelegate()
  48. override;
  49. net::URLRequestContextGetter* CreateRequestContext(
  50. content::ProtocolHandlerMap* protocol_handlers,
  51. content::URLRequestInterceptorScopedVector request_interceptors) override;
  52. net::URLRequestContextGetter* CreateRequestContextForStoragePartition(
  53. const base::FilePath& partition_path,
  54. bool in_memory,
  55. content::ProtocolHandlerMap* protocol_handlers,
  56. content::URLRequestInterceptorScopedVector request_interceptors) override;
  57. net::URLRequestContextGetter* CreateMediaRequestContext() override;
  58. net::URLRequestContextGetter* CreateMediaRequestContextForStoragePartition(
  59. const base::FilePath& partition_path,
  60. bool in_memory) override;
  61. std::string GetMediaDeviceIDSalt() override;
  62. URLRequestContextGetter* url_request_context_getter() const {
  63. return url_request_getter_.get();
  64. }
  65. DevToolsNetworkControllerHandle* network_controller_handle() {
  66. return &network_controller_handle_;
  67. }
  68. void InitPrefs();
  69. PrefService* prefs() { return prefs_.get(); }
  70. protected:
  71. BrowserContext(const std::string& partition, bool in_memory);
  72. ~BrowserContext() override;
  73. // Subclasses should override this to register custom preferences.
  74. virtual void RegisterPrefs(PrefRegistrySimple* pref_registry) {}
  75. // URLRequestContextGetter::Delegate:
  76. std::unique_ptr<net::NetworkDelegate> CreateNetworkDelegate() override;
  77. base::FilePath GetPath() const override;
  78. private:
  79. friend class base::RefCounted<BrowserContext>;
  80. class ResourceContext;
  81. void RegisterInternalPrefs(PrefRegistrySimple* pref_registry);
  82. // partition_id => browser_context
  83. struct PartitionKey {
  84. std::string partition;
  85. bool in_memory;
  86. PartitionKey(const std::string& partition, bool in_memory)
  87. : partition(partition), in_memory(in_memory) {}
  88. bool operator<(const PartitionKey& other) const {
  89. if (partition == other.partition)
  90. return in_memory < other.in_memory;
  91. return partition < other.partition;
  92. }
  93. bool operator==(const PartitionKey& other) const {
  94. return (partition == other.partition) && (in_memory == other.in_memory);
  95. }
  96. };
  97. using BrowserContextMap =
  98. std::map<PartitionKey, base::WeakPtr<brightray::BrowserContext>>;
  99. static BrowserContextMap browser_context_map_;
  100. base::FilePath path_;
  101. bool in_memory_;
  102. DevToolsNetworkControllerHandle network_controller_handle_;
  103. std::unique_ptr<ResourceContext> resource_context_;
  104. scoped_refptr<URLRequestContextGetter> url_request_getter_;
  105. scoped_refptr<storage::SpecialStoragePolicy> storage_policy_;
  106. std::unique_ptr<PrefService> prefs_;
  107. std::unique_ptr<PermissionManager> permission_manager_;
  108. std::unique_ptr<MediaDeviceIDSalt> media_device_id_salt_;
  109. base::WeakPtrFactory<BrowserContext> weak_factory_;
  110. DISALLOW_COPY_AND_ASSIGN(BrowserContext);
  111. };
  112. } // namespace brightray
  113. #endif // BRIGHTRAY_BROWSER_BROWSER_CONTEXT_H_