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

Side by Side Diff: chrome/common/extensions/extension.cc

Issue 10905005: Change browser/page action default icon defined in manifest to support hidpi. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 3 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 | Annotate | Revision Log
OLDNEW
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/common/extensions/extension.h" 5 #include "chrome/common/extensions/extension.h"
6 6
7 #include <ostream> 7 #include <ostream>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 if (base::HexStringToInt(base::StringPiece(id->begin() + i, 123 if (base::HexStringToInt(base::StringPiece(id->begin() + i,
124 id->begin() + i + 1), 124 id->begin() + i + 1),
125 &val)) { 125 &val)) {
126 (*id)[i] = val + 'a'; 126 (*id)[i] = val + 'a';
127 } else { 127 } else {
128 (*id)[i] = 'a'; 128 (*id)[i] = 'a';
129 } 129 }
130 } 130 }
131 } 131 }
132 132
133 // Loads icon paths defined in dictionary |icons_value| into ExtensionIconSet
134 // |icons|. |icons_value| is a dictionary value {icon size -> icon path}. Icons
135 // in |icons_value| whose size is not in |icon_sizes| will be ignored.
136 // Returns success. If load fails, |error| will be set.
137 bool LoadIconsFromDictionary(const DictionaryValue* icons_value,
138 const int* icon_sizes,
139 size_t num_icon_sizes,
140 ExtensionIconSet* icons,
141 string16* error) {
142 DCHECK(icons);
143 for (size_t i = 0; i < num_icon_sizes; ++i) {
144 std::string key = base::IntToString(icon_sizes[i]);
145 if (icons_value->HasKey(key)) {
146 std::string icon_path;
147 if (!icons_value->GetString(key, &icon_path)) {
148 *error = ExtensionErrorUtils::FormatErrorMessageUTF16(
149 errors::kInvalidIconPath, key);
150 return false;
151 }
152
153 if (!icon_path.empty() && icon_path[0] == '/')
154 icon_path = icon_path.substr(1);
155
156 if (icon_path.empty()) {
157 *error = ExtensionErrorUtils::FormatErrorMessageUTF16(
158 errors::kInvalidIconPath, key);
159 return false;
160 }
161 icons->Add(icon_sizes[i], icon_path);
162 }
163 }
164 return true;
165 }
166
133 // A singleton object containing global data needed by the extension objects. 167 // A singleton object containing global data needed by the extension objects.
134 class ExtensionConfig { 168 class ExtensionConfig {
135 public: 169 public:
136 static ExtensionConfig* GetInstance() { 170 static ExtensionConfig* GetInstance() {
137 return Singleton<ExtensionConfig>::get(); 171 return Singleton<ExtensionConfig>::get();
138 } 172 }
139 173
140 Extension::ScriptingWhitelist* whitelist() { return &scripting_whitelist_; } 174 Extension::ScriptingWhitelist* whitelist() { return &scripting_whitelist_; }
141 175
142 private: 176 private:
(...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after
806 if (extension_action->HasKey(keys::kPageActionIcons) && 840 if (extension_action->HasKey(keys::kPageActionIcons) &&
807 extension_action->GetList(keys::kPageActionIcons, &icons)) { 841 extension_action->GetList(keys::kPageActionIcons, &icons)) {
808 for (ListValue::const_iterator iter = icons->begin(); 842 for (ListValue::const_iterator iter = icons->begin();
809 iter != icons->end(); ++iter) { 843 iter != icons->end(); ++iter) {
810 std::string path; 844 std::string path;
811 if (!(*iter)->GetAsString(&path) || path.empty()) { 845 if (!(*iter)->GetAsString(&path) || path.empty()) {
812 *error = ASCIIToUTF16(errors::kInvalidPageActionIconPath); 846 *error = ASCIIToUTF16(errors::kInvalidPageActionIconPath);
813 return scoped_ptr<ExtensionAction>(); 847 return scoped_ptr<ExtensionAction>();
814 } 848 }
815 849
816 result->icon_paths()->push_back(path); 850 scoped_ptr<ExtensionIconSet> icon_set(new ExtensionIconSet);
851 icon_set->Add(extension_misc::EXTENSION_ICON_ACTION, path);
852 result->AddPageActionIcon(icon_set.Pass());
817 } 853 }
818 } 854 }
819 855
820 std::string id; 856 std::string id;
821 if (extension_action->HasKey(keys::kPageActionId)) { 857 if (extension_action->HasKey(keys::kPageActionId)) {
822 if (!extension_action->GetString(keys::kPageActionId, &id)) { 858 if (!extension_action->GetString(keys::kPageActionId, &id)) {
823 *error = ASCIIToUTF16(errors::kInvalidPageActionId); 859 *error = ASCIIToUTF16(errors::kInvalidPageActionId);
824 return scoped_ptr<ExtensionAction>(); 860 return scoped_ptr<ExtensionAction>();
825 } 861 }
826 result->set_id(id); 862 result->set_id(id);
827 } 863 }
828 } 864 }
829 865
830 std::string default_icon;
831 // Read the page action |default_icon| (optional). 866 // Read the page action |default_icon| (optional).
867 // The |default_icon| value can be either dictionary {icon size -> icon path}
868 // or non empty string value.
832 if (extension_action->HasKey(keys::kPageActionDefaultIcon)) { 869 if (extension_action->HasKey(keys::kPageActionDefaultIcon)) {
833 if (!extension_action->GetString(keys::kPageActionDefaultIcon, 870 const DictionaryValue* icons_value = NULL;
834 &default_icon) || 871 std::string default_icon;
835 default_icon.empty()) { 872 if (extension_action->GetDictionary(keys::kPageActionDefaultIcon,
873 &icons_value)) {
874 scoped_ptr<ExtensionIconSet> default_icons(new ExtensionIconSet());
875 if (!LoadIconsFromDictionary(icons_value,
876 extension_misc::kExtensionActionIconSizes,
877 extension_misc::kNumExtensionActionIconSizes,
878 default_icons.get(),
879 error)) {
880 return scoped_ptr<ExtensionAction>();
881 }
882
883 result->set_default_icon(default_icons.Pass());
884 } else if (extension_action->GetString(keys::kPageActionDefaultIcon,
885 &default_icon) &&
886 !default_icon.empty()) {
887 scoped_ptr<ExtensionIconSet> icon_set(new ExtensionIconSet);
888 icon_set->Add(extension_misc::EXTENSION_ICON_ACTION, default_icon);
889 result->set_default_icon(icon_set.Pass());
890 } else {
836 *error = ASCIIToUTF16(errors::kInvalidPageActionIconPath); 891 *error = ASCIIToUTF16(errors::kInvalidPageActionIconPath);
837 return scoped_ptr<ExtensionAction>(); 892 return scoped_ptr<ExtensionAction>();
838 } 893 }
839 result->set_default_icon_path(default_icon);
840 } 894 }
841 895
842 // Read the page action title from |default_title| if present, |name| if not 896 // Read the page action title from |default_title| if present, |name| if not
843 // (both optional). 897 // (both optional).
844 std::string title; 898 std::string title;
845 if (extension_action->HasKey(keys::kPageActionDefaultTitle)) { 899 if (extension_action->HasKey(keys::kPageActionDefaultTitle)) {
846 if (!extension_action->GetString(keys::kPageActionDefaultTitle, &title)) { 900 if (!extension_action->GetString(keys::kPageActionDefaultTitle, &title)) {
847 *error = ASCIIToUTF16(errors::kInvalidPageActionDefaultTitle); 901 *error = ASCIIToUTF16(errors::kInvalidPageActionDefaultTitle);
848 return scoped_ptr<ExtensionAction>(); 902 return scoped_ptr<ExtensionAction>();
849 } 903 }
(...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after
1357 1411
1358 bool Extension::LoadIcons(string16* error) { 1412 bool Extension::LoadIcons(string16* error) {
1359 if (!manifest_->HasKey(keys::kIcons)) 1413 if (!manifest_->HasKey(keys::kIcons))
1360 return true; 1414 return true;
1361 DictionaryValue* icons_value = NULL; 1415 DictionaryValue* icons_value = NULL;
1362 if (!manifest_->GetDictionary(keys::kIcons, &icons_value)) { 1416 if (!manifest_->GetDictionary(keys::kIcons, &icons_value)) {
1363 *error = ASCIIToUTF16(errors::kInvalidIcons); 1417 *error = ASCIIToUTF16(errors::kInvalidIcons);
1364 return false; 1418 return false;
1365 } 1419 }
1366 1420
1367 for (size_t i = 0; i < extension_misc::kNumExtensionIconSizes; ++i) { 1421 return LoadIconsFromDictionary(icons_value,
1368 std::string key = base::IntToString(extension_misc::kExtensionIconSizes[i]); 1422 extension_misc::kExtensionIconSizes,
1369 if (icons_value->HasKey(key)) { 1423 extension_misc::kNumExtensionIconSizes,
1370 std::string icon_path; 1424 &icons_,
1371 if (!icons_value->GetString(key, &icon_path)) { 1425 error);
1372 *error = ExtensionErrorUtils::FormatErrorMessageUTF16(
1373 errors::kInvalidIconPath, key);
1374 return false;
1375 }
1376
1377 if (!icon_path.empty() && icon_path[0] == '/')
1378 icon_path = icon_path.substr(1);
1379
1380 if (icon_path.empty()) {
1381 *error = ExtensionErrorUtils::FormatErrorMessageUTF16(
1382 errors::kInvalidIconPath, key);
1383 return false;
1384 }
1385 icons_.Add(extension_misc::kExtensionIconSizes[i], icon_path);
1386 }
1387 }
1388 return true;
1389 } 1426 }
1390 1427
1391 bool Extension::LoadCommands(string16* error) { 1428 bool Extension::LoadCommands(string16* error) {
1392 if (manifest_->HasKey(keys::kCommands)) { 1429 if (manifest_->HasKey(keys::kCommands)) {
1393 DictionaryValue* commands = NULL; 1430 DictionaryValue* commands = NULL;
1394 if (!manifest_->GetDictionary(keys::kCommands, &commands)) { 1431 if (!manifest_->GetDictionary(keys::kCommands, &commands)) {
1395 *error = ASCIIToUTF16(errors::kInvalidCommandsKey); 1432 *error = ASCIIToUTF16(errors::kInvalidCommandsKey);
1396 return false; 1433 return false;
1397 } 1434 }
1398 1435
(...skipping 876 matching lines...) Expand 10 before | Expand all | Expand 10 after
2275 // that matches the icon of a trusted extension, and users wouldn't be warned 2312 // that matches the icon of a trusted extension, and users wouldn't be warned
2276 // during installation. 2313 // during installation.
2277 2314
2278 if (!script_badge_->GetTitle(ExtensionAction::kDefaultTabId).empty()) { 2315 if (!script_badge_->GetTitle(ExtensionAction::kDefaultTabId).empty()) {
2279 install_warnings_.push_back( 2316 install_warnings_.push_back(
2280 InstallWarning(InstallWarning::FORMAT_TEXT, 2317 InstallWarning(InstallWarning::FORMAT_TEXT,
2281 errors::kScriptBadgeTitleIgnored)); 2318 errors::kScriptBadgeTitleIgnored));
2282 } 2319 }
2283 script_badge_->SetTitle(ExtensionAction::kDefaultTabId, name()); 2320 script_badge_->SetTitle(ExtensionAction::kDefaultTabId, name());
2284 2321
2285 if (!script_badge_->default_icon_path().empty()) { 2322 if (script_badge_->default_icon()) {
2286 install_warnings_.push_back( 2323 install_warnings_.push_back(
2287 InstallWarning(InstallWarning::FORMAT_TEXT, 2324 InstallWarning(InstallWarning::FORMAT_TEXT,
2288 errors::kScriptBadgeIconIgnored)); 2325 errors::kScriptBadgeIconIgnored));
2289 } 2326 }
2290 std::string icon16_path = icons().Get(extension_misc::EXTENSION_ICON_BITTY, 2327
2291 ExtensionIconSet::MATCH_EXACTLY); 2328 scoped_ptr<ExtensionIconSet> icon_set(new ExtensionIconSet);
2292 if (!icon16_path.empty()) { 2329
2293 script_badge_->set_default_icon_path(icon16_path); 2330 for (size_t i = 0; i < extension_misc::kNumScriptBadgeIconSizes; i++) {
2331 std::string path = icons().Get(extension_misc::kScriptBadgeIconSizes[i],
2332 ExtensionIconSet::MATCH_EXACTLY);
2333 if (!path.empty()) {
2334 icon_set->Add(extension_misc::kScriptBadgeIconSizes[i], path);
2335 }
2336 }
2337
2338 if (!icon_set->map().empty()) {
2339 script_badge_->set_default_icon(icon_set.Pass());
2294 } else { 2340 } else {
2295 script_badge_->SetIcon( 2341 script_badge_->set_default_icon(scoped_ptr<ExtensionIconSet>().Pass());
Jeffrey Yasskin 2012/09/13 00:23:36 You shouldn't need .Pass() on this line. Temporary
tbarzic 2012/09/13 02:01:01 Done.
2296 ExtensionAction::kDefaultTabId,
2297 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
2298 IDR_EXTENSIONS_FAVICON));
2299 } 2342 }
2300 2343
2301 return true; 2344 return true;
2302 } 2345 }
2303 2346
2304 bool Extension::LoadFileBrowserHandlers(string16* error) { 2347 bool Extension::LoadFileBrowserHandlers(string16* error) {
2305 if (!manifest_->HasKey(keys::kFileBrowserHandlers)) 2348 if (!manifest_->HasKey(keys::kFileBrowserHandlers))
2306 return true; 2349 return true;
2307 ListValue* file_browser_handlers_value = NULL; 2350 ListValue* file_browser_handlers_value = NULL;
2308 if (!manifest_->GetList(keys::kFileBrowserHandlers, 2351 if (!manifest_->GetList(keys::kFileBrowserHandlers,
(...skipping 799 matching lines...) Expand 10 before | Expand all | Expand 10 after
3108 for (DictionaryValue::key_iterator it = theme_images->begin_keys(); 3151 for (DictionaryValue::key_iterator it = theme_images->begin_keys();
3109 it != theme_images->end_keys(); ++it) { 3152 it != theme_images->end_keys(); ++it) {
3110 std::string val; 3153 std::string val;
3111 if (theme_images->GetStringWithoutPathExpansion(*it, &val)) 3154 if (theme_images->GetStringWithoutPathExpansion(*it, &val))
3112 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(val))); 3155 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(val)));
3113 } 3156 }
3114 } 3157 }
3115 3158
3116 // Page action icons. 3159 // Page action icons.
3117 if (page_action()) { 3160 if (page_action()) {
3118 std::vector<std::string>* icon_paths = page_action()->icon_paths(); 3161 const std::vector<const ExtensionIconSet*>& page_action_icons =
3119 for (std::vector<std::string>::iterator iter = icon_paths->begin(); 3162 page_action()->page_action_icons();
3120 iter != icon_paths->end(); ++iter) { 3163 for (size_t icon = 0; icon < page_action_icons.size(); icon++) {
3121 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(*iter))); 3164 for (ExtensionIconSet::IconMap::const_iterator iter =
3165 page_action_icons[icon]->map().begin();
3166 iter != page_action_icons[icon]->map().end();
3167 ++iter) {
3168 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(iter->second)));
3169 }
3122 } 3170 }
3123 } 3171 }
3124 3172
3125 // Browser action icons. 3173 // Browser action icons.
3126 if (browser_action()) { 3174 if (browser_action()) {
3127 std::vector<std::string>* icon_paths = browser_action()->icon_paths(); 3175 const std::vector<const ExtensionIconSet*>& page_action_icons =
3128 for (std::vector<std::string>::iterator iter = icon_paths->begin(); 3176 browser_action()->page_action_icons();
3129 iter != icon_paths->end(); ++iter) { 3177 for (size_t icon = 0; icon < page_action_icons.size(); icon++) {
3130 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(*iter))); 3178 for (ExtensionIconSet::IconMap::const_iterator iter =
3179 page_action_icons[icon]->map().begin();
3180 iter != page_action_icons[icon]->map().end();
3181 ++iter) {
3182 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(iter->second)));
3183 }
3131 } 3184 }
3132 } 3185 }
3133 3186
3134 return image_paths; 3187 return image_paths;
3135 } 3188 }
3136 3189
3137 GURL Extension::GetFullLaunchURL() const { 3190 GURL Extension::GetFullLaunchURL() const {
3138 return launch_local_path().empty() ? GURL(launch_web_url()) : 3191 return launch_local_path().empty() ? GURL(launch_web_url()) :
3139 url().Resolve(launch_local_path()); 3192 url().Resolve(launch_local_path());
3140 } 3193 }
(...skipping 764 matching lines...) Expand 10 before | Expand all | Expand 10 after
3905 3958
3906 UpdatedExtensionPermissionsInfo::UpdatedExtensionPermissionsInfo( 3959 UpdatedExtensionPermissionsInfo::UpdatedExtensionPermissionsInfo(
3907 const Extension* extension, 3960 const Extension* extension,
3908 const PermissionSet* permissions, 3961 const PermissionSet* permissions,
3909 Reason reason) 3962 Reason reason)
3910 : reason(reason), 3963 : reason(reason),
3911 extension(extension), 3964 extension(extension),
3912 permissions(permissions) {} 3965 permissions(permissions) {}
3913 3966
3914 } // namespace extensions 3967 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698