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"
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/data_reduction_proxy/core/browser/data_store_impl.h"
48 #include "components/domain_reliability/monitor.h"
49 #include "content/public/browser/browser_thread.h"
50 #include "content/public/browser/cookie_store_factory.h"
51 #include "content/public/browser/notification_service.h"
52 #include "content/public/browser/resource_context.h"
53 #include "content/public/browser/storage_partition.h"
54 #include "extensions/browser/extension_protocols.h"
55 #include "extensions/common/constants.h"
56 #include "net/base/cache_type.h"
57 #include "net/base/sdch_manager.h"
58 #include "net/ftp/ftp_network_layer.h"
59 #include "net/http/http_cache.h"
60 #include "net/http/http_server_properties_manager.h"
61 #include "net/sdch/sdch_owner.h"
62 #include "net/ssl/channel_id_service.h"
63 #include "net/url_request/url_request_intercepting_job_factory.h"
64 #include "net/url_request/url_request_job_factory_impl.h"
65 #include "storage/browser/quota/special_storage_policy.h"
69 net::BackendType
ChooseCacheBackendType() {
70 #if defined(OS_ANDROID)
71 return net::CACHE_BACKEND_SIMPLE
;
73 const base::CommandLine
& command_line
=
74 *base::CommandLine::ForCurrentProcess();
75 if (command_line
.HasSwitch(switches::kUseSimpleCacheBackend
)) {
76 const std::string opt_value
=
77 command_line
.GetSwitchValueASCII(switches::kUseSimpleCacheBackend
);
78 if (base::LowerCaseEqualsASCII(opt_value
, "off"))
79 return net::CACHE_BACKEND_BLOCKFILE
;
80 if (opt_value
.empty() || base::LowerCaseEqualsASCII(opt_value
, "on"))
81 return net::CACHE_BACKEND_SIMPLE
;
83 const std::string experiment_name
=
84 base::FieldTrialList::FindFullName("SimpleCacheTrial");
85 if (experiment_name
== "ExperimentYes" ||
86 experiment_name
== "ExperimentYes2") {
87 return net::CACHE_BACKEND_SIMPLE
;
89 return net::CACHE_BACKEND_BLOCKFILE
;
93 bool ShouldUseSdchPersistence() {
94 const std::string group
=
95 base::FieldTrialList::FindFullName("SdchPersistence");
96 const base::CommandLine
* command_line
=
97 base::CommandLine::ForCurrentProcess();
98 if (command_line
->HasSwitch(switches::kEnableSdchPersistence
)) {
101 if (command_line
->HasSwitch(switches::kDisableSdchPersistence
)) {
104 return group
== "Enabled";
109 using content::BrowserThread
;
111 ProfileImplIOData::Handle::Handle(Profile
* profile
)
112 : io_data_(new ProfileImplIOData
),
114 initialized_(false) {
115 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
119 ProfileImplIOData::Handle::~Handle() {
120 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
121 if (io_data_
->predictor_
!= NULL
) {
122 // io_data_->predictor_ might be NULL if Init() was never called
123 // (i.e. we shut down before ProfileImpl::DoFinalInit() got called).
124 bool save_prefs
= true;
125 #if defined(OS_CHROMEOS)
126 save_prefs
= !chromeos::ProfileHelper::IsSigninProfile(profile_
);
129 io_data_
->predictor_
->SaveStateForNextStartupAndTrim();
130 io_data_
->predictor_
->ShutdownOnUIThread();
133 if (io_data_
->http_server_properties_manager_
)
134 io_data_
->http_server_properties_manager_
->ShutdownOnPrefThread();
136 // io_data_->data_reduction_proxy_io_data() might be NULL if Init() was
138 if (io_data_
->data_reduction_proxy_io_data())
139 io_data_
->data_reduction_proxy_io_data()->ShutdownOnUIThread();
141 io_data_
->ShutdownOnUIThread(GetAllContextGetters().Pass());
144 void ProfileImplIOData::Handle::Init(
145 const base::FilePath
& cookie_path
,
146 const base::FilePath
& channel_id_path
,
147 const base::FilePath
& cache_path
,
149 const base::FilePath
& media_cache_path
,
150 int media_cache_max_size
,
151 const base::FilePath
& extensions_cookie_path
,
152 const base::FilePath
& profile_path
,
153 chrome_browser_net::Predictor
* predictor
,
154 content::CookieStoreConfig::SessionCookieMode session_cookie_mode
,
155 storage::SpecialStoragePolicy
* special_storage_policy
,
156 scoped_ptr
<domain_reliability::DomainReliabilityMonitor
>
157 domain_reliability_monitor
) {
158 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
159 DCHECK(!io_data_
->lazy_params_
);
162 LazyParams
* lazy_params
= new LazyParams();
164 lazy_params
->cookie_path
= cookie_path
;
165 lazy_params
->channel_id_path
= channel_id_path
;
166 lazy_params
->cache_path
= cache_path
;
167 lazy_params
->cache_max_size
= cache_max_size
;
168 lazy_params
->media_cache_path
= media_cache_path
;
169 lazy_params
->media_cache_max_size
= media_cache_max_size
;
170 lazy_params
->extensions_cookie_path
= extensions_cookie_path
;
171 lazy_params
->session_cookie_mode
= session_cookie_mode
;
172 lazy_params
->special_storage_policy
= special_storage_policy
;
174 io_data_
->lazy_params_
.reset(lazy_params
);
176 // Keep track of profile path and cache sizes separately so we can use them
177 // on demand when creating storage isolated URLRequestContextGetters.
178 io_data_
->profile_path_
= profile_path
;
179 io_data_
->app_cache_max_size_
= cache_max_size
;
180 io_data_
->app_media_cache_max_size_
= media_cache_max_size
;
182 io_data_
->predictor_
.reset(predictor
);
183 io_data_
->domain_reliability_monitor_
= domain_reliability_monitor
.Pass();
185 io_data_
->InitializeMetricsEnabledStateOnUIThread();
186 if (io_data_
->domain_reliability_monitor_
)
187 io_data_
->domain_reliability_monitor_
->MoveToNetworkThread();
189 // TODO(tbansal): Move this to IO thread once the data reduction proxy
190 // params are unified into a single object.
191 bool enable_quic_for_data_reduction_proxy
=
192 IOThread::ShouldEnableQuicForDataReductionProxy();
194 io_data_
->set_data_reduction_proxy_io_data(
195 CreateDataReductionProxyChromeIOData(
196 g_browser_process
->io_thread()->net_log(), profile_
->GetPrefs(),
197 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
198 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
199 enable_quic_for_data_reduction_proxy
)
202 base::SequencedWorkerPool
* pool
= BrowserThread::GetBlockingPool();
203 scoped_refptr
<base::SequencedTaskRunner
> db_task_runner
=
204 pool
->GetSequencedTaskRunnerWithShutdownBehavior(
205 pool
->GetSequenceToken(),
206 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
);
207 scoped_ptr
<data_reduction_proxy::DataStore
> store(
208 new data_reduction_proxy::DataStoreImpl(profile_path
));
209 DataReductionProxyChromeSettingsFactory::GetForBrowserContext(profile_
)
210 ->InitDataReductionProxySettings(
211 io_data_
->data_reduction_proxy_io_data(), profile_
->GetPrefs(),
212 profile_
->GetRequestContext(), store
.Pass(),
213 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
217 content::ResourceContext
*
218 ProfileImplIOData::Handle::GetResourceContext() const {
219 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
221 return GetResourceContextNoInit();
224 content::ResourceContext
*
225 ProfileImplIOData::Handle::GetResourceContextNoInit() const {
226 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
227 // Don't call LazyInitialize here, since the resource context is created at
228 // the beginning of initalization and is used by some members while they're
229 // being initialized (i.e. AppCacheService).
230 return io_data_
->GetResourceContext();
233 scoped_refptr
<ChromeURLRequestContextGetter
>
234 ProfileImplIOData::Handle::CreateMainRequestContextGetter(
235 content::ProtocolHandlerMap
* protocol_handlers
,
236 content::URLRequestInterceptorScopedVector request_interceptors
,
237 PrefService
* local_state
,
238 IOThread
* io_thread
) const {
239 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
241 DCHECK(!main_request_context_getter_
.get());
242 main_request_context_getter_
= ChromeURLRequestContextGetter::Create(
243 profile_
, io_data_
, protocol_handlers
, request_interceptors
.Pass());
246 ->InitNetworkPredictor(profile_
->GetPrefs(),
249 main_request_context_getter_
.get(),
252 content::NotificationService::current()->Notify(
253 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED
,
254 content::Source
<Profile
>(profile_
),
255 content::NotificationService::NoDetails());
256 return main_request_context_getter_
;
259 scoped_refptr
<ChromeURLRequestContextGetter
>
260 ProfileImplIOData::Handle::GetMediaRequestContextGetter() const {
261 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
263 if (!media_request_context_getter_
.get()) {
264 media_request_context_getter_
=
265 ChromeURLRequestContextGetter::CreateForMedia(profile_
, io_data_
);
267 return media_request_context_getter_
;
270 scoped_refptr
<ChromeURLRequestContextGetter
>
271 ProfileImplIOData::Handle::GetExtensionsRequestContextGetter() const {
272 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
274 if (!extensions_request_context_getter_
.get()) {
275 extensions_request_context_getter_
=
276 ChromeURLRequestContextGetter::CreateForExtensions(profile_
, io_data_
);
278 return extensions_request_context_getter_
;
281 scoped_refptr
<ChromeURLRequestContextGetter
>
282 ProfileImplIOData::Handle::CreateIsolatedAppRequestContextGetter(
283 const base::FilePath
& partition_path
,
285 content::ProtocolHandlerMap
* protocol_handlers
,
286 content::URLRequestInterceptorScopedVector request_interceptors
) const {
287 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
288 // Check that the partition_path is not the same as the base profile path. We
289 // expect isolated partition, which will never go to the default profile path.
290 CHECK(partition_path
!= profile_
->GetPath());
293 // Keep a map of request context getters, one per requested storage partition.
294 StoragePartitionDescriptor
descriptor(partition_path
, in_memory
);
295 ChromeURLRequestContextGetterMap::iterator iter
=
296 app_request_context_getter_map_
.find(descriptor
);
297 if (iter
!= app_request_context_getter_map_
.end())
300 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
301 protocol_handler_interceptor(
302 ProtocolHandlerRegistryFactory::GetForBrowserContext(profile_
)->
303 CreateJobInterceptorFactory());
304 ChromeURLRequestContextGetter
* context
=
305 ChromeURLRequestContextGetter::CreateForIsolatedApp(
309 protocol_handler_interceptor
.Pass(),
311 request_interceptors
.Pass());
312 app_request_context_getter_map_
[descriptor
] = context
;
317 scoped_refptr
<ChromeURLRequestContextGetter
>
318 ProfileImplIOData::Handle::GetIsolatedMediaRequestContextGetter(
319 const base::FilePath
& partition_path
,
320 bool in_memory
) const {
321 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
322 // We must have a non-default path, or this will act like the default media
324 CHECK(partition_path
!= profile_
->GetPath());
327 // Keep a map of request context getters, one per requested storage partition.
328 StoragePartitionDescriptor
descriptor(partition_path
, in_memory
);
329 ChromeURLRequestContextGetterMap::iterator iter
=
330 isolated_media_request_context_getter_map_
.find(descriptor
);
331 if (iter
!= isolated_media_request_context_getter_map_
.end())
334 // Get the app context as the starting point for the media context, so that
335 // it uses the app's cookie store.
336 ChromeURLRequestContextGetterMap::const_iterator app_iter
=
337 app_request_context_getter_map_
.find(descriptor
);
338 DCHECK(app_iter
!= app_request_context_getter_map_
.end());
339 ChromeURLRequestContextGetter
* app_context
= app_iter
->second
.get();
340 ChromeURLRequestContextGetter
* context
=
341 ChromeURLRequestContextGetter::CreateForIsolatedMedia(
342 profile_
, app_context
, io_data_
, descriptor
);
343 isolated_media_request_context_getter_map_
[descriptor
] = context
;
348 DevToolsNetworkController
*
349 ProfileImplIOData::Handle::GetDevToolsNetworkController() const {
350 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
351 return io_data_
->network_controller();
354 void ProfileImplIOData::Handle::ClearNetworkingHistorySince(
356 const base::Closure
& completion
) {
357 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
360 BrowserThread::PostTask(
361 BrowserThread::IO
, FROM_HERE
,
363 &ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread
,
364 base::Unretained(io_data_
),
369 void ProfileImplIOData::Handle::LazyInitialize() const {
370 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
374 // Set initialized_ to true at the beginning in case any of the objects
375 // below try to get the ResourceContext pointer.
377 PrefService
* pref_service
= profile_
->GetPrefs();
378 io_data_
->http_server_properties_manager_
=
379 chrome_browser_net::HttpServerPropertiesManagerFactory::CreateManager(
381 io_data_
->set_http_server_properties(
382 scoped_ptr
<net::HttpServerProperties
>(
383 io_data_
->http_server_properties_manager_
));
384 io_data_
->session_startup_pref()->Init(
385 prefs::kRestoreOnStartup
, pref_service
);
386 io_data_
->session_startup_pref()->MoveToThread(
387 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
));
388 #if defined(SAFE_BROWSING_SERVICE)
389 io_data_
->safe_browsing_enabled()->Init(prefs::kSafeBrowsingEnabled
,
391 io_data_
->safe_browsing_enabled()->MoveToThread(
392 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
));
394 io_data_
->InitializeOnUIThread(profile_
);
397 scoped_ptr
<ProfileIOData::ChromeURLRequestContextGetterVector
>
398 ProfileImplIOData::Handle::GetAllContextGetters() {
399 ChromeURLRequestContextGetterMap::iterator iter
;
400 scoped_ptr
<ChromeURLRequestContextGetterVector
> context_getters(
401 new ChromeURLRequestContextGetterVector());
403 iter
= isolated_media_request_context_getter_map_
.begin();
404 for (; iter
!= isolated_media_request_context_getter_map_
.end(); ++iter
)
405 context_getters
->push_back(iter
->second
);
407 iter
= app_request_context_getter_map_
.begin();
408 for (; iter
!= app_request_context_getter_map_
.end(); ++iter
)
409 context_getters
->push_back(iter
->second
);
411 if (extensions_request_context_getter_
.get())
412 context_getters
->push_back(extensions_request_context_getter_
);
414 if (media_request_context_getter_
.get())
415 context_getters
->push_back(media_request_context_getter_
);
417 if (main_request_context_getter_
.get())
418 context_getters
->push_back(main_request_context_getter_
);
420 return context_getters
.Pass();
423 ProfileImplIOData::LazyParams::LazyParams()
425 media_cache_max_size(0),
427 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES
) {}
429 ProfileImplIOData::LazyParams::~LazyParams() {}
431 ProfileImplIOData::ProfileImplIOData()
432 : ProfileIOData(Profile::REGULAR_PROFILE
),
433 http_server_properties_manager_(NULL
),
434 app_cache_max_size_(0),
435 app_media_cache_max_size_(0) {
438 ProfileImplIOData::~ProfileImplIOData() {
439 DestroyResourceContext();
441 if (media_request_context_
)
442 media_request_context_
->AssertNoURLRequests();
445 void ProfileImplIOData::InitializeInternal(
446 scoped_ptr
<ChromeNetworkDelegate
> chrome_network_delegate
,
447 ProfileParams
* profile_params
,
448 content::ProtocolHandlerMap
* protocol_handlers
,
449 content::URLRequestInterceptorScopedVector request_interceptors
) const {
450 // Set up a persistent store for use by the network stack on the IO thread.
451 base::FilePath
network_json_store_filepath(
452 profile_path_
.Append(chrome::kNetworkPersistentStateFilename
));
453 network_json_store_
= new JsonPrefStore(
454 network_json_store_filepath
,
455 JsonPrefStore::GetTaskRunnerForFile(network_json_store_filepath
,
456 BrowserThread::GetBlockingPool()),
457 scoped_ptr
<PrefFilter
>());
458 network_json_store_
->ReadPrefsAsync(nullptr);
460 net::URLRequestContext
* main_context
= main_request_context();
462 IOThread
* const io_thread
= profile_params
->io_thread
;
463 IOThread::Globals
* const io_thread_globals
= io_thread
->globals();
465 chrome_network_delegate
->set_predictor(predictor_
.get());
467 if (domain_reliability_monitor_
) {
468 domain_reliability::DomainReliabilityMonitor
* monitor
=
469 domain_reliability_monitor_
.get();
470 monitor
->InitURLRequestContext(main_context
);
471 monitor
->AddBakedInConfigs();
472 monitor
->SetDiscardUploads(!GetMetricsEnabledStateOnIOThread());
473 chrome_network_delegate
->set_domain_reliability_monitor(monitor
);
476 ApplyProfileParamsToContext(main_context
);
478 if (http_server_properties_manager_
)
479 http_server_properties_manager_
->InitializeOnNetworkThread();
481 main_context
->set_transport_security_state(transport_security_state());
483 main_context
->set_net_log(io_thread
->net_log());
485 network_delegate_
= data_reduction_proxy_io_data()->CreateNetworkDelegate(
486 chrome_network_delegate
.Pass(), true).Pass();
488 main_context
->set_network_delegate(network_delegate_
.get());
490 main_context
->set_http_server_properties(http_server_properties());
492 main_context
->set_host_resolver(
493 io_thread_globals
->host_resolver
.get());
494 main_context
->set_cert_transparency_verifier(
495 io_thread_globals
->cert_transparency_verifier
.get());
496 main_context
->set_http_auth_handler_factory(
497 io_thread_globals
->http_auth_handler_factory
.get());
499 main_context
->set_fraudulent_certificate_reporter(
500 fraudulent_certificate_reporter());
502 main_context
->set_proxy_service(proxy_service());
503 main_context
->set_backoff_manager(
504 io_thread_globals
->url_request_backoff_manager
.get());
506 scoped_refptr
<net::CookieStore
> cookie_store
= NULL
;
507 net::ChannelIDService
* channel_id_service
= NULL
;
509 // Set up cookie store.
510 if (!cookie_store
.get()) {
511 DCHECK(!lazy_params_
->cookie_path
.empty());
513 content::CookieStoreConfig
cookie_config(
514 lazy_params_
->cookie_path
,
515 lazy_params_
->session_cookie_mode
,
516 lazy_params_
->special_storage_policy
.get(),
517 profile_params
->cookie_monster_delegate
.get());
518 cookie_config
.crypto_delegate
=
519 chrome_browser_net::GetCookieCryptoDelegate();
520 cookie_store
= content::CreateCookieStore(cookie_config
);
523 main_context
->set_cookie_store(cookie_store
.get());
525 // Set up server bound cert service.
526 if (!channel_id_service
) {
527 DCHECK(!lazy_params_
->channel_id_path
.empty());
529 scoped_refptr
<QuotaPolicyChannelIDStore
> channel_id_db
=
530 new QuotaPolicyChannelIDStore(
531 lazy_params_
->channel_id_path
,
532 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
533 BrowserThread::GetBlockingPool()->GetSequenceToken()),
534 lazy_params_
->special_storage_policy
.get());
535 channel_id_service
= new net::ChannelIDService(
536 new net::DefaultChannelIDStore(channel_id_db
.get()),
537 base::WorkerPool::GetTaskRunner(true));
540 set_channel_id_service(channel_id_service
);
541 main_context
->set_channel_id_service(channel_id_service
);
543 scoped_ptr
<net::HttpCache
> main_cache
;
545 // TODO(ttuttle): Remove ScopedTracker below once crbug.com/436671 is fixed.
546 tracked_objects::ScopedTracker
tracking_profile(
547 FROM_HERE_WITH_EXPLICIT_FUNCTION("436671 HttpCache construction"));
548 net::HttpCache::DefaultBackend
* main_backend
=
549 new net::HttpCache::DefaultBackend(
551 ChooseCacheBackendType(),
552 lazy_params_
->cache_path
,
553 lazy_params_
->cache_max_size
,
554 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE
));
555 main_cache
= CreateMainHttpFactory(profile_params
, main_backend
);
558 main_http_factory_
.reset(main_cache
.release());
559 main_context
->set_http_transaction_factory(main_http_factory_
.get());
561 #if !defined(DISABLE_FTP_SUPPORT)
563 new net::FtpNetworkLayer(io_thread_globals
->host_resolver
.get()));
564 #endif // !defined(DISABLE_FTP_SUPPORT)
566 scoped_ptr
<net::URLRequestJobFactoryImpl
> main_job_factory(
567 new net::URLRequestJobFactoryImpl());
568 InstallProtocolHandlers(main_job_factory
.get(), protocol_handlers
);
570 // The data reduction proxy interceptor should be as close to the network
572 request_interceptors
.insert(
573 request_interceptors
.begin(),
574 data_reduction_proxy_io_data()->CreateInterceptor().release());
575 main_job_factory_
= SetUpJobFactoryDefaults(
576 main_job_factory
.Pass(),
577 request_interceptors
.Pass(),
578 profile_params
->protocol_handler_interceptor
.Pass(),
579 main_context
->network_delegate(),
581 main_context
->set_job_factory(main_job_factory_
.get());
582 main_context
->set_network_quality_estimator(
583 io_thread_globals
->network_quality_estimator
.get());
585 #if defined(ENABLE_EXTENSIONS)
586 InitializeExtensionsRequestContext(profile_params
);
589 // Setup SDCH for this profile.
590 sdch_manager_
.reset(new net::SdchManager
);
591 sdch_policy_
.reset(new net::SdchOwner(sdch_manager_
.get(), main_context
));
592 main_context
->set_sdch_manager(sdch_manager_
.get());
593 if (ShouldUseSdchPersistence()) {
594 sdch_policy_
->EnablePersistentStorage(network_json_store_
.get());
597 // Create a media request context based on the main context, but using a
598 // media cache. It shares the same job factory as the main context.
599 StoragePartitionDescriptor
details(profile_path_
, false);
600 media_request_context_
.reset(InitializeMediaRequestContext(main_context
,
603 lazy_params_
.reset();
606 void ProfileImplIOData::
607 InitializeExtensionsRequestContext(ProfileParams
* profile_params
) const {
608 net::URLRequestContext
* extensions_context
= extensions_request_context();
609 IOThread
* const io_thread
= profile_params
->io_thread
;
610 IOThread::Globals
* const io_thread_globals
= io_thread
->globals();
611 ApplyProfileParamsToContext(extensions_context
);
613 extensions_context
->set_transport_security_state(transport_security_state());
615 extensions_context
->set_net_log(io_thread
->net_log());
617 content::CookieStoreConfig
cookie_config(
618 lazy_params_
->extensions_cookie_path
,
619 lazy_params_
->session_cookie_mode
,
621 cookie_config
.crypto_delegate
=
622 chrome_browser_net::GetCookieCryptoDelegate();
623 net::CookieStore
* extensions_cookie_store
=
624 content::CreateCookieStore(cookie_config
);
625 // Enable cookies for devtools and extension URLs.
626 const char* const schemes
[] = {
627 content::kChromeDevToolsScheme
,
628 extensions::kExtensionScheme
630 extensions_cookie_store
->GetCookieMonster()->SetCookieableSchemes(
631 schemes
, arraysize(schemes
));
632 extensions_context
->set_cookie_store(extensions_cookie_store
);
634 scoped_ptr
<net::URLRequestJobFactoryImpl
> extensions_job_factory(
635 new net::URLRequestJobFactoryImpl());
636 // TODO(shalev): The extensions_job_factory has a NULL NetworkDelegate.
637 // Without a network_delegate, this protocol handler will never
638 // handle file: requests, but as a side effect it makes
639 // job_factory::IsHandledProtocol return true, which prevents attempts to
640 // handle the protocol externally. We pass NULL in to
641 // SetUpJobFactory() to get this effect.
642 extensions_job_factory_
= SetUpJobFactoryDefaults(
643 extensions_job_factory
.Pass(),
644 content::URLRequestInterceptorScopedVector(),
645 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>(),
648 extensions_context
->set_job_factory(extensions_job_factory_
.get());
649 extensions_context
->set_backoff_manager(
650 io_thread_globals
->url_request_backoff_manager
.get());
653 net::URLRequestContext
* ProfileImplIOData::InitializeAppRequestContext(
654 net::URLRequestContext
* main_context
,
655 const StoragePartitionDescriptor
& partition_descriptor
,
656 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
657 protocol_handler_interceptor
,
658 content::ProtocolHandlerMap
* protocol_handlers
,
659 content::URLRequestInterceptorScopedVector request_interceptors
) const {
660 // Copy most state from the main context.
661 AppRequestContext
* context
= new AppRequestContext();
662 context
->CopyFrom(main_context
);
664 base::FilePath cookie_path
= partition_descriptor
.path
.Append(
665 chrome::kCookieFilename
);
666 base::FilePath cache_path
=
667 partition_descriptor
.path
.Append(chrome::kCacheDirname
);
669 // Use a separate HTTP disk cache for isolated apps.
670 net::HttpCache::BackendFactory
* app_backend
= NULL
;
671 if (partition_descriptor
.in_memory
) {
672 app_backend
= net::HttpCache::DefaultBackend::InMemory(0);
674 app_backend
= new net::HttpCache::DefaultBackend(
676 ChooseCacheBackendType(),
679 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE
));
681 net::HttpNetworkSession
* main_network_session
=
682 main_http_factory_
->GetSession();
683 scoped_ptr
<net::HttpCache
> app_http_cache
=
684 CreateHttpFactory(main_network_session
, app_backend
);
686 scoped_refptr
<net::CookieStore
> cookie_store
= NULL
;
687 if (partition_descriptor
.in_memory
) {
688 cookie_store
= content::CreateCookieStore(content::CookieStoreConfig());
691 // Use an app-specific cookie store.
692 if (!cookie_store
.get()) {
693 DCHECK(!cookie_path
.empty());
695 // TODO(creis): We should have a cookie delegate for notifying the cookie
696 // extensions API, but we need to update it to understand isolated apps
698 content::CookieStoreConfig
cookie_config(
700 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES
,
702 cookie_config
.crypto_delegate
=
703 chrome_browser_net::GetCookieCryptoDelegate();
704 cookie_store
= content::CreateCookieStore(cookie_config
);
707 // Transfer ownership of the cookies and cache to AppRequestContext.
708 context
->SetCookieStore(cookie_store
.get());
709 context
->SetHttpTransactionFactory(app_http_cache
.Pass());
711 scoped_ptr
<net::URLRequestJobFactoryImpl
> job_factory(
712 new net::URLRequestJobFactoryImpl());
713 InstallProtocolHandlers(job_factory
.get(), protocol_handlers
);
714 // The data reduction proxy interceptor should be as close to the network
716 request_interceptors
.insert(
717 request_interceptors
.begin(),
718 data_reduction_proxy_io_data()->CreateInterceptor().release());
719 scoped_ptr
<net::URLRequestJobFactory
> top_job_factory(
720 SetUpJobFactoryDefaults(job_factory
.Pass(),
721 request_interceptors
.Pass(),
722 protocol_handler_interceptor
.Pass(),
723 main_context
->network_delegate(),
724 ftp_factory_
.get()));
725 context
->SetJobFactory(top_job_factory
.Pass());
730 net::URLRequestContext
*
731 ProfileImplIOData::InitializeMediaRequestContext(
732 net::URLRequestContext
* original_context
,
733 const StoragePartitionDescriptor
& partition_descriptor
) const {
734 // Copy most state from the original context.
735 MediaRequestContext
* context
= new MediaRequestContext();
736 context
->CopyFrom(original_context
);
738 // For in-memory context, return immediately after creating the new
739 // context before attaching a separate cache. It is important to return
740 // a new context rather than just reusing |original_context| because
741 // the caller expects to take ownership of the pointer.
742 if (partition_descriptor
.in_memory
)
745 using content::StoragePartition
;
746 base::FilePath cache_path
;
747 int cache_max_size
= app_media_cache_max_size_
;
748 if (partition_descriptor
.path
== profile_path_
) {
749 // lazy_params_ is only valid for the default media context creation.
750 cache_path
= lazy_params_
->media_cache_path
;
751 cache_max_size
= lazy_params_
->media_cache_max_size
;
753 cache_path
= partition_descriptor
.path
.Append(chrome::kMediaCacheDirname
);
756 // Use a separate HTTP disk cache for isolated apps.
757 net::HttpCache::BackendFactory
* media_backend
=
758 new net::HttpCache::DefaultBackend(
760 ChooseCacheBackendType(),
763 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE
));
764 net::HttpNetworkSession
* main_network_session
=
765 main_http_factory_
->GetSession();
766 scoped_ptr
<net::HttpCache
> media_http_cache
=
767 CreateHttpFactory(main_network_session
, media_backend
);
769 // Transfer ownership of the cache to MediaRequestContext.
770 context
->SetHttpTransactionFactory(media_http_cache
.Pass());
772 // Note that we do not create a new URLRequestJobFactory because
773 // the media context should behave exactly like its parent context
774 // in all respects except for cache behavior on media subresources.
775 // The CopyFrom() step above means that our media context will use
776 // the same URLRequestJobFactory instance that our parent context does.
781 net::URLRequestContext
*
782 ProfileImplIOData::AcquireMediaRequestContext() const {
783 DCHECK(media_request_context_
);
784 return media_request_context_
.get();
787 net::URLRequestContext
* ProfileImplIOData::AcquireIsolatedAppRequestContext(
788 net::URLRequestContext
* main_context
,
789 const StoragePartitionDescriptor
& partition_descriptor
,
790 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
791 protocol_handler_interceptor
,
792 content::ProtocolHandlerMap
* protocol_handlers
,
793 content::URLRequestInterceptorScopedVector request_interceptors
) const {
794 // We create per-app contexts on demand, unlike the others above.
795 net::URLRequestContext
* app_request_context
=
796 InitializeAppRequestContext(main_context
,
797 partition_descriptor
,
798 protocol_handler_interceptor
.Pass(),
800 request_interceptors
.Pass());
801 DCHECK(app_request_context
);
802 return app_request_context
;
805 net::URLRequestContext
*
806 ProfileImplIOData::AcquireIsolatedMediaRequestContext(
807 net::URLRequestContext
* app_context
,
808 const StoragePartitionDescriptor
& partition_descriptor
) const {
809 // We create per-app media contexts on demand, unlike the others above.
810 net::URLRequestContext
* media_request_context
=
811 InitializeMediaRequestContext(app_context
, partition_descriptor
);
812 DCHECK(media_request_context
);
813 return media_request_context
;
816 void ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread(
818 const base::Closure
& completion
) {
819 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
820 DCHECK(initialized());
822 DCHECK(transport_security_state());
823 // Completes synchronously.
824 transport_security_state()->DeleteAllDynamicDataSince(time
);
825 DCHECK(http_server_properties_manager_
);
826 http_server_properties_manager_
->Clear(completion
);