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/off_the_record_profile_io_data.h" | 5 #include "chrome/browser/profiles/off_the_record_profile_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 "build/build_config.h" | 10 #include "build/build_config.h" |
10 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/io_thread.h" | 12 #include "chrome/browser/io_thread.h" |
12 #include "chrome/browser/net/chrome_cookie_policy.h" | 13 #include "chrome/browser/net/chrome_cookie_policy.h" |
13 #include "chrome/browser/net/chrome_dns_cert_provenance_checker_factory.h" | 14 #include "chrome/browser/net/chrome_dns_cert_provenance_checker_factory.h" |
14 #include "chrome/browser/net/chrome_net_log.h" | 15 #include "chrome/browser/net/chrome_net_log.h" |
15 #include "chrome/browser/net/chrome_network_delegate.h" | 16 #include "chrome/browser/net/chrome_network_delegate.h" |
16 #include "chrome/browser/net/chrome_url_request_context.h" | 17 #include "chrome/browser/net/chrome_url_request_context.h" |
| 18 #include "chrome/common/extensions/extension.h" |
17 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
18 #include "content/browser/browser_thread.h" | 20 #include "content/browser/browser_thread.h" |
19 #include "net/ftp/ftp_network_layer.h" | 21 #include "net/ftp/ftp_network_layer.h" |
20 #include "net/http/http_cache.h" | 22 #include "net/http/http_cache.h" |
21 | 23 |
22 OffTheRecordProfileIOData::Handle::Handle(Profile* profile) | 24 OffTheRecordProfileIOData::Handle::Handle(Profile* profile) |
23 : io_data_(new OffTheRecordProfileIOData), | 25 : io_data_(new OffTheRecordProfileIOData), |
24 profile_(profile), | 26 profile_(profile), |
25 initialized_(false) { | 27 initialized_(false) { |
26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
27 DCHECK(profile); | 29 DCHECK(profile); |
28 DCHECK(!io_data_->lazy_params_.get()); | 30 DCHECK(!io_data_->lazy_params_.get()); |
29 LazyParams* lazy_params = new LazyParams; | 31 LazyParams* lazy_params = new LazyParams; |
30 lazy_params->io_thread = g_browser_process->io_thread(); | 32 lazy_params->io_thread = g_browser_process->io_thread(); |
31 io_data_->lazy_params_.reset(lazy_params); | 33 io_data_->lazy_params_.reset(lazy_params); |
32 } | 34 } |
33 | 35 |
34 OffTheRecordProfileIOData::Handle::~Handle() { | 36 OffTheRecordProfileIOData::Handle::~Handle() { |
35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
36 if (main_request_context_getter_) | 38 if (main_request_context_getter_) |
37 main_request_context_getter_->CleanupOnUIThread(); | 39 main_request_context_getter_->CleanupOnUIThread(); |
38 if (extensions_request_context_getter_) | 40 if (extensions_request_context_getter_) |
39 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 } |
40 } | 50 } |
41 | 51 |
42 scoped_refptr<ChromeURLRequestContextGetter> | 52 scoped_refptr<ChromeURLRequestContextGetter> |
43 OffTheRecordProfileIOData::Handle::GetMainRequestContextGetter() const { | 53 OffTheRecordProfileIOData::Handle::GetMainRequestContextGetter() const { |
44 // TODO(oshima): Re-enable when ChromeOS only accesses the profile on the UI | 54 // TODO(oshima): Re-enable when ChromeOS only accesses the profile on the UI |
45 // thread. | 55 // thread. |
46 #if !defined(OS_CHROMEOS) | 56 #if !defined(OS_CHROMEOS) |
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
48 #endif // defined(OS_CHROMEOS) | 58 #endif // defined(OS_CHROMEOS) |
49 LazyInitialize(); | 59 LazyInitialize(); |
50 if (!main_request_context_getter_) { | 60 if (!main_request_context_getter_) { |
51 main_request_context_getter_ = | 61 main_request_context_getter_ = |
52 ChromeURLRequestContextGetter::CreateOffTheRecord(profile_, io_data_); | 62 ChromeURLRequestContextGetter::CreateOffTheRecord(profile_, io_data_); |
53 } | 63 } |
54 return main_request_context_getter_; | 64 return main_request_context_getter_; |
55 } | 65 } |
56 | 66 |
57 scoped_refptr<ChromeURLRequestContextGetter> | 67 scoped_refptr<ChromeURLRequestContextGetter> |
58 OffTheRecordProfileIOData::Handle::GetExtensionsRequestContextGetter() const { | 68 OffTheRecordProfileIOData::Handle::GetExtensionsRequestContextGetter() const { |
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
60 LazyInitialize(); | 70 LazyInitialize(); |
61 if (!extensions_request_context_getter_) { | 71 if (!extensions_request_context_getter_) { |
62 extensions_request_context_getter_ = | 72 extensions_request_context_getter_ = |
63 ChromeURLRequestContextGetter::CreateOffTheRecordForExtensions( | 73 ChromeURLRequestContextGetter::CreateOffTheRecordForExtensions( |
64 profile_, io_data_); | 74 profile_, io_data_); |
65 } | 75 } |
66 return extensions_request_context_getter_; | 76 return extensions_request_context_getter_; |
67 } | 77 } |
68 | 78 |
| 79 scoped_refptr<ChromeURLRequestContextGetter> |
| 80 OffTheRecordProfileIOData::Handle::GetIsolatedAppRequestContextGetter( |
| 81 const std::string& app_id) const { |
| 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 83 DCHECK(!app_id.empty()); |
| 84 LazyInitialize(); |
| 85 |
| 86 // Keep a map of request context getters, one per requested app ID. |
| 87 ChromeURLRequestContextGetterMap::iterator iter = |
| 88 app_request_context_getter_map_.find(app_id); |
| 89 if (iter != app_request_context_getter_map_.end()) |
| 90 return iter->second; |
| 91 |
| 92 ChromeURLRequestContextGetter* context = |
| 93 ChromeURLRequestContextGetter::CreateOffTheRecordForIsolatedApp( |
| 94 profile_, io_data_, app_id); |
| 95 app_request_context_getter_map_[app_id] = context; |
| 96 |
| 97 return context; |
| 98 } |
| 99 |
69 void OffTheRecordProfileIOData::Handle::LazyInitialize() const { | 100 void OffTheRecordProfileIOData::Handle::LazyInitialize() const { |
70 if (!initialized_) { | 101 if (!initialized_) { |
71 InitializeProfileParams(profile_, &io_data_->lazy_params_->profile_params); | 102 InitializeProfileParams(profile_, &io_data_->lazy_params_->profile_params); |
72 initialized_ = true; | 103 initialized_ = true; |
73 } | 104 } |
74 } | 105 } |
75 | 106 |
76 OffTheRecordProfileIOData::LazyParams::LazyParams() : io_thread(NULL) {} | 107 OffTheRecordProfileIOData::LazyParams::LazyParams() : io_thread(NULL) {} |
77 OffTheRecordProfileIOData::LazyParams::~LazyParams() {} | 108 OffTheRecordProfileIOData::LazyParams::~LazyParams() {} |
78 | 109 |
79 OffTheRecordProfileIOData::OffTheRecordProfileIOData() | 110 OffTheRecordProfileIOData::OffTheRecordProfileIOData() |
80 : ProfileIOData(true), | 111 : ProfileIOData(true), |
81 initialized_(false) {} | 112 initialized_(false) {} |
82 OffTheRecordProfileIOData::~OffTheRecordProfileIOData() {} | 113 OffTheRecordProfileIOData::~OffTheRecordProfileIOData() { |
| 114 STLDeleteValues(&app_http_factory_map_); |
| 115 } |
83 | 116 |
84 void OffTheRecordProfileIOData::LazyInitializeInternal() const { | 117 void OffTheRecordProfileIOData::LazyInitializeInternal() const { |
85 main_request_context_ = new RequestContext; | 118 main_request_context_ = new RequestContext; |
86 extensions_request_context_ = new RequestContext; | 119 extensions_request_context_ = new RequestContext; |
87 | 120 |
88 IOThread* const io_thread = lazy_params_->io_thread; | 121 IOThread* const io_thread = lazy_params_->io_thread; |
89 IOThread::Globals* const io_thread_globals = io_thread->globals(); | 122 IOThread::Globals* const io_thread_globals = io_thread->globals(); |
90 const ProfileParams& profile_params = lazy_params_->profile_params; | 123 const ProfileParams& profile_params = lazy_params_->profile_params; |
91 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 124 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
92 | 125 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 main_request_context_->network_delegate(), | 190 main_request_context_->network_delegate(), |
158 main_request_context_->net_log(), | 191 main_request_context_->net_log(), |
159 main_backend); | 192 main_backend); |
160 | 193 |
161 main_http_factory_.reset(cache); | 194 main_http_factory_.reset(cache); |
162 main_request_context_->set_http_transaction_factory(cache); | 195 main_request_context_->set_http_transaction_factory(cache); |
163 main_request_context_->set_ftp_transaction_factory( | 196 main_request_context_->set_ftp_transaction_factory( |
164 new net::FtpNetworkLayer(main_request_context_->host_resolver())); | 197 new net::FtpNetworkLayer(main_request_context_->host_resolver())); |
165 } | 198 } |
166 | 199 |
| 200 scoped_refptr<ProfileIOData::RequestContext> |
| 201 OffTheRecordProfileIOData::InitializeAppRequestContext( |
| 202 scoped_refptr<ChromeURLRequestContext> main_context, |
| 203 const std::string& app_id) const { |
| 204 scoped_refptr<ProfileIOData::RequestContext> context = new RequestContext; |
| 205 |
| 206 // Copy most state from the main context. |
| 207 context->CopyFrom(main_context); |
| 208 |
| 209 // Use a separate in-memory cookie store for the app. |
| 210 // TODO(creis): We should have a cookie delegate for notifying the cookie |
| 211 // extensions API, but we need to update it to understand isolated apps first. |
| 212 context->set_cookie_store( |
| 213 new net::CookieMonster(NULL, NULL)); |
| 214 |
| 215 // Use a separate in-memory cache for the app. |
| 216 net::HttpCache::BackendFactory* app_backend = |
| 217 net::HttpCache::DefaultBackend::InMemory(0); |
| 218 net::HttpNetworkSession* main_network_session = |
| 219 main_http_factory_->GetSession(); |
| 220 net::HttpCache* app_http_cache = |
| 221 new net::HttpCache(main_network_session, app_backend); |
| 222 |
| 223 // Keep track of app_http_cache to delete it when we go away. |
| 224 DCHECK(!app_http_factory_map_[app_id]); |
| 225 app_http_factory_map_[app_id] = app_http_cache; |
| 226 context->set_http_transaction_factory(app_http_cache); |
| 227 |
| 228 return context; |
| 229 } |
| 230 |
167 scoped_refptr<ChromeURLRequestContext> | 231 scoped_refptr<ChromeURLRequestContext> |
168 OffTheRecordProfileIOData::AcquireMainRequestContext() const { | 232 OffTheRecordProfileIOData::AcquireMainRequestContext() const { |
169 DCHECK(main_request_context_); | 233 DCHECK(main_request_context_); |
170 scoped_refptr<ChromeURLRequestContext> context = main_request_context_; | 234 scoped_refptr<ChromeURLRequestContext> context = main_request_context_; |
171 main_request_context_->set_profile_io_data(this); | 235 main_request_context_->set_profile_io_data(this); |
172 main_request_context_ = NULL; | 236 main_request_context_ = NULL; |
173 return context; | 237 return context; |
174 } | 238 } |
175 | 239 |
176 scoped_refptr<ChromeURLRequestContext> | 240 scoped_refptr<ChromeURLRequestContext> |
177 OffTheRecordProfileIOData::AcquireMediaRequestContext() const { | 241 OffTheRecordProfileIOData::AcquireMediaRequestContext() const { |
178 NOTREACHED(); | 242 NOTREACHED(); |
179 return NULL; | 243 return NULL; |
180 } | 244 } |
181 | 245 |
182 scoped_refptr<ChromeURLRequestContext> | 246 scoped_refptr<ChromeURLRequestContext> |
183 OffTheRecordProfileIOData::AcquireExtensionsRequestContext() const { | 247 OffTheRecordProfileIOData::AcquireExtensionsRequestContext() const { |
184 DCHECK(extensions_request_context_); | 248 DCHECK(extensions_request_context_); |
185 scoped_refptr<ChromeURLRequestContext> context = extensions_request_context_; | 249 scoped_refptr<ChromeURLRequestContext> context = extensions_request_context_; |
186 extensions_request_context_->set_profile_io_data(this); | 250 extensions_request_context_->set_profile_io_data(this); |
187 extensions_request_context_ = NULL; | 251 extensions_request_context_ = NULL; |
188 return context; | 252 return context; |
189 } | 253 } |
| 254 |
| 255 scoped_refptr<ChromeURLRequestContext> |
| 256 OffTheRecordProfileIOData::AcquireIsolatedAppRequestContext( |
| 257 scoped_refptr<ChromeURLRequestContext> main_context, |
| 258 const std::string& app_id) const { |
| 259 // We create per-app contexts on demand, unlike the others above. |
| 260 scoped_refptr<RequestContext> app_request_context = |
| 261 InitializeAppRequestContext(main_context, app_id); |
| 262 DCHECK(app_request_context); |
| 263 app_request_context->set_profile_io_data(this); |
| 264 return app_request_context; |
| 265 } |
OLD | NEW |