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