profile_io_data.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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_io_data.h" 6 7#include <string> 8 9#include "base/basictypes.h" 10#include "base/bind.h" 11#include "base/bind_helpers.h" 12#include "base/callback.h" 13#include "base/command_line.h" 14#include "base/compiler_specific.h" 15#include "base/debug/alias.h" 16#include "base/logging.h" 17#include "base/path_service.h" 18#include "base/prefs/pref_service.h" 19#include "base/stl_util.h" 20#include "base/strings/string_number_conversions.h" 21#include "base/strings/string_util.h" 22#include "base/strings/stringprintf.h" 23#include "base/threading/sequenced_worker_pool.h" 24#include "chrome/browser/browser_process.h" 25#include "chrome/browser/chrome_notification_types.h" 26#include "chrome/browser/content_settings/content_settings_provider.h" 27#include "chrome/browser/content_settings/cookie_settings.h" 28#include "chrome/browser/content_settings/host_content_settings_map.h" 29#include "chrome/browser/custom_handlers/protocol_handler_registry.h" 30#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h" 31#include "chrome/browser/devtools/devtools_network_controller.h" 32#include "chrome/browser/devtools/devtools_network_transaction_factory.h" 33#include "chrome/browser/download/download_service.h" 34#include "chrome/browser/download/download_service_factory.h" 35#include "chrome/browser/io_thread.h" 36#include "chrome/browser/media/media_device_id_salt.h" 37#include "chrome/browser/net/about_protocol_handler.h" 38#include "chrome/browser/net/chrome_fraudulent_certificate_reporter.h" 39#include "chrome/browser/net/chrome_http_user_agent_settings.h" 40#include "chrome/browser/net/chrome_net_log.h" 41#include "chrome/browser/net/chrome_network_delegate.h" 42#include "chrome/browser/net/cookie_store_util.h" 43#include "chrome/browser/net/proxy_service_factory.h" 44#include "chrome/browser/profiles/profile.h" 45#include "chrome/browser/profiles/profile_manager.h" 46#include "chrome/browser/signin/signin_names_io_thread.h" 47#include "chrome/common/chrome_paths.h" 48#include "chrome/common/chrome_switches.h" 49#include "chrome/common/pref_names.h" 50#include "chrome/common/url_constants.h" 51#include "components/dom_distiller/core/url_constants.h" 52#include "components/startup_metric_utils/startup_metric_utils.h" 53#include "components/sync_driver/pref_names.h" 54#include "components/url_fixer/url_fixer.h" 55#include "content/public/browser/browser_thread.h" 56#include "content/public/browser/host_zoom_map.h" 57#include "content/public/browser/notification_service.h" 58#include "content/public/browser/resource_context.h" 59#include "net/base/keygen_handler.h" 60#include "net/cookies/canonical_cookie.h" 61#include "net/http/http_transaction_factory.h" 62#include "net/http/http_util.h" 63#include "net/http/transport_security_persister.h" 64#include "net/proxy/proxy_config_service_fixed.h" 65#include "net/proxy/proxy_script_fetcher_impl.h" 66#include "net/proxy/proxy_service.h" 67#include "net/ssl/channel_id_service.h" 68#include "net/ssl/client_cert_store.h" 69#include "net/url_request/data_protocol_handler.h" 70#include "net/url_request/file_protocol_handler.h" 71#include "net/url_request/ftp_protocol_handler.h" 72#include "net/url_request/url_request.h" 73#include "net/url_request/url_request_context.h" 74#include "net/url_request/url_request_file_job.h" 75#include "net/url_request/url_request_intercepting_job_factory.h" 76#include "net/url_request/url_request_interceptor.h" 77#include "net/url_request/url_request_job_factory_impl.h" 78 79#if defined(ENABLE_CONFIGURATION_POLICY) 80#include "chrome/browser/policy/cloud/policy_header_service_factory.h" 81#include "chrome/browser/policy/policy_helpers.h" 82#include "components/policy/core/browser/url_blacklist_manager.h" 83#include "components/policy/core/common/cloud/policy_header_io_helper.h" 84#include "components/policy/core/common/cloud/policy_header_service.h" 85#include "components/policy/core/common/cloud/user_cloud_policy_manager.h" 86#endif 87 88#if defined(ENABLE_EXTENSIONS) 89#include "chrome/browser/extensions/extension_resource_protocols.h" 90#include "extensions/browser/extension_protocols.h" 91#include "extensions/browser/extension_system.h" 92#include "extensions/browser/info_map.h" 93#include "extensions/common/constants.h" 94#endif 95 96#if defined(ENABLE_MANAGED_USERS) 97#include "chrome/browser/supervised_user/supervised_user_service.h" 98#include "chrome/browser/supervised_user/supervised_user_service_factory.h" 99#include "chrome/browser/supervised_user/supervised_user_url_filter.h" 100#endif 101 102#if defined(OS_ANDROID) 103#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" 104#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h" 105#include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h" 106#endif // defined(OS_ANDROID) 107 108#if defined(OS_CHROMEOS) 109#include "chrome/browser/chromeos/drive/drive_protocol_handler.h" 110#include "chrome/browser/chromeos/login/startup_utils.h" 111#include "chrome/browser/chromeos/login/users/user_manager.h" 112#include "chrome/browser/chromeos/net/cert_verify_proc_chromeos.h" 113#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" 114#include "chrome/browser/chromeos/policy/policy_cert_service.h" 115#include "chrome/browser/chromeos/policy/policy_cert_service_factory.h" 116#include "chrome/browser/chromeos/policy/policy_cert_verifier.h" 117#include "chrome/browser/chromeos/profiles/profile_helper.h" 118#include "chrome/browser/chromeos/settings/cros_settings.h" 119#include "chrome/browser/net/nss_context.h" 120#include "chromeos/dbus/cryptohome_client.h" 121#include "chromeos/dbus/dbus_thread_manager.h" 122#include "chromeos/settings/cros_settings_names.h" 123#include "components/user_manager/user.h" 124#include "crypto/nss_util.h" 125#include "crypto/nss_util_internal.h" 126#include "net/cert/multi_threaded_cert_verifier.h" 127#include "net/ssl/client_cert_store_chromeos.h" 128#endif // defined(OS_CHROMEOS) 129 130#if defined(USE_NSS) 131#include "chrome/browser/ui/crypto_module_delegate_nss.h" 132#include "net/ssl/client_cert_store_nss.h" 133#endif 134 135#if defined(OS_WIN) 136#include "net/ssl/client_cert_store_win.h" 137#endif 138 139#if defined(OS_MACOSX) 140#include "net/ssl/client_cert_store_mac.h" 141#endif 142 143using content::BrowserContext; 144using content::BrowserThread; 145using content::ResourceContext; 146using data_reduction_proxy::DataReductionProxyUsageStats; 147 148namespace { 149 150#if defined(DEBUG_DEVTOOLS) 151bool IsSupportedDevToolsURL(const GURL& url, base::FilePath* path) { 152 std::string bundled_path_prefix(chrome::kChromeUIDevToolsBundledPath); 153 bundled_path_prefix = "/" + bundled_path_prefix + "/"; 154 155 if (!url.SchemeIs(content::kChromeDevToolsScheme) || 156 url.host() != chrome::kChromeUIDevToolsHost || 157 !StartsWithASCII(url.path(), bundled_path_prefix, false)) { 158 return false; 159 } 160 161 if (!url.is_valid()) { 162 NOTREACHED(); 163 return false; 164 } 165 166 // Remove Query and Ref from URL. 167 GURL stripped_url; 168 GURL::Replacements replacements; 169 replacements.ClearQuery(); 170 replacements.ClearRef(); 171 stripped_url = url.ReplaceComponents(replacements); 172 173 std::string relative_path; 174 const std::string& spec = stripped_url.possibly_invalid_spec(); 175 const url::Parsed& parsed = stripped_url.parsed_for_possibly_invalid_spec(); 176 int offset = parsed.CountCharactersBefore(url::Parsed::PATH, false); 177 if (offset < static_cast<int>(spec.size())) 178 relative_path.assign(spec.substr(offset + bundled_path_prefix.length())); 179 180 // Check that |relative_path| is not an absolute path (otherwise 181 // AppendASCII() will DCHECK). The awkward use of StringType is because on 182 // some systems FilePath expects a std::string, but on others a std::wstring. 183 base::FilePath p( 184 base::FilePath::StringType(relative_path.begin(), relative_path.end())); 185 if (p.IsAbsolute()) 186 return false; 187 188 base::FilePath inspector_dir; 189 if (!PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir)) 190 return false; 191 192 if (inspector_dir.empty()) 193 return false; 194 195 *path = inspector_dir.AppendASCII(relative_path); 196 return true; 197} 198 199class DebugDevToolsInterceptor : public net::URLRequestInterceptor { 200 public: 201 DebugDevToolsInterceptor() {} 202 virtual ~DebugDevToolsInterceptor() {} 203 204 // net::URLRequestInterceptor implementation. 205 virtual net::URLRequestJob* MaybeInterceptRequest( 206 net::URLRequest* request, 207 net::NetworkDelegate* network_delegate) const OVERRIDE { 208 base::FilePath path; 209 if (IsSupportedDevToolsURL(request->url(), &path)) 210 return new net::URLRequestFileJob( 211 request, network_delegate, path, 212 content::BrowserThread::GetBlockingPool()-> 213 GetTaskRunnerWithShutdownBehavior( 214 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); 215 216 return NULL; 217 } 218}; 219#endif // defined(DEBUG_DEVTOOLS) 220 221#if defined(OS_CHROMEOS) 222// The following four functions are responsible for initializing NSS for each 223// profile on ChromeOS, which has a separate NSS database and TPM slot 224// per-profile. 225// 226// Initialization basically follows these steps: 227// 1) Get some info from chromeos::UserManager about the User for this profile. 228// 2) Tell nss_util to initialize the software slot for this profile. 229// 3) Wait for the TPM module to be loaded by nss_util if it isn't already. 230// 4) Ask CryptohomeClient which TPM slot id corresponds to this profile. 231// 5) Tell nss_util to use that slot id on the TPM module. 232// 233// Some of these steps must happen on the UI thread, others must happen on the 234// IO thread: 235// UI thread IO Thread 236// 237// ProfileIOData::InitializeOnUIThread 238// | 239// ProfileHelper::Get()->GetUserByProfile() 240// \---------------------------------------v 241// StartNSSInitOnIOThread 242// | 243// crypto::InitializeNSSForChromeOSUser 244// | 245// crypto::IsTPMTokenReady 246// | 247// StartTPMSlotInitializationOnIOThread 248// v---------------------------------------/ 249// GetTPMInfoForUserOnUIThread 250// | 251// CryptohomeClient::Pkcs11GetTpmTokenInfoForUser 252// | 253// DidGetTPMInfoForUserOnUIThread 254// \---------------------------------------v 255// crypto::InitializeTPMForChromeOSUser 256 257void DidGetTPMInfoForUserOnUIThread(const std::string& username_hash, 258 chromeos::DBusMethodCallStatus call_status, 259 const std::string& label, 260 const std::string& user_pin, 261 int slot_id) { 262 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 263 if (call_status == chromeos::DBUS_METHOD_CALL_FAILURE) { 264 NOTREACHED() << "dbus error getting TPM info for " << username_hash; 265 return; 266 } 267 DVLOG(1) << "Got TPM slot for " << username_hash << ": " << slot_id; 268 BrowserThread::PostTask( 269 BrowserThread::IO, 270 FROM_HERE, 271 base::Bind( 272 &crypto::InitializeTPMForChromeOSUser, username_hash, slot_id)); 273} 274 275void GetTPMInfoForUserOnUIThread(const std::string& username, 276 const std::string& username_hash) { 277 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 278 DVLOG(1) << "Getting TPM info from cryptohome for " 279 << " " << username << " " << username_hash; 280 chromeos::DBusThreadManager::Get() 281 ->GetCryptohomeClient() 282 ->Pkcs11GetTpmTokenInfoForUser( 283 username, 284 base::Bind(&DidGetTPMInfoForUserOnUIThread, username_hash)); 285} 286 287void StartTPMSlotInitializationOnIOThread(const std::string& username, 288 const std::string& username_hash) { 289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 290 291 BrowserThread::PostTask( 292 BrowserThread::UI, 293 FROM_HERE, 294 base::Bind(&GetTPMInfoForUserOnUIThread, username, username_hash)); 295} 296 297void StartNSSInitOnIOThread(const std::string& username, 298 const std::string& username_hash, 299 const base::FilePath& path) { 300 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 301 DVLOG(1) << "Starting NSS init for " << username 302 << " hash:" << username_hash; 303 304 // Make sure NSS is initialized for the user. 305 crypto::InitializeNSSForChromeOSUser(username, username_hash, path); 306 307 // Check if it's OK to initialize TPM for the user before continuing. This 308 // may not be the case if the TPM slot initialization was previously 309 // requested for the same user. 310 if (!crypto::ShouldInitializeTPMForChromeOSUser(username_hash)) 311 return; 312 313 crypto::WillInitializeTPMForChromeOSUser(username_hash); 314 315 if (crypto::IsTPMTokenEnabledForNSS()) { 316 if (crypto::IsTPMTokenReady(base::Bind( 317 &StartTPMSlotInitializationOnIOThread, username, username_hash))) { 318 StartTPMSlotInitializationOnIOThread(username, username_hash); 319 } else { 320 DVLOG(1) << "Waiting for tpm ready ..."; 321 } 322 } else { 323 crypto::InitializePrivateSoftwareSlotForChromeOSUser(username_hash); 324 } 325} 326#endif // defined(OS_CHROMEOS) 327 328} // namespace 329 330void ProfileIOData::InitializeOnUIThread(Profile* profile) { 331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 332 PrefService* pref_service = profile->GetPrefs(); 333 PrefService* local_state_pref_service = g_browser_process->local_state(); 334 335 scoped_ptr<ProfileParams> params(new ProfileParams); 336 params->path = profile->GetPath(); 337 338 params->io_thread = g_browser_process->io_thread(); 339 340 params->cookie_settings = CookieSettings::Factory::GetForProfile(profile); 341 params->host_content_settings_map = profile->GetHostContentSettingsMap(); 342 params->ssl_config_service = profile->GetSSLConfigService(); 343 params->cookie_monster_delegate = 344 chrome_browser_net::CreateCookieDelegate(profile); 345#if defined(ENABLE_EXTENSIONS) 346 params->extension_info_map = 347 extensions::ExtensionSystem::Get(profile)->info_map(); 348#endif 349 350 ProtocolHandlerRegistry* protocol_handler_registry = 351 ProtocolHandlerRegistryFactory::GetForBrowserContext(profile); 352 DCHECK(protocol_handler_registry); 353 354 // The profile instance is only available here in the InitializeOnUIThread 355 // method, so we create the url job factory here, then save it for 356 // later delivery to the job factory in Init(). 357 params->protocol_handler_interceptor = 358 protocol_handler_registry->CreateJobInterceptorFactory(); 359 360 params->proxy_config_service 361 .reset(ProxyServiceFactory::CreateProxyConfigService( 362 profile->GetProxyConfigTracker())); 363#if defined(ENABLE_MANAGED_USERS) 364 SupervisedUserService* supervised_user_service = 365 SupervisedUserServiceFactory::GetForProfile(profile); 366 params->supervised_user_url_filter = 367 supervised_user_service->GetURLFilterForIOThread(); 368#endif 369#if defined(OS_CHROMEOS) 370 chromeos::UserManager* user_manager = chromeos::UserManager::Get(); 371 if (user_manager) { 372 user_manager::User* user = 373 chromeos::ProfileHelper::Get()->GetUserByProfile(profile); 374 // No need to initialize NSS for users with empty username hash: 375 // Getters for a user's NSS slots always return NULL slot if the user's 376 // username hash is empty, even when the NSS is not initialized for the 377 // user. 378 if (user && !user->username_hash().empty()) { 379 params->username_hash = user->username_hash(); 380 DCHECK(!params->username_hash.empty()); 381 BrowserThread::PostTask(BrowserThread::IO, 382 FROM_HERE, 383 base::Bind(&StartNSSInitOnIOThread, 384 user->email(), 385 user->username_hash(), 386 profile->GetPath())); 387 388 // Use the device-wide system key slot only if the user is of the same 389 // domain as the device is registered to. 390 policy::BrowserPolicyConnectorChromeOS* connector = 391 g_browser_process->platform_part() 392 ->browser_policy_connector_chromeos(); 393 params->use_system_key_slot = 394 connector->GetUserAffiliation(user->email()) == 395 policy::USER_AFFILIATION_MANAGED; 396 } 397 } 398#endif 399 400 params->profile = profile; 401 params->prerender_tracker = g_browser_process->prerender_tracker(); 402 profile_params_.reset(params.release()); 403 404 ChromeNetworkDelegate::InitializePrefsOnUIThread( 405 &enable_referrers_, 406 &enable_do_not_track_, 407 &force_safesearch_, 408 pref_service); 409 410 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy = 411 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); 412#if defined(ENABLE_PRINTING) 413 printing_enabled_.Init(prefs::kPrintingEnabled, pref_service); 414 printing_enabled_.MoveToThread(io_message_loop_proxy); 415#endif 416 417 chrome_http_user_agent_settings_.reset( 418 new ChromeHttpUserAgentSettings(pref_service)); 419 420 // These members are used only for one click sign in, which is not enabled 421 // in incognito mode. So no need to initialize them. 422 if (!IsOffTheRecord()) { 423 signin_names_.reset(new SigninNamesOnIOThread()); 424 425 google_services_user_account_id_.Init( 426 prefs::kGoogleServicesUserAccountId, pref_service); 427 google_services_user_account_id_.MoveToThread(io_message_loop_proxy); 428 429 google_services_username_.Init( 430 prefs::kGoogleServicesUsername, pref_service); 431 google_services_username_.MoveToThread(io_message_loop_proxy); 432 433 google_services_username_pattern_.Init( 434 prefs::kGoogleServicesUsernamePattern, local_state_pref_service); 435 google_services_username_pattern_.MoveToThread(io_message_loop_proxy); 436 437 reverse_autologin_enabled_.Init( 438 prefs::kReverseAutologinEnabled, pref_service); 439 reverse_autologin_enabled_.MoveToThread(io_message_loop_proxy); 440 441 one_click_signin_rejected_email_list_.Init( 442 prefs::kReverseAutologinRejectedEmailList, pref_service); 443 one_click_signin_rejected_email_list_.MoveToThread(io_message_loop_proxy); 444 445 sync_disabled_.Init(sync_driver::prefs::kSyncManaged, pref_service); 446 sync_disabled_.MoveToThread(io_message_loop_proxy); 447 448 signin_allowed_.Init(prefs::kSigninAllowed, pref_service); 449 signin_allowed_.MoveToThread(io_message_loop_proxy); 450 } 451 452 quick_check_enabled_.Init(prefs::kQuickCheckEnabled, 453 local_state_pref_service); 454 quick_check_enabled_.MoveToThread(io_message_loop_proxy); 455 456 media_device_id_salt_ = new MediaDeviceIDSalt(pref_service, IsOffTheRecord()); 457 458 // TODO(bnc): remove per https://crbug.com/334602. 459 network_prediction_enabled_.Init(prefs::kNetworkPredictionEnabled, 460 pref_service); 461 network_prediction_enabled_.MoveToThread(io_message_loop_proxy); 462 463 network_prediction_options_.Init(prefs::kNetworkPredictionOptions, 464 pref_service); 465 466 network_prediction_options_.MoveToThread(io_message_loop_proxy); 467 468#if defined(OS_CHROMEOS) 469 cert_verifier_ = policy::PolicyCertServiceFactory::CreateForProfile(profile); 470#endif 471 // The URLBlacklistManager has to be created on the UI thread to register 472 // observers of |pref_service|, and it also has to clean up on 473 // ShutdownOnUIThread to release these observers on the right thread. 474 // Don't pass it in |profile_params_| to make sure it is correctly cleaned up, 475 // in particular when this ProfileIOData isn't |initialized_| during deletion. 476#if defined(ENABLE_CONFIGURATION_POLICY) 477 policy::URLBlacklist::SegmentURLCallback callback = 478 static_cast<policy::URLBlacklist::SegmentURLCallback>( 479 url_fixer::SegmentURL); 480 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); 481 scoped_refptr<base::SequencedTaskRunner> background_task_runner = 482 pool->GetSequencedTaskRunner(pool->GetSequenceToken()); 483 url_blacklist_manager_.reset( 484 new policy::URLBlacklistManager( 485 pref_service, 486 background_task_runner, 487 io_message_loop_proxy, 488 callback, 489 base::Bind(policy::OverrideBlacklistForURL))); 490 491 if (!IsOffTheRecord()) { 492 // Add policy headers for non-incognito requests. 493 policy::PolicyHeaderService* policy_header_service = 494 policy::PolicyHeaderServiceFactory::GetForBrowserContext(profile); 495 if (policy_header_service) { 496 policy_header_helper_ = policy_header_service->CreatePolicyHeaderIOHelper( 497 io_message_loop_proxy); 498 } 499 } 500#endif 501 502 incognito_availibility_pref_.Init( 503 prefs::kIncognitoModeAvailability, pref_service); 504 incognito_availibility_pref_.MoveToThread(io_message_loop_proxy); 505 506 initialized_on_UI_thread_ = true; 507 508#if defined(OS_ANDROID) 509#if defined(SPDY_PROXY_AUTH_ORIGIN) 510 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 511 base::Bind(&ProfileIOData::SetDataReductionProxyUsageStatsOnIOThread, 512 base::Unretained(this), g_browser_process->io_thread(), profile)); 513#endif 514#endif 515 516 // We need to make sure that content initializes its own data structures that 517 // are associated with each ResourceContext because we might post this 518 // object to the IO thread after this function. 519 BrowserContext::EnsureResourceContextInitialized(profile); 520} 521 522#if defined(OS_ANDROID) 523#if defined(SPDY_PROXY_AUTH_ORIGIN) 524void ProfileIOData::SetDataReductionProxyUsageStatsOnIOThread( 525 IOThread* io_thread, Profile* profile) { 526 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 527 IOThread::Globals* globals = io_thread->globals(); 528 DataReductionProxyUsageStats* usage_stats = 529 globals->data_reduction_proxy_usage_stats.get(); 530 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 531 base::Bind(&ProfileIOData::SetDataReductionProxyUsageStatsOnUIThread, 532 base::Unretained(this), profile, usage_stats)); 533} 534 535void ProfileIOData::SetDataReductionProxyUsageStatsOnUIThread( 536 Profile* profile, 537 DataReductionProxyUsageStats* usage_stats) { 538 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 539 if (g_browser_process->profile_manager()->IsValidProfile(profile)) { 540 DataReductionProxyChromeSettings* data_reduction_proxy_chrome_settings = 541 DataReductionProxyChromeSettingsFactory::GetForBrowserContext(profile); 542 if (data_reduction_proxy_chrome_settings) { 543 data_reduction_proxy_chrome_settings->SetDataReductionProxyUsageStats( 544 usage_stats); 545 } 546 } 547} 548#endif 549#endif 550 551ProfileIOData::MediaRequestContext::MediaRequestContext() { 552} 553 554void ProfileIOData::MediaRequestContext::SetHttpTransactionFactory( 555 scoped_ptr<net::HttpTransactionFactory> http_factory) { 556 http_factory_ = http_factory.Pass(); 557 set_http_transaction_factory(http_factory_.get()); 558} 559 560ProfileIOData::MediaRequestContext::~MediaRequestContext() { 561 AssertNoURLRequests(); 562} 563 564ProfileIOData::AppRequestContext::AppRequestContext() { 565} 566 567void ProfileIOData::AppRequestContext::SetCookieStore( 568 net::CookieStore* cookie_store) { 569 cookie_store_ = cookie_store; 570 set_cookie_store(cookie_store); 571} 572 573void ProfileIOData::AppRequestContext::SetHttpTransactionFactory( 574 scoped_ptr<net::HttpTransactionFactory> http_factory) { 575 http_factory_ = http_factory.Pass(); 576 set_http_transaction_factory(http_factory_.get()); 577} 578 579void ProfileIOData::AppRequestContext::SetJobFactory( 580 scoped_ptr<net::URLRequestJobFactory> job_factory) { 581 job_factory_ = job_factory.Pass(); 582 set_job_factory(job_factory_.get()); 583} 584 585ProfileIOData::AppRequestContext::~AppRequestContext() { 586 AssertNoURLRequests(); 587} 588 589ProfileIOData::ProfileParams::ProfileParams() 590 : io_thread(NULL), 591#if defined(OS_CHROMEOS) 592 use_system_key_slot(false), 593#endif 594 profile(NULL) { 595} 596 597ProfileIOData::ProfileParams::~ProfileParams() {} 598 599ProfileIOData::ProfileIOData(Profile::ProfileType profile_type) 600 : initialized_(false), 601#if defined(OS_CHROMEOS) 602 use_system_key_slot_(false), 603#endif 604 resource_context_(new ResourceContext(this)), 605 initialized_on_UI_thread_(false), 606 profile_type_(profile_type) { 607 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 608} 609 610ProfileIOData::~ProfileIOData() { 611 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) 612 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 613 614 // Pull the contents of the request context maps onto the stack for sanity 615 // checking of values in a minidump. http://crbug.com/260425 616 size_t num_app_contexts = app_request_context_map_.size(); 617 size_t num_media_contexts = isolated_media_request_context_map_.size(); 618 size_t current_context = 0; 619 static const size_t kMaxCachedContexts = 20; 620 net::URLRequestContext* app_context_cache[kMaxCachedContexts] = {0}; 621 void* app_context_vtable_cache[kMaxCachedContexts] = {0}; 622 net::URLRequestContext* media_context_cache[kMaxCachedContexts] = {0}; 623 void* media_context_vtable_cache[kMaxCachedContexts] = {0}; 624 void* tmp_vtable = NULL; 625 base::debug::Alias(&num_app_contexts); 626 base::debug::Alias(&num_media_contexts); 627 base::debug::Alias(¤t_context); 628 base::debug::Alias(app_context_cache); 629 base::debug::Alias(app_context_vtable_cache); 630 base::debug::Alias(media_context_cache); 631 base::debug::Alias(media_context_vtable_cache); 632 base::debug::Alias(&tmp_vtable); 633 634 current_context = 0; 635 for (URLRequestContextMap::const_iterator it = 636 app_request_context_map_.begin(); 637 current_context < kMaxCachedContexts && 638 it != app_request_context_map_.end(); 639 ++it, ++current_context) { 640 app_context_cache[current_context] = it->second; 641 memcpy(&app_context_vtable_cache[current_context], 642 static_cast<void*>(it->second), sizeof(void*)); 643 } 644 645 current_context = 0; 646 for (URLRequestContextMap::const_iterator it = 647 isolated_media_request_context_map_.begin(); 648 current_context < kMaxCachedContexts && 649 it != isolated_media_request_context_map_.end(); 650 ++it, ++current_context) { 651 media_context_cache[current_context] = it->second; 652 memcpy(&media_context_vtable_cache[current_context], 653 static_cast<void*>(it->second), sizeof(void*)); 654 } 655 656 // TODO(ajwong): These AssertNoURLRequests() calls are unnecessary since they 657 // are already done in the URLRequestContext destructor. 658 if (main_request_context_) 659 main_request_context_->AssertNoURLRequests(); 660 if (extensions_request_context_) 661 extensions_request_context_->AssertNoURLRequests(); 662 663 current_context = 0; 664 for (URLRequestContextMap::iterator it = app_request_context_map_.begin(); 665 it != app_request_context_map_.end(); ++it) { 666 if (current_context < kMaxCachedContexts) { 667 CHECK_EQ(app_context_cache[current_context], it->second); 668 memcpy(&tmp_vtable, static_cast<void*>(it->second), sizeof(void*)); 669 CHECK_EQ(app_context_vtable_cache[current_context], tmp_vtable); 670 } 671 it->second->AssertNoURLRequests(); 672 delete it->second; 673 current_context++; 674 } 675 676 current_context = 0; 677 for (URLRequestContextMap::iterator it = 678 isolated_media_request_context_map_.begin(); 679 it != isolated_media_request_context_map_.end(); ++it) { 680 if (current_context < kMaxCachedContexts) { 681 CHECK_EQ(media_context_cache[current_context], it->second); 682 memcpy(&tmp_vtable, static_cast<void*>(it->second), sizeof(void*)); 683 CHECK_EQ(media_context_vtable_cache[current_context], tmp_vtable); 684 } 685 it->second->AssertNoURLRequests(); 686 delete it->second; 687 current_context++; 688 } 689} 690 691// static 692ProfileIOData* ProfileIOData::FromResourceContext( 693 content::ResourceContext* rc) { 694 return (static_cast<ResourceContext*>(rc))->io_data_; 695} 696 697// static 698bool ProfileIOData::IsHandledProtocol(const std::string& scheme) { 699 DCHECK_EQ(scheme, StringToLowerASCII(scheme)); 700 static const char* const kProtocolList[] = { 701 url::kFileScheme, 702 content::kChromeDevToolsScheme, 703 dom_distiller::kDomDistillerScheme, 704#if defined(ENABLE_EXTENSIONS) 705 extensions::kExtensionScheme, 706 extensions::kExtensionResourceScheme, 707#endif 708 content::kChromeUIScheme, 709 url::kDataScheme, 710#if defined(OS_CHROMEOS) 711 chrome::kDriveScheme, 712#endif // defined(OS_CHROMEOS) 713 url::kAboutScheme, 714#if !defined(DISABLE_FTP_SUPPORT) 715 url::kFtpScheme, 716#endif // !defined(DISABLE_FTP_SUPPORT) 717 url::kBlobScheme, 718 url::kFileSystemScheme, 719 chrome::kChromeSearchScheme, 720 }; 721 for (size_t i = 0; i < arraysize(kProtocolList); ++i) { 722 if (scheme == kProtocolList[i]) 723 return true; 724 } 725 return net::URLRequest::IsHandledProtocol(scheme); 726} 727 728// static 729bool ProfileIOData::IsHandledURL(const GURL& url) { 730 if (!url.is_valid()) { 731 // We handle error cases. 732 return true; 733 } 734 735 return IsHandledProtocol(url.scheme()); 736} 737 738// static 739void ProfileIOData::InstallProtocolHandlers( 740 net::URLRequestJobFactoryImpl* job_factory, 741 content::ProtocolHandlerMap* protocol_handlers) { 742 for (content::ProtocolHandlerMap::iterator it = 743 protocol_handlers->begin(); 744 it != protocol_handlers->end(); 745 ++it) { 746 bool set_protocol = job_factory->SetProtocolHandler( 747 it->first, it->second.release()); 748 DCHECK(set_protocol); 749 } 750 protocol_handlers->clear(); 751} 752 753content::ResourceContext* ProfileIOData::GetResourceContext() const { 754 return resource_context_.get(); 755} 756 757net::URLRequestContext* ProfileIOData::GetMainRequestContext() const { 758 DCHECK(initialized_); 759 return main_request_context_.get(); 760} 761 762net::URLRequestContext* ProfileIOData::GetMediaRequestContext() const { 763 DCHECK(initialized_); 764 net::URLRequestContext* context = AcquireMediaRequestContext(); 765 DCHECK(context); 766 return context; 767} 768 769net::URLRequestContext* ProfileIOData::GetExtensionsRequestContext() const { 770 DCHECK(initialized_); 771 return extensions_request_context_.get(); 772} 773 774net::URLRequestContext* ProfileIOData::GetIsolatedAppRequestContext( 775 net::URLRequestContext* main_context, 776 const StoragePartitionDescriptor& partition_descriptor, 777 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> 778 protocol_handler_interceptor, 779 content::ProtocolHandlerMap* protocol_handlers, 780 content::URLRequestInterceptorScopedVector request_interceptors) const { 781 DCHECK(initialized_); 782 net::URLRequestContext* context = NULL; 783 if (ContainsKey(app_request_context_map_, partition_descriptor)) { 784 context = app_request_context_map_[partition_descriptor]; 785 } else { 786 context = 787 AcquireIsolatedAppRequestContext(main_context, 788 partition_descriptor, 789 protocol_handler_interceptor.Pass(), 790 protocol_handlers, 791 request_interceptors.Pass()); 792 app_request_context_map_[partition_descriptor] = context; 793 } 794 DCHECK(context); 795 return context; 796} 797 798net::URLRequestContext* ProfileIOData::GetIsolatedMediaRequestContext( 799 net::URLRequestContext* app_context, 800 const StoragePartitionDescriptor& partition_descriptor) const { 801 DCHECK(initialized_); 802 net::URLRequestContext* context = NULL; 803 if (ContainsKey(isolated_media_request_context_map_, partition_descriptor)) { 804 context = isolated_media_request_context_map_[partition_descriptor]; 805 } else { 806 context = AcquireIsolatedMediaRequestContext(app_context, 807 partition_descriptor); 808 isolated_media_request_context_map_[partition_descriptor] = context; 809 } 810 DCHECK(context); 811 return context; 812} 813 814extensions::InfoMap* ProfileIOData::GetExtensionInfoMap() const { 815 DCHECK(initialized_) << "ExtensionSystem not initialized"; 816#if defined(ENABLE_EXTENSIONS) 817 return extension_info_map_.get(); 818#else 819 return NULL; 820#endif 821} 822 823CookieSettings* ProfileIOData::GetCookieSettings() const { 824 // Allow either Init() or SetCookieSettingsForTesting() to initialize. 825 DCHECK(initialized_ || cookie_settings_.get()); 826 return cookie_settings_.get(); 827} 828 829HostContentSettingsMap* ProfileIOData::GetHostContentSettingsMap() const { 830 DCHECK(initialized_); 831 return host_content_settings_map_.get(); 832} 833 834ResourceContext::SaltCallback ProfileIOData::GetMediaDeviceIDSalt() const { 835 return base::Bind(&MediaDeviceIDSalt::GetSalt, media_device_id_salt_); 836} 837 838bool ProfileIOData::IsOffTheRecord() const { 839 return profile_type() == Profile::INCOGNITO_PROFILE 840 || profile_type() == Profile::GUEST_PROFILE; 841} 842 843void ProfileIOData::InitializeMetricsEnabledStateOnUIThread() { 844 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 845#if defined(OS_CHROMEOS) 846 // Just fetch the value from ChromeOS' settings while we're on the UI thread. 847 // TODO(stevet): For now, this value is only set on profile initialization. 848 // We will want to do something similar to the PrefMember method below in the 849 // future to more accurately capture this state. 850 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, 851 &enable_metrics_); 852#elif defined(OS_ANDROID) 853 // TODO(dwkang): rename or unify the pref for UMA once we have conclusion 854 // in crbugs.com/246495. 855 // Android has it's own preferences for metrics / crash uploading. 856 enable_metrics_.Init(prefs::kCrashReportingEnabled, 857 g_browser_process->local_state()); 858 enable_metrics_.MoveToThread( 859 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); 860#else 861 // Prep the PrefMember and send it to the IO thread, since this value will be 862 // read from there. 863 enable_metrics_.Init(prefs::kMetricsReportingEnabled, 864 g_browser_process->local_state()); 865 enable_metrics_.MoveToThread( 866 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); 867#endif // defined(OS_CHROMEOS) 868} 869 870bool ProfileIOData::GetMetricsEnabledStateOnIOThread() const { 871 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 872#if defined(OS_CHROMEOS) 873 return enable_metrics_; 874#else 875 return enable_metrics_.GetValue(); 876#endif // defined(OS_CHROMEOS) 877} 878 879#if defined(OS_ANDROID) 880bool ProfileIOData::IsDataReductionProxyEnabled() const { 881 return data_reduction_proxy_enabled_.GetValue() || 882 CommandLine::ForCurrentProcess()->HasSwitch( 883 data_reduction_proxy::switches::kEnableDataReductionProxy); 884} 885#endif 886 887base::WeakPtr<net::HttpServerProperties> 888ProfileIOData::http_server_properties() const { 889 return http_server_properties_->GetWeakPtr(); 890} 891 892void ProfileIOData::set_http_server_properties( 893 scoped_ptr<net::HttpServerProperties> http_server_properties) const { 894 http_server_properties_ = http_server_properties.Pass(); 895} 896 897ProfileIOData::ResourceContext::ResourceContext(ProfileIOData* io_data) 898 : io_data_(io_data), 899 host_resolver_(NULL), 900 request_context_(NULL) { 901 DCHECK(io_data); 902} 903 904ProfileIOData::ResourceContext::~ResourceContext() {} 905 906net::HostResolver* ProfileIOData::ResourceContext::GetHostResolver() { 907 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 908 DCHECK(io_data_->initialized_); 909 return host_resolver_; 910} 911 912net::URLRequestContext* ProfileIOData::ResourceContext::GetRequestContext() { 913 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 914 DCHECK(io_data_->initialized_); 915 return request_context_; 916} 917 918scoped_ptr<net::ClientCertStore> 919ProfileIOData::ResourceContext::CreateClientCertStore() { 920 if (!io_data_->client_cert_store_factory_.is_null()) 921 return io_data_->client_cert_store_factory_.Run(); 922#if defined(OS_CHROMEOS) 923 return scoped_ptr<net::ClientCertStore>(new net::ClientCertStoreChromeOS( 924 io_data_->use_system_key_slot(), 925 io_data_->username_hash(), 926 base::Bind(&CreateCryptoModuleBlockingPasswordDelegate, 927 chrome::kCryptoModulePasswordClientAuth))); 928#elif defined(USE_NSS) 929 return scoped_ptr<net::ClientCertStore>(new net::ClientCertStoreNSS( 930 base::Bind(&CreateCryptoModuleBlockingPasswordDelegate, 931 chrome::kCryptoModulePasswordClientAuth))); 932#elif defined(OS_WIN) 933 return scoped_ptr<net::ClientCertStore>(new net::ClientCertStoreWin()); 934#elif defined(OS_MACOSX) 935 return scoped_ptr<net::ClientCertStore>(new net::ClientCertStoreMac()); 936#elif defined(USE_OPENSSL) 937 // OpenSSL does not use the ClientCertStore infrastructure. On Android client 938 // cert matching is done by the OS as part of the call to show the cert 939 // selection dialog. 940 return scoped_ptr<net::ClientCertStore>(); 941#else 942#error Unknown platform. 943#endif 944} 945 946void ProfileIOData::ResourceContext::CreateKeygenHandler( 947 uint32 key_size_in_bits, 948 const std::string& challenge_string, 949 const GURL& url, 950 const base::Callback<void(scoped_ptr<net::KeygenHandler>)>& callback) { 951 DCHECK(!callback.is_null()); 952#if defined(USE_NSS) 953 scoped_ptr<net::KeygenHandler> keygen_handler( 954 new net::KeygenHandler(key_size_in_bits, challenge_string, url)); 955 956 scoped_ptr<ChromeNSSCryptoModuleDelegate> delegate( 957 new ChromeNSSCryptoModuleDelegate(chrome::kCryptoModulePasswordKeygen, 958 net::HostPortPair::FromURL(url))); 959 ChromeNSSCryptoModuleDelegate* delegate_ptr = delegate.get(); 960 keygen_handler->set_crypto_module_delegate( 961 delegate.PassAs<crypto::NSSCryptoModuleDelegate>()); 962 963 base::Closure bound_callback = 964 base::Bind(callback, base::Passed(&keygen_handler)); 965 if (delegate_ptr->InitializeSlot(this, bound_callback)) { 966 // Initialization complete, run the callback synchronously. 967 bound_callback.Run(); 968 return; 969 } 970 // Otherwise, the InitializeSlot will run the callback asynchronously. 971#else 972 callback.Run(make_scoped_ptr( 973 new net::KeygenHandler(key_size_in_bits, challenge_string, url))); 974#endif 975} 976 977bool ProfileIOData::ResourceContext::AllowMicAccess(const GURL& origin) { 978 return AllowContentAccess(origin, CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC); 979} 980 981bool ProfileIOData::ResourceContext::AllowCameraAccess(const GURL& origin) { 982 return AllowContentAccess(origin, CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA); 983} 984 985bool ProfileIOData::ResourceContext::AllowContentAccess( 986 const GURL& origin, ContentSettingsType type) { 987 HostContentSettingsMap* content_settings = 988 io_data_->GetHostContentSettingsMap(); 989 ContentSetting setting = content_settings->GetContentSetting( 990 origin, origin, type, NO_RESOURCE_IDENTIFIER); 991 return setting == CONTENT_SETTING_ALLOW; 992} 993 994ResourceContext::SaltCallback 995ProfileIOData::ResourceContext::GetMediaDeviceIDSalt() { 996 return io_data_->GetMediaDeviceIDSalt(); 997} 998 999// static 1000std::string ProfileIOData::GetSSLSessionCacheShard() { 1001 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 1002 // The SSL session cache is partitioned by setting a string. This returns a 1003 // unique string to partition the SSL session cache. Each time we create a 1004 // new profile, we'll get a fresh SSL session cache which is separate from 1005 // the other profiles. 1006 static unsigned ssl_session_cache_instance = 0; 1007 return base::StringPrintf("profile/%u", ssl_session_cache_instance++); 1008} 1009 1010void ProfileIOData::Init( 1011 content::ProtocolHandlerMap* protocol_handlers, 1012 content::URLRequestInterceptorScopedVector request_interceptors) const { 1013 // The basic logic is implemented here. The specific initialization 1014 // is done in InitializeInternal(), implemented by subtypes. Static helper 1015 // functions have been provided to assist in common operations. 1016 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 1017 DCHECK(!initialized_); 1018 1019 startup_metric_utils::ScopedSlowStartupUMA 1020 scoped_timer("Startup.SlowStartupProfileIODataInit"); 1021 1022 // TODO(jhawkins): Remove once crbug.com/102004 is fixed. 1023 CHECK(initialized_on_UI_thread_); 1024 1025 // TODO(jhawkins): Return to DCHECK once crbug.com/102004 is fixed. 1026 CHECK(profile_params_.get()); 1027 1028 IOThread* const io_thread = profile_params_->io_thread; 1029 IOThread::Globals* const io_thread_globals = io_thread->globals(); 1030 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 1031 1032 // Create the common request contexts. 1033 main_request_context_.reset(new net::URLRequestContext()); 1034 extensions_request_context_.reset(new net::URLRequestContext()); 1035 1036 ChromeNetworkDelegate* network_delegate = 1037 new ChromeNetworkDelegate( 1038#if defined(ENABLE_EXTENSIONS) 1039 io_thread_globals->extension_event_router_forwarder.get(), 1040#else 1041 NULL, 1042#endif 1043 &enable_referrers_); 1044 network_delegate->set_data_reduction_proxy_params( 1045 io_thread_globals->data_reduction_proxy_params.get()); 1046 network_delegate->set_data_reduction_proxy_usage_stats( 1047 io_thread_globals->data_reduction_proxy_usage_stats.get()); 1048 network_delegate->set_data_reduction_proxy_auth_request_handler( 1049 io_thread_globals->data_reduction_proxy_auth_request_handler.get()); 1050 network_delegate->set_on_resolve_proxy_handler( 1051 io_thread_globals->on_resolve_proxy_handler); 1052 if (command_line.HasSwitch(switches::kEnableClientHints)) 1053 network_delegate->SetEnableClientHints(); 1054#if defined(ENABLE_EXTENSIONS) 1055 network_delegate->set_extension_info_map( 1056 profile_params_->extension_info_map.get()); 1057#endif 1058#if defined(ENABLE_CONFIGURATION_POLICY) 1059 network_delegate->set_url_blacklist_manager(url_blacklist_manager_.get()); 1060#endif 1061 network_delegate->set_profile(profile_params_->profile); 1062 network_delegate->set_profile_path(profile_params_->path); 1063 network_delegate->set_cookie_settings(profile_params_->cookie_settings.get()); 1064 network_delegate->set_enable_do_not_track(&enable_do_not_track_); 1065 network_delegate->set_force_google_safe_search(&force_safesearch_); 1066#if defined(OS_ANDROID) 1067 network_delegate->set_data_reduction_proxy_enabled_pref( 1068 &data_reduction_proxy_enabled_); 1069#endif 1070 network_delegate->set_prerender_tracker(profile_params_->prerender_tracker); 1071 network_delegate_.reset(network_delegate); 1072 1073 fraudulent_certificate_reporter_.reset( 1074 new chrome_browser_net::ChromeFraudulentCertificateReporter( 1075 main_request_context_.get())); 1076 1077 // NOTE: Proxy service uses the default io thread network delegate, not the 1078 // delegate just created. 1079 proxy_service_.reset( 1080 ProxyServiceFactory::CreateProxyService( 1081 io_thread->net_log(), 1082 io_thread_globals->proxy_script_fetcher_context.get(), 1083 io_thread_globals->system_network_delegate.get(), 1084 profile_params_->proxy_config_service.release(), 1085 command_line, 1086 quick_check_enabled_.GetValue())); 1087 1088 transport_security_state_.reset(new net::TransportSecurityState()); 1089 transport_security_persister_.reset( 1090 new net::TransportSecurityPersister( 1091 transport_security_state_.get(), 1092 profile_params_->path, 1093 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), 1094 IsOffTheRecord())); 1095 1096 // Take ownership over these parameters. 1097 cookie_settings_ = profile_params_->cookie_settings; 1098 host_content_settings_map_ = profile_params_->host_content_settings_map; 1099#if defined(ENABLE_EXTENSIONS) 1100 extension_info_map_ = profile_params_->extension_info_map; 1101#endif 1102 1103 resource_context_->host_resolver_ = io_thread_globals->host_resolver.get(); 1104 resource_context_->request_context_ = main_request_context_.get(); 1105 1106#if defined(ENABLE_MANAGED_USERS) 1107 supervised_user_url_filter_ = profile_params_->supervised_user_url_filter; 1108#endif 1109 1110#if defined(OS_CHROMEOS) 1111 username_hash_ = profile_params_->username_hash; 1112 use_system_key_slot_ = profile_params_->use_system_key_slot; 1113 if (use_system_key_slot_) 1114 EnableNSSSystemKeySlotForResourceContext(resource_context_.get()); 1115 1116 scoped_refptr<net::CertVerifyProc> verify_proc; 1117 crypto::ScopedPK11Slot public_slot = 1118 crypto::GetPublicSlotForChromeOSUser(username_hash_); 1119 // The private slot won't be ready by this point. It shouldn't be necessary 1120 // for cert trust purposes anyway. 1121 verify_proc = new chromeos::CertVerifyProcChromeOS(public_slot.Pass()); 1122 if (cert_verifier_) { 1123 cert_verifier_->InitializeOnIOThread(verify_proc); 1124 main_request_context_->set_cert_verifier(cert_verifier_.get()); 1125 } else { 1126 main_request_context_->set_cert_verifier( 1127 new net::MultiThreadedCertVerifier(verify_proc.get())); 1128 } 1129#else 1130 main_request_context_->set_cert_verifier( 1131 io_thread_globals->cert_verifier.get()); 1132#endif 1133 1134 InitializeInternal( 1135 profile_params_.get(), protocol_handlers, request_interceptors.Pass()); 1136 1137 profile_params_.reset(); 1138 initialized_ = true; 1139} 1140 1141void ProfileIOData::ApplyProfileParamsToContext( 1142 net::URLRequestContext* context) const { 1143 context->set_http_user_agent_settings( 1144 chrome_http_user_agent_settings_.get()); 1145 context->set_ssl_config_service(profile_params_->ssl_config_service.get()); 1146} 1147 1148scoped_ptr<net::URLRequestJobFactory> ProfileIOData::SetUpJobFactoryDefaults( 1149 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory, 1150 content::URLRequestInterceptorScopedVector request_interceptors, 1151 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> 1152 protocol_handler_interceptor, 1153 net::NetworkDelegate* network_delegate, 1154 net::FtpTransactionFactory* ftp_transaction_factory) const { 1155 // NOTE(willchan): Keep these protocol handlers in sync with 1156 // ProfileIOData::IsHandledProtocol(). 1157 bool set_protocol = job_factory->SetProtocolHandler( 1158 url::kFileScheme, 1159 new net::FileProtocolHandler( 1160 content::BrowserThread::GetBlockingPool()-> 1161 GetTaskRunnerWithShutdownBehavior( 1162 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))); 1163 DCHECK(set_protocol); 1164 1165#if defined(ENABLE_EXTENSIONS) 1166 DCHECK(extension_info_map_.get()); 1167 // Check only for incognito (and not Chrome OS guest mode GUEST_PROFILE). 1168 bool is_incognito = profile_type() == Profile::INCOGNITO_PROFILE; 1169 set_protocol = job_factory->SetProtocolHandler( 1170 extensions::kExtensionScheme, 1171 extensions::CreateExtensionProtocolHandler(is_incognito, 1172 extension_info_map_.get())); 1173 DCHECK(set_protocol); 1174 set_protocol = job_factory->SetProtocolHandler( 1175 extensions::kExtensionResourceScheme, 1176 CreateExtensionResourceProtocolHandler()); 1177 DCHECK(set_protocol); 1178#endif 1179 set_protocol = job_factory->SetProtocolHandler( 1180 url::kDataScheme, new net::DataProtocolHandler()); 1181 DCHECK(set_protocol); 1182#if defined(OS_CHROMEOS) 1183 if (profile_params_) { 1184 set_protocol = job_factory->SetProtocolHandler( 1185 chrome::kDriveScheme, 1186 new drive::DriveProtocolHandler(profile_params_->profile)); 1187 DCHECK(set_protocol); 1188 } 1189#endif // defined(OS_CHROMEOS) 1190 1191 job_factory->SetProtocolHandler( 1192 url::kAboutScheme, new chrome_browser_net::AboutProtocolHandler()); 1193#if !defined(DISABLE_FTP_SUPPORT) 1194 DCHECK(ftp_transaction_factory); 1195 job_factory->SetProtocolHandler( 1196 url::kFtpScheme, 1197 new net::FtpProtocolHandler(ftp_transaction_factory)); 1198#endif // !defined(DISABLE_FTP_SUPPORT) 1199 1200#if defined(DEBUG_DEVTOOLS) 1201 request_interceptors.push_back(new DebugDevToolsInterceptor); 1202#endif 1203 1204 // Set up interceptors in the reverse order. 1205 scoped_ptr<net::URLRequestJobFactory> top_job_factory = 1206 job_factory.PassAs<net::URLRequestJobFactory>(); 1207 for (content::URLRequestInterceptorScopedVector::reverse_iterator i = 1208 request_interceptors.rbegin(); 1209 i != request_interceptors.rend(); 1210 ++i) { 1211 top_job_factory.reset(new net::URLRequestInterceptingJobFactory( 1212 top_job_factory.Pass(), make_scoped_ptr(*i))); 1213 } 1214 request_interceptors.weak_clear(); 1215 1216 if (protocol_handler_interceptor) { 1217 protocol_handler_interceptor->Chain(top_job_factory.Pass()); 1218 return protocol_handler_interceptor.PassAs<net::URLRequestJobFactory>(); 1219 } else { 1220 return top_job_factory.Pass(); 1221 } 1222} 1223 1224void ProfileIOData::ShutdownOnUIThread() { 1225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1226 1227 if (signin_names_) 1228 signin_names_->ReleaseResourcesOnUIThread(); 1229 1230 google_services_user_account_id_.Destroy(); 1231 google_services_username_.Destroy(); 1232 google_services_username_pattern_.Destroy(); 1233 reverse_autologin_enabled_.Destroy(); 1234 one_click_signin_rejected_email_list_.Destroy(); 1235 enable_referrers_.Destroy(); 1236 enable_do_not_track_.Destroy(); 1237 force_safesearch_.Destroy(); 1238#if !defined(OS_CHROMEOS) 1239 enable_metrics_.Destroy(); 1240#endif 1241 safe_browsing_enabled_.Destroy(); 1242#if defined(OS_ANDROID) 1243 data_reduction_proxy_enabled_.Destroy(); 1244#endif 1245 printing_enabled_.Destroy(); 1246 sync_disabled_.Destroy(); 1247 signin_allowed_.Destroy(); 1248 // TODO(bnc): remove per https://crbug.com/334602. 1249 network_prediction_enabled_.Destroy(); 1250 network_prediction_options_.Destroy(); 1251 quick_check_enabled_.Destroy(); 1252 if (media_device_id_salt_) 1253 media_device_id_salt_->ShutdownOnUIThread(); 1254 session_startup_pref_.Destroy(); 1255#if defined(ENABLE_CONFIGURATION_POLICY) 1256 if (url_blacklist_manager_) 1257 url_blacklist_manager_->ShutdownOnUIThread(); 1258#endif 1259 if (chrome_http_user_agent_settings_) 1260 chrome_http_user_agent_settings_->CleanupOnUIThread(); 1261 incognito_availibility_pref_.Destroy(); 1262 bool posted = BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this); 1263 if (!posted) 1264 delete this; 1265} 1266 1267void ProfileIOData::set_channel_id_service( 1268 net::ChannelIDService* channel_id_service) const { 1269 channel_id_service_.reset(channel_id_service); 1270} 1271 1272void ProfileIOData::DestroyResourceContext() { 1273 resource_context_.reset(); 1274} 1275 1276scoped_ptr<net::HttpCache> ProfileIOData::CreateMainHttpFactory( 1277 const ProfileParams* profile_params, 1278 net::HttpCache::BackendFactory* main_backend) const { 1279 net::HttpNetworkSession::Params params; 1280 net::URLRequestContext* context = main_request_context(); 1281 1282 IOThread* const io_thread = profile_params->io_thread; 1283 1284 io_thread->InitializeNetworkSessionParams(¶ms); 1285 1286 params.host_resolver = context->host_resolver(); 1287 params.cert_verifier = context->cert_verifier(); 1288 params.channel_id_service = context->channel_id_service(); 1289 params.transport_security_state = context->transport_security_state(); 1290 params.cert_transparency_verifier = context->cert_transparency_verifier(); 1291 params.proxy_service = context->proxy_service(); 1292 params.ssl_session_cache_shard = GetSSLSessionCacheShard(); 1293 params.ssl_config_service = context->ssl_config_service(); 1294 params.http_auth_handler_factory = context->http_auth_handler_factory(); 1295 params.network_delegate = network_delegate(); 1296 params.http_server_properties = context->http_server_properties(); 1297 params.net_log = context->net_log(); 1298 1299 network_controller_.reset(new DevToolsNetworkController()); 1300 1301 net::HttpNetworkSession* session = new net::HttpNetworkSession(params); 1302 return scoped_ptr<net::HttpCache>(new net::HttpCache( 1303 new DevToolsNetworkTransactionFactory(network_controller_.get(), session), 1304 context->net_log(), main_backend)); 1305} 1306 1307scoped_ptr<net::HttpCache> ProfileIOData::CreateHttpFactory( 1308 net::HttpNetworkSession* shared_session, 1309 net::HttpCache::BackendFactory* backend) const { 1310 return scoped_ptr<net::HttpCache>(new net::HttpCache( 1311 new DevToolsNetworkTransactionFactory( 1312 network_controller_.get(), shared_session), 1313 shared_session->net_log(), backend)); 1314} 1315 1316void ProfileIOData::SetCookieSettingsForTesting( 1317 CookieSettings* cookie_settings) { 1318 DCHECK(!cookie_settings_.get()); 1319 cookie_settings_ = cookie_settings; 1320} 1321 1322void ProfileIOData::set_signin_names_for_testing( 1323 SigninNamesOnIOThread* signin_names) { 1324 signin_names_.reset(signin_names); 1325} 1326