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