Add method to get filters for a bookmark in Enhanced bookmark bridge
[chromium-blink-merge.git] / chrome / browser / profiles / profile_impl_io_data.cc
blobfb22c3c34299d8f1b98dd4b5709c3378ce1f9664
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/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
32 #include "chrome/browser/profiles/profile.h"
33 #include "chrome/common/chrome_constants.h"
34 #include "chrome/common/chrome_switches.h"
35 #include "chrome/common/pref_names.h"
36 #include "chrome/common/url_constants.h"
37 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler.h"
38 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_protocol.h"
39 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h"
40 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_usage_stats.h"
41 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
42 #include "components/domain_reliability/monitor.h"
43 #include "content/public/browser/browser_thread.h"
44 #include "content/public/browser/cookie_store_factory.h"
45 #include "content/public/browser/notification_service.h"
46 #include "content/public/browser/resource_context.h"
47 #include "content/public/browser/storage_partition.h"
48 #include "extensions/browser/extension_protocols.h"
49 #include "extensions/common/constants.h"
50 #include "net/base/cache_type.h"
51 #include "net/base/sdch_dictionary_fetcher.h"
52 #include "net/base/sdch_manager.h"
53 #include "net/ftp/ftp_network_layer.h"
54 #include "net/http/http_cache.h"
55 #include "net/http/http_server_properties_manager.h"
56 #include "net/ssl/channel_id_service.h"
57 #include "net/url_request/url_request_job_factory_impl.h"
58 #include "storage/browser/quota/special_storage_policy.h"
60 namespace {
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 } // namespace
87 using content::BrowserThread;
88 using data_reduction_proxy::DataReductionProxyParams;
90 ProfileImplIOData::Handle::Handle(Profile* profile)
91 : io_data_(new ProfileImplIOData),
92 profile_(profile),
93 initialized_(false) {
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
95 DCHECK(profile);
98 ProfileImplIOData::Handle::~Handle() {
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
100 io_data_->data_reduction_proxy_statistics_prefs()->WritePrefs();
102 if (io_data_->predictor_ != NULL) {
103 // io_data_->predictor_ might be NULL if Init() was never called
104 // (i.e. we shut down before ProfileImpl::DoFinalInit() got called).
105 bool save_prefs = true;
106 #if defined(OS_CHROMEOS)
107 save_prefs = !chromeos::ProfileHelper::IsSigninProfile(profile_);
108 #endif
109 if (save_prefs)
110 io_data_->predictor_->SaveStateForNextStartupAndTrim();
111 io_data_->predictor_->ShutdownOnUIThread();
114 if (io_data_->http_server_properties_manager_)
115 io_data_->http_server_properties_manager_->ShutdownOnPrefThread();
117 io_data_->data_reduction_proxy_enabled()->Destroy();
118 io_data_->ShutdownOnUIThread(GetAllContextGetters().Pass());
121 void ProfileImplIOData::Handle::Init(
122 const base::FilePath& cookie_path,
123 const base::FilePath& channel_id_path,
124 const base::FilePath& cache_path,
125 int cache_max_size,
126 const base::FilePath& media_cache_path,
127 int media_cache_max_size,
128 const base::FilePath& extensions_cookie_path,
129 const base::FilePath& profile_path,
130 const base::FilePath& infinite_cache_path,
131 chrome_browser_net::Predictor* predictor,
132 content::CookieStoreConfig::SessionCookieMode session_cookie_mode,
133 storage::SpecialStoragePolicy* special_storage_policy,
134 scoped_ptr<domain_reliability::DomainReliabilityMonitor>
135 domain_reliability_monitor,
136 const base::Callback<void(bool)>& data_reduction_proxy_unavailable,
137 scoped_ptr<DataReductionProxyChromeConfigurator>
138 data_reduction_proxy_chrome_configurator,
139 scoped_ptr<data_reduction_proxy::DataReductionProxyParams>
140 data_reduction_proxy_params,
141 scoped_ptr<data_reduction_proxy::DataReductionProxyStatisticsPrefs>
142 data_reduction_proxy_statistics_prefs) {
143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
144 DCHECK(!io_data_->lazy_params_);
145 DCHECK(predictor);
147 LazyParams* lazy_params = new LazyParams();
149 lazy_params->cookie_path = cookie_path;
150 lazy_params->channel_id_path = channel_id_path;
151 lazy_params->cache_path = cache_path;
152 lazy_params->cache_max_size = cache_max_size;
153 lazy_params->media_cache_path = media_cache_path;
154 lazy_params->media_cache_max_size = media_cache_max_size;
155 lazy_params->extensions_cookie_path = extensions_cookie_path;
156 lazy_params->infinite_cache_path = infinite_cache_path;
157 lazy_params->session_cookie_mode = session_cookie_mode;
158 lazy_params->special_storage_policy = special_storage_policy;
160 io_data_->lazy_params_.reset(lazy_params);
162 // Keep track of profile path and cache sizes separately so we can use them
163 // on demand when creating storage isolated URLRequestContextGetters.
164 io_data_->profile_path_ = profile_path;
165 io_data_->app_cache_max_size_ = cache_max_size;
166 io_data_->app_media_cache_max_size_ = media_cache_max_size;
168 io_data_->predictor_.reset(predictor);
169 io_data_->domain_reliability_monitor_ = domain_reliability_monitor.Pass();
171 io_data_->InitializeMetricsEnabledStateOnUIThread();
172 if (io_data_->domain_reliability_monitor_)
173 io_data_->domain_reliability_monitor_->MoveToNetworkThread();
175 io_data_->set_data_reduction_proxy_unavailable_callback(
176 data_reduction_proxy_unavailable);
177 io_data_->set_data_reduction_proxy_chrome_configurator(
178 data_reduction_proxy_chrome_configurator.Pass());
179 io_data_->set_data_reduction_proxy_params(data_reduction_proxy_params.Pass());
180 io_data_->set_data_reduction_proxy_statistics_prefs(
181 data_reduction_proxy_statistics_prefs.Pass());
184 content::ResourceContext*
185 ProfileImplIOData::Handle::GetResourceContext() const {
186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
187 LazyInitialize();
188 return GetResourceContextNoInit();
191 content::ResourceContext*
192 ProfileImplIOData::Handle::GetResourceContextNoInit() const {
193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
194 // Don't call LazyInitialize here, since the resource context is created at
195 // the beginning of initalization and is used by some members while they're
196 // being initialized (i.e. AppCacheService).
197 return io_data_->GetResourceContext();
200 scoped_refptr<ChromeURLRequestContextGetter>
201 ProfileImplIOData::Handle::CreateMainRequestContextGetter(
202 content::ProtocolHandlerMap* protocol_handlers,
203 content::URLRequestInterceptorScopedVector request_interceptors,
204 PrefService* local_state,
205 IOThread* io_thread) const {
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
207 LazyInitialize();
208 DCHECK(!main_request_context_getter_.get());
209 main_request_context_getter_ = ChromeURLRequestContextGetter::Create(
210 profile_, io_data_, protocol_handlers, request_interceptors.Pass());
212 io_data_->predictor_
213 ->InitNetworkPredictor(profile_->GetPrefs(),
214 local_state,
215 io_thread,
216 main_request_context_getter_.get(),
217 io_data_);
219 content::NotificationService::current()->Notify(
220 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
221 content::Source<Profile>(profile_),
222 content::NotificationService::NoDetails());
223 return main_request_context_getter_;
226 scoped_refptr<ChromeURLRequestContextGetter>
227 ProfileImplIOData::Handle::GetMediaRequestContextGetter() const {
228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
229 LazyInitialize();
230 if (!media_request_context_getter_.get()) {
231 media_request_context_getter_ =
232 ChromeURLRequestContextGetter::CreateForMedia(profile_, io_data_);
234 return media_request_context_getter_;
237 scoped_refptr<ChromeURLRequestContextGetter>
238 ProfileImplIOData::Handle::GetExtensionsRequestContextGetter() const {
239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
240 LazyInitialize();
241 if (!extensions_request_context_getter_.get()) {
242 extensions_request_context_getter_ =
243 ChromeURLRequestContextGetter::CreateForExtensions(profile_, io_data_);
245 return extensions_request_context_getter_;
248 scoped_refptr<ChromeURLRequestContextGetter>
249 ProfileImplIOData::Handle::CreateIsolatedAppRequestContextGetter(
250 const base::FilePath& partition_path,
251 bool in_memory,
252 content::ProtocolHandlerMap* protocol_handlers,
253 content::URLRequestInterceptorScopedVector request_interceptors) const {
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
255 // Check that the partition_path is not the same as the base profile path. We
256 // expect isolated partition, which will never go to the default profile path.
257 CHECK(partition_path != profile_->GetPath());
258 LazyInitialize();
260 // Keep a map of request context getters, one per requested storage partition.
261 StoragePartitionDescriptor descriptor(partition_path, in_memory);
262 ChromeURLRequestContextGetterMap::iterator iter =
263 app_request_context_getter_map_.find(descriptor);
264 if (iter != app_request_context_getter_map_.end())
265 return iter->second;
267 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
268 protocol_handler_interceptor(
269 ProtocolHandlerRegistryFactory::GetForBrowserContext(profile_)->
270 CreateJobInterceptorFactory());
271 ChromeURLRequestContextGetter* context =
272 ChromeURLRequestContextGetter::CreateForIsolatedApp(
273 profile_,
274 io_data_,
275 descriptor,
276 protocol_handler_interceptor.Pass(),
277 protocol_handlers,
278 request_interceptors.Pass());
279 app_request_context_getter_map_[descriptor] = context;
281 return context;
284 scoped_refptr<ChromeURLRequestContextGetter>
285 ProfileImplIOData::Handle::GetIsolatedMediaRequestContextGetter(
286 const base::FilePath& partition_path,
287 bool in_memory) const {
288 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
289 // We must have a non-default path, or this will act like the default media
290 // context.
291 CHECK(partition_path != profile_->GetPath());
292 LazyInitialize();
294 // Keep a map of request context getters, one per requested storage partition.
295 StoragePartitionDescriptor descriptor(partition_path, in_memory);
296 ChromeURLRequestContextGetterMap::iterator iter =
297 isolated_media_request_context_getter_map_.find(descriptor);
298 if (iter != isolated_media_request_context_getter_map_.end())
299 return iter->second;
301 // Get the app context as the starting point for the media context, so that
302 // it uses the app's cookie store.
303 ChromeURLRequestContextGetterMap::const_iterator app_iter =
304 app_request_context_getter_map_.find(descriptor);
305 DCHECK(app_iter != app_request_context_getter_map_.end());
306 ChromeURLRequestContextGetter* app_context = app_iter->second.get();
307 ChromeURLRequestContextGetter* context =
308 ChromeURLRequestContextGetter::CreateForIsolatedMedia(
309 profile_, app_context, io_data_, descriptor);
310 isolated_media_request_context_getter_map_[descriptor] = context;
312 return context;
315 DevToolsNetworkController*
316 ProfileImplIOData::Handle::GetDevToolsNetworkController() const {
317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
318 return io_data_->network_controller();
321 void ProfileImplIOData::Handle::ClearNetworkingHistorySince(
322 base::Time time,
323 const base::Closure& completion) {
324 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
325 LazyInitialize();
327 BrowserThread::PostTask(
328 BrowserThread::IO, FROM_HERE,
329 base::Bind(
330 &ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread,
331 base::Unretained(io_data_),
332 time,
333 completion));
336 void ProfileImplIOData::Handle::LazyInitialize() const {
337 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
338 if (initialized_)
339 return;
341 // Set initialized_ to true at the beginning in case any of the objects
342 // below try to get the ResourceContext pointer.
343 initialized_ = true;
344 PrefService* pref_service = profile_->GetPrefs();
345 io_data_->http_server_properties_manager_ =
346 chrome_browser_net::HttpServerPropertiesManagerFactory::CreateManager(
347 pref_service);
348 io_data_->set_http_server_properties(
349 scoped_ptr<net::HttpServerProperties>(
350 io_data_->http_server_properties_manager_));
351 io_data_->session_startup_pref()->Init(
352 prefs::kRestoreOnStartup, pref_service);
353 io_data_->session_startup_pref()->MoveToThread(
354 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
355 #if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
356 io_data_->safe_browsing_enabled()->Init(prefs::kSafeBrowsingEnabled,
357 pref_service);
358 io_data_->safe_browsing_enabled()->MoveToThread(
359 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
360 #endif
361 io_data_->data_reduction_proxy_enabled()->Init(
362 data_reduction_proxy::prefs::kDataReductionProxyEnabled, pref_service);
363 io_data_->data_reduction_proxy_enabled()->MoveToThread(
364 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
365 io_data_->InitializeOnUIThread(profile_);
368 scoped_ptr<ProfileIOData::ChromeURLRequestContextGetterVector>
369 ProfileImplIOData::Handle::GetAllContextGetters() {
370 ChromeURLRequestContextGetterMap::iterator iter;
371 scoped_ptr<ChromeURLRequestContextGetterVector> context_getters(
372 new ChromeURLRequestContextGetterVector());
374 iter = isolated_media_request_context_getter_map_.begin();
375 for (; iter != isolated_media_request_context_getter_map_.end(); ++iter)
376 context_getters->push_back(iter->second);
378 iter = app_request_context_getter_map_.begin();
379 for (; iter != app_request_context_getter_map_.end(); ++iter)
380 context_getters->push_back(iter->second);
382 if (extensions_request_context_getter_.get())
383 context_getters->push_back(extensions_request_context_getter_);
385 if (media_request_context_getter_.get())
386 context_getters->push_back(media_request_context_getter_);
388 if (main_request_context_getter_.get())
389 context_getters->push_back(main_request_context_getter_);
391 return context_getters.Pass();
394 ProfileImplIOData::LazyParams::LazyParams()
395 : cache_max_size(0),
396 media_cache_max_size(0),
397 session_cookie_mode(
398 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES) {}
400 ProfileImplIOData::LazyParams::~LazyParams() {}
402 ProfileImplIOData::ProfileImplIOData()
403 : ProfileIOData(Profile::REGULAR_PROFILE),
404 http_server_properties_manager_(NULL),
405 app_cache_max_size_(0),
406 app_media_cache_max_size_(0) {
409 ProfileImplIOData::~ProfileImplIOData() {
410 if (initialized())
411 network_delegate()->set_domain_reliability_monitor(NULL);
413 DestroyResourceContext();
415 if (media_request_context_)
416 media_request_context_->AssertNoURLRequests();
419 void ProfileImplIOData::InitializeInternal(
420 ProfileParams* profile_params,
421 content::ProtocolHandlerMap* protocol_handlers,
422 content::URLRequestInterceptorScopedVector request_interceptors) const {
423 net::URLRequestContext* main_context = main_request_context();
425 IOThread* const io_thread = profile_params->io_thread;
426 IOThread::Globals* const io_thread_globals = io_thread->globals();
428 set_data_reduction_proxy_auth_request_handler(
429 scoped_ptr<data_reduction_proxy::DataReductionProxyAuthRequestHandler>
430 (new data_reduction_proxy::DataReductionProxyAuthRequestHandler(
431 DataReductionProxyChromeSettings::GetClient(),
432 data_reduction_proxy_params(),
433 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))));
434 set_data_reduction_proxy_usage_stats(
435 scoped_ptr<data_reduction_proxy::DataReductionProxyUsageStats>
436 (new data_reduction_proxy::DataReductionProxyUsageStats(
437 data_reduction_proxy_params(),
438 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)
439 .get())));
440 data_reduction_proxy_usage_stats()->set_unavailable_callback(
441 data_reduction_proxy_unavailable_callback());
443 network_delegate()->set_data_reduction_proxy_enabled_pref(
444 &data_reduction_proxy_enabled_);
445 network_delegate()->set_data_reduction_proxy_params(
446 data_reduction_proxy_params());
447 network_delegate()->set_data_reduction_proxy_usage_stats(
448 data_reduction_proxy_usage_stats());
449 network_delegate()->set_data_reduction_proxy_auth_request_handler(
450 data_reduction_proxy_auth_request_handler());
451 network_delegate()->set_data_reduction_proxy_statistics_prefs(
452 data_reduction_proxy_statistics_prefs());
453 network_delegate()->set_on_resolve_proxy_handler(
454 base::Bind(data_reduction_proxy::OnResolveProxyHandler));
455 network_delegate()->set_proxy_config_getter(
456 base::Bind(
457 &DataReductionProxyChromeConfigurator::GetProxyConfigOnIO,
458 base::Unretained(data_reduction_proxy_chrome_configurator())));
459 network_delegate()->set_predictor(predictor_.get());
461 // Initialize context members.
463 ApplyProfileParamsToContext(main_context);
465 if (http_server_properties_manager_)
466 http_server_properties_manager_->InitializeOnNetworkThread();
468 main_context->set_transport_security_state(transport_security_state());
470 main_context->set_net_log(io_thread->net_log());
472 main_context->set_network_delegate(network_delegate());
474 main_context->set_http_server_properties(http_server_properties());
476 main_context->set_host_resolver(
477 io_thread_globals->host_resolver.get());
478 main_context->set_cert_transparency_verifier(
479 io_thread_globals->cert_transparency_verifier.get());
480 main_context->set_http_auth_handler_factory(
481 io_thread_globals->http_auth_handler_factory.get());
483 main_context->set_fraudulent_certificate_reporter(
484 fraudulent_certificate_reporter());
486 main_context->set_throttler_manager(
487 io_thread_globals->throttler_manager.get());
489 main_context->set_proxy_service(proxy_service());
491 scoped_refptr<net::CookieStore> cookie_store = NULL;
492 net::ChannelIDService* channel_id_service = NULL;
493 if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
494 // Don't use existing cookies and use an in-memory store.
495 using content::CookieStoreConfig;
496 cookie_store = content::CreateCookieStore(CookieStoreConfig(
497 base::FilePath(),
498 CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
499 NULL,
500 profile_params->cookie_monster_delegate.get()));
501 // Don't use existing channel ids and use an in-memory store.
502 channel_id_service = new net::ChannelIDService(
503 new net::DefaultChannelIDStore(NULL),
504 base::WorkerPool::GetTaskRunner(true));
508 // setup cookie store
509 if (!cookie_store.get()) {
510 DCHECK(!lazy_params_->cookie_path.empty());
512 content::CookieStoreConfig cookie_config(
513 lazy_params_->cookie_path,
514 lazy_params_->session_cookie_mode,
515 lazy_params_->special_storage_policy.get(),
516 profile_params->cookie_monster_delegate.get());
517 cookie_config.crypto_delegate =
518 chrome_browser_net::GetCookieCryptoDelegate();
519 cookie_store = content::CreateCookieStore(cookie_config);
522 main_context->set_cookie_store(cookie_store.get());
524 // Setup server bound cert service.
525 if (!channel_id_service) {
526 DCHECK(!lazy_params_->channel_id_path.empty());
528 scoped_refptr<QuotaPolicyChannelIDStore> channel_id_db =
529 new QuotaPolicyChannelIDStore(
530 lazy_params_->channel_id_path,
531 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
532 BrowserThread::GetBlockingPool()->GetSequenceToken()),
533 lazy_params_->special_storage_policy.get());
534 channel_id_service = new net::ChannelIDService(
535 new net::DefaultChannelIDStore(channel_id_db.get()),
536 base::WorkerPool::GetTaskRunner(true));
539 set_channel_id_service(channel_id_service);
540 main_context->set_channel_id_service(channel_id_service);
542 net::HttpCache::DefaultBackend* main_backend =
543 new net::HttpCache::DefaultBackend(
544 net::DISK_CACHE,
545 ChooseCacheBackendType(),
546 lazy_params_->cache_path,
547 lazy_params_->cache_max_size,
548 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
549 scoped_ptr<net::HttpCache> main_cache = CreateMainHttpFactory(
550 profile_params, main_backend);
551 main_cache->InitializeInfiniteCache(lazy_params_->infinite_cache_path);
553 if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
554 main_cache->set_mode(
555 chrome_browser_net::IsCookieRecordMode() ?
556 net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
559 main_http_factory_.reset(main_cache.release());
560 main_context->set_http_transaction_factory(main_http_factory_.get());
562 #if !defined(DISABLE_FTP_SUPPORT)
563 ftp_factory_.reset(
564 new net::FtpNetworkLayer(io_thread_globals->host_resolver.get()));
565 #endif // !defined(DISABLE_FTP_SUPPORT)
567 scoped_ptr<net::URLRequestJobFactoryImpl> main_job_factory(
568 new net::URLRequestJobFactoryImpl());
569 InstallProtocolHandlers(main_job_factory.get(), protocol_handlers);
570 main_job_factory_ = SetUpJobFactoryDefaults(
571 main_job_factory.Pass(),
572 request_interceptors.Pass(),
573 profile_params->protocol_handler_interceptor.Pass(),
574 network_delegate(),
575 ftp_factory_.get());
576 main_context->set_job_factory(main_job_factory_.get());
578 #if defined(ENABLE_EXTENSIONS)
579 InitializeExtensionsRequestContext(profile_params);
580 #endif
582 // Setup the SDCHManager for this profile.
583 sdch_manager_.reset(new net::SdchManager);
584 sdch_manager_->set_sdch_fetcher(scoped_ptr<net::SdchFetcher>(
585 new net::SdchDictionaryFetcher(sdch_manager_.get(),
586 main_context)).Pass());
587 main_context->set_sdch_manager(sdch_manager_.get());
589 // Create a media request context based on the main context, but using a
590 // media cache. It shares the same job factory as the main context.
591 StoragePartitionDescriptor details(profile_path_, false);
592 media_request_context_.reset(InitializeMediaRequestContext(main_context,
593 details));
595 if (domain_reliability_monitor_) {
596 domain_reliability::DomainReliabilityMonitor* monitor =
597 domain_reliability_monitor_.get();
598 monitor->InitURLRequestContext(main_context);
599 monitor->AddBakedInConfigs();
600 monitor->SetDiscardUploads(!GetMetricsEnabledStateOnIOThread());
601 network_delegate()->set_domain_reliability_monitor(monitor);
604 lazy_params_.reset();
607 void ProfileImplIOData::
608 InitializeExtensionsRequestContext(ProfileParams* profile_params) const {
609 net::URLRequestContext* extensions_context = extensions_request_context();
610 IOThread* const io_thread = profile_params->io_thread;
611 IOThread::Globals* const io_thread_globals = io_thread->globals();
612 ApplyProfileParamsToContext(extensions_context);
614 extensions_context->set_transport_security_state(transport_security_state());
616 extensions_context->set_net_log(io_thread->net_log());
618 extensions_context->set_throttler_manager(
619 io_thread_globals->throttler_manager.get());
621 content::CookieStoreConfig cookie_config(
622 lazy_params_->extensions_cookie_path,
623 lazy_params_->session_cookie_mode,
624 NULL, NULL);
625 cookie_config.crypto_delegate =
626 chrome_browser_net::GetCookieCryptoDelegate();
627 net::CookieStore* extensions_cookie_store =
628 content::CreateCookieStore(cookie_config);
629 // Enable cookies for devtools and extension URLs.
630 const char* const schemes[] = {
631 content::kChromeDevToolsScheme,
632 extensions::kExtensionScheme
634 extensions_cookie_store->GetCookieMonster()->SetCookieableSchemes(
635 schemes, arraysize(schemes));
636 extensions_context->set_cookie_store(extensions_cookie_store);
638 scoped_ptr<net::URLRequestJobFactoryImpl> extensions_job_factory(
639 new net::URLRequestJobFactoryImpl());
640 // TODO(shalev): The extensions_job_factory has a NULL NetworkDelegate.
641 // Without a network_delegate, this protocol handler will never
642 // handle file: requests, but as a side effect it makes
643 // job_factory::IsHandledProtocol return true, which prevents attempts to
644 // handle the protocol externally. We pass NULL in to
645 // SetUpJobFactory() to get this effect.
646 extensions_job_factory_ = SetUpJobFactoryDefaults(
647 extensions_job_factory.Pass(),
648 content::URLRequestInterceptorScopedVector(),
649 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>(),
650 NULL,
651 ftp_factory_.get());
652 extensions_context->set_job_factory(extensions_job_factory_.get());
655 net::URLRequestContext* ProfileImplIOData::InitializeAppRequestContext(
656 net::URLRequestContext* main_context,
657 const StoragePartitionDescriptor& partition_descriptor,
658 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
659 protocol_handler_interceptor,
660 content::ProtocolHandlerMap* protocol_handlers,
661 content::URLRequestInterceptorScopedVector request_interceptors) const {
662 // Copy most state from the main context.
663 AppRequestContext* context = new AppRequestContext();
664 context->CopyFrom(main_context);
666 base::FilePath cookie_path = partition_descriptor.path.Append(
667 chrome::kCookieFilename);
668 base::FilePath cache_path =
669 partition_descriptor.path.Append(chrome::kCacheDirname);
671 // Use a separate HTTP disk cache for isolated apps.
672 net::HttpCache::BackendFactory* app_backend = NULL;
673 if (partition_descriptor.in_memory) {
674 app_backend = net::HttpCache::DefaultBackend::InMemory(0);
675 } else {
676 app_backend = new net::HttpCache::DefaultBackend(
677 net::DISK_CACHE,
678 ChooseCacheBackendType(),
679 cache_path,
680 app_cache_max_size_,
681 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
683 net::HttpNetworkSession* main_network_session =
684 main_http_factory_->GetSession();
685 scoped_ptr<net::HttpCache> app_http_cache =
686 CreateHttpFactory(main_network_session, app_backend);
688 scoped_refptr<net::CookieStore> cookie_store = NULL;
689 if (partition_descriptor.in_memory) {
690 cookie_store = content::CreateCookieStore(content::CookieStoreConfig());
691 } else if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
692 // Don't use existing cookies and use an in-memory store.
693 // TODO(creis): We should have a cookie delegate for notifying the cookie
694 // extensions API, but we need to update it to understand isolated apps
695 // first.
696 cookie_store = content::CreateCookieStore(content::CookieStoreConfig());
697 app_http_cache->set_mode(
698 chrome_browser_net::IsCookieRecordMode() ?
699 net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
702 // Use an app-specific cookie store.
703 if (!cookie_store.get()) {
704 DCHECK(!cookie_path.empty());
706 // TODO(creis): We should have a cookie delegate for notifying the cookie
707 // extensions API, but we need to update it to understand isolated apps
708 // first.
709 content::CookieStoreConfig cookie_config(
710 cookie_path,
711 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
712 NULL, NULL);
713 cookie_config.crypto_delegate =
714 chrome_browser_net::GetCookieCryptoDelegate();
715 cookie_store = content::CreateCookieStore(cookie_config);
718 // Transfer ownership of the cookies and cache to AppRequestContext.
719 context->SetCookieStore(cookie_store.get());
720 context->SetHttpTransactionFactory(app_http_cache.Pass());
722 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
723 new net::URLRequestJobFactoryImpl());
724 InstallProtocolHandlers(job_factory.get(), protocol_handlers);
725 scoped_ptr<net::URLRequestJobFactory> top_job_factory(
726 SetUpJobFactoryDefaults(job_factory.Pass(),
727 request_interceptors.Pass(),
728 protocol_handler_interceptor.Pass(),
729 network_delegate(),
730 ftp_factory_.get()));
731 context->SetJobFactory(top_job_factory.Pass());
733 return context;
736 net::URLRequestContext*
737 ProfileImplIOData::InitializeMediaRequestContext(
738 net::URLRequestContext* original_context,
739 const StoragePartitionDescriptor& partition_descriptor) const {
740 // Copy most state from the original context.
741 MediaRequestContext* context = new MediaRequestContext();
742 context->CopyFrom(original_context);
744 // For in-memory context, return immediately after creating the new
745 // context before attaching a separate cache. It is important to return
746 // a new context rather than just reusing |original_context| because
747 // the caller expects to take ownership of the pointer.
748 if (partition_descriptor.in_memory)
749 return context;
751 using content::StoragePartition;
752 base::FilePath cache_path;
753 int cache_max_size = app_media_cache_max_size_;
754 if (partition_descriptor.path == profile_path_) {
755 // lazy_params_ is only valid for the default media context creation.
756 cache_path = lazy_params_->media_cache_path;
757 cache_max_size = lazy_params_->media_cache_max_size;
758 } else {
759 cache_path = partition_descriptor.path.Append(chrome::kMediaCacheDirname);
762 // Use a separate HTTP disk cache for isolated apps.
763 net::HttpCache::BackendFactory* media_backend =
764 new net::HttpCache::DefaultBackend(
765 net::MEDIA_CACHE,
766 ChooseCacheBackendType(),
767 cache_path,
768 cache_max_size,
769 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
770 net::HttpNetworkSession* main_network_session =
771 main_http_factory_->GetSession();
772 scoped_ptr<net::HttpCache> media_http_cache =
773 CreateHttpFactory(main_network_session, media_backend);
775 // Transfer ownership of the cache to MediaRequestContext.
776 context->SetHttpTransactionFactory(media_http_cache.Pass());
778 // Note that we do not create a new URLRequestJobFactory because
779 // the media context should behave exactly like its parent context
780 // in all respects except for cache behavior on media subresources.
781 // The CopyFrom() step above means that our media context will use
782 // the same URLRequestJobFactory instance that our parent context does.
784 return context;
787 net::URLRequestContext*
788 ProfileImplIOData::AcquireMediaRequestContext() const {
789 DCHECK(media_request_context_);
790 return media_request_context_.get();
793 net::URLRequestContext* ProfileImplIOData::AcquireIsolatedAppRequestContext(
794 net::URLRequestContext* main_context,
795 const StoragePartitionDescriptor& partition_descriptor,
796 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
797 protocol_handler_interceptor,
798 content::ProtocolHandlerMap* protocol_handlers,
799 content::URLRequestInterceptorScopedVector request_interceptors) const {
800 // We create per-app contexts on demand, unlike the others above.
801 net::URLRequestContext* app_request_context =
802 InitializeAppRequestContext(main_context,
803 partition_descriptor,
804 protocol_handler_interceptor.Pass(),
805 protocol_handlers,
806 request_interceptors.Pass());
807 DCHECK(app_request_context);
808 return app_request_context;
811 net::URLRequestContext*
812 ProfileImplIOData::AcquireIsolatedMediaRequestContext(
813 net::URLRequestContext* app_context,
814 const StoragePartitionDescriptor& partition_descriptor) const {
815 // We create per-app media contexts on demand, unlike the others above.
816 net::URLRequestContext* media_request_context =
817 InitializeMediaRequestContext(app_context, partition_descriptor);
818 DCHECK(media_request_context);
819 return media_request_context;
822 void ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread(
823 base::Time time,
824 const base::Closure& completion) {
825 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
826 DCHECK(initialized());
828 DCHECK(transport_security_state());
829 // Completes synchronously.
830 transport_security_state()->DeleteAllDynamicDataSince(time);
831 DCHECK(http_server_properties_manager_);
832 http_server_properties_manager_->Clear(completion);
835 bool ProfileImplIOData::IsDataReductionProxyEnabled() const {
836 return data_reduction_proxy_enabled_.GetValue() ||
837 CommandLine::ForCurrentProcess()->HasSwitch(
838 data_reduction_proxy::switches::kEnableDataReductionProxy);