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 file.
5 #include "chrome/browser/profiles/profile_impl_io_data.h"
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/prefs/pref_member.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/stl_util.h"
15 #include "base/strings/string_util.h"
16 #include "base/threading/sequenced_worker_pool.h"
17 #include "base/threading/worker_pool.h"
18 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/chromeos/profiles/profile_helper.h"
20 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
21 #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
22 #include "chrome/browser/io_thread.h"
23 #include "chrome/browser/net/chrome_net_log.h"
24 #include "chrome/browser/net/chrome_network_delegate.h"
25 #include "chrome/browser/net/connect_interceptor.h"
26 #include "chrome/browser/net/cookie_store_util.h"
27 #include "chrome/browser/net/http_server_properties_manager_factory.h"
28 #include "chrome/browser/net/predictor.h"
29 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_configurator.h"
30 #include "chrome/browser/net/sqlite_channel_id_store.h"
31 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/common/chrome_constants.h"
33 #include "chrome/common/chrome_switches.h"
34 #include "chrome/common/pref_names.h"
35 #include "chrome/common/url_constants.h"
36 #include "components/domain_reliability/monitor.h"
37 #include "content/public/browser/browser_thread.h"
38 #include "content/public/browser/cookie_store_factory.h"
39 #include "content/public/browser/notification_service.h"
40 #include "content/public/browser/resource_context.h"
41 #include "content/public/browser/storage_partition.h"
42 #include "extensions/browser/extension_protocols.h"
43 #include "extensions/common/constants.h"
44 #include "net/base/cache_type.h"
45 #include "net/base/sdch_dictionary_fetcher.h"
46 #include "net/base/sdch_manager.h"
47 #include "net/ftp/ftp_network_layer.h"
48 #include "net/http/http_cache.h"
49 #include "net/http/http_server_properties_manager.h"
50 #include "net/ssl/channel_id_service.h"
51 #include "net/url_request/url_request_job_factory_impl.h"
52 #include "webkit/browser/quota/special_storage_policy.h"
54 #if defined(SPDY_PROXY_AUTH_ORIGIN)
55 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
56 #include "components/data_reduction_proxy/browser/data_reduction_proxy_auth_request_handler.h"
57 #include "components/data_reduction_proxy/browser/data_reduction_proxy_protocol.h"
58 #include "components/data_reduction_proxy/browser/data_reduction_proxy_usage_stats.h"
59 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names.h"
60 #endif // defined(SPDY_PROXY_AUTH_ORIGIN)
64 net::BackendType ChooseCacheBackendType() {
65 #if defined(OS_ANDROID)
66 return net::CACHE_BACKEND_SIMPLE;
68 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
69 if (command_line.HasSwitch(switches::kUseSimpleCacheBackend)) {
70 const std::string opt_value =
71 command_line.GetSwitchValueASCII(switches::kUseSimpleCacheBackend);
72 if (LowerCaseEqualsASCII(opt_value, "off"))
73 return net::CACHE_BACKEND_BLOCKFILE;
74 if (opt_value == "" || LowerCaseEqualsASCII(opt_value, "on"))
75 return net::CACHE_BACKEND_SIMPLE;
77 const std::string experiment_name =
78 base::FieldTrialList::FindFullName("SimpleCacheTrial");
79 if (experiment_name == "ExperimentYes" ||
80 experiment_name == "ExperimentYes2") {
81 return net::CACHE_BACKEND_SIMPLE;
83 return net::CACHE_BACKEND_BLOCKFILE;
89 using content::BrowserThread;
90 #if defined(SPDY_PROXY_AUTH_ORIGIN)
91 using data_reduction_proxy::DataReductionProxyParams;
92 #endif // defined(SPDY_PROXY_AUTH_ORIGIN)
94 ProfileImplIOData::Handle::Handle(Profile* profile)
95 : io_data_(new ProfileImplIOData),
98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
102 ProfileImplIOData::Handle::~Handle() {
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
104 if (io_data_->predictor_ != NULL) {
105 // io_data_->predictor_ might be NULL if Init() was never called
106 // (i.e. we shut down before ProfileImpl::DoFinalInit() got called).
107 bool save_prefs = true;
108 #if defined(OS_CHROMEOS)
109 save_prefs = !chromeos::ProfileHelper::IsSigninProfile(profile_);
112 io_data_->predictor_->SaveStateForNextStartupAndTrim();
113 io_data_->predictor_->ShutdownOnUIThread();
116 if (io_data_->http_server_properties_manager_)
117 io_data_->http_server_properties_manager_->ShutdownOnPrefThread();
119 ChromeURLRequestContextGetterMap::iterator iter;
121 iter = isolated_media_request_context_getter_map_.begin();
122 for (; iter != isolated_media_request_context_getter_map_.end(); ++iter)
123 iter->second->Invalidate();
125 iter = app_request_context_getter_map_.begin();
126 for (; iter != app_request_context_getter_map_.end(); ++iter)
127 iter->second->Invalidate();
129 if (extensions_request_context_getter_)
130 extensions_request_context_getter_->Invalidate();
132 if (media_request_context_getter_)
133 media_request_context_getter_->Invalidate();
135 if (main_request_context_getter_)
136 main_request_context_getter_->Invalidate();
138 io_data_->ShutdownOnUIThread();
141 void ProfileImplIOData::Handle::Init(
142 const base::FilePath& cookie_path,
143 const base::FilePath& channel_id_path,
144 const base::FilePath& cache_path,
146 const base::FilePath& media_cache_path,
147 int media_cache_max_size,
148 const base::FilePath& extensions_cookie_path,
149 const base::FilePath& profile_path,
150 const base::FilePath& infinite_cache_path,
151 chrome_browser_net::Predictor* predictor,
152 content::CookieStoreConfig::SessionCookieMode session_cookie_mode,
153 quota::SpecialStoragePolicy* special_storage_policy,
154 scoped_ptr<domain_reliability::DomainReliabilityMonitor>
155 domain_reliability_monitor,
156 const base::Callback<void(bool)>& data_reduction_proxy_unavailable,
157 scoped_ptr<DataReductionProxyChromeConfigurator>
158 data_reduction_proxy_chrome_configurator,
159 scoped_ptr<data_reduction_proxy::DataReductionProxyParams>
160 data_reduction_proxy_params) {
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
162 DCHECK(!io_data_->lazy_params_);
165 LazyParams* lazy_params = new LazyParams();
167 lazy_params->cookie_path = cookie_path;
168 lazy_params->channel_id_path = channel_id_path;
169 lazy_params->cache_path = cache_path;
170 lazy_params->cache_max_size = cache_max_size;
171 lazy_params->media_cache_path = media_cache_path;
172 lazy_params->media_cache_max_size = media_cache_max_size;
173 lazy_params->extensions_cookie_path = extensions_cookie_path;
174 lazy_params->infinite_cache_path = infinite_cache_path;
175 lazy_params->session_cookie_mode = session_cookie_mode;
176 lazy_params->special_storage_policy = special_storage_policy;
178 io_data_->lazy_params_.reset(lazy_params);
180 // Keep track of profile path and cache sizes separately so we can use them
181 // on demand when creating storage isolated URLRequestContextGetters.
182 io_data_->profile_path_ = profile_path;
183 io_data_->app_cache_max_size_ = cache_max_size;
184 io_data_->app_media_cache_max_size_ = media_cache_max_size;
186 io_data_->predictor_.reset(predictor);
187 io_data_->domain_reliability_monitor_ = domain_reliability_monitor.Pass();
189 io_data_->InitializeMetricsEnabledStateOnUIThread();
191 #if defined(SPDY_PROXY_AUTH_ORIGIN)
192 io_data_->data_reduction_proxy_unavailable_callback_ =
193 data_reduction_proxy_unavailable;
194 io_data_->data_reduction_proxy_chrome_configurator_ =
195 data_reduction_proxy_chrome_configurator.Pass();
196 io_data_->data_reduction_proxy_params_ =
197 data_reduction_proxy_params.Pass();
198 #endif // defined(SPDY_PROXY_AUTH_ORIGIN)
201 content::ResourceContext*
202 ProfileImplIOData::Handle::GetResourceContext() const {
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
205 return GetResourceContextNoInit();
208 content::ResourceContext*
209 ProfileImplIOData::Handle::GetResourceContextNoInit() const {
210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
211 // Don't call LazyInitialize here, since the resource context is created at
212 // the beginning of initalization and is used by some members while they're
213 // being initialized (i.e. AppCacheService).
214 return io_data_->GetResourceContext();
217 scoped_refptr<ChromeURLRequestContextGetter>
218 ProfileImplIOData::Handle::CreateMainRequestContextGetter(
219 content::ProtocolHandlerMap* protocol_handlers,
220 content::URLRequestInterceptorScopedVector request_interceptors,
221 PrefService* local_state,
222 IOThread* io_thread) const {
223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
225 DCHECK(!main_request_context_getter_.get());
226 main_request_context_getter_ = ChromeURLRequestContextGetter::Create(
227 profile_, io_data_, protocol_handlers, request_interceptors.Pass());
230 ->InitNetworkPredictor(profile_->GetPrefs(),
233 main_request_context_getter_.get(),
236 content::NotificationService::current()->Notify(
237 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
238 content::Source<Profile>(profile_),
239 content::NotificationService::NoDetails());
240 return main_request_context_getter_;
243 scoped_refptr<ChromeURLRequestContextGetter>
244 ProfileImplIOData::Handle::GetMediaRequestContextGetter() const {
245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
247 if (!media_request_context_getter_.get()) {
248 media_request_context_getter_ =
249 ChromeURLRequestContextGetter::CreateForMedia(profile_, io_data_);
251 return media_request_context_getter_;
254 scoped_refptr<ChromeURLRequestContextGetter>
255 ProfileImplIOData::Handle::GetExtensionsRequestContextGetter() const {
256 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
258 if (!extensions_request_context_getter_.get()) {
259 extensions_request_context_getter_ =
260 ChromeURLRequestContextGetter::CreateForExtensions(profile_, io_data_);
262 return extensions_request_context_getter_;
265 scoped_refptr<ChromeURLRequestContextGetter>
266 ProfileImplIOData::Handle::CreateIsolatedAppRequestContextGetter(
267 const base::FilePath& partition_path,
269 content::ProtocolHandlerMap* protocol_handlers,
270 content::URLRequestInterceptorScopedVector request_interceptors) const {
271 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
272 // Check that the partition_path is not the same as the base profile path. We
273 // expect isolated partition, which will never go to the default profile path.
274 CHECK(partition_path != profile_->GetPath());
277 // Keep a map of request context getters, one per requested storage partition.
278 StoragePartitionDescriptor descriptor(partition_path, in_memory);
279 ChromeURLRequestContextGetterMap::iterator iter =
280 app_request_context_getter_map_.find(descriptor);
281 if (iter != app_request_context_getter_map_.end())
284 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
285 protocol_handler_interceptor(
286 ProtocolHandlerRegistryFactory::GetForBrowserContext(profile_)->
287 CreateJobInterceptorFactory());
288 ChromeURLRequestContextGetter* context =
289 ChromeURLRequestContextGetter::CreateForIsolatedApp(
293 protocol_handler_interceptor.Pass(),
295 request_interceptors.Pass());
296 app_request_context_getter_map_[descriptor] = context;
301 scoped_refptr<ChromeURLRequestContextGetter>
302 ProfileImplIOData::Handle::GetIsolatedMediaRequestContextGetter(
303 const base::FilePath& partition_path,
304 bool in_memory) const {
305 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
306 // We must have a non-default path, or this will act like the default media
308 CHECK(partition_path != profile_->GetPath());
311 // Keep a map of request context getters, one per requested storage partition.
312 StoragePartitionDescriptor descriptor(partition_path, in_memory);
313 ChromeURLRequestContextGetterMap::iterator iter =
314 isolated_media_request_context_getter_map_.find(descriptor);
315 if (iter != isolated_media_request_context_getter_map_.end())
318 // Get the app context as the starting point for the media context, so that
319 // it uses the app's cookie store.
320 ChromeURLRequestContextGetterMap::const_iterator app_iter =
321 app_request_context_getter_map_.find(descriptor);
322 DCHECK(app_iter != app_request_context_getter_map_.end());
323 ChromeURLRequestContextGetter* app_context = app_iter->second.get();
324 ChromeURLRequestContextGetter* context =
325 ChromeURLRequestContextGetter::CreateForIsolatedMedia(
326 profile_, app_context, io_data_, descriptor);
327 isolated_media_request_context_getter_map_[descriptor] = context;
332 DevToolsNetworkController*
333 ProfileImplIOData::Handle::GetDevToolsNetworkController() const {
334 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
335 return io_data_->network_controller();
338 void ProfileImplIOData::Handle::ClearNetworkingHistorySince(
340 const base::Closure& completion) {
341 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
344 BrowserThread::PostTask(
345 BrowserThread::IO, FROM_HERE,
347 &ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread,
348 base::Unretained(io_data_),
353 void ProfileImplIOData::Handle::LazyInitialize() const {
357 // Set initialized_ to true at the beginning in case any of the objects
358 // below try to get the ResourceContext pointer.
360 PrefService* pref_service = profile_->GetPrefs();
361 io_data_->http_server_properties_manager_ =
362 chrome_browser_net::HttpServerPropertiesManagerFactory::CreateManager(
364 io_data_->set_http_server_properties(
365 scoped_ptr<net::HttpServerProperties>(
366 io_data_->http_server_properties_manager_));
367 io_data_->session_startup_pref()->Init(
368 prefs::kRestoreOnStartup, pref_service);
369 io_data_->session_startup_pref()->MoveToThread(
370 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
371 #if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
372 io_data_->safe_browsing_enabled()->Init(prefs::kSafeBrowsingEnabled,
374 io_data_->safe_browsing_enabled()->MoveToThread(
375 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
377 #if defined(SPDY_PROXY_AUTH_ORIGIN)
378 io_data_->data_reduction_proxy_enabled()->Init(
379 data_reduction_proxy::prefs::kDataReductionProxyEnabled, pref_service);
380 io_data_->data_reduction_proxy_enabled()->MoveToThread(
381 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
382 #endif // defined(SPDY_PROXY_AUTH_ORIGIN)
383 io_data_->InitializeOnUIThread(profile_);
386 ProfileImplIOData::LazyParams::LazyParams()
388 media_cache_max_size(0),
390 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES) {}
392 ProfileImplIOData::LazyParams::~LazyParams() {}
394 ProfileImplIOData::ProfileImplIOData()
395 : ProfileIOData(Profile::REGULAR_PROFILE),
396 http_server_properties_manager_(NULL),
397 app_cache_max_size_(0),
398 app_media_cache_max_size_(0) {
401 ProfileImplIOData::~ProfileImplIOData() {
403 network_delegate()->set_domain_reliability_monitor(NULL);
405 DestroyResourceContext();
407 if (media_request_context_)
408 media_request_context_->AssertNoURLRequests();
411 void ProfileImplIOData::InitializeInternal(
412 ProfileParams* profile_params,
413 content::ProtocolHandlerMap* protocol_handlers,
414 content::URLRequestInterceptorScopedVector request_interceptors) const {
415 net::URLRequestContext* main_context = main_request_context();
417 IOThread* const io_thread = profile_params->io_thread;
418 IOThread::Globals* const io_thread_globals = io_thread->globals();
420 #if defined(SPDY_PROXY_AUTH_ORIGIN)
421 data_reduction_proxy_auth_request_handler_.reset(
422 new data_reduction_proxy::DataReductionProxyAuthRequestHandler(
423 DataReductionProxyChromeSettings::GetClient(),
424 DataReductionProxyChromeSettings::GetBuildAndPatchNumber(),
425 data_reduction_proxy_params_.get(),
426 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
427 data_reduction_proxy_usage_stats_.reset(
428 new data_reduction_proxy::DataReductionProxyUsageStats(
429 data_reduction_proxy_params_.get(),
430 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)));
431 data_reduction_proxy_usage_stats_->set_unavailable_callback(
432 data_reduction_proxy_unavailable_callback_);
435 network_delegate()->set_data_reduction_proxy_params(
436 data_reduction_proxy_params_.get());
437 network_delegate()->set_data_reduction_proxy_usage_stats(
438 data_reduction_proxy_usage_stats_.get());
439 network_delegate()->set_data_reduction_proxy_auth_request_handler(
440 data_reduction_proxy_auth_request_handler_.get());
441 network_delegate()->set_on_resolve_proxy_handler(
442 base::Bind(data_reduction_proxy::OnResolveProxyHandler));
443 network_delegate()->set_proxy_config_getter(
445 &DataReductionProxyChromeConfigurator::GetProxyConfigOnIO,
446 base::Unretained(data_reduction_proxy_chrome_configurator_.get())));
447 #endif // defined(SPDY_PROXY_AUTH_ORIGIN)
449 network_delegate()->set_predictor(predictor_.get());
451 // Initialize context members.
453 ApplyProfileParamsToContext(main_context);
455 if (http_server_properties_manager_)
456 http_server_properties_manager_->InitializeOnNetworkThread();
458 main_context->set_transport_security_state(transport_security_state());
460 main_context->set_net_log(io_thread->net_log());
462 main_context->set_network_delegate(network_delegate());
464 main_context->set_http_server_properties(http_server_properties());
466 main_context->set_host_resolver(
467 io_thread_globals->host_resolver.get());
468 main_context->set_cert_transparency_verifier(
469 io_thread_globals->cert_transparency_verifier.get());
470 main_context->set_http_auth_handler_factory(
471 io_thread_globals->http_auth_handler_factory.get());
473 main_context->set_fraudulent_certificate_reporter(
474 fraudulent_certificate_reporter());
476 main_context->set_throttler_manager(
477 io_thread_globals->throttler_manager.get());
479 main_context->set_proxy_service(proxy_service());
481 scoped_refptr<net::CookieStore> cookie_store = NULL;
482 net::ChannelIDService* channel_id_service = NULL;
483 if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
484 // Don't use existing cookies and use an in-memory store.
485 using content::CookieStoreConfig;
486 cookie_store = content::CreateCookieStore(CookieStoreConfig(
488 CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
490 profile_params->cookie_monster_delegate.get()));
491 // Don't use existing channel ids and use an in-memory store.
492 channel_id_service = new net::ChannelIDService(
493 new net::DefaultChannelIDStore(NULL),
494 base::WorkerPool::GetTaskRunner(true));
498 // setup cookie store
499 if (!cookie_store.get()) {
500 DCHECK(!lazy_params_->cookie_path.empty());
502 content::CookieStoreConfig cookie_config(
503 lazy_params_->cookie_path,
504 lazy_params_->session_cookie_mode,
505 lazy_params_->special_storage_policy.get(),
506 profile_params->cookie_monster_delegate.get());
507 cookie_config.crypto_delegate =
508 chrome_browser_net::GetCookieCryptoDelegate();
509 cookie_store = content::CreateCookieStore(cookie_config);
512 main_context->set_cookie_store(cookie_store.get());
514 // Setup server bound cert service.
515 if (!channel_id_service) {
516 DCHECK(!lazy_params_->channel_id_path.empty());
518 scoped_refptr<SQLiteChannelIDStore> channel_id_db =
519 new SQLiteChannelIDStore(
520 lazy_params_->channel_id_path,
521 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
522 BrowserThread::GetBlockingPool()->GetSequenceToken()),
523 lazy_params_->special_storage_policy.get());
524 channel_id_service = new net::ChannelIDService(
525 new net::DefaultChannelIDStore(channel_id_db.get()),
526 base::WorkerPool::GetTaskRunner(true));
529 set_channel_id_service(channel_id_service);
530 main_context->set_channel_id_service(channel_id_service);
532 net::HttpCache::DefaultBackend* main_backend =
533 new net::HttpCache::DefaultBackend(
535 ChooseCacheBackendType(),
536 lazy_params_->cache_path,
537 lazy_params_->cache_max_size,
538 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)
540 scoped_ptr<net::HttpCache> main_cache = CreateMainHttpFactory(
541 profile_params, main_backend);
542 main_cache->InitializeInfiniteCache(lazy_params_->infinite_cache_path);
544 if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
545 main_cache->set_mode(
546 chrome_browser_net::IsCookieRecordMode() ?
547 net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
550 main_http_factory_.reset(main_cache.release());
551 main_context->set_http_transaction_factory(main_http_factory_.get());
553 #if !defined(DISABLE_FTP_SUPPORT)
555 new net::FtpNetworkLayer(io_thread_globals->host_resolver.get()));
556 #endif // !defined(DISABLE_FTP_SUPPORT)
558 scoped_ptr<net::URLRequestJobFactoryImpl> main_job_factory(
559 new net::URLRequestJobFactoryImpl());
560 InstallProtocolHandlers(main_job_factory.get(), protocol_handlers);
561 main_job_factory_ = SetUpJobFactoryDefaults(
562 main_job_factory.Pass(),
563 request_interceptors.Pass(),
564 profile_params->protocol_handler_interceptor.Pass(),
567 main_context->set_job_factory(main_job_factory_.get());
569 #if defined(ENABLE_EXTENSIONS)
570 InitializeExtensionsRequestContext(profile_params);
573 // Setup the SDCHManager for this profile.
574 sdch_manager_.reset(new net::SdchManager);
575 sdch_manager_->set_sdch_fetcher(
576 new net::SdchDictionaryFetcher(
578 // SdchDictionaryFetcher takes a reference to the Getter, and
579 // hence implicitly takes ownership.
580 new net::TrivialURLRequestContextGetter(
582 content::BrowserThread::GetMessageLoopProxyForThread(
583 content::BrowserThread::IO))));
584 main_context->set_sdch_manager(sdch_manager_.get());
586 // Create a media request context based on the main context, but using a
587 // media cache. It shares the same job factory as the main context.
588 StoragePartitionDescriptor details(profile_path_, false);
589 media_request_context_.reset(InitializeMediaRequestContext(main_context,
592 if (domain_reliability_monitor_) {
593 domain_reliability_monitor_->Init(
595 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
596 domain_reliability_monitor_->AddBakedInConfigs();
597 network_delegate()->set_domain_reliability_monitor(
598 domain_reliability_monitor_.get());
601 lazy_params_.reset();
604 void ProfileImplIOData::
605 InitializeExtensionsRequestContext(ProfileParams* profile_params) const {
606 net::URLRequestContext* extensions_context = extensions_request_context();
607 IOThread* const io_thread = profile_params->io_thread;
608 IOThread::Globals* const io_thread_globals = io_thread->globals();
609 ApplyProfileParamsToContext(extensions_context);
611 extensions_context->set_transport_security_state(transport_security_state());
613 extensions_context->set_net_log(io_thread->net_log());
615 extensions_context->set_throttler_manager(
616 io_thread_globals->throttler_manager.get());
618 content::CookieStoreConfig cookie_config(
619 lazy_params_->extensions_cookie_path,
620 lazy_params_->session_cookie_mode,
622 cookie_config.crypto_delegate =
623 chrome_browser_net::GetCookieCryptoDelegate();
624 net::CookieStore* extensions_cookie_store =
625 content::CreateCookieStore(cookie_config);
626 // Enable cookies for devtools and extension URLs.
627 const char* const schemes[] = {
628 content::kChromeDevToolsScheme,
629 extensions::kExtensionScheme
631 extensions_cookie_store->GetCookieMonster()->SetCookieableSchemes(
632 schemes, arraysize(schemes));
633 extensions_context->set_cookie_store(extensions_cookie_store);
635 scoped_ptr<net::URLRequestJobFactoryImpl> extensions_job_factory(
636 new net::URLRequestJobFactoryImpl());
637 // TODO(shalev): The extensions_job_factory has a NULL NetworkDelegate.
638 // Without a network_delegate, this protocol handler will never
639 // handle file: requests, but as a side effect it makes
640 // job_factory::IsHandledProtocol return true, which prevents attempts to
641 // handle the protocol externally. We pass NULL in to
642 // SetUpJobFactory() to get this effect.
643 extensions_job_factory_ = SetUpJobFactoryDefaults(
644 extensions_job_factory.Pass(),
645 content::URLRequestInterceptorScopedVector(),
646 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>(),
649 extensions_context->set_job_factory(extensions_job_factory_.get());
652 net::URLRequestContext* ProfileImplIOData::InitializeAppRequestContext(
653 net::URLRequestContext* main_context,
654 const StoragePartitionDescriptor& partition_descriptor,
655 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
656 protocol_handler_interceptor,
657 content::ProtocolHandlerMap* protocol_handlers,
658 content::URLRequestInterceptorScopedVector request_interceptors) const {
659 // Copy most state from the main context.
660 AppRequestContext* context = new AppRequestContext();
661 context->CopyFrom(main_context);
663 base::FilePath cookie_path = partition_descriptor.path.Append(
664 chrome::kCookieFilename);
665 base::FilePath cache_path =
666 partition_descriptor.path.Append(chrome::kCacheDirname);
668 // Use a separate HTTP disk cache for isolated apps.
669 net::HttpCache::BackendFactory* app_backend = NULL;
670 if (partition_descriptor.in_memory) {
671 app_backend = net::HttpCache::DefaultBackend::InMemory(0);
673 app_backend = new net::HttpCache::DefaultBackend(
675 ChooseCacheBackendType(),
678 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)
681 net::HttpNetworkSession* main_network_session =
682 main_http_factory_->GetSession();
683 scoped_ptr<net::HttpCache> app_http_cache =
684 CreateHttpFactory(main_network_session, app_backend);
686 scoped_refptr<net::CookieStore> cookie_store = NULL;
687 if (partition_descriptor.in_memory) {
688 cookie_store = content::CreateCookieStore(content::CookieStoreConfig());
689 } else if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
690 // Don't use existing cookies and use an in-memory store.
691 // TODO(creis): We should have a cookie delegate for notifying the cookie
692 // extensions API, but we need to update it to understand isolated apps
694 cookie_store = content::CreateCookieStore(content::CookieStoreConfig());
695 app_http_cache->set_mode(
696 chrome_browser_net::IsCookieRecordMode() ?
697 net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
700 // Use an app-specific cookie store.
701 if (!cookie_store.get()) {
702 DCHECK(!cookie_path.empty());
704 // TODO(creis): We should have a cookie delegate for notifying the cookie
705 // extensions API, but we need to update it to understand isolated apps
707 content::CookieStoreConfig cookie_config(
709 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
711 cookie_config.crypto_delegate =
712 chrome_browser_net::GetCookieCryptoDelegate();
713 cookie_store = content::CreateCookieStore(cookie_config);
716 // Transfer ownership of the cookies and cache to AppRequestContext.
717 context->SetCookieStore(cookie_store.get());
718 context->SetHttpTransactionFactory(
719 scoped_ptr<net::HttpTransactionFactory>(
720 app_http_cache.PassAs<net::HttpTransactionFactory>()));
722 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
723 new net::URLRequestJobFactoryImpl());
724 InstallProtocolHandlers(job_factory.get(), protocol_handlers);
725 scoped_ptr<net::URLRequestJobFactory> top_job_factory(
726 SetUpJobFactoryDefaults(job_factory.Pass(),
727 request_interceptors.Pass(),
728 protocol_handler_interceptor.Pass(),
730 ftp_factory_.get()));
731 context->SetJobFactory(top_job_factory.Pass());
736 net::URLRequestContext*
737 ProfileImplIOData::InitializeMediaRequestContext(
738 net::URLRequestContext* original_context,
739 const StoragePartitionDescriptor& partition_descriptor) const {
740 // Copy most state from the original context.
741 MediaRequestContext* context = new MediaRequestContext();
742 context->CopyFrom(original_context);
744 // For in-memory context, return immediately after creating the new
745 // context before attaching a separate cache. It is important to return
746 // a new context rather than just reusing |original_context| because
747 // the caller expects to take ownership of the pointer.
748 if (partition_descriptor.in_memory)
751 using content::StoragePartition;
752 base::FilePath cache_path;
753 int cache_max_size = app_media_cache_max_size_;
754 if (partition_descriptor.path == profile_path_) {
755 // lazy_params_ is only valid for the default media context creation.
756 cache_path = lazy_params_->media_cache_path;
757 cache_max_size = lazy_params_->media_cache_max_size;
759 cache_path = partition_descriptor.path.Append(chrome::kMediaCacheDirname);
762 // Use a separate HTTP disk cache for isolated apps.
763 net::HttpCache::BackendFactory* media_backend =
764 new net::HttpCache::DefaultBackend(
766 ChooseCacheBackendType(),
769 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)
771 net::HttpNetworkSession* main_network_session =
772 main_http_factory_->GetSession();
773 scoped_ptr<net::HttpCache> media_http_cache =
774 CreateHttpFactory(main_network_session, media_backend);
776 // Transfer ownership of the cache to MediaRequestContext.
777 context->SetHttpTransactionFactory(
778 media_http_cache.PassAs<net::HttpTransactionFactory>());
780 // Note that we do not create a new URLRequestJobFactory because
781 // the media context should behave exactly like its parent context
782 // in all respects except for cache behavior on media subresources.
783 // The CopyFrom() step above means that our media context will use
784 // the same URLRequestJobFactory instance that our parent context does.
789 net::URLRequestContext*
790 ProfileImplIOData::AcquireMediaRequestContext() const {
791 DCHECK(media_request_context_);
792 return media_request_context_.get();
795 net::URLRequestContext* ProfileImplIOData::AcquireIsolatedAppRequestContext(
796 net::URLRequestContext* main_context,
797 const StoragePartitionDescriptor& partition_descriptor,
798 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
799 protocol_handler_interceptor,
800 content::ProtocolHandlerMap* protocol_handlers,
801 content::URLRequestInterceptorScopedVector request_interceptors) const {
802 // We create per-app contexts on demand, unlike the others above.
803 net::URLRequestContext* app_request_context =
804 InitializeAppRequestContext(main_context,
805 partition_descriptor,
806 protocol_handler_interceptor.Pass(),
808 request_interceptors.Pass());
809 DCHECK(app_request_context);
810 return app_request_context;
813 net::URLRequestContext*
814 ProfileImplIOData::AcquireIsolatedMediaRequestContext(
815 net::URLRequestContext* app_context,
816 const StoragePartitionDescriptor& partition_descriptor) const {
817 // We create per-app media contexts on demand, unlike the others above.
818 net::URLRequestContext* media_request_context =
819 InitializeMediaRequestContext(app_context, partition_descriptor);
820 DCHECK(media_request_context);
821 return media_request_context;
824 void ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread(
826 const base::Closure& completion) {
827 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
828 DCHECK(initialized());
830 DCHECK(transport_security_state());
831 // Completes synchronously.
832 transport_security_state()->DeleteAllDynamicDataSince(time);
833 DCHECK(http_server_properties_manager_);
834 http_server_properties_manager_->Clear(completion);