OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/url_request/view_cache_helper.h" | 5 #include "net/url_request/view_cache_helper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "net/base/escape.h" | 10 #include "net/base/escape.h" |
11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 #include "net/disk_cache/disk_cache.h" | 13 #include "net/disk_cache/disk_cache.h" |
14 #include "net/http/http_cache.h" | 14 #include "net/http/http_cache.h" |
15 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
16 #include "net/http/http_response_info.h" | 16 #include "net/http/http_response_info.h" |
17 #include "net/url_request/url_request_context.h" | 17 #include "net/url_request/url_request_context.h" |
18 | 18 |
19 #define VIEW_CACHE_HEAD \ | 19 #define VIEW_CACHE_HEAD \ |
20 "<html><meta charset=\"utf-8\">" \ | 20 "<html><meta charset=\"utf-8\">" \ |
21 "<meta http-equiv=\"Content-Security-Policy\" " \ | 21 "<meta http-equiv=\"Content-Security-Policy\" " \ |
22 " content=\"object-src 'none'; script-src 'none' 'unsafe-eval'\">" \ | 22 " content=\"object-src 'none'; script-src 'none' 'unsafe-eval'\">" \ |
23 "<body><table>" | 23 "<body><table>" |
24 | 24 |
25 #define VIEW_CACHE_TAIL \ | 25 #define VIEW_CACHE_TAIL "</table></body></html>" |
26 "</table></body></html>" | |
27 | 26 |
28 namespace net { | 27 namespace net { |
29 | 28 |
30 namespace { | 29 namespace { |
31 | 30 |
32 std::string FormatEntryInfo(disk_cache::Entry* entry, | 31 std::string FormatEntryInfo(disk_cache::Entry* entry, |
33 const std::string& url_prefix) { | 32 const std::string& url_prefix) { |
34 std::string key = entry->GetKey(); | 33 std::string key = entry->GetKey(); |
35 GURL url = GURL(url_prefix + key); | 34 GURL url = GURL(url_prefix + key); |
36 std::string row = | 35 std::string row = "<tr><td><a href=\"" + url.spec() + "\">" + |
37 "<tr><td><a href=\"" + url.spec() + "\">" + EscapeForHTML(key) + | 36 EscapeForHTML(key) + "</a></td></tr>"; |
38 "</a></td></tr>"; | |
39 return row; | 37 return row; |
40 } | 38 } |
41 | 39 |
42 } // namespace. | 40 } // namespace. |
43 | 41 |
44 ViewCacheHelper::ViewCacheHelper() | 42 ViewCacheHelper::ViewCacheHelper() |
45 : context_(NULL), | 43 : context_(NULL), |
46 disk_cache_(NULL), | 44 disk_cache_(NULL), |
47 entry_(NULL), | 45 entry_(NULL), |
48 iter_(NULL), | 46 iter_(NULL), |
(...skipping 17 matching lines...) Expand all Loading... |
66 } | 64 } |
67 | 65 |
68 int ViewCacheHelper::GetContentsHTML(const URLRequestContext* context, | 66 int ViewCacheHelper::GetContentsHTML(const URLRequestContext* context, |
69 const std::string& url_prefix, | 67 const std::string& url_prefix, |
70 std::string* out, | 68 std::string* out, |
71 const CompletionCallback& callback) { | 69 const CompletionCallback& callback) { |
72 return GetInfoHTML(std::string(), context, url_prefix, out, callback); | 70 return GetInfoHTML(std::string(), context, url_prefix, out, callback); |
73 } | 71 } |
74 | 72 |
75 // static | 73 // static |
76 void ViewCacheHelper::HexDump(const char *buf, size_t buf_len, | 74 void ViewCacheHelper::HexDump(const char* buf, |
| 75 size_t buf_len, |
77 std::string* result) { | 76 std::string* result) { |
78 const size_t kMaxRows = 16; | 77 const size_t kMaxRows = 16; |
79 int offset = 0; | 78 int offset = 0; |
80 | 79 |
81 const unsigned char *p; | 80 const unsigned char* p; |
82 while (buf_len) { | 81 while (buf_len) { |
83 base::StringAppendF(result, "%08x: ", offset); | 82 base::StringAppendF(result, "%08x: ", offset); |
84 offset += kMaxRows; | 83 offset += kMaxRows; |
85 | 84 |
86 p = (const unsigned char *) buf; | 85 p = (const unsigned char*)buf; |
87 | 86 |
88 size_t i; | 87 size_t i; |
89 size_t row_max = std::min(kMaxRows, buf_len); | 88 size_t row_max = std::min(kMaxRows, buf_len); |
90 | 89 |
91 // print hex codes: | 90 // print hex codes: |
92 for (i = 0; i < row_max; ++i) | 91 for (i = 0; i < row_max; ++i) |
93 base::StringAppendF(result, "%02x ", *p++); | 92 base::StringAppendF(result, "%02x ", *p++); |
94 for (i = row_max; i < kMaxRows; ++i) | 93 for (i = row_max; i < kMaxRows; ++i) |
95 result->append(" "); | 94 result->append(" "); |
96 result->append(" "); | 95 result->append(" "); |
97 | 96 |
98 // print ASCII glyphs if possible: | 97 // print ASCII glyphs if possible: |
99 p = (const unsigned char *) buf; | 98 p = (const unsigned char*)buf; |
100 for (i = 0; i < row_max; ++i, ++p) { | 99 for (i = 0; i < row_max; ++i, ++p) { |
101 if (*p < 0x7F && *p > 0x1F) { | 100 if (*p < 0x7F && *p > 0x1F) { |
102 AppendEscapedCharForHTML(*p, result); | 101 AppendEscapedCharForHTML(*p, result); |
103 } else { | 102 } else { |
104 result->push_back('.'); | 103 result->push_back('.'); |
105 } | 104 } |
106 } | 105 } |
107 | 106 |
108 result->push_back('\n'); | 107 result->push_back('\n'); |
109 | 108 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 next_state_ = STATE_GET_BACKEND_COMPLETE; | 210 next_state_ = STATE_GET_BACKEND_COMPLETE; |
212 | 211 |
213 if (!context_->http_transaction_factory()) | 212 if (!context_->http_transaction_factory()) |
214 return ERR_FAILED; | 213 return ERR_FAILED; |
215 | 214 |
216 HttpCache* http_cache = context_->http_transaction_factory()->GetCache(); | 215 HttpCache* http_cache = context_->http_transaction_factory()->GetCache(); |
217 if (!http_cache) | 216 if (!http_cache) |
218 return ERR_FAILED; | 217 return ERR_FAILED; |
219 | 218 |
220 return http_cache->GetBackend( | 219 return http_cache->GetBackend( |
221 &disk_cache_, base::Bind(&ViewCacheHelper::OnIOComplete, | 220 &disk_cache_, |
222 base::Unretained(this))); | 221 base::Bind(&ViewCacheHelper::OnIOComplete, base::Unretained(this))); |
223 } | 222 } |
224 | 223 |
225 int ViewCacheHelper::DoGetBackendComplete(int result) { | 224 int ViewCacheHelper::DoGetBackendComplete(int result) { |
226 if (result == ERR_FAILED) { | 225 if (result == ERR_FAILED) { |
227 data_->append("no disk cache"); | 226 data_->append("no disk cache"); |
228 return OK; | 227 return OK; |
229 } | 228 } |
230 | 229 |
231 DCHECK_EQ(OK, result); | 230 DCHECK_EQ(OK, result); |
232 if (key_.empty()) { | 231 if (key_.empty()) { |
233 data_->assign(VIEW_CACHE_HEAD); | 232 data_->assign(VIEW_CACHE_HEAD); |
234 DCHECK(!iter_); | 233 DCHECK(!iter_); |
235 next_state_ = STATE_OPEN_NEXT_ENTRY; | 234 next_state_ = STATE_OPEN_NEXT_ENTRY; |
236 return OK; | 235 return OK; |
237 } | 236 } |
238 | 237 |
239 next_state_ = STATE_OPEN_ENTRY; | 238 next_state_ = STATE_OPEN_ENTRY; |
240 return OK; | 239 return OK; |
241 } | 240 } |
242 | 241 |
243 int ViewCacheHelper::DoOpenNextEntry() { | 242 int ViewCacheHelper::DoOpenNextEntry() { |
244 next_state_ = STATE_OPEN_NEXT_ENTRY_COMPLETE; | 243 next_state_ = STATE_OPEN_NEXT_ENTRY_COMPLETE; |
245 return disk_cache_->OpenNextEntry( | 244 return disk_cache_->OpenNextEntry( |
246 &iter_, &entry_, | 245 &iter_, |
| 246 &entry_, |
247 base::Bind(&ViewCacheHelper::OnIOComplete, base::Unretained(this))); | 247 base::Bind(&ViewCacheHelper::OnIOComplete, base::Unretained(this))); |
248 } | 248 } |
249 | 249 |
250 int ViewCacheHelper::DoOpenNextEntryComplete(int result) { | 250 int ViewCacheHelper::DoOpenNextEntryComplete(int result) { |
251 if (result == ERR_FAILED) { | 251 if (result == ERR_FAILED) { |
252 data_->append(VIEW_CACHE_TAIL); | 252 data_->append(VIEW_CACHE_TAIL); |
253 return OK; | 253 return OK; |
254 } | 254 } |
255 | 255 |
256 DCHECK_EQ(OK, result); | 256 DCHECK_EQ(OK, result); |
257 data_->append(FormatEntryInfo(entry_, url_prefix_)); | 257 data_->append(FormatEntryInfo(entry_, url_prefix_)); |
258 entry_->Close(); | 258 entry_->Close(); |
259 entry_ = NULL; | 259 entry_ = NULL; |
260 | 260 |
261 next_state_ = STATE_OPEN_NEXT_ENTRY; | 261 next_state_ = STATE_OPEN_NEXT_ENTRY; |
262 return OK; | 262 return OK; |
263 } | 263 } |
264 | 264 |
265 int ViewCacheHelper::DoOpenEntry() { | 265 int ViewCacheHelper::DoOpenEntry() { |
266 next_state_ = STATE_OPEN_ENTRY_COMPLETE; | 266 next_state_ = STATE_OPEN_ENTRY_COMPLETE; |
267 return disk_cache_->OpenEntry( | 267 return disk_cache_->OpenEntry( |
268 key_, &entry_, | 268 key_, |
| 269 &entry_, |
269 base::Bind(&ViewCacheHelper::OnIOComplete, base::Unretained(this))); | 270 base::Bind(&ViewCacheHelper::OnIOComplete, base::Unretained(this))); |
270 } | 271 } |
271 | 272 |
272 int ViewCacheHelper::DoOpenEntryComplete(int result) { | 273 int ViewCacheHelper::DoOpenEntryComplete(int result) { |
273 if (result == ERR_FAILED) { | 274 if (result == ERR_FAILED) { |
274 data_->append("no matching cache entry for: " + EscapeForHTML(key_)); | 275 data_->append("no matching cache entry for: " + EscapeForHTML(key_)); |
275 return OK; | 276 return OK; |
276 } | 277 } |
277 | 278 |
278 data_->assign(VIEW_CACHE_HEAD); | 279 data_->assign(VIEW_CACHE_HEAD); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 entry_ = NULL; | 359 entry_ = NULL; |
359 } | 360 } |
360 return OK; | 361 return OK; |
361 } | 362 } |
362 | 363 |
363 void ViewCacheHelper::OnIOComplete(int result) { | 364 void ViewCacheHelper::OnIOComplete(int result) { |
364 DoLoop(result); | 365 DoLoop(result); |
365 } | 366 } |
366 | 367 |
367 } // namespace net. | 368 } // namespace net. |
OLD | NEW |