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