browser_context.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/permission_manager.h"
  12. #include "brightray/browser/url_request_context_getter.h"
  13. #include "content/public/browser/browser_context.h"
  14. class PrefRegistrySimple;
  15. class PrefService;
  16. namespace storage {
  17. class SpecialStoragePolicy;
  18. }
  19. namespace brightray {
  20. class PermissionManager;
  21. class BrowserContext : public base::RefCounted<BrowserContext>,
  22. public content::BrowserContext,
  23. public brightray::URLRequestContextGetter::Delegate {
  24. public:
  25. // Get the BrowserContext according to its |partition| and |in_memory|,
  26. // empty pointer when be returned when there is no matching BrowserContext.
  27. static scoped_refptr<BrowserContext> Get(const std::string& partition,
  28. bool in_memory);
  29. base::WeakPtr<BrowserContext> GetWeakPtr() {
  30. return weak_factory_.GetWeakPtr();
  31. }
  32. // Get the request context, if there is no one, create it.
  33. URLRequestContextGetter* GetRequestContext();
  34. // content::BrowserContext:
  35. std::unique_ptr<content::ZoomLevelDelegate> CreateZoomLevelDelegate(
  36. const base::FilePath& partition_path) override;
  37. bool IsOffTheRecord() const override;
  38. content::ResourceContext* GetResourceContext() override;
  39. content::DownloadManagerDelegate* GetDownloadManagerDelegate() override;
  40. content::BrowserPluginGuestManager* GetGuestManager() override;
  41. storage::SpecialStoragePolicy* GetSpecialStoragePolicy() override;
  42. content::PushMessagingService* GetPushMessagingService() override;
  43. content::SSLHostStateDelegate* GetSSLHostStateDelegate() override;
  44. content::PermissionManager* GetPermissionManager() override;
  45. content::BackgroundFetchDelegate* GetBackgroundFetchDelegate() 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. void InitPrefs();
  66. PrefService* prefs() { return prefs_.get(); }
  67. protected:
  68. BrowserContext(const std::string& partition, bool in_memory);
  69. ~BrowserContext() override;
  70. // Subclasses should override this to register custom preferences.
  71. virtual void RegisterPrefs(PrefRegistrySimple* pref_registry) {}
  72. // URLRequestContextGetter::Delegate:
  73. std::unique_ptr<net::NetworkDelegate> CreateNetworkDelegate() override;
  74. base::FilePath GetPath() const override;
  75. private:
  76. friend class base::RefCounted<BrowserContext>;
  77. class ResourceContext;
  78. void RegisterInternalPrefs(PrefRegistrySimple* pref_registry);
  79. // partition_id => browser_context
  80. struct PartitionKey {
  81. std::string partition;
  82. bool in_memory;
  83. PartitionKey(const std::string& partition, bool in_memory)
  84. : partition(partition), in_memory(in_memory) {}
  85. bool operator<(const PartitionKey& other) const {
  86. if (partition == other.partition)
  87. return in_memory < other.in_memory;
  88. return partition < other.partition;
  89. }
  90. bool operator==(const PartitionKey& other) const {
  91. return (partition == other.partition) && (in_memory == other.in_memory);
  92. }
  93. };
  94. using BrowserContextMap =
  95. std::map<PartitionKey, base::WeakPtr<brightray::BrowserContext>>;
  96. static BrowserContextMap browser_context_map_;
  97. base::FilePath path_;
  98. bool in_memory_;
  99. std::unique_ptr<ResourceContext> resource_context_;
  100. scoped_refptr<URLRequestContextGetter> url_request_getter_;
  101. scoped_refptr<storage::SpecialStoragePolicy> storage_policy_;
  102. std::unique_ptr<PrefService> prefs_;
  103. std::unique_ptr<PermissionManager> permission_manager_;
  104. std::unique_ptr<MediaDeviceIDSalt> media_device_id_salt_;
  105. base::WeakPtrFactory<BrowserContext> weak_factory_;
  106. DISALLOW_COPY_AND_ASSIGN(BrowserContext);
  107. };
  108. } // namespace brightray
  109. #endif // BRIGHTRAY_BROWSER_BROWSER_CONTEXT_H_