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/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 Loading... |
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 673 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
816 if (extension_action->HasKey(keys::kPageActionIcons) && | 850 if (extension_action->HasKey(keys::kPageActionIcons) && |
817 extension_action->GetList(keys::kPageActionIcons, &icons)) { | 851 extension_action->GetList(keys::kPageActionIcons, &icons)) { |
818 for (ListValue::const_iterator iter = icons->begin(); | 852 for (ListValue::const_iterator iter = icons->begin(); |
819 iter != icons->end(); ++iter) { | 853 iter != icons->end(); ++iter) { |
820 std::string path; | 854 std::string path; |
821 if (!(*iter)->GetAsString(&path) || path.empty()) { | 855 if (!(*iter)->GetAsString(&path) || path.empty()) { |
822 *error = ASCIIToUTF16(errors::kInvalidPageActionIconPath); | 856 *error = ASCIIToUTF16(errors::kInvalidPageActionIconPath); |
823 return scoped_ptr<ExtensionAction>(); | 857 return scoped_ptr<ExtensionAction>(); |
824 } | 858 } |
825 | 859 |
826 result->set_default_icon_path(path); | 860 scoped_ptr<ExtensionIconSet> icon_set(new ExtensionIconSet); |
| 861 icon_set->Add(extension_misc::EXTENSION_ICON_ACTION, path); |
| 862 result->set_default_icon(icon_set.Pass()); |
827 break; | 863 break; |
828 } | 864 } |
829 } | 865 } |
830 | 866 |
831 std::string id; | 867 std::string id; |
832 if (extension_action->HasKey(keys::kPageActionId)) { | 868 if (extension_action->HasKey(keys::kPageActionId)) { |
833 if (!extension_action->GetString(keys::kPageActionId, &id)) { | 869 if (!extension_action->GetString(keys::kPageActionId, &id)) { |
834 *error = ASCIIToUTF16(errors::kInvalidPageActionId); | 870 *error = ASCIIToUTF16(errors::kInvalidPageActionId); |
835 return scoped_ptr<ExtensionAction>(); | 871 return scoped_ptr<ExtensionAction>(); |
836 } | 872 } |
837 result->set_id(id); | 873 result->set_id(id); |
838 } | 874 } |
839 } | 875 } |
840 | 876 |
841 std::string default_icon; | |
842 // Read the page action |default_icon| (optional). | 877 // Read the page action |default_icon| (optional). |
| 878 // The |default_icon| value can be either dictionary {icon size -> icon path} |
| 879 // or non empty string value. |
843 if (extension_action->HasKey(keys::kPageActionDefaultIcon)) { | 880 if (extension_action->HasKey(keys::kPageActionDefaultIcon)) { |
844 if (!extension_action->GetString(keys::kPageActionDefaultIcon, | 881 const DictionaryValue* icons_value = NULL; |
845 &default_icon) || | 882 std::string default_icon; |
846 default_icon.empty()) { | 883 if (extension_action->GetDictionary(keys::kPageActionDefaultIcon, |
| 884 &icons_value)) { |
| 885 scoped_ptr<ExtensionIconSet> default_icons(new ExtensionIconSet()); |
| 886 if (!LoadIconsFromDictionary(icons_value, |
| 887 extension_misc::kExtensionActionIconSizes, |
| 888 extension_misc::kNumExtensionActionIconSizes, |
| 889 default_icons.get(), |
| 890 error)) { |
| 891 return scoped_ptr<ExtensionAction>(); |
| 892 } |
| 893 |
| 894 result->set_default_icon(default_icons.Pass()); |
| 895 } else if (extension_action->GetString(keys::kPageActionDefaultIcon, |
| 896 &default_icon) && |
| 897 !default_icon.empty()) { |
| 898 scoped_ptr<ExtensionIconSet> icon_set(new ExtensionIconSet); |
| 899 icon_set->Add(extension_misc::EXTENSION_ICON_ACTION, default_icon); |
| 900 result->set_default_icon(icon_set.Pass()); |
| 901 } else { |
847 *error = ASCIIToUTF16(errors::kInvalidPageActionIconPath); | 902 *error = ASCIIToUTF16(errors::kInvalidPageActionIconPath); |
848 return scoped_ptr<ExtensionAction>(); | 903 return scoped_ptr<ExtensionAction>(); |
849 } | 904 } |
850 result->set_default_icon_path(default_icon); | |
851 } | 905 } |
852 | 906 |
853 // Read the page action title from |default_title| if present, |name| if not | 907 // Read the page action title from |default_title| if present, |name| if not |
854 // (both optional). | 908 // (both optional). |
855 std::string title; | 909 std::string title; |
856 if (extension_action->HasKey(keys::kPageActionDefaultTitle)) { | 910 if (extension_action->HasKey(keys::kPageActionDefaultTitle)) { |
857 if (!extension_action->GetString(keys::kPageActionDefaultTitle, &title)) { | 911 if (!extension_action->GetString(keys::kPageActionDefaultTitle, &title)) { |
858 *error = ASCIIToUTF16(errors::kInvalidPageActionDefaultTitle); | 912 *error = ASCIIToUTF16(errors::kInvalidPageActionDefaultTitle); |
859 return scoped_ptr<ExtensionAction>(); | 913 return scoped_ptr<ExtensionAction>(); |
860 } | 914 } |
(...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1368 | 1422 |
1369 bool Extension::LoadIcons(string16* error) { | 1423 bool Extension::LoadIcons(string16* error) { |
1370 if (!manifest_->HasKey(keys::kIcons)) | 1424 if (!manifest_->HasKey(keys::kIcons)) |
1371 return true; | 1425 return true; |
1372 DictionaryValue* icons_value = NULL; | 1426 DictionaryValue* icons_value = NULL; |
1373 if (!manifest_->GetDictionary(keys::kIcons, &icons_value)) { | 1427 if (!manifest_->GetDictionary(keys::kIcons, &icons_value)) { |
1374 *error = ASCIIToUTF16(errors::kInvalidIcons); | 1428 *error = ASCIIToUTF16(errors::kInvalidIcons); |
1375 return false; | 1429 return false; |
1376 } | 1430 } |
1377 | 1431 |
1378 for (size_t i = 0; i < extension_misc::kNumExtensionIconSizes; ++i) { | 1432 return LoadIconsFromDictionary(icons_value, |
1379 std::string key = base::IntToString(extension_misc::kExtensionIconSizes[i]); | 1433 extension_misc::kExtensionIconSizes, |
1380 if (icons_value->HasKey(key)) { | 1434 extension_misc::kNumExtensionIconSizes, |
1381 std::string icon_path; | 1435 &icons_, |
1382 if (!icons_value->GetString(key, &icon_path)) { | 1436 error); |
1383 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( | |
1384 errors::kInvalidIconPath, key); | |
1385 return false; | |
1386 } | |
1387 | |
1388 if (!icon_path.empty() && icon_path[0] == '/') | |
1389 icon_path = icon_path.substr(1); | |
1390 | |
1391 if (icon_path.empty()) { | |
1392 *error = ExtensionErrorUtils::FormatErrorMessageUTF16( | |
1393 errors::kInvalidIconPath, key); | |
1394 return false; | |
1395 } | |
1396 icons_.Add(extension_misc::kExtensionIconSizes[i], icon_path); | |
1397 } | |
1398 } | |
1399 return true; | |
1400 } | 1437 } |
1401 | 1438 |
1402 bool Extension::LoadCommands(string16* error) { | 1439 bool Extension::LoadCommands(string16* error) { |
1403 if (manifest_->HasKey(keys::kCommands)) { | 1440 if (manifest_->HasKey(keys::kCommands)) { |
1404 DictionaryValue* commands = NULL; | 1441 DictionaryValue* commands = NULL; |
1405 if (!manifest_->GetDictionary(keys::kCommands, &commands)) { | 1442 if (!manifest_->GetDictionary(keys::kCommands, &commands)) { |
1406 *error = ASCIIToUTF16(errors::kInvalidCommandsKey); | 1443 *error = ASCIIToUTF16(errors::kInvalidCommandsKey); |
1407 return false; | 1444 return false; |
1408 } | 1445 } |
1409 | 1446 |
(...skipping 952 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2362 // that matches the icon of a trusted extension, and users wouldn't be warned | 2399 // that matches the icon of a trusted extension, and users wouldn't be warned |
2363 // during installation. | 2400 // during installation. |
2364 | 2401 |
2365 if (!script_badge_->GetTitle(ExtensionAction::kDefaultTabId).empty()) { | 2402 if (!script_badge_->GetTitle(ExtensionAction::kDefaultTabId).empty()) { |
2366 install_warnings_.push_back( | 2403 install_warnings_.push_back( |
2367 InstallWarning(InstallWarning::FORMAT_TEXT, | 2404 InstallWarning(InstallWarning::FORMAT_TEXT, |
2368 errors::kScriptBadgeTitleIgnored)); | 2405 errors::kScriptBadgeTitleIgnored)); |
2369 } | 2406 } |
2370 script_badge_->SetTitle(ExtensionAction::kDefaultTabId, name()); | 2407 script_badge_->SetTitle(ExtensionAction::kDefaultTabId, name()); |
2371 | 2408 |
2372 if (!script_badge_->default_icon_path().empty()) { | 2409 if (script_badge_->default_icon()) { |
2373 install_warnings_.push_back( | 2410 install_warnings_.push_back( |
2374 InstallWarning(InstallWarning::FORMAT_TEXT, | 2411 InstallWarning(InstallWarning::FORMAT_TEXT, |
2375 errors::kScriptBadgeIconIgnored)); | 2412 errors::kScriptBadgeIconIgnored)); |
2376 } | 2413 } |
2377 std::string icon16_path = icons().Get(extension_misc::EXTENSION_ICON_BITTY, | 2414 |
2378 ExtensionIconSet::MATCH_EXACTLY); | 2415 scoped_ptr<ExtensionIconSet> icon_set(new ExtensionIconSet); |
2379 if (!icon16_path.empty()) { | 2416 |
2380 script_badge_->set_default_icon_path(icon16_path); | 2417 for (size_t i = 0; i < extension_misc::kNumScriptBadgeIconSizes; i++) { |
| 2418 std::string path = icons().Get(extension_misc::kScriptBadgeIconSizes[i], |
| 2419 ExtensionIconSet::MATCH_EXACTLY); |
| 2420 if (!path.empty()) { |
| 2421 icon_set->Add(extension_misc::kScriptBadgeIconSizes[i], path); |
| 2422 } |
| 2423 } |
| 2424 |
| 2425 if (!icon_set->map().empty()) { |
| 2426 script_badge_->set_default_icon(icon_set.Pass()); |
2381 } else { | 2427 } else { |
2382 script_badge_->SetIcon( | 2428 script_badge_->set_default_icon(scoped_ptr<ExtensionIconSet>()); |
2383 ExtensionAction::kDefaultTabId, | |
2384 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
2385 IDR_EXTENSIONS_FAVICON)); | |
2386 } | 2429 } |
2387 | 2430 |
2388 return true; | 2431 return true; |
2389 } | 2432 } |
2390 | 2433 |
2391 bool Extension::LoadFileBrowserHandlers(string16* error) { | 2434 bool Extension::LoadFileBrowserHandlers(string16* error) { |
2392 if (!manifest_->HasKey(keys::kFileBrowserHandlers)) | 2435 if (!manifest_->HasKey(keys::kFileBrowserHandlers)) |
2393 return true; | 2436 return true; |
2394 ListValue* file_browser_handlers_value = NULL; | 2437 ListValue* file_browser_handlers_value = NULL; |
2395 if (!manifest_->GetList(keys::kFileBrowserHandlers, | 2438 if (!manifest_->GetList(keys::kFileBrowserHandlers, |
(...skipping 797 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3193 DictionaryValue* theme_images = GetThemeImages(); | 3236 DictionaryValue* theme_images = GetThemeImages(); |
3194 if (theme_images) { | 3237 if (theme_images) { |
3195 for (DictionaryValue::key_iterator it = theme_images->begin_keys(); | 3238 for (DictionaryValue::key_iterator it = theme_images->begin_keys(); |
3196 it != theme_images->end_keys(); ++it) { | 3239 it != theme_images->end_keys(); ++it) { |
3197 std::string val; | 3240 std::string val; |
3198 if (theme_images->GetStringWithoutPathExpansion(*it, &val)) | 3241 if (theme_images->GetStringWithoutPathExpansion(*it, &val)) |
3199 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(val))); | 3242 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(val))); |
3200 } | 3243 } |
3201 } | 3244 } |
3202 | 3245 |
3203 // Page action icons. | |
3204 if (page_action()) { | 3246 if (page_action()) { |
3205 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide( | 3247 for (ExtensionIconSet::IconMap::const_iterator iter = |
3206 page_action()->default_icon_path()))); | 3248 page_action()->default_icon()->map().begin(); |
| 3249 iter != page_action()->default_icon()->map().end(); |
| 3250 ++iter) { |
| 3251 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(iter->second))); |
| 3252 } |
3207 } | 3253 } |
3208 | 3254 |
3209 // Browser action icons. | |
3210 if (browser_action()) { | 3255 if (browser_action()) { |
3211 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide( | 3256 for (ExtensionIconSet::IconMap::const_iterator iter = |
3212 browser_action()->default_icon_path()))); | 3257 browser_action()->default_icon()->map().begin(); |
| 3258 iter != browser_action()->default_icon()->map().end(); |
| 3259 ++iter) { |
| 3260 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(iter->second))); |
| 3261 } |
3213 } | 3262 } |
3214 | 3263 |
3215 return image_paths; | 3264 return image_paths; |
3216 } | 3265 } |
3217 | 3266 |
3218 GURL Extension::GetFullLaunchURL() const { | 3267 GURL Extension::GetFullLaunchURL() const { |
3219 return launch_local_path().empty() ? GURL(launch_web_url()) : | 3268 return launch_local_path().empty() ? GURL(launch_web_url()) : |
3220 url().Resolve(launch_local_path()); | 3269 url().Resolve(launch_local_path()); |
3221 } | 3270 } |
3222 | 3271 |
(...skipping 739 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3962 | 4011 |
3963 UpdatedExtensionPermissionsInfo::UpdatedExtensionPermissionsInfo( | 4012 UpdatedExtensionPermissionsInfo::UpdatedExtensionPermissionsInfo( |
3964 const Extension* extension, | 4013 const Extension* extension, |
3965 const PermissionSet* permissions, | 4014 const PermissionSet* permissions, |
3966 Reason reason) | 4015 Reason reason) |
3967 : reason(reason), | 4016 : reason(reason), |
3968 extension(extension), | 4017 extension(extension), |
3969 permissions(permissions) {} | 4018 permissions(permissions) {} |
3970 | 4019 |
3971 } // namespace extensions | 4020 } // namespace extensions |
OLD | NEW |