atom_browser_context.cc 11 KB

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