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

Side by Side Diff: chrome/common/extensions/api/icons/icons_handler.cc

Issue 11786003: Move Icons out of Extension class (Closed) Base URL: http://git.chromium.org/chromium/src.git@dc_unref_browser_action
Patch Set: Created 7 years, 10 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
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/common/extensions/api/icons/icons_handler.h"
6
7 #include "base/file_util.h"
8 #include "base/lazy_instance.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/string_number_conversions.h"
11 #include "base/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "chrome/common/extensions/extension.h"
14 #include "chrome/common/extensions/extension_manifest_constants.h"
15 #include "chrome/common/extensions/manifest_handler_helpers.h"
16 #include "grit/theme_resources.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "ui/base/resource/resource_bundle.h"
19 #include "webkit/glue/image_decoder.h"
20
21 namespace extensions {
22
23 static base::LazyInstance<ExtensionIconSet> g_empty_icon_set =
24 LAZY_INSTANCE_INITIALIZER;
25
26 const int IconsInfo::kPageActionIconMaxSize = 19;
27 const int IconsInfo::kBrowserActionIconMaxSize = 19;
28
29 // static
30 const ExtensionIconSet& IconsInfo::GetIcons(const Extension* extension) {
31 IconsInfo* info = static_cast<IconsInfo*>(
32 extension->GetManifestData(extension_manifest_keys::kIcons));
33 return info ? info->icons : g_empty_icon_set.Get();
34 }
35
36 // static
37 void IconsInfo::DecodeIcon(const Extension* extension,
38 int preferred_icon_size,
39 ExtensionIconSet::MatchType match_type,
40 scoped_ptr<SkBitmap>* result) {
41 std::string path = GetIcons(extension).Get(preferred_icon_size, match_type);
42 int size = GetIcons(extension).GetIconSizeFromPath(path);
43 ExtensionResource icon_resource = extension->GetResource(path);
44 DecodeIconFromPath(icon_resource.GetFilePath(), size, result);
45 }
46
47 // static
48 void IconsInfo::DecodeIcon(const Extension* extension,
49 int icon_size,
50 scoped_ptr<SkBitmap>* result) {
51 DecodeIcon(extension, icon_size, ExtensionIconSet::MATCH_EXACTLY, result);
52 }
53
54 // static
55 void IconsInfo::DecodeIconFromPath(const FilePath& icon_path,
56 int icon_size,
57 scoped_ptr<SkBitmap>* result) {
58 if (icon_path.empty())
59 return;
60
61 std::string file_contents;
62 if (!file_util::ReadFileToString(icon_path, &file_contents)) {
63 DLOG(ERROR) << "Could not read icon file: " << icon_path.LossyDisplayName();
64 return;
65 }
66
67 // Decode the image using WebKit's image decoder.
68 const unsigned char* data =
69 reinterpret_cast<const unsigned char*>(file_contents.data());
70 webkit_glue::ImageDecoder decoder;
71 scoped_ptr<SkBitmap> decoded(new SkBitmap());
72 *decoded = decoder.Decode(data, file_contents.length());
73 if (decoded->empty()) {
74 DLOG(ERROR) << "Could not decode icon file: "
75 << icon_path.LossyDisplayName();
76 return;
77 }
78
79 if (decoded->width() != icon_size || decoded->height() != icon_size) {
80 DLOG(ERROR) << "Icon file has unexpected size: "
81 << base::IntToString(decoded->width()) << "x"
82 << base::IntToString(decoded->height());
83 return;
84 }
85
86 result->swap(decoded);
87 }
88
89 // static
90 const gfx::ImageSkia& IconsInfo::GetDefaultAppIcon() {
91 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
92 IDR_APP_DEFAULT_ICON);
93 }
94
95 // static
96 const gfx::ImageSkia& IconsInfo::GetDefaultExtensionIcon() {
97 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
98 IDR_EXTENSION_DEFAULT_ICON);
99 }
100
101 // static
102 ExtensionResource IconsInfo::GetIconResource(
103 const Extension* extension,
104 int size,
105 ExtensionIconSet::MatchType match_type) {
106 std::string path = GetIcons(extension).Get(size, match_type);
107 return path.empty() ? ExtensionResource() : extension->GetResource(path);
108 }
109
110 // static
111 GURL IconsInfo::GetIconURL(const Extension* extension,
112 int size,
113 ExtensionIconSet::MatchType match_type) {
114 std::string path = GetIcons(extension).Get(size, match_type);
115 return path.empty() ? GURL() : extension->GetResourceURL(path);
116 }
117
118 IconsHandler::IconsHandler() {
119 }
120
121 IconsHandler::~IconsHandler() {
122 }
123
124 bool IconsHandler::Parse(const base::Value* value,
125 Extension* extension,
126 string16* error) {
127 scoped_ptr<IconsInfo> icons_info(new IconsInfo);
128 const DictionaryValue* icons_dict = NULL;
129 if (!value->GetAsDictionary(&icons_dict)) {
130 *error = ASCIIToUTF16(extension_manifest_errors::kInvalidIcons);
131 return false;
132 }
133
134 if (!manifest_handler_helpers::LoadIconsFromDictionary(
135 icons_dict,
136 extension_misc::kExtensionIconSizes,
137 extension_misc::kNumExtensionIconSizes,
138 &icons_info->icons,
139 error)) {
140 return false;
141 }
142
143 extension->SetManifestData(extension_manifest_keys::kIcons,
144 icons_info.release());
145 return true;
146 }
147
148 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698