Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1017)

Unified Diff: chrome/browser/ui/webui/fileicon_source.cc

Issue 9167019: Consolidate icon loading on ChromeOS (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/webui/fileicon_source.cc
diff --git a/chrome/browser/ui/webui/fileicon_source.cc b/chrome/browser/ui/webui/fileicon_source.cc
index 30ee3dac6131c4397e61fa0a637d1e8922306cbb..8198b80eb86a63f524f00fbfd27fc47f31fa8a7e 100644
--- a/chrome/browser/ui/webui/fileicon_source.cc
+++ b/chrome/browser/ui/webui/fileicon_source.cc
@@ -4,20 +4,75 @@
#include "chrome/browser/ui/webui/fileicon_source.h"
+#include "base/basictypes.h"
#include "base/callback.h"
#include "base/file_path.h"
#include "base/memory/ref_counted_memory.h"
+#include "base/string_split.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/common/time_format.h"
+#include "googleurl/src/gurl.h"
#include "grit/generated_resources.h"
#include "net/base/escape.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image.h"
+namespace {
+
+typedef std::map<std::string, IconLoader::IconSize> QueryIconSizeMap;
+
// The path used in internal URLs to file icon data.
-static const char kFileIconPath[] = "fileicon";
+const char kFileIconPath[] = "fileicon";
+
+// URL parameter specifying icon size.
+const char kIconSize[] = "iconsize";
+
+void GetPathAndQuery(const std::string& url,
+ std::string* path,
+ std::string* query) {
+ // We receive the url with chrome://fileicon/ stripped but GURL expects it.
+ const GURL gurl("chrome://fileicon/" + url);
+ path->assign(net::UnescapeURLComponent(
+ gurl.path().substr(1), (net::UnescapeRule::URL_SPECIAL_CHARS |
+ net::UnescapeRule::SPACES)));
+ query->assign(gurl.query());
+}
+
+QueryIconSizeMap BuildQueryIconSizeMap() {
+ QueryIconSizeMap::value_type kQueryIconSizeData[] = {
+ std::make_pair("small", IconLoader::SMALL),
+ std::make_pair("normal", IconLoader::NORMAL),
+ std::make_pair("large", IconLoader::LARGE)
+ };
+
+ size_t kQSize = arraysize(kQueryIconSizeData);
+ return QueryIconSizeMap(&kQueryIconSizeData[0],
+ &kQueryIconSizeData[kQSize]);
+}
+
+// Simple parser for data on the query.
+IconLoader::IconSize QueryToIconSize(const std::string& query) {
+ CR_DEFINE_STATIC_LOCAL(
+ QueryIconSizeMap, kQueryIconSizeMap, (BuildQueryIconSizeMap()));
+ typedef std::pair<std::string, std::string> KVPair;
+ std::vector<KVPair> parameters;
+ if (base::SplitStringIntoKeyValuePairs(query, '=', '&', &parameters)) {
+ for (std::vector<KVPair>::const_iterator itk = parameters.begin();
+ itk != parameters.end(); ++itk) {
+ if (itk->first == kIconSize) {
+ QueryIconSizeMap::const_iterator itq(
+ kQueryIconSizeMap.find(itk->second));
+ if (itq != kQueryIconSizeMap.end())
+ return itq->second;
+ }
+ }
+ }
+ return IconLoader::NORMAL;
+}
+
+}
FileIconSource::FileIconSource()
: DataSource(kFileIconPath, MessageLoop::current()) {}
@@ -26,23 +81,11 @@ FileIconSource::~FileIconSource() {
cancelable_consumer_.CancelAllRequests();
}
-void FileIconSource::StartDataRequest(const std::string& path,
- bool is_incognito,
- int request_id) {
- std::string escaped_path = net::UnescapeURLComponent(path,
- net::UnescapeRule::SPACES);
-#if defined(OS_WIN)
- // The path we receive has the wrong slashes and escaping for what we need;
- // this only appears to matter for getting icons from .exe files.
- std::replace(escaped_path.begin(), escaped_path.end(), '/', '\\');
- FilePath escaped_filepath(UTF8ToWide(escaped_path));
-#elif defined(OS_POSIX)
- // The correct encoding on Linux may not actually be UTF8.
- FilePath escaped_filepath(escaped_path);
-#endif
-
+void FileIconSource::FetchFileIcon(const FilePath& path,
+ IconLoader::IconSize icon_size,
+ int request_id) {
IconManager* im = g_browser_process->icon_manager();
- gfx::Image* icon = im->LookupIcon(escaped_filepath, IconLoader::NORMAL);
+ gfx::Image* icon = im->LookupIcon(path, icon_size);
ilja 2012/01/22 02:31:43 Shouldn't that still be named "escaped_filepath"?
asanka 2012/01/23 19:16:47 The FilePath argument here shouldn't contain any e
if (icon) {
scoped_refptr<RefCountedBytes> icon_data(new RefCountedBytes);
@@ -51,9 +94,8 @@ void FileIconSource::StartDataRequest(const std::string& path,
SendResponse(request_id, icon_data);
} else {
// Icon was not in cache, go fetch it slowly.
- IconManager::Handle h = im->LoadIcon(escaped_filepath,
- IconLoader::NORMAL,
- &cancelable_consumer_,
+ IconManager::Handle h = im->LoadIcon(
+ path, icon_size, &cancelable_consumer_,
base::Bind(&FileIconSource::OnFileIconDataAvailable,
base::Unretained(this)));
@@ -62,6 +104,24 @@ void FileIconSource::StartDataRequest(const std::string& path,
}
}
+void FileIconSource::StartDataRequest(const std::string& path,
+ bool is_incognito,
+ int request_id) {
+ std::string query;
+ std::string escaped_path;
+ GetPathAndQuery(path, &escaped_path, &query);
+#if defined(OS_WIN)
+ // The path we receive has the wrong slashes and escaping for what we need;
+ // this only appears to matter for getting icons from .exe files.
+ std::replace(escaped_path.begin(), escaped_path.end(), '/', '\\');
+ FilePath escaped_filepath(UTF8ToWide(escaped_path));
+#elif defined(OS_POSIX)
+ // The correct encoding on Linux may not actually be UTF8.
+ FilePath escaped_filepath(escaped_path);
+#endif
+ FetchFileIcon(escaped_filepath, QueryToIconSize(query), request_id);
+}
+
std::string FileIconSource::GetMimeType(const std::string&) const {
// Rely on image decoder inferring the correct type.
return std::string();

Powered by Google App Engine
This is Rietveld 408576698