atom_browser_context.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #include "atom/browser/atom_browser_context.h"
  5. #include <utility>
  6. #include "atom/app/atom_content_client.h"
  7. #include "atom/browser/atom_blob_reader.h"
  8. #include "atom/browser/atom_browser_main_parts.h"
  9. #include "atom/browser/atom_download_manager_delegate.h"
  10. #include "atom/browser/atom_permission_manager.h"
  11. #include "atom/browser/browser.h"
  12. #include "atom/browser/cookie_change_notifier.h"
  13. #include "atom/browser/net/resolve_proxy_helper.h"
  14. #include "atom/browser/pref_store_delegate.h"
  15. #include "atom/browser/special_storage_policy.h"
  16. #include "atom/browser/web_view_manager.h"
  17. #include "atom/common/atom_version.h"
  18. #include "atom/common/chrome_version.h"
  19. #include "atom/common/options_switches.h"
  20. #include "base/command_line.h"
  21. #include "base/files/file_path.h"
  22. #include "base/path_service.h"
  23. #include "base/strings/string_util.h"
  24. #include "base/strings/stringprintf.h"
  25. #include "base/threading/sequenced_task_runner_handle.h"
  26. #include "base/threading/thread_restrictions.h"
  27. #include "brightray/browser/brightray_paths.h"
  28. #include "brightray/browser/inspectable_web_contents_impl.h"
  29. #include "brightray/browser/zoom_level_delegate.h"
  30. #include "brightray/common/application_info.h"
  31. #include "chrome/common/chrome_paths.h"
  32. #include "chrome/common/pref_names.h"
  33. #include "components/keyed_service/content/browser_context_dependency_manager.h"
  34. #include "components/prefs/json_pref_store.h"
  35. #include "components/prefs/pref_registry_simple.h"
  36. #include "components/prefs/pref_service.h"
  37. #include "components/prefs/pref_service_factory.h"
  38. #include "components/prefs/value_map_pref_store.h"
  39. #include "components/proxy_config/pref_proxy_config_tracker_impl.h"
  40. #include "components/proxy_config/proxy_config_pref_names.h"
  41. #include "content/browser/blob_storage/chrome_blob_storage_context.h"
  42. #include "content/public/browser/storage_partition.h"
  43. #include "content/public/common/content_client.h"
  44. #include "content/public/common/user_agent.h"
  45. #include "net/base/escape.h"
  46. using content::BrowserThread;
  47. namespace atom {
  48. namespace {
  49. // Convert string to lower case and escape it.
  50. std::string MakePartitionName(const std::string& input) {
  51. return net::EscapePath(base::ToLowerASCII(input));
  52. }
  53. } // namespace
  54. // static
  55. AtomBrowserContext::BrowserContextMap AtomBrowserContext::browser_context_map_;
  56. AtomBrowserContext::AtomBrowserContext(const std::string& partition,
  57. bool in_memory,
  58. const base::DictionaryValue& options)
  59. : base::RefCountedDeleteOnSequence<AtomBrowserContext>(
  60. base::ThreadTaskRunnerHandle::Get()),
  61. in_memory_pref_store_(nullptr),
  62. storage_policy_(new SpecialStoragePolicy),
  63. in_memory_(in_memory),
  64. weak_factory_(this) {
  65. AtomContentClient* client =
  66. static_cast<AtomContentClient*>(content::GetContentClient());
  67. user_agent_ = client->GetUserAgent();
  68. // Read options.
  69. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  70. use_cache_ = !command_line->HasSwitch(switches::kDisableHttpCache);
  71. options.GetBoolean("cache", &use_cache_);
  72. base::StringToInt(command_line->GetSwitchValueASCII(switches::kDiskCacheSize),
  73. &max_cache_size_);
  74. if (!base::PathService::Get(brightray::DIR_USER_DATA, &path_)) {
  75. base::PathService::Get(brightray::DIR_APP_DATA, &path_);
  76. path_ = path_.Append(
  77. base::FilePath::FromUTF8Unsafe(brightray::GetApplicationName()));
  78. base::PathService::Override(brightray::DIR_USER_DATA, path_);
  79. }
  80. if (!in_memory && !partition.empty())
  81. path_ = path_.Append(FILE_PATH_LITERAL("Partitions"))
  82. .Append(base::FilePath::FromUTF8Unsafe(
  83. MakePartitionName(partition)));
  84. content::BrowserContext::Initialize(this, path_);
  85. // Initialize Pref Registry.
  86. InitPrefs();
  87. proxy_config_monitor_ = std::make_unique<ProxyConfigMonitor>(prefs_.get());
  88. io_handle_ = new URLRequestContextGetter::Handle(weak_factory_.GetWeakPtr());
  89. cookie_change_notifier_ = std::make_unique<CookieChangeNotifier>(this);
  90. BrowserContextDependencyManager::GetInstance()->MarkBrowserContextLive(this);
  91. }
  92. AtomBrowserContext::~AtomBrowserContext() {
  93. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  94. NotifyWillBeDestroyed(this);
  95. ShutdownStoragePartitions();
  96. io_handle_->ShutdownOnUIThread();
  97. // Notify any keyed services of browser context destruction.
  98. BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
  99. this);
  100. }
  101. void AtomBrowserContext::InitPrefs() {
  102. auto prefs_path = GetPath().Append(FILE_PATH_LITERAL("Preferences"));
  103. base::ThreadRestrictions::ScopedAllowIO allow_io;
  104. PrefServiceFactory prefs_factory;
  105. scoped_refptr<JsonPrefStore> pref_store =
  106. base::MakeRefCounted<JsonPrefStore>(prefs_path);
  107. pref_store->ReadPrefs(); // Synchronous.
  108. prefs_factory.set_user_prefs(pref_store);
  109. auto registry = WrapRefCounted(new PrefRegistrySimple);
  110. registry->RegisterFilePathPref(prefs::kSelectFileLastDirectory,
  111. base::FilePath());
  112. base::FilePath download_dir;
  113. base::PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &download_dir);
  114. registry->RegisterFilePathPref(prefs::kDownloadDefaultDirectory,
  115. download_dir);
  116. registry->RegisterDictionaryPref(prefs::kDevToolsFileSystemPaths);
  117. brightray::InspectableWebContentsImpl::RegisterPrefs(registry.get());
  118. brightray::MediaDeviceIDSalt::RegisterPrefs(registry.get());
  119. brightray::ZoomLevelDelegate::RegisterPrefs(registry.get());
  120. PrefProxyConfigTrackerImpl::RegisterPrefs(registry.get());
  121. prefs_ = prefs_factory.Create(
  122. registry.get(),
  123. std::make_unique<PrefStoreDelegate>(weak_factory_.GetWeakPtr()));
  124. prefs_->UpdateCommandLinePrefStore(new ValueMapPrefStore);
  125. }
  126. void AtomBrowserContext::SetUserAgent(const std::string& user_agent) {
  127. user_agent_ = user_agent;
  128. }
  129. net::URLRequestContextGetter* AtomBrowserContext::CreateRequestContext(
  130. content::ProtocolHandlerMap* protocol_handlers,
  131. content::URLRequestInterceptorScopedVector protocol_interceptors) {
  132. return io_handle_
  133. ->CreateMainRequestContextGetter(protocol_handlers,
  134. std::move(protocol_interceptors))
  135. .get();
  136. }
  137. net::URLRequestContextGetter* AtomBrowserContext::CreateMediaRequestContext() {
  138. return io_handle_->GetMainRequestContextGetter().get();
  139. }
  140. net::URLRequestContextGetter* AtomBrowserContext::GetRequestContext() {
  141. return GetDefaultStoragePartition(this)->GetURLRequestContext();
  142. }
  143. network::mojom::NetworkContextPtr AtomBrowserContext::GetNetworkContext() {
  144. return io_handle_->GetNetworkContext();
  145. }
  146. base::FilePath AtomBrowserContext::GetPath() const {
  147. return path_;
  148. }
  149. bool AtomBrowserContext::IsOffTheRecord() const {
  150. return in_memory_;
  151. }
  152. bool AtomBrowserContext::CanUseHttpCache() const {
  153. return use_cache_;
  154. }
  155. int AtomBrowserContext::GetMaxCacheSize() const {
  156. return max_cache_size_;
  157. }
  158. content::ResourceContext* AtomBrowserContext::GetResourceContext() {
  159. return io_handle_->GetResourceContext();
  160. }
  161. std::string AtomBrowserContext::GetMediaDeviceIDSalt() {
  162. if (!media_device_id_salt_.get())
  163. media_device_id_salt_.reset(new brightray::MediaDeviceIDSalt(prefs_.get()));
  164. return media_device_id_salt_->GetSalt();
  165. }
  166. std::unique_ptr<content::ZoomLevelDelegate>
  167. AtomBrowserContext::CreateZoomLevelDelegate(
  168. const base::FilePath& partition_path) {
  169. if (!IsOffTheRecord()) {
  170. return std::make_unique<brightray::ZoomLevelDelegate>(prefs(),
  171. partition_path);
  172. }
  173. return std::unique_ptr<content::ZoomLevelDelegate>();
  174. }
  175. content::DownloadManagerDelegate*
  176. AtomBrowserContext::GetDownloadManagerDelegate() {
  177. if (!download_manager_delegate_.get()) {
  178. auto* download_manager = content::BrowserContext::GetDownloadManager(this);
  179. download_manager_delegate_.reset(
  180. new AtomDownloadManagerDelegate(download_manager));
  181. }
  182. return download_manager_delegate_.get();
  183. }
  184. content::BrowserPluginGuestManager* AtomBrowserContext::GetGuestManager() {
  185. if (!guest_manager_)
  186. guest_manager_.reset(new WebViewManager);
  187. return guest_manager_.get();
  188. }
  189. content::PermissionControllerDelegate*
  190. AtomBrowserContext::GetPermissionControllerDelegate() {
  191. if (!permission_manager_.get())
  192. permission_manager_.reset(new AtomPermissionManager);
  193. return permission_manager_.get();
  194. }
  195. storage::SpecialStoragePolicy* AtomBrowserContext::GetSpecialStoragePolicy() {
  196. return storage_policy_.get();
  197. }
  198. std::string AtomBrowserContext::GetUserAgent() const {
  199. return user_agent_;
  200. }
  201. AtomBlobReader* AtomBrowserContext::GetBlobReader() {
  202. if (!blob_reader_.get()) {
  203. content::ChromeBlobStorageContext* blob_context =
  204. content::ChromeBlobStorageContext::GetFor(this);
  205. blob_reader_.reset(new AtomBlobReader(blob_context));
  206. }
  207. return blob_reader_.get();
  208. }
  209. content::PushMessagingService* AtomBrowserContext::GetPushMessagingService() {
  210. return nullptr;
  211. }
  212. content::SSLHostStateDelegate* AtomBrowserContext::GetSSLHostStateDelegate() {
  213. return nullptr;
  214. }
  215. content::BackgroundFetchDelegate*
  216. AtomBrowserContext::GetBackgroundFetchDelegate() {
  217. return nullptr;
  218. }
  219. content::BackgroundSyncController*
  220. AtomBrowserContext::GetBackgroundSyncController() {
  221. return nullptr;
  222. }
  223. content::BrowsingDataRemoverDelegate*
  224. AtomBrowserContext::GetBrowsingDataRemoverDelegate() {
  225. return nullptr;
  226. }
  227. net::URLRequestContextGetter*
  228. AtomBrowserContext::CreateRequestContextForStoragePartition(
  229. const base::FilePath& partition_path,
  230. bool in_memory,
  231. content::ProtocolHandlerMap* protocol_handlers,
  232. content::URLRequestInterceptorScopedVector request_interceptors) {
  233. NOTREACHED();
  234. return nullptr;
  235. }
  236. net::URLRequestContextGetter*
  237. AtomBrowserContext::CreateMediaRequestContextForStoragePartition(
  238. const base::FilePath& partition_path,
  239. bool in_memory) {
  240. NOTREACHED();
  241. return nullptr;
  242. }
  243. ResolveProxyHelper* AtomBrowserContext::GetResolveProxyHelper() {
  244. if (!resolve_proxy_helper_) {
  245. resolve_proxy_helper_ = base::MakeRefCounted<ResolveProxyHelper>(this);
  246. }
  247. return resolve_proxy_helper_.get();
  248. }
  249. // static
  250. scoped_refptr<AtomBrowserContext> AtomBrowserContext::From(
  251. const std::string& partition,
  252. bool in_memory,
  253. const base::DictionaryValue& options) {
  254. PartitionKey key(partition, in_memory);
  255. auto* browser_context = browser_context_map_[key].get();
  256. if (browser_context)
  257. return scoped_refptr<AtomBrowserContext>(browser_context);
  258. auto* new_context = new AtomBrowserContext(partition, in_memory, options);
  259. browser_context_map_[key] = new_context->GetWeakPtr();
  260. return scoped_refptr<AtomBrowserContext>(new_context);
  261. }
  262. } // namespace atom