Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / profiles / profile_impl_io_data.cc
blob794780dbd0c0a762a7f169fd8beb80da09b486ef
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/profiles/profile_impl_io_data.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/file_util.h"
10 #include "base/logging.h"
11 #include "base/stl_util.h"
12 #include "base/threading/worker_pool.h"
13 #include "chrome/browser/api/prefs/pref_member.h"
14 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
15 #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
16 #include "chrome/browser/io_thread.h"
17 #include "chrome/browser/net/chrome_net_log.h"
18 #include "chrome/browser/net/clear_on_exit_policy.h"
19 #include "chrome/browser/net/connect_interceptor.h"
20 #include "chrome/browser/net/http_server_properties_manager.h"
21 #include "chrome/browser/net/predictor.h"
22 #include "chrome/browser/net/sqlite_persistent_cookie_store.h"
23 #include "chrome/browser/net/sqlite_server_bound_cert_store.h"
24 #include "chrome/browser/prefs/pref_service.h"
25 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/browser/ui/webui/chrome_url_data_manager_backend.h"
27 #include "chrome/common/chrome_constants.h"
28 #include "chrome/common/chrome_notification_types.h"
29 #include "chrome/common/chrome_switches.h"
30 #include "chrome/common/pref_names.h"
31 #include "chrome/common/url_constants.h"
32 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/notification_service.h"
34 #include "content/public/browser/resource_context.h"
35 #include "content/public/browser/storage_partition.h"
36 #include "extensions/common/constants.h"
37 #include "net/base/server_bound_cert_service.h"
38 #include "net/ftp/ftp_network_layer.h"
39 #include "net/http/http_cache.h"
40 #include "net/url_request/url_request_job_factory_impl.h"
41 #include "webkit/quota/special_storage_policy.h"
43 using content::BrowserThread;
45 ProfileImplIOData::Handle::Handle(Profile* profile)
46 : io_data_(new ProfileImplIOData),
47 profile_(profile),
48 initialized_(false) {
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
50 DCHECK(profile);
53 ProfileImplIOData::Handle::~Handle() {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
55 if (io_data_->predictor_.get() != NULL) {
56 // io_data_->predictor_ might be NULL if Init() was never called
57 // (i.e. we shut down before ProfileImpl::DoFinalInit() got called).
58 PrefService* user_prefs = profile_->GetPrefs();
59 io_data_->predictor_->ShutdownOnUIThread(user_prefs);
62 if (io_data_->http_server_properties_manager())
63 io_data_->http_server_properties_manager()->ShutdownOnUIThread();
64 io_data_->ShutdownOnUIThread();
67 void ProfileImplIOData::Handle::Init(
68 const FilePath& cookie_path,
69 const FilePath& server_bound_cert_path,
70 const FilePath& cache_path,
71 int cache_max_size,
72 const FilePath& media_cache_path,
73 int media_cache_max_size,
74 const FilePath& extensions_cookie_path,
75 const FilePath& profile_path,
76 const FilePath& infinite_cache_path,
77 chrome_browser_net::Predictor* predictor,
78 PrefService* local_state,
79 IOThread* io_thread,
80 bool restore_old_session_cookies,
81 quota::SpecialStoragePolicy* special_storage_policy) {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
83 DCHECK(!io_data_->lazy_params_.get());
84 DCHECK(predictor);
86 LazyParams* lazy_params = new LazyParams;
88 lazy_params->cookie_path = cookie_path;
89 lazy_params->server_bound_cert_path = server_bound_cert_path;
90 lazy_params->cache_path = cache_path;
91 lazy_params->cache_max_size = cache_max_size;
92 lazy_params->media_cache_path = media_cache_path;
93 lazy_params->media_cache_max_size = media_cache_max_size;
94 lazy_params->extensions_cookie_path = extensions_cookie_path;
95 lazy_params->infinite_cache_path = infinite_cache_path;
96 lazy_params->restore_old_session_cookies = restore_old_session_cookies;
97 lazy_params->special_storage_policy = special_storage_policy;
99 io_data_->lazy_params_.reset(lazy_params);
101 // Keep track of profile path and cache sizes separately so we can use them
102 // on demand when creating storage isolated URLRequestContextGetters.
103 io_data_->profile_path_ = profile_path;
104 io_data_->app_cache_max_size_ = cache_max_size;
105 io_data_->app_media_cache_max_size_ = media_cache_max_size;
107 io_data_->predictor_.reset(predictor);
109 if (!main_request_context_getter_) {
110 main_request_context_getter_ =
111 ChromeURLRequestContextGetter::CreateOriginal(
112 profile_, io_data_);
114 io_data_->predictor_->InitNetworkPredictor(profile_->GetPrefs(),
115 local_state,
116 io_thread,
117 main_request_context_getter_);
119 io_data_->InitializeMetricsEnabledStateOnUIThread();
122 base::Callback<ChromeURLDataManagerBackend*(void)>
123 ProfileImplIOData::Handle::GetChromeURLDataManagerBackendGetter() const {
124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
125 LazyInitialize();
126 return base::Bind(&ProfileIOData::GetChromeURLDataManagerBackend,
127 base::Unretained(io_data_));
130 content::ResourceContext*
131 ProfileImplIOData::Handle::GetResourceContext() const {
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
133 LazyInitialize();
134 return GetResourceContextNoInit();
137 content::ResourceContext*
138 ProfileImplIOData::Handle::GetResourceContextNoInit() const {
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
140 // Don't call LazyInitialize here, since the resource context is created at
141 // the beginning of initalization and is used by some members while they're
142 // being initialized (i.e. AppCacheService).
143 return io_data_->GetResourceContext();
146 scoped_refptr<ChromeURLRequestContextGetter>
147 ProfileImplIOData::Handle::GetMainRequestContextGetter() const {
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
149 LazyInitialize();
150 if (!main_request_context_getter_) {
151 main_request_context_getter_ =
152 ChromeURLRequestContextGetter::CreateOriginal(
153 profile_, io_data_);
155 content::NotificationService::current()->Notify(
156 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
157 content::Source<Profile>(profile_),
158 content::NotificationService::NoDetails());
160 return main_request_context_getter_;
163 scoped_refptr<ChromeURLRequestContextGetter>
164 ProfileImplIOData::Handle::GetMediaRequestContextGetter() const {
165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
166 LazyInitialize();
167 if (!media_request_context_getter_) {
168 media_request_context_getter_ =
169 ChromeURLRequestContextGetter::CreateOriginalForMedia(
170 profile_, io_data_);
172 return media_request_context_getter_;
175 scoped_refptr<ChromeURLRequestContextGetter>
176 ProfileImplIOData::Handle::GetExtensionsRequestContextGetter() const {
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
178 LazyInitialize();
179 if (!extensions_request_context_getter_) {
180 extensions_request_context_getter_ =
181 ChromeURLRequestContextGetter::CreateOriginalForExtensions(
182 profile_, io_data_);
184 return extensions_request_context_getter_;
187 scoped_refptr<ChromeURLRequestContextGetter>
188 ProfileImplIOData::Handle::GetIsolatedAppRequestContextGetter(
189 const FilePath& partition_path,
190 bool in_memory) const {
191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
192 // Check that the partition_path is not the same as the base profile path. We
193 // expect isolated partition, which will never go to the default profile path.
194 CHECK(partition_path != profile_->GetPath());
195 LazyInitialize();
197 // Keep a map of request context getters, one per requested storage partition.
198 StoragePartitionDescriptor descriptor(partition_path, in_memory);
199 ChromeURLRequestContextGetterMap::iterator iter =
200 app_request_context_getter_map_.find(descriptor);
201 if (iter != app_request_context_getter_map_.end())
202 return iter->second;
204 scoped_ptr<net::URLRequestJobFactory::Interceptor>
205 protocol_handler_interceptor(
206 ProtocolHandlerRegistryFactory::GetForProfile(profile_)->
207 CreateURLInterceptor());
208 ChromeURLRequestContextGetter* context =
209 ChromeURLRequestContextGetter::CreateOriginalForIsolatedApp(
210 profile_, io_data_, descriptor,
211 protocol_handler_interceptor.Pass());
212 app_request_context_getter_map_[descriptor] = context;
214 return context;
217 scoped_refptr<ChromeURLRequestContextGetter>
218 ProfileImplIOData::Handle::GetIsolatedMediaRequestContextGetter(
219 const FilePath& partition_path,
220 bool in_memory) const {
221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
222 // We must have a non-default path, or this will act like the default media
223 // context.
224 CHECK(partition_path != profile_->GetPath());
225 LazyInitialize();
227 // Keep a map of request context getters, one per requested storage partition.
228 StoragePartitionDescriptor descriptor(partition_path, in_memory);
229 ChromeURLRequestContextGetterMap::iterator iter =
230 isolated_media_request_context_getter_map_.find(descriptor);
231 if (iter != isolated_media_request_context_getter_map_.end())
232 return iter->second;
234 // Get the app context as the starting point for the media context, so that
235 // it uses the app's cookie store.
236 ChromeURLRequestContextGetter* app_context =
237 GetIsolatedAppRequestContextGetter(partition_path, in_memory);
238 ChromeURLRequestContextGetter* context =
239 ChromeURLRequestContextGetter::CreateOriginalForIsolatedMedia(
240 profile_, app_context, io_data_, descriptor);
241 isolated_media_request_context_getter_map_[descriptor] = context;
243 return context;
246 void ProfileImplIOData::Handle::ClearNetworkingHistorySince(
247 base::Time time,
248 const base::Closure& completion) {
249 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
250 LazyInitialize();
252 BrowserThread::PostTask(
253 BrowserThread::IO, FROM_HERE,
254 base::Bind(
255 &ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread,
256 base::Unretained(io_data_),
257 time,
258 completion));
261 void ProfileImplIOData::Handle::LazyInitialize() const {
262 if (initialized_)
263 return;
265 // Set initialized_ to true at the beginning in case any of the objects
266 // below try to get the ResourceContext pointer.
267 initialized_ = true;
268 PrefService* pref_service = profile_->GetPrefs();
269 io_data_->set_http_server_properties_manager(
270 new chrome_browser_net::HttpServerPropertiesManager(pref_service));
271 io_data_->session_startup_pref()->Init(
272 prefs::kRestoreOnStartup, pref_service, NULL);
273 io_data_->session_startup_pref()->MoveToThread(
274 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
275 #if defined(ENABLE_SAFE_BROWSING)
276 io_data_->safe_browsing_enabled()->Init(prefs::kSafeBrowsingEnabled,
277 pref_service, NULL);
278 io_data_->safe_browsing_enabled()->MoveToThread(
279 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
280 #endif
281 io_data_->InitializeOnUIThread(profile_);
284 ProfileImplIOData::LazyParams::LazyParams()
285 : cache_max_size(0),
286 media_cache_max_size(0),
287 restore_old_session_cookies(false) {}
289 ProfileImplIOData::LazyParams::~LazyParams() {}
291 ProfileImplIOData::ProfileImplIOData()
292 : ProfileIOData(false) {}
293 ProfileImplIOData::~ProfileImplIOData() {
294 DestroyResourceContext();
296 if (media_request_context_.get())
297 media_request_context_->AssertNoURLRequests();
300 void ProfileImplIOData::LazyInitializeInternal(
301 ProfileParams* profile_params) const {
302 ChromeURLRequestContext* main_context = main_request_context();
304 IOThread* const io_thread = profile_params->io_thread;
305 IOThread::Globals* const io_thread_globals = io_thread->globals();
306 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
307 // Only allow Record Mode if we are in a Debug build or where we are running
308 // a cycle, and the user has limited control.
309 bool record_mode = command_line.HasSwitch(switches::kRecordMode) &&
310 (chrome::kRecordModeEnabled ||
311 command_line.HasSwitch(switches::kVisitURLs));
312 bool playback_mode = command_line.HasSwitch(switches::kPlaybackMode);
314 // Initialize context members.
316 ApplyProfileParamsToContext(main_context);
318 if (http_server_properties_manager())
319 http_server_properties_manager()->InitializeOnIOThread();
321 main_context->set_transport_security_state(transport_security_state());
323 main_context->set_net_log(io_thread->net_log());
325 main_context->set_network_delegate(network_delegate());
327 main_context->set_http_server_properties(http_server_properties_manager());
329 main_context->set_host_resolver(
330 io_thread_globals->host_resolver.get());
331 main_context->set_cert_verifier(
332 io_thread_globals->cert_verifier.get());
333 main_context->set_http_auth_handler_factory(
334 io_thread_globals->http_auth_handler_factory.get());
336 main_context->set_fraudulent_certificate_reporter(
337 fraudulent_certificate_reporter());
339 main_context->set_throttler_manager(
340 io_thread_globals->throttler_manager.get());
342 main_context->set_proxy_service(proxy_service());
344 scoped_refptr<net::CookieStore> cookie_store = NULL;
345 net::ServerBoundCertService* server_bound_cert_service = NULL;
346 if (record_mode || playback_mode) {
347 // Don't use existing cookies and use an in-memory store.
348 cookie_store = new net::CookieMonster(
349 NULL, profile_params->cookie_monster_delegate);
350 // Don't use existing server-bound certs and use an in-memory store.
351 server_bound_cert_service = new net::ServerBoundCertService(
352 new net::DefaultServerBoundCertStore(NULL),
353 base::WorkerPool::GetTaskRunner(true));
356 // setup cookie store
357 if (!cookie_store) {
358 DCHECK(!lazy_params_->cookie_path.empty());
360 scoped_refptr<SQLitePersistentCookieStore> cookie_db =
361 new SQLitePersistentCookieStore(
362 lazy_params_->cookie_path,
363 lazy_params_->restore_old_session_cookies,
364 new ClearOnExitPolicy(lazy_params_->special_storage_policy));
365 cookie_store =
366 new net::CookieMonster(cookie_db.get(),
367 profile_params->cookie_monster_delegate);
368 cookie_store->GetCookieMonster()->SetPersistSessionCookies(true);
371 main_context->set_cookie_store(cookie_store);
373 // Setup server bound cert service.
374 if (!server_bound_cert_service) {
375 DCHECK(!lazy_params_->server_bound_cert_path.empty());
377 scoped_refptr<SQLiteServerBoundCertStore> server_bound_cert_db =
378 new SQLiteServerBoundCertStore(
379 lazy_params_->server_bound_cert_path,
380 new ClearOnExitPolicy(lazy_params_->special_storage_policy));
381 server_bound_cert_service = new net::ServerBoundCertService(
382 new net::DefaultServerBoundCertStore(server_bound_cert_db.get()),
383 base::WorkerPool::GetTaskRunner(true));
386 set_server_bound_cert_service(server_bound_cert_service);
387 main_context->set_server_bound_cert_service(server_bound_cert_service);
389 net::HttpCache::DefaultBackend* main_backend =
390 new net::HttpCache::DefaultBackend(
391 net::DISK_CACHE,
392 lazy_params_->cache_path,
393 lazy_params_->cache_max_size,
394 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
395 net::HttpNetworkSession::Params network_session_params;
396 PopulateNetworkSessionParams(profile_params, &network_session_params);
397 net::HttpCache* main_cache = new net::HttpCache(
398 network_session_params, main_backend);
399 main_cache->InitializeInfiniteCache(lazy_params_->infinite_cache_path);
401 if (record_mode || playback_mode) {
402 main_cache->set_mode(
403 record_mode ? net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
406 main_http_factory_.reset(main_cache);
407 main_context->set_http_transaction_factory(main_cache);
409 #if !defined(DISABLE_FTP_SUPPORT)
410 ftp_factory_.reset(
411 new net::FtpNetworkLayer(io_thread_globals->host_resolver.get()));
412 main_context->set_ftp_transaction_factory(ftp_factory_.get());
413 #endif // !defined(DISABLE_FTP_SUPPORT)
415 main_context->set_chrome_url_data_manager_backend(
416 chrome_url_data_manager_backend());
418 scoped_ptr<net::URLRequestJobFactoryImpl> main_job_factory(
419 new net::URLRequestJobFactoryImpl());
420 SetUpJobFactory(main_job_factory.get(),
421 profile_params->protocol_handler_interceptor.Pass(),
422 network_delegate(),
423 main_context->ftp_transaction_factory(),
424 main_context->ftp_auth_cache());
425 main_job_factory_ = main_job_factory.Pass();
426 main_context->set_job_factory(main_job_factory_.get());
428 #if defined(ENABLE_EXTENSIONS)
429 InitializeExtensionsRequestContext(profile_params);
430 #endif
432 // Create a media request context based on the main context, but using a
433 // media cache. It shares the same job factory as the main context.
434 StoragePartitionDescriptor details(FilePath(), false);
435 media_request_context_.reset(InitializeMediaRequestContext(main_context,
436 details));
438 lazy_params_.reset();
441 void ProfileImplIOData::
442 InitializeExtensionsRequestContext(ProfileParams* profile_params) const {
443 ChromeURLRequestContext* extensions_context = extensions_request_context();
444 IOThread* const io_thread = profile_params->io_thread;
445 IOThread::Globals* const io_thread_globals = io_thread->globals();
446 ApplyProfileParamsToContext(extensions_context);
448 extensions_context->set_transport_security_state(transport_security_state());
450 extensions_context->set_net_log(io_thread->net_log());
452 extensions_context->set_throttler_manager(
453 io_thread_globals->throttler_manager.get());
455 net::CookieMonster* extensions_cookie_store =
456 new net::CookieMonster(
457 new SQLitePersistentCookieStore(
458 lazy_params_->extensions_cookie_path,
459 lazy_params_->restore_old_session_cookies, NULL), NULL);
460 // Enable cookies for devtools and extension URLs.
461 const char* schemes[] = {chrome::kChromeDevToolsScheme,
462 extensions::kExtensionScheme};
463 extensions_cookie_store->SetCookieableSchemes(schemes, 2);
464 extensions_context->set_cookie_store(extensions_cookie_store);
466 #if !defined(DISABLE_FTP_SUPPORT)
467 DCHECK(ftp_factory_.get());
468 extensions_context->set_ftp_transaction_factory(ftp_factory_.get());
469 #endif // !defined(DISABLE_FTP_SUPPORT)
471 scoped_ptr<net::URLRequestJobFactoryImpl> extensions_job_factory(
472 new net::URLRequestJobFactoryImpl());
473 // TODO(shalev): The extensions_job_factory has a NULL NetworkDelegate.
474 // Without a network_delegate, this protocol handler will never
475 // handle file: requests, but as a side effect it makes
476 // job_factory::IsHandledProtocol return true, which prevents attempts to
477 // handle the protocol externally. We pass NULL in to
478 // SetUpJobFactory() to get this effect.
479 SetUpJobFactory(extensions_job_factory.get(),
480 scoped_ptr<net::URLRequestJobFactoryImpl::Interceptor>(NULL),
481 NULL,
482 extensions_context->ftp_transaction_factory(),
483 extensions_context->ftp_auth_cache());
484 extensions_job_factory_ = extensions_job_factory.Pass();
485 extensions_context->set_job_factory(extensions_job_factory_.get());
488 ChromeURLRequestContext*
489 ProfileImplIOData::InitializeAppRequestContext(
490 ChromeURLRequestContext* main_context,
491 const StoragePartitionDescriptor& partition_descriptor,
492 scoped_ptr<net::URLRequestJobFactory::Interceptor>
493 protocol_handler_interceptor) const {
494 // Copy most state from the main context.
495 AppRequestContext* context = new AppRequestContext(load_time_stats());
496 context->CopyFrom(main_context);
498 FilePath cookie_path = partition_descriptor.path.Append(
499 chrome::kCookieFilename);
500 FilePath cache_path = partition_descriptor.path.Append(chrome::kCacheDirname);
502 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
503 // Only allow Record Mode if we are in a Debug build or where we are running
504 // a cycle, and the user has limited control.
505 bool record_mode = command_line.HasSwitch(switches::kRecordMode) &&
506 (chrome::kRecordModeEnabled ||
507 command_line.HasSwitch(switches::kVisitURLs));
508 bool playback_mode = command_line.HasSwitch(switches::kPlaybackMode);
510 // Use a separate HTTP disk cache for isolated apps.
511 net::HttpCache::BackendFactory* app_backend = NULL;
512 if (partition_descriptor.in_memory) {
513 app_backend = net::HttpCache::DefaultBackend::InMemory(0);
514 } else {
515 app_backend = new net::HttpCache::DefaultBackend(
516 net::DISK_CACHE,
517 cache_path,
518 app_cache_max_size_,
519 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
521 net::HttpNetworkSession* main_network_session =
522 main_http_factory_->GetSession();
523 net::HttpCache* app_http_cache =
524 new net::HttpCache(main_network_session, app_backend);
526 scoped_refptr<net::CookieStore> cookie_store = NULL;
527 if (partition_descriptor.in_memory) {
528 cookie_store = new net::CookieMonster(NULL, NULL);
529 } else if (record_mode || playback_mode) {
530 // Don't use existing cookies and use an in-memory store.
531 // TODO(creis): We should have a cookie delegate for notifying the cookie
532 // extensions API, but we need to update it to understand isolated apps
533 // first.
534 cookie_store = new net::CookieMonster(NULL, NULL);
535 app_http_cache->set_mode(
536 record_mode ? net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
539 // Use an app-specific cookie store.
540 if (!cookie_store) {
541 DCHECK(!cookie_path.empty());
543 scoped_refptr<SQLitePersistentCookieStore> cookie_db =
544 new SQLitePersistentCookieStore(cookie_path, false, NULL);
545 // TODO(creis): We should have a cookie delegate for notifying the cookie
546 // extensions API, but we need to update it to understand isolated apps
547 // first.
548 cookie_store = new net::CookieMonster(cookie_db.get(), NULL);
551 // Transfer ownership of the cookies and cache to AppRequestContext.
552 context->SetCookieStore(cookie_store);
553 context->SetHttpTransactionFactory(
554 scoped_ptr<net::HttpTransactionFactory>(app_http_cache));
556 // Overwrite the job factory that we inherit from the main context so
557 // that we can later provide our own handles for storage related protocols.
558 // Install all the usual protocol handlers unless we are in a browser plugin
559 // guest process, in which case only web-safe schemes are allowed.
560 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
561 new net::URLRequestJobFactoryImpl());
562 if (!partition_descriptor.in_memory) {
563 SetUpJobFactory(job_factory.get(), protocol_handler_interceptor.Pass(),
564 network_delegate(),
565 context->ftp_transaction_factory(),
566 context->ftp_auth_cache());
568 context->SetJobFactory(job_factory.PassAs<net::URLRequestJobFactory>());
570 return context;
573 ChromeURLRequestContext*
574 ProfileImplIOData::InitializeMediaRequestContext(
575 ChromeURLRequestContext* original_context,
576 const StoragePartitionDescriptor& partition_descriptor) const {
577 // If this is for a in_memory partition, we can simply use the original
578 // context (like off-the-record mode).
579 if (partition_descriptor.in_memory)
580 return original_context;
582 // Copy most state from the original context.
583 MediaRequestContext* context = new MediaRequestContext(load_time_stats());
584 context->CopyFrom(original_context);
586 using content::StoragePartition;
587 FilePath cache_path;
588 int cache_max_size = app_media_cache_max_size_;
589 if (partition_descriptor.path == profile_path_) {
590 // lazy_params_ is only valid for the default media context creation.
591 cache_path = lazy_params_->media_cache_path;
592 cache_max_size = lazy_params_->media_cache_max_size;
593 } else {
594 cache_path = partition_descriptor.path.Append(chrome::kMediaCacheDirname);
597 // Use a separate HTTP disk cache for isolated apps.
598 net::HttpCache::BackendFactory* media_backend =
599 new net::HttpCache::DefaultBackend(
600 net::MEDIA_CACHE,
601 cache_path,
602 cache_max_size,
603 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
604 net::HttpNetworkSession* main_network_session =
605 main_http_factory_->GetSession();
606 scoped_ptr<net::HttpTransactionFactory> media_http_cache(
607 new net::HttpCache(main_network_session, media_backend));
609 // Transfer ownership of the cache to MediaRequestContext.
610 context->SetHttpTransactionFactory(media_http_cache.Pass());
612 // Note that we do not create a new URLRequestJobFactory because
613 // the media context should behave exactly like its parent context
614 // in all respects except for cache behavior on media subresources.
615 // The CopyFrom() step above means that our media context will use
616 // the same URLRequestJobFactory instance that our parent context does.
618 return context;
621 ChromeURLRequestContext*
622 ProfileImplIOData::AcquireMediaRequestContext() const {
623 DCHECK(media_request_context_.get());
624 return media_request_context_.get();
627 ChromeURLRequestContext*
628 ProfileImplIOData::AcquireIsolatedAppRequestContext(
629 ChromeURLRequestContext* main_context,
630 const StoragePartitionDescriptor& partition_descriptor,
631 scoped_ptr<net::URLRequestJobFactory::Interceptor>
632 protocol_handler_interceptor) const {
633 // We create per-app contexts on demand, unlike the others above.
634 ChromeURLRequestContext* app_request_context =
635 InitializeAppRequestContext(main_context, partition_descriptor,
636 protocol_handler_interceptor.Pass());
637 DCHECK(app_request_context);
638 return app_request_context;
641 ChromeURLRequestContext*
642 ProfileImplIOData::AcquireIsolatedMediaRequestContext(
643 ChromeURLRequestContext* app_context,
644 const StoragePartitionDescriptor& partition_descriptor) const {
645 // We create per-app media contexts on demand, unlike the others above.
646 ChromeURLRequestContext* media_request_context =
647 InitializeMediaRequestContext(app_context, partition_descriptor);
648 DCHECK(media_request_context);
649 return media_request_context;
652 chrome_browser_net::LoadTimeStats* ProfileImplIOData::GetLoadTimeStats(
653 IOThread::Globals* io_thread_globals) const {
654 return io_thread_globals->load_time_stats.get();
657 void ProfileImplIOData::SetUpJobFactory(
658 net::URLRequestJobFactoryImpl* job_factory,
659 scoped_ptr<net::URLRequestJobFactory::Interceptor>
660 protocol_handler_interceptor,
661 net::NetworkDelegate* network_delegate,
662 net::FtpTransactionFactory* ftp_transaction_factory,
663 net::FtpAuthCache* ftp_auth_cache) const {
664 SetUpJobFactoryDefaults(job_factory, protocol_handler_interceptor.Pass(),
665 network_delegate, ftp_transaction_factory,
666 ftp_auth_cache);
668 job_factory->AddInterceptor(
669 new chrome_browser_net::ConnectInterceptor(predictor_.get()));
672 void ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread(
673 base::Time time,
674 const base::Closure& completion) {
675 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
676 LazyInitialize();
678 DCHECK(transport_security_state());
679 transport_security_state()->DeleteSince(time); // Completes synchronously.
680 DCHECK(http_server_properties_manager());
681 http_server_properties_manager()->Clear(completion);