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

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: License year update Created 7 years, 11 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 namespace {
24
25 struct EmptyIconSet {
Yoyo Zhou 2013/01/08 01:26:13 Do you really need to wrap this in a struct?
Devlin 2013/01/16 23:32:29 Done.
26 ExtensionIconSet icons;
27 };
28
29 static base::LazyInstance<EmptyIconSet> g_empty_icon_set =
30 LAZY_INSTANCE_INITIALIZER;
31
32 } // namespace
33
34 const int IconsInfo::kPageActionIconMaxSize = 19;
35 const int IconsInfo::kBrowserActionIconMaxSize = 19;
36
37 // static
38 const ExtensionIconSet& IconsInfo::GetIcons(const Extension* extension) {
39 IconsInfo* info = static_cast<IconsInfo*>(
40 extension->GetManifestData(extension_manifest_keys::kIcons));
41 return info ? info->icons : g_empty_icon_set.Get().icons;
42 }
43
44 // static
45 void IconsInfo::DecodeIcon(const Extension* extension,
46 int preferred_icon_size,
47 ExtensionIconSet::MatchType match_type,
48 scoped_ptr<SkBitmap>* result) {
49 std::string path = GetIcons(extension).Get(preferred_icon_size, match_type);
50 int size = GetIcons(extension).GetIconSizeFromPath(path);
51 ExtensionResource icon_resource = extension->GetResource(path);
52 DecodeIconFromPath(icon_resource.GetFilePath(), size, result);
53 }
54
55 // static
56 void IconsInfo::DecodeIcon(const Extension* extension,
57 int icon_size,
58 scoped_ptr<SkBitmap>* result) {
59 DecodeIcon(extension, icon_size, ExtensionIconSet::MATCH_EXACTLY, result);
60 }
61
62 // static
63 void IconsInfo::DecodeIconFromPath(const FilePath& icon_path,
64 int icon_size,
65 scoped_ptr<SkBitmap>* result) {
66 if (icon_path.empty())
67 return;
68
69 std::string file_contents;
70 if (!file_util::ReadFileToString(icon_path, &file_contents)) {
71 DLOG(ERROR) << "Could not read icon file: " << icon_path.LossyDisplayName();
72 return;
73 }
74
75 // Decode the image using WebKit's image decoder.
76 const unsigned char* data =
77 reinterpret_cast<const unsigned char*>(file_contents.data());
78 webkit_glue::ImageDecoder decoder;
79 scoped_ptr<SkBitmap> decoded(new SkBitmap());
80 *decoded = decoder.Decode(data, file_contents.length());
81 if (decoded->empty()) {
82 DLOG(ERROR) << "Could not decode icon file: "
83 << icon_path.LossyDisplayName();
84 return;
85 }
86
87 if (decoded->width() != icon_size || decoded->height() != icon_size) {
88 DLOG(ERROR) << "Icon file has unexpected size: "
89 << base::IntToString(decoded->width()) << "x"
90 << base::IntToString(decoded->height());
91 return;
92 }
93
94 result->swap(decoded);
95 }
96
97 // static
98 const gfx::ImageSkia& IconsInfo::GetDefaultIcon(bool is_app) {
99 int id = is_app ? IDR_APP_DEFAULT_ICON : IDR_EXTENSION_DEFAULT_ICON;
100 return *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(id);
101 }
102
103 // static
104 ExtensionResource IconsInfo::GetIconResource(
105 const Extension* extension,
106 int size,
107 ExtensionIconSet::MatchType match_type) {
108 std::string path = GetIcons(extension).Get(size, match_type);
109 return path.empty() ? ExtensionResource() : extension->GetResource(path);
110 }
111
112 // static
113 GURL IconsInfo::GetIconURL(const Extension* extension,
114 int size,
115 ExtensionIconSet::MatchType match_type) {
116 std::string path = GetIcons(extension).Get(size, match_type);
117 return path.empty() ? GURL() : extension->GetResourceURL(path);
118 }
119
120 IconsHandler::IconsHandler() {
121 }
122
123 IconsHandler::~IconsHandler() {
124 }
125
126 bool IconsHandler::Parse(const base::Value* value,
127 Extension* extension,
128 string16* error) {
129 scoped_ptr<IconsInfo> icons_info(new IconsInfo);
130 const DictionaryValue* icons_dict = NULL;
131 if (!value->GetAsDictionary(&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(extension_manifest_keys::kIcons,
146 icons_info.release());
147 return true;
148 }
149
150 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698