diff options
author | kkanetkar@chromium.org <kkanetkar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-27 01:36:43 +0000 |
---|---|---|
committer | kkanetkar@chromium.org <kkanetkar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-27 01:36:43 +0000 |
commit | 042ecea347c6984c3263671f45ebec79e9fbb26f (patch) | |
tree | bfedface274ededad945b9c7d7871a19941d56ee /webkit/appcache/view_appcache_internals_job.cc | |
parent | 43929df205159d29c8e968e93c8572b05c92de17 (diff) | |
download | chromium_src-042ecea347c6984c3263671f45ebec79e9fbb26f.zip chromium_src-042ecea347c6984c3263671f45ebec79e9fbb26f.tar.gz chromium_src-042ecea347c6984c3263671f45ebec79e9fbb26f.tar.bz2 |
A basic implementation of information about appcache. Lists manifest files, time and size information.
BUG=38463
TEST=Run chrome, navigate to about:appcache-internals.
Review URL: http://codereview.chromium.org/1109009
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42888 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/appcache/view_appcache_internals_job.cc')
-rw-r--r-- | webkit/appcache/view_appcache_internals_job.cc | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/webkit/appcache/view_appcache_internals_job.cc b/webkit/appcache/view_appcache_internals_job.cc new file mode 100644 index 0000000..49384ac --- /dev/null +++ b/webkit/appcache/view_appcache_internals_job.cc @@ -0,0 +1,167 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "webkit/appcache/view_appcache_internals_job.h" + +#include "base/logging.h" +#include "base/format_macros.h" +#include "base/utf_string_conversions.h" +#include "base/i18n/time_formatting.h" +#include "base/string_util.h" +#include "webkit/appcache/appcache_service.h" + +namespace { + +const char kErrorMessage[] = "Error in retrieving Application caches."; +const char kEmptyAppCachesMessage[] = "No available Application caches."; + +void StartHTML(std::string* out) { + DCHECK(out); + out->append( + "<!DOCTYPE HTML>" + "<html><head><title>AppCache Internals</title>" + "<style>" + "body { font-family: sans-serif; font-size: 0.8em; }\n" + "tt, code, pre { font-family: WebKitHack, monospace; }\n" + ".subsection_body { margin: 10px 0 10px 2em; }\n" + ".subsection_title { font-weight: bold; }\n" + "</style>"); +} + +void EndHTML(std::string* out) { + DCHECK(out); + out->append("</body></html>"); +} + +void AddLiTag(const std::string& element_title, + const std::string& element_data, std::string* out) { + DCHECK(out); + out->append("<li>"); + out->append(element_title); + out->append(element_data); + out->append("</li>"); +} + +void WrapInHREF(const std::string& in, std::string* out) { + out->append("<a href='"); + out->append(in); + out->append("'>"); + out->append(in); + out->append("</a><br/>"); +} + +void AddHTMLFromAppCacheToOutput( + const appcache::AppCacheInfoVector& appcaches, std::string* out) { + for (std::vector<appcache::AppCacheInfo>::const_iterator info = + appcaches.begin(); + info != appcaches.end(); ++info) { + out->append("<p>"); + std::string anchored_manifest; + WrapInHREF(info->manifest_url.spec(), &anchored_manifest); + out->append("Manifest: "); + out->append(anchored_manifest); + out->append("<ul>"); + + AddLiTag("Size: ", + WideToUTF8(FormatBytes( + info->size, GetByteDisplayUnits(info->size), true)), + out); + AddLiTag("Creation Time: ", + WideToUTF8(TimeFormatFriendlyDateAndTime( + info->creation_time)), + out); + AddLiTag("Last Access Time: ", + WideToUTF8( + TimeFormatFriendlyDateAndTime(info->last_access_time)), + out); + AddLiTag("Last Update Time: ", + WideToUTF8(TimeFormatFriendlyDateAndTime(info->last_update_time)), + out); + + out->append("</ul></p></br>"); + } +} + +struct ManifestURLComparator { + public: + bool operator() ( + const appcache::AppCacheInfo& lhs, + const appcache::AppCacheInfo& rhs) const { + return (lhs.manifest_url.spec() < rhs.manifest_url.spec()); + } +} manifest_url_comparator; + + +} // namespace + +namespace appcache { + +ViewAppCacheInternalsJob::ViewAppCacheInternalsJob( + URLRequest* request, + AppCacheService* service) : URLRequestSimpleJob(request), + appcache_service_(service) { +} + +ViewAppCacheInternalsJob::~ViewAppCacheInternalsJob() { + // Cancel callback if job is destroyed before callback is called. + if (appcache_info_callback_) + appcache_info_callback_.release()->Cancel(); +} + +void ViewAppCacheInternalsJob::Start() { + if (!request_) + return; + + info_collection_ = new AppCacheInfoCollection; + appcache_info_callback_ = + new net::CancelableCompletionCallback<ViewAppCacheInternalsJob>( + this, &ViewAppCacheInternalsJob::OnGotAppCacheInfo); + + appcache_service_->GetAllAppCacheInfo( + info_collection_, appcache_info_callback_); +} + +void ViewAppCacheInternalsJob::GenerateHTMLAppCacheInfo( + std::string* out) const { + typedef std::map<GURL, AppCacheInfoVector> InfoByOrigin; + AppCacheInfoVector appcaches; + for (InfoByOrigin::const_iterator origin = + info_collection_->infos_by_origin.begin(); + origin != info_collection_->infos_by_origin.end(); ++origin) { + for (AppCacheInfoVector::const_iterator info = + origin->second.begin(); info != origin->second.end(); ++info) + appcaches.push_back(*info); + } + + std::sort(appcaches.begin(), appcaches.end(), manifest_url_comparator); + + AddHTMLFromAppCacheToOutput(appcaches, out); +} + +void ViewAppCacheInternalsJob::OnGotAppCacheInfo(int rv) { + appcache_info_callback_ = NULL; + if (rv != net::OK) + info_collection_ = NULL; + StartAsync(); +} + +bool ViewAppCacheInternalsJob::GetData(std::string* mime_type, + std::string* charset, + std::string* data) const { + mime_type->assign("text/html"); + charset->assign("UTF-8"); + + data->clear(); + StartHTML(data); + if (!info_collection_.get()) + data->append(kErrorMessage); + else if (info_collection_->infos_by_origin.empty()) + data->append(kEmptyAppCachesMessage); + else + GenerateHTMLAppCacheInfo(data); + EndHTML(data); + return true; +} + +} // namespace appcache |