Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / profiles / profile_impl_io_data.cc
blobeb1e734e83f33098988d371dcbf3e335d33709cd
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_factory.h"
28 #include "chrome/browser/net/predictor.h"
29 #include "chrome/browser/net/quota_policy_channel_id_store.h"
30 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_configurator.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/chrome_version_info.h"
35 #include "chrome/common/pref_names.h"
36 #include "chrome/common/url_constants.h"
37 #include "components/data_reduction_proxy/browser/data_reduction_proxy_statistics_prefs.h"
38 #include "components/domain_reliability/monitor.h"
39 #include "content/public/browser/browser_thread.h"
40 #include "content/public/browser/cookie_store_factory.h"
41 #include "content/public/browser/notification_service.h"
42 #include "content/public/browser/resource_context.h"
43 #include "content/public/browser/storage_partition.h"
44 #include "extensions/browser/extension_protocols.h"
45 #include "extensions/common/constants.h"
46 #include "net/base/cache_type.h"
47 #include "net/base/sdch_dictionary_fetcher.h"
48 #include "net/base/sdch_manager.h"
49 #include "net/ftp/ftp_network_layer.h"
50 #include "net/http/http_cache.h"
51 #include "net/http/http_server_properties_manager.h"
52 #include "net/ssl/channel_id_service.h"
53 #include "net/url_request/url_request_job_factory_impl.h"
54 #include "storage/browser/quota/special_storage_policy.h"
56 #if defined(SPDY_PROXY_AUTH_ORIGIN)
57 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
58 #include "components/data_reduction_proxy/browser/data_reduction_proxy_auth_request_handler.h"
59 #include "components/data_reduction_proxy/browser/data_reduction_proxy_protocol.h"
60 #include "components/data_reduction_proxy/browser/data_reduction_proxy_usage_stats.h"
61 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names.h"
62 #endif // defined(SPDY_PROXY_AUTH_ORIGIN)
64 namespace {
66 net::BackendType ChooseCacheBackendType() {
67 #if defined(OS_ANDROID)
68 return net::CACHE_BACKEND_SIMPLE;
69 #else
70 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
71 if (command_line.HasSwitch(switches::kUseSimpleCacheBackend)) {
72 const std::string opt_value =
73 command_line.GetSwitchValueASCII(switches::kUseSimpleCacheBackend);
74 if (LowerCaseEqualsASCII(opt_value, "off"))
75 return net::CACHE_BACKEND_BLOCKFILE;
76 if (opt_value == "" || LowerCaseEqualsASCII(opt_value, "on"))
77 return net::CACHE_BACKEND_SIMPLE;
79 const std::string experiment_name =
80 base::FieldTrialList::FindFullName("SimpleCacheTrial");
81 if (experiment_name == "ExperimentYes" ||
82 experiment_name == "ExperimentYes2") {
83 return net::CACHE_BACKEND_SIMPLE;
85 return net::CACHE_BACKEND_BLOCKFILE;
86 #endif
89 } // namespace
91 using content::BrowserThread;
92 #if defined(SPDY_PROXY_AUTH_ORIGIN)
93 using data_reduction_proxy::DataReductionProxyParams;
94 #endif // defined(SPDY_PROXY_AUTH_ORIGIN)
96 ProfileImplIOData::Handle::Handle(Profile* profile)
97 : io_data_(new ProfileImplIOData),
98 profile_(profile),
99 initialized_(false) {
100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
101 DCHECK(profile);
104 ProfileImplIOData::Handle::~Handle() {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
106 #if defined(SPDY_PROXY_AUTH_ORIGIN)
107 io_data_->data_reduction_proxy_statistics_prefs_->WritePrefs();
108 #endif
110 if (io_data_->predictor_ != NULL) {
111 // io_data_->predictor_ might be NULL if Init() was never called
112 // (i.e. we shut down before ProfileImpl::DoFinalInit() got called).
113 bool save_prefs = true;
114 #if defined(OS_CHROMEOS)
115 save_prefs = !chromeos::ProfileHelper::IsSigninProfile(profile_);
116 #endif
117 if (save_prefs)
118 io_data_->predictor_->SaveStateForNextStartupAndTrim();
119 io_data_->predictor_->ShutdownOnUIThread();
122 if (io_data_->http_server_properties_manager_)
123 io_data_->http_server_properties_manager_->ShutdownOnPrefThread();
125 if (io_data_->domain_reliability_monitor_)
126 io_data_->domain_reliability_monitor_->DestroyReportingPref();
128 io_data_->ShutdownOnUIThread(GetAllContextGetters().Pass());
131 void ProfileImplIOData::Handle::Init(
132 const base::FilePath& cookie_path,
133 const base::FilePath& channel_id_path,
134 const base::FilePath& cache_path,
135 int cache_max_size,
136 const base::FilePath& media_cache_path,
137 int media_cache_max_size,
138 const base::FilePath& extensions_cookie_path,
139 const base::FilePath& profile_path,
140 const base::FilePath& infinite_cache_path,
141 chrome_browser_net::Predictor* predictor,
142 content::CookieStoreConfig::SessionCookieMode session_cookie_mode,
143 storage::SpecialStoragePolicy* special_storage_policy,
144 scoped_ptr<domain_reliability::DomainReliabilityMonitor>
145 domain_reliability_monitor,
146 const base::Callback<void(bool)>& data_reduction_proxy_unavailable,
147 scoped_ptr<DataReductionProxyChromeConfigurator>
148 data_reduction_proxy_chrome_configurator,
149 scoped_ptr<data_reduction_proxy::DataReductionProxyParams>
150 data_reduction_proxy_params,
151 scoped_ptr<data_reduction_proxy::DataReductionProxyStatisticsPrefs>
152 data_reduction_proxy_statistics_prefs) {
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
154 DCHECK(!io_data_->lazy_params_);
155 DCHECK(predictor);
157 LazyParams* lazy_params = new LazyParams();
159 lazy_params->cookie_path = cookie_path;
160 lazy_params->channel_id_path = channel_id_path;
161 lazy_params->cache_path = cache_path;
162 lazy_params->cache_max_size = cache_max_size;
163 lazy_params->media_cache_path = media_cache_path;
164 lazy_params->media_cache_max_size = media_cache_max_size;
165 lazy_params->extensions_cookie_path = extensions_cookie_path;
166 lazy_params->infinite_cache_path = infinite_cache_path;
167 lazy_params->session_cookie_mode = session_cookie_mode;
168 lazy_params->special_storage_policy = special_storage_policy;
170 io_data_->lazy_params_.reset(lazy_params);
172 // Keep track of profile path and cache sizes separately so we can use them
173 // on demand when creating storage isolated URLRequestContextGetters.
174 io_data_->profile_path_ = profile_path;
175 io_data_->app_cache_max_size_ = cache_max_size;
176 io_data_->app_media_cache_max_size_ = media_cache_max_size;
178 io_data_->predictor_.reset(predictor);
179 io_data_->domain_reliability_monitor_ = domain_reliability_monitor.Pass();
181 io_data_->InitializeMetricsEnabledStateOnUIThread();
182 if (io_data_->domain_reliability_monitor_)
183 io_data_->domain_reliability_monitor_->MoveToNetworkThread();
185 #if defined(SPDY_PROXY_AUTH_ORIGIN)
186 io_data_->data_reduction_proxy_unavailable_callback_ =
187 data_reduction_proxy_unavailable;
188 io_data_->data_reduction_proxy_chrome_configurator_ =
189 data_reduction_proxy_chrome_configurator.Pass();
190 io_data_->data_reduction_proxy_params_ =
191 data_reduction_proxy_params.Pass();
192 io_data_->data_reduction_proxy_statistics_prefs_ =
193 data_reduction_proxy_statistics_prefs.Pass();
194 #endif // defined(SPDY_PROXY_AUTH_ORIGIN)
197 content::ResourceContext*
198 ProfileImplIOData::Handle::GetResourceContext() const {
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
200 LazyInitialize();
201 return GetResourceContextNoInit();
204 content::ResourceContext*
205 ProfileImplIOData::Handle::GetResourceContextNoInit() const {
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
207 // Don't call LazyInitialize here, since the resource context is created at
208 // the beginning of initalization and is used by some members while they're
209 // being initialized (i.e. AppCacheService).
210 return io_data_->GetResourceContext();
213 scoped_refptr<ChromeURLRequestContextGetter>
214 ProfileImplIOData::Handle::CreateMainRequestContextGetter(
215 content::ProtocolHandlerMap* protocol_handlers,
216 content::URLRequestInterceptorScopedVector request_interceptors,
217 PrefService* local_state,
218 IOThread* io_thread) const {
219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
220 LazyInitialize();
221 DCHECK(!main_request_context_getter_.get());
222 main_request_context_getter_ = ChromeURLRequestContextGetter::Create(
223 profile_, io_data_, protocol_handlers, request_interceptors.Pass());
225 io_data_->predictor_
226 ->InitNetworkPredictor(profile_->GetPrefs(),
227 local_state,
228 io_thread,
229 main_request_context_getter_.get(),
230 io_data_);
232 content::NotificationService::current()->Notify(
233 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
234 content::Source<Profile>(profile_),
235 content::NotificationService::NoDetails());
236 return main_request_context_getter_;
239 scoped_refptr<ChromeURLRequestContextGetter>
240 ProfileImplIOData::Handle::GetMediaRequestContextGetter() const {
241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
242 LazyInitialize();
243 if (!media_request_context_getter_.get()) {
244 media_request_context_getter_ =
245 ChromeURLRequestContextGetter::CreateForMedia(profile_, io_data_);
247 return media_request_context_getter_;
250 scoped_refptr<ChromeURLRequestContextGetter>
251 ProfileImplIOData::Handle::GetExtensionsRequestContextGetter() const {
252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
253 LazyInitialize();
254 if (!extensions_request_context_getter_.get()) {
255 extensions_request_context_getter_ =
256 ChromeURLRequestContextGetter::CreateForExtensions(profile_, io_data_);
258 return extensions_request_context_getter_;
261 scoped_refptr<ChromeURLRequestContextGetter>
262 ProfileImplIOData::Handle::CreateIsolatedAppRequestContextGetter(
263 const base::FilePath& partition_path,
264 bool in_memory,
265 content::ProtocolHandlerMap* protocol_handlers,
266 content::URLRequestInterceptorScopedVector request_interceptors) const {
267 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
268 // Check that the partition_path is not the same as the base profile path. We
269 // expect isolated partition, which will never go to the default profile path.
270 CHECK(partition_path != profile_->GetPath());
271 LazyInitialize();
273 // Keep a map of request context getters, one per requested storage partition.
274 StoragePartitionDescriptor descriptor(partition_path, in_memory);
275 ChromeURLRequestContextGetterMap::iterator iter =
276 app_request_context_getter_map_.find(descriptor);
277 if (iter != app_request_context_getter_map_.end())
278 return iter->second;
280 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
281 protocol_handler_interceptor(
282 ProtocolHandlerRegistryFactory::GetForBrowserContext(profile_)->
283 CreateJobInterceptorFactory());
284 ChromeURLRequestContextGetter* context =
285 ChromeURLRequestContextGetter::CreateForIsolatedApp(
286 profile_,
287 io_data_,
288 descriptor,
289 protocol_handler_interceptor.Pass(),
290 protocol_handlers,
291 request_interceptors.Pass());
292 app_request_context_getter_map_[descriptor] = context;
294 return context;
297 scoped_refptr<ChromeURLRequestContextGetter>
298 ProfileImplIOData::Handle::GetIsolatedMediaRequestContextGetter(
299 const base::FilePath& partition_path,
300 bool in_memory) const {
301 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
302 // We must have a non-default path, or this will act like the default media
303 // context.
304 CHECK(partition_path != profile_->GetPath());
305 LazyInitialize();
307 // Keep a map of request context getters, one per requested storage partition.
308 StoragePartitionDescriptor descriptor(partition_path, in_memory);
309 ChromeURLRequestContextGetterMap::iterator iter =
310 isolated_media_request_context_getter_map_.find(descriptor);
311 if (iter != isolated_media_request_context_getter_map_.end())
312 return iter->second;
314 // Get the app context as the starting point for the media context, so that
315 // it uses the app's cookie store.
316 ChromeURLRequestContextGetterMap::const_iterator app_iter =
317 app_request_context_getter_map_.find(descriptor);
318 DCHECK(app_iter != app_request_context_getter_map_.end());
319 ChromeURLRequestContextGetter* app_context = app_iter->second.get();
320 ChromeURLRequestContextGetter* context =
321 ChromeURLRequestContextGetter::CreateForIsolatedMedia(
322 profile_, app_context, io_data_, descriptor);
323 isolated_media_request_context_getter_map_[descriptor] = context;
325 return context;
328 DevToolsNetworkController*
329 ProfileImplIOData::Handle::GetDevToolsNetworkController() const {
330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
331 return io_data_->network_controller();
334 void ProfileImplIOData::Handle::ClearNetworkingHistorySince(
335 base::Time time,
336 const base::Closure& completion) {
337 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
338 LazyInitialize();
340 BrowserThread::PostTask(
341 BrowserThread::IO, FROM_HERE,
342 base::Bind(
343 &ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread,
344 base::Unretained(io_data_),
345 time,
346 completion));
349 void ProfileImplIOData::Handle::LazyInitialize() const {
350 if (initialized_)
351 return;
353 // Set initialized_ to true at the beginning in case any of the objects
354 // below try to get the ResourceContext pointer.
355 initialized_ = true;
356 PrefService* pref_service = profile_->GetPrefs();
357 io_data_->http_server_properties_manager_ =
358 chrome_browser_net::HttpServerPropertiesManagerFactory::CreateManager(
359 pref_service);
360 io_data_->set_http_server_properties(
361 scoped_ptr<net::HttpServerProperties>(
362 io_data_->http_server_properties_manager_));
363 io_data_->session_startup_pref()->Init(
364 prefs::kRestoreOnStartup, pref_service);
365 io_data_->session_startup_pref()->MoveToThread(
366 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
367 #if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
368 io_data_->safe_browsing_enabled()->Init(prefs::kSafeBrowsingEnabled,
369 pref_service);
370 io_data_->safe_browsing_enabled()->MoveToThread(
371 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
372 #endif
373 #if defined(SPDY_PROXY_AUTH_ORIGIN)
374 io_data_->data_reduction_proxy_enabled()->Init(
375 data_reduction_proxy::prefs::kDataReductionProxyEnabled, pref_service);
376 io_data_->data_reduction_proxy_enabled()->MoveToThread(
377 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
378 #endif // defined(SPDY_PROXY_AUTH_ORIGIN)
379 io_data_->InitializeOnUIThread(profile_);
382 scoped_ptr<ProfileIOData::ChromeURLRequestContextGetterVector>
383 ProfileImplIOData::Handle::GetAllContextGetters() {
384 ChromeURLRequestContextGetterMap::iterator iter;
385 scoped_ptr<ChromeURLRequestContextGetterVector> context_getters(
386 new ChromeURLRequestContextGetterVector());
388 iter = isolated_media_request_context_getter_map_.begin();
389 for (; iter != isolated_media_request_context_getter_map_.end(); ++iter)
390 context_getters->push_back(iter->second);
392 iter = app_request_context_getter_map_.begin();
393 for (; iter != app_request_context_getter_map_.end(); ++iter)
394 context_getters->push_back(iter->second);
396 if (extensions_request_context_getter_.get())
397 context_getters->push_back(extensions_request_context_getter_);
399 if (media_request_context_getter_.get())
400 context_getters->push_back(media_request_context_getter_);
402 if (main_request_context_getter_.get())
403 context_getters->push_back(main_request_context_getter_);
405 return context_getters.Pass();
408 ProfileImplIOData::LazyParams::LazyParams()
409 : cache_max_size(0),
410 media_cache_max_size(0),
411 session_cookie_mode(
412 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES) {}
414 ProfileImplIOData::LazyParams::~LazyParams() {}
416 ProfileImplIOData::ProfileImplIOData()
417 : ProfileIOData(Profile::REGULAR_PROFILE),
418 http_server_properties_manager_(NULL),
419 app_cache_max_size_(0),
420 app_media_cache_max_size_(0) {
423 ProfileImplIOData::~ProfileImplIOData() {
424 if (initialized())
425 network_delegate()->set_domain_reliability_monitor(NULL);
427 DestroyResourceContext();
429 if (media_request_context_)
430 media_request_context_->AssertNoURLRequests();
433 void ProfileImplIOData::InitializeInternal(
434 ProfileParams* profile_params,
435 content::ProtocolHandlerMap* protocol_handlers,
436 content::URLRequestInterceptorScopedVector request_interceptors) const {
437 net::URLRequestContext* main_context = main_request_context();
439 IOThread* const io_thread = profile_params->io_thread;
440 IOThread::Globals* const io_thread_globals = io_thread->globals();
442 #if defined(SPDY_PROXY_AUTH_ORIGIN)
443 data_reduction_proxy_auth_request_handler_.reset(
444 new data_reduction_proxy::DataReductionProxyAuthRequestHandler(
445 DataReductionProxyChromeSettings::GetClient(),
446 chrome::VersionInfo().Version(),
447 data_reduction_proxy_params_.get(),
448 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
449 data_reduction_proxy_usage_stats_.reset(
450 new data_reduction_proxy::DataReductionProxyUsageStats(
451 data_reduction_proxy_params_.get(),
452 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)
453 .get()));
454 data_reduction_proxy_usage_stats_->set_unavailable_callback(
455 data_reduction_proxy_unavailable_callback_);
458 network_delegate()->set_data_reduction_proxy_params(
459 data_reduction_proxy_params_.get());
460 network_delegate()->set_data_reduction_proxy_usage_stats(
461 data_reduction_proxy_usage_stats_.get());
462 network_delegate()->set_data_reduction_proxy_auth_request_handler(
463 data_reduction_proxy_auth_request_handler_.get());
464 network_delegate()->set_data_reduction_proxy_statistics_prefs(
465 data_reduction_proxy_statistics_prefs_.get());
466 network_delegate()->set_on_resolve_proxy_handler(
467 base::Bind(data_reduction_proxy::OnResolveProxyHandler));
468 network_delegate()->set_proxy_config_getter(
469 base::Bind(
470 &DataReductionProxyChromeConfigurator::GetProxyConfigOnIO,
471 base::Unretained(data_reduction_proxy_chrome_configurator_.get())));
472 #endif // defined(SPDY_PROXY_AUTH_ORIGIN)
474 network_delegate()->set_predictor(predictor_.get());
476 // Initialize context members.
478 ApplyProfileParamsToContext(main_context);
480 if (http_server_properties_manager_)
481 http_server_properties_manager_->InitializeOnNetworkThread();
483 main_context->set_transport_security_state(transport_security_state());
485 main_context->set_net_log(io_thread->net_log());
487 main_context->set_network_delegate(network_delegate());
489 main_context->set_http_server_properties(http_server_properties());
491 main_context->set_host_resolver(
492 io_thread_globals->host_resolver.get());
493 main_context->set_cert_transparency_verifier(
494 io_thread_globals->cert_transparency_verifier.get());
495 main_context->set_http_auth_handler_factory(
496 io_thread_globals->http_auth_handler_factory.get());
498 main_context->set_fraudulent_certificate_reporter(
499 fraudulent_certificate_reporter());
501 main_context->set_throttler_manager(
502 io_thread_globals->throttler_manager.get());
504 main_context->set_proxy_service(proxy_service());
506 scoped_refptr<net::CookieStore> cookie_store = NULL;
507 net::ChannelIDService* channel_id_service = NULL;
508 if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
509 // Don't use existing cookies and use an in-memory store.
510 using content::CookieStoreConfig;
511 cookie_store = content::CreateCookieStore(CookieStoreConfig(
512 base::FilePath(),
513 CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
514 NULL,
515 profile_params->cookie_monster_delegate.get()));
516 // Don't use existing channel ids and use an in-memory store.
517 channel_id_service = new net::ChannelIDService(
518 new net::DefaultChannelIDStore(NULL),
519 base::WorkerPool::GetTaskRunner(true));
523 // setup cookie store
524 if (!cookie_store.get()) {
525 DCHECK(!lazy_params_->cookie_path.empty());
527 content::CookieStoreConfig cookie_config(
528 lazy_params_->cookie_path,
529 lazy_params_->session_cookie_mode,
530 lazy_params_->special_storage_policy.get(),
531 profile_params->cookie_monster_delegate.get());
532 cookie_config.crypto_delegate =
533 chrome_browser_net::GetCookieCryptoDelegate();
534 cookie_store = content::CreateCookieStore(cookie_config);
537 main_context->set_cookie_store(cookie_store.get());
539 // Setup server bound cert service.
540 if (!channel_id_service) {
541 DCHECK(!lazy_params_->channel_id_path.empty());
543 scoped_refptr<QuotaPolicyChannelIDStore> channel_id_db =
544 new QuotaPolicyChannelIDStore(
545 lazy_params_->channel_id_path,
546 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
547 BrowserThread::GetBlockingPool()->GetSequenceToken()),
548 lazy_params_->special_storage_policy.get());
549 channel_id_service = new net::ChannelIDService(
550 new net::DefaultChannelIDStore(channel_id_db.get()),
551 base::WorkerPool::GetTaskRunner(true));
554 set_channel_id_service(channel_id_service);
555 main_context->set_channel_id_service(channel_id_service);
557 net::HttpCache::DefaultBackend* main_backend =
558 new net::HttpCache::DefaultBackend(
559 net::DISK_CACHE,
560 ChooseCacheBackendType(),
561 lazy_params_->cache_path,
562 lazy_params_->cache_max_size,
563 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
564 scoped_ptr<net::HttpCache> main_cache = CreateMainHttpFactory(
565 profile_params, main_backend);
566 main_cache->InitializeInfiniteCache(lazy_params_->infinite_cache_path);
568 if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
569 main_cache->set_mode(
570 chrome_browser_net::IsCookieRecordMode() ?
571 net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
574 main_http_factory_.reset(main_cache.release());
575 main_context->set_http_transaction_factory(main_http_factory_.get());
577 #if !defined(DISABLE_FTP_SUPPORT)
578 ftp_factory_.reset(
579 new net::FtpNetworkLayer(io_thread_globals->host_resolver.get()));
580 #endif // !defined(DISABLE_FTP_SUPPORT)
582 scoped_ptr<net::URLRequestJobFactoryImpl> main_job_factory(
583 new net::URLRequestJobFactoryImpl());
584 InstallProtocolHandlers(main_job_factory.get(), protocol_handlers);
585 main_job_factory_ = SetUpJobFactoryDefaults(
586 main_job_factory.Pass(),
587 request_interceptors.Pass(),
588 profile_params->protocol_handler_interceptor.Pass(),
589 network_delegate(),
590 ftp_factory_.get());
591 main_context->set_job_factory(main_job_factory_.get());
593 #if defined(ENABLE_EXTENSIONS)
594 InitializeExtensionsRequestContext(profile_params);
595 #endif
597 // Setup the SDCHManager for this profile.
598 sdch_manager_.reset(new net::SdchManager);
599 sdch_manager_->set_sdch_fetcher(scoped_ptr<net::SdchFetcher>(
600 new net::SdchDictionaryFetcher(sdch_manager_.get(),
601 main_context)).Pass());
602 main_context->set_sdch_manager(sdch_manager_.get());
604 // Create a media request context based on the main context, but using a
605 // media cache. It shares the same job factory as the main context.
606 StoragePartitionDescriptor details(profile_path_, false);
607 media_request_context_.reset(InitializeMediaRequestContext(main_context,
608 details));
610 if (domain_reliability_monitor_) {
611 domain_reliability::DomainReliabilityMonitor* monitor =
612 domain_reliability_monitor_.get();
613 monitor->InitURLRequestContext(main_context);
614 monitor->AddBakedInConfigs();
615 network_delegate()->set_domain_reliability_monitor(monitor);
618 lazy_params_.reset();
621 void ProfileImplIOData::
622 InitializeExtensionsRequestContext(ProfileParams* profile_params) const {
623 net::URLRequestContext* extensions_context = extensions_request_context();
624 IOThread* const io_thread = profile_params->io_thread;
625 IOThread::Globals* const io_thread_globals = io_thread->globals();
626 ApplyProfileParamsToContext(extensions_context);
628 extensions_context->set_transport_security_state(transport_security_state());
630 extensions_context->set_net_log(io_thread->net_log());
632 extensions_context->set_throttler_manager(
633 io_thread_globals->throttler_manager.get());
635 content::CookieStoreConfig cookie_config(
636 lazy_params_->extensions_cookie_path,
637 lazy_params_->session_cookie_mode,
638 NULL, NULL);
639 cookie_config.crypto_delegate =
640 chrome_browser_net::GetCookieCryptoDelegate();
641 net::CookieStore* extensions_cookie_store =
642 content::CreateCookieStore(cookie_config);
643 // Enable cookies for devtools and extension URLs.
644 const char* const schemes[] = {
645 content::kChromeDevToolsScheme,
646 extensions::kExtensionScheme
648 extensions_cookie_store->GetCookieMonster()->SetCookieableSchemes(
649 schemes, arraysize(schemes));
650 extensions_context->set_cookie_store(extensions_cookie_store);
652 scoped_ptr<net::URLRequestJobFactoryImpl> extensions_job_factory(
653 new net::URLRequestJobFactoryImpl());
654 // TODO(shalev): The extensions_job_factory has a NULL NetworkDelegate.
655 // Without a network_delegate, this protocol handler will never
656 // handle file: requests, but as a side effect it makes
657 // job_factory::IsHandledProtocol return true, which prevents attempts to
658 // handle the protocol externally. We pass NULL in to
659 // SetUpJobFactory() to get this effect.
660 extensions_job_factory_ = SetUpJobFactoryDefaults(
661 extensions_job_factory.Pass(),
662 content::URLRequestInterceptorScopedVector(),
663 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>(),
664 NULL,
665 ftp_factory_.get());
666 extensions_context->set_job_factory(extensions_job_factory_.get());
669 net::URLRequestContext* ProfileImplIOData::InitializeAppRequestContext(
670 net::URLRequestContext* main_context,
671 const StoragePartitionDescriptor& partition_descriptor,
672 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
673 protocol_handler_interceptor,
674 content::ProtocolHandlerMap* protocol_handlers,
675 content::URLRequestInterceptorScopedVector request_interceptors) const {
676 // Copy most state from the main context.
677 AppRequestContext* context = new AppRequestContext();
678 context->CopyFrom(main_context);
680 base::FilePath cookie_path = partition_descriptor.path.Append(
681 chrome::kCookieFilename);
682 base::FilePath cache_path =
683 partition_descriptor.path.Append(chrome::kCacheDirname);
685 // Use a separate HTTP disk cache for isolated apps.
686 net::HttpCache::BackendFactory* app_backend = NULL;
687 if (partition_descriptor.in_memory) {
688 app_backend = net::HttpCache::DefaultBackend::InMemory(0);
689 } else {
690 app_backend = new net::HttpCache::DefaultBackend(
691 net::DISK_CACHE,
692 ChooseCacheBackendType(),
693 cache_path,
694 app_cache_max_size_,
695 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
697 net::HttpNetworkSession* main_network_session =
698 main_http_factory_->GetSession();
699 scoped_ptr<net::HttpCache> app_http_cache =
700 CreateHttpFactory(main_network_session, app_backend);
702 scoped_refptr<net::CookieStore> cookie_store = NULL;
703 if (partition_descriptor.in_memory) {
704 cookie_store = content::CreateCookieStore(content::CookieStoreConfig());
705 } else if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
706 // Don't use existing cookies and use an in-memory store.
707 // TODO(creis): We should have a cookie delegate for notifying the cookie
708 // extensions API, but we need to update it to understand isolated apps
709 // first.
710 cookie_store = content::CreateCookieStore(content::CookieStoreConfig());
711 app_http_cache->set_mode(
712 chrome_browser_net::IsCookieRecordMode() ?
713 net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
716 // Use an app-specific cookie store.
717 if (!cookie_store.get()) {
718 DCHECK(!cookie_path.empty());
720 // TODO(creis): We should have a cookie delegate for notifying the cookie
721 // extensions API, but we need to update it to understand isolated apps
722 // first.
723 content::CookieStoreConfig cookie_config(
724 cookie_path,
725 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
726 NULL, NULL);
727 cookie_config.crypto_delegate =
728 chrome_browser_net::GetCookieCryptoDelegate();
729 cookie_store = content::CreateCookieStore(cookie_config);
732 // Transfer ownership of the cookies and cache to AppRequestContext.
733 context->SetCookieStore(cookie_store.get());
734 context->SetHttpTransactionFactory(
735 scoped_ptr<net::HttpTransactionFactory>(
736 app_http_cache.PassAs<net::HttpTransactionFactory>()));
738 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
739 new net::URLRequestJobFactoryImpl());
740 InstallProtocolHandlers(job_factory.get(), protocol_handlers);
741 scoped_ptr<net::URLRequestJobFactory> top_job_factory(
742 SetUpJobFactoryDefaults(job_factory.Pass(),
743 request_interceptors.Pass(),
744 protocol_handler_interceptor.Pass(),
745 network_delegate(),
746 ftp_factory_.get()));
747 context->SetJobFactory(top_job_factory.Pass());
749 return context;
752 net::URLRequestContext*
753 ProfileImplIOData::InitializeMediaRequestContext(
754 net::URLRequestContext* original_context,
755 const StoragePartitionDescriptor& partition_descriptor) const {
756 // Copy most state from the original context.
757 MediaRequestContext* context = new MediaRequestContext();
758 context->CopyFrom(original_context);
760 // For in-memory context, return immediately after creating the new
761 // context before attaching a separate cache. It is important to return
762 // a new context rather than just reusing |original_context| because
763 // the caller expects to take ownership of the pointer.
764 if (partition_descriptor.in_memory)
765 return context;
767 using content::StoragePartition;
768 base::FilePath cache_path;
769 int cache_max_size = app_media_cache_max_size_;
770 if (partition_descriptor.path == profile_path_) {
771 // lazy_params_ is only valid for the default media context creation.
772 cache_path = lazy_params_->media_cache_path;
773 cache_max_size = lazy_params_->media_cache_max_size;
774 } else {
775 cache_path = partition_descriptor.path.Append(chrome::kMediaCacheDirname);
778 // Use a separate HTTP disk cache for isolated apps.
779 net::HttpCache::BackendFactory* media_backend =
780 new net::HttpCache::DefaultBackend(
781 net::MEDIA_CACHE,
782 ChooseCacheBackendType(),
783 cache_path,
784 cache_max_size,
785 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
786 net::HttpNetworkSession* main_network_session =
787 main_http_factory_->GetSession();
788 scoped_ptr<net::HttpCache> media_http_cache =
789 CreateHttpFactory(main_network_session, media_backend);
791 // Transfer ownership of the cache to MediaRequestContext.
792 context->SetHttpTransactionFactory(
793 media_http_cache.PassAs<net::HttpTransactionFactory>());
795 // Note that we do not create a new URLRequestJobFactory because
796 // the media context should behave exactly like its parent context
797 // in all respects except for cache behavior on media subresources.
798 // The CopyFrom() step above means that our media context will use
799 // the same URLRequestJobFactory instance that our parent context does.
801 return context;
804 net::URLRequestContext*
805 ProfileImplIOData::AcquireMediaRequestContext() const {
806 DCHECK(media_request_context_);
807 return media_request_context_.get();
810 net::URLRequestContext* ProfileImplIOData::AcquireIsolatedAppRequestContext(
811 net::URLRequestContext* main_context,
812 const StoragePartitionDescriptor& partition_descriptor,
813 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
814 protocol_handler_interceptor,
815 content::ProtocolHandlerMap* protocol_handlers,
816 content::URLRequestInterceptorScopedVector request_interceptors) const {
817 // We create per-app contexts on demand, unlike the others above.
818 net::URLRequestContext* app_request_context =
819 InitializeAppRequestContext(main_context,
820 partition_descriptor,
821 protocol_handler_interceptor.Pass(),
822 protocol_handlers,
823 request_interceptors.Pass());
824 DCHECK(app_request_context);
825 return app_request_context;
828 net::URLRequestContext*
829 ProfileImplIOData::AcquireIsolatedMediaRequestContext(
830 net::URLRequestContext* app_context,
831 const StoragePartitionDescriptor& partition_descriptor) const {
832 // We create per-app media contexts on demand, unlike the others above.
833 net::URLRequestContext* media_request_context =
834 InitializeMediaRequestContext(app_context, partition_descriptor);
835 DCHECK(media_request_context);
836 return media_request_context;
839 void ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread(
840 base::Time time,
841 const base::Closure& completion) {
842 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
843 DCHECK(initialized());
845 DCHECK(transport_security_state());
846 // Completes synchronously.
847 transport_security_state()->DeleteAllDynamicDataSince(time);
848 DCHECK(http_server_properties_manager_);
849 http_server_properties_manager_->Clear(completion);