| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/extension_icon_source.h" | 5 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 #include "webkit/glue/image_decoder.h" | 30 #include "webkit/glue/image_decoder.h" |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 scoped_refptr<RefCountedMemory> BitmapToMemory(const SkBitmap* image) { | 34 scoped_refptr<RefCountedMemory> BitmapToMemory(const SkBitmap* image) { |
| 35 RefCountedBytes* image_bytes = new RefCountedBytes; | 35 RefCountedBytes* image_bytes = new RefCountedBytes; |
| 36 gfx::PNGCodec::EncodeBGRASkBitmap(*image, false, &image_bytes->data()); | 36 gfx::PNGCodec::EncodeBGRASkBitmap(*image, false, &image_bytes->data()); |
| 37 return image_bytes; | 37 return image_bytes; |
| 38 } | 38 } |
| 39 | 39 |
| 40 void DesaturateImage(SkBitmap* image) { | 40 SkBitmap DesaturateImage(const SkBitmap* image) { |
| 41 color_utils::HSL shift = {-1, 0, 0.6}; | 41 color_utils::HSL shift = {-1, 0, 0.6}; |
| 42 *image = SkBitmapOperations::CreateHSLShiftedBitmap(*image, shift); | 42 return SkBitmapOperations::CreateHSLShiftedBitmap(*image, shift); |
| 43 } | 43 } |
| 44 | 44 |
| 45 SkBitmap* ToBitmap(const unsigned char* data, size_t size) { | 45 SkBitmap* ToBitmap(const unsigned char* data, size_t size) { |
| 46 webkit_glue::ImageDecoder decoder; | 46 webkit_glue::ImageDecoder decoder; |
| 47 SkBitmap* decoded = new SkBitmap(); | 47 SkBitmap* decoded = new SkBitmap(); |
| 48 *decoded = decoder.Decode(data, size); | 48 *decoded = decoder.Decode(data, size); |
| 49 return decoded; | 49 return decoded; |
| 50 } | 50 } |
| 51 | 51 |
| 52 } // namespace | 52 } // namespace |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 | 161 |
| 162 const SkBitmap* ExtensionIconSource::GetDefaultExtensionImage() { | 162 const SkBitmap* ExtensionIconSource::GetDefaultExtensionImage() { |
| 163 if (!default_extension_data_.get()) { | 163 if (!default_extension_data_.get()) { |
| 164 default_extension_data_.reset( | 164 default_extension_data_.reset( |
| 165 LoadImageByResourceId(IDR_EXTENSION_DEFAULT_ICON)); | 165 LoadImageByResourceId(IDR_EXTENSION_DEFAULT_ICON)); |
| 166 } | 166 } |
| 167 | 167 |
| 168 return default_extension_data_.get(); | 168 return default_extension_data_.get(); |
| 169 } | 169 } |
| 170 | 170 |
| 171 void ExtensionIconSource::FinalizeImage(SkBitmap* image, | 171 void ExtensionIconSource::FinalizeImage(const SkBitmap* image, |
| 172 int request_id) { | 172 int request_id) { |
| 173 SkBitmap bitmap; |
| 173 if (GetData(request_id)->grayscale) | 174 if (GetData(request_id)->grayscale) |
| 174 DesaturateImage(image); | 175 bitmap = DesaturateImage(image); |
| 176 else |
| 177 bitmap = *image; |
| 175 | 178 |
| 176 ClearData(request_id); | 179 ClearData(request_id); |
| 177 SendResponse(request_id, BitmapToMemory(image)); | 180 SendResponse(request_id, BitmapToMemory(&bitmap)); |
| 178 } | 181 } |
| 179 | 182 |
| 180 void ExtensionIconSource::LoadDefaultImage(int request_id) { | 183 void ExtensionIconSource::LoadDefaultImage(int request_id) { |
| 181 ExtensionIconRequest* request = GetData(request_id); | 184 ExtensionIconRequest* request = GetData(request_id); |
| 182 const SkBitmap* default_image = NULL; | 185 const SkBitmap* default_image = NULL; |
| 183 | 186 |
| 184 if (request->extension->id() == extension_misc::kWebStoreAppId) | 187 if (request->extension->id() == extension_misc::kWebStoreAppId) |
| 185 default_image = GetWebStoreImage(); | 188 default_image = GetWebStoreImage(); |
| 186 else if (request->extension->is_app()) | 189 else if (request->extension->is_app()) |
| 187 default_image = GetDefaultAppImage(); | 190 default_image = GetDefaultAppImage(); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 // If we don't need a grayscale image, then we can bypass FinalizeImage | 270 // If we don't need a grayscale image, then we can bypass FinalizeImage |
| 268 // to avoid unnecessary conversions. | 271 // to avoid unnecessary conversions. |
| 269 ClearData(request_id); | 272 ClearData(request_id); |
| 270 SendResponse(request_id, favicon.image_data); | 273 SendResponse(request_id, favicon.image_data); |
| 271 } else { | 274 } else { |
| 272 FinalizeImage(ToBitmap(favicon.image_data->front(), | 275 FinalizeImage(ToBitmap(favicon.image_data->front(), |
| 273 favicon.image_data->size()), request_id); | 276 favicon.image_data->size()), request_id); |
| 274 } | 277 } |
| 275 } | 278 } |
| 276 | 279 |
| 277 void ExtensionIconSource::OnImageLoaded(SkBitmap* image, | 280 void ExtensionIconSource::OnImageLoaded(const gfx::Image* image, |
| 278 const ExtensionResource& resource, | 281 const std::string& extension_id, |
| 279 int index) { | 282 int index) { |
| 280 int request_id = tracker_map_[index]; | 283 int request_id = tracker_map_[index]; |
| 281 tracker_map_.erase(tracker_map_.find(index)); | 284 tracker_map_.erase(tracker_map_.find(index)); |
| 282 | 285 |
| 283 if (!image || image->empty()) | 286 if (!image || image->IsEmpty()) |
| 284 LoadIconFailed(request_id); | 287 LoadIconFailed(request_id); |
| 285 else | 288 else |
| 286 FinalizeImage(image, request_id); | 289 FinalizeImage(image->ToSkBitmap(), request_id); |
| 287 } | 290 } |
| 288 | 291 |
| 289 bool ExtensionIconSource::ParseData(const std::string& path, | 292 bool ExtensionIconSource::ParseData(const std::string& path, |
| 290 int request_id) { | 293 int request_id) { |
| 291 // Extract the parameters from the path by lower casing and splitting. | 294 // Extract the parameters from the path by lower casing and splitting. |
| 292 std::string path_lower = StringToLowerASCII(path); | 295 std::string path_lower = StringToLowerASCII(path); |
| 293 std::vector<std::string> path_parts; | 296 std::vector<std::string> path_parts; |
| 294 | 297 |
| 295 base::SplitString(path_lower, '/', &path_parts); | 298 base::SplitString(path_lower, '/', &path_parts); |
| 296 if (path_lower.empty() || path_parts.size() < 3) | 299 if (path_lower.empty() || path_parts.size() < 3) |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 | 365 |
| 363 void ExtensionIconSource::ClearData(int request_id) { | 366 void ExtensionIconSource::ClearData(int request_id) { |
| 364 std::map<int, ExtensionIconRequest*>::iterator i = | 367 std::map<int, ExtensionIconRequest*>::iterator i = |
| 365 request_map_.find(request_id); | 368 request_map_.find(request_id); |
| 366 if (i == request_map_.end()) | 369 if (i == request_map_.end()) |
| 367 return; | 370 return; |
| 368 | 371 |
| 369 delete i->second; | 372 delete i->second; |
| 370 request_map_.erase(i); | 373 request_map_.erase(i); |
| 371 } | 374 } |
| OLD | NEW |