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

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

Issue 13460012: Move IconsHandler from c/c/e/api/ to c/c/e (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Latest master for CQ Created 7 years, 8 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/strings/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_file_util.h"
15 #include "chrome/common/extensions/extension_manifest_constants.h"
16 #include "chrome/common/extensions/manifest_handler_helpers.h"
17 #include "grit/generated_resources.h"
18 #include "grit/theme_resources.h"
19 #include "third_party/skia/include/core/SkBitmap.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "webkit/glue/image_decoder.h"
22
23 namespace keys = extension_manifest_keys;
24
25 namespace extensions {
26
27 static base::LazyInstance<ExtensionIconSet> g_empty_icon_set =
28 LAZY_INSTANCE_INITIALIZER;
29
30 const int IconsInfo::kPageActionIconMaxSize = 19;
31 const int IconsInfo::kBrowserActionIconMaxSize = 19;
32
33 // static
34 const ExtensionIconSet& IconsInfo::GetIcons(const Extension* extension) {
35 IconsInfo* info = static_cast<IconsInfo*>(
36 extension->GetManifestData(keys::kIcons));
37 return info ? info->icons : g_empty_icon_set.Get();
38 }
39
40 // static
41 void IconsInfo::DecodeIcon(const Extension* extension,
42 int preferred_icon_size,
43 ExtensionIconSet::MatchType match_type,
44 scoped_ptr<SkBitmap>* result) {
45 std::string path = GetIcons(extension).Get(preferred_icon_size, match_type);
46 int size = GetIcons(extension).GetIconSizeFromPath(path);
47 ExtensionResource icon_resource = extension->GetResource(path);
48 DecodeIconFromPath(icon_resource.GetFilePath(), size, result);
49 }
50
51 // static
52 void IconsInfo::DecodeIcon(const Extension* extension,
53 int icon_size,
54 scoped_ptr<SkBitmap>* result) {
55 DecodeIcon(extension, icon_size, ExtensionIconSet::MATCH_EXACTLY, result);
56 }
57
58 // static
59 void IconsInfo::DecodeIconFromPath(const base::FilePath& icon_path,
60 int icon_size,
61 scoped_ptr<SkBitmap>* result) {
62 if (icon_path.empty())
63 return;
64
65 std::string file_contents;
66 if (!file_util::ReadFileToString(icon_path, &file_contents)) {
67 DLOG(ERROR) << "Could not read icon file: " << icon_path.LossyDisplayName();
68 return;
69 }
70
71 // Decode the image using WebKit's image decoder.
72 const unsigned char* data =
73 reinterpret_cast<const unsigned char*>(file_contents.data());
74 webkit_glue::ImageDecoder decoder;
75 scoped_ptr<SkBitmap> decoded(new SkBitmap());
76 *decoded = decoder.Decode(data, file_contents.length());
77 if (decoded->empty()) {
78 DLOG(ERROR) << "Could not decode icon file: "
79 << icon_path.LossyDisplayName();
80 return;
81 }
82
83 if (decoded->width() != icon_size || decoded->height() != icon_size) {
84 DLOG(ERROR) << "Icon file has unexpected size: "
85 << base::IntToString(decoded->width()) << "x"
86 << base::IntToString(decoded->height());
87 return;
88 }
89
90 result->swap(decoded);
91 }
92
93 // static
94 const gfx::ImageSkia& IconsInfo::GetDefaultAppIcon() {
95 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
96 IDR_APP_DEFAULT_ICON);
97 }
98
99 // static
100 const gfx::ImageSkia& IconsInfo::GetDefaultExtensionIcon() {
101 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
102 IDR_EXTENSION_DEFAULT_ICON);
103 }
104
105 // static
106 ExtensionResource IconsInfo::GetIconResource(
107 const Extension* extension,
108 int size,
109 ExtensionIconSet::MatchType match_type) {
110 std::string path = GetIcons(extension).Get(size, match_type);
111 return path.empty() ? ExtensionResource() : extension->GetResource(path);
112 }
113
114 // static
115 GURL IconsInfo::GetIconURL(const Extension* extension,
116 int size,
117 ExtensionIconSet::MatchType match_type) {
118 std::string path = GetIcons(extension).Get(size, match_type);
119 return path.empty() ? GURL() : extension->GetResourceURL(path);
120 }
121
122 IconsHandler::IconsHandler() {
123 }
124
125 IconsHandler::~IconsHandler() {
126 }
127
128 bool IconsHandler::Parse(Extension* extension, string16* error) {
129 scoped_ptr<IconsInfo> icons_info(new IconsInfo);
130 const DictionaryValue* icons_dict = NULL;
131 if (!extension->manifest()->GetDictionary(keys::kIcons, &icons_dict)) {
132 *error = ASCIIToUTF16(extension_manifest_errors::kInvalidIcons);
133 return false;
134 }
135
136 if (!manifest_handler_helpers::LoadIconsFromDictionary(
137 icons_dict,
138 extension_misc::kExtensionIconSizes,
139 extension_misc::kNumExtensionIconSizes,
140 &icons_info->icons,
141 error)) {
142 return false;
143 }
144
145 extension->SetManifestData(keys::kIcons, icons_info.release());
146 return true;
147 }
148
149 bool IconsHandler::Validate(const Extension* extension,
150 std::string* error,
151 std::vector<InstallWarning>* warnings) const {
152 return extension_file_util::ValidateExtensionIconSet(
153 IconsInfo::GetIcons(extension),
154 extension,
155 IDS_EXTENSION_LOAD_ICON_FAILED,
156 error);
157 }
158
159 const std::vector<std::string> IconsHandler::Keys() const {
160 return SingleKey(keys::kIcons);
161 }
162
163 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/common/extensions/api/icons/icons_handler.h ('k') | chrome/common/extensions/api/icons/icons_manifest_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698