browser_context.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "brightray/browser/brightray_paths.h"
  9. #include "brightray/browser/browser_client.h"
  10. #include "brightray/browser/inspectable_web_contents_impl.h"
  11. #include "brightray/browser/media/media_device_id_salt.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. std::string GetMediaDeviceIDSalt() override {
  46. auto media_device_id_salt_ = getter_->GetMediaDeviceIDSalt();
  47. if (media_device_id_salt_)
  48. return media_device_id_salt_->GetSalt();
  49. return content::ResourceContext::GetMediaDeviceIDSalt();
  50. }
  51. URLRequestContextGetter* getter_;
  52. };
  53. // static
  54. BrowserContext::BrowserContextMap BrowserContext::browser_context_map_;
  55. // static
  56. scoped_refptr<BrowserContext> BrowserContext::Get(
  57. const std::string& partition, bool in_memory) {
  58. PartitionKey key(partition, in_memory);
  59. if (browser_context_map_[key].get())
  60. return make_scoped_refptr(browser_context_map_[key].get());
  61. return nullptr;
  62. }
  63. BrowserContext::BrowserContext(const std::string& partition, bool in_memory)
  64. : in_memory_(in_memory),
  65. resource_context_(new ResourceContext),
  66. storage_policy_(new SpecialStoragePolicy),
  67. weak_factory_(this) {
  68. if (!PathService::Get(DIR_USER_DATA, &path_)) {
  69. PathService::Get(DIR_APP_DATA, &path_);
  70. path_ = path_.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName()));
  71. PathService::Override(DIR_USER_DATA, path_);
  72. }
  73. if (!in_memory_ && !partition.empty())
  74. path_ = path_.Append(FILE_PATH_LITERAL("Partitions"))
  75. .Append(base::FilePath::FromUTF8Unsafe(
  76. MakePartitionName(partition)));
  77. content::BrowserContext::Initialize(this, path_);
  78. browser_context_map_[PartitionKey(partition, in_memory)] = GetWeakPtr();
  79. }
  80. BrowserContext::~BrowserContext() {
  81. NotifyWillBeDestroyed(this);
  82. ShutdownStoragePartitions();
  83. BrowserThread::DeleteSoon(BrowserThread::IO,
  84. FROM_HERE,
  85. resource_context_.release());
  86. }
  87. void BrowserContext::InitPrefs() {
  88. auto prefs_path = GetPath().Append(FILE_PATH_LITERAL("Preferences"));
  89. PrefServiceFactory prefs_factory;
  90. prefs_factory.SetUserPrefsFile(prefs_path,
  91. JsonPrefStore::GetTaskRunnerForFile(
  92. prefs_path, BrowserThread::GetBlockingPool()).get());
  93. auto registry = make_scoped_refptr(new PrefRegistrySimple);
  94. RegisterInternalPrefs(registry.get());
  95. RegisterPrefs(registry.get());
  96. prefs_ = prefs_factory.Create(registry.get());
  97. }
  98. void BrowserContext::RegisterInternalPrefs(PrefRegistrySimple* registry) {
  99. InspectableWebContentsImpl::RegisterPrefs(registry);
  100. MediaDeviceIDSalt::RegisterPrefs(registry);
  101. ZoomLevelDelegate::RegisterPrefs(registry);
  102. }
  103. URLRequestContextGetter* BrowserContext::GetRequestContext() {
  104. return static_cast<URLRequestContextGetter*>(
  105. GetDefaultStoragePartition(this)->GetURLRequestContext());
  106. }
  107. net::URLRequestContextGetter* BrowserContext::CreateRequestContext(
  108. content::ProtocolHandlerMap* protocol_handlers,
  109. content::URLRequestInterceptorScopedVector protocol_interceptors) {
  110. DCHECK(!url_request_getter_.get());
  111. url_request_getter_ = new URLRequestContextGetter(
  112. this,
  113. network_controller_handle(),
  114. static_cast<NetLog*>(BrowserClient::Get()->GetNetLog()),
  115. GetPath(),
  116. in_memory_,
  117. BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
  118. BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE),
  119. protocol_handlers,
  120. std::move(protocol_interceptors));
  121. resource_context_->set_url_request_context_getter(url_request_getter_.get());
  122. return url_request_getter_.get();
  123. }
  124. net::NetworkDelegate* BrowserContext::CreateNetworkDelegate() {
  125. return new NetworkDelegate;
  126. }
  127. MediaDeviceIDSalt* BrowserContext::GetMediaDeviceIDSalt() {
  128. if (IsOffTheRecord())
  129. return nullptr;
  130. if (!media_device_id_salt_.get())
  131. media_device_id_salt_.reset(new MediaDeviceIDSalt(prefs_.get()));
  132. return media_device_id_salt_.get();
  133. }
  134. base::FilePath BrowserContext::GetPath() const {
  135. return path_;
  136. }
  137. std::unique_ptr<content::ZoomLevelDelegate>
  138. BrowserContext::CreateZoomLevelDelegate(const base::FilePath& partition_path) {
  139. if (!IsOffTheRecord()) {
  140. return base::MakeUnique<ZoomLevelDelegate>(prefs(), partition_path);
  141. }
  142. return std::unique_ptr<content::ZoomLevelDelegate>();
  143. }
  144. bool BrowserContext::IsOffTheRecord() const {
  145. return in_memory_;
  146. }
  147. content::ResourceContext* BrowserContext::GetResourceContext() {
  148. return resource_context_.get();
  149. }
  150. content::DownloadManagerDelegate* BrowserContext::GetDownloadManagerDelegate() {
  151. return nullptr;
  152. }
  153. content::BrowserPluginGuestManager* BrowserContext::GetGuestManager() {
  154. return nullptr;
  155. }
  156. storage::SpecialStoragePolicy* BrowserContext::GetSpecialStoragePolicy() {
  157. return storage_policy_.get();
  158. }
  159. content::PushMessagingService* BrowserContext::GetPushMessagingService() {
  160. return nullptr;
  161. }
  162. content::SSLHostStateDelegate* BrowserContext::GetSSLHostStateDelegate() {
  163. return nullptr;
  164. }
  165. content::PermissionManager* BrowserContext::GetPermissionManager() {
  166. if (!permission_manager_.get())
  167. permission_manager_.reset(new PermissionManager);
  168. return permission_manager_.get();
  169. }
  170. content::BackgroundSyncController*
  171. BrowserContext::GetBackgroundSyncController() {
  172. return nullptr;
  173. }
  174. net::URLRequestContextGetter*
  175. BrowserContext::CreateRequestContextForStoragePartition(
  176. const base::FilePath& partition_path,
  177. bool in_memory,
  178. content::ProtocolHandlerMap* protocol_handlers,
  179. content::URLRequestInterceptorScopedVector request_interceptors) {
  180. return nullptr;
  181. }
  182. net::URLRequestContextGetter*
  183. 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