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"
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/metrics/field_trial.h"
12 #include "base/prefs/pref_member.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/profiler/scoped_tracker.h"
15 #include "base/sequenced_task_runner.h"
16 #include "base/stl_util.h"
17 #include "base/strings/string_util.h"
18 #include "base/threading/sequenced_worker_pool.h"
19 #include "base/threading/worker_pool.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/chrome_notification_types.h"
22 #include "chrome/browser/chromeos/profiles/profile_helper.h"
23 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
24 #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
25 #include "chrome/browser/io_thread.h"
26 #include "chrome/browser/net/chrome_net_log.h"
27 #include "chrome/browser/net/chrome_network_delegate.h"
28 #include "chrome/browser/net/connect_interceptor.h"
29 #include "chrome/browser/net/cookie_store_util.h"
30 #include "chrome/browser/net/http_server_properties_manager_factory.h"
31 #include "chrome/browser/net/predictor.h"
32 #include "chrome/browser/net/quota_policy_channel_id_store.h"
33 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_io_data.h"
34 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
35 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
36 #include "chrome/browser/profiles/profile.h"
37 #include "chrome/common/chrome_constants.h"
38 #include "chrome/common/chrome_switches.h"
39 #include "chrome/common/pref_names.h"
40 #include "chrome/common/url_constants.h"
41 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h"
42 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
43 #include "components/domain_reliability/monitor.h"
44 #include "content/public/browser/browser_thread.h"
45 #include "content/public/browser/cookie_store_factory.h"
46 #include "content/public/browser/notification_service.h"
47 #include "content/public/browser/resource_context.h"
48 #include "content/public/browser/storage_partition.h"
49 #include "extensions/browser/extension_protocols.h"
50 #include "extensions/common/constants.h"
51 #include "net/base/cache_type.h"
52 #include "net/base/sdch_manager.h"
53 #include "net/ftp/ftp_network_layer.h"
54 #include "net/http/http_cache.h"
55 #include "net/http/http_server_properties_manager.h"
56 #include "net/sdch/sdch_owner.h"
57 #include "net/ssl/channel_id_service.h"
58 #include "net/url_request/url_request_intercepting_job_factory.h"
59 #include "net/url_request/url_request_job_factory_impl.h"
60 #include "storage/browser/quota/special_storage_policy.h"
64 net::BackendType
ChooseCacheBackendType() {
65 #if defined(OS_ANDROID)
66 return net::CACHE_BACKEND_SIMPLE
;
68 const base::CommandLine
& command_line
=
69 *base::CommandLine::ForCurrentProcess();
70 if (command_line
.HasSwitch(switches::kUseSimpleCacheBackend
)) {
71 const std::string opt_value
=
72 command_line
.GetSwitchValueASCII(switches::kUseSimpleCacheBackend
);
73 if (LowerCaseEqualsASCII(opt_value
, "off"))
74 return net::CACHE_BACKEND_BLOCKFILE
;
75 if (opt_value
.empty() || LowerCaseEqualsASCII(opt_value
, "on"))
76 return net::CACHE_BACKEND_SIMPLE
;
78 const std::string experiment_name
=
79 base::FieldTrialList::FindFullName("SimpleCacheTrial");
80 if (experiment_name
== "ExperimentYes" ||
81 experiment_name
== "ExperimentYes2") {
82 return net::CACHE_BACKEND_SIMPLE
;
84 return net::CACHE_BACKEND_BLOCKFILE
;
90 using content::BrowserThread
;
92 ProfileImplIOData::Handle::Handle(Profile
* profile
)
93 : io_data_(new ProfileImplIOData
),
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
100 ProfileImplIOData::Handle::~Handle() {
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
102 if (io_data_
->predictor_
!= NULL
) {
103 // io_data_->predictor_ might be NULL if Init() was never called
104 // (i.e. we shut down before ProfileImpl::DoFinalInit() got called).
105 bool save_prefs
= true;
106 #if defined(OS_CHROMEOS)
107 save_prefs
= !chromeos::ProfileHelper::IsSigninProfile(profile_
);
110 io_data_
->predictor_
->SaveStateForNextStartupAndTrim();
111 io_data_
->predictor_
->ShutdownOnUIThread();
114 if (io_data_
->http_server_properties_manager_
)
115 io_data_
->http_server_properties_manager_
->ShutdownOnPrefThread();
117 io_data_
->data_reduction_proxy_io_data()->ShutdownOnUIThread();
118 io_data_
->ShutdownOnUIThread(GetAllContextGetters().Pass());
121 void ProfileImplIOData::Handle::Init(
122 const base::FilePath
& cookie_path
,
123 const base::FilePath
& channel_id_path
,
124 const base::FilePath
& cache_path
,
126 const base::FilePath
& media_cache_path
,
127 int media_cache_max_size
,
128 const base::FilePath
& extensions_cookie_path
,
129 const base::FilePath
& profile_path
,
130 const base::FilePath
& infinite_cache_path
,
131 chrome_browser_net::Predictor
* predictor
,
132 content::CookieStoreConfig::SessionCookieMode session_cookie_mode
,
133 storage::SpecialStoragePolicy
* special_storage_policy
,
134 scoped_ptr
<domain_reliability::DomainReliabilityMonitor
>
135 domain_reliability_monitor
) {
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
137 DCHECK(!io_data_
->lazy_params_
);
140 LazyParams
* lazy_params
= new LazyParams();
142 lazy_params
->cookie_path
= cookie_path
;
143 lazy_params
->channel_id_path
= channel_id_path
;
144 lazy_params
->cache_path
= cache_path
;
145 lazy_params
->cache_max_size
= cache_max_size
;
146 lazy_params
->media_cache_path
= media_cache_path
;
147 lazy_params
->media_cache_max_size
= media_cache_max_size
;
148 lazy_params
->extensions_cookie_path
= extensions_cookie_path
;
149 lazy_params
->infinite_cache_path
= infinite_cache_path
;
150 lazy_params
->session_cookie_mode
= session_cookie_mode
;
151 lazy_params
->special_storage_policy
= special_storage_policy
;
153 io_data_
->lazy_params_
.reset(lazy_params
);
155 // Keep track of profile path and cache sizes separately so we can use them
156 // on demand when creating storage isolated URLRequestContextGetters.
157 io_data_
->profile_path_
= profile_path
;
158 io_data_
->app_cache_max_size_
= cache_max_size
;
159 io_data_
->app_media_cache_max_size_
= media_cache_max_size
;
161 io_data_
->predictor_
.reset(predictor
);
162 io_data_
->domain_reliability_monitor_
= domain_reliability_monitor
.Pass();
164 io_data_
->InitializeMetricsEnabledStateOnUIThread();
165 if (io_data_
->domain_reliability_monitor_
)
166 io_data_
->domain_reliability_monitor_
->MoveToNetworkThread();
168 // TODO(tbansal): Move this to IO thread once the data reduction proxy
169 // params are unified into a single object.
170 bool enable_quic_for_data_reduction_proxy
=
171 IOThread::ShouldEnableQuicForDataReductionProxy();
173 io_data_
->set_data_reduction_proxy_io_data(
174 CreateDataReductionProxyChromeIOData(
175 g_browser_process
->io_thread()->net_log(), profile_
->GetPrefs(),
176 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
),
177 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
178 enable_quic_for_data_reduction_proxy
)
181 DataReductionProxyChromeSettingsFactory::GetForBrowserContext(profile_
)->
182 InitDataReductionProxySettings(
183 io_data_
->data_reduction_proxy_io_data(), profile_
->GetPrefs(),
184 profile_
->GetRequestContext(),
185 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
));
188 content::ResourceContext
*
189 ProfileImplIOData::Handle::GetResourceContext() const {
190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
192 return GetResourceContextNoInit();
195 content::ResourceContext
*
196 ProfileImplIOData::Handle::GetResourceContextNoInit() const {
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
198 // Don't call LazyInitialize here, since the resource context is created at
199 // the beginning of initalization and is used by some members while they're
200 // being initialized (i.e. AppCacheService).
201 return io_data_
->GetResourceContext();
204 scoped_refptr
<ChromeURLRequestContextGetter
>
205 ProfileImplIOData::Handle::CreateMainRequestContextGetter(
206 content::ProtocolHandlerMap
* protocol_handlers
,
207 content::URLRequestInterceptorScopedVector request_interceptors
,
208 PrefService
* local_state
,
209 IOThread
* io_thread
) const {
210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
212 DCHECK(!main_request_context_getter_
.get());
213 main_request_context_getter_
= ChromeURLRequestContextGetter::Create(
214 profile_
, io_data_
, protocol_handlers
, request_interceptors
.Pass());
217 ->InitNetworkPredictor(profile_
->GetPrefs(),
220 main_request_context_getter_
.get(),
223 content::NotificationService::current()->Notify(
224 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED
,
225 content::Source
<Profile
>(profile_
),
226 content::NotificationService::NoDetails());
227 return main_request_context_getter_
;
230 scoped_refptr
<ChromeURLRequestContextGetter
>
231 ProfileImplIOData::Handle::GetMediaRequestContextGetter() const {
232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
234 if (!media_request_context_getter_
.get()) {
235 media_request_context_getter_
=
236 ChromeURLRequestContextGetter::CreateForMedia(profile_
, io_data_
);
238 return media_request_context_getter_
;
241 scoped_refptr
<ChromeURLRequestContextGetter
>
242 ProfileImplIOData::Handle::GetExtensionsRequestContextGetter() const {
243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
245 if (!extensions_request_context_getter_
.get()) {
246 extensions_request_context_getter_
=
247 ChromeURLRequestContextGetter::CreateForExtensions(profile_
, io_data_
);
249 return extensions_request_context_getter_
;
252 scoped_refptr
<ChromeURLRequestContextGetter
>
253 ProfileImplIOData::Handle::CreateIsolatedAppRequestContextGetter(
254 const base::FilePath
& partition_path
,
256 content::ProtocolHandlerMap
* protocol_handlers
,
257 content::URLRequestInterceptorScopedVector request_interceptors
) const {
258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
259 // Check that the partition_path is not the same as the base profile path. We
260 // expect isolated partition, which will never go to the default profile path.
261 CHECK(partition_path
!= profile_
->GetPath());
264 // Keep a map of request context getters, one per requested storage partition.
265 StoragePartitionDescriptor
descriptor(partition_path
, in_memory
);
266 ChromeURLRequestContextGetterMap::iterator iter
=
267 app_request_context_getter_map_
.find(descriptor
);
268 if (iter
!= app_request_context_getter_map_
.end())
271 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
272 protocol_handler_interceptor(
273 ProtocolHandlerRegistryFactory::GetForBrowserContext(profile_
)->
274 CreateJobInterceptorFactory());
275 ChromeURLRequestContextGetter
* context
=
276 ChromeURLRequestContextGetter::CreateForIsolatedApp(
280 protocol_handler_interceptor
.Pass(),
282 request_interceptors
.Pass());
283 app_request_context_getter_map_
[descriptor
] = context
;
288 scoped_refptr
<ChromeURLRequestContextGetter
>
289 ProfileImplIOData::Handle::GetIsolatedMediaRequestContextGetter(
290 const base::FilePath
& partition_path
,
291 bool in_memory
) const {
292 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
293 // We must have a non-default path, or this will act like the default media
295 CHECK(partition_path
!= profile_
->GetPath());
298 // Keep a map of request context getters, one per requested storage partition.
299 StoragePartitionDescriptor
descriptor(partition_path
, in_memory
);
300 ChromeURLRequestContextGetterMap::iterator iter
=
301 isolated_media_request_context_getter_map_
.find(descriptor
);
302 if (iter
!= isolated_media_request_context_getter_map_
.end())
305 // Get the app context as the starting point for the media context, so that
306 // it uses the app's cookie store.
307 ChromeURLRequestContextGetterMap::const_iterator app_iter
=
308 app_request_context_getter_map_
.find(descriptor
);
309 DCHECK(app_iter
!= app_request_context_getter_map_
.end());
310 ChromeURLRequestContextGetter
* app_context
= app_iter
->second
.get();
311 ChromeURLRequestContextGetter
* context
=
312 ChromeURLRequestContextGetter::CreateForIsolatedMedia(
313 profile_
, app_context
, io_data_
, descriptor
);
314 isolated_media_request_context_getter_map_
[descriptor
] = context
;
319 DevToolsNetworkController
*
320 ProfileImplIOData::Handle::GetDevToolsNetworkController() const {
321 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
322 return io_data_
->network_controller();
325 void ProfileImplIOData::Handle::ClearNetworkingHistorySince(
327 const base::Closure
& completion
) {
328 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
331 BrowserThread::PostTask(
332 BrowserThread::IO
, FROM_HERE
,
334 &ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread
,
335 base::Unretained(io_data_
),
340 void ProfileImplIOData::Handle::LazyInitialize() const {
341 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
345 // Set initialized_ to true at the beginning in case any of the objects
346 // below try to get the ResourceContext pointer.
348 PrefService
* pref_service
= profile_
->GetPrefs();
349 io_data_
->http_server_properties_manager_
=
350 chrome_browser_net::HttpServerPropertiesManagerFactory::CreateManager(
352 io_data_
->set_http_server_properties(
353 scoped_ptr
<net::HttpServerProperties
>(
354 io_data_
->http_server_properties_manager_
));
355 io_data_
->session_startup_pref()->Init(
356 prefs::kRestoreOnStartup
, pref_service
);
357 io_data_
->session_startup_pref()->MoveToThread(
358 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
));
359 #if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
360 io_data_
->safe_browsing_enabled()->Init(prefs::kSafeBrowsingEnabled
,
362 io_data_
->safe_browsing_enabled()->MoveToThread(
363 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
));
365 io_data_
->InitializeOnUIThread(profile_
);
368 scoped_ptr
<ProfileIOData::ChromeURLRequestContextGetterVector
>
369 ProfileImplIOData::Handle::GetAllContextGetters() {
370 ChromeURLRequestContextGetterMap::iterator iter
;
371 scoped_ptr
<ChromeURLRequestContextGetterVector
> context_getters(
372 new ChromeURLRequestContextGetterVector());
374 iter
= isolated_media_request_context_getter_map_
.begin();
375 for (; iter
!= isolated_media_request_context_getter_map_
.end(); ++iter
)
376 context_getters
->push_back(iter
->second
);
378 iter
= app_request_context_getter_map_
.begin();
379 for (; iter
!= app_request_context_getter_map_
.end(); ++iter
)
380 context_getters
->push_back(iter
->second
);
382 if (extensions_request_context_getter_
.get())
383 context_getters
->push_back(extensions_request_context_getter_
);
385 if (media_request_context_getter_
.get())
386 context_getters
->push_back(media_request_context_getter_
);
388 if (main_request_context_getter_
.get())
389 context_getters
->push_back(main_request_context_getter_
);
391 return context_getters
.Pass();
394 ProfileImplIOData::LazyParams::LazyParams()
396 media_cache_max_size(0),
398 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES
) {}
400 ProfileImplIOData::LazyParams::~LazyParams() {}
402 ProfileImplIOData::ProfileImplIOData()
403 : ProfileIOData(Profile::REGULAR_PROFILE
),
404 http_server_properties_manager_(NULL
),
405 app_cache_max_size_(0),
406 app_media_cache_max_size_(0) {
409 ProfileImplIOData::~ProfileImplIOData() {
410 DestroyResourceContext();
412 if (media_request_context_
)
413 media_request_context_
->AssertNoURLRequests();
416 void ProfileImplIOData::InitializeInternal(
417 scoped_ptr
<ChromeNetworkDelegate
> chrome_network_delegate
,
418 ProfileParams
* profile_params
,
419 content::ProtocolHandlerMap
* protocol_handlers
,
420 content::URLRequestInterceptorScopedVector request_interceptors
) const {
421 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
422 tracked_objects::ScopedTracker
tracking_profile(
423 FROM_HERE_WITH_EXPLICIT_FUNCTION(
424 "436671 ProfileImplIOData::InitializeInternal"));
426 net::URLRequestContext
* main_context
= main_request_context();
428 IOThread
* const io_thread
= profile_params
->io_thread
;
429 IOThread::Globals
* const io_thread_globals
= io_thread
->globals();
431 chrome_network_delegate
->set_predictor(predictor_
.get());
433 if (domain_reliability_monitor_
) {
434 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
435 tracked_objects::ScopedTracker
tracking_profile1(
436 FROM_HERE_WITH_EXPLICIT_FUNCTION(
437 "436671 ProfileImplIOData::InitializeInternal1"));
439 domain_reliability::DomainReliabilityMonitor
* monitor
=
440 domain_reliability_monitor_
.get();
441 monitor
->InitURLRequestContext(main_context
);
442 monitor
->AddBakedInConfigs();
443 monitor
->SetDiscardUploads(!GetMetricsEnabledStateOnIOThread());
444 chrome_network_delegate
->set_domain_reliability_monitor(monitor
);
447 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
448 tracked_objects::ScopedTracker
tracking_profile2(
449 FROM_HERE_WITH_EXPLICIT_FUNCTION(
450 "436671 ProfileImplIOData::InitializeInternal2"));
452 ApplyProfileParamsToContext(main_context
);
454 if (http_server_properties_manager_
)
455 http_server_properties_manager_
->InitializeOnNetworkThread();
457 main_context
->set_transport_security_state(transport_security_state());
459 main_context
->set_net_log(io_thread
->net_log());
461 network_delegate_
= data_reduction_proxy_io_data()->CreateNetworkDelegate(
462 chrome_network_delegate
.Pass(), true).Pass();
464 main_context
->set_network_delegate(network_delegate_
.get());
466 main_context
->set_http_server_properties(http_server_properties());
468 main_context
->set_host_resolver(
469 io_thread_globals
->host_resolver
.get());
470 main_context
->set_cert_transparency_verifier(
471 io_thread_globals
->cert_transparency_verifier
.get());
472 main_context
->set_http_auth_handler_factory(
473 io_thread_globals
->http_auth_handler_factory
.get());
475 main_context
->set_fraudulent_certificate_reporter(
476 fraudulent_certificate_reporter());
478 main_context
->set_throttler_manager(
479 io_thread_globals
->throttler_manager
.get());
481 main_context
->set_proxy_service(proxy_service());
483 scoped_refptr
<net::CookieStore
> cookie_store
= NULL
;
484 net::ChannelIDService
* channel_id_service
= NULL
;
485 if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
486 // Don't use existing cookies and use an in-memory store.
487 using content::CookieStoreConfig
;
488 cookie_store
= content::CreateCookieStore(CookieStoreConfig(
490 CookieStoreConfig::EPHEMERAL_SESSION_COOKIES
,
492 profile_params
->cookie_monster_delegate
.get()));
493 // Don't use existing channel ids and use an in-memory store.
494 channel_id_service
= new net::ChannelIDService(
495 new net::DefaultChannelIDStore(NULL
),
496 base::WorkerPool::GetTaskRunner(true));
499 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
500 tracked_objects::ScopedTracker
tracking_profile5(
501 FROM_HERE_WITH_EXPLICIT_FUNCTION(
502 "436671 ProfileImplIOData::InitializeInternal5"));
504 // setup cookie store
505 if (!cookie_store
.get()) {
506 DCHECK(!lazy_params_
->cookie_path
.empty());
508 content::CookieStoreConfig
cookie_config(
509 lazy_params_
->cookie_path
,
510 lazy_params_
->session_cookie_mode
,
511 lazy_params_
->special_storage_policy
.get(),
512 profile_params
->cookie_monster_delegate
.get());
513 cookie_config
.crypto_delegate
=
514 chrome_browser_net::GetCookieCryptoDelegate();
515 cookie_store
= content::CreateCookieStore(cookie_config
);
518 main_context
->set_cookie_store(cookie_store
.get());
520 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
521 tracked_objects::ScopedTracker
tracking_profile6(
522 FROM_HERE_WITH_EXPLICIT_FUNCTION(
523 "436671 ProfileImplIOData::InitializeInternal6"));
525 // Setup 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 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
544 tracked_objects::ScopedTracker
tracking_profile7(
545 FROM_HERE_WITH_EXPLICIT_FUNCTION(
546 "436671 ProfileImplIOData::InitializeInternal7"));
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 scoped_ptr
<net::HttpCache
> main_cache
= CreateMainHttpFactory(
556 profile_params
, main_backend
);
558 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
559 tracked_objects::ScopedTracker
tracking_profile71(
560 FROM_HERE_WITH_EXPLICIT_FUNCTION(
561 "436671 ProfileImplIOData::InitializeInternal71"));
563 main_cache
->InitializeInfiniteCache(lazy_params_
->infinite_cache_path
);
565 if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
566 main_cache
->set_mode(
567 chrome_browser_net::IsCookieRecordMode() ?
568 net::HttpCache::RECORD
: net::HttpCache::PLAYBACK
);
571 main_http_factory_
.reset(main_cache
.release());
572 main_context
->set_http_transaction_factory(main_http_factory_
.get());
574 #if !defined(DISABLE_FTP_SUPPORT)
576 new net::FtpNetworkLayer(io_thread_globals
->host_resolver
.get()));
577 #endif // !defined(DISABLE_FTP_SUPPORT)
579 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
580 tracked_objects::ScopedTracker
tracking_profile8(
581 FROM_HERE_WITH_EXPLICIT_FUNCTION(
582 "436671 ProfileImplIOData::InitializeInternal8"));
584 scoped_ptr
<net::URLRequestJobFactoryImpl
> main_job_factory(
585 new net::URLRequestJobFactoryImpl());
586 InstallProtocolHandlers(main_job_factory
.get(), protocol_handlers
);
588 // The data reduction proxy interceptor should be as close to the network
590 request_interceptors
.insert(
591 request_interceptors
.begin(),
592 data_reduction_proxy_io_data()->CreateInterceptor().release());
593 main_job_factory_
= SetUpJobFactoryDefaults(
594 main_job_factory
.Pass(),
595 request_interceptors
.Pass(),
596 profile_params
->protocol_handler_interceptor
.Pass(),
597 main_context
->network_delegate(),
599 main_context
->set_job_factory(main_job_factory_
.get());
601 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
602 tracked_objects::ScopedTracker
tracking_profile9(
603 FROM_HERE_WITH_EXPLICIT_FUNCTION(
604 "436671 ProfileImplIOData::InitializeInternal9"));
606 #if defined(ENABLE_EXTENSIONS)
607 InitializeExtensionsRequestContext(profile_params
);
610 // Setup SDCH for this profile.
611 sdch_manager_
.reset(new net::SdchManager
);
612 sdch_policy_
.reset(new net::SdchOwner(sdch_manager_
.get(), main_context
));
613 main_context
->set_sdch_manager(sdch_manager_
.get());
615 // Create a media request context based on the main context, but using a
616 // media cache. It shares the same job factory as the main context.
617 StoragePartitionDescriptor
details(profile_path_
, false);
618 media_request_context_
.reset(InitializeMediaRequestContext(main_context
,
621 lazy_params_
.reset();
624 void ProfileImplIOData::
625 InitializeExtensionsRequestContext(ProfileParams
* profile_params
) const {
626 net::URLRequestContext
* extensions_context
= extensions_request_context();
627 IOThread
* const io_thread
= profile_params
->io_thread
;
628 IOThread::Globals
* const io_thread_globals
= io_thread
->globals();
629 ApplyProfileParamsToContext(extensions_context
);
631 extensions_context
->set_transport_security_state(transport_security_state());
633 extensions_context
->set_net_log(io_thread
->net_log());
635 extensions_context
->set_throttler_manager(
636 io_thread_globals
->throttler_manager
.get());
638 content::CookieStoreConfig
cookie_config(
639 lazy_params_
->extensions_cookie_path
,
640 lazy_params_
->session_cookie_mode
,
642 cookie_config
.crypto_delegate
=
643 chrome_browser_net::GetCookieCryptoDelegate();
644 net::CookieStore
* extensions_cookie_store
=
645 content::CreateCookieStore(cookie_config
);
646 // Enable cookies for devtools and extension URLs.
647 const char* const schemes
[] = {
648 content::kChromeDevToolsScheme
,
649 extensions::kExtensionScheme
651 extensions_cookie_store
->GetCookieMonster()->SetCookieableSchemes(
652 schemes
, arraysize(schemes
));
653 extensions_context
->set_cookie_store(extensions_cookie_store
);
655 scoped_ptr
<net::URLRequestJobFactoryImpl
> extensions_job_factory(
656 new net::URLRequestJobFactoryImpl());
657 // TODO(shalev): The extensions_job_factory has a NULL NetworkDelegate.
658 // Without a network_delegate, this protocol handler will never
659 // handle file: requests, but as a side effect it makes
660 // job_factory::IsHandledProtocol return true, which prevents attempts to
661 // handle the protocol externally. We pass NULL in to
662 // SetUpJobFactory() to get this effect.
663 extensions_job_factory_
= SetUpJobFactoryDefaults(
664 extensions_job_factory
.Pass(),
665 content::URLRequestInterceptorScopedVector(),
666 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>(),
669 extensions_context
->set_job_factory(extensions_job_factory_
.get());
672 net::URLRequestContext
* ProfileImplIOData::InitializeAppRequestContext(
673 net::URLRequestContext
* main_context
,
674 const StoragePartitionDescriptor
& partition_descriptor
,
675 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
676 protocol_handler_interceptor
,
677 content::ProtocolHandlerMap
* protocol_handlers
,
678 content::URLRequestInterceptorScopedVector request_interceptors
) const {
679 // Copy most state from the main context.
680 AppRequestContext
* context
= new AppRequestContext();
681 context
->CopyFrom(main_context
);
683 base::FilePath cookie_path
= partition_descriptor
.path
.Append(
684 chrome::kCookieFilename
);
685 base::FilePath cache_path
=
686 partition_descriptor
.path
.Append(chrome::kCacheDirname
);
688 // Use a separate HTTP disk cache for isolated apps.
689 net::HttpCache::BackendFactory
* app_backend
= NULL
;
690 if (partition_descriptor
.in_memory
) {
691 app_backend
= net::HttpCache::DefaultBackend::InMemory(0);
693 app_backend
= new net::HttpCache::DefaultBackend(
695 ChooseCacheBackendType(),
698 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE
));
700 net::HttpNetworkSession
* main_network_session
=
701 main_http_factory_
->GetSession();
702 scoped_ptr
<net::HttpCache
> app_http_cache
=
703 CreateHttpFactory(main_network_session
, app_backend
);
705 scoped_refptr
<net::CookieStore
> cookie_store
= NULL
;
706 if (partition_descriptor
.in_memory
) {
707 cookie_store
= content::CreateCookieStore(content::CookieStoreConfig());
708 } else if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
709 // Don't use existing cookies and use an in-memory store.
710 // TODO(creis): We should have a cookie delegate for notifying the cookie
711 // extensions API, but we need to update it to understand isolated apps
713 cookie_store
= content::CreateCookieStore(content::CookieStoreConfig());
714 app_http_cache
->set_mode(
715 chrome_browser_net::IsCookieRecordMode() ?
716 net::HttpCache::RECORD
: net::HttpCache::PLAYBACK
);
719 // Use an app-specific cookie store.
720 if (!cookie_store
.get()) {
721 DCHECK(!cookie_path
.empty());
723 // TODO(creis): We should have a cookie delegate for notifying the cookie
724 // extensions API, but we need to update it to understand isolated apps
726 content::CookieStoreConfig
cookie_config(
728 content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES
,
730 cookie_config
.crypto_delegate
=
731 chrome_browser_net::GetCookieCryptoDelegate();
732 cookie_store
= content::CreateCookieStore(cookie_config
);
735 // Transfer ownership of the cookies and cache to AppRequestContext.
736 context
->SetCookieStore(cookie_store
.get());
737 context
->SetHttpTransactionFactory(app_http_cache
.Pass());
739 scoped_ptr
<net::URLRequestJobFactoryImpl
> job_factory(
740 new net::URLRequestJobFactoryImpl());
741 InstallProtocolHandlers(job_factory
.get(), protocol_handlers
);
742 // The data reduction proxy interceptor should be as close to the network
744 request_interceptors
.insert(
745 request_interceptors
.begin(),
746 data_reduction_proxy_io_data()->CreateInterceptor().release());
747 scoped_ptr
<net::URLRequestJobFactory
> top_job_factory(
748 SetUpJobFactoryDefaults(job_factory
.Pass(),
749 request_interceptors
.Pass(),
750 protocol_handler_interceptor
.Pass(),
751 main_context
->network_delegate(),
752 ftp_factory_
.get()));
753 context
->SetJobFactory(top_job_factory
.Pass());
758 net::URLRequestContext
*
759 ProfileImplIOData::InitializeMediaRequestContext(
760 net::URLRequestContext
* original_context
,
761 const StoragePartitionDescriptor
& partition_descriptor
) const {
762 // Copy most state from the original context.
763 MediaRequestContext
* context
= new MediaRequestContext();
764 context
->CopyFrom(original_context
);
766 // For in-memory context, return immediately after creating the new
767 // context before attaching a separate cache. It is important to return
768 // a new context rather than just reusing |original_context| because
769 // the caller expects to take ownership of the pointer.
770 if (partition_descriptor
.in_memory
)
773 using content::StoragePartition
;
774 base::FilePath cache_path
;
775 int cache_max_size
= app_media_cache_max_size_
;
776 if (partition_descriptor
.path
== profile_path_
) {
777 // lazy_params_ is only valid for the default media context creation.
778 cache_path
= lazy_params_
->media_cache_path
;
779 cache_max_size
= lazy_params_
->media_cache_max_size
;
781 cache_path
= partition_descriptor
.path
.Append(chrome::kMediaCacheDirname
);
784 // Use a separate HTTP disk cache for isolated apps.
785 net::HttpCache::BackendFactory
* media_backend
=
786 new net::HttpCache::DefaultBackend(
788 ChooseCacheBackendType(),
791 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE
));
792 net::HttpNetworkSession
* main_network_session
=
793 main_http_factory_
->GetSession();
794 scoped_ptr
<net::HttpCache
> media_http_cache
=
795 CreateHttpFactory(main_network_session
, media_backend
);
797 // Transfer ownership of the cache to MediaRequestContext.
798 context
->SetHttpTransactionFactory(media_http_cache
.Pass());
800 // Note that we do not create a new URLRequestJobFactory because
801 // the media context should behave exactly like its parent context
802 // in all respects except for cache behavior on media subresources.
803 // The CopyFrom() step above means that our media context will use
804 // the same URLRequestJobFactory instance that our parent context does.
809 net::URLRequestContext
*
810 ProfileImplIOData::AcquireMediaRequestContext() const {
811 DCHECK(media_request_context_
);
812 return media_request_context_
.get();
815 net::URLRequestContext
* ProfileImplIOData::AcquireIsolatedAppRequestContext(
816 net::URLRequestContext
* main_context
,
817 const StoragePartitionDescriptor
& partition_descriptor
,
818 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
819 protocol_handler_interceptor
,
820 content::ProtocolHandlerMap
* protocol_handlers
,
821 content::URLRequestInterceptorScopedVector request_interceptors
) const {
822 // We create per-app contexts on demand, unlike the others above.
823 net::URLRequestContext
* app_request_context
=
824 InitializeAppRequestContext(main_context
,
825 partition_descriptor
,
826 protocol_handler_interceptor
.Pass(),
828 request_interceptors
.Pass());
829 DCHECK(app_request_context
);
830 return app_request_context
;
833 net::URLRequestContext
*
834 ProfileImplIOData::AcquireIsolatedMediaRequestContext(
835 net::URLRequestContext
* app_context
,
836 const StoragePartitionDescriptor
& partition_descriptor
) const {
837 // We create per-app media contexts on demand, unlike the others above.
838 net::URLRequestContext
* media_request_context
=
839 InitializeMediaRequestContext(app_context
, partition_descriptor
);
840 DCHECK(media_request_context
);
841 return media_request_context
;
844 void ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread(
846 const base::Closure
& completion
) {
847 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
848 DCHECK(initialized());
850 DCHECK(transport_security_state());
851 // Completes synchronously.
852 transport_security_state()->DeleteAllDynamicDataSince(time
);
853 DCHECK(http_server_properties_manager_
);
854 http_server_properties_manager_
->Clear(completion
);