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