| 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 "webkit/browser/blob/view_blob_internals_job.h" | 5 #include "webkit/browser/blob/view_blob_internals_job.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/i18n/number_formatting.h" | 10 #include "base/i18n/number_formatting.h" |
| 11 #include "base/i18n/time_formatting.h" | 11 #include "base/i18n/time_formatting.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 17 #include "net/base/escape.h" | 18 #include "net/base/escape.h" |
| 18 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 19 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
| 20 #include "webkit/browser/blob/blob_storage_controller.h" | 21 #include "webkit/browser/blob/blob_storage_context.h" |
| 21 #include "webkit/common/blob/blob_data.h" | 22 #include "webkit/common/blob/blob_data.h" |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 const char kEmptyBlobStorageMessage[] = "No available blob data."; | 26 const char kEmptyBlobStorageMessage[] = "No available blob data."; |
| 26 const char kRemove[] = "Remove"; | 27 const char kRemove[] = "Remove"; |
| 27 const char kContentType[] = "Content Type: "; | 28 const char kContentType[] = "Content Type: "; |
| 28 const char kContentDisposition[] = "Content Disposition: "; | 29 const char kContentDisposition[] = "Content Disposition: "; |
| 29 const char kCount[] = "Count: "; | 30 const char kCount[] = "Count: "; |
| 30 const char kIndex[] = "Index: "; | 31 const char kIndex[] = "Index: "; |
| 31 const char kType[] = "Type: "; | 32 const char kType[] = "Type: "; |
| 32 const char kPath[] = "Path: "; | 33 const char kPath[] = "Path: "; |
| 33 const char kURL[] = "URL: "; | 34 const char kURL[] = "URL: "; |
| 34 const char kModificationTime[] = "Modification Time: "; | 35 const char kModificationTime[] = "Modification Time: "; |
| 35 const char kOffset[] = "Offset: "; | 36 const char kOffset[] = "Offset: "; |
| 36 const char kLength[] = "Length: "; | 37 const char kLength[] = "Length: "; |
| 38 const char kUUID[] = "Uuid: "; |
| 39 const char kRefcount[] = "Refcount: "; |
| 37 | 40 |
| 38 void StartHTML(std::string* out) { | 41 void StartHTML(std::string* out) { |
| 39 out->append( | 42 out->append( |
| 40 "<!DOCTYPE HTML>" | 43 "<!DOCTYPE HTML>" |
| 41 "<html><title>Blob Storage Internals</title>" | 44 "<html><title>Blob Storage Internals</title>" |
| 42 "<meta http-equiv=\"Content-Security-Policy\"" | 45 "<meta http-equiv=\"Content-Security-Policy\"" |
| 43 " content=\"object-src 'none'; script-src 'none'\">\n" | 46 " content=\"object-src 'none'; script-src 'none'\">\n" |
| 44 "<style>\n" | 47 "<style>\n" |
| 45 "body { font-family: sans-serif; font-size: 0.8em; }\n" | 48 "body { font-family: sans-serif; font-size: 0.8em; }\n" |
| 46 "tt, code, pre { font-family: WebKitHack, monospace; }\n" | 49 "tt, code, pre { font-family: WebKitHack, monospace; }\n" |
| 47 "form { display: inline }\n" | 50 "form { display: inline }\n" |
| 48 ".subsection_body { margin: 10px 0 10px 2em; }\n" | 51 ".subsection_body { margin: 10px 0 10px 2em; }\n" |
| 49 ".subsection_title { font-weight: bold; }\n" | 52 ".subsection_title { font-weight: bold; }\n" |
| 50 "</style>\n" | 53 "</style>\n" |
| 51 "</head><body>\n"); | 54 "</head><body>\n\n"); |
| 52 } | 55 } |
| 53 | 56 |
| 54 void EndHTML(std::string* out) { | 57 void EndHTML(std::string* out) { |
| 55 out->append("</body></html>"); | 58 out->append("\n</body></html>"); |
| 56 } | 59 } |
| 57 | 60 |
| 58 void AddHTMLBoldText(const std::string& text, std::string* out) { | 61 void AddHTMLBoldText(const std::string& text, std::string* out) { |
| 59 out->append("<b>"); | 62 out->append("<b>"); |
| 60 out->append(net::EscapeForHTML(text)); | 63 out->append(net::EscapeForHTML(text)); |
| 61 out->append("</b>"); | 64 out->append("</b>"); |
| 62 } | 65 } |
| 63 | 66 |
| 64 void StartHTMLList(std::string* out) { | 67 void StartHTMLList(std::string* out) { |
| 65 out->append("<ul>"); | 68 out->append("\n<ul>"); |
| 66 } | 69 } |
| 67 | 70 |
| 68 void EndHTMLList(std::string* out) { | 71 void EndHTMLList(std::string* out) { |
| 69 out->append("</ul>"); | 72 out->append("</ul>\n"); |
| 70 } | 73 } |
| 71 | 74 |
| 72 void AddHTMLListItem(const std::string& element_title, | 75 void AddHTMLListItem(const std::string& element_title, |
| 73 const std::string& element_data, | 76 const std::string& element_data, |
| 74 std::string* out) { | 77 std::string* out) { |
| 75 out->append("<li>"); | 78 out->append("<li>"); |
| 76 // No need to escape element_title since constant string is passed. | 79 // No need to escape element_title since constant string is passed. |
| 77 out->append(element_title); | 80 out->append(element_title); |
| 78 out->append(net::EscapeForHTML(element_data)); | 81 out->append(net::EscapeForHTML(element_data)); |
| 79 out->append("</li>"); | 82 out->append("</li>\n"); |
| 80 } | 83 } |
| 81 | 84 |
| 82 void AddHTMLButton(const std::string& title, | 85 void AddHorizontalRule(std::string* out) { |
| 83 const std::string& command, | 86 out->append("\n<hr>\n"); |
| 84 std::string* out) { | |
| 85 // No need to escape title since constant string is passed. | |
| 86 std::string escaped_command = net::EscapeForHTML(command.c_str()); | |
| 87 base::StringAppendF(out, | |
| 88 "<form action=\"\" method=\"GET\">\n" | |
| 89 "<input type=\"hidden\" name=\"remove\" value=\"%s\">\n" | |
| 90 "<input type=\"submit\" value=\"%s\">\n" | |
| 91 "</form><br/>\n", | |
| 92 escaped_command.c_str(), | |
| 93 title.c_str()); | |
| 94 } | 87 } |
| 95 | 88 |
| 96 } // namespace | 89 } // namespace |
| 97 | 90 |
| 98 namespace webkit_blob { | 91 namespace webkit_blob { |
| 99 | 92 |
| 100 ViewBlobInternalsJob::ViewBlobInternalsJob( | 93 ViewBlobInternalsJob::ViewBlobInternalsJob( |
| 101 net::URLRequest* request, | 94 net::URLRequest* request, |
| 102 net::NetworkDelegate* network_delegate, | 95 net::NetworkDelegate* network_delegate, |
| 103 BlobStorageController* blob_storage_controller) | 96 BlobStorageContext* blob_storage_context) |
| 104 : net::URLRequestSimpleJob(request, network_delegate), | 97 : net::URLRequestSimpleJob(request, network_delegate), |
| 105 blob_storage_controller_(blob_storage_controller), | 98 blob_storage_context_(blob_storage_context), |
| 106 weak_factory_(this) { | 99 weak_factory_(this) { |
| 107 } | 100 } |
| 108 | 101 |
| 109 ViewBlobInternalsJob::~ViewBlobInternalsJob() { | 102 ViewBlobInternalsJob::~ViewBlobInternalsJob() { |
| 110 } | 103 } |
| 111 | 104 |
| 112 void ViewBlobInternalsJob::Start() { | 105 void ViewBlobInternalsJob::Start() { |
| 113 base::MessageLoop::current()->PostTask( | 106 base::MessageLoop::current()->PostTask( |
| 114 FROM_HERE, | 107 FROM_HERE, |
| 115 base::Bind(&ViewBlobInternalsJob::DoWorkAsync, | 108 base::Bind(&ViewBlobInternalsJob::StartAsync, |
| 116 weak_factory_.GetWeakPtr())); | 109 weak_factory_.GetWeakPtr())); |
| 117 } | 110 } |
| 118 | 111 |
| 119 bool ViewBlobInternalsJob::IsRedirectResponse(GURL* location, | 112 bool ViewBlobInternalsJob::IsRedirectResponse(GURL* location, |
| 120 int* http_status_code) { | 113 int* http_status_code) { |
| 121 if (request_->url().has_query()) { | 114 if (request_->url().has_query()) { |
| 122 // Strip the query parameters. | 115 // Strip the query parameters. |
| 123 GURL::Replacements replacements; | 116 GURL::Replacements replacements; |
| 124 replacements.ClearQuery(); | 117 replacements.ClearQuery(); |
| 125 *location = request_->url().ReplaceComponents(replacements); | 118 *location = request_->url().ReplaceComponents(replacements); |
| 126 *http_status_code = 307; | 119 *http_status_code = 307; |
| 127 return true; | 120 return true; |
| 128 } | 121 } |
| 129 return false; | 122 return false; |
| 130 } | 123 } |
| 131 | 124 |
| 132 void ViewBlobInternalsJob::Kill() { | 125 void ViewBlobInternalsJob::Kill() { |
| 133 net::URLRequestSimpleJob::Kill(); | 126 net::URLRequestSimpleJob::Kill(); |
| 134 weak_factory_.InvalidateWeakPtrs(); | 127 weak_factory_.InvalidateWeakPtrs(); |
| 135 } | 128 } |
| 136 | 129 |
| 137 void ViewBlobInternalsJob::DoWorkAsync() { | |
| 138 if (request_->url().has_query() && | |
| 139 StartsWithASCII(request_->url().query(), "remove=", true)) { | |
| 140 std::string blob_url = request_->url().query().substr(strlen("remove=")); | |
| 141 blob_url = net::UnescapeURLComponent(blob_url, | |
| 142 net::UnescapeRule::NORMAL | net::UnescapeRule::URL_SPECIAL_CHARS); | |
| 143 blob_storage_controller_->RemoveBlob(GURL(blob_url)); | |
| 144 } | |
| 145 | |
| 146 StartAsync(); | |
| 147 } | |
| 148 | |
| 149 int ViewBlobInternalsJob::GetData( | 130 int ViewBlobInternalsJob::GetData( |
| 150 std::string* mime_type, | 131 std::string* mime_type, |
| 151 std::string* charset, | 132 std::string* charset, |
| 152 std::string* data, | 133 std::string* data, |
| 153 const net::CompletionCallback& callback) const { | 134 const net::CompletionCallback& callback) const { |
| 154 mime_type->assign("text/html"); | 135 mime_type->assign("text/html"); |
| 155 charset->assign("UTF-8"); | 136 charset->assign("UTF-8"); |
| 156 | 137 |
| 157 data->clear(); | 138 data->clear(); |
| 158 StartHTML(data); | 139 StartHTML(data); |
| 159 if (blob_storage_controller_->blob_map_.empty()) | 140 if (blob_storage_context_->blob_map_.empty()) |
| 160 data->append(kEmptyBlobStorageMessage); | 141 data->append(kEmptyBlobStorageMessage); |
| 161 else | 142 else |
| 162 GenerateHTML(data); | 143 GenerateHTML(data); |
| 163 EndHTML(data); | 144 EndHTML(data); |
| 164 return net::OK; | 145 return net::OK; |
| 165 } | 146 } |
| 166 | 147 |
| 167 void ViewBlobInternalsJob::GenerateHTML(std::string* out) const { | 148 void ViewBlobInternalsJob::GenerateHTML(std::string* out) const { |
| 168 for (BlobStorageController::BlobMap::const_iterator iter = | 149 for (BlobStorageContext::BlobMap::const_iterator iter = |
| 169 blob_storage_controller_->blob_map_.begin(); | 150 blob_storage_context_->blob_map_.begin(); |
| 170 iter != blob_storage_controller_->blob_map_.end(); | 151 iter != blob_storage_context_->blob_map_.end(); |
| 171 ++iter) { | 152 ++iter) { |
| 172 AddHTMLBoldText(iter->first, out); | 153 AddHTMLBoldText(iter->first, out); |
| 173 AddHTMLButton(kRemove, iter->first, out); | 154 GenerateHTMLForBlobData(*(iter->second.data.get()), |
| 174 GenerateHTMLForBlobData(*iter->second.get(), out); | 155 iter->second.refcount, |
| 156 out); |
| 157 } |
| 158 if (!blob_storage_context_->public_blob_urls_.empty()) { |
| 159 AddHorizontalRule(out); |
| 160 for (BlobStorageContext::BlobURLMap::const_iterator iter = |
| 161 blob_storage_context_->public_blob_urls_.begin(); |
| 162 iter != blob_storage_context_->public_blob_urls_.end(); |
| 163 ++iter) { |
| 164 AddHTMLBoldText(iter->first.spec(), out); |
| 165 StartHTMLList(out); |
| 166 AddHTMLListItem(kUUID, iter->second, out); |
| 167 EndHTMLList(out); |
| 168 } |
| 169 } |
| 170 if (!blob_storage_context_->deprecated_blob_urls_.empty()) { |
| 171 AddHorizontalRule(out); |
| 172 for (BlobStorageContext::BlobURLMap::const_iterator iter = |
| 173 blob_storage_context_->deprecated_blob_urls_.begin(); |
| 174 iter != blob_storage_context_->deprecated_blob_urls_.end(); |
| 175 ++iter) { |
| 176 AddHTMLBoldText(iter->first.spec(), out); |
| 177 StartHTMLList(out); |
| 178 AddHTMLListItem(kUUID, iter->second, out); |
| 179 EndHTMLList(out); |
| 180 } |
| 175 } | 181 } |
| 176 } | 182 } |
| 177 | 183 |
| 178 void ViewBlobInternalsJob::GenerateHTMLForBlobData(const BlobData& blob_data, | 184 void ViewBlobInternalsJob::GenerateHTMLForBlobData(const BlobData& blob_data, |
| 185 int refcount, |
| 179 std::string* out) { | 186 std::string* out) { |
| 180 StartHTMLList(out); | 187 StartHTMLList(out); |
| 181 | 188 |
| 189 AddHTMLListItem(kRefcount, base::IntToString(refcount), out); |
| 182 if (!blob_data.content_type().empty()) | 190 if (!blob_data.content_type().empty()) |
| 183 AddHTMLListItem(kContentType, blob_data.content_type(), out); | 191 AddHTMLListItem(kContentType, blob_data.content_type(), out); |
| 184 if (!blob_data.content_disposition().empty()) | 192 if (!blob_data.content_disposition().empty()) |
| 185 AddHTMLListItem(kContentDisposition, blob_data.content_disposition(), out); | 193 AddHTMLListItem(kContentDisposition, blob_data.content_disposition(), out); |
| 186 | 194 |
| 187 bool has_multi_items = blob_data.items().size() > 1; | 195 bool has_multi_items = blob_data.items().size() > 1; |
| 188 if (has_multi_items) { | 196 if (has_multi_items) { |
| 189 AddHTMLListItem(kCount, | 197 AddHTMLListItem(kCount, |
| 190 UTF16ToUTF8(base::FormatNumber(blob_data.items().size())), out); | 198 UTF16ToUTF8(base::FormatNumber(blob_data.items().size())), out); |
| 191 } | 199 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 206 AddHTMLListItem(kPath, | 214 AddHTMLListItem(kPath, |
| 207 net::EscapeForHTML(item.path().AsUTF8Unsafe()), | 215 net::EscapeForHTML(item.path().AsUTF8Unsafe()), |
| 208 out); | 216 out); |
| 209 if (!item.expected_modification_time().is_null()) { | 217 if (!item.expected_modification_time().is_null()) { |
| 210 AddHTMLListItem(kModificationTime, UTF16ToUTF8( | 218 AddHTMLListItem(kModificationTime, UTF16ToUTF8( |
| 211 TimeFormatFriendlyDateAndTime(item.expected_modification_time())), | 219 TimeFormatFriendlyDateAndTime(item.expected_modification_time())), |
| 212 out); | 220 out); |
| 213 } | 221 } |
| 214 break; | 222 break; |
| 215 case BlobData::Item::TYPE_BLOB: | 223 case BlobData::Item::TYPE_BLOB: |
| 216 AddHTMLListItem(kType, "blob", out); | 224 NOTREACHED(); // Should be flattened in the storage context. |
| 217 AddHTMLListItem(kURL, item.url().spec(), out); | |
| 218 break; | 225 break; |
| 219 case BlobData::Item::TYPE_FILE_FILESYSTEM: | 226 case BlobData::Item::TYPE_FILE_FILESYSTEM: |
| 220 AddHTMLListItem(kType, "filesystem", out); | 227 AddHTMLListItem(kType, "filesystem", out); |
| 221 AddHTMLListItem(kURL, item.url().spec(), out); | 228 AddHTMLListItem(kURL, item.filesystem_url().spec(), out); |
| 222 if (!item.expected_modification_time().is_null()) { | 229 if (!item.expected_modification_time().is_null()) { |
| 223 AddHTMLListItem(kModificationTime, UTF16ToUTF8( | 230 AddHTMLListItem(kModificationTime, UTF16ToUTF8( |
| 224 TimeFormatFriendlyDateAndTime(item.expected_modification_time())), | 231 TimeFormatFriendlyDateAndTime(item.expected_modification_time())), |
| 225 out); | 232 out); |
| 226 } | 233 } |
| 227 break; | 234 break; |
| 228 case BlobData::Item::TYPE_UNKNOWN: | 235 case BlobData::Item::TYPE_UNKNOWN: |
| 229 NOTREACHED(); | 236 NOTREACHED(); |
| 230 break; | 237 break; |
| 231 } | 238 } |
| 232 if (item.offset()) { | 239 if (item.offset()) { |
| 233 AddHTMLListItem(kOffset, UTF16ToUTF8(base::FormatNumber( | 240 AddHTMLListItem(kOffset, UTF16ToUTF8(base::FormatNumber( |
| 234 static_cast<int64>(item.offset()))), out); | 241 static_cast<int64>(item.offset()))), out); |
| 235 } | 242 } |
| 236 if (static_cast<int64>(item.length()) != -1) { | 243 if (static_cast<int64>(item.length()) != -1) { |
| 237 AddHTMLListItem(kLength, UTF16ToUTF8(base::FormatNumber( | 244 AddHTMLListItem(kLength, UTF16ToUTF8(base::FormatNumber( |
| 238 static_cast<int64>(item.length()))), out); | 245 static_cast<int64>(item.length()))), out); |
| 239 } | 246 } |
| 240 | 247 |
| 241 if (has_multi_items) | 248 if (has_multi_items) |
| 242 EndHTMLList(out); | 249 EndHTMLList(out); |
| 243 } | 250 } |
| 244 | 251 |
| 245 EndHTMLList(out); | 252 EndHTMLList(out); |
| 246 } | 253 } |
| 247 | 254 |
| 248 } // namespace webkit_blob | 255 } // namespace webkit_blob |
| OLD | NEW |