Center distiller results on desktop
[chromium-blink-merge.git] / chrome / browser / profiles / profile_impl_io_data.cc
blob827c441ed01908000d7a54ef477667732eb3eabb
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 <set>
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/metrics/field_trial.h"
14 #include "base/prefs/json_pref_store.h"
15 #include "base/prefs/pref_filter.h"
16 #include "base/prefs/pref_member.h"
17 #include "base/prefs/pref_service.h"
18 #include "base/profiler/scoped_tracker.h"
19 #include "base/sequenced_task_runner.h"
20 #include "base/stl_util.h"
21 #include "base/strings/string_util.h"
22 #include "base/threading/sequenced_worker_pool.h"
23 #include "base/threading/worker_pool.h"
24 #include "chrome/browser/browser_process.h"
25 #include "chrome/browser/chrome_notification_types.h"
26 #include "chrome/browser/chromeos/profiles/profile_helper.h"
27 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
28 #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
29 #include "chrome/browser/io_thread.h"
30 #include "chrome/browser/net/chrome_net_log.h"
31 #include "chrome/browser/net/chrome_network_delegate.h"
32 #include "chrome/browser/net/connect_interceptor.h"
33 #include "chrome/browser/net/cookie_store_util.h"
34 #include "chrome/browser/net/http_server_properties_manager_factory.h"
35 #include "chrome/browser/net/predictor.h"
36 #include "chrome/browser/net/quota_policy_channel_id_store.h"
37 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_io_data.h"
38 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
39 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
40 #include "chrome/browser/profiles/profile.h"
41 #include "chrome/common/chrome_constants.h"
42 #include "chrome/common/chrome_switches.h"
43 #include "chrome/common/pref_names.h"
44 #include "chrome/common/url_constants.h"
45 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h"
46 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
47 #include "components/domain_reliability/monitor.h"
48 #include "content/public/browser/browser_thread.h"
49 #include "content/public/browser/cookie_store_factory.h"
50 #include "content/public/browser/notification_service.h"
51 #include "content/public/browser/resource_context.h"
52 #include "content/public/browser/storage_partition.h"
53 #include "extensions/browser/extension_protocols.h"
54 #include "extensions/common/constants.h"
55 #include "net/base/cache_type.h"
56 #include "net/base/sdch_manager.h"
57 #include "net/ftp/ftp_network_layer.h"
58 #include "net/http/http_cache.h"
59 #include "net/http/http_server_properties_manager.h"
60 #include "net/sdch/sdch_owner.h"
61 #include "net/ssl/channel_id_service.h"
62 #include "net/url_request/url_request_intercepting_job_factory.h"
63 #include "net/url_request/url_request_job_factory_impl.h"
64 #include "storage/browser/quota/special_storage_policy.h"
66 namespace {
68 net::BackendType ChooseCacheBackendType() {
69 #if defined(OS_ANDROID)
70 return net::CACHE_BACKEND_SIMPLE;
71 #else
72 const base::CommandLine& command_line =
73 *base::CommandLine::ForCurrentProcess();
74 if (command_line.HasSwitch(switches::kUseSimpleCacheBackend)) {
75 const std::string opt_value =
76 command_line.GetSwitchValueASCII(switches::kUseSimpleCacheBackend);
77 if (LowerCaseEqualsASCII(opt_value, "off"))
78 return net::CACHE_BACKEND_BLOCKFILE;
79 if (opt_value.empty() || LowerCaseEqualsASCII(opt_value, "on"))
80 return net::CACHE_BACKEND_SIMPLE;
82 const std::string experiment_name =
83 base::FieldTrialList::FindFullName("SimpleCacheTrial");
84 if (experiment_name == "ExperimentYes" ||
85 experiment_name == "ExperimentYes2") {
86 return net::CACHE_BACKEND_SIMPLE;
88 return net::CACHE_BACKEND_BLOCKFILE;
89 #endif
92 bool ShouldUseSdchPersistence() {
93 const std::string group =
94 base::FieldTrialList::FindFullName("SdchPersistence");
95 const base::CommandLine* command_line =
96 base::CommandLine::ForCurrentProcess();
97 if (command_line->HasSwitch(switches::kEnableSdchPersistence)) {
98 return true;
100 if (command_line->HasSwitch(switches::kDisableSdchPersistence)) {
101 return false;
103 return group == "Enabled";
106 } // namespace
108 using content::BrowserThread;
110 ProfileImplIOData::Handle::Handle(Profile* profile)
111 : io_data_(new ProfileImplIOData),
112 profile_(profile),
113 initialized_(false) {
114 DCHECK_CURRENTLY_ON(BrowserThread::UI);
115 DCHECK(profile);
118 ProfileImplIOData::Handle::~Handle() {
119 DCHECK_CURRENTLY_ON(BrowserThread::UI);
120 if (io_data_->predictor_ != NULL) {
121 // io_data_->predictor_ might be NULL if Init() was never called
122 // (i.e. we shut down before ProfileImpl::DoFinalInit() got called).
123 bool save_prefs = true;
124 #if defined(OS_CHROMEOS)
125 save_prefs = !chromeos::ProfileHelper::IsSigninProfile(profile_);
126 #endif
127 if (save_prefs)
128 io_data_->predictor_->SaveStateForNextStartupAndTrim();
129 io_data_->predictor_->ShutdownOnUIThread();
132 if (io_data_->http_server_properties_manager_)
133 io_data_->http_server_properties_manager_->ShutdownOnPrefThread();
135 // io_data_->data_reduction_proxy_io_data() might be NULL if Init() was
136 // never called.
137 if (io_data_->data_reduction_proxy_io_data())
138 io_data_->data_reduction_proxy_io_data()->ShutdownOnUIThread();
140 io_data_->ShutdownOnUIThread(GetAllContextGetters().Pass());
143 void ProfileImplIOData::Handle::Init(
144 const base::FilePath& cookie_path,
145 const base::FilePath& channel_id_path,
146 const base::FilePath& cache_path,
147 int cache_max_size,
148 const base::FilePath& media_cache_path,
149 int media_cache_max_size,
150 const base::FilePath& extensions_cookie_path,
151 const base::FilePath& profile_path,
152 chrome_browser_net::Predictor* predictor,
153 content::CookieStoreConfig::SessionCookieMode session_cookie_mode,
154 storage::SpecialStoragePolicy* special_storage_policy,
155 scoped_ptr<domain_reliability::DomainReliabilityMonitor>
156 domain_reliability_monitor) {
157 DCHECK_CURRENTLY_ON(BrowserThread::UI);
158 DCHECK(!io_data_->lazy_params_);
159 DCHECK(predictor);
161 LazyParams* lazy_params = new LazyParams();
163 lazy_params->cookie_path = cookie_path;
164 lazy_params->channel_id_path = channel_id_path;
165 lazy_params->cache_path = cache_path;
166 lazy_params->cache_max_size = cache_max_size;
167 lazy_params->media_cache_path = media_cache_path;
168 lazy_params->media_cache_max_size = media_cache_max_size;
169 lazy_params->extensions_cookie_path = extensions_cookie_path;
170 lazy_params->session_cookie_mode = session_cookie_mode;
171 lazy_params->special_storage_policy = special_storage_policy;
173 io_data_->lazy_params_.reset(lazy_params);
175 // Keep track of profile path and cache sizes separately so we can use them
176 // on demand when creating storage isolated URLRequestContextGetters.
177 io_data_->profile_path_ = profile_path;
178 io_data_->app_cache_max_size_ = cache_max_size;
179 io_data_->app_media_cache_max_size_ = media_cache_max_size;
181 io_data_->predictor_.reset(predictor);
182 io_data_->domain_reliability_monitor_ = domain_reliability_monitor.Pass();
184 io_data_->InitializeMetricsEnabledStateOnUIThread();
185 if (io_data_->domain_reliability_monitor_)
186 io_data_->domain_reliability_monitor_->MoveToNetworkThread();
188 // TODO(tbansal): Move this to IO thread once the data reduction proxy
189 // params are unified into a single object.
190 bool enable_quic_for_data_reduction_proxy =
191 IOThread::ShouldEnableQuicForDataReductionProxy();
193 io_data_->set_data_reduction_proxy_io_data(
194 CreateDataReductionProxyChromeIOData(
195 g_browser_process->io_thread()->net_log(), profile_->GetPrefs(),
196 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
197 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
198 enable_quic_for_data_reduction_proxy)
199 .Pass());
201 DataReductionProxyChromeSettingsFactory::GetForBrowserContext(profile_)->
202 InitDataReductionProxySettings(
203 io_data_->data_reduction_proxy_io_data(), profile_->GetPrefs(),
204 profile_->GetRequestContext(),
205 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI));
208 content::ResourceContext*
209 ProfileImplIOData::Handle::GetResourceContext() const {
210 DCHECK_CURRENTLY_ON(BrowserThread::UI);
211 LazyInitialize();
212 return GetResourceContextNoInit();
215 content::ResourceContext*
216 ProfileImplIOData::Handle::GetResourceContextNoInit() const {
217 DCHECK_CURRENTLY_ON(BrowserThread::UI);
218 // Don't call LazyInitialize here, since the resource context is created at
219 // the beginning of initalization and is used by some members while they're
220 // being initialized (i.e. AppCacheService).
221 return io_data_->GetResourceContext();
224 scoped_refptr<ChromeURLRequestContextGetter>
225 ProfileImplIOData::Handle::CreateMainRequestContextGetter(
226 content::ProtocolHandlerMap* protocol_handlers,
227 content::URLRequestInterceptorScopedVector request_interceptors,
228 PrefService* local_state,
229 IOThread* io_thread) const {
230 DCHECK_CURRENTLY_ON(BrowserThread::UI);
231 LazyInitialize();
232 DCHECK(!main_request_context_getter_.get());
233 main_request_context_getter_ = ChromeURLRequestContextGetter::Create(
234 profile_, io_data_, protocol_handlers, request_interceptors.Pass());
236 io_data_->predictor_
237 ->InitNetworkPredictor(profile_->GetPrefs(),
238 local_state,
239 io_thread,
240 main_request_context_getter_.get(),
241 io_data_);
243 content::NotificationService::current()->Notify(
244 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
245 content::Source<Profile>(profile_),
246 content::NotificationService::NoDetails());
247 return main_request_context_getter_;
250 scoped_refptr<ChromeURLRequestContextGetter>
251 ProfileImplIOData::Handle::GetMediaRequestContextGetter() const {
252 DCHECK_CURRENTLY_ON(BrowserThread::UI);
253 LazyInitialize();
254 if (!media_request_context_getter_.get()) {
255 media_request_context_getter_ =
256 ChromeURLRequestContextGetter::CreateForMedia(profile_, io_data_);
258 return media_request_context_getter_;
261 scoped_refptr<ChromeURLRequestContextGetter>
262 ProfileImplIOData::Handle::GetExtensionsRequestContextGetter() const {
263 DCHECK_CURRENTLY_ON(BrowserThread::UI);
264 LazyInitialize();
265 if (!extensions_request_context_getter_.get()) {
266 extensions_request_context_getter_ =
267 ChromeURLRequestContextGetter::CreateForExtensions(profile_, io_data_);
269 return extensions_request_context_getter_;
272 scoped_refptr<ChromeURLRequestContextGetter>
273 ProfileImplIOData::Handle::CreateIsolatedAppRequestContextGetter(
274 const base::FilePath& partition_path,
275 bool in_memory,
276 content::ProtocolHandlerMap* protocol_handlers,
277 content::URLRequestInterceptorScopedVector request_interceptors) const {
278 DCHECK_CURRENTLY_ON(BrowserThread::UI);
279 // Check that the partition_path is not the same as the base profile path. We
280 // expect isolated partition, which will never go to the default profile path.
281 CHECK(partition_path != profile_->GetPath());
282 LazyInitialize();
284 // Keep a map of request context getters, one per requested storage partition.
285 StoragePartitionDescriptor descriptor(partition_path, in_memory);
286 ChromeURLRequestContextGetterMap::iterator iter =
287 app_request_context_getter_map_.find(descriptor);
288 if (iter != app_request_context_getter_map_.end())
289 return iter->second;
291 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
292 protocol_handler_interceptor(
293 ProtocolHandlerRegistryFactory::GetForBrowserContext(profile_)->
294 CreateJobInterceptorFactory());
295 ChromeURLRequestContextGetter* context =
296 ChromeURLRequestContextGetter::CreateForIsolatedApp(
297 profile_,
298 io_data_,
299 descriptor,
300 protocol_handler_interceptor.Pass(),
301 protocol_handlers,
302 request_interceptors.Pass());
303 app_request_context_getter_map_[descriptor] = context;
305 return context;
308 scoped_refptr<ChromeURLRequestContextGetter>
309 ProfileImplIOData::Handle::GetIsolatedMediaRequestContextGetter(
310 const base::FilePath& partition_path,
311 bool in_memory) const {
312 DCHECK_CURRENTLY_ON(BrowserThread::UI);
313 // We must have a non-default path, or this will act like the default media
314 // context.
315 CHECK(partition_path != profile_->GetPath());
316 LazyInitialize();
318 // Keep a map of request context getters, one per requested storage partition.
319 StoragePartitionDescriptor descriptor(partition_path, in_memory);
320 ChromeURLRequestContextGetterMap::iterator iter =
321 isolated_media_request_context_getter_map_.find(descriptor);
322 if (iter != isolated_media_request_context_getter_map_.end())
323 return iter->second;
325 // Get the app context as the starting point for the media context, so that
326 // it uses the app's cookie store.
327 ChromeURLRequestContextGetterMap::const_iterator app_iter =
328 app_request_context_getter_map_.find(descriptor);
329 DCHECK(app_iter != app_request_context_getter_map_.end());
330 ChromeURLRequestContextGetter* app_context = app_iter->second.get();
331 ChromeURLRequestContextGetter* context =
332 ChromeURLRequestContextGetter::CreateForIsolatedMedia(
333 profile_, app_context, io_data_, descriptor);
334 isolated_media_request_context_getter_map_[descriptor] = context;
336 return context;
339 DevToolsNetworkController*
340 ProfileImplIOData::Handle::GetDevToolsNetworkController() const {
341 DCHECK_CURRENTLY_ON(BrowserThread::UI);
342 return io_data_->network_controller();
345 void ProfileImplIOData::Handle::ClearNetworkingHistorySince(
346 base::Time time,
347 const base::Closure& completion) {
348 DCHECK_CURRENTLY_ON(BrowserThread::UI);
349 LazyInitialize();
351 BrowserThread::PostTask(
352 BrowserThread::IO, FROM_HERE,
353 base::Bind(
354 &ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread,
355 base::Unretained(io_data_),
356 time,
357 completion));
360 void ProfileImplIOData::Handle::LazyInitialize() const {
361 DCHECK_CURRENTLY_ON(BrowserThread::UI);
362 if (initialized_)
363 return;
365 // Set initialized_ to true at the beginning in case any of the objects
366 // below try to get the ResourceContext pointer.
367 initialized_ = true;
368 PrefService* pref_service = profile_->GetPrefs();
369 io_data_->http_server_properties_manager_ =
370 chrome_browser_net::HttpServerPropertiesManagerFactory::CreateManager(
371 pref_service);
372 io_data_->set_http_server_properties(
373 scoped_ptr<net::HttpServerProperties>(
374 io_data_->http_server_properties_manager_));
375 io_data_->session_startup_pref()->Init(
376 prefs::kRestoreOnStartup, pref_service);
377 io_data_->session_startup_pref()->MoveToThread(
378 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
379 #if defined(SAFE_BROWSING_SERVICE)
380 io_data_->safe_browsing_enabled()->Init(prefs::kSafeBrowsingEnabled,
381 pref_service);
382 io_data_->safe_browsing_enabled()->MoveToThread(
383 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
384 #endif
385 io_data_->InitializeOnUIThread(profile_);
388 scoped_ptr<ProfileIOData::ChromeURLRequestContextGetterVector>
389 ProfileImplIOData::Handle::GetAllContextGetters() {
390 ChromeURLRequestContextGetterMap::iterator iter;
391 scoped_ptr<ChromeURLRequestContextGetterVector> context_getters(
392 new ChromeURLRequestContextGetterVector());
394 iter = isolated_media_request_context_getter_map_.begin();
395 for (; iter != isolated_media_request_context_getter_map_.end(); ++iter)
396 context_getters->push_back(iter->second);
398 iter = app_request_context_getter_map_.begin();
399 for (; iter != app_request_context_getter_map_.end(); ++iter)
400 context_getters->push_back(iter->second);
402 if (extensions_request_context_getter_.get())
403 context_getters->push_back(extensions_request_context_getter_);
405 if (media_request_context_getter_.get())
406 context_getters->push_back(media_request_context_getter_);
408 if (main_request_context_getter_.get())
409 context_getters->push_back(main_request_context_getter_);
411 return context_getters.Pass();
414 ProfileImplIOData::LazyParams::LazyParams()
415 : cache_max_size(0),
416 media_cache_max_size(0),
417 session_cookie_mode(
418 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES) {}
420 ProfileImplIOData::LazyParams::~LazyParams() {}
422 ProfileImplIOData::ProfileImplIOData()
423 : ProfileIOData(Profile::REGULAR_PROFILE),
424 http_server_properties_manager_(NULL),
425 app_cache_max_size_(0),
426 app_media_cache_max_size_(0) {
429 ProfileImplIOData::~ProfileImplIOData() {
430 DestroyResourceContext();
432 if (media_request_context_)
433 media_request_context_->AssertNoURLRequests();
436 void ProfileImplIOData::InitializeInternal(
437 scoped_ptr<ChromeNetworkDelegate> chrome_network_delegate,
438 ProfileParams* profile_params,
439 content::ProtocolHandlerMap* protocol_handlers,
440 content::URLRequestInterceptorScopedVector request_interceptors) const {
441 // Set up a persistent store for use by the network stack on the IO thread.
442 base::FilePath network_json_store_filepath(
443 profile_path_.Append(chrome::kNetworkPersistentStateFilename));
444 network_json_store_ = new JsonPrefStore(
445 network_json_store_filepath,
446 JsonPrefStore::GetTaskRunnerForFile(network_json_store_filepath,
447 BrowserThread::GetBlockingPool()),
448 scoped_ptr<PrefFilter>());
449 network_json_store_->ReadPrefsAsync(nullptr);
451 net::URLRequestContext* main_context = main_request_context();
453 IOThread* const io_thread = profile_params->io_thread;
454 IOThread::Globals* const io_thread_globals = io_thread->globals();
456 chrome_network_delegate->set_predictor(predictor_.get());
458 if (domain_reliability_monitor_) {
459 domain_reliability::DomainReliabilityMonitor* monitor =
460 domain_reliability_monitor_.get();
461 monitor->InitURLRequestContext(main_context);
462 monitor->AddBakedInConfigs();
463 monitor->SetDiscardUploads(!GetMetricsEnabledStateOnIOThread());
464 chrome_network_delegate->set_domain_reliability_monitor(monitor);
467 ApplyProfileParamsToContext(main_context);
469 if (http_server_properties_manager_)
470 http_server_properties_manager_->InitializeOnNetworkThread();
472 main_context->set_transport_security_state(transport_security_state());
474 main_context->set_net_log(io_thread->net_log());
476 network_delegate_ = data_reduction_proxy_io_data()->CreateNetworkDelegate(
477 chrome_network_delegate.Pass(), true).Pass();
479 main_context->set_network_delegate(network_delegate_.get());
481 main_context->set_http_server_properties(http_server_properties());
483 main_context->set_host_resolver(
484 io_thread_globals->host_resolver.get());
485 main_context->set_cert_transparency_verifier(
486 io_thread_globals->cert_transparency_verifier.get());
487 main_context->set_http_auth_handler_factory(
488 io_thread_globals->http_auth_handler_factory.get());
490 main_context->set_fraudulent_certificate_reporter(
491 fraudulent_certificate_reporter());
493 main_context->set_throttler_manager(
494 io_thread_globals->throttler_manager.get());
496 main_context->set_proxy_service(proxy_service());
498 scoped_refptr<net::CookieStore> cookie_store = NULL;
499 net::ChannelIDService* channel_id_service = NULL;
501 // Set up cookie store.
502 if (!cookie_store.get()) {
503 DCHECK(!lazy_params_->cookie_path.empty());
505 content::CookieStoreConfig cookie_config(
506 lazy_params_->cookie_path,
507 lazy_params_->session_cookie_mode,
508 lazy_params_->special_storage_policy.get(),
509 profile_params->cookie_monster_delegate.get());
510 cookie_config.crypto_delegate =
511 chrome_browser_net::GetCookieCryptoDelegate();
512 cookie_store = content::CreateCookieStore(cookie_config);
515 main_context->set_cookie_store(cookie_store.get());
517 // Set up server bound cert service.
518 if (!channel_id_service) {
519 DCHECK(!lazy_params_->channel_id_path.empty());
521 scoped_refptr<QuotaPolicyChannelIDStore> channel_id_db =
522 new QuotaPolicyChannelIDStore(
523 lazy_params_->channel_id_path,
524 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
525 BrowserThread::GetBlockingPool()->GetSequenceToken()),
526 lazy_params_->special_storage_policy.get());
527 channel_id_service = new net::ChannelIDService(
528 new net::DefaultChannelIDStore(channel_id_db.get()),
529 base::WorkerPool::GetTaskRunner(true));
532 set_channel_id_service(channel_id_service);
533 main_context->set_channel_id_service(channel_id_service);
535 scoped_ptr<net::HttpCache> main_cache;
537 // TODO(ttuttle): Remove ScopedTracker below once crbug.com/436671 is fixed.
538 tracked_objects::ScopedTracker tracking_profile(
539 FROM_HERE_WITH_EXPLICIT_FUNCTION("436671 HttpCache construction"));
540 net::HttpCache::DefaultBackend* main_backend =
541 new net::HttpCache::DefaultBackend(
542 net::DISK_CACHE,
543 ChooseCacheBackendType(),
544 lazy_params_->cache_path,
545 lazy_params_->cache_max_size,
546 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
547 main_cache = CreateMainHttpFactory(profile_params, main_backend);
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)
554 ftp_factory_.reset(
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);
562 // The data reduction proxy interceptor should be as close to the network
563 // as possible.
564 request_interceptors.insert(
565 request_interceptors.begin(),
566 data_reduction_proxy_io_data()->CreateInterceptor().release());
567 main_job_factory_ = SetUpJobFactoryDefaults(
568 main_job_factory.Pass(),
569 request_interceptors.Pass(),
570 profile_params->protocol_handler_interceptor.Pass(),
571 main_context->network_delegate(),
572 ftp_factory_.get());
573 main_context->set_job_factory(main_job_factory_.get());
575 #if defined(ENABLE_EXTENSIONS)
576 InitializeExtensionsRequestContext(profile_params);
577 #endif
579 // Setup SDCH for this profile.
580 sdch_manager_.reset(new net::SdchManager);
581 sdch_policy_.reset(new net::SdchOwner(sdch_manager_.get(), main_context));
582 main_context->set_sdch_manager(sdch_manager_.get());
583 if (ShouldUseSdchPersistence()) {
584 sdch_policy_->EnablePersistentStorage(network_json_store_.get());
587 // Create a media request context based on the main context, but using a
588 // media cache. It shares the same job factory as the main context.
589 StoragePartitionDescriptor details(profile_path_, false);
590 media_request_context_.reset(InitializeMediaRequestContext(main_context,
591 details));
593 lazy_params_.reset();
596 void ProfileImplIOData::
597 InitializeExtensionsRequestContext(ProfileParams* profile_params) const {
598 net::URLRequestContext* extensions_context = extensions_request_context();
599 IOThread* const io_thread = profile_params->io_thread;
600 IOThread::Globals* const io_thread_globals = io_thread->globals();
601 ApplyProfileParamsToContext(extensions_context);
603 extensions_context->set_transport_security_state(transport_security_state());
605 extensions_context->set_net_log(io_thread->net_log());
607 extensions_context->set_throttler_manager(
608 io_thread_globals->throttler_manager.get());
610 content::CookieStoreConfig cookie_config(
611 lazy_params_->extensions_cookie_path,
612 lazy_params_->session_cookie_mode,
613 NULL, NULL);
614 cookie_config.crypto_delegate =
615 chrome_browser_net::GetCookieCryptoDelegate();
616 net::CookieStore* extensions_cookie_store =
617 content::CreateCookieStore(cookie_config);
618 // Enable cookies for devtools and extension URLs.
619 const char* const schemes[] = {
620 content::kChromeDevToolsScheme,
621 extensions::kExtensionScheme
623 extensions_cookie_store->GetCookieMonster()->SetCookieableSchemes(
624 schemes, arraysize(schemes));
625 extensions_context->set_cookie_store(extensions_cookie_store);
627 scoped_ptr<net::URLRequestJobFactoryImpl> extensions_job_factory(
628 new net::URLRequestJobFactoryImpl());
629 // TODO(shalev): The extensions_job_factory has a NULL NetworkDelegate.
630 // Without a network_delegate, this protocol handler will never
631 // handle file: requests, but as a side effect it makes
632 // job_factory::IsHandledProtocol return true, which prevents attempts to
633 // handle the protocol externally. We pass NULL in to
634 // SetUpJobFactory() to get this effect.
635 extensions_job_factory_ = SetUpJobFactoryDefaults(
636 extensions_job_factory.Pass(),
637 content::URLRequestInterceptorScopedVector(),
638 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>(),
639 NULL,
640 ftp_factory_.get());
641 extensions_context->set_job_factory(extensions_job_factory_.get());
644 net::URLRequestContext* ProfileImplIOData::InitializeAppRequestContext(
645 net::URLRequestContext* main_context,
646 const StoragePartitionDescriptor& partition_descriptor,
647 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
648 protocol_handler_interceptor,
649 content::ProtocolHandlerMap* protocol_handlers,
650 content::URLRequestInterceptorScopedVector request_interceptors) const {
651 // Copy most state from the main context.
652 AppRequestContext* context = new AppRequestContext();
653 context->CopyFrom(main_context);
655 base::FilePath cookie_path = partition_descriptor.path.Append(
656 chrome::kCookieFilename);
657 base::FilePath cache_path =
658 partition_descriptor.path.Append(chrome::kCacheDirname);
660 // Use a separate HTTP disk cache for isolated apps.
661 net::HttpCache::BackendFactory* app_backend = NULL;
662 if (partition_descriptor.in_memory) {
663 app_backend = net::HttpCache::DefaultBackend::InMemory(0);
664 } else {
665 app_backend = new net::HttpCache::DefaultBackend(
666 net::DISK_CACHE,
667 ChooseCacheBackendType(),
668 cache_path,
669 app_cache_max_size_,
670 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
672 net::HttpNetworkSession* main_network_session =
673 main_http_factory_->GetSession();
674 scoped_ptr<net::HttpCache> app_http_cache =
675 CreateHttpFactory(main_network_session, app_backend);
677 scoped_refptr<net::CookieStore> cookie_store = NULL;
678 if (partition_descriptor.in_memory) {
679 cookie_store = content::CreateCookieStore(content::CookieStoreConfig());
682 // Use an app-specific cookie store.
683 if (!cookie_store.get()) {
684 DCHECK(!cookie_path.empty());
686 // TODO(creis): We should have a cookie delegate for notifying the cookie
687 // extensions API, but we need to update it to understand isolated apps
688 // first.
689 content::CookieStoreConfig cookie_config(
690 cookie_path,
691 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
692 NULL, NULL);
693 cookie_config.crypto_delegate =
694 chrome_browser_net::GetCookieCryptoDelegate();
695 cookie_store = content::CreateCookieStore(cookie_config);
698 // Transfer ownership of the cookies and cache to AppRequestContext.
699 context->SetCookieStore(cookie_store.get());
700 context->SetHttpTransactionFactory(app_http_cache.Pass());
702 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
703 new net::URLRequestJobFactoryImpl());
704 InstallProtocolHandlers(job_factory.get(), protocol_handlers);
705 // The data reduction proxy interceptor should be as close to the network
706 // as possible.
707 request_interceptors.insert(
708 request_interceptors.begin(),
709 data_reduction_proxy_io_data()->CreateInterceptor().release());
710 scoped_ptr<net::URLRequestJobFactory> top_job_factory(
711 SetUpJobFactoryDefaults(job_factory.Pass(),
712 request_interceptors.Pass(),
713 protocol_handler_interceptor.Pass(),
714 main_context->network_delegate(),
715 ftp_factory_.get()));
716 context->SetJobFactory(top_job_factory.Pass());
718 return context;
721 net::URLRequestContext*
722 ProfileImplIOData::InitializeMediaRequestContext(
723 net::URLRequestContext* original_context,
724 const StoragePartitionDescriptor& partition_descriptor) const {
725 // Copy most state from the original context.
726 MediaRequestContext* context = new MediaRequestContext();
727 context->CopyFrom(original_context);
729 // For in-memory context, return immediately after creating the new
730 // context before attaching a separate cache. It is important to return
731 // a new context rather than just reusing |original_context| because
732 // the caller expects to take ownership of the pointer.
733 if (partition_descriptor.in_memory)
734 return context;
736 using content::StoragePartition;
737 base::FilePath cache_path;
738 int cache_max_size = app_media_cache_max_size_;
739 if (partition_descriptor.path == profile_path_) {
740 // lazy_params_ is only valid for the default media context creation.
741 cache_path = lazy_params_->media_cache_path;
742 cache_max_size = lazy_params_->media_cache_max_size;
743 } else {
744 cache_path = partition_descriptor.path.Append(chrome::kMediaCacheDirname);
747 // Use a separate HTTP disk cache for isolated apps.
748 net::HttpCache::BackendFactory* media_backend =
749 new net::HttpCache::DefaultBackend(
750 net::MEDIA_CACHE,
751 ChooseCacheBackendType(),
752 cache_path,
753 cache_max_size,
754 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
755 net::HttpNetworkSession* main_network_session =
756 main_http_factory_->GetSession();
757 scoped_ptr<net::HttpCache> media_http_cache =
758 CreateHttpFactory(main_network_session, media_backend);
760 // Transfer ownership of the cache to MediaRequestContext.
761 context->SetHttpTransactionFactory(media_http_cache.Pass());
763 // Note that we do not create a new URLRequestJobFactory because
764 // the media context should behave exactly like its parent context
765 // in all respects except for cache behavior on media subresources.
766 // The CopyFrom() step above means that our media context will use
767 // the same URLRequestJobFactory instance that our parent context does.
769 return context;
772 net::URLRequestContext*
773 ProfileImplIOData::AcquireMediaRequestContext() const {
774 DCHECK(media_request_context_);
775 return media_request_context_.get();
778 net::URLRequestContext* ProfileImplIOData::AcquireIsolatedAppRequestContext(
779 net::URLRequestContext* main_context,
780 const StoragePartitionDescriptor& partition_descriptor,
781 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
782 protocol_handler_interceptor,
783 content::ProtocolHandlerMap* protocol_handlers,
784 content::URLRequestInterceptorScopedVector request_interceptors) const {
785 // We create per-app contexts on demand, unlike the others above.
786 net::URLRequestContext* app_request_context =
787 InitializeAppRequestContext(main_context,
788 partition_descriptor,
789 protocol_handler_interceptor.Pass(),
790 protocol_handlers,
791 request_interceptors.Pass());
792 DCHECK(app_request_context);
793 return app_request_context;
796 net::URLRequestContext*
797 ProfileImplIOData::AcquireIsolatedMediaRequestContext(
798 net::URLRequestContext* app_context,
799 const StoragePartitionDescriptor& partition_descriptor) const {
800 // We create per-app media contexts on demand, unlike the others above.
801 net::URLRequestContext* media_request_context =
802 InitializeMediaRequestContext(app_context, partition_descriptor);
803 DCHECK(media_request_context);
804 return media_request_context;
807 void ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread(
808 base::Time time,
809 const base::Closure& completion) {
810 DCHECK_CURRENTLY_ON(BrowserThread::IO);
811 DCHECK(initialized());
813 DCHECK(transport_security_state());
814 // Completes synchronously.
815 transport_security_state()->DeleteAllDynamicDataSince(time);
816 DCHECK(http_server_properties_manager_);
817 http_server_properties_manager_->Clear(completion);