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/domain_reliability/monitor.h"
48 #include "content/public/browser/browser_thread.h"
49 #include "content/public/browser/cookie_store_factory.h"
50 #include "content/public/browser/notification_service.h"
51 #include "content/public/browser/resource_context.h"
52 #include "content/public/browser/storage_partition.h"
53 #include "extensions/browser/extension_protocols.h"
54 #include "extensions/common/constants.h"
55 #include "net/base/cache_type.h"
56 #include "net/base/sdch_manager.h"
57 #include "net/ftp/ftp_network_layer.h"
58 #include "net/http/http_cache.h"
59 #include "net/http/http_server_properties_manager.h"
60 #include "net/sdch/sdch_owner.h"
61 #include "net/ssl/channel_id_service.h"
62 #include "net/url_request/url_request_intercepting_job_factory.h"
63 #include "net/url_request/url_request_job_factory_impl.h"
64 #include "storage/browser/quota/special_storage_policy.h"
68 net::BackendType
ChooseCacheBackendType() {
69 #if defined(OS_ANDROID)
70 return net::CACHE_BACKEND_SIMPLE
;
72 const base::CommandLine
& command_line
=
73 *base::CommandLine::ForCurrentProcess();
74 if (command_line
.HasSwitch(switches::kUseSimpleCacheBackend
)) {
75 const std::string opt_value
=
76 command_line
.GetSwitchValueASCII(switches::kUseSimpleCacheBackend
);
77 if (LowerCaseEqualsASCII(opt_value
, "off"))
78 return net::CACHE_BACKEND_BLOCKFILE
;
79 if (opt_value
.empty() || LowerCaseEqualsASCII(opt_value
, "on"))
80 return net::CACHE_BACKEND_SIMPLE
;
82 const std::string experiment_name
=
83 base::FieldTrialList::FindFullName("SimpleCacheTrial");
84 if (experiment_name
== "ExperimentYes" ||
85 experiment_name
== "ExperimentYes2") {
86 return net::CACHE_BACKEND_SIMPLE
;
88 return net::CACHE_BACKEND_BLOCKFILE
;
92 bool ShouldUseSdchPersistence() {
93 const std::string group
=
94 base::FieldTrialList::FindFullName("SdchPersistence");
95 const base::CommandLine
* command_line
=
96 base::CommandLine::ForCurrentProcess();
97 if (command_line
->HasSwitch(switches::kEnableSdchPersistence
)) {
100 if (command_line
->HasSwitch(switches::kDisableSdchPersistence
)) {
103 return group
== "Enabled";
108 using content::BrowserThread
;
110 ProfileImplIOData::Handle::Handle(Profile
* profile
)
111 : io_data_(new ProfileImplIOData
),
113 initialized_(false) {
114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
118 ProfileImplIOData::Handle::~Handle() {
119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
120 if (io_data_
->predictor_
!= NULL
) {
121 // io_data_->predictor_ might be NULL if Init() was never called
122 // (i.e. we shut down before ProfileImpl::DoFinalInit() got called).
123 bool save_prefs
= true;
124 #if defined(OS_CHROMEOS)
125 save_prefs
= !chromeos::ProfileHelper::IsSigninProfile(profile_
);
128 io_data_
->predictor_
->SaveStateForNextStartupAndTrim();
129 io_data_
->predictor_
->ShutdownOnUIThread();
132 if (io_data_
->http_server_properties_manager_
)
133 io_data_
->http_server_properties_manager_
->ShutdownOnPrefThread();
135 io_data_
->data_reduction_proxy_io_data()->ShutdownOnUIThread();
136 io_data_
->ShutdownOnUIThread(GetAllContextGetters().Pass());
139 void ProfileImplIOData::Handle::Init(
140 const base::FilePath
& cookie_path
,
141 const base::FilePath
& channel_id_path
,
142 const base::FilePath
& cache_path
,
144 const base::FilePath
& media_cache_path
,
145 int media_cache_max_size
,
146 const base::FilePath
& extensions_cookie_path
,
147 const base::FilePath
& profile_path
,
148 chrome_browser_net::Predictor
* predictor
,
149 content::CookieStoreConfig::SessionCookieMode session_cookie_mode
,
150 storage::SpecialStoragePolicy
* special_storage_policy
,
151 scoped_ptr
<domain_reliability::DomainReliabilityMonitor
>
152 domain_reliability_monitor
) {
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
154 DCHECK(!io_data_
->lazy_params_
);
157 LazyParams
* lazy_params
= new LazyParams();
159 lazy_params
->cookie_path
= cookie_path
;
160 lazy_params
->channel_id_path
= channel_id_path
;
161 lazy_params
->cache_path
= cache_path
;
162 lazy_params
->cache_max_size
= cache_max_size
;
163 lazy_params
->media_cache_path
= media_cache_path
;
164 lazy_params
->media_cache_max_size
= media_cache_max_size
;
165 lazy_params
->extensions_cookie_path
= extensions_cookie_path
;
166 lazy_params
->session_cookie_mode
= session_cookie_mode
;
167 lazy_params
->special_storage_policy
= special_storage_policy
;
169 io_data_
->lazy_params_
.reset(lazy_params
);
171 // Keep track of profile path and cache sizes separately so we can use them
172 // on demand when creating storage isolated URLRequestContextGetters.
173 io_data_
->profile_path_
= profile_path
;
174 io_data_
->app_cache_max_size_
= cache_max_size
;
175 io_data_
->app_media_cache_max_size_
= media_cache_max_size
;
177 io_data_
->predictor_
.reset(predictor
);
178 io_data_
->domain_reliability_monitor_
= domain_reliability_monitor
.Pass();
180 io_data_
->InitializeMetricsEnabledStateOnUIThread();
181 if (io_data_
->domain_reliability_monitor_
)
182 io_data_
->domain_reliability_monitor_
->MoveToNetworkThread();
184 // TODO(tbansal): Move this to IO thread once the data reduction proxy
185 // params are unified into a single object.
186 bool enable_quic_for_data_reduction_proxy
=
187 IOThread::ShouldEnableQuicForDataReductionProxy();
189 io_data_
->set_data_reduction_proxy_io_data(
190 CreateDataReductionProxyChromeIOData(
191 g_browser_process
->io_thread()->net_log(), profile_
->GetPrefs(),
192 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
193 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
194 enable_quic_for_data_reduction_proxy
)
197 DataReductionProxyChromeSettingsFactory::GetForBrowserContext(profile_
)->
198 InitDataReductionProxySettings(
199 io_data_
->data_reduction_proxy_io_data(), profile_
->GetPrefs(),
200 profile_
->GetRequestContext(),
201 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
));
204 content::ResourceContext
*
205 ProfileImplIOData::Handle::GetResourceContext() const {
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
208 return GetResourceContextNoInit();
211 content::ResourceContext
*
212 ProfileImplIOData::Handle::GetResourceContextNoInit() const {
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
214 // Don't call LazyInitialize here, since the resource context is created at
215 // the beginning of initalization and is used by some members while they're
216 // being initialized (i.e. AppCacheService).
217 return io_data_
->GetResourceContext();
220 scoped_refptr
<ChromeURLRequestContextGetter
>
221 ProfileImplIOData::Handle::CreateMainRequestContextGetter(
222 content::ProtocolHandlerMap
* protocol_handlers
,
223 content::URLRequestInterceptorScopedVector request_interceptors
,
224 PrefService
* local_state
,
225 IOThread
* io_thread
) const {
226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
228 DCHECK(!main_request_context_getter_
.get());
229 main_request_context_getter_
= ChromeURLRequestContextGetter::Create(
230 profile_
, io_data_
, protocol_handlers
, request_interceptors
.Pass());
233 ->InitNetworkPredictor(profile_
->GetPrefs(),
236 main_request_context_getter_
.get(),
239 content::NotificationService::current()->Notify(
240 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED
,
241 content::Source
<Profile
>(profile_
),
242 content::NotificationService::NoDetails());
243 return main_request_context_getter_
;
246 scoped_refptr
<ChromeURLRequestContextGetter
>
247 ProfileImplIOData::Handle::GetMediaRequestContextGetter() const {
248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
250 if (!media_request_context_getter_
.get()) {
251 media_request_context_getter_
=
252 ChromeURLRequestContextGetter::CreateForMedia(profile_
, io_data_
);
254 return media_request_context_getter_
;
257 scoped_refptr
<ChromeURLRequestContextGetter
>
258 ProfileImplIOData::Handle::GetExtensionsRequestContextGetter() const {
259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
261 if (!extensions_request_context_getter_
.get()) {
262 extensions_request_context_getter_
=
263 ChromeURLRequestContextGetter::CreateForExtensions(profile_
, io_data_
);
265 return extensions_request_context_getter_
;
268 scoped_refptr
<ChromeURLRequestContextGetter
>
269 ProfileImplIOData::Handle::CreateIsolatedAppRequestContextGetter(
270 const base::FilePath
& partition_path
,
272 content::ProtocolHandlerMap
* protocol_handlers
,
273 content::URLRequestInterceptorScopedVector request_interceptors
) const {
274 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
275 // Check that the partition_path is not the same as the base profile path. We
276 // expect isolated partition, which will never go to the default profile path.
277 CHECK(partition_path
!= profile_
->GetPath());
280 // Keep a map of request context getters, one per requested storage partition.
281 StoragePartitionDescriptor
descriptor(partition_path
, in_memory
);
282 ChromeURLRequestContextGetterMap::iterator iter
=
283 app_request_context_getter_map_
.find(descriptor
);
284 if (iter
!= app_request_context_getter_map_
.end())
287 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
288 protocol_handler_interceptor(
289 ProtocolHandlerRegistryFactory::GetForBrowserContext(profile_
)->
290 CreateJobInterceptorFactory());
291 ChromeURLRequestContextGetter
* context
=
292 ChromeURLRequestContextGetter::CreateForIsolatedApp(
296 protocol_handler_interceptor
.Pass(),
298 request_interceptors
.Pass());
299 app_request_context_getter_map_
[descriptor
] = context
;
304 scoped_refptr
<ChromeURLRequestContextGetter
>
305 ProfileImplIOData::Handle::GetIsolatedMediaRequestContextGetter(
306 const base::FilePath
& partition_path
,
307 bool in_memory
) const {
308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
309 // We must have a non-default path, or this will act like the default media
311 CHECK(partition_path
!= profile_
->GetPath());
314 // Keep a map of request context getters, one per requested storage partition.
315 StoragePartitionDescriptor
descriptor(partition_path
, in_memory
);
316 ChromeURLRequestContextGetterMap::iterator iter
=
317 isolated_media_request_context_getter_map_
.find(descriptor
);
318 if (iter
!= isolated_media_request_context_getter_map_
.end())
321 // Get the app context as the starting point for the media context, so that
322 // it uses the app's cookie store.
323 ChromeURLRequestContextGetterMap::const_iterator app_iter
=
324 app_request_context_getter_map_
.find(descriptor
);
325 DCHECK(app_iter
!= app_request_context_getter_map_
.end());
326 ChromeURLRequestContextGetter
* app_context
= app_iter
->second
.get();
327 ChromeURLRequestContextGetter
* context
=
328 ChromeURLRequestContextGetter::CreateForIsolatedMedia(
329 profile_
, app_context
, io_data_
, descriptor
);
330 isolated_media_request_context_getter_map_
[descriptor
] = context
;
335 DevToolsNetworkController
*
336 ProfileImplIOData::Handle::GetDevToolsNetworkController() const {
337 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
338 return io_data_
->network_controller();
341 void ProfileImplIOData::Handle::ClearNetworkingHistorySince(
343 const base::Closure
& completion
) {
344 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
347 BrowserThread::PostTask(
348 BrowserThread::IO
, FROM_HERE
,
350 &ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread
,
351 base::Unretained(io_data_
),
356 void ProfileImplIOData::Handle::LazyInitialize() const {
357 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
361 // Set initialized_ to true at the beginning in case any of the objects
362 // below try to get the ResourceContext pointer.
364 PrefService
* pref_service
= profile_
->GetPrefs();
365 io_data_
->http_server_properties_manager_
=
366 chrome_browser_net::HttpServerPropertiesManagerFactory::CreateManager(
368 io_data_
->set_http_server_properties(
369 scoped_ptr
<net::HttpServerProperties
>(
370 io_data_
->http_server_properties_manager_
));
371 io_data_
->session_startup_pref()->Init(
372 prefs::kRestoreOnStartup
, pref_service
);
373 io_data_
->session_startup_pref()->MoveToThread(
374 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
));
375 #if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
376 io_data_
->safe_browsing_enabled()->Init(prefs::kSafeBrowsingEnabled
,
378 io_data_
->safe_browsing_enabled()->MoveToThread(
379 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
));
381 io_data_
->InitializeOnUIThread(profile_
);
384 scoped_ptr
<ProfileIOData::ChromeURLRequestContextGetterVector
>
385 ProfileImplIOData::Handle::GetAllContextGetters() {
386 ChromeURLRequestContextGetterMap::iterator iter
;
387 scoped_ptr
<ChromeURLRequestContextGetterVector
> context_getters(
388 new ChromeURLRequestContextGetterVector());
390 iter
= isolated_media_request_context_getter_map_
.begin();
391 for (; iter
!= isolated_media_request_context_getter_map_
.end(); ++iter
)
392 context_getters
->push_back(iter
->second
);
394 iter
= app_request_context_getter_map_
.begin();
395 for (; iter
!= app_request_context_getter_map_
.end(); ++iter
)
396 context_getters
->push_back(iter
->second
);
398 if (extensions_request_context_getter_
.get())
399 context_getters
->push_back(extensions_request_context_getter_
);
401 if (media_request_context_getter_
.get())
402 context_getters
->push_back(media_request_context_getter_
);
404 if (main_request_context_getter_
.get())
405 context_getters
->push_back(main_request_context_getter_
);
407 return context_getters
.Pass();
410 ProfileImplIOData::LazyParams::LazyParams()
412 media_cache_max_size(0),
414 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES
) {}
416 ProfileImplIOData::LazyParams::~LazyParams() {}
418 ProfileImplIOData::ProfileImplIOData()
419 : ProfileIOData(Profile::REGULAR_PROFILE
),
420 http_server_properties_manager_(NULL
),
421 app_cache_max_size_(0),
422 app_media_cache_max_size_(0) {
425 ProfileImplIOData::~ProfileImplIOData() {
426 DestroyResourceContext();
428 if (media_request_context_
)
429 media_request_context_
->AssertNoURLRequests();
432 void ProfileImplIOData::InitializeInternal(
433 scoped_ptr
<ChromeNetworkDelegate
> chrome_network_delegate
,
434 ProfileParams
* profile_params
,
435 content::ProtocolHandlerMap
* protocol_handlers
,
436 content::URLRequestInterceptorScopedVector request_interceptors
) const {
437 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
438 tracked_objects::ScopedTracker
tracking_profile(
439 FROM_HERE_WITH_EXPLICIT_FUNCTION(
440 "436671 ProfileImplIOData::InitializeInternal"));
442 // Set up a persistent store for use by the network stack on the IO thread.
443 base::FilePath
network_json_store_filepath(
444 profile_path_
.Append(chrome::kNetworkPersistentStateFilename
));
445 network_json_store_
= new JsonPrefStore(
446 network_json_store_filepath
,
447 JsonPrefStore::GetTaskRunnerForFile(network_json_store_filepath
,
448 BrowserThread::GetBlockingPool()),
449 scoped_ptr
<PrefFilter
>());
450 network_json_store_
->ReadPrefsAsync(nullptr);
452 net::URLRequestContext
* main_context
= main_request_context();
454 IOThread
* const io_thread
= profile_params
->io_thread
;
455 IOThread::Globals
* const io_thread_globals
= io_thread
->globals();
457 chrome_network_delegate
->set_predictor(predictor_
.get());
459 if (domain_reliability_monitor_
) {
460 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
461 tracked_objects::ScopedTracker
tracking_profile1(
462 FROM_HERE_WITH_EXPLICIT_FUNCTION(
463 "436671 ProfileImplIOData::InitializeInternal1"));
465 domain_reliability::DomainReliabilityMonitor
* monitor
=
466 domain_reliability_monitor_
.get();
467 monitor
->InitURLRequestContext(main_context
);
468 monitor
->AddBakedInConfigs();
469 monitor
->SetDiscardUploads(!GetMetricsEnabledStateOnIOThread());
470 chrome_network_delegate
->set_domain_reliability_monitor(monitor
);
473 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
474 tracked_objects::ScopedTracker
tracking_profile2(
475 FROM_HERE_WITH_EXPLICIT_FUNCTION(
476 "436671 ProfileImplIOData::InitializeInternal2"));
478 ApplyProfileParamsToContext(main_context
);
480 if (http_server_properties_manager_
)
481 http_server_properties_manager_
->InitializeOnNetworkThread();
483 main_context
->set_transport_security_state(transport_security_state());
485 main_context
->set_net_log(io_thread
->net_log());
487 network_delegate_
= data_reduction_proxy_io_data()->CreateNetworkDelegate(
488 chrome_network_delegate
.Pass(), true).Pass();
490 main_context
->set_network_delegate(network_delegate_
.get());
492 main_context
->set_http_server_properties(http_server_properties());
494 main_context
->set_host_resolver(
495 io_thread_globals
->host_resolver
.get());
496 main_context
->set_cert_transparency_verifier(
497 io_thread_globals
->cert_transparency_verifier
.get());
498 main_context
->set_http_auth_handler_factory(
499 io_thread_globals
->http_auth_handler_factory
.get());
501 main_context
->set_fraudulent_certificate_reporter(
502 fraudulent_certificate_reporter());
504 main_context
->set_throttler_manager(
505 io_thread_globals
->throttler_manager
.get());
507 main_context
->set_proxy_service(proxy_service());
509 scoped_refptr
<net::CookieStore
> cookie_store
= NULL
;
510 net::ChannelIDService
* channel_id_service
= NULL
;
512 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
513 tracked_objects::ScopedTracker
tracking_profile5(
514 FROM_HERE_WITH_EXPLICIT_FUNCTION(
515 "436671 ProfileImplIOData::InitializeInternal5"));
517 // setup cookie store
518 if (!cookie_store
.get()) {
519 DCHECK(!lazy_params_
->cookie_path
.empty());
521 content::CookieStoreConfig
cookie_config(
522 lazy_params_
->cookie_path
,
523 lazy_params_
->session_cookie_mode
,
524 lazy_params_
->special_storage_policy
.get(),
525 profile_params
->cookie_monster_delegate
.get());
526 cookie_config
.crypto_delegate
=
527 chrome_browser_net::GetCookieCryptoDelegate();
528 cookie_store
= content::CreateCookieStore(cookie_config
);
531 main_context
->set_cookie_store(cookie_store
.get());
533 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
534 tracked_objects::ScopedTracker
tracking_profile6(
535 FROM_HERE_WITH_EXPLICIT_FUNCTION(
536 "436671 ProfileImplIOData::InitializeInternal6"));
538 // Setup server bound cert service.
539 if (!channel_id_service
) {
540 DCHECK(!lazy_params_
->channel_id_path
.empty());
542 scoped_refptr
<QuotaPolicyChannelIDStore
> channel_id_db
=
543 new QuotaPolicyChannelIDStore(
544 lazy_params_
->channel_id_path
,
545 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
546 BrowserThread::GetBlockingPool()->GetSequenceToken()),
547 lazy_params_
->special_storage_policy
.get());
548 channel_id_service
= new net::ChannelIDService(
549 new net::DefaultChannelIDStore(channel_id_db
.get()),
550 base::WorkerPool::GetTaskRunner(true));
553 set_channel_id_service(channel_id_service
);
554 main_context
->set_channel_id_service(channel_id_service
);
556 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
557 tracked_objects::ScopedTracker
tracking_profile7(
558 FROM_HERE_WITH_EXPLICIT_FUNCTION(
559 "436671 ProfileImplIOData::InitializeInternal7"));
561 net::HttpCache::DefaultBackend
* main_backend
=
562 new net::HttpCache::DefaultBackend(
564 ChooseCacheBackendType(),
565 lazy_params_
->cache_path
,
566 lazy_params_
->cache_max_size
,
567 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE
));
568 scoped_ptr
<net::HttpCache
> main_cache
= CreateMainHttpFactory(
569 profile_params
, main_backend
);
571 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
572 tracked_objects::ScopedTracker
tracking_profile71(
573 FROM_HERE_WITH_EXPLICIT_FUNCTION(
574 "436671 ProfileImplIOData::InitializeInternal71"));
576 main_http_factory_
.reset(main_cache
.release());
577 main_context
->set_http_transaction_factory(main_http_factory_
.get());
579 #if !defined(DISABLE_FTP_SUPPORT)
581 new net::FtpNetworkLayer(io_thread_globals
->host_resolver
.get()));
582 #endif // !defined(DISABLE_FTP_SUPPORT)
584 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
585 tracked_objects::ScopedTracker
tracking_profile8(
586 FROM_HERE_WITH_EXPLICIT_FUNCTION(
587 "436671 ProfileImplIOData::InitializeInternal8"));
589 scoped_ptr
<net::URLRequestJobFactoryImpl
> main_job_factory(
590 new net::URLRequestJobFactoryImpl());
591 InstallProtocolHandlers(main_job_factory
.get(), protocol_handlers
);
593 // The data reduction proxy interceptor should be as close to the network
595 request_interceptors
.insert(
596 request_interceptors
.begin(),
597 data_reduction_proxy_io_data()->CreateInterceptor().release());
598 main_job_factory_
= SetUpJobFactoryDefaults(
599 main_job_factory
.Pass(),
600 request_interceptors
.Pass(),
601 profile_params
->protocol_handler_interceptor
.Pass(),
602 main_context
->network_delegate(),
604 main_context
->set_job_factory(main_job_factory_
.get());
606 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
607 tracked_objects::ScopedTracker
tracking_profile9(
608 FROM_HERE_WITH_EXPLICIT_FUNCTION(
609 "436671 ProfileImplIOData::InitializeInternal9"));
611 #if defined(ENABLE_EXTENSIONS)
612 InitializeExtensionsRequestContext(profile_params
);
615 // Setup SDCH for this profile.
616 sdch_manager_
.reset(new net::SdchManager
);
617 sdch_policy_
.reset(new net::SdchOwner(sdch_manager_
.get(), main_context
));
618 main_context
->set_sdch_manager(sdch_manager_
.get());
619 if (ShouldUseSdchPersistence()) {
620 sdch_policy_
->EnablePersistentStorage(network_json_store_
.get());
623 // Create a media request context based on the main context, but using a
624 // media cache. It shares the same job factory as the main context.
625 StoragePartitionDescriptor
details(profile_path_
, false);
626 media_request_context_
.reset(InitializeMediaRequestContext(main_context
,
629 lazy_params_
.reset();
632 void ProfileImplIOData::
633 InitializeExtensionsRequestContext(ProfileParams
* profile_params
) const {
634 net::URLRequestContext
* extensions_context
= extensions_request_context();
635 IOThread
* const io_thread
= profile_params
->io_thread
;
636 IOThread::Globals
* const io_thread_globals
= io_thread
->globals();
637 ApplyProfileParamsToContext(extensions_context
);
639 extensions_context
->set_transport_security_state(transport_security_state());
641 extensions_context
->set_net_log(io_thread
->net_log());
643 extensions_context
->set_throttler_manager(
644 io_thread_globals
->throttler_manager
.get());
646 content::CookieStoreConfig
cookie_config(
647 lazy_params_
->extensions_cookie_path
,
648 lazy_params_
->session_cookie_mode
,
650 cookie_config
.crypto_delegate
=
651 chrome_browser_net::GetCookieCryptoDelegate();
652 net::CookieStore
* extensions_cookie_store
=
653 content::CreateCookieStore(cookie_config
);
654 // Enable cookies for devtools and extension URLs.
655 const char* const schemes
[] = {
656 content::kChromeDevToolsScheme
,
657 extensions::kExtensionScheme
659 extensions_cookie_store
->GetCookieMonster()->SetCookieableSchemes(
660 schemes
, arraysize(schemes
));
661 extensions_context
->set_cookie_store(extensions_cookie_store
);
663 scoped_ptr
<net::URLRequestJobFactoryImpl
> extensions_job_factory(
664 new net::URLRequestJobFactoryImpl());
665 // TODO(shalev): The extensions_job_factory has a NULL NetworkDelegate.
666 // Without a network_delegate, this protocol handler will never
667 // handle file: requests, but as a side effect it makes
668 // job_factory::IsHandledProtocol return true, which prevents attempts to
669 // handle the protocol externally. We pass NULL in to
670 // SetUpJobFactory() to get this effect.
671 extensions_job_factory_
= SetUpJobFactoryDefaults(
672 extensions_job_factory
.Pass(),
673 content::URLRequestInterceptorScopedVector(),
674 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>(),
677 extensions_context
->set_job_factory(extensions_job_factory_
.get());
680 net::URLRequestContext
* ProfileImplIOData::InitializeAppRequestContext(
681 net::URLRequestContext
* main_context
,
682 const StoragePartitionDescriptor
& partition_descriptor
,
683 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
684 protocol_handler_interceptor
,
685 content::ProtocolHandlerMap
* protocol_handlers
,
686 content::URLRequestInterceptorScopedVector request_interceptors
) const {
687 // Copy most state from the main context.
688 AppRequestContext
* context
= new AppRequestContext();
689 context
->CopyFrom(main_context
);
691 base::FilePath cookie_path
= partition_descriptor
.path
.Append(
692 chrome::kCookieFilename
);
693 base::FilePath cache_path
=
694 partition_descriptor
.path
.Append(chrome::kCacheDirname
);
696 // Use a separate HTTP disk cache for isolated apps.
697 net::HttpCache::BackendFactory
* app_backend
= NULL
;
698 if (partition_descriptor
.in_memory
) {
699 app_backend
= net::HttpCache::DefaultBackend::InMemory(0);
701 app_backend
= new net::HttpCache::DefaultBackend(
703 ChooseCacheBackendType(),
706 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE
));
708 net::HttpNetworkSession
* main_network_session
=
709 main_http_factory_
->GetSession();
710 scoped_ptr
<net::HttpCache
> app_http_cache
=
711 CreateHttpFactory(main_network_session
, app_backend
);
713 scoped_refptr
<net::CookieStore
> cookie_store
= NULL
;
714 if (partition_descriptor
.in_memory
) {
715 cookie_store
= content::CreateCookieStore(content::CookieStoreConfig());
718 // Use an app-specific cookie store.
719 if (!cookie_store
.get()) {
720 DCHECK(!cookie_path
.empty());
722 // TODO(creis): We should have a cookie delegate for notifying the cookie
723 // extensions API, but we need to update it to understand isolated apps
725 content::CookieStoreConfig
cookie_config(
727 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES
,
729 cookie_config
.crypto_delegate
=
730 chrome_browser_net::GetCookieCryptoDelegate();
731 cookie_store
= content::CreateCookieStore(cookie_config
);
734 // Transfer ownership of the cookies and cache to AppRequestContext.
735 context
->SetCookieStore(cookie_store
.get());
736 context
->SetHttpTransactionFactory(app_http_cache
.Pass());
738 scoped_ptr
<net::URLRequestJobFactoryImpl
> job_factory(
739 new net::URLRequestJobFactoryImpl());
740 InstallProtocolHandlers(job_factory
.get(), protocol_handlers
);
741 // The data reduction proxy interceptor should be as close to the network
743 request_interceptors
.insert(
744 request_interceptors
.begin(),
745 data_reduction_proxy_io_data()->CreateInterceptor().release());
746 scoped_ptr
<net::URLRequestJobFactory
> top_job_factory(
747 SetUpJobFactoryDefaults(job_factory
.Pass(),
748 request_interceptors
.Pass(),
749 protocol_handler_interceptor
.Pass(),
750 main_context
->network_delegate(),
751 ftp_factory_
.get()));
752 context
->SetJobFactory(top_job_factory
.Pass());
757 net::URLRequestContext
*
758 ProfileImplIOData::InitializeMediaRequestContext(
759 net::URLRequestContext
* original_context
,
760 const StoragePartitionDescriptor
& partition_descriptor
) const {
761 // Copy most state from the original context.
762 MediaRequestContext
* context
= new MediaRequestContext();
763 context
->CopyFrom(original_context
);
765 // For in-memory context, return immediately after creating the new
766 // context before attaching a separate cache. It is important to return
767 // a new context rather than just reusing |original_context| because
768 // the caller expects to take ownership of the pointer.
769 if (partition_descriptor
.in_memory
)
772 using content::StoragePartition
;
773 base::FilePath cache_path
;
774 int cache_max_size
= app_media_cache_max_size_
;
775 if (partition_descriptor
.path
== profile_path_
) {
776 // lazy_params_ is only valid for the default media context creation.
777 cache_path
= lazy_params_
->media_cache_path
;
778 cache_max_size
= lazy_params_
->media_cache_max_size
;
780 cache_path
= partition_descriptor
.path
.Append(chrome::kMediaCacheDirname
);
783 // Use a separate HTTP disk cache for isolated apps.
784 net::HttpCache::BackendFactory
* media_backend
=
785 new net::HttpCache::DefaultBackend(
787 ChooseCacheBackendType(),
790 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE
));
791 net::HttpNetworkSession
* main_network_session
=
792 main_http_factory_
->GetSession();
793 scoped_ptr
<net::HttpCache
> media_http_cache
=
794 CreateHttpFactory(main_network_session
, media_backend
);
796 // Transfer ownership of the cache to MediaRequestContext.
797 context
->SetHttpTransactionFactory(media_http_cache
.Pass());
799 // Note that we do not create a new URLRequestJobFactory because
800 // the media context should behave exactly like its parent context
801 // in all respects except for cache behavior on media subresources.
802 // The CopyFrom() step above means that our media context will use
803 // the same URLRequestJobFactory instance that our parent context does.
808 net::URLRequestContext
*
809 ProfileImplIOData::AcquireMediaRequestContext() const {
810 DCHECK(media_request_context_
);
811 return media_request_context_
.get();
814 net::URLRequestContext
* ProfileImplIOData::AcquireIsolatedAppRequestContext(
815 net::URLRequestContext
* main_context
,
816 const StoragePartitionDescriptor
& partition_descriptor
,
817 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
818 protocol_handler_interceptor
,
819 content::ProtocolHandlerMap
* protocol_handlers
,
820 content::URLRequestInterceptorScopedVector request_interceptors
) const {
821 // We create per-app contexts on demand, unlike the others above.
822 net::URLRequestContext
* app_request_context
=
823 InitializeAppRequestContext(main_context
,
824 partition_descriptor
,
825 protocol_handler_interceptor
.Pass(),
827 request_interceptors
.Pass());
828 DCHECK(app_request_context
);
829 return app_request_context
;
832 net::URLRequestContext
*
833 ProfileImplIOData::AcquireIsolatedMediaRequestContext(
834 net::URLRequestContext
* app_context
,
835 const StoragePartitionDescriptor
& partition_descriptor
) const {
836 // We create per-app media contexts on demand, unlike the others above.
837 net::URLRequestContext
* media_request_context
=
838 InitializeMediaRequestContext(app_context
, partition_descriptor
);
839 DCHECK(media_request_context
);
840 return media_request_context
;
843 void ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread(
845 const base::Closure
& completion
) {
846 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
847 DCHECK(initialized());
849 DCHECK(transport_security_state());
850 // Completes synchronously.
851 transport_security_state()->DeleteAllDynamicDataSince(time
);
852 DCHECK(http_server_properties_manager_
);
853 http_server_properties_manager_
->Clear(completion
);