1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsAboutCache.h"
#include "nsIInputStream.h"
#include "nsIURI.h"
#include "nsCOMPtr.h"
#include "nsNetUtil.h"
#include "nsIPipe.h"
#include "nsContentUtils.h"
#include "nsEscape.h"
#include "nsAboutProtocolUtils.h"
#include "nsPrintfCString.h"
#include "nsICacheStorageService.h"
#include "nsICacheStorage.h"
#include "CacheFileUtils.h"
#include "CacheObserver.h"
#include "nsThreadUtils.h"
#include "mozilla/Components.h"
using namespace mozilla::net;
NS_IMPL_ISUPPORTS(nsAboutCache, nsIAboutModule)
NS_IMPL_ISUPPORTS(nsAboutCache::Channel, nsIChannel, nsIRequest,
nsICacheStorageVisitor)
NS_IMETHODIMP
nsAboutCache::NewChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo,
nsIChannel** result) {
nsresult rv;
NS_ENSURE_ARG_POINTER(aURI);
RefPtr<Channel> channel = new Channel();
rv = channel->Init(aURI, aLoadInfo);
if (NS_FAILED(rv)) return rv;
channel.forget(result);
return NS_OK;
}
nsresult nsAboutCache::Channel::Init(nsIURI* aURI, nsILoadInfo* aLoadInfo) {
nsresult rv;
mCancel = false;
nsCOMPtr<nsIInputStream> inputStream;
NS_NewPipe(getter_AddRefs(inputStream), getter_AddRefs(mStream), 16384,
(uint32_t)-1,
true, // non-blocking input
false // blocking output
);
nsAutoCString storageName;
rv = ParseURI(aURI, storageName);
if (NS_FAILED(rv)) return rv;
mOverview = storageName.IsEmpty();
if (mOverview) {
// ...and visit all we can
mStorageList.AppendElement("memory"_ns);
mStorageList.AppendElement("disk"_ns);
} else {
// ...and visit just the specified storage, entries will output too
mStorageList.AppendElement(storageName);
}
// The entries header is added on encounter of the first entry
mEntriesHeaderAdded = false;
rv = NS_NewInputStreamChannelInternal(getter_AddRefs(mChannel), aURI,
inputStream.forget(), "text/html"_ns,
"utf-8"_ns, aLoadInfo);
if (NS_FAILED(rv)) return rv;
mBuffer.AssignLiteral(
"<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
" <title>Network Cache Storage Information</title>\n"
" <meta charset=\"utf-8\">\n"
" <meta name=\"color-scheme\" content=\"light dark\">\n"
" <meta http-equiv=\"Content-Security-Policy\" content=\"default-src "
"chrome:; object-src 'none'\"/>\n"
" <link rel=\"stylesheet\" "
"href=\"chrome://global/skin/in-content/info-pages.css\"/>\n"
" <link rel=\"stylesheet\" "
"href=\"chrome://global/skin/aboutCache.css\"/>\n"
"</head>\n"
"<body class=\"aboutPageWideContainer\">\n"
"<h1>Information about the Network Cache Storage Service</h1>\n");
if (!mOverview) {
mBuffer.AppendLiteral(
"<a href=\"about:cache?storage=\">Back to overview</a>\n");
mBuffer.AppendLiteral(
"<p id=\"explanation-dataSize\">Data sizes refer to the size of the "
"response body and do not reflect the amount of disk space that the "
"file occupies.</p>\n");
}
rv = FlushBuffer();
if (NS_FAILED(rv)) {
NS_WARNING("Failed to flush buffer");
}
return NS_OK;
}
NS_IMETHODIMP nsAboutCache::Channel::AsyncOpen(nsIStreamListener* aListener) {
nsresult rv;
if (!mChannel) {
return NS_ERROR_UNEXPECTED;
}
// Kick the walk loop.
rv = VisitNextStorage();
if (NS_FAILED(rv)) return rv;
rv = mChannel->AsyncOpen(aListener);
if (NS_FAILED(rv)) return rv;
return NS_OK;
}
NS_IMETHODIMP nsAboutCache::Channel::Open(nsIInputStream** _retval) {
return NS_ERROR_NOT_IMPLEMENTED;
}
nsresult nsAboutCache::Channel::ParseURI(nsIURI* uri, nsACString& storage) {
//
// about:cache[?storage=<storage-name>[&context=<context-key>]]
//
nsresult rv;
nsAutoCString path;
rv = uri->GetPathQueryRef(path);
if (NS_FAILED(rv)) return rv;
storage.Truncate();
nsACString::const_iterator start, valueStart, end;
path.BeginReading(start);
path.EndReading(end);
valueStart = end;
if (!FindInReadable("?storage="_ns, start, valueStart)) {
return NS_OK;
}
storage.Assign(Substring(valueStart, end));
return NS_OK;
}
nsresult nsAboutCache::Channel::VisitNextStorage() {
if (!mStorageList.Length()) return NS_ERROR_NOT_AVAILABLE;
mStorageName = mStorageList[0];
mStorageList.RemoveElementAt(0);
// Must re-dispatch since we cannot start another visit cycle
// from visitor callback. The cache v1 service doesn't like it.
// TODO - mayhemer, bug 913828, remove this dispatch and call
// directly.
return NS_DispatchToMainThread(mozilla::NewRunnableMethod(
"nsAboutCache::Channel::FireVisitStorage", this,
&nsAboutCache::Channel::FireVisitStorage));
}
void nsAboutCache::Channel::FireVisitStorage() {
nsresult rv;
rv = VisitStorage(mStorageName);
if (NS_FAILED(rv)) {
nsAutoCString escaped;
nsAppendEscapedHTML(mStorageName, escaped);
mBuffer.Append(nsPrintfCString(
"<p>Unrecognized storage name '%s' in about:cache URL</p>",
escaped.get()));
rv = FlushBuffer();
if (NS_FAILED(rv)) {
NS_WARNING("Failed to flush buffer");
}
// Simulate finish of a visit cycle, this tries the next storage
// or closes the output stream (i.e. the UI loader will stop spinning)
OnCacheEntryVisitCompleted();
}
}
nsresult nsAboutCache::Channel::VisitStorage(nsACString const& storageName) {
nsresult rv;
rv = GetStorage(storageName, nullptr, getter_AddRefs(mStorage));
if (NS_FAILED(rv)) return rv;
rv = mStorage->AsyncVisitStorage(this, !mOverview);
if (NS_FAILED(rv)) return rv;
return NS_OK;
}
// static
nsresult nsAboutCache::GetStorage(nsACString const& storageName,
nsILoadContextInfo* loadInfo,
nsICacheStorage** storage) {
nsresult rv;
nsCOMPtr<nsICacheStorageService> cacheService;
cacheService = mozilla::components::CacheStorage::Service(&rv);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsICacheStorage> cacheStorage;
if (storageName == "disk") {
rv = cacheService->DiskCacheStorage(loadInfo, getter_AddRefs(cacheStorage));
} else if (storageName == "memory") {
rv = cacheService->MemoryCacheStorage(loadInfo,
getter_AddRefs(cacheStorage));
} else {
rv = NS_ERROR_UNEXPECTED;
}
if (NS_FAILED(rv)) return rv;
cacheStorage.forget(storage);
return NS_OK;
}
NS_IMETHODIMP
nsAboutCache::Channel::OnCacheStorageInfo(uint32_t aEntryCount,
uint64_t aConsumption,
uint64_t aCapacity,
nsIFile* aDirectory) {
// We need mStream for this
if (!mStream) {
return NS_ERROR_FAILURE;
}
mBuffer.AssignLiteral("<h2>");
nsAppendEscapedHTML(mStorageName, mBuffer);
mBuffer.AppendLiteral(
"</h2>\n"
"<table id=\"");
mBuffer.AppendLiteral("\">\n");
// Write out cache info
// Number of entries
mBuffer.AppendLiteral(
" <tr>\n"
" <th>Number of entries:</th>\n"
" <td>");
mBuffer.AppendInt(aEntryCount);
mBuffer.AppendLiteral(
"</td>\n"
" </tr>\n");
// Maximum storage size
mBuffer.AppendLiteral(
" <tr>\n"
" <th>Maximum storage size:</th>\n"
" <td>");
mBuffer.AppendInt(aCapacity / 1024);
mBuffer.AppendLiteral(
" KiB</td>\n"
" </tr>\n");
// Storage in use
mBuffer.AppendLiteral(
" <tr>\n"
" <th>Storage in use:</th>\n"
" <td>");
mBuffer.AppendInt(aConsumption / 1024);
mBuffer.AppendLiteral(
" KiB</td>\n"
" </tr>\n");
// Storage disk location
mBuffer.AppendLiteral(
" <tr>\n"
" <th>Storage disk location:</th>\n"
" <td>");
if (aDirectory) {
nsAutoString path;
aDirectory->GetPath(path);
mBuffer.Append(NS_ConvertUTF16toUTF8(path));
} else {
mBuffer.AppendLiteral("none, only stored in memory");
}
mBuffer.AppendLiteral(
" </td>\n"
" </tr>\n");
if (mOverview) { // The about:cache case
if (aEntryCount != 0) { // Add the "List Cache Entries" link
mBuffer.AppendLiteral(
" <tr>\n"
" <td colspan=\"2\"><a href=\"about:cache?storage=");
nsAppendEscapedHTML(mStorageName, mBuffer);
mBuffer.AppendLiteral(
"\">List Cache Entries</a></td>\n"
" </tr>\n");
}
}
mBuffer.AppendLiteral("</table>\n");
// The entries header is added on encounter of the first entry
mEntriesHeaderAdded = false;
nsresult rv = FlushBuffer();
if (NS_FAILED(rv)) {
NS_WARNING("Failed to flush buffer");
}
if (mOverview) {
// OnCacheEntryVisitCompleted() is not called when we do not iterate
// cache entries. Since this moves forward to the next storage in
// the list we want to visit, artificially call it here.
OnCacheEntryVisitCompleted();
}
return NS_OK;
}
NS_IMETHODIMP
nsAboutCache::Channel::OnCacheEntryInfo(
nsIURI* aURI, const nsACString& aIdEnhance, int64_t aDataSize,
int64_t aAltDataSize, uint32_t aFetchCount, uint32_t aLastModified,
uint32_t aExpirationTime, bool aPinned, nsILoadContextInfo* aInfo) {
// We need mStream for this
if (!mStream || mCancel) {
// Returning a failure from this callback stops the iteration
return NS_ERROR_FAILURE;
}
if (!mEntriesHeaderAdded) {
mBuffer.AppendLiteral(
"<hr/>\n"
"<table id=\"entries\">\n"
" <colgroup>\n"
" <col id=\"col-key\">\n"
" <col id=\"col-dataSize\">\n"
" <col id=\"col-altDataSize\">\n"
" <col id=\"col-fetchCount\">\n"
" <col id=\"col-lastModified\">\n"
" <col id=\"col-expires\">\n"
" <col id=\"col-pinned\">\n"
" </colgroup>\n"
" <thead>\n"
" <tr>\n"
" <th>Key</th>\n"
" <th>Data size</th>\n"
" <th>Alternative Data size</th>\n"
" <th>Fetch count</th>\n"
" <th>Last Modifed</th>\n"
" <th>Expires</th>\n"
" <th>Pinning</th>\n"
" </tr>\n"
" </thead>\n");
mEntriesHeaderAdded = true;
}
// Generate a about:cache-entry URL for this entry...
nsAutoCString url;
url.AssignLiteral("about:cache-entry?storage=");
nsAppendEscapedHTML(mStorageName, url);
nsAutoCString context;
CacheFileUtils::AppendKeyPrefix(aInfo, context);
url.AppendLiteral("&context=");
nsAppendEscapedHTML(context, url);
url.AppendLiteral("&eid=");
nsAppendEscapedHTML(aIdEnhance, url);
nsAutoCString cacheUriSpec;
aURI->GetAsciiSpec(cacheUriSpec);
nsAutoCString escapedCacheURI;
nsAppendEscapedHTML(cacheUriSpec, escapedCacheURI);
url.AppendLiteral("&uri=");
url += escapedCacheURI;
// Entry start...
mBuffer.AppendLiteral(" <tr>\n");
// URI
mBuffer.AppendLiteral(" <td><a href=\"");
mBuffer.Append(url);
mBuffer.AppendLiteral("\">");
if (!aIdEnhance.IsEmpty()) {
nsAppendEscapedHTML(aIdEnhance, mBuffer);
mBuffer.Append(':');
}
mBuffer.Append(escapedCacheURI);
mBuffer.AppendLiteral("</a>");
if (!context.IsEmpty()) {
mBuffer.AppendLiteral("<br><span title=\"Context separation key\">");
nsAutoCString escapedContext;
nsAppendEscapedHTML(context, escapedContext);
mBuffer.Append(escapedContext);
mBuffer.AppendLiteral("</span>");
}
mBuffer.AppendLiteral("</td>\n");
// Content length
mBuffer.AppendLiteral(" <td>");
mBuffer.AppendInt(aDataSize);
mBuffer.AppendLiteral(" bytes</td>\n");
// Length of alternative content
mBuffer.AppendLiteral(" <td>");
mBuffer.AppendInt(aAltDataSize);
mBuffer.AppendLiteral(" bytes</td>\n");
// Number of accesses
mBuffer.AppendLiteral(" <td>");
mBuffer.AppendInt(aFetchCount);
mBuffer.AppendLiteral("</td>\n");
// vars for reporting time
char buf[255];
// Last modified time
mBuffer.AppendLiteral(" <td>");
if (aLastModified) {
PrintTimeString(buf, sizeof(buf), aLastModified);
mBuffer.Append(buf);
} else {
mBuffer.AppendLiteral("No last modified time");
}
mBuffer.AppendLiteral("</td>\n");
// Expires time
mBuffer.AppendLiteral(" <td>");
// Bug - 633747.
// When expiration time is 0, we show 1970-01-01 01:00:00 which is confusing.
// So we check if time is 0, then we show a message, "Expired Immediately"
if (aExpirationTime == 0) {
mBuffer.AppendLiteral("Expired Immediately");
} else if (aExpirationTime < 0xFFFFFFFF) {
PrintTimeString(buf, sizeof(buf), aExpirationTime);
mBuffer.Append(buf);
} else {
mBuffer.AppendLiteral("No expiration time");
}
mBuffer.AppendLiteral("</td>\n");
// Pinning
mBuffer.AppendLiteral(" <td>");
if (aPinned) {
mBuffer.AppendLiteral("Pinned");
} else {
mBuffer.AppendLiteral(" ");
}
mBuffer.AppendLiteral("</td>\n");
// Entry is done...
mBuffer.AppendLiteral(" </tr>\n");
return FlushBuffer();
}
NS_IMETHODIMP
nsAboutCache::Channel::OnCacheEntryVisitCompleted() {
if (!mStream) {
return NS_ERROR_FAILURE;
}
if (mEntriesHeaderAdded) {
mBuffer.AppendLiteral("</table>\n");
}
// Kick another storage visiting (from a storage that allows us.)
while (mStorageList.Length()) {
nsresult rv = VisitNextStorage();
if (NS_SUCCEEDED(rv)) {
// Expecting new round of OnCache* calls.
return NS_OK;
}
}
// We are done!
mBuffer.AppendLiteral(
"</body>\n"
"</html>\n");
nsresult rv = FlushBuffer();
if (NS_FAILED(rv)) {
NS_WARNING("Failed to flush buffer");
}
mStream->Close();
return NS_OK;
}
nsresult nsAboutCache::Channel::FlushBuffer() {
nsresult rv;
uint32_t bytesWritten;
rv = mStream->Write(mBuffer.get(), mBuffer.Length(), &bytesWritten);
mBuffer.Truncate();
if (NS_FAILED(rv)) {
mCancel = true;
}
return rv;
}
NS_IMETHODIMP
nsAboutCache::GetURIFlags(nsIURI* aURI, uint32_t* result) {
*result = nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
nsIAboutModule::IS_SECURE_CHROME_UI;
return NS_OK;
}
// static
nsresult nsAboutCache::Create(REFNSIID aIID, void** aResult) {
RefPtr<nsAboutCache> about = new nsAboutCache();
return about->QueryInterface(aIID, aResult);
}
NS_IMETHODIMP
nsAboutCache::GetChromeURI(nsIURI* aURI, nsIURI** chromeURI) {
return NS_ERROR_ILLEGAL_VALUE;
}
////////////////////////////////////////////////////////////////////////////////
|