| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/net/chrome_url_request_context.h" | 5 #include "chrome/browser/net/chrome_url_request_context.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
| 11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/io_thread.h" | 12 #include "chrome/browser/io_thread.h" |
| 13 #include "chrome/browser/net/load_time_stats.h" | 13 #include "chrome/browser/net/load_time_stats.h" |
| 14 #include "chrome/browser/prefs/pref_service.h" | |
| 15 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/profiles/profile_io_data.h" | 15 #include "chrome/browser/profiles/profile_io_data.h" |
| 17 #include "chrome/common/chrome_notification_types.h" | |
| 18 #include "chrome/common/pref_names.h" | |
| 19 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/notification_details.h" | |
| 21 #include "content/public/browser/notification_source.h" | |
| 22 #include "content/public/common/content_client.h" | |
| 23 #include "net/cookies/cookie_store.h" | 17 #include "net/cookies/cookie_store.h" |
| 24 #include "net/http/http_util.h" | |
| 25 | 18 |
| 26 using content::BrowserThread; | 19 using content::BrowserThread; |
| 27 | 20 |
| 28 class ChromeURLRequestContextFactory { | 21 class ChromeURLRequestContextFactory { |
| 29 public: | 22 public: |
| 30 ChromeURLRequestContextFactory() {} | 23 ChromeURLRequestContextFactory() {} |
| 31 virtual ~ChromeURLRequestContextFactory() {} | 24 virtual ~ChromeURLRequestContextFactory() {} |
| 32 | 25 |
| 33 // Called to create a new instance (will only be called once). | 26 // Called to create a new instance (will only be called once). |
| 34 virtual ChromeURLRequestContext* Create() = 0; | 27 virtual ChromeURLRequestContext* Create() = 0; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 const ProfileIOData* const profile_io_data_; | 140 const ProfileIOData* const profile_io_data_; |
| 148 }; | 141 }; |
| 149 | 142 |
| 150 } // namespace | 143 } // namespace |
| 151 | 144 |
| 152 // ---------------------------------------------------------------------------- | 145 // ---------------------------------------------------------------------------- |
| 153 // ChromeURLRequestContextGetter | 146 // ChromeURLRequestContextGetter |
| 154 // ---------------------------------------------------------------------------- | 147 // ---------------------------------------------------------------------------- |
| 155 | 148 |
| 156 ChromeURLRequestContextGetter::ChromeURLRequestContextGetter( | 149 ChromeURLRequestContextGetter::ChromeURLRequestContextGetter( |
| 157 Profile* profile, | |
| 158 ChromeURLRequestContextFactory* factory) | 150 ChromeURLRequestContextFactory* factory) |
| 159 : factory_(factory) { | 151 : factory_(factory) { |
| 160 DCHECK(factory); | 152 DCHECK(factory); |
| 161 DCHECK(profile); | |
| 162 RegisterPrefsObserver(profile); | |
| 163 } | 153 } |
| 164 | 154 |
| 165 ChromeURLRequestContextGetter::~ChromeURLRequestContextGetter() { | 155 ChromeURLRequestContextGetter::~ChromeURLRequestContextGetter() {} |
| 166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 167 | |
| 168 DCHECK(registrar_.IsEmpty()) << "Probably didn't call CleanupOnUIThread"; | |
| 169 } | |
| 170 | 156 |
| 171 // Lazily create a ChromeURLRequestContext using our factory. | 157 // Lazily create a ChromeURLRequestContext using our factory. |
| 172 net::URLRequestContext* ChromeURLRequestContextGetter::GetURLRequestContext() { | 158 net::URLRequestContext* ChromeURLRequestContextGetter::GetURLRequestContext() { |
| 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 174 | 160 |
| 175 if (!url_request_context_) { | 161 if (!url_request_context_) { |
| 176 DCHECK(factory_.get()); | 162 DCHECK(factory_.get()); |
| 177 url_request_context_ = factory_->Create()->GetWeakPtr(); | 163 url_request_context_ = factory_->Create()->GetWeakPtr(); |
| 178 factory_.reset(); | 164 factory_.reset(); |
| 179 } | 165 } |
| 180 | 166 |
| 181 // Should not be NULL, unless we're trying to use the URLRequestContextGetter | 167 // Should not be NULL, unless we're trying to use the URLRequestContextGetter |
| 182 // after the Profile has already been deleted. | 168 // after the Profile has already been deleted. |
| 183 CHECK(url_request_context_.get()); | 169 CHECK(url_request_context_.get()); |
| 184 | 170 |
| 185 return url_request_context_; | 171 return url_request_context_; |
| 186 } | 172 } |
| 187 | 173 |
| 188 scoped_refptr<base::SingleThreadTaskRunner> | 174 scoped_refptr<base::SingleThreadTaskRunner> |
| 189 ChromeURLRequestContextGetter::GetNetworkTaskRunner() const { | 175 ChromeURLRequestContextGetter::GetNetworkTaskRunner() const { |
| 190 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); | 176 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); |
| 191 } | 177 } |
| 192 | 178 |
| 193 // static | 179 // static |
| 194 ChromeURLRequestContextGetter* ChromeURLRequestContextGetter::CreateOriginal( | 180 ChromeURLRequestContextGetter* ChromeURLRequestContextGetter::CreateOriginal( |
| 195 Profile* profile, | 181 Profile* profile, |
| 196 const ProfileIOData* profile_io_data) { | 182 const ProfileIOData* profile_io_data) { |
| 197 DCHECK(!profile->IsOffTheRecord()); | 183 DCHECK(!profile->IsOffTheRecord()); |
| 198 return new ChromeURLRequestContextGetter( | 184 return new ChromeURLRequestContextGetter( |
| 199 profile, | |
| 200 new FactoryForMain(profile_io_data)); | 185 new FactoryForMain(profile_io_data)); |
| 201 } | 186 } |
| 202 | 187 |
| 203 // static | 188 // static |
| 204 ChromeURLRequestContextGetter* | 189 ChromeURLRequestContextGetter* |
| 205 ChromeURLRequestContextGetter::CreateOriginalForMedia( | 190 ChromeURLRequestContextGetter::CreateOriginalForMedia( |
| 206 Profile* profile, const ProfileIOData* profile_io_data) { | 191 Profile* profile, const ProfileIOData* profile_io_data) { |
| 207 DCHECK(!profile->IsOffTheRecord()); | 192 DCHECK(!profile->IsOffTheRecord()); |
| 208 return new ChromeURLRequestContextGetter( | 193 return new ChromeURLRequestContextGetter( |
| 209 profile, | |
| 210 new FactoryForMedia(profile_io_data)); | 194 new FactoryForMedia(profile_io_data)); |
| 211 } | 195 } |
| 212 | 196 |
| 213 // static | 197 // static |
| 214 ChromeURLRequestContextGetter* | 198 ChromeURLRequestContextGetter* |
| 215 ChromeURLRequestContextGetter::CreateOriginalForExtensions( | 199 ChromeURLRequestContextGetter::CreateOriginalForExtensions( |
| 216 Profile* profile, const ProfileIOData* profile_io_data) { | 200 Profile* profile, const ProfileIOData* profile_io_data) { |
| 217 DCHECK(!profile->IsOffTheRecord()); | 201 DCHECK(!profile->IsOffTheRecord()); |
| 218 return new ChromeURLRequestContextGetter( | 202 return new ChromeURLRequestContextGetter( |
| 219 profile, | |
| 220 new FactoryForExtensions(profile_io_data)); | 203 new FactoryForExtensions(profile_io_data)); |
| 221 } | 204 } |
| 222 | 205 |
| 223 // static | 206 // static |
| 224 ChromeURLRequestContextGetter* | 207 ChromeURLRequestContextGetter* |
| 225 ChromeURLRequestContextGetter::CreateOriginalForIsolatedApp( | 208 ChromeURLRequestContextGetter::CreateOriginalForIsolatedApp( |
| 226 Profile* profile, | 209 Profile* profile, |
| 227 const ProfileIOData* profile_io_data, | 210 const ProfileIOData* profile_io_data, |
| 228 const std::string& app_id, | 211 const std::string& app_id, |
| 229 scoped_ptr<net::URLRequestJobFactory::Interceptor> | 212 scoped_ptr<net::URLRequestJobFactory::Interceptor> |
| 230 protocol_handler_interceptor) { | 213 protocol_handler_interceptor) { |
| 231 DCHECK(!profile->IsOffTheRecord()); | 214 DCHECK(!profile->IsOffTheRecord()); |
| 232 ChromeURLRequestContextGetter* main_context = | 215 ChromeURLRequestContextGetter* main_context = |
| 233 static_cast<ChromeURLRequestContextGetter*>(profile->GetRequestContext()); | 216 static_cast<ChromeURLRequestContextGetter*>(profile->GetRequestContext()); |
| 234 return new ChromeURLRequestContextGetter( | 217 return new ChromeURLRequestContextGetter( |
| 235 profile, | |
| 236 new FactoryForIsolatedApp(profile_io_data, app_id, main_context, | 218 new FactoryForIsolatedApp(profile_io_data, app_id, main_context, |
| 237 protocol_handler_interceptor.Pass())); | 219 protocol_handler_interceptor.Pass())); |
| 238 } | 220 } |
| 239 | 221 |
| 240 // static | 222 // static |
| 241 ChromeURLRequestContextGetter* | 223 ChromeURLRequestContextGetter* |
| 242 ChromeURLRequestContextGetter::CreateOriginalForIsolatedMedia( | 224 ChromeURLRequestContextGetter::CreateOriginalForIsolatedMedia( |
| 243 Profile* profile, | 225 Profile* profile, |
| 244 ChromeURLRequestContextGetter* app_context, | 226 ChromeURLRequestContextGetter* app_context, |
| 245 const ProfileIOData* profile_io_data, | 227 const ProfileIOData* profile_io_data, |
| 246 const std::string& app_id) { | 228 const std::string& app_id) { |
| 247 DCHECK(!profile->IsOffTheRecord()); | 229 DCHECK(!profile->IsOffTheRecord()); |
| 248 return new ChromeURLRequestContextGetter( | 230 return new ChromeURLRequestContextGetter( |
| 249 profile, | |
| 250 new FactoryForIsolatedMedia(profile_io_data, app_id, app_context)); | 231 new FactoryForIsolatedMedia(profile_io_data, app_id, app_context)); |
| 251 } | 232 } |
| 252 | 233 |
| 253 // static | 234 // static |
| 254 ChromeURLRequestContextGetter* | 235 ChromeURLRequestContextGetter* |
| 255 ChromeURLRequestContextGetter::CreateOffTheRecord( | 236 ChromeURLRequestContextGetter::CreateOffTheRecord( |
| 256 Profile* profile, const ProfileIOData* profile_io_data) { | 237 Profile* profile, const ProfileIOData* profile_io_data) { |
| 257 DCHECK(profile->IsOffTheRecord()); | 238 DCHECK(profile->IsOffTheRecord()); |
| 258 return new ChromeURLRequestContextGetter( | 239 return new ChromeURLRequestContextGetter( |
| 259 profile, new FactoryForMain(profile_io_data)); | 240 new FactoryForMain(profile_io_data)); |
| 260 } | 241 } |
| 261 | 242 |
| 262 // static | 243 // static |
| 263 ChromeURLRequestContextGetter* | 244 ChromeURLRequestContextGetter* |
| 264 ChromeURLRequestContextGetter::CreateOffTheRecordForExtensions( | 245 ChromeURLRequestContextGetter::CreateOffTheRecordForExtensions( |
| 265 Profile* profile, const ProfileIOData* profile_io_data) { | 246 Profile* profile, const ProfileIOData* profile_io_data) { |
| 266 DCHECK(profile->IsOffTheRecord()); | 247 DCHECK(profile->IsOffTheRecord()); |
| 267 return new ChromeURLRequestContextGetter( | 248 return new ChromeURLRequestContextGetter( |
| 268 profile, new FactoryForExtensions(profile_io_data)); | 249 new FactoryForExtensions(profile_io_data)); |
| 269 } | 250 } |
| 270 | 251 |
| 271 // static | 252 // static |
| 272 ChromeURLRequestContextGetter* | 253 ChromeURLRequestContextGetter* |
| 273 ChromeURLRequestContextGetter::CreateOffTheRecordForIsolatedApp( | 254 ChromeURLRequestContextGetter::CreateOffTheRecordForIsolatedApp( |
| 274 Profile* profile, | 255 Profile* profile, |
| 275 const ProfileIOData* profile_io_data, | 256 const ProfileIOData* profile_io_data, |
| 276 const std::string& app_id, | 257 const std::string& app_id, |
| 277 scoped_ptr<net::URLRequestJobFactory::Interceptor> | 258 scoped_ptr<net::URLRequestJobFactory::Interceptor> |
| 278 protocol_handler_interceptor) { | 259 protocol_handler_interceptor) { |
| 279 DCHECK(profile->IsOffTheRecord()); | 260 DCHECK(profile->IsOffTheRecord()); |
| 280 ChromeURLRequestContextGetter* main_context = | 261 ChromeURLRequestContextGetter* main_context = |
| 281 static_cast<ChromeURLRequestContextGetter*>(profile->GetRequestContext()); | 262 static_cast<ChromeURLRequestContextGetter*>(profile->GetRequestContext()); |
| 282 return new ChromeURLRequestContextGetter( | 263 return new ChromeURLRequestContextGetter( |
| 283 profile, | |
| 284 new FactoryForIsolatedApp(profile_io_data, app_id, main_context, | 264 new FactoryForIsolatedApp(profile_io_data, app_id, main_context, |
| 285 protocol_handler_interceptor.Pass())); | 265 protocol_handler_interceptor.Pass())); |
| 286 } | 266 } |
| 287 | 267 |
| 288 void ChromeURLRequestContextGetter::CleanupOnUIThread() { | |
| 289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 290 // Unregister for pref notifications. | |
| 291 DCHECK(!registrar_.IsEmpty()) << "Called more than once!"; | |
| 292 registrar_.RemoveAll(); | |
| 293 } | |
| 294 | |
| 295 // content::NotificationObserver implementation. | |
| 296 void ChromeURLRequestContextGetter::Observe( | |
| 297 int type, | |
| 298 const content::NotificationSource& source, | |
| 299 const content::NotificationDetails& details) { | |
| 300 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 301 | |
| 302 if (chrome::NOTIFICATION_PREF_CHANGED == type) { | |
| 303 std::string* pref_name_in = content::Details<std::string>(details).ptr(); | |
| 304 PrefService* prefs = content::Source<PrefService>(source).ptr(); | |
| 305 DCHECK(pref_name_in && prefs); | |
| 306 if (*pref_name_in == prefs::kAcceptLanguages) { | |
| 307 std::string accept_language = | |
| 308 prefs->GetString(prefs::kAcceptLanguages); | |
| 309 BrowserThread::PostTask( | |
| 310 BrowserThread::IO, FROM_HERE, | |
| 311 base::Bind( | |
| 312 &ChromeURLRequestContextGetter::OnAcceptLanguageChange, | |
| 313 this, | |
| 314 accept_language)); | |
| 315 } else if (*pref_name_in == prefs::kDefaultCharset) { | |
| 316 std::string default_charset = prefs->GetString(prefs::kDefaultCharset); | |
| 317 BrowserThread::PostTask( | |
| 318 BrowserThread::IO, FROM_HERE, | |
| 319 base::Bind( | |
| 320 &ChromeURLRequestContextGetter::OnDefaultCharsetChange, | |
| 321 this, | |
| 322 default_charset)); | |
| 323 } | |
| 324 } else { | |
| 325 NOTREACHED(); | |
| 326 } | |
| 327 } | |
| 328 | |
| 329 void ChromeURLRequestContextGetter::RegisterPrefsObserver(Profile* profile) { | |
| 330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 331 | |
| 332 registrar_.Init(profile->GetPrefs()); | |
| 333 registrar_.Add(prefs::kAcceptLanguages, this); | |
| 334 registrar_.Add(prefs::kDefaultCharset, this); | |
| 335 } | |
| 336 | |
| 337 void ChromeURLRequestContextGetter::OnAcceptLanguageChange( | |
| 338 const std::string& accept_language) { | |
| 339 GetIOContext()->OnAcceptLanguageChange(accept_language); | |
| 340 } | |
| 341 | |
| 342 void ChromeURLRequestContextGetter::OnDefaultCharsetChange( | |
| 343 const std::string& default_charset) { | |
| 344 GetIOContext()->OnDefaultCharsetChange(default_charset); | |
| 345 } | |
| 346 | |
| 347 // ---------------------------------------------------------------------------- | 268 // ---------------------------------------------------------------------------- |
| 348 // ChromeURLRequestContext | 269 // ChromeURLRequestContext |
| 349 // ---------------------------------------------------------------------------- | 270 // ---------------------------------------------------------------------------- |
| 350 | 271 |
| 351 ChromeURLRequestContext::ChromeURLRequestContext( | 272 ChromeURLRequestContext::ChromeURLRequestContext( |
| 352 ContextType type, | 273 ContextType type, |
| 353 chrome_browser_net::LoadTimeStats* load_time_stats) | 274 chrome_browser_net::LoadTimeStats* load_time_stats) |
| 354 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 275 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 355 chrome_url_data_manager_backend_(NULL), | 276 chrome_url_data_manager_backend_(NULL), |
| 356 is_incognito_(false), | 277 is_incognito_(false), |
| (...skipping 20 matching lines...) Expand all Loading... |
| 377 ChromeURLDataManagerBackend* | 298 ChromeURLDataManagerBackend* |
| 378 ChromeURLRequestContext::chrome_url_data_manager_backend() const { | 299 ChromeURLRequestContext::chrome_url_data_manager_backend() const { |
| 379 return chrome_url_data_manager_backend_; | 300 return chrome_url_data_manager_backend_; |
| 380 } | 301 } |
| 381 | 302 |
| 382 void ChromeURLRequestContext::set_chrome_url_data_manager_backend( | 303 void ChromeURLRequestContext::set_chrome_url_data_manager_backend( |
| 383 ChromeURLDataManagerBackend* backend) { | 304 ChromeURLDataManagerBackend* backend) { |
| 384 DCHECK(backend); | 305 DCHECK(backend); |
| 385 chrome_url_data_manager_backend_ = backend; | 306 chrome_url_data_manager_backend_ = backend; |
| 386 } | 307 } |
| 387 | |
| 388 const std::string& ChromeURLRequestContext::GetUserAgent( | |
| 389 const GURL& url) const { | |
| 390 return content::GetUserAgent(url); | |
| 391 } | |
| 392 | |
| 393 void ChromeURLRequestContext::OnAcceptLanguageChange( | |
| 394 const std::string& accept_language) { | |
| 395 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 396 set_accept_language( | |
| 397 net::HttpUtil::GenerateAcceptLanguageHeader(accept_language)); | |
| 398 } | |
| 399 | |
| 400 void ChromeURLRequestContext::OnDefaultCharsetChange( | |
| 401 const std::string& default_charset) { | |
| 402 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 403 set_referrer_charset(default_charset); | |
| 404 set_accept_charset( | |
| 405 net::HttpUtil::GenerateAcceptCharsetHeader(default_charset)); | |
| 406 } | |
| OLD | NEW |