172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Use of this source code is governed by a BSD-style license that can be
372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// found in the LICENSE file.
472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "chrome/browser/profiles/profile_io_data.h"
672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
7dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include <string>
8dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "base/basictypes.h"
1072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "base/command_line.h"
11ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/compiler_specific.h"
1272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "base/logging.h"
13ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/stl_util-inl.h"
14dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "base/string_number_conversions.h"
1572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "chrome/browser/browser_process.h"
16ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
17ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/browser/extensions/user_script_master.h"
18dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "chrome/browser/io_thread.h"
19dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "chrome/browser/net/chrome_cookie_notification_details.h"
20ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/browser/net/chrome_cookie_policy.h"
21ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/browser/net/chrome_dns_cert_provenance_checker_factory.h"
22ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/browser/net/chrome_net_log.h"
23ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/browser/net/chrome_network_delegate.h"
24dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "chrome/browser/net/pref_proxy_config_service.h"
25ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/browser/net/proxy_service_factory.h"
26dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "chrome/browser/prefs/pref_service.h"
27ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/browser/profiles/profile.h"
28dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "chrome/common/chrome_switches.h"
29dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "chrome/common/pref_names.h"
30dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "content/browser/browser_thread.h"
31ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "content/browser/resource_context.h"
32ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "content/common/notification_service.h"
33dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "net/http/http_util.h"
34dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "net/proxy/proxy_config_service_fixed.h"
35dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "net/proxy/proxy_script_fetcher_impl.h"
36dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "net/proxy/proxy_service.h"
37ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "webkit/database/database_tracker.h"
3872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
39dc0f95d653279beabeb9817299e2902918ba123eKristian Monsennamespace {
4072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
41dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// ----------------------------------------------------------------------------
42dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// CookieMonster::Delegate implementation
43dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// ----------------------------------------------------------------------------
44dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenclass ChromeCookieMonsterDelegate : public net::CookieMonster::Delegate {
45dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen public:
46dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  explicit ChromeCookieMonsterDelegate(Profile* profile) {
47dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
48dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    profile_getter_ = new ProfileGetter(profile);
49dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  }
5072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
51dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // net::CookieMonster::Delegate implementation.
52dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  virtual void OnCookieChanged(
53dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      const net::CookieMonster::CanonicalCookie& cookie,
54ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      bool removed,
55ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      net::CookieMonster::Delegate::ChangeCause cause) {
56dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    BrowserThread::PostTask(
57dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen        BrowserThread::UI, FROM_HERE,
58dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen        NewRunnableMethod(this,
59dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen            &ChromeCookieMonsterDelegate::OnCookieChangedAsyncHelper,
60dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen            cookie,
61ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen            removed,
62ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen            cause));
6372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  }
6472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
65dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen private:
66dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // This class allows us to safely access the Profile pointer. The Delegate
67dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // itself cannot observe the PROFILE_DESTROYED notification, since it cannot
68dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // guarantee to be deleted on the UI thread and therefore unregister from
69dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // the notifications. All methods of ProfileGetter must be invoked on the UI
70dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // thread.
71dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  class ProfileGetter
72dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      : public base::RefCountedThreadSafe<ProfileGetter,
73dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen                                          BrowserThread::DeleteOnUIThread>,
74dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen        public NotificationObserver {
75dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen   public:
76dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    explicit ProfileGetter(Profile* profile) : profile_(profile) {
77dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
78dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      registrar_.Add(this,
79dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen                     NotificationType::PROFILE_DESTROYED,
80dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen                     Source<Profile>(profile_));
81dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    }
82dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
83dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    // NotificationObserver implementation.
84dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    void Observe(NotificationType type,
85dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen                 const NotificationSource& source,
86dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen                 const NotificationDetails& details) {
87dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
88dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      if (NotificationType::PROFILE_DESTROYED == type) {
89dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen        Profile* profile = Source<Profile>(source).ptr();
90dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen        if (profile_ == profile)
91dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen          profile_ = NULL;
92dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      }
93dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    }
94dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
95dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    Profile* get() {
96dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
97dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      return profile_;
98dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    }
99dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
100dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen   private:
101dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;
102dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    friend class DeleteTask<ProfileGetter>;
103dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
104dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    virtual ~ProfileGetter() {}
105dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
106dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    NotificationRegistrar registrar_;
107dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
108dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    Profile* profile_;
109dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  };
110dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
111dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  virtual ~ChromeCookieMonsterDelegate() {}
112dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
113dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  void OnCookieChangedAsyncHelper(
114dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      const net::CookieMonster::CanonicalCookie& cookie,
115ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      bool removed,
116ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      net::CookieMonster::Delegate::ChangeCause cause) {
117dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    if (profile_getter_->get()) {
118ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      ChromeCookieDetails cookie_details(&cookie, removed, cause);
119dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      NotificationService::current()->Notify(
120dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen          NotificationType::COOKIE_CHANGED,
121dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen          Source<Profile>(profile_getter_->get()),
122dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen          Details<ChromeCookieDetails>(&cookie_details));
123dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    }
12472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  }
12572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
126dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  scoped_refptr<ProfileGetter> profile_getter_;
127dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen};
128dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
129dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen}  // namespace
130dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
131ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenvoid ProfileIOData::InitializeProfileParams(Profile* profile) {
13272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
133dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  PrefService* pref_service = profile->GetPrefs();
13472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
135ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  scoped_ptr<ProfileParams> params(new ProfileParams);
136ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  params->is_incognito = profile->IsOffTheRecord();
137dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->clear_local_state_on_exit =
138dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      pref_service->GetBoolean(prefs::kClearSiteDataOnExit);
13972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
140dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->appcache_service = profile->GetAppCacheService();
14172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
142dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Set up Accept-Language and Accept-Charset header values
143dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->accept_language = net::HttpUtil::GenerateAcceptLanguageHeader(
144dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      pref_service->GetString(prefs::kAcceptLanguages));
145dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  std::string default_charset = pref_service->GetString(prefs::kDefaultCharset);
146dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->accept_charset =
147dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      net::HttpUtil::GenerateAcceptCharsetHeader(default_charset);
14872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
149dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // At this point, we don't know the charset of the referring page
150dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // where a url request originates from. This is used to get a suggested
151dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // filename from Content-Disposition header made of raw 8bit characters.
152dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // Down the road, it can be overriden if it becomes known (for instance,
153dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // when download request is made through the context menu in a web page).
154dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // At the moment, it'll remain 'undeterministic' when a user
155dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // types a URL in the omnibar or click on a download link in a page.
156dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // For the latter, we need a change on the webkit-side.
157dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // We initialize it to the default charset here and a user will
158dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // have an *arguably* better default charset for interpreting a raw 8bit
159dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // C-D header field.  It means the native OS codepage fallback in
160dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  // net_util::GetSuggestedFilename is unlikely to be taken.
161dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->referrer_charset = default_charset;
162dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
163ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  params->io_thread = g_browser_process->io_thread();
164ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
165dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->host_content_settings_map = profile->GetHostContentSettingsMap();
166dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->host_zoom_map = profile->GetHostZoomMap();
167dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->transport_security_state = profile->GetTransportSecurityState();
16872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
169dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  if (profile->GetUserScriptMaster()) {
170dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen    params->user_script_dir_path =
171dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen        profile->GetUserScriptMaster()->user_script_dir();
17272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  }
17372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
174dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->ssl_config_service = profile->GetSSLConfigService();
175dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->cookie_monster_delegate = new ChromeCookieMonsterDelegate(profile);
176dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->database_tracker = profile->GetDatabaseTracker();
177dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->appcache_service = profile->GetAppCacheService();
178dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->blob_storage_context = profile->GetBlobStorageContext();
179dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->file_system_context = profile->GetFileSystemContext();
180dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->extension_info_map = profile->GetExtensionInfoMap();
181dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->prerender_manager = profile->GetPrerenderManager();
182dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->protocol_handler_registry = profile->GetProtocolHandlerRegistry();
183dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
184ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  params->proxy_config_service.reset(
185ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      ProxyServiceFactory::CreateProxyConfigService(
186ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen          profile->GetProxyConfigTracker()));
187dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  params->profile_id = profile->GetRuntimeId();
188ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  profile_params_.reset(params.release());
189dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen}
19072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
19172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenProfileIOData::RequestContext::RequestContext() {}
19272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenProfileIOData::RequestContext::~RequestContext() {}
19372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
194dc0f95d653279beabeb9817299e2902918ba123eKristian MonsenProfileIOData::ProfileParams::ProfileParams()
195ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    : is_incognito(false),
196ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      clear_local_state_on_exit(false),
197ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      profile_id(Profile::kInvalidProfileId) {}
198dc0f95d653279beabeb9817299e2902918ba123eKristian MonsenProfileIOData::ProfileParams::~ProfileParams() {}
199dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen
200ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenProfileIOData::ProfileIOData(bool is_incognito)
201ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    : initialized_(false),
202ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      ALLOW_THIS_IN_INITIALIZER_LIST(resource_context_(this)) {
20372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
20472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}
20572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
20672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenProfileIOData::~ProfileIOData() {
20772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // If we have never initialized ProfileIOData, then Handle may hold the only
20872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // reference to it. The important thing is to make sure it hasn't been
20972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // initialized yet, because the lazily initialized variables are supposed to
21072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // live on the IO thread.
21172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  if (BrowserThread::CurrentlyOn(BrowserThread::UI))
21272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen    DCHECK(!initialized_);
21372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  else
21472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
21572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}
21672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
21772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenscoped_refptr<ChromeURLRequestContext>
21872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenProfileIOData::GetMainRequestContext() const {
21972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  LazyInitialize();
220ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  scoped_refptr<RequestContext> context = main_request_context_;
221ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  context->set_profile_io_data(this);
222ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  main_request_context_ = NULL;
22372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  return context;
22472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}
22572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
22672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenscoped_refptr<ChromeURLRequestContext>
22772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenProfileIOData::GetMediaRequestContext() const {
22872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  LazyInitialize();
229dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  scoped_refptr<ChromeURLRequestContext> context =
230dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen      AcquireMediaRequestContext();
231dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  DCHECK(context);
23272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  return context;
23372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}
23472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
23572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenscoped_refptr<ChromeURLRequestContext>
23672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenProfileIOData::GetExtensionsRequestContext() const {
23772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  LazyInitialize();
238ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  scoped_refptr<RequestContext> context =
239ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      extensions_request_context_;
240ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  context->set_profile_io_data(this);
241ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  extensions_request_context_ = NULL;
242ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  return context;
243ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}
244ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
245ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenscoped_refptr<ChromeURLRequestContext>
246ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenProfileIOData::GetIsolatedAppRequestContext(
247ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    scoped_refptr<ChromeURLRequestContext> main_context,
248ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    const std::string& app_id) const {
249ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  LazyInitialize();
250dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  scoped_refptr<ChromeURLRequestContext> context =
251ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      AcquireIsolatedAppRequestContext(main_context, app_id);
252dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  DCHECK(context);
25372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  return context;
25472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}
25572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
256ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenconst content::ResourceContext& ProfileIOData::GetResourceContext() const {
257ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  return resource_context_;
258ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}
259ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
260ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenProfileIOData::ResourceContext::ResourceContext(const ProfileIOData* io_data)
261ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    : io_data_(io_data) {
262ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  DCHECK(io_data);
263ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}
264ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
265ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenProfileIOData::ResourceContext::~ResourceContext() {}
266ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
267ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenvoid ProfileIOData::ResourceContext::EnsureInitialized() const {
268ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  io_data_->LazyInitialize();
269ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}
270ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
27172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenvoid ProfileIOData::LazyInitialize() const {
27272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
27372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  if (initialized_)
27472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen    return;
275ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  DCHECK(profile_params_.get());
276ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
277ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  IOThread* const io_thread = profile_params_->io_thread;
278ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  IOThread::Globals* const io_thread_globals = io_thread->globals();
279ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
280ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
281ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Create the common request contexts.
282ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  main_request_context_ = new RequestContext;
283ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  extensions_request_context_ = new RequestContext;
284ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
285ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  profile_params_->appcache_service->set_request_context(main_request_context_);
286ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
287ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Create objects pointed to by URLRequestContext.
288ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  cookie_policy_.reset(
289ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      new ChromeCookiePolicy(profile_params_->host_content_settings_map));
290ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
291ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  network_delegate_.reset(new ChromeNetworkDelegate(
292ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen        io_thread_globals->extension_event_router_forwarder.get(),
293ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen        profile_params_->profile_id,
294ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen        &enable_referrers_,
295ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen        profile_params_->protocol_handler_registry));
296ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
297ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  dns_cert_checker_.reset(
298ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      CreateDnsCertProvenanceChecker(io_thread_globals->dnsrr_resolver.get(),
299ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                                     main_request_context_));
300ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
301ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  proxy_service_ =
302ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      ProxyServiceFactory::CreateProxyService(
303ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen          io_thread->net_log(),
304ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen          io_thread_globals->proxy_script_fetcher_context.get(),
305ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen          profile_params_->proxy_config_service.release(),
306ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen          command_line);
307ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
308ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Take ownership over these parameters.
309ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  database_tracker_ = profile_params_->database_tracker;
310ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  appcache_service_ = profile_params_->appcache_service;
311ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  blob_storage_context_ = profile_params_->blob_storage_context;
312ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  file_system_context_ = profile_params_->file_system_context;
313ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
314ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  resource_context_.set_host_resolver(io_thread_globals->host_resolver.get());
315ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  resource_context_.set_request_context(main_request_context_);
316ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  resource_context_.set_database_tracker(database_tracker_);
317ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  resource_context_.set_appcache_service(appcache_service_);
318ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  resource_context_.set_blob_storage_context(blob_storage_context_);
319ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  resource_context_.set_file_system_context(file_system_context_);
320ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
321ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  LazyInitializeInternal(profile_params_.get());
322ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
323ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  profile_params_.reset();
324dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  initialized_ = true;
325dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen}
32672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
327dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenvoid ProfileIOData::ApplyProfileParamsToContext(
328ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    ChromeURLRequestContext* context) const {
329ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  context->set_is_incognito(profile_params_->is_incognito);
330ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  context->set_accept_language(profile_params_->accept_language);
331ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  context->set_accept_charset(profile_params_->accept_charset);
332ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  context->set_referrer_charset(profile_params_->referrer_charset);
333ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  context->set_user_script_dir_path(profile_params_->user_script_dir_path);
334dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  context->set_host_content_settings_map(
335ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      profile_params_->host_content_settings_map);
336ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  context->set_host_zoom_map(profile_params_->host_zoom_map);
337dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  context->set_transport_security_state(
338ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      profile_params_->transport_security_state);
339ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  context->set_ssl_config_service(profile_params_->ssl_config_service);
340ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  context->set_appcache_service(profile_params_->appcache_service);
341ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  context->set_blob_storage_context(profile_params_->blob_storage_context);
342ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  context->set_file_system_context(profile_params_->file_system_context);
343ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  context->set_extension_info_map(profile_params_->extension_info_map);
344ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  context->set_prerender_manager(profile_params_->prerender_manager);
345dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen}
34672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
347ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenvoid ProfileIOData::ShutdownOnUIThread() {
348dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
349ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  enable_referrers_.Destroy();
35072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}
351