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_network_delegate.h"
31 #include "chrome/browser/net/connect_interceptor.h"
32 #include "chrome/browser/net/cookie_store_util.h"
33 #include "chrome/browser/net/http_server_properties_manager_factory.h"
34 #include "chrome/browser/net/predictor.h"
35 #include "chrome/browser/net/quota_policy_channel_id_store.h"
36 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_io_data.h"
37 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
38 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
39 #include "chrome/browser/profiles/profile.h"
40 #include "chrome/common/chrome_constants.h"
41 #include "chrome/common/chrome_switches.h"
42 #include "chrome/common/pref_names.h"
43 #include "chrome/common/url_constants.h"
44 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h"
45 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
46 #include "components/data_reduction_proxy/core/browser/data_store_impl.h"
47 #include "components/domain_reliability/monitor.h"
48 #include "components/net_log/chrome_net_log.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 DevToolsNetworkControllerHandle
*
349 ProfileImplIOData::Handle::GetDevToolsNetworkControllerHandle() const {
350 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
351 return io_data_
->network_controller_handle();
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_proxy_service(proxy_service());
500 main_context
->set_backoff_manager(
501 io_thread_globals
->url_request_backoff_manager
.get());
503 scoped_refptr
<net::CookieStore
> cookie_store
= NULL
;
504 net::ChannelIDService
* channel_id_service
= NULL
;
506 // Set up cookie store.
507 if (!cookie_store
.get()) {
508 DCHECK(!lazy_params_
->cookie_path
.empty());
510 content::CookieStoreConfig
cookie_config(
511 lazy_params_
->cookie_path
,
512 lazy_params_
->session_cookie_mode
,
513 lazy_params_
->special_storage_policy
.get(),
514 profile_params
->cookie_monster_delegate
.get());
515 cookie_config
.crypto_delegate
=
516 chrome_browser_net::GetCookieCryptoDelegate();
517 cookie_store
= content::CreateCookieStore(cookie_config
);
520 main_context
->set_cookie_store(cookie_store
.get());
522 // Set up server bound cert service.
523 if (!channel_id_service
) {
524 DCHECK(!lazy_params_
->channel_id_path
.empty());
526 scoped_refptr
<QuotaPolicyChannelIDStore
> channel_id_db
=
527 new QuotaPolicyChannelIDStore(
528 lazy_params_
->channel_id_path
,
529 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
530 BrowserThread::GetBlockingPool()->GetSequenceToken()),
531 lazy_params_
->special_storage_policy
.get());
532 channel_id_service
= new net::ChannelIDService(
533 new net::DefaultChannelIDStore(channel_id_db
.get()),
534 base::WorkerPool::GetTaskRunner(true));
537 set_channel_id_service(channel_id_service
);
538 main_context
->set_channel_id_service(channel_id_service
);
540 scoped_ptr
<net::HttpCache
> main_cache
;
542 // TODO(ttuttle): Remove ScopedTracker below once crbug.com/436671 is fixed.
543 tracked_objects::ScopedTracker
tracking_profile(
544 FROM_HERE_WITH_EXPLICIT_FUNCTION("436671 HttpCache construction"));
545 net::HttpCache::DefaultBackend
* main_backend
=
546 new net::HttpCache::DefaultBackend(
548 ChooseCacheBackendType(),
549 lazy_params_
->cache_path
,
550 lazy_params_
->cache_max_size
,
551 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE
));
552 main_cache
= CreateMainHttpFactory(profile_params
, main_backend
);
555 main_http_factory_
.reset(main_cache
.release());
556 main_context
->set_http_transaction_factory(main_http_factory_
.get());
558 #if !defined(DISABLE_FTP_SUPPORT)
560 new net::FtpNetworkLayer(io_thread_globals
->host_resolver
.get()));
561 #endif // !defined(DISABLE_FTP_SUPPORT)
563 scoped_ptr
<net::URLRequestJobFactoryImpl
> main_job_factory(
564 new net::URLRequestJobFactoryImpl());
565 InstallProtocolHandlers(main_job_factory
.get(), protocol_handlers
);
567 // The data reduction proxy interceptor should be as close to the network
569 request_interceptors
.insert(
570 request_interceptors
.begin(),
571 data_reduction_proxy_io_data()->CreateInterceptor().release());
572 main_job_factory_
= SetUpJobFactoryDefaults(
573 main_job_factory
.Pass(),
574 request_interceptors
.Pass(),
575 profile_params
->protocol_handler_interceptor
.Pass(),
576 main_context
->network_delegate(),
578 main_context
->set_job_factory(main_job_factory_
.get());
579 main_context
->set_network_quality_estimator(
580 io_thread_globals
->network_quality_estimator
.get());
582 #if defined(ENABLE_EXTENSIONS)
583 InitializeExtensionsRequestContext(profile_params
);
586 // Setup SDCH for this profile.
587 sdch_manager_
.reset(new net::SdchManager
);
588 sdch_policy_
.reset(new net::SdchOwner(sdch_manager_
.get(), main_context
));
589 main_context
->set_sdch_manager(sdch_manager_
.get());
590 if (ShouldUseSdchPersistence()) {
591 sdch_policy_
->EnablePersistentStorage(network_json_store_
.get());
594 // Create a media request context based on the main context, but using a
595 // media cache. It shares the same job factory as the main context.
596 StoragePartitionDescriptor
details(profile_path_
, false);
597 media_request_context_
.reset(InitializeMediaRequestContext(main_context
,
600 lazy_params_
.reset();
603 void ProfileImplIOData::
604 InitializeExtensionsRequestContext(ProfileParams
* profile_params
) const {
605 net::URLRequestContext
* extensions_context
= extensions_request_context();
606 IOThread
* const io_thread
= profile_params
->io_thread
;
607 IOThread::Globals
* const io_thread_globals
= io_thread
->globals();
608 ApplyProfileParamsToContext(extensions_context
);
610 extensions_context
->set_transport_security_state(transport_security_state());
612 extensions_context
->set_net_log(io_thread
->net_log());
614 content::CookieStoreConfig
cookie_config(
615 lazy_params_
->extensions_cookie_path
,
616 lazy_params_
->session_cookie_mode
,
618 cookie_config
.crypto_delegate
=
619 chrome_browser_net::GetCookieCryptoDelegate();
620 net::CookieStore
* extensions_cookie_store
=
621 content::CreateCookieStore(cookie_config
);
622 // Enable cookies for devtools and extension URLs.
623 const char* const schemes
[] = {
624 content::kChromeDevToolsScheme
,
625 extensions::kExtensionScheme
627 extensions_cookie_store
->GetCookieMonster()->SetCookieableSchemes(
628 schemes
, arraysize(schemes
));
629 extensions_context
->set_cookie_store(extensions_cookie_store
);
631 scoped_ptr
<net::URLRequestJobFactoryImpl
> extensions_job_factory(
632 new net::URLRequestJobFactoryImpl());
633 // TODO(shalev): The extensions_job_factory has a NULL NetworkDelegate.
634 // Without a network_delegate, this protocol handler will never
635 // handle file: requests, but as a side effect it makes
636 // job_factory::IsHandledProtocol return true, which prevents attempts to
637 // handle the protocol externally. We pass NULL in to
638 // SetUpJobFactory() to get this effect.
639 extensions_job_factory_
= SetUpJobFactoryDefaults(
640 extensions_job_factory
.Pass(),
641 content::URLRequestInterceptorScopedVector(),
642 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>(),
645 extensions_context
->set_job_factory(extensions_job_factory_
.get());
646 extensions_context
->set_backoff_manager(
647 io_thread_globals
->url_request_backoff_manager
.get());
650 net::URLRequestContext
* ProfileImplIOData::InitializeAppRequestContext(
651 net::URLRequestContext
* main_context
,
652 const StoragePartitionDescriptor
& partition_descriptor
,
653 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
654 protocol_handler_interceptor
,
655 content::ProtocolHandlerMap
* protocol_handlers
,
656 content::URLRequestInterceptorScopedVector request_interceptors
) const {
657 // Copy most state from the main context.
658 AppRequestContext
* context
= new AppRequestContext();
659 context
->CopyFrom(main_context
);
661 base::FilePath cookie_path
= partition_descriptor
.path
.Append(
662 chrome::kCookieFilename
);
663 base::FilePath cache_path
=
664 partition_descriptor
.path
.Append(chrome::kCacheDirname
);
666 // Use a separate HTTP disk cache for isolated apps.
667 net::HttpCache::BackendFactory
* app_backend
= NULL
;
668 if (partition_descriptor
.in_memory
) {
669 app_backend
= net::HttpCache::DefaultBackend::InMemory(0);
671 app_backend
= new net::HttpCache::DefaultBackend(
673 ChooseCacheBackendType(),
676 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE
));
678 net::HttpNetworkSession
* main_network_session
=
679 main_http_factory_
->GetSession();
680 scoped_ptr
<net::HttpCache
> app_http_cache
=
681 CreateHttpFactory(main_network_session
, app_backend
);
683 scoped_refptr
<net::CookieStore
> cookie_store
= NULL
;
684 if (partition_descriptor
.in_memory
) {
685 cookie_store
= content::CreateCookieStore(content::CookieStoreConfig());
688 // Use an app-specific cookie store.
689 if (!cookie_store
.get()) {
690 DCHECK(!cookie_path
.empty());
692 // TODO(creis): We should have a cookie delegate for notifying the cookie
693 // extensions API, but we need to update it to understand isolated apps
695 content::CookieStoreConfig
cookie_config(
697 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES
,
699 cookie_config
.crypto_delegate
=
700 chrome_browser_net::GetCookieCryptoDelegate();
701 cookie_store
= content::CreateCookieStore(cookie_config
);
704 // Transfer ownership of the cookies and cache to AppRequestContext.
705 context
->SetCookieStore(cookie_store
.get());
706 context
->SetHttpTransactionFactory(app_http_cache
.Pass());
708 scoped_ptr
<net::URLRequestJobFactoryImpl
> job_factory(
709 new net::URLRequestJobFactoryImpl());
710 InstallProtocolHandlers(job_factory
.get(), protocol_handlers
);
711 // The data reduction proxy interceptor should be as close to the network
713 request_interceptors
.insert(
714 request_interceptors
.begin(),
715 data_reduction_proxy_io_data()->CreateInterceptor().release());
716 scoped_ptr
<net::URLRequestJobFactory
> top_job_factory(
717 SetUpJobFactoryDefaults(job_factory
.Pass(),
718 request_interceptors
.Pass(),
719 protocol_handler_interceptor
.Pass(),
720 main_context
->network_delegate(),
721 ftp_factory_
.get()));
722 context
->SetJobFactory(top_job_factory
.Pass());
727 net::URLRequestContext
*
728 ProfileImplIOData::InitializeMediaRequestContext(
729 net::URLRequestContext
* original_context
,
730 const StoragePartitionDescriptor
& partition_descriptor
) const {
731 // Copy most state from the original context.
732 MediaRequestContext
* context
= new MediaRequestContext();
733 context
->CopyFrom(original_context
);
735 // For in-memory context, return immediately after creating the new
736 // context before attaching a separate cache. It is important to return
737 // a new context rather than just reusing |original_context| because
738 // the caller expects to take ownership of the pointer.
739 if (partition_descriptor
.in_memory
)
742 using content::StoragePartition
;
743 base::FilePath cache_path
;
744 int cache_max_size
= app_media_cache_max_size_
;
745 if (partition_descriptor
.path
== profile_path_
) {
746 // lazy_params_ is only valid for the default media context creation.
747 cache_path
= lazy_params_
->media_cache_path
;
748 cache_max_size
= lazy_params_
->media_cache_max_size
;
750 cache_path
= partition_descriptor
.path
.Append(chrome::kMediaCacheDirname
);
753 // Use a separate HTTP disk cache for isolated apps.
754 net::HttpCache::BackendFactory
* media_backend
=
755 new net::HttpCache::DefaultBackend(
757 ChooseCacheBackendType(),
760 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE
));
761 net::HttpNetworkSession
* main_network_session
=
762 main_http_factory_
->GetSession();
763 scoped_ptr
<net::HttpCache
> media_http_cache
=
764 CreateHttpFactory(main_network_session
, media_backend
);
766 // Transfer ownership of the cache to MediaRequestContext.
767 context
->SetHttpTransactionFactory(media_http_cache
.Pass());
769 // Note that we do not create a new URLRequestJobFactory because
770 // the media context should behave exactly like its parent context
771 // in all respects except for cache behavior on media subresources.
772 // The CopyFrom() step above means that our media context will use
773 // the same URLRequestJobFactory instance that our parent context does.
778 net::URLRequestContext
*
779 ProfileImplIOData::AcquireMediaRequestContext() const {
780 DCHECK(media_request_context_
);
781 return media_request_context_
.get();
784 net::URLRequestContext
* ProfileImplIOData::AcquireIsolatedAppRequestContext(
785 net::URLRequestContext
* main_context
,
786 const StoragePartitionDescriptor
& partition_descriptor
,
787 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
788 protocol_handler_interceptor
,
789 content::ProtocolHandlerMap
* protocol_handlers
,
790 content::URLRequestInterceptorScopedVector request_interceptors
) const {
791 // We create per-app contexts on demand, unlike the others above.
792 net::URLRequestContext
* app_request_context
=
793 InitializeAppRequestContext(main_context
,
794 partition_descriptor
,
795 protocol_handler_interceptor
.Pass(),
797 request_interceptors
.Pass());
798 DCHECK(app_request_context
);
799 return app_request_context
;
802 net::URLRequestContext
*
803 ProfileImplIOData::AcquireIsolatedMediaRequestContext(
804 net::URLRequestContext
* app_context
,
805 const StoragePartitionDescriptor
& partition_descriptor
) const {
806 // We create per-app media contexts on demand, unlike the others above.
807 net::URLRequestContext
* media_request_context
=
808 InitializeMediaRequestContext(app_context
, partition_descriptor
);
809 DCHECK(media_request_context
);
810 return media_request_context
;
813 void ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread(
815 const base::Closure
& completion
) {
816 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
817 DCHECK(initialized());
819 DCHECK(transport_security_state());
820 // Completes synchronously.
821 transport_security_state()->DeleteAllDynamicDataSince(time
);
822 DCHECK(http_server_properties_manager_
);
823 http_server_properties_manager_
->Clear(completion
);