[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / profiles / profile_impl_io_data.cc
blob915f5c3d082ef78ee17765cf3f2d29dc955ba1e0
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"
7 #include "base/bind.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.h"
28 #include "chrome/browser/net/predictor.h"
29 #include "chrome/browser/net/sqlite_server_bound_cert_store.h"
30 #include "chrome/browser/profiles/profile.h"
31 #include "chrome/common/chrome_constants.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/pref_names.h"
34 #include "chrome/common/url_constants.h"
35 #include "components/domain_reliability/monitor.h"
36 #include "content/public/browser/browser_thread.h"
37 #include "content/public/browser/cookie_store_factory.h"
38 #include "content/public/browser/notification_service.h"
39 #include "content/public/browser/resource_context.h"
40 #include "content/public/browser/storage_partition.h"
41 #include "extensions/browser/extension_protocols.h"
42 #include "extensions/common/constants.h"
43 #include "net/base/cache_type.h"
44 #include "net/ftp/ftp_network_layer.h"
45 #include "net/http/http_cache.h"
46 #include "net/ssl/server_bound_cert_service.h"
47 #include "net/url_request/protocol_intercept_job_factory.h"
48 #include "net/url_request/url_request_job_factory_impl.h"
49 #include "webkit/browser/quota/special_storage_policy.h"
51 #if defined(OS_ANDROID) || defined(OS_IOS)
52 #if defined(SPDY_PROXY_AUTH_VALUE)
53 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.h"
54 #endif
55 #endif
57 namespace {
59 // Identifies Chrome as the source of Domain Reliability uploads it sends.
60 const char* kDomainReliabilityUploadReporterString = "chrome";
62 net::BackendType ChooseCacheBackendType() {
63 #if defined(OS_ANDROID)
64 return net::CACHE_BACKEND_SIMPLE;
65 #else
66 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
67 if (command_line.HasSwitch(switches::kUseSimpleCacheBackend)) {
68 const std::string opt_value =
69 command_line.GetSwitchValueASCII(switches::kUseSimpleCacheBackend);
70 if (LowerCaseEqualsASCII(opt_value, "off"))
71 return net::CACHE_BACKEND_BLOCKFILE;
72 if (opt_value == "" || LowerCaseEqualsASCII(opt_value, "on"))
73 return net::CACHE_BACKEND_SIMPLE;
75 const std::string experiment_name =
76 base::FieldTrialList::FindFullName("SimpleCacheTrial");
77 if (experiment_name == "ExperimentYes" ||
78 experiment_name == "ExperimentYes2") {
79 return net::CACHE_BACKEND_SIMPLE;
81 return net::CACHE_BACKEND_BLOCKFILE;
82 #endif
85 bool IsDomainReliabilityMonitoringEnabled() {
86 CommandLine* command_line = CommandLine::ForCurrentProcess();
87 if (command_line->HasSwitch(switches::kDisableDomainReliability))
88 return false;
89 if (command_line->HasSwitch(switches::kEnableDomainReliability))
90 return true;
91 return base::FieldTrialList::FindFullName("DomRel-Enable") == "enable";
94 } // namespace
96 using content::BrowserThread;
98 ProfileImplIOData::Handle::Handle(Profile* profile)
99 : io_data_(new ProfileImplIOData),
100 profile_(profile),
101 initialized_(false) {
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
103 DCHECK(profile);
106 ProfileImplIOData::Handle::~Handle() {
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
108 if (io_data_->predictor_ != NULL) {
109 // io_data_->predictor_ might be NULL if Init() was never called
110 // (i.e. we shut down before ProfileImpl::DoFinalInit() got called).
111 bool save_prefs = true;
112 #if defined(OS_CHROMEOS)
113 save_prefs = !chromeos::ProfileHelper::IsSigninProfile(profile_);
114 #endif
115 if (save_prefs) {
116 io_data_->predictor_->SaveStateForNextStartupAndTrim(
117 profile_->GetPrefs());
119 io_data_->predictor_->ShutdownOnUIThread();
122 if (io_data_->http_server_properties_manager_)
123 io_data_->http_server_properties_manager_->ShutdownOnUIThread();
124 io_data_->ShutdownOnUIThread();
127 void ProfileImplIOData::Handle::Init(
128 const base::FilePath& cookie_path,
129 const base::FilePath& server_bound_cert_path,
130 const base::FilePath& cache_path,
131 int cache_max_size,
132 const base::FilePath& media_cache_path,
133 int media_cache_max_size,
134 const base::FilePath& extensions_cookie_path,
135 const base::FilePath& profile_path,
136 const base::FilePath& infinite_cache_path,
137 chrome_browser_net::Predictor* predictor,
138 content::CookieStoreConfig::SessionCookieMode session_cookie_mode,
139 quota::SpecialStoragePolicy* special_storage_policy) {
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
141 DCHECK(!io_data_->lazy_params_);
142 DCHECK(predictor);
144 LazyParams* lazy_params = new LazyParams();
146 lazy_params->cookie_path = cookie_path;
147 lazy_params->server_bound_cert_path = server_bound_cert_path;
148 lazy_params->cache_path = cache_path;
149 lazy_params->cache_max_size = cache_max_size;
150 lazy_params->media_cache_path = media_cache_path;
151 lazy_params->media_cache_max_size = media_cache_max_size;
152 lazy_params->extensions_cookie_path = extensions_cookie_path;
153 lazy_params->infinite_cache_path = infinite_cache_path;
154 lazy_params->session_cookie_mode = session_cookie_mode;
155 lazy_params->special_storage_policy = special_storage_policy;
157 io_data_->lazy_params_.reset(lazy_params);
159 // Keep track of profile path and cache sizes separately so we can use them
160 // on demand when creating storage isolated URLRequestContextGetters.
161 io_data_->profile_path_ = profile_path;
162 io_data_->app_cache_max_size_ = cache_max_size;
163 io_data_->app_media_cache_max_size_ = media_cache_max_size;
165 io_data_->predictor_.reset(predictor);
167 io_data_->InitializeMetricsEnabledStateOnUIThread();
170 content::ResourceContext*
171 ProfileImplIOData::Handle::GetResourceContext() const {
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
173 LazyInitialize();
174 return GetResourceContextNoInit();
177 content::ResourceContext*
178 ProfileImplIOData::Handle::GetResourceContextNoInit() const {
179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
180 // Don't call LazyInitialize here, since the resource context is created at
181 // the beginning of initalization and is used by some members while they're
182 // being initialized (i.e. AppCacheService).
183 return io_data_->GetResourceContext();
186 scoped_refptr<ChromeURLRequestContextGetter>
187 ProfileImplIOData::Handle::CreateMainRequestContextGetter(
188 content::ProtocolHandlerMap* protocol_handlers,
189 content::ProtocolHandlerScopedVector protocol_interceptors,
190 PrefService* local_state,
191 IOThread* io_thread) const {
192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
193 LazyInitialize();
194 DCHECK(!main_request_context_getter_.get());
195 main_request_context_getter_ = ChromeURLRequestContextGetter::Create(
196 profile_, io_data_, protocol_handlers, protocol_interceptors.Pass());
198 io_data_->predictor_
199 ->InitNetworkPredictor(profile_->GetPrefs(),
200 local_state,
201 io_thread,
202 main_request_context_getter_.get());
204 content::NotificationService::current()->Notify(
205 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
206 content::Source<Profile>(profile_),
207 content::NotificationService::NoDetails());
208 return main_request_context_getter_;
211 scoped_refptr<ChromeURLRequestContextGetter>
212 ProfileImplIOData::Handle::GetMediaRequestContextGetter() const {
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
214 LazyInitialize();
215 if (!media_request_context_getter_.get()) {
216 media_request_context_getter_ =
217 ChromeURLRequestContextGetter::CreateForMedia(profile_, io_data_);
219 return media_request_context_getter_;
222 scoped_refptr<ChromeURLRequestContextGetter>
223 ProfileImplIOData::Handle::GetExtensionsRequestContextGetter() const {
224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
225 LazyInitialize();
226 if (!extensions_request_context_getter_.get()) {
227 extensions_request_context_getter_ =
228 ChromeURLRequestContextGetter::CreateForExtensions(profile_, io_data_);
230 return extensions_request_context_getter_;
233 scoped_refptr<ChromeURLRequestContextGetter>
234 ProfileImplIOData::Handle::CreateIsolatedAppRequestContextGetter(
235 const base::FilePath& partition_path,
236 bool in_memory,
237 content::ProtocolHandlerMap* protocol_handlers,
238 content::ProtocolHandlerScopedVector protocol_interceptors) const {
239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
240 // Check that the partition_path is not the same as the base profile path. We
241 // expect isolated partition, which will never go to the default profile path.
242 CHECK(partition_path != profile_->GetPath());
243 LazyInitialize();
245 // Keep a map of request context getters, one per requested storage partition.
246 StoragePartitionDescriptor descriptor(partition_path, in_memory);
247 ChromeURLRequestContextGetterMap::iterator iter =
248 app_request_context_getter_map_.find(descriptor);
249 if (iter != app_request_context_getter_map_.end())
250 return iter->second;
252 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
253 protocol_handler_interceptor(
254 ProtocolHandlerRegistryFactory::GetForProfile(profile_)->
255 CreateJobInterceptorFactory());
256 ChromeURLRequestContextGetter* context =
257 ChromeURLRequestContextGetter::CreateForIsolatedApp(
258 profile_,
259 io_data_,
260 descriptor,
261 protocol_handler_interceptor.Pass(),
262 protocol_handlers,
263 protocol_interceptors.Pass());
264 app_request_context_getter_map_[descriptor] = context;
266 return context;
269 scoped_refptr<ChromeURLRequestContextGetter>
270 ProfileImplIOData::Handle::GetIsolatedMediaRequestContextGetter(
271 const base::FilePath& partition_path,
272 bool in_memory) const {
273 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
274 // We must have a non-default path, or this will act like the default media
275 // context.
276 CHECK(partition_path != profile_->GetPath());
277 LazyInitialize();
279 // Keep a map of request context getters, one per requested storage partition.
280 StoragePartitionDescriptor descriptor(partition_path, in_memory);
281 ChromeURLRequestContextGetterMap::iterator iter =
282 isolated_media_request_context_getter_map_.find(descriptor);
283 if (iter != isolated_media_request_context_getter_map_.end())
284 return iter->second;
286 // Get the app context as the starting point for the media context, so that
287 // it uses the app's cookie store.
288 ChromeURLRequestContextGetterMap::const_iterator app_iter =
289 app_request_context_getter_map_.find(descriptor);
290 DCHECK(app_iter != app_request_context_getter_map_.end());
291 ChromeURLRequestContextGetter* app_context = app_iter->second.get();
292 ChromeURLRequestContextGetter* context =
293 ChromeURLRequestContextGetter::CreateForIsolatedMedia(
294 profile_, app_context, io_data_, descriptor);
295 isolated_media_request_context_getter_map_[descriptor] = context;
297 return context;
300 void ProfileImplIOData::Handle::ClearNetworkingHistorySince(
301 base::Time time,
302 const base::Closure& completion) {
303 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
304 LazyInitialize();
306 BrowserThread::PostTask(
307 BrowserThread::IO, FROM_HERE,
308 base::Bind(
309 &ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread,
310 base::Unretained(io_data_),
311 time,
312 completion));
315 void ProfileImplIOData::Handle::ClearDomainReliabilityMonitor(
316 domain_reliability::DomainReliabilityClearMode mode,
317 const base::Closure& completion) {
318 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
319 LazyInitialize();
321 BrowserThread::PostTask(
322 BrowserThread::IO, FROM_HERE,
323 base::Bind(
324 &ProfileImplIOData::ClearDomainReliabilityMonitorOnIOThread,
325 base::Unretained(io_data_),
326 mode,
327 completion));
330 void ProfileImplIOData::Handle::LazyInitialize() const {
331 if (initialized_)
332 return;
334 // Set initialized_ to true at the beginning in case any of the objects
335 // below try to get the ResourceContext pointer.
336 initialized_ = true;
337 PrefService* pref_service = profile_->GetPrefs();
338 io_data_->http_server_properties_manager_ =
339 new chrome_browser_net::HttpServerPropertiesManager(pref_service);
340 io_data_->set_http_server_properties(
341 scoped_ptr<net::HttpServerProperties>(
342 io_data_->http_server_properties_manager_));
343 io_data_->session_startup_pref()->Init(
344 prefs::kRestoreOnStartup, pref_service);
345 io_data_->session_startup_pref()->MoveToThread(
346 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
347 #if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
348 io_data_->safe_browsing_enabled()->Init(prefs::kSafeBrowsingEnabled,
349 pref_service);
350 io_data_->safe_browsing_enabled()->MoveToThread(
351 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
352 #endif
353 io_data_->InitializeOnUIThread(profile_);
356 ProfileImplIOData::LazyParams::LazyParams()
357 : cache_max_size(0),
358 media_cache_max_size(0),
359 session_cookie_mode(
360 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES) {}
362 ProfileImplIOData::LazyParams::~LazyParams() {}
364 ProfileImplIOData::ProfileImplIOData()
365 : ProfileIOData(Profile::REGULAR_PROFILE),
366 http_server_properties_manager_(NULL),
367 app_cache_max_size_(0),
368 app_media_cache_max_size_(0) {
371 ProfileImplIOData::~ProfileImplIOData() {
372 DestroyResourceContext();
374 if (media_request_context_)
375 media_request_context_->AssertNoURLRequests();
378 void ProfileImplIOData::InitializeInternal(
379 ProfileParams* profile_params,
380 content::ProtocolHandlerMap* protocol_handlers,
381 content::ProtocolHandlerScopedVector protocol_interceptors) const {
382 ChromeURLRequestContext* main_context = main_request_context();
384 IOThread* const io_thread = profile_params->io_thread;
385 IOThread::Globals* const io_thread_globals = io_thread->globals();
387 network_delegate()->set_predictor(predictor_.get());
389 // Initialize context members.
391 ApplyProfileParamsToContext(main_context);
393 if (http_server_properties_manager_)
394 http_server_properties_manager_->InitializeOnIOThread();
396 main_context->set_transport_security_state(transport_security_state());
398 main_context->set_net_log(io_thread->net_log());
400 main_context->set_network_delegate(network_delegate());
402 main_context->set_http_server_properties(http_server_properties());
404 main_context->set_host_resolver(
405 io_thread_globals->host_resolver.get());
406 main_context->set_cert_transparency_verifier(
407 io_thread_globals->cert_transparency_verifier.get());
408 main_context->set_http_auth_handler_factory(
409 io_thread_globals->http_auth_handler_factory.get());
411 main_context->set_fraudulent_certificate_reporter(
412 fraudulent_certificate_reporter());
414 main_context->set_throttler_manager(
415 io_thread_globals->throttler_manager.get());
417 main_context->set_proxy_service(proxy_service());
419 scoped_refptr<net::CookieStore> cookie_store = NULL;
420 net::ServerBoundCertService* server_bound_cert_service = NULL;
421 if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
422 // Don't use existing cookies and use an in-memory store.
423 using content::CookieStoreConfig;
424 cookie_store = content::CreateCookieStore(CookieStoreConfig(
425 base::FilePath(),
426 CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
427 NULL,
428 profile_params->cookie_monster_delegate.get()));
429 // Don't use existing server-bound certs and use an in-memory store.
430 server_bound_cert_service = new net::ServerBoundCertService(
431 new net::DefaultServerBoundCertStore(NULL),
432 base::WorkerPool::GetTaskRunner(true));
436 // setup cookie store
437 if (!cookie_store.get()) {
438 DCHECK(!lazy_params_->cookie_path.empty());
440 content::CookieStoreConfig cookie_config(
441 lazy_params_->cookie_path,
442 lazy_params_->session_cookie_mode,
443 lazy_params_->special_storage_policy.get(),
444 profile_params->cookie_monster_delegate.get());
445 cookie_config.crypto_delegate =
446 chrome_browser_net::GetCookieCryptoDelegate();
447 cookie_store = content::CreateCookieStore(cookie_config);
450 main_context->set_cookie_store(cookie_store.get());
452 // Setup server bound cert service.
453 if (!server_bound_cert_service) {
454 DCHECK(!lazy_params_->server_bound_cert_path.empty());
456 scoped_refptr<SQLiteServerBoundCertStore> server_bound_cert_db =
457 new SQLiteServerBoundCertStore(
458 lazy_params_->server_bound_cert_path,
459 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
460 BrowserThread::GetBlockingPool()->GetSequenceToken()),
461 lazy_params_->special_storage_policy.get());
462 server_bound_cert_service = new net::ServerBoundCertService(
463 new net::DefaultServerBoundCertStore(server_bound_cert_db.get()),
464 base::WorkerPool::GetTaskRunner(true));
467 set_server_bound_cert_service(server_bound_cert_service);
468 main_context->set_server_bound_cert_service(server_bound_cert_service);
470 net::HttpCache::DefaultBackend* main_backend =
471 new net::HttpCache::DefaultBackend(
472 net::DISK_CACHE,
473 ChooseCacheBackendType(),
474 lazy_params_->cache_path,
475 lazy_params_->cache_max_size,
476 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)
477 .get());
478 net::HttpNetworkSession::Params network_session_params;
479 PopulateNetworkSessionParams(profile_params, &network_session_params);
480 net::HttpCache* main_cache = new net::HttpCache(
481 network_session_params, main_backend);
482 main_cache->InitializeInfiniteCache(lazy_params_->infinite_cache_path);
484 #if defined(OS_ANDROID) || defined(OS_IOS)
485 #if defined(SPDY_PROXY_AUTH_VALUE)
486 data_reduction_proxy::DataReductionProxySettings::
487 InitDataReductionProxySession(main_cache->GetSession(),
488 SPDY_PROXY_AUTH_VALUE);
489 #endif
490 #endif
492 if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
493 main_cache->set_mode(
494 chrome_browser_net::IsCookieRecordMode() ?
495 net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
498 main_http_factory_.reset(main_cache);
499 main_context->set_http_transaction_factory(main_cache);
501 #if !defined(DISABLE_FTP_SUPPORT)
502 ftp_factory_.reset(
503 new net::FtpNetworkLayer(io_thread_globals->host_resolver.get()));
504 #endif // !defined(DISABLE_FTP_SUPPORT)
506 scoped_ptr<net::URLRequestJobFactoryImpl> main_job_factory(
507 new net::URLRequestJobFactoryImpl());
508 InstallProtocolHandlers(main_job_factory.get(), protocol_handlers);
509 main_job_factory_ = SetUpJobFactoryDefaults(
510 main_job_factory.Pass(),
511 protocol_interceptors.Pass(),
512 profile_params->protocol_handler_interceptor.Pass(),
513 network_delegate(),
514 ftp_factory_.get());
515 main_context->set_job_factory(main_job_factory_.get());
517 #if defined(ENABLE_EXTENSIONS)
518 InitializeExtensionsRequestContext(profile_params);
519 #endif
521 // Create a media request context based on the main context, but using a
522 // media cache. It shares the same job factory as the main context.
523 StoragePartitionDescriptor details(profile_path_, false);
524 media_request_context_.reset(InitializeMediaRequestContext(main_context,
525 details));
527 if (IsDomainReliabilityMonitoringEnabled()) {
528 domain_reliability_monitor_.reset(
529 new domain_reliability::DomainReliabilityMonitor(
530 main_context, kDomainReliabilityUploadReporterString));
531 domain_reliability_monitor_->AddBakedInConfigs();
532 network_delegate()->set_domain_reliability_monitor(
533 domain_reliability_monitor_.get());
536 lazy_params_.reset();
539 void ProfileImplIOData::
540 InitializeExtensionsRequestContext(ProfileParams* profile_params) const {
541 ChromeURLRequestContext* extensions_context = extensions_request_context();
542 IOThread* const io_thread = profile_params->io_thread;
543 IOThread::Globals* const io_thread_globals = io_thread->globals();
544 ApplyProfileParamsToContext(extensions_context);
546 extensions_context->set_transport_security_state(transport_security_state());
548 extensions_context->set_net_log(io_thread->net_log());
550 extensions_context->set_throttler_manager(
551 io_thread_globals->throttler_manager.get());
553 content::CookieStoreConfig cookie_config(
554 lazy_params_->extensions_cookie_path,
555 lazy_params_->session_cookie_mode,
556 NULL, NULL);
557 cookie_config.crypto_delegate =
558 chrome_browser_net::GetCookieCryptoDelegate();
559 net::CookieStore* extensions_cookie_store =
560 content::CreateCookieStore(cookie_config);
561 // Enable cookies for devtools and extension URLs.
562 const char* schemes[] = {content::kChromeDevToolsScheme,
563 extensions::kExtensionScheme};
564 extensions_cookie_store->GetCookieMonster()->SetCookieableSchemes(schemes, 2);
565 extensions_context->set_cookie_store(extensions_cookie_store);
567 scoped_ptr<net::URLRequestJobFactoryImpl> extensions_job_factory(
568 new net::URLRequestJobFactoryImpl());
569 // TODO(shalev): The extensions_job_factory has a NULL NetworkDelegate.
570 // Without a network_delegate, this protocol handler will never
571 // handle file: requests, but as a side effect it makes
572 // job_factory::IsHandledProtocol return true, which prevents attempts to
573 // handle the protocol externally. We pass NULL in to
574 // SetUpJobFactory() to get this effect.
575 extensions_job_factory_ = SetUpJobFactoryDefaults(
576 extensions_job_factory.Pass(),
577 content::ProtocolHandlerScopedVector(),
578 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>(),
579 NULL,
580 ftp_factory_.get());
581 extensions_context->set_job_factory(extensions_job_factory_.get());
584 ChromeURLRequestContext* ProfileImplIOData::InitializeAppRequestContext(
585 ChromeURLRequestContext* main_context,
586 const StoragePartitionDescriptor& partition_descriptor,
587 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
588 protocol_handler_interceptor,
589 content::ProtocolHandlerMap* protocol_handlers,
590 content::ProtocolHandlerScopedVector protocol_interceptors) const {
591 // Copy most state from the main context.
592 AppRequestContext* context = new AppRequestContext();
593 context->CopyFrom(main_context);
595 base::FilePath cookie_path = partition_descriptor.path.Append(
596 chrome::kCookieFilename);
597 base::FilePath cache_path =
598 partition_descriptor.path.Append(chrome::kCacheDirname);
600 // Use a separate HTTP disk cache for isolated apps.
601 net::HttpCache::BackendFactory* app_backend = NULL;
602 if (partition_descriptor.in_memory) {
603 app_backend = net::HttpCache::DefaultBackend::InMemory(0);
604 } else {
605 app_backend = new net::HttpCache::DefaultBackend(
606 net::DISK_CACHE,
607 ChooseCacheBackendType(),
608 cache_path,
609 app_cache_max_size_,
610 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)
611 .get());
613 net::HttpNetworkSession* main_network_session =
614 main_http_factory_->GetSession();
615 net::HttpCache* app_http_cache =
616 new net::HttpCache(main_network_session, app_backend);
618 scoped_refptr<net::CookieStore> cookie_store = NULL;
619 if (partition_descriptor.in_memory) {
620 cookie_store = content::CreateCookieStore(content::CookieStoreConfig());
621 } else if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
622 // Don't use existing cookies and use an in-memory store.
623 // TODO(creis): We should have a cookie delegate for notifying the cookie
624 // extensions API, but we need to update it to understand isolated apps
625 // first.
626 cookie_store = content::CreateCookieStore(content::CookieStoreConfig());
627 app_http_cache->set_mode(
628 chrome_browser_net::IsCookieRecordMode() ?
629 net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
632 // Use an app-specific cookie store.
633 if (!cookie_store.get()) {
634 DCHECK(!cookie_path.empty());
636 // TODO(creis): We should have a cookie delegate for notifying the cookie
637 // extensions API, but we need to update it to understand isolated apps
638 // first.
639 content::CookieStoreConfig cookie_config(
640 cookie_path,
641 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
642 NULL, NULL);
643 cookie_config.crypto_delegate =
644 chrome_browser_net::GetCookieCryptoDelegate();
645 cookie_store = content::CreateCookieStore(cookie_config);
648 // Transfer ownership of the cookies and cache to AppRequestContext.
649 context->SetCookieStore(cookie_store.get());
650 context->SetHttpTransactionFactory(
651 scoped_ptr<net::HttpTransactionFactory>(app_http_cache));
653 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
654 new net::URLRequestJobFactoryImpl());
655 InstallProtocolHandlers(job_factory.get(), protocol_handlers);
656 scoped_ptr<net::URLRequestJobFactory> top_job_factory(
657 SetUpJobFactoryDefaults(job_factory.Pass(),
658 protocol_interceptors.Pass(),
659 protocol_handler_interceptor.Pass(),
660 network_delegate(),
661 ftp_factory_.get()));
662 context->SetJobFactory(top_job_factory.Pass());
664 return context;
667 ChromeURLRequestContext*
668 ProfileImplIOData::InitializeMediaRequestContext(
669 ChromeURLRequestContext* original_context,
670 const StoragePartitionDescriptor& partition_descriptor) const {
671 // Copy most state from the original context.
672 MediaRequestContext* context = new MediaRequestContext();
673 context->CopyFrom(original_context);
675 // For in-memory context, return immediately after creating the new
676 // context before attaching a separate cache. It is important to return
677 // a new context rather than just reusing |original_context| because
678 // the caller expects to take ownership of the pointer.
679 if (partition_descriptor.in_memory)
680 return context;
682 using content::StoragePartition;
683 base::FilePath cache_path;
684 int cache_max_size = app_media_cache_max_size_;
685 if (partition_descriptor.path == profile_path_) {
686 // lazy_params_ is only valid for the default media context creation.
687 cache_path = lazy_params_->media_cache_path;
688 cache_max_size = lazy_params_->media_cache_max_size;
689 } else {
690 cache_path = partition_descriptor.path.Append(chrome::kMediaCacheDirname);
693 // Use a separate HTTP disk cache for isolated apps.
694 net::HttpCache::BackendFactory* media_backend =
695 new net::HttpCache::DefaultBackend(
696 net::MEDIA_CACHE,
697 ChooseCacheBackendType(),
698 cache_path,
699 cache_max_size,
700 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)
701 .get());
702 net::HttpNetworkSession* main_network_session =
703 main_http_factory_->GetSession();
704 scoped_ptr<net::HttpTransactionFactory> media_http_cache(
705 new net::HttpCache(main_network_session, media_backend));
707 // Transfer ownership of the cache to MediaRequestContext.
708 context->SetHttpTransactionFactory(media_http_cache.Pass());
710 // Note that we do not create a new URLRequestJobFactory because
711 // the media context should behave exactly like its parent context
712 // in all respects except for cache behavior on media subresources.
713 // The CopyFrom() step above means that our media context will use
714 // the same URLRequestJobFactory instance that our parent context does.
716 return context;
719 ChromeURLRequestContext*
720 ProfileImplIOData::AcquireMediaRequestContext() const {
721 DCHECK(media_request_context_);
722 return media_request_context_.get();
725 ChromeURLRequestContext* ProfileImplIOData::AcquireIsolatedAppRequestContext(
726 ChromeURLRequestContext* main_context,
727 const StoragePartitionDescriptor& partition_descriptor,
728 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
729 protocol_handler_interceptor,
730 content::ProtocolHandlerMap* protocol_handlers,
731 content::ProtocolHandlerScopedVector protocol_interceptors) const {
732 // We create per-app contexts on demand, unlike the others above.
733 ChromeURLRequestContext* app_request_context =
734 InitializeAppRequestContext(main_context,
735 partition_descriptor,
736 protocol_handler_interceptor.Pass(),
737 protocol_handlers,
738 protocol_interceptors.Pass());
739 DCHECK(app_request_context);
740 return app_request_context;
743 ChromeURLRequestContext*
744 ProfileImplIOData::AcquireIsolatedMediaRequestContext(
745 ChromeURLRequestContext* app_context,
746 const StoragePartitionDescriptor& partition_descriptor) const {
747 // We create per-app media contexts on demand, unlike the others above.
748 ChromeURLRequestContext* media_request_context =
749 InitializeMediaRequestContext(app_context, partition_descriptor);
750 DCHECK(media_request_context);
751 return media_request_context;
754 void ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread(
755 base::Time time,
756 const base::Closure& completion) {
757 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
758 DCHECK(initialized());
760 DCHECK(transport_security_state());
761 // Completes synchronously.
762 transport_security_state()->DeleteAllDynamicDataSince(time);
763 DCHECK(http_server_properties_manager_);
764 http_server_properties_manager_->Clear(completion);
767 void ProfileImplIOData::ClearDomainReliabilityMonitorOnIOThread(
768 domain_reliability::DomainReliabilityClearMode mode,
769 const base::Closure& completion) {
770 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
771 DCHECK(initialized());
773 if (domain_reliability_monitor_)
774 domain_reliability_monitor_->ClearBrowsingData(mode);
776 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion);