browser_context.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #include "brightray/browser/browser_context.h"
  5. #include "base/files/file_path.h"
  6. #include "base/path_service.h"
  7. #include "base/strings/string_util.h"
  8. #include "base/threading/thread_restrictions.h"
  9. #include "brightray/browser/brightray_paths.h"
  10. #include "brightray/browser/browser_client.h"
  11. #include "brightray/browser/inspectable_web_contents_impl.h"
  12. #include "brightray/browser/network_delegate.h"
  13. #include "brightray/browser/special_storage_policy.h"
  14. #include "brightray/browser/zoom_level_delegate.h"
  15. #include "brightray/common/application_info.h"
  16. #include "components/prefs/json_pref_store.h"
  17. #include "components/prefs/pref_registry_simple.h"
  18. #include "components/prefs/pref_service.h"
  19. #include "components/prefs/pref_service_factory.h"
  20. #include "content/public/browser/browser_thread.h"
  21. #include "content/public/browser/resource_context.h"
  22. #include "content/public/browser/storage_partition.h"
  23. #include "net/base/escape.h"
  24. using content::BrowserThread;
  25. namespace brightray {
  26. namespace {
  27. // Convert string to lower case and escape it.
  28. std::string MakePartitionName(const std::string& input) {
  29. return net::EscapePath(base::ToLowerASCII(input));
  30. }
  31. } // namespace
  32. class BrowserContext::ResourceContext : public content::ResourceContext {
  33. public:
  34. ResourceContext() : getter_(nullptr) {}
  35. void set_url_request_context_getter(URLRequestContextGetter* getter) {
  36. getter_ = getter;
  37. }
  38. private:
  39. net::HostResolver* GetHostResolver() override {
  40. return getter_->host_resolver();
  41. }
  42. net::URLRequestContext* GetRequestContext() override {
  43. return getter_->GetURLRequestContext();
  44. }
  45. URLRequestContextGetter* getter_;
  46. };
  47. // static
  48. BrowserContext::BrowserContextMap BrowserContext::browser_context_map_;
  49. // static
  50. scoped_refptr<BrowserContext> BrowserContext::Get(const std::string& partition,
  51. bool in_memory) {
  52. PartitionKey key(partition, in_memory);
  53. if (browser_context_map_[key].get())
  54. return WrapRefCounted(browser_context_map_[key].get());
  55. return nullptr;
  56. }
  57. BrowserContext::BrowserContext(const std::string& partition, bool in_memory)
  58. : in_memory_(in_memory),
  59. resource_context_(new ResourceContext),
  60. storage_policy_(new SpecialStoragePolicy),
  61. weak_factory_(this) {
  62. if (!PathService::Get(DIR_USER_DATA, &path_)) {
  63. PathService::Get(DIR_APP_DATA, &path_);
  64. path_ = path_.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName()));
  65. PathService::Override(DIR_USER_DATA, path_);
  66. }
  67. if (!in_memory_ && !partition.empty())
  68. path_ = path_.Append(FILE_PATH_LITERAL("Partitions"))
  69. .Append(base::FilePath::FromUTF8Unsafe(
  70. MakePartitionName(partition)));
  71. content::BrowserContext::Initialize(this, path_);
  72. browser_context_map_[PartitionKey(partition, in_memory)] = GetWeakPtr();
  73. }
  74. BrowserContext::~BrowserContext() {
  75. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  76. NotifyWillBeDestroyed(this);
  77. ShutdownStoragePartitions();
  78. if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
  79. BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
  80. resource_context_.release());
  81. BrowserThread::PostTask(
  82. BrowserThread::IO, FROM_HERE,
  83. base::BindOnce(&URLRequestContextGetter::NotifyContextShutdownOnIO,
  84. base::RetainedRef(url_request_getter_)));
  85. }
  86. }
  87. void BrowserContext::InitPrefs() {
  88. auto prefs_path = GetPath().Append(FILE_PATH_LITERAL("Preferences"));
  89. base::ThreadRestrictions::ScopedAllowIO allow_io;
  90. PrefServiceFactory prefs_factory;
  91. scoped_refptr<JsonPrefStore> pref_store =
  92. base::MakeRefCounted<JsonPrefStore>(prefs_path);
  93. pref_store->ReadPrefs(); // Synchronous.
  94. prefs_factory.set_user_prefs(pref_store);
  95. auto registry = WrapRefCounted(new PrefRegistrySimple);
  96. RegisterInternalPrefs(registry.get());
  97. RegisterPrefs(registry.get());
  98. prefs_ = prefs_factory.Create(registry.get());
  99. }
  100. void BrowserContext::RegisterInternalPrefs(PrefRegistrySimple* registry) {
  101. InspectableWebContentsImpl::RegisterPrefs(registry);
  102. MediaDeviceIDSalt::RegisterPrefs(registry);
  103. ZoomLevelDelegate::RegisterPrefs(registry);
  104. }
  105. URLRequestContextGetter* BrowserContext::GetRequestContext() {
  106. return static_cast<URLRequestContextGetter*>(
  107. GetDefaultStoragePartition(this)->GetURLRequestContext());
  108. }
  109. net::URLRequestContextGetter* BrowserContext::CreateRequestContext(
  110. content::ProtocolHandlerMap* protocol_handlers,
  111. content::URLRequestInterceptorScopedVector protocol_interceptors) {
  112. DCHECK(!url_request_getter_.get());
  113. url_request_getter_ = new URLRequestContextGetter(
  114. this, static_cast<NetLog*>(BrowserClient::Get()->GetNetLog()), GetPath(),
  115. in_memory_, BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
  116. protocol_handlers, std::move(protocol_interceptors));
  117. resource_context_->set_url_request_context_getter(url_request_getter_.get());
  118. return url_request_getter_.get();
  119. }
  120. std::unique_ptr<net::NetworkDelegate> BrowserContext::CreateNetworkDelegate() {
  121. return std::make_unique<NetworkDelegate>();
  122. }
  123. std::string BrowserContext::GetMediaDeviceIDSalt() {
  124. if (!media_device_id_salt_.get())
  125. media_device_id_salt_.reset(new MediaDeviceIDSalt(prefs_.get()));
  126. return media_device_id_salt_->GetSalt();
  127. }
  128. base::FilePath BrowserContext::GetPath() const {
  129. return path_;
  130. }
  131. std::unique_ptr<content::ZoomLevelDelegate>
  132. BrowserContext::CreateZoomLevelDelegate(const base::FilePath& partition_path) {
  133. if (!IsOffTheRecord()) {
  134. return std::make_unique<ZoomLevelDelegate>(prefs(), partition_path);
  135. }
  136. return std::unique_ptr<content::ZoomLevelDelegate>();
  137. }
  138. bool BrowserContext::IsOffTheRecord() const {
  139. return in_memory_;
  140. }
  141. content::ResourceContext* BrowserContext::GetResourceContext() {
  142. return resource_context_.get();
  143. }
  144. content::DownloadManagerDelegate* BrowserContext::GetDownloadManagerDelegate() {
  145. return nullptr;
  146. }
  147. content::BrowserPluginGuestManager* BrowserContext::GetGuestManager() {
  148. return nullptr;
  149. }
  150. storage::SpecialStoragePolicy* BrowserContext::GetSpecialStoragePolicy() {
  151. return storage_policy_.get();
  152. }
  153. content::PushMessagingService* BrowserContext::GetPushMessagingService() {
  154. return nullptr;
  155. }
  156. content::SSLHostStateDelegate* BrowserContext::GetSSLHostStateDelegate() {
  157. return nullptr;
  158. }
  159. content::PermissionManager* BrowserContext::GetPermissionManager() {
  160. if (!permission_manager_.get())
  161. permission_manager_.reset(new PermissionManager);
  162. return permission_manager_.get();
  163. }
  164. content::BackgroundFetchDelegate* BrowserContext::GetBackgroundFetchDelegate() {
  165. return nullptr;
  166. }
  167. content::BackgroundSyncController*
  168. BrowserContext::GetBackgroundSyncController() {
  169. return nullptr;
  170. }
  171. content::BrowsingDataRemoverDelegate*
  172. BrowserContext::GetBrowsingDataRemoverDelegate() {
  173. return nullptr;
  174. }
  175. net::URLRequestContextGetter*
  176. BrowserContext::CreateRequestContextForStoragePartition(
  177. const base::FilePath& partition_path,
  178. bool in_memory,
  179. content::ProtocolHandlerMap* protocol_handlers,
  180. content::URLRequestInterceptorScopedVector request_interceptors) {
  181. return nullptr;
  182. }
  183. net::URLRequestContextGetter* BrowserContext::CreateMediaRequestContext() {
  184. return url_request_getter_.get();
  185. }
  186. net::URLRequestContextGetter*
  187. BrowserContext::CreateMediaRequestContextForStoragePartition(
  188. const base::FilePath& partition_path,
  189. bool in_memory) {
  190. return nullptr;
  191. }
  192. } // namespace brightray