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

Side by Side 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: " 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/browser/ui/webui/fileicon_source.h" 5 #include "chrome/browser/ui/webui/fileicon_source.h"
6 6
7 #include "base/basictypes.h"
7 #include "base/callback.h" 8 #include "base/callback.h"
8 #include "base/file_path.h" 9 #include "base/file_path.h"
9 #include "base/memory/ref_counted_memory.h" 10 #include "base/memory/ref_counted_memory.h"
11 #include "base/string_split.h"
10 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/browser_process.h" 13 #include "chrome/browser/browser_process.h"
12 #include "chrome/common/time_format.h" 14 #include "chrome/common/time_format.h"
15 #include "googleurl/src/gurl.h"
13 #include "grit/generated_resources.h" 16 #include "grit/generated_resources.h"
14 #include "net/base/escape.h" 17 #include "net/base/escape.h"
15 #include "third_party/skia/include/core/SkBitmap.h" 18 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "ui/gfx/codec/png_codec.h" 19 #include "ui/gfx/codec/png_codec.h"
17 #include "ui/gfx/image/image.h" 20 #include "ui/gfx/image/image.h"
18 21
22 namespace {
23
24 typedef std::map<std::string, IconLoader::IconSize> QueryIconSizeMap;
25
19 // The path used in internal URLs to file icon data. 26 // The path used in internal URLs to file icon data.
20 static const char kFileIconPath[] = "fileicon"; 27 const char kFileIconPath[] = "fileicon";
28
29 // URL parameter specifying icon size.
30 const char kIconSize[] = "iconsize";
31
32 // Assuming the url is of the form '/path?query', convert the path portion into
33 // a FilePath and return the resulting |file_path| and |query|. The path
34 // portion may have been encoded using encodeURIComponent().
35 void GetFilePathAndQuery(const std::string& url,
achuithb 2012/01/23 20:59:27 I assume GetFilePathAndQuery and QueryToIconSize a
asanka 2012/01/26 00:49:35 Yes.
36 FilePath* file_path,
37 std::string* query) {
38 std::string path;
39 // We receive the url with chrome://fileicon/ stripped but GURL expects it.
40 const GURL gurl("chrome://fileicon/" + url);
41 path.assign(net::UnescapeURLComponent(
42 gurl.path().substr(1), (net::UnescapeRule::URL_SPECIAL_CHARS |
43 net::UnescapeRule::SPACES)));
44 #if defined(OS_WIN)
45 // The path we receive has the wrong slashes and escaping for what we need;
46 // this only appears to matter for getting icons from .exe files.
47 std::replace(path.begin(), path.end(), '/', '\\');
48 *file_path = FilePath(UTF8ToWide(path));
49 #elif defined(OS_POSIX)
50 // The correct encoding on Linux may not actually be UTF8.
51 *file_path = FilePath(path);
52 #endif
53 query->assign(gurl.query());
54 }
55
56 QueryIconSizeMap BuildQueryIconSizeMap() {
57 QueryIconSizeMap::value_type kQueryIconSizeData[] = {
58 std::make_pair("small", IconLoader::SMALL),
59 std::make_pair("normal", IconLoader::NORMAL),
60 std::make_pair("large", IconLoader::LARGE)
61 };
62
63 size_t kQSize = arraysize(kQueryIconSizeData);
64 return QueryIconSizeMap(&kQueryIconSizeData[0],
65 &kQueryIconSizeData[kQSize]);
66 }
67
68 // Simple parser for data on the query.
69 IconLoader::IconSize QueryToIconSize(const std::string& query) {
70 CR_DEFINE_STATIC_LOCAL(
achuithb 2012/01/23 20:59:27 Do you think a comment would be useful here? I had
asanka 2012/01/26 00:49:35 Done. This code actually changed a bit, and I'm no
71 QueryIconSizeMap, kQueryIconSizeMap, (BuildQueryIconSizeMap()));
72 typedef std::pair<std::string, std::string> KVPair;
73 std::vector<KVPair> parameters;
74 if (base::SplitStringIntoKeyValuePairs(query, '=', '&', &parameters)) {
75 for (std::vector<KVPair>::const_iterator itk = parameters.begin();
76 itk != parameters.end(); ++itk) {
77 if (itk->first == kIconSize) {
78 QueryIconSizeMap::const_iterator itq(
79 kQueryIconSizeMap.find(itk->second));
80 if (itq != kQueryIconSizeMap.end())
81 return itq->second;
82 }
83 }
84 }
85 return IconLoader::NORMAL;
86 }
87
88 }
21 89
22 FileIconSource::FileIconSource() 90 FileIconSource::FileIconSource()
23 : DataSource(kFileIconPath, MessageLoop::current()) {} 91 : DataSource(kFileIconPath, MessageLoop::current()) {}
24 92
25 FileIconSource::~FileIconSource() { 93 FileIconSource::~FileIconSource() {
26 cancelable_consumer_.CancelAllRequests(); 94 cancelable_consumer_.CancelAllRequests();
27 } 95 }
28 96
29 void FileIconSource::StartDataRequest(const std::string& path, 97 void FileIconSource::FetchFileIcon(const FilePath& path,
30 bool is_incognito, 98 IconLoader::IconSize icon_size,
31 int request_id) { 99 int request_id) {
32 std::string escaped_path = net::UnescapeURLComponent(path,
33 net::UnescapeRule::SPACES);
34 #if defined(OS_WIN)
35 // The path we receive has the wrong slashes and escaping for what we need;
36 // this only appears to matter for getting icons from .exe files.
37 std::replace(escaped_path.begin(), escaped_path.end(), '/', '\\');
38 FilePath escaped_filepath(UTF8ToWide(escaped_path));
39 #elif defined(OS_POSIX)
40 // The correct encoding on Linux may not actually be UTF8.
41 FilePath escaped_filepath(escaped_path);
42 #endif
43
44 IconManager* im = g_browser_process->icon_manager(); 100 IconManager* im = g_browser_process->icon_manager();
45 gfx::Image* icon = im->LookupIcon(escaped_filepath, IconLoader::NORMAL); 101 gfx::Image* icon = im->LookupIcon(path, icon_size);
46 102
47 if (icon) { 103 if (icon) {
48 scoped_refptr<RefCountedBytes> icon_data(new RefCountedBytes); 104 scoped_refptr<RefCountedBytes> icon_data(new RefCountedBytes);
49 gfx::PNGCodec::EncodeBGRASkBitmap(*icon, false, &icon_data->data()); 105 gfx::PNGCodec::EncodeBGRASkBitmap(*icon, false, &icon_data->data());
50 106
51 SendResponse(request_id, icon_data); 107 SendResponse(request_id, icon_data);
52 } else { 108 } else {
53 // Icon was not in cache, go fetch it slowly. 109 // Icon was not in cache, go fetch it slowly.
54 IconManager::Handle h = im->LoadIcon(escaped_filepath, 110 IconManager::Handle h = im->LoadIcon(
55 IconLoader::NORMAL, 111 path, icon_size, &cancelable_consumer_,
56 &cancelable_consumer_,
57 base::Bind(&FileIconSource::OnFileIconDataAvailable, 112 base::Bind(&FileIconSource::OnFileIconDataAvailable,
58 base::Unretained(this))); 113 base::Unretained(this)));
59 114
60 // Attach the ChromeURLDataManager request ID to the history request. 115 // Attach the ChromeURLDataManager request ID to the history request.
61 cancelable_consumer_.SetClientData(im, h, request_id); 116 cancelable_consumer_.SetClientData(im, h, request_id);
62 } 117 }
63 } 118 }
64 119
120 void FileIconSource::StartDataRequest(const std::string& url_path,
121 bool is_incognito,
122 int request_id) {
123 std::string query;
124 FilePath file_path;
125 GetFilePathAndQuery(url_path, &file_path, &query);
126 FetchFileIcon(file_path, QueryToIconSize(query), request_id);
127 }
128
65 std::string FileIconSource::GetMimeType(const std::string&) const { 129 std::string FileIconSource::GetMimeType(const std::string&) const {
66 // Rely on image decoder inferring the correct type. 130 // Rely on image decoder inferring the correct type.
67 return std::string(); 131 return std::string();
68 } 132 }
69 133
70 void FileIconSource::OnFileIconDataAvailable(IconManager::Handle handle, 134 void FileIconSource::OnFileIconDataAvailable(IconManager::Handle handle,
71 gfx::Image* icon) { 135 gfx::Image* icon) {
72 IconManager* im = g_browser_process->icon_manager(); 136 IconManager* im = g_browser_process->icon_manager();
73 int request_id = cancelable_consumer_.GetClientData(im, handle); 137 int request_id = cancelable_consumer_.GetClientData(im, handle);
74 138
75 if (icon) { 139 if (icon) {
76 scoped_refptr<RefCountedBytes> icon_data(new RefCountedBytes); 140 scoped_refptr<RefCountedBytes> icon_data(new RefCountedBytes);
77 gfx::PNGCodec::EncodeBGRASkBitmap(*icon, false, &icon_data->data()); 141 gfx::PNGCodec::EncodeBGRASkBitmap(*icon, false, &icon_data->data());
78 142
79 SendResponse(request_id, icon_data); 143 SendResponse(request_id, icon_data);
80 } else { 144 } else {
81 // TODO(glen): send a dummy icon. 145 // TODO(glen): send a dummy icon.
82 SendResponse(request_id, NULL); 146 SendResponse(request_id, NULL);
83 } 147 }
84 } 148 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698