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 "base/stl_util-inl.h" |
9 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/io_thread.h" | 11 #include "chrome/browser/io_thread.h" |
11 #include "chrome/browser/net/chrome_cookie_policy.h" | 12 #include "chrome/browser/net/chrome_cookie_policy.h" |
12 #include "chrome/browser/net/chrome_dns_cert_provenance_checker_factory.h" | 13 #include "chrome/browser/net/chrome_dns_cert_provenance_checker_factory.h" |
13 #include "chrome/browser/net/chrome_net_log.h" | 14 #include "chrome/browser/net/chrome_net_log.h" |
14 #include "chrome/browser/net/chrome_network_delegate.h" | 15 #include "chrome/browser/net/chrome_network_delegate.h" |
15 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" | 16 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" |
16 #include "chrome/common/chrome_constants.h" | 17 #include "chrome/common/chrome_constants.h" |
17 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/extensions/extension.h" |
| 20 #include "chrome/common/pref_names.h" |
18 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
19 #include "content/browser/browser_thread.h" | 22 #include "content/browser/browser_thread.h" |
20 #include "net/ftp/ftp_network_layer.h" | 23 #include "net/ftp/ftp_network_layer.h" |
21 #include "net/http/http_cache.h" | 24 #include "net/http/http_cache.h" |
22 | 25 |
23 ProfileImplIOData::Handle::Handle(Profile* profile) | 26 ProfileImplIOData::Handle::Handle(Profile* profile) |
24 : io_data_(new ProfileImplIOData), | 27 : io_data_(new ProfileImplIOData), |
25 profile_(profile), | 28 profile_(profile), |
26 initialized_(false) { | 29 initialized_(false) { |
27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
28 DCHECK(profile); | 31 DCHECK(profile); |
29 } | 32 } |
30 | 33 |
31 ProfileImplIOData::Handle::~Handle() { | 34 ProfileImplIOData::Handle::~Handle() { |
32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
33 if (main_request_context_getter_) | 36 if (main_request_context_getter_) |
34 main_request_context_getter_->CleanupOnUIThread(); | 37 main_request_context_getter_->CleanupOnUIThread(); |
35 if (media_request_context_getter_) | 38 if (media_request_context_getter_) |
36 media_request_context_getter_->CleanupOnUIThread(); | 39 media_request_context_getter_->CleanupOnUIThread(); |
37 if (extensions_request_context_getter_) | 40 if (extensions_request_context_getter_) |
38 extensions_request_context_getter_->CleanupOnUIThread(); | 41 extensions_request_context_getter_->CleanupOnUIThread(); |
| 42 |
| 43 // Clean up all isolated app request contexts. |
| 44 for (ChromeURLRequestContextGetterMap::iterator iter = |
| 45 app_request_context_getter_map_.begin(); |
| 46 iter != app_request_context_getter_map_.end(); |
| 47 ++iter) { |
| 48 iter->second->CleanupOnUIThread(); |
| 49 } |
39 } | 50 } |
40 | 51 |
41 void ProfileImplIOData::Handle::Init(const FilePath& cookie_path, | 52 void ProfileImplIOData::Handle::Init(const FilePath& cookie_path, |
42 const FilePath& cache_path, | 53 const FilePath& cache_path, |
43 int cache_max_size, | 54 int cache_max_size, |
44 const FilePath& media_cache_path, | 55 const FilePath& media_cache_path, |
45 int media_cache_max_size, | 56 int media_cache_max_size, |
46 const FilePath& extensions_cookie_path) { | 57 const FilePath& extensions_cookie_path) { |
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
48 DCHECK(!io_data_->lazy_params_.get()); | 59 DCHECK(!io_data_->lazy_params_.get()); |
49 LazyParams* lazy_params = new LazyParams; | 60 LazyParams* lazy_params = new LazyParams; |
50 | 61 |
51 lazy_params->cookie_path = cookie_path; | 62 lazy_params->cookie_path = cookie_path; |
52 lazy_params->cache_path = cache_path; | 63 lazy_params->cache_path = cache_path; |
53 lazy_params->cache_max_size = cache_max_size; | 64 lazy_params->cache_max_size = cache_max_size; |
54 lazy_params->media_cache_path = media_cache_path; | 65 lazy_params->media_cache_path = media_cache_path; |
55 lazy_params->media_cache_max_size = media_cache_max_size; | 66 lazy_params->media_cache_max_size = media_cache_max_size; |
56 lazy_params->extensions_cookie_path = extensions_cookie_path; | 67 lazy_params->extensions_cookie_path = extensions_cookie_path; |
57 | 68 |
58 lazy_params->io_thread = g_browser_process->io_thread(); | 69 lazy_params->io_thread = g_browser_process->io_thread(); |
59 | 70 |
60 io_data_->lazy_params_.reset(lazy_params); | 71 io_data_->lazy_params_.reset(lazy_params); |
61 } | 72 } |
62 | 73 |
| 74 void ProfileImplIOData::Handle::InitIsolatedApp(const Extension* installed_app, |
| 75 const FilePath& cookie_path, |
| 76 const FilePath& cache_path, |
| 77 int cache_max_size) { |
| 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 79 |
| 80 // This may be called multiple times before InitializeAppRequestContext. |
| 81 // Only do the initialization once. |
| 82 std::string id = installed_app->id(); |
| 83 if (io_data_->lazy_params_map_.find(id) != io_data_->lazy_params_map_.end()) |
| 84 return; |
| 85 |
| 86 // We only need to keep track of parameters that differ from the main context. |
| 87 LazyParams* lazy_params = new LazyParams; |
| 88 lazy_params->cookie_path = cookie_path; |
| 89 lazy_params->cache_path = cache_path; |
| 90 lazy_params->cache_max_size = cache_max_size; |
| 91 |
| 92 // Needed for the cookie store. |
| 93 lazy_params->profile_params.clear_local_state_on_exit = |
| 94 profile_->GetPrefs()->GetBoolean(prefs::kClearSiteDataOnExit); |
| 95 |
| 96 // Keep track of this app's LazyParams until InitializeAppRequestContext. |
| 97 io_data_->lazy_params_map_[id] = lazy_params; |
| 98 } |
| 99 |
63 scoped_refptr<ChromeURLRequestContextGetter> | 100 scoped_refptr<ChromeURLRequestContextGetter> |
64 ProfileImplIOData::Handle::GetMainRequestContextGetter() const { | 101 ProfileImplIOData::Handle::GetMainRequestContextGetter() const { |
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
66 LazyInitialize(); | 103 LazyInitialize(); |
67 if (!main_request_context_getter_) { | 104 if (!main_request_context_getter_) { |
68 main_request_context_getter_ = | 105 main_request_context_getter_ = |
69 ChromeURLRequestContextGetter::CreateOriginal( | 106 ChromeURLRequestContextGetter::CreateOriginal( |
70 profile_, io_data_); | 107 profile_, io_data_); |
71 } | 108 } |
72 return main_request_context_getter_; | 109 return main_request_context_getter_; |
(...skipping 16 matching lines...) Expand all Loading... |
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
90 LazyInitialize(); | 127 LazyInitialize(); |
91 if (!extensions_request_context_getter_) { | 128 if (!extensions_request_context_getter_) { |
92 extensions_request_context_getter_ = | 129 extensions_request_context_getter_ = |
93 ChromeURLRequestContextGetter::CreateOriginalForExtensions( | 130 ChromeURLRequestContextGetter::CreateOriginalForExtensions( |
94 profile_, io_data_); | 131 profile_, io_data_); |
95 } | 132 } |
96 return extensions_request_context_getter_; | 133 return extensions_request_context_getter_; |
97 } | 134 } |
98 | 135 |
| 136 scoped_refptr<ChromeURLRequestContextGetter> |
| 137 ProfileImplIOData::Handle::GetIsolatedAppRequestContextGetter( |
| 138 const Extension* installed_app) const { |
| 139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 140 CHECK(installed_app); |
| 141 LazyInitialize(); |
| 142 |
| 143 // Keep a map of request context getters, one per requested app ID. |
| 144 std::string id = installed_app->id(); |
| 145 ChromeURLRequestContextGetterMap::iterator iter = |
| 146 app_request_context_getter_map_.find(id); |
| 147 if (iter != app_request_context_getter_map_.end()) |
| 148 return iter->second; |
| 149 |
| 150 ChromeURLRequestContextGetter* context = |
| 151 ChromeURLRequestContextGetter::CreateOriginalForIsolatedApp( |
| 152 profile_, io_data_, installed_app); |
| 153 app_request_context_getter_map_[id] = context; |
| 154 |
| 155 return context; |
| 156 } |
| 157 |
99 void ProfileImplIOData::Handle::LazyInitialize() const { | 158 void ProfileImplIOData::Handle::LazyInitialize() const { |
100 if (!initialized_) { | 159 if (!initialized_) { |
101 InitializeProfileParams(profile_, &io_data_->lazy_params_->profile_params); | 160 InitializeProfileParams(profile_, &io_data_->lazy_params_->profile_params); |
102 initialized_ = true; | 161 initialized_ = true; |
103 } | 162 } |
104 } | 163 } |
105 | 164 |
106 ProfileImplIOData::LazyParams::LazyParams() | 165 ProfileImplIOData::LazyParams::LazyParams() |
107 : cache_max_size(0), | 166 : cache_max_size(0), |
108 media_cache_max_size(0), | 167 media_cache_max_size(0), |
109 io_thread(NULL) {} | 168 io_thread(NULL) {} |
110 ProfileImplIOData::LazyParams::~LazyParams() {} | 169 ProfileImplIOData::LazyParams::~LazyParams() {} |
111 | 170 |
112 ProfileImplIOData::ProfileImplIOData() : ProfileIOData(false) {} | 171 ProfileImplIOData::ProfileImplIOData() : ProfileIOData(false) {} |
113 ProfileImplIOData::~ProfileImplIOData() {} | 172 ProfileImplIOData::~ProfileImplIOData() { |
| 173 STLDeleteValues(&lazy_params_map_); |
| 174 } |
114 | 175 |
115 void ProfileImplIOData::LazyInitializeInternal() const { | 176 void ProfileImplIOData::LazyInitializeInternal() const { |
116 main_request_context_ = new RequestContext; | 177 main_request_context_ = new RequestContext; |
117 media_request_context_ = new RequestContext; | 178 media_request_context_ = new RequestContext; |
118 extensions_request_context_ = new RequestContext; | 179 extensions_request_context_ = new RequestContext; |
119 | 180 |
120 IOThread* const io_thread = lazy_params_->io_thread; | 181 IOThread* const io_thread = lazy_params_->io_thread; |
121 IOThread::Globals* const io_thread_globals = io_thread->globals(); | 182 IOThread::Globals* const io_thread_globals = io_thread->globals(); |
122 const ProfileParams& profile_params = lazy_params_->profile_params; | 183 const ProfileParams& profile_params = lazy_params_->profile_params; |
123 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 184 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 // Enable cookies for devtools and extension URLs. | 297 // Enable cookies for devtools and extension URLs. |
237 const char* schemes[] = {chrome::kChromeDevToolsScheme, | 298 const char* schemes[] = {chrome::kChromeDevToolsScheme, |
238 chrome::kExtensionScheme}; | 299 chrome::kExtensionScheme}; |
239 extensions_cookie_store->SetCookieableSchemes(schemes, 2); | 300 extensions_cookie_store->SetCookieableSchemes(schemes, 2); |
240 | 301 |
241 main_request_context_->set_cookie_store(cookie_store); | 302 main_request_context_->set_cookie_store(cookie_store); |
242 media_request_context_->set_cookie_store(cookie_store); | 303 media_request_context_->set_cookie_store(cookie_store); |
243 extensions_request_context_->set_cookie_store( | 304 extensions_request_context_->set_cookie_store( |
244 extensions_cookie_store); | 305 extensions_cookie_store); |
245 | 306 |
| 307 main_request_context_->set_cookie_delegate( |
| 308 profile_params.cookie_monster_delegate); |
| 309 media_request_context_->set_cookie_delegate( |
| 310 profile_params.cookie_monster_delegate); |
| 311 |
246 main_http_factory_.reset(main_cache); | 312 main_http_factory_.reset(main_cache); |
247 media_http_factory_.reset(media_cache); | 313 media_http_factory_.reset(media_cache); |
248 main_request_context_->set_http_transaction_factory(main_cache); | 314 main_request_context_->set_http_transaction_factory(main_cache); |
249 media_request_context_->set_http_transaction_factory(media_cache); | 315 media_request_context_->set_http_transaction_factory(media_cache); |
250 | 316 |
251 main_request_context_->set_ftp_transaction_factory( | 317 main_request_context_->set_ftp_transaction_factory( |
252 new net::FtpNetworkLayer(io_thread_globals->host_resolver.get())); | 318 new net::FtpNetworkLayer(io_thread_globals->host_resolver.get())); |
253 | 319 |
254 lazy_params_.reset(); | 320 lazy_params_.reset(); |
255 } | 321 } |
256 | 322 |
| 323 scoped_refptr<ProfileIOData::RequestContext> |
| 324 ProfileImplIOData::InitializeAppRequestContext( |
| 325 scoped_refptr<ChromeURLRequestContext> main_context, |
| 326 const Extension* installed_app) const { |
| 327 scoped_refptr<ProfileIOData::RequestContext> context = new RequestContext; |
| 328 |
| 329 // Copy most state from the main context. |
| 330 main_context->Copy(context); |
| 331 |
| 332 // Get the app-specific LazyParams, which was created in InitIsolatedApp. |
| 333 std::string id = installed_app->id(); |
| 334 LazyParamsMap::iterator iter = lazy_params_map_.find(id); |
| 335 DCHECK(iter != lazy_params_map_.end()); |
| 336 scoped_ptr<LazyParams> lazy_params(iter->second); |
| 337 |
| 338 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 339 bool record_mode = chrome::kRecordModeEnabled && |
| 340 command_line.HasSwitch(switches::kRecordMode); |
| 341 bool playback_mode = command_line.HasSwitch(switches::kPlaybackMode); |
| 342 |
| 343 // Use a separate HTTP disk cache for isolated apps. |
| 344 net::HttpCache::DefaultBackend* app_backend = |
| 345 new net::HttpCache::DefaultBackend( |
| 346 net::DISK_CACHE, |
| 347 lazy_params->cache_path, |
| 348 lazy_params->cache_max_size, |
| 349 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)); |
| 350 net::HttpNetworkSession* main_network_session = |
| 351 main_http_factory_->GetSession(); |
| 352 net::HttpCache* app_cache = |
| 353 new net::HttpCache(main_network_session, app_backend); |
| 354 |
| 355 scoped_refptr<net::CookieStore> cookie_store = NULL; |
| 356 if (record_mode || playback_mode) { |
| 357 // Don't use existing cookies and use an in-memory store. |
| 358 cookie_store = new net::CookieMonster( |
| 359 NULL, main_context->cookie_delegate()); |
| 360 app_cache->set_mode( |
| 361 record_mode ? net::HttpCache::RECORD : net::HttpCache::PLAYBACK); |
| 362 } |
| 363 |
| 364 // Use an app-specific cookie store. |
| 365 if (!cookie_store) { |
| 366 DCHECK(!lazy_params->cookie_path.empty()); |
| 367 |
| 368 scoped_refptr<SQLitePersistentCookieStore> cookie_db = |
| 369 new SQLitePersistentCookieStore(lazy_params->cookie_path); |
| 370 cookie_db->SetClearLocalStateOnExit( |
| 371 lazy_params->profile_params.clear_local_state_on_exit); |
| 372 cookie_store = |
| 373 new net::CookieMonster(cookie_db.get(), |
| 374 main_context->cookie_delegate()); |
| 375 } |
| 376 |
| 377 context->set_cookie_store(cookie_store); |
| 378 context->set_cookie_delegate(main_context->cookie_delegate()); |
| 379 |
| 380 context->set_http_transaction_factory(app_cache); |
| 381 |
| 382 lazy_params_map_.erase(iter); |
| 383 return context; |
| 384 } |
| 385 |
257 scoped_refptr<ChromeURLRequestContext> | 386 scoped_refptr<ChromeURLRequestContext> |
258 ProfileImplIOData::AcquireMainRequestContext() const { | 387 ProfileImplIOData::AcquireMainRequestContext() const { |
259 DCHECK(main_request_context_); | 388 DCHECK(main_request_context_); |
260 scoped_refptr<ChromeURLRequestContext> context = main_request_context_; | 389 scoped_refptr<ChromeURLRequestContext> context = main_request_context_; |
261 main_request_context_->set_profile_io_data(this); | 390 main_request_context_->set_profile_io_data(this); |
262 main_request_context_ = NULL; | 391 main_request_context_ = NULL; |
263 return context; | 392 return context; |
264 } | 393 } |
265 | 394 |
266 scoped_refptr<ChromeURLRequestContext> | 395 scoped_refptr<ChromeURLRequestContext> |
267 ProfileImplIOData::AcquireMediaRequestContext() const { | 396 ProfileImplIOData::AcquireMediaRequestContext() const { |
268 DCHECK(media_request_context_); | 397 DCHECK(media_request_context_); |
269 scoped_refptr<ChromeURLRequestContext> context = media_request_context_; | 398 scoped_refptr<ChromeURLRequestContext> context = media_request_context_; |
270 media_request_context_->set_profile_io_data(this); | 399 media_request_context_->set_profile_io_data(this); |
271 media_request_context_ = NULL; | 400 media_request_context_ = NULL; |
272 return context; | 401 return context; |
273 } | 402 } |
274 | 403 |
275 scoped_refptr<ChromeURLRequestContext> | 404 scoped_refptr<ChromeURLRequestContext> |
276 ProfileImplIOData::AcquireExtensionsRequestContext() const { | 405 ProfileImplIOData::AcquireExtensionsRequestContext() const { |
277 DCHECK(extensions_request_context_); | 406 DCHECK(extensions_request_context_); |
278 scoped_refptr<ChromeURLRequestContext> context = extensions_request_context_; | 407 scoped_refptr<ChromeURLRequestContext> context = extensions_request_context_; |
279 extensions_request_context_->set_profile_io_data(this); | 408 extensions_request_context_->set_profile_io_data(this); |
280 extensions_request_context_ = NULL; | 409 extensions_request_context_ = NULL; |
281 return context; | 410 return context; |
282 } | 411 } |
| 412 |
| 413 scoped_refptr<ChromeURLRequestContext> |
| 414 ProfileImplIOData::AcquireIsolatedAppRequestContext( |
| 415 scoped_refptr<ChromeURLRequestContext> main_context, |
| 416 const Extension* installed_app) const { |
| 417 // We create per-app contexts on demand, unlike the others above. |
| 418 scoped_refptr<RequestContext> app_request_context = |
| 419 InitializeAppRequestContext(main_context, installed_app); |
| 420 DCHECK(app_request_context); |
| 421 app_request_context->set_profile_io_data(this); |
| 422 return app_request_context; |
| 423 } |
OLD | NEW |