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" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "base/string_split.h" | 12 #include "base/string_split.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
16 #include "chrome/browser/extensions/extension_prefs.h" | 16 #include "chrome/browser/extensions/extension_prefs.h" |
17 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
18 #include "chrome/browser/extensions/extension_system.h" | 18 #include "chrome/browser/extensions/extension_system.h" |
19 #include "chrome/browser/extensions/image_loader.h" | 19 #include "chrome/browser/extensions/image_loader.h" |
20 #include "chrome/browser/favicon/favicon_service_factory.h" | 20 #include "chrome/browser/favicon/favicon_service_factory.h" |
21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
22 #include "chrome/common/extensions/extension.h" | 23 #include "chrome/common/extensions/extension.h" |
23 #include "chrome/common/extensions/extension_constants.h" | 24 #include "chrome/common/extensions/extension_constants.h" |
24 #include "chrome/common/extensions/extension_resource.h" | 25 #include "chrome/common/extensions/extension_resource.h" |
25 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
26 #include "googleurl/src/gurl.h" | 27 #include "googleurl/src/gurl.h" |
27 #include "grit/component_extension_resources_map.h" | 28 #include "grit/component_extension_resources_map.h" |
28 #include "grit/theme_resources.h" | 29 #include "grit/theme_resources.h" |
29 #include "skia/ext/image_operations.h" | 30 #include "skia/ext/image_operations.h" |
30 #include "ui/base/layout.h" | 31 #include "ui/base/layout.h" |
31 #include "ui/base/resource/resource_bundle.h" | 32 #include "ui/base/resource/resource_bundle.h" |
(...skipping 19 matching lines...) Expand all Loading... |
51 SkBitmap* ToBitmap(const unsigned char* data, size_t size) { | 52 SkBitmap* ToBitmap(const unsigned char* data, size_t size) { |
52 webkit_glue::ImageDecoder decoder; | 53 webkit_glue::ImageDecoder decoder; |
53 SkBitmap* decoded = new SkBitmap(); | 54 SkBitmap* decoded = new SkBitmap(); |
54 *decoded = decoder.Decode(data, size); | 55 *decoded = decoder.Decode(data, size); |
55 return decoded; | 56 return decoded; |
56 } | 57 } |
57 | 58 |
58 } // namespace | 59 } // namespace |
59 | 60 |
60 ExtensionIconSource::ExtensionIconSource(Profile* profile) | 61 ExtensionIconSource::ExtensionIconSource(Profile* profile) |
61 : DataSource(chrome::kChromeUIExtensionIconHost, MessageLoop::current()), | 62 : profile_(profile) { |
62 profile_(profile) { | |
63 } | 63 } |
64 | 64 |
65 struct ExtensionIconSource::ExtensionIconRequest { | 65 struct ExtensionIconSource::ExtensionIconRequest { |
66 int request_id; | 66 int request_id; |
67 const extensions::Extension* extension; | 67 const extensions::Extension* extension; |
68 bool grayscale; | 68 bool grayscale; |
69 int size; | 69 int size; |
70 ExtensionIconSet::MatchType match; | 70 ExtensionIconSet::MatchType match; |
71 }; | 71 }; |
72 | 72 |
(...skipping 23 matching lines...) Expand all Loading... |
96 std::string contents = ResourceBundle::GetSharedInstance() | 96 std::string contents = ResourceBundle::GetSharedInstance() |
97 .GetRawDataResourceForScale(resource_id, | 97 .GetRawDataResourceForScale(resource_id, |
98 ui::SCALE_FACTOR_100P).as_string(); | 98 ui::SCALE_FACTOR_100P).as_string(); |
99 | 99 |
100 // Convert and return it. | 100 // Convert and return it. |
101 const unsigned char* data = | 101 const unsigned char* data = |
102 reinterpret_cast<const unsigned char*>(contents.data()); | 102 reinterpret_cast<const unsigned char*>(contents.data()); |
103 return ToBitmap(data, contents.length()); | 103 return ToBitmap(data, contents.length()); |
104 } | 104 } |
105 | 105 |
| 106 std::string ExtensionIconSource::GetSource() { |
| 107 return chrome::kChromeUIExtensionIconHost; |
| 108 } |
| 109 |
106 std::string ExtensionIconSource::GetMimeType(const std::string&) const { | 110 std::string ExtensionIconSource::GetMimeType(const std::string&) const { |
107 // We need to explicitly return a mime type, otherwise if the user tries to | 111 // We need to explicitly return a mime type, otherwise if the user tries to |
108 // drag the image they get no extension. | 112 // drag the image they get no extension. |
109 return "image/png"; | 113 return "image/png"; |
110 } | 114 } |
111 | 115 |
112 void ExtensionIconSource::StartDataRequest(const std::string& path, | 116 void ExtensionIconSource::StartDataRequest(const std::string& path, |
113 bool is_incognito, | 117 bool is_incognito, |
114 int request_id) { | 118 int request_id) { |
115 // This is where everything gets started. First, parse the request and make | 119 // This is where everything gets started. First, parse the request and make |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 157 |
154 void ExtensionIconSource::FinalizeImage(const SkBitmap* image, | 158 void ExtensionIconSource::FinalizeImage(const SkBitmap* image, |
155 int request_id) { | 159 int request_id) { |
156 SkBitmap bitmap; | 160 SkBitmap bitmap; |
157 if (GetData(request_id)->grayscale) | 161 if (GetData(request_id)->grayscale) |
158 bitmap = DesaturateImage(image); | 162 bitmap = DesaturateImage(image); |
159 else | 163 else |
160 bitmap = *image; | 164 bitmap = *image; |
161 | 165 |
162 ClearData(request_id); | 166 ClearData(request_id); |
163 SendResponse(request_id, BitmapToMemory(&bitmap)); | 167 url_data_source()->SendResponse(request_id, BitmapToMemory(&bitmap)); |
164 } | 168 } |
165 | 169 |
166 void ExtensionIconSource::LoadDefaultImage(int request_id) { | 170 void ExtensionIconSource::LoadDefaultImage(int request_id) { |
167 ExtensionIconRequest* request = GetData(request_id); | 171 ExtensionIconRequest* request = GetData(request_id); |
168 const SkBitmap* default_image = NULL; | 172 const SkBitmap* default_image = NULL; |
169 | 173 |
170 if (request->extension->is_app()) | 174 if (request->extension->is_app()) |
171 default_image = GetDefaultAppImage(); | 175 default_image = GetDefaultAppImage(); |
172 else | 176 else |
173 default_image = GetDefaultExtensionImage(); | 177 default_image = GetDefaultExtensionImage(); |
(...skipping 10 matching lines...) Expand all Loading... |
184 | 188 |
185 FinalizeImage(&resized_image, request_id); | 189 FinalizeImage(&resized_image, request_id); |
186 } | 190 } |
187 | 191 |
188 void ExtensionIconSource::LoadExtensionImage(const ExtensionResource& icon, | 192 void ExtensionIconSource::LoadExtensionImage(const ExtensionResource& icon, |
189 int request_id) { | 193 int request_id) { |
190 ExtensionIconRequest* request = GetData(request_id); | 194 ExtensionIconRequest* request = GetData(request_id); |
191 extensions::ImageLoader::Get(profile_)->LoadImageAsync( | 195 extensions::ImageLoader::Get(profile_)->LoadImageAsync( |
192 request->extension, icon, | 196 request->extension, icon, |
193 gfx::Size(request->size, request->size), | 197 gfx::Size(request->size, request->size), |
194 base::Bind(&ExtensionIconSource::OnImageLoaded, this, request_id)); | 198 base::Bind(&ExtensionIconSource::OnImageLoaded, AsWeakPtr(), request_id)); |
195 } | 199 } |
196 | 200 |
197 void ExtensionIconSource::LoadFaviconImage(int request_id) { | 201 void ExtensionIconSource::LoadFaviconImage(int request_id) { |
198 FaviconService* favicon_service = | 202 FaviconService* favicon_service = |
199 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 203 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
200 // Fall back to the default icons if the service isn't available. | 204 // Fall back to the default icons if the service isn't available. |
201 if (favicon_service == NULL) { | 205 if (favicon_service == NULL) { |
202 LoadDefaultImage(request_id); | 206 LoadDefaultImage(request_id); |
203 return; | 207 return; |
204 } | 208 } |
(...skipping 16 matching lines...) Expand all Loading... |
221 // Fallback to the default icon if there wasn't a favicon. | 225 // Fallback to the default icon if there wasn't a favicon. |
222 if (!bitmap_result.is_valid()) { | 226 if (!bitmap_result.is_valid()) { |
223 LoadDefaultImage(request_id); | 227 LoadDefaultImage(request_id); |
224 return; | 228 return; |
225 } | 229 } |
226 | 230 |
227 if (!request->grayscale) { | 231 if (!request->grayscale) { |
228 // If we don't need a grayscale image, then we can bypass FinalizeImage | 232 // If we don't need a grayscale image, then we can bypass FinalizeImage |
229 // to avoid unnecessary conversions. | 233 // to avoid unnecessary conversions. |
230 ClearData(request_id); | 234 ClearData(request_id); |
231 SendResponse(request_id, bitmap_result.bitmap_data); | 235 url_data_source()->SendResponse(request_id, bitmap_result.bitmap_data); |
232 } else { | 236 } else { |
233 FinalizeImage(ToBitmap(bitmap_result.bitmap_data->front(), | 237 FinalizeImage(ToBitmap(bitmap_result.bitmap_data->front(), |
234 bitmap_result.bitmap_data->size()), request_id); | 238 bitmap_result.bitmap_data->size()), request_id); |
235 } | 239 } |
236 } | 240 } |
237 | 241 |
238 void ExtensionIconSource::OnImageLoaded(int request_id, | 242 void ExtensionIconSource::OnImageLoaded(int request_id, |
239 const gfx::Image& image) { | 243 const gfx::Image& image) { |
240 if (image.IsEmpty()) | 244 if (image.IsEmpty()) |
241 LoadIconFailed(request_id); | 245 LoadIconFailed(request_id); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 | 299 |
296 SetData(request_id, extension, grayscale, size, match_type); | 300 SetData(request_id, extension, grayscale, size, match_type); |
297 | 301 |
298 return true; | 302 return true; |
299 } | 303 } |
300 | 304 |
301 void ExtensionIconSource::SendDefaultResponse(int request_id) { | 305 void ExtensionIconSource::SendDefaultResponse(int request_id) { |
302 // We send back the default application icon (not resized or desaturated) | 306 // We send back the default application icon (not resized or desaturated) |
303 // as the default response, like when there is no data. | 307 // as the default response, like when there is no data. |
304 ClearData(request_id); | 308 ClearData(request_id); |
305 SendResponse(request_id, BitmapToMemory(GetDefaultAppImage())); | 309 url_data_source()->SendResponse( |
| 310 request_id, BitmapToMemory(GetDefaultAppImage())); |
306 } | 311 } |
307 | 312 |
308 void ExtensionIconSource::SetData(int request_id, | 313 void ExtensionIconSource::SetData(int request_id, |
309 const extensions::Extension* extension, | 314 const extensions::Extension* extension, |
310 bool grayscale, | 315 bool grayscale, |
311 int size, | 316 int size, |
312 ExtensionIconSet::MatchType match) { | 317 ExtensionIconSet::MatchType match) { |
313 ExtensionIconRequest* request = new ExtensionIconRequest(); | 318 ExtensionIconRequest* request = new ExtensionIconRequest(); |
314 request->request_id = request_id; | 319 request->request_id = request_id; |
315 request->extension = extension; | 320 request->extension = extension; |
(...skipping 10 matching lines...) Expand all Loading... |
326 | 331 |
327 void ExtensionIconSource::ClearData(int request_id) { | 332 void ExtensionIconSource::ClearData(int request_id) { |
328 std::map<int, ExtensionIconRequest*>::iterator i = | 333 std::map<int, ExtensionIconRequest*>::iterator i = |
329 request_map_.find(request_id); | 334 request_map_.find(request_id); |
330 if (i == request_map_.end()) | 335 if (i == request_map_.end()) |
331 return; | 336 return; |
332 | 337 |
333 delete i->second; | 338 delete i->second; |
334 request_map_.erase(i); | 339 request_map_.erase(i); |
335 } | 340 } |
OLD | NEW |