OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/profiles/profile_impl_io_data.h" | 5 #include "chrome/browser/profiles/profile_impl_io_data.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/browser_thread.h" | 10 #include "chrome/browser/browser_thread.h" |
11 #include "chrome/browser/extensions/extension_io_event_router.h" | 11 #include "chrome/browser/extensions/extension_io_event_router.h" |
12 #include "chrome/browser/io_thread.h" | 12 #include "chrome/browser/io_thread.h" |
13 #include "chrome/browser/net/chrome_cookie_policy.h" | 13 #include "chrome/browser/net/chrome_cookie_policy.h" |
14 #include "chrome/browser/net/chrome_dns_cert_provenance_checker_factory.h" | 14 #include "chrome/browser/net/chrome_dns_cert_provenance_checker_factory.h" |
15 #include "chrome/browser/net/chrome_net_log.h" | 15 #include "chrome/browser/net/chrome_net_log.h" |
16 #include "chrome/browser/net/chrome_network_delegate.h" | 16 #include "chrome/browser/net/chrome_network_delegate.h" |
17 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" | 17 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" |
18 #include "chrome/common/chrome_constants.h" | 18 #include "chrome/common/chrome_constants.h" |
19 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/extensions/extension.h" |
20 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
21 #include "net/ftp/ftp_network_layer.h" | 22 #include "net/ftp/ftp_network_layer.h" |
22 #include "net/http/http_cache.h" | 23 #include "net/http/http_cache.h" |
23 | 24 |
24 ProfileImplIOData::Handle::Handle(Profile* profile) | 25 ProfileImplIOData::Handle::Handle(Profile* profile) |
25 : io_data_(new ProfileImplIOData), | 26 : io_data_(new ProfileImplIOData), |
26 profile_(profile), | 27 profile_(profile), |
27 initialized_(false) { | 28 initialized_(false) { |
28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
29 DCHECK(profile); | 30 DCHECK(profile); |
30 } | 31 } |
31 | 32 |
32 ProfileImplIOData::Handle::~Handle() { | 33 ProfileImplIOData::Handle::~Handle() { |
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
34 if (main_request_context_getter_) | 35 if (main_request_context_getter_) |
35 main_request_context_getter_->CleanupOnUIThread(); | 36 main_request_context_getter_->CleanupOnUIThread(); |
36 if (media_request_context_getter_) | 37 if (media_request_context_getter_) |
37 media_request_context_getter_->CleanupOnUIThread(); | 38 media_request_context_getter_->CleanupOnUIThread(); |
38 if (extensions_request_context_getter_) | 39 if (extensions_request_context_getter_) |
39 extensions_request_context_getter_->CleanupOnUIThread(); | 40 extensions_request_context_getter_->CleanupOnUIThread(); |
| 41 |
| 42 // Clean up all isolated app request contexts. |
| 43 for (ChromeURLRequestContextGetterMap::iterator iter = |
| 44 app_request_context_getter_map_.begin(); |
| 45 iter != app_request_context_getter_map_.end(); |
| 46 ++iter) { |
| 47 iter->second->CleanupOnUIThread(); |
| 48 } |
40 } | 49 } |
41 | 50 |
42 void ProfileImplIOData::Handle::Init(const FilePath& cookie_path, | 51 void ProfileImplIOData::Handle::Init(const FilePath& cookie_path, |
43 const FilePath& cache_path, | 52 const FilePath& cache_path, |
44 int cache_max_size, | 53 int cache_max_size, |
45 const FilePath& media_cache_path, | 54 const FilePath& media_cache_path, |
46 int media_cache_max_size, | 55 int media_cache_max_size, |
47 const FilePath& extensions_cookie_path) { | 56 const FilePath& extensions_cookie_path) { |
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
49 DCHECK(!io_data_->lazy_params_.get()); | 58 DCHECK(!io_data_->lazy_params_.get()); |
50 LazyParams* lazy_params = new LazyParams; | 59 LazyParams* lazy_params = new LazyParams; |
51 | 60 |
52 lazy_params->cookie_path = cookie_path; | 61 lazy_params->cookie_path = cookie_path; |
53 lazy_params->cache_path = cache_path; | 62 lazy_params->cache_path = cache_path; |
54 lazy_params->cache_max_size = cache_max_size; | 63 lazy_params->cache_max_size = cache_max_size; |
55 lazy_params->media_cache_path = media_cache_path; | 64 lazy_params->media_cache_path = media_cache_path; |
56 lazy_params->media_cache_max_size = media_cache_max_size; | 65 lazy_params->media_cache_max_size = media_cache_max_size; |
57 lazy_params->extensions_cookie_path = extensions_cookie_path; | 66 lazy_params->extensions_cookie_path = extensions_cookie_path; |
58 | 67 |
59 lazy_params->io_thread = g_browser_process->io_thread(); | 68 lazy_params->io_thread = g_browser_process->io_thread(); |
60 | 69 |
61 io_data_->lazy_params_.reset(lazy_params); | 70 io_data_->lazy_params_.reset(lazy_params); |
62 } | 71 } |
63 | 72 |
| 73 void ProfileImplIOData::Handle::InitIsolatedApp(const Extension* installed_app, |
| 74 const FilePath& cookie_path, |
| 75 const FilePath& cache_path, |
| 76 int cache_max_size) { |
| 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 78 |
| 79 // This may be called multiple times before InitializeAppRequestContext. |
| 80 // Only do the initialization once. |
| 81 std::string id = installed_app->id(); |
| 82 if (io_data_->lazy_params_map_.find(id) != io_data_->lazy_params_map_.end()) |
| 83 return; |
| 84 |
| 85 LazyParams* lazy_params = new LazyParams; |
| 86 lazy_params->cookie_path = cookie_path; |
| 87 lazy_params->cache_path = cache_path; |
| 88 lazy_params->cache_max_size = cache_max_size; |
| 89 |
| 90 lazy_params->io_thread = g_browser_process->io_thread(); |
| 91 |
| 92 InitializeProfileParams(profile_, &lazy_params->profile_params); |
| 93 |
| 94 // Keep track of this app's LazyParams until InitializeAppRequestContext. |
| 95 io_data_->lazy_params_map_[id] = lazy_params; |
| 96 } |
| 97 |
64 scoped_refptr<ChromeURLRequestContextGetter> | 98 scoped_refptr<ChromeURLRequestContextGetter> |
65 ProfileImplIOData::Handle::GetMainRequestContextGetter() const { | 99 ProfileImplIOData::Handle::GetMainRequestContextGetter() const { |
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
67 LazyInitialize(); | 101 LazyInitialize(); |
68 if (!main_request_context_getter_) { | 102 if (!main_request_context_getter_) { |
69 main_request_context_getter_ = | 103 main_request_context_getter_ = |
70 ChromeURLRequestContextGetter::CreateOriginal( | 104 ChromeURLRequestContextGetter::CreateOriginal( |
71 profile_, io_data_); | 105 profile_, io_data_); |
72 } | 106 } |
73 return main_request_context_getter_; | 107 return main_request_context_getter_; |
(...skipping 16 matching lines...) Expand all Loading... |
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
91 LazyInitialize(); | 125 LazyInitialize(); |
92 if (!extensions_request_context_getter_) { | 126 if (!extensions_request_context_getter_) { |
93 extensions_request_context_getter_ = | 127 extensions_request_context_getter_ = |
94 ChromeURLRequestContextGetter::CreateOriginalForExtensions( | 128 ChromeURLRequestContextGetter::CreateOriginalForExtensions( |
95 profile_, io_data_); | 129 profile_, io_data_); |
96 } | 130 } |
97 return extensions_request_context_getter_; | 131 return extensions_request_context_getter_; |
98 } | 132 } |
99 | 133 |
| 134 scoped_refptr<ChromeURLRequestContextGetter> |
| 135 ProfileImplIOData::Handle::GetIsolatedAppRequestContextGetter( |
| 136 const Extension* installed_app) const { |
| 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 138 CHECK(installed_app); |
| 139 LazyInitialize(); |
| 140 |
| 141 // Keep a map of request context getters, one per requested app ID. |
| 142 std::string id = installed_app->id(); |
| 143 ChromeURLRequestContextGetterMap::iterator iter = |
| 144 app_request_context_getter_map_.find(id); |
| 145 if (iter != app_request_context_getter_map_.end()) |
| 146 return iter->second; |
| 147 |
| 148 ChromeURLRequestContextGetter* context = |
| 149 ChromeURLRequestContextGetter::CreateOriginalForIsolatedApp( |
| 150 profile_, io_data_, installed_app); |
| 151 app_request_context_getter_map_[id] = context; |
| 152 |
| 153 return context; |
| 154 } |
| 155 |
100 void ProfileImplIOData::Handle::LazyInitialize() const { | 156 void ProfileImplIOData::Handle::LazyInitialize() const { |
101 if (!initialized_) { | 157 if (!initialized_) { |
102 InitializeProfileParams(profile_, &io_data_->lazy_params_->profile_params); | 158 InitializeProfileParams(profile_, &io_data_->lazy_params_->profile_params); |
103 initialized_ = true; | 159 initialized_ = true; |
104 } | 160 } |
105 } | 161 } |
106 | 162 |
107 ProfileImplIOData::LazyParams::LazyParams() | 163 ProfileImplIOData::LazyParams::LazyParams() |
108 : cache_max_size(0), | 164 : cache_max_size(0), |
109 media_cache_max_size(0), | 165 media_cache_max_size(0), |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 media_http_factory_.reset(media_cache); | 304 media_http_factory_.reset(media_cache); |
249 main_request_context_->set_http_transaction_factory(main_cache); | 305 main_request_context_->set_http_transaction_factory(main_cache); |
250 media_request_context_->set_http_transaction_factory(media_cache); | 306 media_request_context_->set_http_transaction_factory(media_cache); |
251 | 307 |
252 main_request_context_->set_ftp_transaction_factory( | 308 main_request_context_->set_ftp_transaction_factory( |
253 new net::FtpNetworkLayer(io_thread_globals->host_resolver.get())); | 309 new net::FtpNetworkLayer(io_thread_globals->host_resolver.get())); |
254 | 310 |
255 lazy_params_.reset(); | 311 lazy_params_.reset(); |
256 } | 312 } |
257 | 313 |
| 314 scoped_refptr<ProfileIOData::RequestContext> |
| 315 ProfileImplIOData::InitializeAppRequestContext(const Extension* app) const { |
| 316 scoped_refptr<ProfileIOData::RequestContext> context = new RequestContext; |
| 317 |
| 318 // Get the app-specific LazyParams, which was created in InitIsolatedApp. |
| 319 std::string id = app->id(); |
| 320 LazyParamsMap::iterator iter = lazy_params_map_.find(id); |
| 321 DCHECK(iter != lazy_params_map_.end()); |
| 322 scoped_ptr<LazyParams> lazy_params(iter->second); |
| 323 |
| 324 IOThread* const io_thread = lazy_params->io_thread; |
| 325 IOThread::Globals* const io_thread_globals = io_thread->globals(); |
| 326 const ProfileParams& profile_params = lazy_params->profile_params; |
| 327 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 328 bool record_mode = chrome::kRecordModeEnabled && |
| 329 command_line.HasSwitch(switches::kRecordMode); |
| 330 bool playback_mode = command_line.HasSwitch(switches::kPlaybackMode); |
| 331 |
| 332 // Initialize context members. |
| 333 |
| 334 ApplyProfileParamsToContext(profile_params, context); |
| 335 |
| 336 scoped_refptr<ChromeCookiePolicy> cookie_policy = |
| 337 new ChromeCookiePolicy(profile_params.host_content_settings_map); |
| 338 context->set_chrome_cookie_policy(cookie_policy); |
| 339 |
| 340 context->set_net_log(lazy_params->io_thread->net_log()); |
| 341 |
| 342 DCHECK(network_delegate_.get()); |
| 343 context->set_network_delegate(network_delegate_.get()); |
| 344 |
| 345 context->set_host_resolver(io_thread_globals->host_resolver.get()); |
| 346 context->set_cert_verifier(io_thread_globals->cert_verifier.get()); |
| 347 context->set_dnsrr_resolver(io_thread_globals->dnsrr_resolver.get()); |
| 348 context->set_http_auth_handler_factory( |
| 349 io_thread_globals->http_auth_handler_factory.get()); |
| 350 |
| 351 DCHECK(dns_cert_checker_.get()); |
| 352 context->set_dns_cert_checker(dns_cert_checker_.get()); |
| 353 |
| 354 net::ProxyService* proxy_service = |
| 355 CreateProxyService( |
| 356 io_thread->net_log(), |
| 357 io_thread_globals->proxy_script_fetcher_context.get(), |
| 358 lazy_params->profile_params.proxy_config_service.release(), |
| 359 command_line); |
| 360 context->set_proxy_service(proxy_service); |
| 361 |
| 362 // Use a separate HTTP disk cache for isolated apps. |
| 363 net::HttpCache::DefaultBackend* app_backend = |
| 364 new net::HttpCache::DefaultBackend( |
| 365 net::DISK_CACHE, |
| 366 lazy_params->cache_path, |
| 367 lazy_params->cache_max_size, |
| 368 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)); |
| 369 net::HttpNetworkSession* main_network_session = main_http_factory_->GetSession
(); |
| 370 net::HttpCache* app_cache = |
| 371 new net::HttpCache(main_network_session, app_backend); |
| 372 |
| 373 scoped_refptr<net::CookieStore> cookie_store = NULL; |
| 374 if (record_mode || playback_mode) { |
| 375 // Don't use existing cookies and use an in-memory store. |
| 376 cookie_store = new net::CookieMonster( |
| 377 NULL, profile_params.cookie_monster_delegate); |
| 378 app_cache->set_mode( |
| 379 record_mode ? net::HttpCache::RECORD : net::HttpCache::PLAYBACK); |
| 380 } |
| 381 |
| 382 // Use an app-specific cookie store. |
| 383 if (!cookie_store) { |
| 384 DCHECK(!lazy_params->cookie_path.empty()); |
| 385 |
| 386 scoped_refptr<SQLitePersistentCookieStore> cookie_db = |
| 387 new SQLitePersistentCookieStore(lazy_params->cookie_path); |
| 388 cookie_db->SetClearLocalStateOnExit( |
| 389 profile_params.clear_local_state_on_exit); |
| 390 cookie_store = |
| 391 new net::CookieMonster(cookie_db.get(), |
| 392 profile_params.cookie_monster_delegate); |
| 393 } |
| 394 |
| 395 context->set_cookie_store(cookie_store); |
| 396 |
| 397 context->set_http_transaction_factory(app_cache); |
| 398 |
| 399 context->set_ftp_transaction_factory( |
| 400 new net::FtpNetworkLayer(io_thread_globals->host_resolver.get())); |
| 401 |
| 402 lazy_params_map_.erase(iter); |
| 403 return context; |
| 404 } |
| 405 |
258 scoped_refptr<ChromeURLRequestContext> | 406 scoped_refptr<ChromeURLRequestContext> |
259 ProfileImplIOData::AcquireMainRequestContext() const { | 407 ProfileImplIOData::AcquireMainRequestContext() const { |
260 DCHECK(main_request_context_); | 408 DCHECK(main_request_context_); |
261 scoped_refptr<ChromeURLRequestContext> context = main_request_context_; | 409 scoped_refptr<ChromeURLRequestContext> context = main_request_context_; |
262 main_request_context_->set_profile_io_data(this); | 410 main_request_context_->set_profile_io_data(this); |
263 main_request_context_ = NULL; | 411 main_request_context_ = NULL; |
264 return context; | 412 return context; |
265 } | 413 } |
266 | 414 |
267 scoped_refptr<ChromeURLRequestContext> | 415 scoped_refptr<ChromeURLRequestContext> |
268 ProfileImplIOData::AcquireMediaRequestContext() const { | 416 ProfileImplIOData::AcquireMediaRequestContext() const { |
269 DCHECK(media_request_context_); | 417 DCHECK(media_request_context_); |
270 scoped_refptr<ChromeURLRequestContext> context = media_request_context_; | 418 scoped_refptr<ChromeURLRequestContext> context = media_request_context_; |
271 media_request_context_->set_profile_io_data(this); | 419 media_request_context_->set_profile_io_data(this); |
272 media_request_context_ = NULL; | 420 media_request_context_ = NULL; |
273 return context; | 421 return context; |
274 } | 422 } |
275 | 423 |
276 scoped_refptr<ChromeURLRequestContext> | 424 scoped_refptr<ChromeURLRequestContext> |
277 ProfileImplIOData::AcquireExtensionsRequestContext() const { | 425 ProfileImplIOData::AcquireExtensionsRequestContext() const { |
278 DCHECK(extensions_request_context_); | 426 DCHECK(extensions_request_context_); |
279 scoped_refptr<ChromeURLRequestContext> context = extensions_request_context_; | 427 scoped_refptr<ChromeURLRequestContext> context = extensions_request_context_; |
280 extensions_request_context_->set_profile_io_data(this); | 428 extensions_request_context_->set_profile_io_data(this); |
281 extensions_request_context_ = NULL; | 429 extensions_request_context_ = NULL; |
282 return context; | 430 return context; |
283 } | 431 } |
| 432 |
| 433 scoped_refptr<ChromeURLRequestContext> |
| 434 ProfileImplIOData::AcquireIsolatedAppRequestContext( |
| 435 const Extension* installed_app) const { |
| 436 // We create per-app contexts on demand, unlike the others above. |
| 437 scoped_refptr<RequestContext> app_request_context = |
| 438 InitializeAppRequestContext(installed_app); |
| 439 DCHECK(app_request_context); |
| 440 app_request_context->set_profile_io_data(this); |
| 441 return app_request_context; |
| 442 } |
OLD | NEW |