OLD | NEW |
---|---|
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_chromeos.h" | 5 #include "chrome/browser/icon_loader.h" |
6 | 6 |
7 #include <string> | |
7 #include <map> | 8 #include <map> |
8 #include <utility> | 9 #include <utility> |
9 #include <vector> | |
10 | 10 |
11 #include "base/file_util.h" | 11 #include "base/basictypes.h" |
12 #include "base/memory/singleton.h" | 12 #include "base/bind.h" |
13 #include "base/string_split.h" | 13 #include "base/message_loop.h" |
14 #include "base/string_util.h" | |
15 #include "base/threading/thread_restrictions.h" | |
16 #include "chrome/browser/icon_loader.h" | 14 #include "chrome/browser/icon_loader.h" |
17 #include "chrome/browser/io_thread.h" | |
18 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | |
19 #include "chrome/common/url_constants.h" | |
20 #include "googleurl/src/gurl.h" | |
21 #include "grit/component_extension_resources.h" | 15 #include "grit/component_extension_resources.h" |
22 #include "grit/component_extension_resources_map.h" | 16 #include "skia/ext/image_operations.h" |
23 #include "grit/generated_resources.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
24 #include "grit/theme_resources.h" | |
25 #include "grit/ui_resources.h" | |
26 #include "net/base/escape.h" | |
27 #include "net/base/mime_util.h" | |
28 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
19 #include "ui/gfx/canvas_skia.h" | |
20 #include "ui/gfx/codec/png_codec.h" | |
21 #include "ui/gfx/image/image.h" | |
22 #include "webkit/glue/image_decoder.h" | |
29 | 23 |
30 namespace { | 24 namespace { |
31 | 25 |
32 struct IdrBySize { | 26 struct IdrBySize { |
33 int idr_small_; | 27 int idr_small_; |
34 int idr_normal_; | 28 int idr_normal_; |
35 int idr_large_; | 29 int idr_large_; |
36 }; | 30 }; |
37 | 31 |
38 typedef std::map<std::string, IconLoader::IconSize> QueryIconSizeMap; | |
39 typedef std::map<std::string, IdrBySize> ExtensionIconSizeMap; | 32 typedef std::map<std::string, IdrBySize> ExtensionIconSizeMap; |
40 | 33 |
41 const char *kIconSize = "iconsize"; | |
42 | |
43 const IdrBySize kAudioIdrs = { | 34 const IdrBySize kAudioIdrs = { |
44 IDR_FILE_MANAGER_IMG_FILETYPE_AUDIO, | 35 IDR_FILE_MANAGER_IMG_FILETYPE_AUDIO, |
45 IDR_FILE_MANAGER_IMG_FILETYPE_LARGE_AUDIO, | 36 IDR_FILE_MANAGER_IMG_FILETYPE_LARGE_AUDIO, |
46 IDR_FILE_MANAGER_IMG_FILETYPE_LARGE_AUDIO | 37 IDR_FILE_MANAGER_IMG_FILETYPE_LARGE_AUDIO |
47 }; | 38 }; |
48 const IdrBySize kDeviceIdrs = { | 39 const IdrBySize kDeviceIdrs = { |
49 IDR_FILE_MANAGER_IMG_FILETYPE_DEVICE, | 40 IDR_FILE_MANAGER_IMG_FILETYPE_DEVICE, |
50 IDR_FILE_MANAGER_IMG_FILETYPE_DEVICE, | 41 IDR_FILE_MANAGER_IMG_FILETYPE_DEVICE, |
51 IDR_FILE_MANAGER_IMG_FILETYPE_DEVICE | 42 IDR_FILE_MANAGER_IMG_FILETYPE_DEVICE |
52 }; | 43 }; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 IDR_FILE_MANAGER_IMG_FILETYPE_TEXT, | 90 IDR_FILE_MANAGER_IMG_FILETYPE_TEXT, |
100 IDR_FILE_MANAGER_IMG_FILETYPE_TEXT, | 91 IDR_FILE_MANAGER_IMG_FILETYPE_TEXT, |
101 IDR_FILE_MANAGER_IMG_FILETYPE_TEXT | 92 IDR_FILE_MANAGER_IMG_FILETYPE_TEXT |
102 }; | 93 }; |
103 const IdrBySize kVideoIdrs = { | 94 const IdrBySize kVideoIdrs = { |
104 IDR_FILE_MANAGER_IMG_FILETYPE_VIDEO, | 95 IDR_FILE_MANAGER_IMG_FILETYPE_VIDEO, |
105 IDR_FILE_MANAGER_IMG_FILETYPE_LARGE_VIDEO, | 96 IDR_FILE_MANAGER_IMG_FILETYPE_LARGE_VIDEO, |
106 IDR_FILE_MANAGER_IMG_FILETYPE_LARGE_VIDEO | 97 IDR_FILE_MANAGER_IMG_FILETYPE_LARGE_VIDEO |
107 }; | 98 }; |
108 | 99 |
109 QueryIconSizeMap BuildQueryIconSizeMap() { | |
110 QueryIconSizeMap::value_type kQueryIconSizeData[] = { | |
111 std::make_pair("small", IconLoader::SMALL), | |
112 std::make_pair("normal", IconLoader::NORMAL), | |
113 std::make_pair("large", IconLoader::LARGE) | |
114 }; | |
115 | |
116 size_t kQSize = arraysize(kQueryIconSizeData); | |
117 return QueryIconSizeMap(&kQueryIconSizeData[0], | |
118 &kQueryIconSizeData[kQSize]); | |
119 } | |
120 | |
121 // The code below should match translation in | 100 // The code below should match translation in |
122 // chrome/browser/resources/file_manager/js/file_manager.js | 101 // chrome/browser/resources/file_manager/js/file_manager.js |
123 // chrome/browser/resources/file_manager/css/file_manager.css | 102 // chrome/browser/resources/file_manager/css/file_manager.css |
124 // 'audio': /\.(mp3|m4a|oga|ogg|wav)$/i, | 103 // 'audio': /\.(mp3|m4a|oga|ogg|wav)$/i, |
125 // 'html': /\.(html?)$/i, | 104 // 'html': /\.(html?)$/i, |
126 // 'image': /\.(bmp|gif|jpe?g|ico|png|webp)$/i, | 105 // 'image': /\.(bmp|gif|jpe?g|ico|png|webp)$/i, |
127 // 'pdf' : /\.(pdf)$/i, | 106 // 'pdf' : /\.(pdf)$/i, |
128 // 'text': /\.(pod|rst|txt|log)$/i, | 107 // 'text': /\.(pod|rst|txt|log)$/i, |
129 // 'video': /\.(mov|mp4|m4v|mpe?g4?|ogm|ogv|ogx|webm)$/i | 108 // 'video': /\.(mov|mp4|m4v|mpe?g4?|ogm|ogv|ogx|webm)$/i |
130 | 109 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 std::make_pair(".ogv", kVideoIdrs), | 144 std::make_pair(".ogv", kVideoIdrs), |
166 std::make_pair(".ogx", kVideoIdrs), | 145 std::make_pair(".ogx", kVideoIdrs), |
167 std::make_pair(".webm", kVideoIdrs), | 146 std::make_pair(".webm", kVideoIdrs), |
168 }; | 147 }; |
169 | 148 |
170 const size_t kESize = arraysize(kExtensionIdrBySizeData); | 149 const size_t kESize = arraysize(kExtensionIdrBySizeData); |
171 return ExtensionIconSizeMap(&kExtensionIdrBySizeData[0], | 150 return ExtensionIconSizeMap(&kExtensionIdrBySizeData[0], |
172 &kExtensionIdrBySizeData[kESize]); | 151 &kExtensionIdrBySizeData[kESize]); |
173 } | 152 } |
174 | 153 |
175 // Split on the very first &. The first part is path, the rest query. | |
176 void GetExtensionAndQuery(const std::string& url, | |
177 std::string* extension, | |
178 std::string* query) { | |
179 // We receive the url with chrome://fileicon/ stripped but GURL expects it. | |
180 const GURL gurl("chrome://fileicon/" + net::EscapePath(url)); | |
181 const std::string path = gurl.path(); | |
182 *extension = StringToLowerASCII(FilePath(path).Extension()); | |
183 *query = gurl.query(); | |
184 } | |
185 | |
186 // Simple parser for data on the query. | |
187 IconLoader::IconSize QueryToIconSize(const std::string& query) { | |
188 CR_DEFINE_STATIC_LOCAL( | |
189 QueryIconSizeMap, kQueryIconSizeMap, (BuildQueryIconSizeMap())); | |
190 typedef std::pair<std::string, std::string> KVPair; | |
191 std::vector<KVPair> parameters; | |
192 if (base::SplitStringIntoKeyValuePairs(query, '=', '&', ¶meters)) { | |
193 for (std::vector<KVPair>::const_iterator itk = parameters.begin(); | |
194 itk != parameters.end(); ++itk) { | |
195 if (itk->first == kIconSize) { | |
196 QueryIconSizeMap::const_iterator itq( | |
197 kQueryIconSizeMap.find(itk->second)); | |
198 if (itq != kQueryIconSizeMap.end()) | |
199 return itq->second; | |
200 } | |
201 } | |
202 } | |
203 return IconLoader::NORMAL; | |
204 } | |
205 | |
206 // Finds matching resource of proper size. Fallback to generic. | 154 // Finds matching resource of proper size. Fallback to generic. |
207 int UrlToIDR(const std::string& url) { | 155 int FileTypeToIDR(const std::string& extension, IconLoader::IconSize size) { |
208 CR_DEFINE_STATIC_LOCAL( | 156 CR_DEFINE_STATIC_LOCAL( |
209 ExtensionIconSizeMap, kExtensionIdrSizeMap, (BuildExtensionIdrSizeMap())); | 157 ExtensionIconSizeMap, kExtensionIdrSizeMap, (BuildExtensionIdrSizeMap())); |
210 std::string extension, query; | |
211 int idr = -1; | 158 int idr = -1; |
212 GetExtensionAndQuery(url, &extension, &query); | |
213 const IconLoader::IconSize size = QueryToIconSize(query); | |
214 ExtensionIconSizeMap::const_iterator it = | 159 ExtensionIconSizeMap::const_iterator it = |
215 kExtensionIdrSizeMap.find(extension); | 160 kExtensionIdrSizeMap.find(extension); |
216 if (it != kExtensionIdrSizeMap.end()) { | 161 if (it != kExtensionIdrSizeMap.end()) { |
217 IdrBySize idrbysize = it->second; | 162 IdrBySize idrbysize = it->second; |
218 if (size == IconLoader::SMALL) { | 163 if (size == IconLoader::SMALL) { |
219 idr = idrbysize.idr_small_; | 164 idr = idrbysize.idr_small_; |
220 } else if (size == IconLoader::NORMAL) { | 165 } else if (size == IconLoader::NORMAL) { |
221 idr = idrbysize.idr_normal_; | 166 idr = idrbysize.idr_normal_; |
222 } else if (size == IconLoader::LARGE) { | 167 } else if (size == IconLoader::LARGE) { |
223 idr = idrbysize.idr_large_; | 168 idr = idrbysize.idr_large_; |
224 } | 169 } |
225 } | 170 } |
226 if (idr == -1) { | 171 if (idr == -1) { |
227 if (size == IconLoader::SMALL) { | 172 if (size == IconLoader::SMALL) { |
228 idr = kGenericIdrs.idr_small_; | 173 idr = kGenericIdrs.idr_small_; |
229 } else if (size == IconLoader::NORMAL) { | 174 } else if (size == IconLoader::NORMAL) { |
230 idr = kGenericIdrs.idr_normal_; | 175 idr = kGenericIdrs.idr_normal_; |
231 } else { | 176 } else { |
232 idr = kGenericIdrs.idr_large_; | 177 idr = kGenericIdrs.idr_large_; |
233 } | 178 } |
234 } | 179 } |
235 return idr; | 180 return idr; |
236 } | 181 } |
182 | |
183 SkBitmap* GenerateBitmapWithSize(SkBitmap* source, int pixel_size) { | |
184 DCHECK(source); | |
185 DCHECK(source->width() == source->height()); | |
186 | |
187 if (pixel_size == -1 || source->width() == pixel_size) | |
188 return new SkBitmap(*source); | |
189 | |
190 return new SkBitmap(skia::ImageOperations::Resize( | |
191 *source, skia::ImageOperations::RESIZE_BEST, pixel_size, pixel_size)); | |
192 } | |
193 | |
194 int IconSizeToPixelSize(IconLoader::IconSize size) { | |
195 switch (size) { | |
196 case IconLoader::SMALL: return 16; | |
197 case IconLoader::NORMAL: return 32; | |
198 case IconLoader::LARGE: // fallthrough | |
ilja
2012/01/22 02:31:43
What happens when LARGE is requested? CrOS file ma
asanka
2012/01/23 19:16:47
The pixel size determined here is used to decide w
| |
199 case IconLoader::ALL: // fallthrough | |
200 default: | |
201 return -1; | |
202 } | |
203 } | |
204 | |
237 } // namespace | 205 } // namespace |
238 | 206 |
239 FileIconSourceCros::FileIconSourceCros() | 207 void IconLoader::ReadIcon() { |
240 : DataSource("fileicon", NULL) { | 208 int idr = FileTypeToIDR(group_, icon_size_); |
209 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
210 scoped_refptr<RefCountedStaticMemory> bytes(rb.LoadDataResourceBytes(idr)); | |
211 DCHECK(bytes.get()); | |
212 SkBitmap bitmap; | |
213 if (!gfx::PNGCodec::Decode(bytes->front(), bytes->size(), &bitmap)) { | |
214 NOTREACHED(); | |
215 } | |
216 image_.reset(new gfx::Image( | |
217 GenerateBitmapWithSize(&bitmap, IconSizeToPixelSize(icon_size_)))); | |
218 target_message_loop_->PostTask( | |
219 FROM_HERE, base::Bind(&IconLoader::NotifyDelegate, this)); | |
241 } | 220 } |
242 | |
243 FileIconSourceCros::~FileIconSourceCros() { | |
244 } | |
245 | |
246 void FileIconSourceCros::StartDataRequest(const std::string& url, | |
247 bool is_incognito, | |
248 int request_id) { | |
249 int idr = UrlToIDR(url); | |
250 const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
251 scoped_refptr<RefCountedStaticMemory> bytes(rb.LoadDataResourceBytes(idr)); | |
252 SendResponse(request_id, bytes); | |
253 } | |
254 | |
255 // The mime type refers to the type of the response/icon served. | |
256 std::string FileIconSourceCros::GetMimeType( | |
257 const std::string& url) const { | |
258 return "image/png"; | |
259 } | |
OLD | NEW |