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/file_util.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stl_util-inl.h" |
9 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/io_thread.h" | 12 #include "chrome/browser/io_thread.h" |
11 #include "chrome/browser/net/chrome_cookie_policy.h" | 13 #include "chrome/browser/net/chrome_cookie_policy.h" |
12 #include "chrome/browser/net/chrome_dns_cert_provenance_checker_factory.h" | 14 #include "chrome/browser/net/chrome_dns_cert_provenance_checker_factory.h" |
13 #include "chrome/browser/net/chrome_net_log.h" | 15 #include "chrome/browser/net/chrome_net_log.h" |
14 #include "chrome/browser/net/chrome_network_delegate.h" | 16 #include "chrome/browser/net/chrome_network_delegate.h" |
15 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" | 17 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" |
16 #include "chrome/common/chrome_constants.h" | 18 #include "chrome/common/chrome_constants.h" |
17 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/extensions/extension.h" |
| 21 #include "chrome/common/pref_names.h" |
18 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
19 #include "content/browser/browser_thread.h" | 23 #include "content/browser/browser_thread.h" |
20 #include "net/ftp/ftp_network_layer.h" | 24 #include "net/ftp/ftp_network_layer.h" |
21 #include "net/http/http_cache.h" | 25 #include "net/http/http_cache.h" |
22 | 26 |
23 ProfileImplIOData::Handle::Handle(Profile* profile) | 27 ProfileImplIOData::Handle::Handle(Profile* profile) |
24 : io_data_(new ProfileImplIOData), | 28 : io_data_(new ProfileImplIOData), |
25 profile_(profile), | 29 profile_(profile), |
26 initialized_(false) { | 30 initialized_(false) { |
27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
28 DCHECK(profile); | 32 DCHECK(profile); |
29 } | 33 } |
30 | 34 |
31 ProfileImplIOData::Handle::~Handle() { | 35 ProfileImplIOData::Handle::~Handle() { |
32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
33 if (main_request_context_getter_) | 37 if (main_request_context_getter_) |
34 main_request_context_getter_->CleanupOnUIThread(); | 38 main_request_context_getter_->CleanupOnUIThread(); |
35 if (media_request_context_getter_) | 39 if (media_request_context_getter_) |
36 media_request_context_getter_->CleanupOnUIThread(); | 40 media_request_context_getter_->CleanupOnUIThread(); |
37 if (extensions_request_context_getter_) | 41 if (extensions_request_context_getter_) |
38 extensions_request_context_getter_->CleanupOnUIThread(); | 42 extensions_request_context_getter_->CleanupOnUIThread(); |
| 43 |
| 44 // Clean up all isolated app request contexts. |
| 45 for (ChromeURLRequestContextGetterMap::iterator iter = |
| 46 app_request_context_getter_map_.begin(); |
| 47 iter != app_request_context_getter_map_.end(); |
| 48 ++iter) { |
| 49 iter->second->CleanupOnUIThread(); |
| 50 } |
39 } | 51 } |
40 | 52 |
41 void ProfileImplIOData::Handle::Init(const FilePath& cookie_path, | 53 void ProfileImplIOData::Handle::Init(const FilePath& cookie_path, |
42 const FilePath& cache_path, | 54 const FilePath& cache_path, |
43 int cache_max_size, | 55 int cache_max_size, |
44 const FilePath& media_cache_path, | 56 const FilePath& media_cache_path, |
45 int media_cache_max_size, | 57 int media_cache_max_size, |
46 const FilePath& extensions_cookie_path) { | 58 const FilePath& extensions_cookie_path, |
| 59 const FilePath& app_path) { |
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
48 DCHECK(!io_data_->lazy_params_.get()); | 61 DCHECK(!io_data_->lazy_params_.get()); |
49 LazyParams* lazy_params = new LazyParams; | 62 LazyParams* lazy_params = new LazyParams; |
50 | 63 |
51 lazy_params->cookie_path = cookie_path; | 64 lazy_params->cookie_path = cookie_path; |
52 lazy_params->cache_path = cache_path; | 65 lazy_params->cache_path = cache_path; |
53 lazy_params->cache_max_size = cache_max_size; | 66 lazy_params->cache_max_size = cache_max_size; |
54 lazy_params->media_cache_path = media_cache_path; | 67 lazy_params->media_cache_path = media_cache_path; |
55 lazy_params->media_cache_max_size = media_cache_max_size; | 68 lazy_params->media_cache_max_size = media_cache_max_size; |
56 lazy_params->extensions_cookie_path = extensions_cookie_path; | 69 lazy_params->extensions_cookie_path = extensions_cookie_path; |
57 | 70 |
58 lazy_params->io_thread = g_browser_process->io_thread(); | 71 lazy_params->io_thread = g_browser_process->io_thread(); |
59 | 72 |
60 io_data_->lazy_params_.reset(lazy_params); | 73 io_data_->lazy_params_.reset(lazy_params); |
| 74 |
| 75 // Keep track of isolated app path separately so we can use it on demand. |
| 76 io_data_->app_path_ = app_path; |
61 } | 77 } |
62 | 78 |
63 scoped_refptr<ChromeURLRequestContextGetter> | 79 scoped_refptr<ChromeURLRequestContextGetter> |
64 ProfileImplIOData::Handle::GetMainRequestContextGetter() const { | 80 ProfileImplIOData::Handle::GetMainRequestContextGetter() const { |
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
66 LazyInitialize(); | 82 LazyInitialize(); |
67 if (!main_request_context_getter_) { | 83 if (!main_request_context_getter_) { |
68 main_request_context_getter_ = | 84 main_request_context_getter_ = |
69 ChromeURLRequestContextGetter::CreateOriginal( | 85 ChromeURLRequestContextGetter::CreateOriginal( |
70 profile_, io_data_); | 86 profile_, io_data_); |
(...skipping 18 matching lines...) Expand all Loading... |
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
90 LazyInitialize(); | 106 LazyInitialize(); |
91 if (!extensions_request_context_getter_) { | 107 if (!extensions_request_context_getter_) { |
92 extensions_request_context_getter_ = | 108 extensions_request_context_getter_ = |
93 ChromeURLRequestContextGetter::CreateOriginalForExtensions( | 109 ChromeURLRequestContextGetter::CreateOriginalForExtensions( |
94 profile_, io_data_); | 110 profile_, io_data_); |
95 } | 111 } |
96 return extensions_request_context_getter_; | 112 return extensions_request_context_getter_; |
97 } | 113 } |
98 | 114 |
| 115 scoped_refptr<ChromeURLRequestContextGetter> |
| 116 ProfileImplIOData::Handle::GetIsolatedAppRequestContextGetter( |
| 117 const Extension* installed_app) const { |
| 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 119 CHECK(installed_app); |
| 120 LazyInitialize(); |
| 121 |
| 122 // Keep a map of request context getters, one per requested app ID. |
| 123 std::string id = installed_app->id(); |
| 124 ChromeURLRequestContextGetterMap::iterator iter = |
| 125 app_request_context_getter_map_.find(id); |
| 126 if (iter != app_request_context_getter_map_.end()) |
| 127 return iter->second; |
| 128 |
| 129 ChromeURLRequestContextGetter* context = |
| 130 ChromeURLRequestContextGetter::CreateOriginalForIsolatedApp( |
| 131 profile_, io_data_, installed_app); |
| 132 app_request_context_getter_map_[id] = context; |
| 133 |
| 134 return context; |
| 135 } |
| 136 |
99 void ProfileImplIOData::Handle::LazyInitialize() const { | 137 void ProfileImplIOData::Handle::LazyInitialize() const { |
100 if (!initialized_) { | 138 if (!initialized_) { |
101 InitializeProfileParams(profile_, &io_data_->lazy_params_->profile_params); | 139 InitializeProfileParams(profile_, &io_data_->lazy_params_->profile_params); |
| 140 // Keep track of clear_local_state_on_exit for isolated apps. |
| 141 io_data_->clear_local_state_on_exit_ = |
| 142 io_data_->lazy_params_->profile_params.clear_local_state_on_exit; |
102 initialized_ = true; | 143 initialized_ = true; |
103 } | 144 } |
104 } | 145 } |
105 | 146 |
106 ProfileImplIOData::LazyParams::LazyParams() | 147 ProfileImplIOData::LazyParams::LazyParams() |
107 : cache_max_size(0), | 148 : cache_max_size(0), |
108 media_cache_max_size(0), | 149 media_cache_max_size(0), |
109 io_thread(NULL) {} | 150 io_thread(NULL) {} |
110 ProfileImplIOData::LazyParams::~LazyParams() {} | 151 ProfileImplIOData::LazyParams::~LazyParams() {} |
111 | 152 |
112 ProfileImplIOData::ProfileImplIOData() : ProfileIOData(false) {} | 153 ProfileImplIOData::ProfileImplIOData() : ProfileIOData(false) {} |
113 ProfileImplIOData::~ProfileImplIOData() {} | 154 ProfileImplIOData::~ProfileImplIOData() { |
| 155 STLDeleteValues(&app_http_factory_map_); |
| 156 } |
114 | 157 |
115 void ProfileImplIOData::LazyInitializeInternal() const { | 158 void ProfileImplIOData::LazyInitializeInternal() const { |
116 main_request_context_ = new RequestContext; | 159 main_request_context_ = new RequestContext; |
117 media_request_context_ = new RequestContext; | 160 media_request_context_ = new RequestContext; |
118 extensions_request_context_ = new RequestContext; | 161 extensions_request_context_ = new RequestContext; |
119 | 162 |
120 IOThread* const io_thread = lazy_params_->io_thread; | 163 IOThread* const io_thread = lazy_params_->io_thread; |
121 IOThread::Globals* const io_thread_globals = io_thread->globals(); | 164 IOThread::Globals* const io_thread_globals = io_thread->globals(); |
122 const ProfileParams& profile_params = lazy_params_->profile_params; | 165 const ProfileParams& profile_params = lazy_params_->profile_params; |
123 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 166 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 media_http_factory_.reset(media_cache); | 290 media_http_factory_.reset(media_cache); |
248 main_request_context_->set_http_transaction_factory(main_cache); | 291 main_request_context_->set_http_transaction_factory(main_cache); |
249 media_request_context_->set_http_transaction_factory(media_cache); | 292 media_request_context_->set_http_transaction_factory(media_cache); |
250 | 293 |
251 main_request_context_->set_ftp_transaction_factory( | 294 main_request_context_->set_ftp_transaction_factory( |
252 new net::FtpNetworkLayer(io_thread_globals->host_resolver.get())); | 295 new net::FtpNetworkLayer(io_thread_globals->host_resolver.get())); |
253 | 296 |
254 lazy_params_.reset(); | 297 lazy_params_.reset(); |
255 } | 298 } |
256 | 299 |
| 300 scoped_refptr<ProfileIOData::RequestContext> |
| 301 ProfileImplIOData::InitializeAppRequestContext( |
| 302 scoped_refptr<ChromeURLRequestContext> main_context, |
| 303 const Extension* installed_app) const { |
| 304 scoped_refptr<ProfileIOData::RequestContext> context = new RequestContext; |
| 305 |
| 306 // Copy most state from the main context. |
| 307 context->CopyFrom(main_context); |
| 308 |
| 309 FilePath app_path = app_path_.AppendASCII(installed_app->id()); |
| 310 FilePath cookie_path = app_path.Append(chrome::kCookieFilename); |
| 311 FilePath cache_path = app_path.Append(chrome::kCacheDirname); |
| 312 // TODO(creis): Determine correct cache size. |
| 313 int cache_max_size = 0; |
| 314 |
| 315 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 316 bool record_mode = chrome::kRecordModeEnabled && |
| 317 command_line.HasSwitch(switches::kRecordMode); |
| 318 bool playback_mode = command_line.HasSwitch(switches::kPlaybackMode); |
| 319 |
| 320 // Use a separate HTTP disk cache for isolated apps. |
| 321 net::HttpCache::DefaultBackend* app_backend = |
| 322 new net::HttpCache::DefaultBackend( |
| 323 net::DISK_CACHE, |
| 324 cache_path, |
| 325 cache_max_size, |
| 326 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)); |
| 327 net::HttpNetworkSession* main_network_session = |
| 328 main_http_factory_->GetSession(); |
| 329 net::HttpCache* app_http_cache = |
| 330 new net::HttpCache(main_network_session, app_backend); |
| 331 |
| 332 scoped_refptr<net::CookieStore> cookie_store = NULL; |
| 333 if (record_mode || playback_mode) { |
| 334 // Don't use existing cookies and use an in-memory store. |
| 335 // TODO(creis): We should have a cookie delegate for notifying the cookie |
| 336 // extensions API, but we need to update it to understand isolated apps |
| 337 // first. |
| 338 cookie_store = new net::CookieMonster(NULL, NULL); |
| 339 app_http_cache->set_mode( |
| 340 record_mode ? net::HttpCache::RECORD : net::HttpCache::PLAYBACK); |
| 341 } |
| 342 |
| 343 // Use an app-specific cookie store. |
| 344 if (!cookie_store) { |
| 345 DCHECK(!cookie_path.empty()); |
| 346 |
| 347 scoped_refptr<SQLitePersistentCookieStore> cookie_db = |
| 348 new SQLitePersistentCookieStore(cookie_path); |
| 349 cookie_db->SetClearLocalStateOnExit(clear_local_state_on_exit_); |
| 350 // TODO(creis): We should have a cookie delegate for notifying the cookie |
| 351 // extensions API, but we need to update it to understand isolated apps |
| 352 // first. |
| 353 cookie_store = new net::CookieMonster(cookie_db.get(), NULL); |
| 354 } |
| 355 |
| 356 context->set_cookie_store(cookie_store); |
| 357 |
| 358 // Keep track of app_http_cache to delete it when we go away. |
| 359 app_http_factory_map_[installed_app->id()] = app_http_cache; |
| 360 context->set_http_transaction_factory(app_http_cache); |
| 361 |
| 362 return context; |
| 363 } |
| 364 |
257 scoped_refptr<ChromeURLRequestContext> | 365 scoped_refptr<ChromeURLRequestContext> |
258 ProfileImplIOData::AcquireMainRequestContext() const { | 366 ProfileImplIOData::AcquireMainRequestContext() const { |
259 DCHECK(main_request_context_); | 367 DCHECK(main_request_context_); |
260 scoped_refptr<ChromeURLRequestContext> context = main_request_context_; | 368 scoped_refptr<ChromeURLRequestContext> context = main_request_context_; |
261 main_request_context_->set_profile_io_data(this); | 369 main_request_context_->set_profile_io_data(this); |
262 main_request_context_ = NULL; | 370 main_request_context_ = NULL; |
263 return context; | 371 return context; |
264 } | 372 } |
265 | 373 |
266 scoped_refptr<ChromeURLRequestContext> | 374 scoped_refptr<ChromeURLRequestContext> |
267 ProfileImplIOData::AcquireMediaRequestContext() const { | 375 ProfileImplIOData::AcquireMediaRequestContext() const { |
268 DCHECK(media_request_context_); | 376 DCHECK(media_request_context_); |
269 scoped_refptr<ChromeURLRequestContext> context = media_request_context_; | 377 scoped_refptr<ChromeURLRequestContext> context = media_request_context_; |
270 media_request_context_->set_profile_io_data(this); | 378 media_request_context_->set_profile_io_data(this); |
271 media_request_context_ = NULL; | 379 media_request_context_ = NULL; |
272 return context; | 380 return context; |
273 } | 381 } |
274 | 382 |
275 scoped_refptr<ChromeURLRequestContext> | 383 scoped_refptr<ChromeURLRequestContext> |
276 ProfileImplIOData::AcquireExtensionsRequestContext() const { | 384 ProfileImplIOData::AcquireExtensionsRequestContext() const { |
277 DCHECK(extensions_request_context_); | 385 DCHECK(extensions_request_context_); |
278 scoped_refptr<ChromeURLRequestContext> context = extensions_request_context_; | 386 scoped_refptr<ChromeURLRequestContext> context = extensions_request_context_; |
279 extensions_request_context_->set_profile_io_data(this); | 387 extensions_request_context_->set_profile_io_data(this); |
280 extensions_request_context_ = NULL; | 388 extensions_request_context_ = NULL; |
281 return context; | 389 return context; |
282 } | 390 } |
| 391 |
| 392 scoped_refptr<ChromeURLRequestContext> |
| 393 ProfileImplIOData::AcquireIsolatedAppRequestContext( |
| 394 scoped_refptr<ChromeURLRequestContext> main_context, |
| 395 const Extension* installed_app) const { |
| 396 // We create per-app contexts on demand, unlike the others above. |
| 397 scoped_refptr<RequestContext> app_request_context = |
| 398 InitializeAppRequestContext(main_context, installed_app); |
| 399 DCHECK(app_request_context); |
| 400 app_request_context->set_profile_io_data(this); |
| 401 return app_request_context; |
| 402 } |
OLD | NEW |