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