Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / profiles / profile_io_data.cc
blob0963814dfe9779f2d6761433ad2ca2389c51b892
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/profiles/profile_io_data.h"
7 #include <string>
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/thread_task_runner_handle.h"
24 #include "base/threading/sequenced_worker_pool.h"
25 #include "chrome/browser/browser_process.h"
26 #include "chrome/browser/chrome_notification_types.h"
27 #include "chrome/browser/content_settings/cookie_settings_factory.h"
28 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
29 #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
30 #include "chrome/browser/devtools/devtools_network_controller.h"
31 #include "chrome/browser/devtools/devtools_network_transaction_factory.h"
32 #include "chrome/browser/download/download_service.h"
33 #include "chrome/browser/download/download_service_factory.h"
34 #include "chrome/browser/io_thread.h"
35 #include "chrome/browser/media/media_device_id_salt.h"
36 #include "chrome/browser/net/about_protocol_handler.h"
37 #include "chrome/browser/net/chrome_http_user_agent_settings.h"
38 #include "chrome/browser/net/chrome_net_log.h"
39 #include "chrome/browser/net/chrome_network_delegate.h"
40 #include "chrome/browser/net/chrome_url_request_context_getter.h"
41 #include "chrome/browser/net/cookie_store_util.h"
42 #include "chrome/browser/net/proxy_service_factory.h"
43 #include "chrome/browser/net/resource_prefetch_predictor_observer.h"
44 #include "chrome/browser/predictors/resource_prefetch_predictor.h"
45 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h"
46 #include "chrome/browser/profiles/profile.h"
47 #include "chrome/browser/profiles/profile_manager.h"
48 #include "chrome/browser/ssl/chrome_fraudulent_certificate_reporter.h"
49 #include "chrome/browser/ui/search/new_tab_page_interceptor_service.h"
50 #include "chrome/browser/ui/search/new_tab_page_interceptor_service_factory.h"
51 #include "chrome/common/chrome_paths.h"
52 #include "chrome/common/chrome_switches.h"
53 #include "chrome/common/pref_names.h"
54 #include "chrome/common/url_constants.h"
55 #include "components/content_settings/core/browser/content_settings_provider.h"
56 #include "components/content_settings/core/browser/cookie_settings.h"
57 #include "components/content_settings/core/browser/host_content_settings_map.h"
58 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h"
59 #include "components/dom_distiller/core/url_constants.h"
60 #include "components/sync_driver/pref_names.h"
61 #include "components/url_fixer/url_fixer.h"
62 #include "content/public/browser/browser_thread.h"
63 #include "content/public/browser/host_zoom_map.h"
64 #include "content/public/browser/notification_service.h"
65 #include "content/public/browser/resource_context.h"
66 #include "net/base/keygen_handler.h"
67 #include "net/cookies/canonical_cookie.h"
68 #include "net/http/http_transaction_factory.h"
69 #include "net/http/http_util.h"
70 #include "net/http/transport_security_persister.h"
71 #include "net/proxy/proxy_config_service_fixed.h"
72 #include "net/proxy/proxy_script_fetcher_impl.h"
73 #include "net/proxy/proxy_service.h"
74 #include "net/ssl/channel_id_service.h"
75 #include "net/ssl/client_cert_store.h"
76 #include "net/url_request/data_protocol_handler.h"
77 #include "net/url_request/file_protocol_handler.h"
78 #include "net/url_request/ftp_protocol_handler.h"
79 #include "net/url_request/url_request.h"
80 #include "net/url_request/url_request_context.h"
81 #include "net/url_request/url_request_file_job.h"
82 #include "net/url_request/url_request_intercepting_job_factory.h"
83 #include "net/url_request/url_request_interceptor.h"
84 #include "net/url_request/url_request_job_factory_impl.h"
86 #if defined(ENABLE_CONFIGURATION_POLICY)
87 #include "chrome/browser/policy/cloud/policy_header_service_factory.h"
88 #include "chrome/browser/policy/policy_helpers.h"
89 #include "components/policy/core/browser/url_blacklist_manager.h"
90 #include "components/policy/core/common/cloud/policy_header_io_helper.h"
91 #include "components/policy/core/common/cloud/policy_header_service.h"
92 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
93 #endif
95 #if defined(ENABLE_EXTENSIONS)
96 #include "chrome/browser/extensions/extension_resource_protocols.h"
97 #include "extensions/browser/extension_protocols.h"
98 #include "extensions/browser/extension_system.h"
99 #include "extensions/browser/extension_throttle_manager.h"
100 #include "extensions/browser/info_map.h"
101 #include "extensions/common/constants.h"
102 #endif
104 #if defined(ENABLE_SUPERVISED_USERS)
105 #include "chrome/browser/supervised_user/supervised_user_service.h"
106 #include "chrome/browser/supervised_user/supervised_user_service_factory.h"
107 #include "chrome/browser/supervised_user/supervised_user_url_filter.h"
108 #endif
110 #if defined(OS_ANDROID)
111 #include "content/public/browser/android/content_protocol_handler.h"
112 #endif // defined(OS_ANDROID)
114 #if defined(OS_CHROMEOS)
115 #include "chrome/browser/chromeos/fileapi/external_file_protocol_handler.h"
116 #include "chrome/browser/chromeos/login/startup_utils.h"
117 #include "chrome/browser/chromeos/net/cert_verify_proc_chromeos.h"
118 #include "chrome/browser/chromeos/net/client_cert_filter_chromeos.h"
119 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
120 #include "chrome/browser/chromeos/policy/policy_cert_service.h"
121 #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h"
122 #include "chrome/browser/chromeos/policy/policy_cert_verifier.h"
123 #include "chrome/browser/chromeos/profiles/profile_helper.h"
124 #include "chrome/browser/chromeos/settings/cros_settings.h"
125 #include "chrome/browser/net/nss_context.h"
126 #include "chromeos/dbus/dbus_thread_manager.h"
127 #include "chromeos/settings/cros_settings_names.h"
128 #include "chromeos/tpm/tpm_token_info_getter.h"
129 #include "components/user_manager/user.h"
130 #include "components/user_manager/user_manager.h"
131 #include "crypto/nss_util.h"
132 #include "crypto/nss_util_internal.h"
133 #include "net/cert/cert_verifier.h"
134 #include "net/cert/multi_threaded_cert_verifier.h"
135 #include "net/ssl/client_cert_store_chromeos.h"
136 #endif // defined(OS_CHROMEOS)
138 #if defined(USE_NSS_CERTS)
139 #include "chrome/browser/ui/crypto_module_delegate_nss.h"
140 #include "net/ssl/client_cert_store_nss.h"
141 #endif
143 #if defined(OS_WIN)
144 #include "net/ssl/client_cert_store_win.h"
145 #endif
147 #if defined(OS_MACOSX)
148 #include "net/ssl/client_cert_store_mac.h"
149 #endif
151 using content::BrowserContext;
152 using content::BrowserThread;
153 using content::ResourceContext;
155 namespace {
157 #if defined(DEBUG_DEVTOOLS)
158 bool IsSupportedDevToolsURL(const GURL& url, base::FilePath* path) {
159 std::string bundled_path_prefix(chrome::kChromeUIDevToolsBundledPath);
160 bundled_path_prefix = "/" + bundled_path_prefix + "/";
162 if (!url.SchemeIs(content::kChromeDevToolsScheme) ||
163 url.host() != chrome::kChromeUIDevToolsHost ||
164 !base::StartsWith(url.path(), bundled_path_prefix,
165 base::CompareCase::INSENSITIVE_ASCII)) {
166 return false;
169 if (!url.is_valid()) {
170 NOTREACHED();
171 return false;
174 // Remove Query and Ref from URL.
175 GURL stripped_url;
176 GURL::Replacements replacements;
177 replacements.ClearQuery();
178 replacements.ClearRef();
179 stripped_url = url.ReplaceComponents(replacements);
181 std::string relative_path;
182 const std::string& spec = stripped_url.possibly_invalid_spec();
183 const url::Parsed& parsed = stripped_url.parsed_for_possibly_invalid_spec();
184 int offset = parsed.CountCharactersBefore(url::Parsed::PATH, false);
185 if (offset < static_cast<int>(spec.size()))
186 relative_path.assign(spec.substr(offset + bundled_path_prefix.length()));
188 // Check that |relative_path| is not an absolute path (otherwise
189 // AppendASCII() will DCHECK). The awkward use of StringType is because on
190 // some systems FilePath expects a std::string, but on others a std::wstring.
191 base::FilePath p(
192 base::FilePath::StringType(relative_path.begin(), relative_path.end()));
193 if (p.IsAbsolute())
194 return false;
196 base::FilePath inspector_dir;
197 if (!PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir))
198 return false;
200 if (inspector_dir.empty())
201 return false;
203 *path = inspector_dir.AppendASCII(relative_path);
204 return true;
207 class DebugDevToolsInterceptor : public net::URLRequestInterceptor {
208 public:
209 // net::URLRequestInterceptor implementation.
210 net::URLRequestJob* MaybeInterceptRequest(
211 net::URLRequest* request,
212 net::NetworkDelegate* network_delegate) const override {
213 base::FilePath path;
214 if (IsSupportedDevToolsURL(request->url(), &path))
215 return new net::URLRequestFileJob(
216 request, network_delegate, path,
217 content::BrowserThread::GetBlockingPool()->
218 GetTaskRunnerWithShutdownBehavior(
219 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
221 return NULL;
224 #endif // defined(DEBUG_DEVTOOLS)
226 #if defined(OS_CHROMEOS)
227 // The following four functions are responsible for initializing NSS for each
228 // profile on ChromeOS, which has a separate NSS database and TPM slot
229 // per-profile.
231 // Initialization basically follows these steps:
232 // 1) Get some info from user_manager::UserManager about the User for this
233 // profile.
234 // 2) Tell nss_util to initialize the software slot for this profile.
235 // 3) Wait for the TPM module to be loaded by nss_util if it isn't already.
236 // 4) Ask CryptohomeClient which TPM slot id corresponds to this profile.
237 // 5) Tell nss_util to use that slot id on the TPM module.
239 // Some of these steps must happen on the UI thread, others must happen on the
240 // IO thread:
241 // UI thread IO Thread
243 // ProfileIOData::InitializeOnUIThread
244 // |
245 // ProfileHelper::Get()->GetUserByProfile()
246 // \---------------------------------------v
247 // StartNSSInitOnIOThread
248 // |
249 // crypto::InitializeNSSForChromeOSUser
250 // |
251 // crypto::IsTPMTokenReady
252 // |
253 // StartTPMSlotInitializationOnIOThread
254 // v---------------------------------------/
255 // GetTPMInfoForUserOnUIThread
256 // |
257 // chromeos::TPMTokenInfoGetter::Start
258 // |
259 // DidGetTPMInfoForUserOnUIThread
260 // \---------------------------------------v
261 // crypto::InitializeTPMForChromeOSUser
263 void DidGetTPMInfoForUserOnUIThread(
264 scoped_ptr<chromeos::TPMTokenInfoGetter> getter,
265 const std::string& username_hash,
266 const chromeos::TPMTokenInfo& info) {
267 DCHECK_CURRENTLY_ON(BrowserThread::UI);
268 if (info.tpm_is_enabled && info.token_slot_id != -1) {
269 DVLOG(1) << "Got TPM slot for " << username_hash << ": "
270 << info.token_slot_id;
271 BrowserThread::PostTask(
272 BrowserThread::IO,
273 FROM_HERE,
274 base::Bind(&crypto::InitializeTPMForChromeOSUser,
275 username_hash, info.token_slot_id));
276 } else {
277 NOTREACHED() << "TPMTokenInfoGetter reported invalid token.";
281 void GetTPMInfoForUserOnUIThread(const std::string& username,
282 const std::string& username_hash) {
283 DCHECK_CURRENTLY_ON(BrowserThread::UI);
284 DVLOG(1) << "Getting TPM info from cryptohome for "
285 << " " << username << " " << username_hash;
286 scoped_ptr<chromeos::TPMTokenInfoGetter> scoped_token_info_getter =
287 chromeos::TPMTokenInfoGetter::CreateForUserToken(
288 username,
289 chromeos::DBusThreadManager::Get()->GetCryptohomeClient(),
290 base::ThreadTaskRunnerHandle::Get());
291 chromeos::TPMTokenInfoGetter* token_info_getter =
292 scoped_token_info_getter.get();
294 // Bind |token_info_getter| to the callback to ensure it does not go away
295 // before TPM token info is fetched.
296 // TODO(tbarzic, pneubeck): Handle this in a nicer way when this logic is
297 // moved to a separate profile service.
298 token_info_getter->Start(
299 base::Bind(&DidGetTPMInfoForUserOnUIThread,
300 base::Passed(&scoped_token_info_getter),
301 username_hash));
304 void StartTPMSlotInitializationOnIOThread(const std::string& username,
305 const std::string& username_hash) {
306 DCHECK_CURRENTLY_ON(BrowserThread::IO);
308 BrowserThread::PostTask(
309 BrowserThread::UI,
310 FROM_HERE,
311 base::Bind(&GetTPMInfoForUserOnUIThread, username, username_hash));
314 void StartNSSInitOnIOThread(const std::string& username,
315 const std::string& username_hash,
316 const base::FilePath& path) {
317 DCHECK_CURRENTLY_ON(BrowserThread::IO);
318 DVLOG(1) << "Starting NSS init for " << username
319 << " hash:" << username_hash;
321 // Make sure NSS is initialized for the user.
322 crypto::InitializeNSSForChromeOSUser(username_hash, path);
324 // Check if it's OK to initialize TPM for the user before continuing. This
325 // may not be the case if the TPM slot initialization was previously
326 // requested for the same user.
327 if (!crypto::ShouldInitializeTPMForChromeOSUser(username_hash))
328 return;
330 crypto::WillInitializeTPMForChromeOSUser(username_hash);
332 if (crypto::IsTPMTokenEnabledForNSS()) {
333 if (crypto::IsTPMTokenReady(base::Bind(
334 &StartTPMSlotInitializationOnIOThread, username, username_hash))) {
335 StartTPMSlotInitializationOnIOThread(username, username_hash);
336 } else {
337 DVLOG(1) << "Waiting for tpm ready ...";
339 } else {
340 crypto::InitializePrivateSoftwareSlotForChromeOSUser(username_hash);
343 #endif // defined(OS_CHROMEOS)
345 #if defined(USE_NSS_CERTS)
346 void InitializeAndPassKeygenHandler(
347 scoped_ptr<net::KeygenHandler> keygen_handler,
348 const base::Callback<void(scoped_ptr<net::KeygenHandler>)>& callback,
349 scoped_ptr<ChromeNSSCryptoModuleDelegate> delegate) {
350 if (delegate)
351 keygen_handler->set_crypto_module_delegate(delegate.Pass());
352 callback.Run(keygen_handler.Pass());
354 #endif // defined(USE_NSS_CERTS)
356 // For safe shutdown, must be called before the ProfileIOData is destroyed.
357 void NotifyContextGettersOfShutdownOnIO(
358 scoped_ptr<ProfileIOData::ChromeURLRequestContextGetterVector> getters) {
359 DCHECK_CURRENTLY_ON(BrowserThread::IO);
360 ProfileIOData::ChromeURLRequestContextGetterVector::iterator iter;
361 for (auto& chrome_context_getter : *getters)
362 chrome_context_getter->NotifyContextShuttingDown();
365 } // namespace
367 void ProfileIOData::InitializeOnUIThread(Profile* profile) {
368 DCHECK_CURRENTLY_ON(BrowserThread::UI);
369 PrefService* pref_service = profile->GetPrefs();
370 PrefService* local_state_pref_service = g_browser_process->local_state();
372 scoped_ptr<ProfileParams> params(new ProfileParams);
373 params->path = profile->GetPath();
375 params->io_thread = g_browser_process->io_thread();
377 params->cookie_settings = CookieSettingsFactory::GetForProfile(profile);
378 params->host_content_settings_map = profile->GetHostContentSettingsMap();
379 params->ssl_config_service = profile->GetSSLConfigService();
380 params->cookie_monster_delegate =
381 chrome_browser_net::CreateCookieDelegate(profile);
382 #if defined(ENABLE_EXTENSIONS)
383 params->extension_info_map =
384 extensions::ExtensionSystem::Get(profile)->info_map();
385 #endif
387 if (predictors::ResourcePrefetchPredictor* predictor =
388 predictors::ResourcePrefetchPredictorFactory::GetForProfile(
389 profile)) {
390 resource_prefetch_predictor_observer_.reset(
391 new chrome_browser_net::ResourcePrefetchPredictorObserver(predictor));
394 ProtocolHandlerRegistry* protocol_handler_registry =
395 ProtocolHandlerRegistryFactory::GetForBrowserContext(profile);
396 DCHECK(protocol_handler_registry);
398 // The profile instance is only available here in the InitializeOnUIThread
399 // method, so we create the url job factory here, then save it for
400 // later delivery to the job factory in Init().
401 params->protocol_handler_interceptor =
402 protocol_handler_registry->CreateJobInterceptorFactory();
404 NewTabPageInterceptorService* new_tab_interceptor_service =
405 NewTabPageInterceptorServiceFactory::GetForProfile(profile);
406 if (new_tab_interceptor_service) {
407 params->new_tab_page_interceptor =
408 new_tab_interceptor_service->CreateInterceptor();
411 params->proxy_config_service
412 .reset(ProxyServiceFactory::CreateProxyConfigService(
413 profile->GetProxyConfigTracker()));
414 #if defined(ENABLE_SUPERVISED_USERS)
415 SupervisedUserService* supervised_user_service =
416 SupervisedUserServiceFactory::GetForProfile(profile);
417 params->supervised_user_url_filter =
418 supervised_user_service->GetURLFilterForIOThread();
419 #endif
420 #if defined(OS_CHROMEOS)
421 user_manager::UserManager* user_manager = user_manager::UserManager::Get();
422 if (user_manager) {
423 const user_manager::User* user =
424 chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
425 // No need to initialize NSS for users with empty username hash:
426 // Getters for a user's NSS slots always return NULL slot if the user's
427 // username hash is empty, even when the NSS is not initialized for the
428 // user.
429 if (user && !user->username_hash().empty()) {
430 params->username_hash = user->username_hash();
431 DCHECK(!params->username_hash.empty());
432 BrowserThread::PostTask(BrowserThread::IO,
433 FROM_HERE,
434 base::Bind(&StartNSSInitOnIOThread,
435 user->email(),
436 user->username_hash(),
437 profile->GetPath()));
439 // Use the device-wide system key slot only if the user is of the same
440 // domain as the device is registered to.
441 policy::BrowserPolicyConnectorChromeOS* connector =
442 g_browser_process->platform_part()
443 ->browser_policy_connector_chromeos();
444 params->use_system_key_slot =
445 connector->GetUserAffiliation(user->email()) ==
446 policy::USER_AFFILIATION_MANAGED;
449 #endif
451 params->profile = profile;
452 profile_params_.reset(params.release());
454 ChromeNetworkDelegate::InitializePrefsOnUIThread(
455 &enable_referrers_,
456 &enable_do_not_track_,
457 &force_google_safesearch_,
458 &force_youtube_safety_mode_,
459 pref_service);
461 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner =
462 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
464 chrome_http_user_agent_settings_.reset(
465 new ChromeHttpUserAgentSettings(pref_service));
467 // These members are used only for sign in, which is not enabled
468 // in incognito mode. So no need to initialize them.
469 if (!IsOffTheRecord()) {
470 google_services_user_account_id_.Init(
471 prefs::kGoogleServicesUserAccountId, pref_service);
472 google_services_user_account_id_.MoveToThread(io_task_runner);
474 sync_disabled_.Init(sync_driver::prefs::kSyncManaged, pref_service);
475 sync_disabled_.MoveToThread(io_task_runner);
477 signin_allowed_.Init(prefs::kSigninAllowed, pref_service);
478 signin_allowed_.MoveToThread(io_task_runner);
481 quick_check_enabled_.Init(prefs::kQuickCheckEnabled,
482 local_state_pref_service);
483 quick_check_enabled_.MoveToThread(io_task_runner);
485 media_device_id_salt_ = new MediaDeviceIDSalt(pref_service, IsOffTheRecord());
487 network_prediction_options_.Init(prefs::kNetworkPredictionOptions,
488 pref_service);
490 network_prediction_options_.MoveToThread(io_task_runner);
492 #if defined(OS_CHROMEOS)
493 scoped_ptr<policy::PolicyCertVerifier> verifier =
494 policy::PolicyCertServiceFactory::CreateForProfile(profile);
495 policy_cert_verifier_ = verifier.get();
496 cert_verifier_ = verifier.Pass();
497 #endif
498 // The URLBlacklistManager has to be created on the UI thread to register
499 // observers of |pref_service|, and it also has to clean up on
500 // ShutdownOnUIThread to release these observers on the right thread.
501 // Don't pass it in |profile_params_| to make sure it is correctly cleaned up,
502 // in particular when this ProfileIOData isn't |initialized_| during deletion.
503 #if defined(ENABLE_CONFIGURATION_POLICY)
504 policy::URLBlacklist::SegmentURLCallback callback =
505 static_cast<policy::URLBlacklist::SegmentURLCallback>(
506 url_fixer::SegmentURL);
507 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
508 scoped_refptr<base::SequencedTaskRunner> background_task_runner =
509 pool->GetSequencedTaskRunner(pool->GetSequenceToken());
510 url_blacklist_manager_.reset(new policy::URLBlacklistManager(
511 pref_service, background_task_runner, io_task_runner, callback,
512 base::Bind(policy::OverrideBlacklistForURL)));
514 if (!IsOffTheRecord()) {
515 // Add policy headers for non-incognito requests.
516 policy::PolicyHeaderService* policy_header_service =
517 policy::PolicyHeaderServiceFactory::GetForBrowserContext(profile);
518 if (policy_header_service) {
519 policy_header_helper_ =
520 policy_header_service->CreatePolicyHeaderIOHelper(io_task_runner);
523 #endif
525 incognito_availibility_pref_.Init(
526 prefs::kIncognitoModeAvailability, pref_service);
527 incognito_availibility_pref_.MoveToThread(io_task_runner);
529 initialized_on_UI_thread_ = true;
531 // We need to make sure that content initializes its own data structures that
532 // are associated with each ResourceContext because we might post this
533 // object to the IO thread after this function.
534 BrowserContext::EnsureResourceContextInitialized(profile);
537 ProfileIOData::MediaRequestContext::MediaRequestContext() {
540 void ProfileIOData::MediaRequestContext::SetHttpTransactionFactory(
541 scoped_ptr<net::HttpTransactionFactory> http_factory) {
542 http_factory_ = http_factory.Pass();
543 set_http_transaction_factory(http_factory_.get());
546 ProfileIOData::MediaRequestContext::~MediaRequestContext() {
547 AssertNoURLRequests();
550 ProfileIOData::AppRequestContext::AppRequestContext() {
553 void ProfileIOData::AppRequestContext::SetCookieStore(
554 net::CookieStore* cookie_store) {
555 cookie_store_ = cookie_store;
556 set_cookie_store(cookie_store);
559 void ProfileIOData::AppRequestContext::SetHttpTransactionFactory(
560 scoped_ptr<net::HttpTransactionFactory> http_factory) {
561 http_factory_ = http_factory.Pass();
562 set_http_transaction_factory(http_factory_.get());
565 void ProfileIOData::AppRequestContext::SetJobFactory(
566 scoped_ptr<net::URLRequestJobFactory> job_factory) {
567 job_factory_ = job_factory.Pass();
568 set_job_factory(job_factory_.get());
571 ProfileIOData::AppRequestContext::~AppRequestContext() {
572 AssertNoURLRequests();
575 ProfileIOData::ProfileParams::ProfileParams()
576 : io_thread(NULL),
577 #if defined(OS_CHROMEOS)
578 use_system_key_slot(false),
579 #endif
580 profile(NULL) {
583 ProfileIOData::ProfileParams::~ProfileParams() {}
585 ProfileIOData::ProfileIOData(Profile::ProfileType profile_type)
586 : initialized_(false),
587 #if defined(OS_CHROMEOS)
588 policy_cert_verifier_(NULL),
589 use_system_key_slot_(false),
590 #endif
591 resource_context_(new ResourceContext(this)),
592 initialized_on_UI_thread_(false),
593 profile_type_(profile_type) {
594 DCHECK_CURRENTLY_ON(BrowserThread::UI);
597 ProfileIOData::~ProfileIOData() {
598 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO))
599 DCHECK_CURRENTLY_ON(BrowserThread::IO);
601 // Pull the contents of the request context maps onto the stack for sanity
602 // checking of values in a minidump. http://crbug.com/260425
603 size_t num_app_contexts = app_request_context_map_.size();
604 size_t num_media_contexts = isolated_media_request_context_map_.size();
605 size_t current_context = 0;
606 static const size_t kMaxCachedContexts = 20;
607 net::URLRequestContext* app_context_cache[kMaxCachedContexts] = {0};
608 void* app_context_vtable_cache[kMaxCachedContexts] = {0};
609 net::URLRequestContext* media_context_cache[kMaxCachedContexts] = {0};
610 void* media_context_vtable_cache[kMaxCachedContexts] = {0};
611 void* tmp_vtable = NULL;
612 base::debug::Alias(&num_app_contexts);
613 base::debug::Alias(&num_media_contexts);
614 base::debug::Alias(&current_context);
615 base::debug::Alias(app_context_cache);
616 base::debug::Alias(app_context_vtable_cache);
617 base::debug::Alias(media_context_cache);
618 base::debug::Alias(media_context_vtable_cache);
619 base::debug::Alias(&tmp_vtable);
621 current_context = 0;
622 for (URLRequestContextMap::const_iterator it =
623 app_request_context_map_.begin();
624 current_context < kMaxCachedContexts &&
625 it != app_request_context_map_.end();
626 ++it, ++current_context) {
627 app_context_cache[current_context] = it->second;
628 memcpy(&app_context_vtable_cache[current_context],
629 static_cast<void*>(it->second), sizeof(void*));
632 current_context = 0;
633 for (URLRequestContextMap::const_iterator it =
634 isolated_media_request_context_map_.begin();
635 current_context < kMaxCachedContexts &&
636 it != isolated_media_request_context_map_.end();
637 ++it, ++current_context) {
638 media_context_cache[current_context] = it->second;
639 memcpy(&media_context_vtable_cache[current_context],
640 static_cast<void*>(it->second), sizeof(void*));
643 // TODO(ajwong): These AssertNoURLRequests() calls are unnecessary since they
644 // are already done in the URLRequestContext destructor.
645 if (main_request_context_)
646 main_request_context_->AssertNoURLRequests();
647 if (extensions_request_context_)
648 extensions_request_context_->AssertNoURLRequests();
650 current_context = 0;
651 for (URLRequestContextMap::iterator it = app_request_context_map_.begin();
652 it != app_request_context_map_.end(); ++it) {
653 if (current_context < kMaxCachedContexts) {
654 CHECK_EQ(app_context_cache[current_context], it->second);
655 memcpy(&tmp_vtable, static_cast<void*>(it->second), sizeof(void*));
656 CHECK_EQ(app_context_vtable_cache[current_context], tmp_vtable);
658 it->second->AssertNoURLRequests();
659 delete it->second;
660 current_context++;
663 current_context = 0;
664 for (URLRequestContextMap::iterator it =
665 isolated_media_request_context_map_.begin();
666 it != isolated_media_request_context_map_.end(); ++it) {
667 if (current_context < kMaxCachedContexts) {
668 CHECK_EQ(media_context_cache[current_context], it->second);
669 memcpy(&tmp_vtable, static_cast<void*>(it->second), sizeof(void*));
670 CHECK_EQ(media_context_vtable_cache[current_context], tmp_vtable);
672 it->second->AssertNoURLRequests();
673 delete it->second;
674 current_context++;
678 // static
679 ProfileIOData* ProfileIOData::FromResourceContext(
680 content::ResourceContext* rc) {
681 return (static_cast<ResourceContext*>(rc))->io_data_;
684 // static
685 bool ProfileIOData::IsHandledProtocol(const std::string& scheme) {
686 DCHECK_EQ(scheme, base::StringToLowerASCII(scheme));
687 static const char* const kProtocolList[] = {
688 url::kFileScheme,
689 content::kChromeDevToolsScheme,
690 dom_distiller::kDomDistillerScheme,
691 #if defined(ENABLE_EXTENSIONS)
692 extensions::kExtensionScheme,
693 extensions::kExtensionResourceScheme,
694 #endif
695 content::kChromeUIScheme,
696 url::kDataScheme,
697 #if defined(OS_CHROMEOS)
698 content::kExternalFileScheme,
699 #endif // defined(OS_CHROMEOS)
700 url::kAboutScheme,
701 #if !defined(DISABLE_FTP_SUPPORT)
702 url::kFtpScheme,
703 #endif // !defined(DISABLE_FTP_SUPPORT)
704 url::kBlobScheme,
705 url::kFileSystemScheme,
706 chrome::kChromeSearchScheme,
708 for (size_t i = 0; i < arraysize(kProtocolList); ++i) {
709 if (scheme == kProtocolList[i])
710 return true;
712 return net::URLRequest::IsHandledProtocol(scheme);
715 // static
716 bool ProfileIOData::IsHandledURL(const GURL& url) {
717 if (!url.is_valid()) {
718 // We handle error cases.
719 return true;
722 return IsHandledProtocol(url.scheme());
725 // static
726 void ProfileIOData::InstallProtocolHandlers(
727 net::URLRequestJobFactoryImpl* job_factory,
728 content::ProtocolHandlerMap* protocol_handlers) {
729 for (content::ProtocolHandlerMap::iterator it =
730 protocol_handlers->begin();
731 it != protocol_handlers->end();
732 ++it) {
733 bool set_protocol = job_factory->SetProtocolHandler(
734 it->first, it->second.release());
735 DCHECK(set_protocol);
737 protocol_handlers->clear();
740 content::ResourceContext* ProfileIOData::GetResourceContext() const {
741 return resource_context_.get();
744 net::URLRequestContext* ProfileIOData::GetMainRequestContext() const {
745 DCHECK(initialized_);
746 return main_request_context_.get();
749 net::URLRequestContext* ProfileIOData::GetMediaRequestContext() const {
750 DCHECK(initialized_);
751 net::URLRequestContext* context = AcquireMediaRequestContext();
752 DCHECK(context);
753 return context;
756 net::URLRequestContext* ProfileIOData::GetExtensionsRequestContext() const {
757 DCHECK(initialized_);
758 return extensions_request_context_.get();
761 net::URLRequestContext* ProfileIOData::GetIsolatedAppRequestContext(
762 net::URLRequestContext* main_context,
763 const StoragePartitionDescriptor& partition_descriptor,
764 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
765 protocol_handler_interceptor,
766 content::ProtocolHandlerMap* protocol_handlers,
767 content::URLRequestInterceptorScopedVector request_interceptors) const {
768 DCHECK(initialized_);
769 net::URLRequestContext* context = NULL;
770 if (ContainsKey(app_request_context_map_, partition_descriptor)) {
771 context = app_request_context_map_[partition_descriptor];
772 } else {
773 context =
774 AcquireIsolatedAppRequestContext(main_context,
775 partition_descriptor,
776 protocol_handler_interceptor.Pass(),
777 protocol_handlers,
778 request_interceptors.Pass());
779 app_request_context_map_[partition_descriptor] = context;
781 DCHECK(context);
782 return context;
785 net::URLRequestContext* ProfileIOData::GetIsolatedMediaRequestContext(
786 net::URLRequestContext* app_context,
787 const StoragePartitionDescriptor& partition_descriptor) const {
788 DCHECK(initialized_);
789 net::URLRequestContext* context = NULL;
790 if (ContainsKey(isolated_media_request_context_map_, partition_descriptor)) {
791 context = isolated_media_request_context_map_[partition_descriptor];
792 } else {
793 context = AcquireIsolatedMediaRequestContext(app_context,
794 partition_descriptor);
795 isolated_media_request_context_map_[partition_descriptor] = context;
797 DCHECK(context);
798 return context;
801 extensions::InfoMap* ProfileIOData::GetExtensionInfoMap() const {
802 DCHECK(initialized_) << "ExtensionSystem not initialized";
803 #if defined(ENABLE_EXTENSIONS)
804 return extension_info_map_.get();
805 #else
806 return nullptr;
807 #endif
810 extensions::ExtensionThrottleManager*
811 ProfileIOData::GetExtensionThrottleManager() const {
812 DCHECK(initialized_) << "ExtensionSystem not initialized";
813 #if defined(ENABLE_EXTENSIONS)
814 return extension_throttle_manager_.get();
815 #else
816 return nullptr;
817 #endif
820 content_settings::CookieSettings* ProfileIOData::GetCookieSettings() const {
821 // Allow either Init() or SetCookieSettingsForTesting() to initialize.
822 DCHECK(initialized_ || cookie_settings_.get());
823 return cookie_settings_.get();
826 HostContentSettingsMap* ProfileIOData::GetHostContentSettingsMap() const {
827 DCHECK(initialized_);
828 return host_content_settings_map_.get();
831 ResourceContext::SaltCallback ProfileIOData::GetMediaDeviceIDSalt() const {
832 return base::Bind(&MediaDeviceIDSalt::GetSalt, media_device_id_salt_);
835 bool ProfileIOData::IsOffTheRecord() const {
836 return profile_type() == Profile::INCOGNITO_PROFILE
837 || profile_type() == Profile::GUEST_PROFILE;
840 void ProfileIOData::InitializeMetricsEnabledStateOnUIThread() {
841 DCHECK_CURRENTLY_ON(BrowserThread::UI);
842 #if defined(OS_CHROMEOS)
843 // Just fetch the value from ChromeOS' settings while we're on the UI thread.
844 // TODO(stevet): For now, this value is only set on profile initialization.
845 // We will want to do something similar to the PrefMember method below in the
846 // future to more accurately capture this state.
847 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref,
848 &enable_metrics_);
849 #elif defined(OS_ANDROID)
850 // TODO(dwkang): rename or unify the pref for UMA once we have conclusion
851 // in crbugs.com/246495.
852 // Android has it's own preferences for metrics / crash uploading.
853 enable_metrics_.Init(prefs::kCrashReportingEnabled,
854 g_browser_process->local_state());
855 enable_metrics_.MoveToThread(
856 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
857 #else
858 // Prep the PrefMember and send it to the IO thread, since this value will be
859 // read from there.
860 enable_metrics_.Init(prefs::kMetricsReportingEnabled,
861 g_browser_process->local_state());
862 enable_metrics_.MoveToThread(
863 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
864 #endif // defined(OS_CHROMEOS)
867 bool ProfileIOData::GetMetricsEnabledStateOnIOThread() const {
868 DCHECK_CURRENTLY_ON(BrowserThread::IO);
869 #if defined(OS_CHROMEOS)
870 return enable_metrics_;
871 #else
872 return enable_metrics_.GetValue();
873 #endif // defined(OS_CHROMEOS)
876 bool ProfileIOData::IsDataReductionProxyEnabled() const {
877 return data_reduction_proxy_io_data() &&
878 data_reduction_proxy_io_data()->IsEnabled();
881 void ProfileIOData::set_data_reduction_proxy_io_data(
882 scoped_ptr<data_reduction_proxy::DataReductionProxyIOData>
883 data_reduction_proxy_io_data) const {
884 data_reduction_proxy_io_data_ = data_reduction_proxy_io_data.Pass();
887 base::WeakPtr<net::HttpServerProperties>
888 ProfileIOData::http_server_properties() const {
889 return http_server_properties_->GetWeakPtr();
892 void ProfileIOData::set_http_server_properties(
893 scoped_ptr<net::HttpServerProperties> http_server_properties) const {
894 http_server_properties_ = http_server_properties.Pass();
897 ProfileIOData::ResourceContext::ResourceContext(ProfileIOData* io_data)
898 : io_data_(io_data),
899 host_resolver_(NULL),
900 request_context_(NULL) {
901 DCHECK(io_data);
904 ProfileIOData::ResourceContext::~ResourceContext() {}
906 net::HostResolver* ProfileIOData::ResourceContext::GetHostResolver() {
907 DCHECK_CURRENTLY_ON(BrowserThread::IO);
908 DCHECK(io_data_->initialized_);
909 return host_resolver_;
912 net::URLRequestContext* ProfileIOData::ResourceContext::GetRequestContext() {
913 DCHECK_CURRENTLY_ON(BrowserThread::IO);
914 DCHECK(io_data_->initialized_);
915 return request_context_;
918 scoped_ptr<net::ClientCertStore>
919 ProfileIOData::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 make_scoped_ptr(new chromeos::ClientCertFilterChromeOS(
925 io_data_->use_system_key_slot(), io_data_->username_hash())),
926 base::Bind(&CreateCryptoModuleBlockingPasswordDelegate,
927 chrome::kCryptoModulePasswordClientAuth)));
928 #elif defined(USE_NSS_CERTS)
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
946 void 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_CERTS)
953 scoped_ptr<net::KeygenHandler> keygen_handler(
954 new net::KeygenHandler(key_size_in_bits, challenge_string, url));
956 base::Callback<void(scoped_ptr<ChromeNSSCryptoModuleDelegate>)>
957 got_delegate_callback = base::Bind(&InitializeAndPassKeygenHandler,
958 base::Passed(&keygen_handler),
959 callback);
961 ChromeNSSCryptoModuleDelegate::CreateForResourceContext(
962 chrome::kCryptoModulePasswordKeygen,
963 net::HostPortPair::FromURL(url),
964 this,
965 got_delegate_callback);
966 #else
967 callback.Run(make_scoped_ptr(
968 new net::KeygenHandler(key_size_in_bits, challenge_string, url)));
969 #endif
972 ResourceContext::SaltCallback
973 ProfileIOData::ResourceContext::GetMediaDeviceIDSalt() {
974 return io_data_->GetMediaDeviceIDSalt();
977 // static
978 std::string ProfileIOData::GetSSLSessionCacheShard() {
979 DCHECK_CURRENTLY_ON(BrowserThread::IO);
980 // The SSL session cache is partitioned by setting a string. This returns a
981 // unique string to partition the SSL session cache. Each time we create a
982 // new profile, we'll get a fresh SSL session cache which is separate from
983 // the other profiles.
984 static unsigned ssl_session_cache_instance = 0;
985 return base::StringPrintf("profile/%u", ssl_session_cache_instance++);
988 void ProfileIOData::Init(
989 content::ProtocolHandlerMap* protocol_handlers,
990 content::URLRequestInterceptorScopedVector request_interceptors) const {
991 // The basic logic is implemented here. The specific initialization
992 // is done in InitializeInternal(), implemented by subtypes. Static helper
993 // functions have been provided to assist in common operations.
994 DCHECK_CURRENTLY_ON(BrowserThread::IO);
995 DCHECK(!initialized_);
997 // TODO(jhawkins): Remove once crbug.com/102004 is fixed.
998 CHECK(initialized_on_UI_thread_);
1000 // TODO(jhawkins): Return to DCHECK once crbug.com/102004 is fixed.
1001 CHECK(profile_params_.get());
1003 IOThread* const io_thread = profile_params_->io_thread;
1004 IOThread::Globals* const io_thread_globals = io_thread->globals();
1005 const base::CommandLine& command_line =
1006 *base::CommandLine::ForCurrentProcess();
1008 // Create the common request contexts.
1009 main_request_context_.reset(new net::URLRequestContext());
1010 extensions_request_context_.reset(new net::URLRequestContext());
1012 scoped_ptr<ChromeNetworkDelegate> network_delegate(
1013 new ChromeNetworkDelegate(
1014 #if defined(ENABLE_EXTENSIONS)
1015 io_thread_globals->extension_event_router_forwarder.get(),
1016 #else
1017 NULL,
1018 #endif
1019 &enable_referrers_));
1020 #if defined(ENABLE_EXTENSIONS)
1021 network_delegate->set_extension_info_map(
1022 profile_params_->extension_info_map.get());
1023 if (!command_line.HasSwitch(switches::kDisableExtensionsHttpThrottling)) {
1024 extension_throttle_manager_.reset(
1025 new extensions::ExtensionThrottleManager());
1027 #endif
1029 #if defined(ENABLE_CONFIGURATION_POLICY)
1030 network_delegate->set_url_blacklist_manager(url_blacklist_manager_.get());
1031 #endif
1032 network_delegate->set_profile(profile_params_->profile);
1033 network_delegate->set_profile_path(profile_params_->path);
1034 network_delegate->set_cookie_settings(profile_params_->cookie_settings.get());
1035 network_delegate->set_enable_do_not_track(&enable_do_not_track_);
1036 network_delegate->set_force_google_safe_search(&force_google_safesearch_);
1037 network_delegate->set_force_youtube_safety_mode(&force_youtube_safety_mode_);
1038 fraudulent_certificate_reporter_.reset(
1039 new ChromeFraudulentCertificateReporter(main_request_context_.get()));
1041 // NOTE: Proxy service uses the default io thread network delegate, not the
1042 // delegate just created.
1043 proxy_service_.reset(
1044 ProxyServiceFactory::CreateProxyService(
1045 io_thread->net_log(),
1046 io_thread_globals->proxy_script_fetcher_context.get(),
1047 io_thread_globals->system_network_delegate.get(),
1048 profile_params_->proxy_config_service.release(),
1049 command_line,
1050 quick_check_enabled_.GetValue()));
1051 transport_security_state_.reset(new net::TransportSecurityState());
1052 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
1053 transport_security_persister_.reset(
1054 new net::TransportSecurityPersister(
1055 transport_security_state_.get(),
1056 profile_params_->path,
1057 pool->GetSequencedTaskRunnerWithShutdownBehavior(
1058 pool->GetSequenceToken(),
1059 base::SequencedWorkerPool::BLOCK_SHUTDOWN),
1060 IsOffTheRecord()));
1062 // Take ownership over these parameters.
1063 cookie_settings_ = profile_params_->cookie_settings;
1064 host_content_settings_map_ = profile_params_->host_content_settings_map;
1065 #if defined(ENABLE_EXTENSIONS)
1066 extension_info_map_ = profile_params_->extension_info_map;
1067 #endif
1069 resource_context_->host_resolver_ = io_thread_globals->host_resolver.get();
1070 resource_context_->request_context_ = main_request_context_.get();
1072 if (profile_params_->resource_prefetch_predictor_observer_) {
1073 resource_prefetch_predictor_observer_.reset(
1074 profile_params_->resource_prefetch_predictor_observer_.release());
1077 #if defined(ENABLE_SUPERVISED_USERS)
1078 supervised_user_url_filter_ = profile_params_->supervised_user_url_filter;
1079 #endif
1081 #if defined(OS_CHROMEOS)
1082 username_hash_ = profile_params_->username_hash;
1083 use_system_key_slot_ = profile_params_->use_system_key_slot;
1084 if (use_system_key_slot_)
1085 EnableNSSSystemKeySlotForResourceContext(resource_context_.get());
1087 crypto::ScopedPK11Slot public_slot =
1088 crypto::GetPublicSlotForChromeOSUser(username_hash_);
1089 // The private slot won't be ready by this point. It shouldn't be necessary
1090 // for cert trust purposes anyway.
1091 scoped_refptr<net::CertVerifyProc> verify_proc(
1092 new chromeos::CertVerifyProcChromeOS(public_slot.Pass()));
1093 if (policy_cert_verifier_) {
1094 DCHECK_EQ(policy_cert_verifier_, cert_verifier_.get());
1095 policy_cert_verifier_->InitializeOnIOThread(verify_proc);
1096 } else {
1097 cert_verifier_.reset(new net::MultiThreadedCertVerifier(verify_proc.get()));
1099 main_request_context_->set_cert_verifier(cert_verifier_.get());
1100 #else
1101 main_request_context_->set_cert_verifier(
1102 io_thread_globals->cert_verifier.get());
1103 #endif
1105 // Install the New Tab Page Interceptor.
1106 if (profile_params_->new_tab_page_interceptor.get()) {
1107 request_interceptors.push_back(
1108 profile_params_->new_tab_page_interceptor.release());
1111 InitializeInternal(
1112 network_delegate.Pass(), profile_params_.get(),
1113 protocol_handlers, request_interceptors.Pass());
1115 profile_params_.reset();
1116 initialized_ = true;
1119 void ProfileIOData::ApplyProfileParamsToContext(
1120 net::URLRequestContext* context) const {
1121 context->set_http_user_agent_settings(
1122 chrome_http_user_agent_settings_.get());
1123 context->set_ssl_config_service(profile_params_->ssl_config_service.get());
1126 scoped_ptr<net::URLRequestJobFactory> ProfileIOData::SetUpJobFactoryDefaults(
1127 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory,
1128 content::URLRequestInterceptorScopedVector request_interceptors,
1129 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
1130 protocol_handler_interceptor,
1131 net::NetworkDelegate* network_delegate,
1132 net::FtpTransactionFactory* ftp_transaction_factory) const {
1133 // NOTE(willchan): Keep these protocol handlers in sync with
1134 // ProfileIOData::IsHandledProtocol().
1135 bool set_protocol = job_factory->SetProtocolHandler(
1136 url::kFileScheme,
1137 new net::FileProtocolHandler(
1138 content::BrowserThread::GetBlockingPool()->
1139 GetTaskRunnerWithShutdownBehavior(
1140 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
1141 DCHECK(set_protocol);
1143 #if defined(ENABLE_EXTENSIONS)
1144 DCHECK(extension_info_map_.get());
1145 // Check only for incognito (and not Chrome OS guest mode GUEST_PROFILE).
1146 bool is_incognito = profile_type() == Profile::INCOGNITO_PROFILE;
1147 set_protocol = job_factory->SetProtocolHandler(
1148 extensions::kExtensionScheme,
1149 extensions::CreateExtensionProtocolHandler(is_incognito,
1150 extension_info_map_.get()));
1151 DCHECK(set_protocol);
1152 set_protocol = job_factory->SetProtocolHandler(
1153 extensions::kExtensionResourceScheme,
1154 CreateExtensionResourceProtocolHandler());
1155 DCHECK(set_protocol);
1156 #endif
1157 set_protocol = job_factory->SetProtocolHandler(
1158 url::kDataScheme, new net::DataProtocolHandler());
1159 DCHECK(set_protocol);
1160 #if defined(OS_CHROMEOS)
1161 if (profile_params_) {
1162 set_protocol = job_factory->SetProtocolHandler(
1163 content::kExternalFileScheme,
1164 new chromeos::ExternalFileProtocolHandler(profile_params_->profile));
1165 DCHECK(set_protocol);
1167 #endif // defined(OS_CHROMEOS)
1168 #if defined(OS_ANDROID)
1169 set_protocol = job_factory->SetProtocolHandler(
1170 url::kContentScheme,
1171 content::ContentProtocolHandler::Create(
1172 content::BrowserThread::GetBlockingPool()->
1173 GetTaskRunnerWithShutdownBehavior(
1174 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
1175 #endif
1177 job_factory->SetProtocolHandler(
1178 url::kAboutScheme, new chrome_browser_net::AboutProtocolHandler());
1179 #if !defined(DISABLE_FTP_SUPPORT)
1180 DCHECK(ftp_transaction_factory);
1181 job_factory->SetProtocolHandler(
1182 url::kFtpScheme,
1183 new net::FtpProtocolHandler(ftp_transaction_factory));
1184 #endif // !defined(DISABLE_FTP_SUPPORT)
1186 #if defined(DEBUG_DEVTOOLS)
1187 request_interceptors.push_back(new DebugDevToolsInterceptor);
1188 #endif
1190 // Set up interceptors in the reverse order.
1191 scoped_ptr<net::URLRequestJobFactory> top_job_factory = job_factory.Pass();
1192 for (content::URLRequestInterceptorScopedVector::reverse_iterator i =
1193 request_interceptors.rbegin();
1194 i != request_interceptors.rend();
1195 ++i) {
1196 top_job_factory.reset(new net::URLRequestInterceptingJobFactory(
1197 top_job_factory.Pass(), make_scoped_ptr(*i)));
1199 request_interceptors.weak_clear();
1201 if (protocol_handler_interceptor) {
1202 protocol_handler_interceptor->Chain(top_job_factory.Pass());
1203 return protocol_handler_interceptor.Pass();
1204 } else {
1205 return top_job_factory.Pass();
1209 void ProfileIOData::ShutdownOnUIThread(
1210 scoped_ptr<ChromeURLRequestContextGetterVector> context_getters) {
1211 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1213 google_services_user_account_id_.Destroy();
1214 enable_referrers_.Destroy();
1215 enable_do_not_track_.Destroy();
1216 force_google_safesearch_.Destroy();
1217 force_youtube_safety_mode_.Destroy();
1218 #if !defined(OS_CHROMEOS)
1219 enable_metrics_.Destroy();
1220 #endif
1221 safe_browsing_enabled_.Destroy();
1222 sync_disabled_.Destroy();
1223 signin_allowed_.Destroy();
1224 network_prediction_options_.Destroy();
1225 quick_check_enabled_.Destroy();
1226 if (media_device_id_salt_.get())
1227 media_device_id_salt_->ShutdownOnUIThread();
1228 session_startup_pref_.Destroy();
1229 #if defined(ENABLE_CONFIGURATION_POLICY)
1230 if (url_blacklist_manager_)
1231 url_blacklist_manager_->ShutdownOnUIThread();
1232 #endif
1233 if (chrome_http_user_agent_settings_)
1234 chrome_http_user_agent_settings_->CleanupOnUIThread();
1235 incognito_availibility_pref_.Destroy();
1237 if (!context_getters->empty()) {
1238 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
1239 BrowserThread::PostTask(
1240 BrowserThread::IO, FROM_HERE,
1241 base::Bind(&NotifyContextGettersOfShutdownOnIO,
1242 base::Passed(&context_getters)));
1246 bool posted = BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this);
1247 if (!posted)
1248 delete this;
1251 void ProfileIOData::set_channel_id_service(
1252 net::ChannelIDService* channel_id_service) const {
1253 channel_id_service_.reset(channel_id_service);
1256 void ProfileIOData::DestroyResourceContext() {
1257 resource_context_.reset();
1260 scoped_ptr<net::HttpCache> ProfileIOData::CreateMainHttpFactory(
1261 const ProfileParams* profile_params,
1262 net::HttpCache::BackendFactory* main_backend) const {
1263 net::HttpNetworkSession::Params params;
1264 net::URLRequestContext* context = main_request_context();
1266 IOThread* const io_thread = profile_params->io_thread;
1268 io_thread->InitializeNetworkSessionParams(&params);
1270 params.host_resolver = context->host_resolver();
1271 params.cert_verifier = context->cert_verifier();
1272 params.channel_id_service = context->channel_id_service();
1273 params.transport_security_state = context->transport_security_state();
1274 params.cert_transparency_verifier = context->cert_transparency_verifier();
1275 params.proxy_service = context->proxy_service();
1276 params.ssl_session_cache_shard = GetSSLSessionCacheShard();
1277 params.ssl_config_service = context->ssl_config_service();
1278 params.http_auth_handler_factory = context->http_auth_handler_factory();
1279 params.network_delegate = context->network_delegate();
1280 params.http_server_properties = context->http_server_properties();
1281 params.net_log = context->net_log();
1282 if (data_reduction_proxy_io_data_.get())
1283 params.proxy_delegate = data_reduction_proxy_io_data_->proxy_delegate();
1285 network_controller_.reset(new DevToolsNetworkController());
1287 net::HttpNetworkSession* session = new net::HttpNetworkSession(params);
1288 return scoped_ptr<net::HttpCache>(new net::HttpCache(
1289 new DevToolsNetworkTransactionFactory(network_controller_.get(), session),
1290 context->net_log(), main_backend));
1293 scoped_ptr<net::HttpCache> ProfileIOData::CreateHttpFactory(
1294 net::HttpNetworkSession* shared_session,
1295 net::HttpCache::BackendFactory* backend) const {
1296 return scoped_ptr<net::HttpCache>(new net::HttpCache(
1297 new DevToolsNetworkTransactionFactory(
1298 network_controller_.get(), shared_session),
1299 shared_session->net_log(), backend));
1302 void ProfileIOData::SetCookieSettingsForTesting(
1303 content_settings::CookieSettings* cookie_settings) {
1304 DCHECK(!cookie_settings_.get());
1305 cookie_settings_ = cookie_settings;