diff options
author | eroman@chromium.org <eroman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-14 20:51:04 +0000 |
---|---|---|
committer | eroman@chromium.org <eroman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-14 20:51:04 +0000 |
commit | 13c34d1395636e081be2318e8ae12bc8fa71ca57 (patch) | |
tree | 4a719a69338f1cc14fe23db88e061c513cf122db | |
parent | 1364d8b89dd74afd35a5d9fe4bb4f3c66d09c3c4 (diff) | |
download | chromium_src-13c34d1395636e081be2318e8ae12bc8fa71ca57.zip chromium_src-13c34d1395636e081be2318e8ae12bc8fa71ca57.tar.gz chromium_src-13c34d1395636e081be2318e8ae12bc8fa71ca57.tar.bz2 |
Change the URLs used to access "view-cache:" and "view-net-internals:".
"net-internal:*" ==> "chrome://net-internals/*"
"view-cache:*" ==> "chrome://net-internals/view-cache/*"
"view-cache:stats" ==> "chrome://net-internals/httpcache.stats"
As before, there are also aliases from the "about:*" page:
"about:net-internal[/*]" aliases "chrome://net-internals/*"
"about:cache[/*]" aliases "chrome://net-internals/view-cache"
BUG=http://crbug.com/21551
Review URL: http://codereview.chromium.org/202067
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@26158 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/browser_about_handler.cc | 38 | ||||
-rw-r--r-- | chrome/browser/child_process_security_policy_unittest.cc | 2 | ||||
-rw-r--r-- | chrome/browser/dom_ui/chrome_url_data_manager.cc | 35 | ||||
-rw-r--r-- | chrome/common/url_constants.cc | 7 | ||||
-rw-r--r-- | chrome/common/url_constants.h | 8 | ||||
-rw-r--r-- | net/base/net_util.h | 2 | ||||
-rw-r--r-- | net/base/net_util_unittest.cc | 2 | ||||
-rw-r--r-- | net/net.gyp | 8 | ||||
-rw-r--r-- | net/url_request/url_request_job_manager.cc | 4 | ||||
-rw-r--r-- | net/url_request/url_request_view_cache_job.h | 32 | ||||
-rw-r--r-- | net/url_request/url_request_view_net_internal_job.h | 26 | ||||
-rw-r--r-- | net/url_request/url_request_view_net_internals_job.cc (renamed from net/url_request/url_request_view_net_internal_job.cc) | 93 | ||||
-rw-r--r-- | net/url_request/url_request_view_net_internals_job.h | 42 | ||||
-rw-r--r-- | net/url_request/view_cache_helper.cc (renamed from net/url_request/url_request_view_cache_job.cc) | 78 | ||||
-rw-r--r-- | net/url_request/view_cache_helper.h | 24 |
15 files changed, 252 insertions, 149 deletions
diff --git a/chrome/browser/browser_about_handler.cc b/chrome/browser/browser_about_handler.cc index 047ca97..3e4baf3 100644 --- a/chrome/browser/browser_about_handler.cc +++ b/chrome/browser/browser_about_handler.cc @@ -759,6 +759,23 @@ void ChromeOSAboutVersionHandler::OnVersion( #endif +// Returns true if |url|'s spec starts with |about_specifier|, and is +// terminated by the start of a path. +bool StartsWithAboutSpecifier(const GURL& url, const char* about_specifier) { + return StartsWithASCII(url.spec(), about_specifier, true) && + (url.spec().size() == strlen(about_specifier) || + url.spec()[strlen(about_specifier)] == '/'); +} + +// Transforms a URL of the form "about:foo/XXX" to <url_prefix> + "XXX". +GURL RemapAboutURL(const std::string& url_prefix, const GURL& url) { + std::string path; + size_t split = url.spec().find('/'); + if (split != std::string::npos) + path = url.spec().substr(split + 1); + return GURL(url_prefix + path); +} + } // namespace // ----------------------------------------------------------------------------- @@ -774,23 +791,16 @@ bool WillHandleBrowserAboutURL(GURL* url, Profile* profile) { if (LowerCaseEqualsASCII(url->spec(), chrome::kAboutBlankURL)) return false; - // Handle rewriting view-cache URLs. This allows us to load about:cache. - if (LowerCaseEqualsASCII(url->spec(), chrome::kAboutCacheURL)) { - // Create an mapping from about:cache to the view-cache: internal URL. - *url = GURL(std::string(chrome::kViewCacheScheme) + ":"); + // Rewrite about:cache/* URLs to chrome://net-internals/view-cache/* + if (StartsWithAboutSpecifier(*url, chrome::kAboutCacheURL)) { + *url = RemapAboutURL(chrome::kNetworkViewCacheURL + std::string("/"), + *url); return true; } - // Handle rewriting net-internal URLs. This allows us to load - // about:net-internal. - if (StartsWithASCII(url->spec(), chrome::kAboutNetInternalURL, true)) { - // Create a mapping from about:net-internal to the view-net-internal: - // internal URL. - std::string path; - size_t split = url->spec().find('/'); - if (split != std::string::npos) - path = url->spec().substr(split + 1); - *url = GURL(std::string(chrome::kViewNetInternalScheme) + ":" + path); + // Rewrite about:net-internals/* URLs to chrome://net-internals/* + if (StartsWithAboutSpecifier(*url, chrome::kAboutNetInternalsURL)) { + *url = RemapAboutURL(chrome::kNetworkViewInternalsURL, *url); return true; } diff --git a/chrome/browser/child_process_security_policy_unittest.cc b/chrome/browser/child_process_security_policy_unittest.cc index 1cdccc9..87fd0dc 100644 --- a/chrome/browser/child_process_security_policy_unittest.cc +++ b/chrome/browser/child_process_security_policy_unittest.cc @@ -72,8 +72,6 @@ TEST_F(ChildProcessSecurityPolicyTest, StandardSchemesTest) { EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd"))); EXPECT_FALSE(p->CanRequestURL(kRendererID, - GURL("view-cache:http://www.google.com/"))); - EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("chrome://foo/bar"))); p->Remove(kRendererID); diff --git a/chrome/browser/dom_ui/chrome_url_data_manager.cc b/chrome/browser/dom_ui/chrome_url_data_manager.cc index c3ef325..fccb406 100644 --- a/chrome/browser/dom_ui/chrome_url_data_manager.cc +++ b/chrome/browser/dom_ui/chrome_url_data_manager.cc @@ -22,6 +22,7 @@ #include "net/url_request/url_request.h" #include "net/url_request/url_request_file_job.h" #include "net/url_request/url_request_job.h" +#include "net/url_request/url_request_view_net_internals_job.h" #include "grit/locale_settings.h" @@ -296,6 +297,34 @@ void ChromeURLDataManager::DataSource::SetFontAndTextDirection( L"rtl" : L"ltr"); } +// This class describes how to form chrome://net-internals/DESCRIPTION +// URLs, and conversely how to extract DESCRIPTION. +// +// This needs to be passed to URLRequestViewNetInternalsJob, which lives +// in the network module and doesn't know anything about what URL protocol +// it has been registered with. +class NetInternalsURLFormat : public URLRequestViewNetInternalsJob::URLFormat { + public: + virtual std::string GetDetails(const GURL& url) { + DCHECK(IsSupportedURL(url)); + size_t start = strlen(chrome::kNetworkViewInternalsURL); + if (start >= url.spec().size()) + return std::string(); + return url.spec().substr(start); + } + + virtual GURL MakeURL(const std::string& details) { + return GURL(std::string(chrome::kNetworkViewInternalsURL) + details); + } + + static bool IsSupportedURL(const GURL& url) { + // Note that kNetworkViewInternalsURL is terminated by a '/'. + return StartsWithASCII(url.spec(), + chrome::kNetworkViewInternalsURL, + true /*case_sensitive*/); + } +}; + URLRequestJob* ChromeURLDataManager::Factory(URLRequest* request, const std::string& scheme) { // Try first with a file handler @@ -304,6 +333,12 @@ URLRequestJob* ChromeURLDataManager::Factory(URLRequest* request, return new URLRequestChromeFileJob(request, FilePath::FromWStringHack(path)); + // Next check for chrome://net-internals/, which uses its own job type. + if (NetInternalsURLFormat::IsSupportedURL(request->url())) { + static NetInternalsURLFormat url_format; + return new URLRequestViewNetInternalsJob(request, &url_format); + } + // Fall back to using a custom handler return new URLRequestChromeJob(request); } diff --git a/chrome/common/url_constants.cc b/chrome/common/url_constants.cc index ed128cd..aad1747 100644 --- a/chrome/common/url_constants.cc +++ b/chrome/common/url_constants.cc @@ -20,15 +20,13 @@ const char kJavaScriptScheme[] = "javascript"; const char kMailToScheme[] = "mailto"; const char kPrintScheme[] = "print"; const char kUserScriptScheme[] = "chrome-user-script"; -const char kViewCacheScheme[] = "view-cache"; -const char kViewNetInternalScheme[] = "view-net-internal"; const char kViewSourceScheme[] = "view-source"; const char kStandardSchemeSeparator[] = "://"; const char kAboutBlankURL[] = "about:blank"; const char kAboutCacheURL[] = "about:cache"; -const char kAboutNetInternalURL[] = "about:net-internal"; +const char kAboutNetInternalsURL[] = "about:net-internals"; const char kAboutCrashURL[] = "about:crash"; const char kAboutHangURL[] = "about:hang"; const char kAboutMemoryURL[] = "about:memory"; @@ -63,4 +61,7 @@ const char kSyncMergeAndSyncPath[] = "mergeandsync"; const char kSyncThrobberPath[] = "throbber.png"; const char kSyncSetupFlowPath[] = "setup"; +const char kNetworkViewInternalsURL[] = "chrome://net-internals/"; +const char kNetworkViewCacheURL[] = "chrome://net-internals/view-cache"; + } // namespace chrome diff --git a/chrome/common/url_constants.h b/chrome/common/url_constants.h index c647339..8526642 100644 --- a/chrome/common/url_constants.h +++ b/chrome/common/url_constants.h @@ -24,8 +24,6 @@ extern const char kJavaScriptScheme[]; extern const char kMailToScheme[]; extern const char kPrintScheme[]; extern const char kUserScriptScheme[]; -extern const char kViewCacheScheme[]; -extern const char kViewNetInternalScheme[]; extern const char kViewSourceScheme[]; // Used to separate a standard scheme and the hostname: "://". @@ -35,7 +33,7 @@ extern const char kStandardSchemeSeparator[]; extern const char kAboutBlankURL[]; extern const char kAboutBrowserCrash[]; extern const char kAboutCacheURL[]; -extern const char kAboutNetInternalURL[]; +extern const char kAboutNetInternalsURL[]; extern const char kAboutCrashURL[]; extern const char kAboutHangURL[]; extern const char kAboutMemoryURL[]; @@ -71,6 +69,10 @@ extern const char kSyncMergeAndSyncPath[]; extern const char kSyncThrobberPath[]; extern const char kSyncSetupFlowPath[]; +// Network related URLs. +extern const char kNetworkViewCacheURL[]; +extern const char kNetworkViewInternalsURL[]; + } // namespace chrome #endif // CHROME_COMMON_URL_CONSTANTS_H_ diff --git a/net/base/net_util.h b/net/base/net_util.h index 60e5636..89386bf8 100644 --- a/net/base/net_util.h +++ b/net/base/net_util.h @@ -178,7 +178,7 @@ std::wstring StripWWW(const std::wstring& text); // Gets the filename from the raw Content-Disposition header (as read from the // network). Otherwise uses the last path component name or hostname from // |url|. Note: it's possible for the suggested filename to be empty (e.g., -// file:/// or view-cache:). referrer_charset is used as one of charsets +// file:///). referrer_charset is used as one of charsets // to interpret a raw 8bit string in C-D header (after interpreting // as UTF-8 fails). See the comment for GetFilenameFromCD for more details. std::wstring GetSuggestedFilename(const GURL& url, diff --git a/net/base/net_util_unittest.cc b/net/base/net_util_unittest.cc index fed9c0d6..2047b14 100644 --- a/net/base/net_util_unittest.cc +++ b/net/base/net_util_unittest.cc @@ -802,7 +802,7 @@ TEST(NetUtilTest, GetSuggestedFilename) { "", L"", L"download"}, - {"view-cache:", + {"non-standard-scheme:", "", "", L"", diff --git a/net/net.gyp b/net/net.gyp index 34281d1..5fed81d 100644 --- a/net/net.gyp +++ b/net/net.gyp @@ -361,10 +361,10 @@ 'url_request/url_request_status.h', 'url_request/url_request_test_job.cc', 'url_request/url_request_test_job.h', - 'url_request/url_request_view_cache_job.cc', - 'url_request/url_request_view_cache_job.h', - 'url_request/url_request_view_net_internal_job.cc', - 'url_request/url_request_view_net_internal_job.h', + 'url_request/url_request_view_net_internals_job.cc', + 'url_request/url_request_view_net_internals_job.h', + 'url_request/view_cache_helper.cc', + 'url_request/view_cache_helper.h', ], 'export_dependent_settings': [ '../base/base.gyp:base', diff --git a/net/url_request/url_request_job_manager.cc b/net/url_request/url_request_job_manager.cc index 241edec..617a938 100644 --- a/net/url_request/url_request_job_manager.cc +++ b/net/url_request/url_request_job_manager.cc @@ -19,8 +19,6 @@ #include "net/url_request/url_request_new_ftp_job.h" #endif #include "net/url_request/url_request_http_job.h" -#include "net/url_request/url_request_view_cache_job.h" -#include "net/url_request/url_request_view_net_internal_job.h" // The built-in set of protocol factories namespace { @@ -42,8 +40,6 @@ static const SchemeToFactory kBuiltinFactories[] = { { "ftp", URLRequestNewFtpJob::Factory }, #endif { "about", URLRequestAboutJob::Factory }, - { "view-cache", URLRequestViewCacheJob::Factory }, - { "view-net-internal", URLRequestViewNetInternalJob::Factory }, }; URLRequestJobManager::URLRequestJobManager() { diff --git a/net/url_request/url_request_view_cache_job.h b/net/url_request/url_request_view_cache_job.h deleted file mode 100644 index e85d273..0000000 --- a/net/url_request/url_request_view_cache_job.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2006-2008 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. - -#ifndef NET_URL_REQUEST_URL_REQUEST_VIEW_CACHE_JOB_H_ -#define NET_URL_REQUEST_URL_REQUEST_VIEW_CACHE_JOB_H_ - -#include "net/url_request/url_request.h" -#include "net/url_request/url_request_simple_job.h" - -namespace disk_cache { -class Backend; -} - -// A job subclass that implements the view-cache: protocol, which simply -// provides a debug view of the cache or of a particular cache entry. -class URLRequestViewCacheJob : public URLRequestSimpleJob { - public: - URLRequestViewCacheJob(URLRequest* request) : URLRequestSimpleJob(request) {} - - static URLRequest::ProtocolFactory Factory; - - // override from URLRequestSimpleJob - virtual bool GetData(std::string* mime_type, - std::string* charset, - std::string* data) const; - - private: - disk_cache::Backend* GetDiskCache() const; -}; - -#endif // NET_URL_REQUEST_URL_REQUEST_VIEW_CACHE_JOB_H_ diff --git a/net/url_request/url_request_view_net_internal_job.h b/net/url_request/url_request_view_net_internal_job.h deleted file mode 100644 index 4044041..0000000 --- a/net/url_request/url_request_view_net_internal_job.h +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2009 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. - -#ifndef NET_URL_REQUEST_URL_REQUEST_VIEW_NET_INTERNAL_JOB_H_ -#define NET_URL_REQUEST_URL_REQUEST_VIEW_NET_INTERNAL_JOB_H_ - -#include "net/url_request/url_request.h" -#include "net/url_request/url_request_simple_job.h" - -// A job subclass that implements the view-net-internal: protocol, which simply -// provides a debug view of the various network components. -class URLRequestViewNetInternalJob : public URLRequestSimpleJob { - public: - URLRequestViewNetInternalJob(URLRequest* request) - : URLRequestSimpleJob(request) {} - - static URLRequest::ProtocolFactory Factory; - - // override from URLRequestSimpleJob - virtual bool GetData(std::string* mime_type, - std::string* charset, - std::string* data) const; -}; - -#endif // NET_URL_REQUEST_URL_REQUEST_VIEW_NET_INTERNAL_JOB_H_ diff --git a/net/url_request/url_request_view_net_internal_job.cc b/net/url_request/url_request_view_net_internals_job.cc index 1095ae4..eac53bc 100644 --- a/net/url_request/url_request_view_net_internal_job.cc +++ b/net/url_request/url_request_view_net_internals_job.cc @@ -4,7 +4,7 @@ #include <sstream> -#include "net/url_request/url_request_view_net_internal_job.h" +#include "net/url_request/url_request_view_net_internals_job.h" #include "base/stl_util-inl.h" #include "base/string_util.h" @@ -16,9 +16,12 @@ #include "net/proxy/proxy_service.h" #include "net/url_request/url_request.h" #include "net/url_request/url_request_context.h" +#include "net/url_request/view_cache_helper.h" namespace { +const char kViewHttpCacheSubPath[] = "view-cache"; + //------------------------------------------------------------------------------ // Format helpers. //------------------------------------------------------------------------------ @@ -51,12 +54,13 @@ class SubSection { virtual void OutputBody(URLRequestContext* context, std::string* out) {} // Outputs this subsection, and all of its children. - void OutputRecursive(URLRequestContext* context, std::string* out) { + void OutputRecursive(URLRequestContext* context, + URLRequestViewNetInternalsJob::URLFormat* url_format, + std::string* out) { if (!is_root()) { - std::string section_url = - std::string("view-net-internal:") + GetFullyQualifiedName(); // Canonicalizing the URL escapes characters which cause problems in HTML. - section_url = GURL(section_url).spec(); + std::string section_url = + url_format->MakeURL(GetFullyQualifiedName()).spec(); // Print the heading. out->append(StringPrintf("<div>" @@ -73,7 +77,7 @@ class SubSection { OutputBody(context, out); for (size_t i = 0; i < children_.size(); ++i) - children_[i]->OutputRecursive(context, out); + children_[i]->OutputRecursive(context, url_format, out); if (!is_root()) out->append("</div>"); @@ -339,35 +343,80 @@ class URLRequestSubSection : public SubSection { } }; +class HttpCacheStatsSubSection : public SubSection { + public: + HttpCacheStatsSubSection(SubSection* parent) + : SubSection(parent, "stats", "Statistics") { + } + + virtual void OutputBody(URLRequestContext* context, std::string* out) { + ViewCacheHelper::GetStatisticsHTML(context, out); + } +}; + +class HttpCacheSection : public SubSection { + public: + HttpCacheSection(SubSection* parent) + : SubSection(parent, "httpcache", "HttpCache") { + AddSubSection(new HttpCacheStatsSubSection(this)); + } + + virtual void OutputBody(URLRequestContext* context, std::string* out) { + // Advertise the view-cache URL (too much data to inline it). + out->append("<p><a href='/"); + out->append(kViewHttpCacheSubPath); + out->append("'>View all cache entries</a></p>"); + } +}; + class AllSubSections : public SubSection { public: AllSubSections() : SubSection(NULL, "", "") { AddSubSection(new ProxyServiceSubSection(this)); AddSubSection(new HostResolverSubSection(this)); AddSubSection(new URLRequestSubSection(this)); + AddSubSection(new HttpCacheSection(this)); } }; -} // namespace +// Returns true if |path| is a subpath for "view-cache". +// If it is, then |key| is assigned the subpath. +bool GetViewCacheKeyFromPath(const std::string path, + std::string* key) { + if (!StartsWithASCII(path, kViewHttpCacheSubPath, true)) + return false; + + if (path.size() > strlen(kViewHttpCacheSubPath) && + path[strlen(kViewHttpCacheSubPath)] != '/') + return false; -// static -URLRequestJob* URLRequestViewNetInternalJob::Factory( - URLRequest* request, const std::string& scheme) { - return new URLRequestViewNetInternalJob(request); + if (path.size() > strlen(kViewHttpCacheSubPath) + 1) + *key = path.substr(strlen(kViewHttpCacheSubPath) + 1); + + return true; } -bool URLRequestViewNetInternalJob::GetData(std::string* mime_type, - std::string* charset, - std::string* data) const { - DCHECK_EQ(std::string("view-net-internal"), request_->url().scheme()); +} // namespace +bool URLRequestViewNetInternalsJob::GetData(std::string* mime_type, + std::string* charset, + std::string* data) const { mime_type->assign("text/html"); charset->assign("UTF-8"); URLRequestContext* context = request_->context(); - std::string path = request_->url().path(); + std::string details = url_format_->GetDetails(request_->url()); data->clear(); + + // Use a different handler for "view-cache/*" subpaths. + std::string cache_key; + if (GetViewCacheKeyFromPath(details, &cache_key)) { + GURL url = url_format_->MakeURL(kViewHttpCacheSubPath + std::string("/")); + ViewCacheHelper::GetEntryInfoHTML(cache_key, context, url.spec(), data); + return true; + } + data->append("<html><head><title>Network internals</title>" "<style>" "body { font-family: sans-serif; }\n" @@ -376,22 +425,22 @@ bool URLRequestViewNetInternalJob::GetData(std::string* mime_type, ".subsection_name { font-size: 80%; }\n" "</style>" "</head><body>" - "<p><a href='http://sites.google.com/a/chromium.org/dev/" - "developers/design-documents/view-net-internal'>" + "<p><a href='http://dev.chromium.org/" + "developers/design-documents/view-net-internals'>" "Help: how do I use this?</a></p>"); SubSection* all = Singleton<AllSubSections>::get(); SubSection* section = all; // Display only the subsection tree asked for. - if (!path.empty()) - section = all->FindSubSectionByName(path); + if (!details.empty()) + section = all->FindSubSectionByName(details); if (section) { - section->OutputRecursive(context, data); + section->OutputRecursive(context, url_format_, data); } else { data->append("<i>Nothing found for \""); - data->append(EscapeForHTML(path)); + data->append(EscapeForHTML(details)); data->append("\"</i>"); } diff --git a/net/url_request/url_request_view_net_internals_job.h b/net/url_request/url_request_view_net_internals_job.h new file mode 100644 index 0000000..967d20c --- /dev/null +++ b/net/url_request/url_request_view_net_internals_job.h @@ -0,0 +1,42 @@ +// Copyright (c) 2009 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. + +#ifndef NET_URL_REQUEST_URL_REQUEST_VIEW_NET_INTERNALS_JOB_H_ +#define NET_URL_REQUEST_URL_REQUEST_VIEW_NET_INTERNALS_JOB_H_ + +#include "net/url_request/url_request.h" +#include "net/url_request/url_request_simple_job.h" + +// A job subclass that implements a protocol to inspect the internal +// state of the network stack. The exact format of the URLs is left up to +// the caller, and is described by a URLFormat instance passed into +// the constructor. +class URLRequestViewNetInternalsJob : public URLRequestSimpleJob { + public: + class URLFormat; + + // |url_format| must remain valid for the duration |this|'s lifespan. + URLRequestViewNetInternalsJob(URLRequest* request, + URLFormat* url_format) + : URLRequestSimpleJob(request), url_format_(url_format) {} + + // override from URLRequestSimpleJob + virtual bool GetData(std::string* mime_type, + std::string* charset, + std::string* data) const; + + private: + URLFormat* url_format_; +}; + +// Describes how to pack/unpack the filter string (details) +// from a URL. +class URLRequestViewNetInternalsJob::URLFormat { + public: + virtual ~URLFormat() {} + virtual std::string GetDetails(const GURL& url) = 0; + virtual GURL MakeURL(const std::string& details) = 0; +}; + +#endif // NET_URL_REQUEST_URL_REQUEST_VIEW_NET_INTERNALS_JOB_H_ diff --git a/net/url_request/url_request_view_cache_job.cc b/net/url_request/view_cache_helper.cc index 112f409..1e66d21 100644 --- a/net/url_request/url_request_view_cache_job.cc +++ b/net/url_request/view_cache_helper.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "net/url_request/url_request_view_cache_job.h" +#include "net/url_request/view_cache_helper.h" #include "base/string_util.h" #include "net/base/escape.h" @@ -10,6 +10,7 @@ #include "net/disk_cache/disk_cache.h" #include "net/http/http_cache.h" #include "net/http/http_response_headers.h" +#include "net/http/http_response_info.h" #include "net/url_request/url_request_context.h" #define VIEW_CACHE_HEAD \ @@ -55,10 +56,13 @@ static void HexDump(const char *buf, size_t buf_len, std::string* result) { } } -static std::string FormatEntryInfo(disk_cache::Entry* entry) { - std::string key = EscapeForHTML(entry->GetKey()); +static std::string FormatEntryInfo(disk_cache::Entry* entry, + const std::string& url_prefix) { + std::string key = entry->GetKey(); + GURL url = GURL(url_prefix + key); std::string row = - "<tr><td><a href=\"view-cache:" + key + "\">" + key + "</a></td></tr>"; + "<tr><td><a href=\"" + url.spec() + "\">" + EscapeForHTML(key) + + "</a></td></tr>"; return row; } @@ -101,6 +105,20 @@ static std::string FormatEntryDetails(disk_cache::Entry* entry) { return result; } +static disk_cache::Backend* GetDiskCache(URLRequestContext* context) { + if (!context) + return NULL; + + if (!context->http_transaction_factory()) + return NULL; + + net::HttpCache* http_cache = context->http_transaction_factory()->GetCache(); + if (!http_cache) + return NULL; + + return http_cache->disk_cache(); +} + static std::string FormatStatistics(disk_cache::Backend* disk_cache) { std::vector<std::pair<std::string, std::string> > stats; disk_cache->GetStats(&stats); @@ -117,57 +135,43 @@ static std::string FormatStatistics(disk_cache::Backend* disk_cache) { } // static -URLRequestJob* URLRequestViewCacheJob::Factory(URLRequest* request, - const std::string& scheme) { - return new URLRequestViewCacheJob(request); -} - -bool URLRequestViewCacheJob::GetData(std::string* mime_type, - std::string* charset, - std::string* data) const { - mime_type->assign("text/html"); - charset->assign("UTF-8"); - - disk_cache::Backend* disk_cache = GetDiskCache(); +void ViewCacheHelper::GetEntryInfoHTML(const std::string& key, + URLRequestContext* context, + const std::string& url_prefix, + std::string* data) { + disk_cache::Backend* disk_cache = GetDiskCache(context); if (!disk_cache) { data->assign("no disk cache"); - return true; + return; } - if (request_->url().spec() == "view-cache:") { + if (key.empty()) { data->assign(VIEW_CACHE_HEAD); void* iter = NULL; disk_cache::Entry* entry; while (disk_cache->OpenNextEntry(&iter, &entry)) { - data->append(FormatEntryInfo(entry)); + data->append(FormatEntryInfo(entry, url_prefix)); entry->Close(); } data->append(VIEW_CACHE_TAIL); - } else if (request_->url().spec() == "view-cache:stats") { - data->assign(FormatStatistics(disk_cache)); } else { disk_cache::Entry* entry; - if (disk_cache->OpenEntry(request_->url().path(), &entry)) { + if (disk_cache->OpenEntry(key, &entry)) { data->assign(FormatEntryDetails(entry)); entry->Close(); } else { - data->assign("no matching cache entry"); + data->assign("no matching cache entry for: " + key); } } - return true; } -disk_cache::Backend* URLRequestViewCacheJob::GetDiskCache() const { - if (!request_->context()) - return NULL; - - if (!request_->context()->http_transaction_factory()) - return NULL; - - net::HttpCache* http_cache = - request_->context()->http_transaction_factory()->GetCache(); - if (!http_cache) - return NULL; - - return http_cache->disk_cache(); +// static +void ViewCacheHelper::GetStatisticsHTML(URLRequestContext* context, + std::string* data) { + disk_cache::Backend* disk_cache = GetDiskCache(context); + if (!disk_cache) { + data->append("no disk cache"); + return; + } + data->append(FormatStatistics(disk_cache)); } diff --git a/net/url_request/view_cache_helper.h b/net/url_request/view_cache_helper.h new file mode 100644 index 0000000..2648699 --- /dev/null +++ b/net/url_request/view_cache_helper.h @@ -0,0 +1,24 @@ +// Copyright (c) 2006-2008 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. + +#ifndef NET_URL_REQUEST_VIEW_CACHE_HELPER_H_ +#define NET_URL_REQUEST_VIEW_CACHE_HELPER_H_ + +#include <string> + +class URLRequestContext; + +class ViewCacheHelper { + public: + // Formats the cache information for |key| as HTML. + static void GetEntryInfoHTML(const std::string& key, + URLRequestContext* context, + const std::string& url_prefix, + std::string* out); + + static void GetStatisticsHTML(URLRequestContext* context, + std::string* out); +}; + +#endif // NET_URL_REQUEST_VIEW_CACHE_HELPER_H_ |