| 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/api/extension_action/script_badge_handler.h" | 5 #include "chrome/common/extensions/api/extension_action/script_badge_handler.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
| 11 #include "chrome/common/extensions/extension_constants.h" | 11 #include "chrome/common/extensions/extension_constants.h" |
| 12 #include "chrome/common/extensions/extension_manifest_constants.h" | 12 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 13 #include "chrome/common/extensions/feature_switch.h" | 13 #include "chrome/common/extensions/feature_switch.h" |
| 14 #include "chrome/common/extensions/manifest.h" | 14 #include "chrome/common/extensions/manifest.h" |
| 15 #include "chrome/common/extensions/manifest_handler_helpers.h" | 15 #include "chrome/common/extensions/manifest_handler_helpers.h" |
| 16 #include "extensions/common/install_warning.h" |
| 16 | 17 |
| 17 namespace errors = extension_manifest_errors; | 18 namespace errors = extension_manifest_errors; |
| 18 | 19 |
| 19 namespace extensions { | 20 namespace extensions { |
| 20 | 21 |
| 21 ScriptBadgeHandler::ScriptBadgeHandler() { | 22 ScriptBadgeHandler::ScriptBadgeHandler() { |
| 22 } | 23 } |
| 23 | 24 |
| 24 ScriptBadgeHandler::~ScriptBadgeHandler() { | 25 ScriptBadgeHandler::~ScriptBadgeHandler() { |
| 25 } | 26 } |
| 26 | 27 |
| 27 bool ScriptBadgeHandler::Parse(Extension* extension, string16* error) { | 28 bool ScriptBadgeHandler::Parse(Extension* extension, string16* error) { |
| 28 scoped_ptr<ActionInfo> action_info(new ActionInfo); | 29 scoped_ptr<ActionInfo> action_info(new ActionInfo); |
| 29 | 30 |
| 30 // Provide a default script badge if one isn't declared in the manifest. | 31 // Provide a default script badge if one isn't declared in the manifest. |
| 31 if (!extension->manifest()->HasKey(extension_manifest_keys::kScriptBadge)) { | 32 if (!extension->manifest()->HasKey(extension_manifest_keys::kScriptBadge)) { |
| 32 SetActionInfoDefaults(extension, action_info.get()); | 33 SetActionInfoDefaults(extension, action_info.get()); |
| 33 ActionInfo::SetScriptBadgeInfo(extension, action_info.release()); | 34 ActionInfo::SetScriptBadgeInfo(extension, action_info.release()); |
| 34 return true; | 35 return true; |
| 35 } | 36 } |
| 36 | 37 |
| 37 // So as to not confuse developers if they specify a script badge section | 38 // So as to not confuse developers if they specify a script badge section |
| 38 // in the manifest, show a warning if the script badge declaration isn't | 39 // in the manifest, show a warning if the script badge declaration isn't |
| 39 // going to have any effect. | 40 // going to have any effect. |
| 40 if (!FeatureSwitch::script_badges()->IsEnabled()) { | 41 if (!FeatureSwitch::script_badges()->IsEnabled()) { |
| 41 extension->AddInstallWarning( | 42 extension->AddInstallWarning( |
| 42 Extension::InstallWarning(Extension::InstallWarning::FORMAT_TEXT, | 43 InstallWarning(InstallWarning::FORMAT_TEXT, |
| 43 errors::kScriptBadgeRequiresFlag)); | 44 errors::kScriptBadgeRequiresFlag)); |
| 44 } | 45 } |
| 45 | 46 |
| 46 const DictionaryValue* dict = NULL; | 47 const DictionaryValue* dict = NULL; |
| 47 if (!extension->manifest()->GetDictionary( | 48 if (!extension->manifest()->GetDictionary( |
| 48 extension_manifest_keys::kScriptBadge, &dict)) { | 49 extension_manifest_keys::kScriptBadge, &dict)) { |
| 49 *error = ASCIIToUTF16(errors::kInvalidScriptBadge); | 50 *error = ASCIIToUTF16(errors::kInvalidScriptBadge); |
| 50 return false; | 51 return false; |
| 51 } | 52 } |
| 52 | 53 |
| 53 action_info = | 54 action_info = |
| 54 manifest_handler_helpers::LoadActionInfo(extension, dict, error); | 55 manifest_handler_helpers::LoadActionInfo(extension, dict, error); |
| 55 | 56 |
| 56 if (!action_info.get()) | 57 if (!action_info.get()) |
| 57 return false; // Failed to parse script badge definition. | 58 return false; // Failed to parse script badge definition. |
| 58 | 59 |
| 59 // Script badges always use their extension's title and icon so users can rely | 60 // Script badges always use their extension's title and icon so users can rely |
| 60 // on the visual appearance to know which extension is running. This isn't | 61 // on the visual appearance to know which extension is running. This isn't |
| 61 // bulletproof since an malicious extension could use a different 16x16 icon | 62 // bulletproof since an malicious extension could use a different 16x16 icon |
| 62 // that matches the icon of a trusted extension, and users wouldn't be warned | 63 // that matches the icon of a trusted extension, and users wouldn't be warned |
| 63 // during installation. | 64 // during installation. |
| 64 | 65 |
| 65 if (!action_info->default_title.empty()) { | 66 if (!action_info->default_title.empty()) { |
| 66 extension->AddInstallWarning( | 67 extension->AddInstallWarning( |
| 67 Extension::InstallWarning(Extension::InstallWarning::FORMAT_TEXT, | 68 InstallWarning(InstallWarning::FORMAT_TEXT, |
| 68 errors::kScriptBadgeTitleIgnored)); | 69 errors::kScriptBadgeTitleIgnored)); |
| 69 } | 70 } |
| 70 | 71 |
| 71 if (!action_info->default_icon.empty()) { | 72 if (!action_info->default_icon.empty()) { |
| 72 extension->AddInstallWarning( | 73 extension->AddInstallWarning( |
| 73 Extension::InstallWarning(Extension::InstallWarning::FORMAT_TEXT, | 74 InstallWarning(InstallWarning::FORMAT_TEXT, |
| 74 errors::kScriptBadgeIconIgnored)); | 75 errors::kScriptBadgeIconIgnored)); |
| 75 } | 76 } |
| 76 | 77 |
| 77 SetActionInfoDefaults(extension, action_info.get()); | 78 SetActionInfoDefaults(extension, action_info.get()); |
| 78 ActionInfo::SetScriptBadgeInfo(extension, action_info.release()); | 79 ActionInfo::SetScriptBadgeInfo(extension, action_info.release()); |
| 79 return true; | 80 return true; |
| 80 } | 81 } |
| 81 | 82 |
| 82 bool ScriptBadgeHandler::AlwaysParseForType(Extension::Type type) { | 83 bool ScriptBadgeHandler::AlwaysParseForType(Manifest::Type type) { |
| 83 return type == Extension::TYPE_EXTENSION; | 84 return type == Manifest::TYPE_EXTENSION; |
| 84 } | 85 } |
| 85 | 86 |
| 86 void ScriptBadgeHandler::SetActionInfoDefaults(const Extension* extension, | 87 void ScriptBadgeHandler::SetActionInfoDefaults(const Extension* extension, |
| 87 ActionInfo* info) { | 88 ActionInfo* info) { |
| 88 info->default_title = extension->name(); | 89 info->default_title = extension->name(); |
| 89 info->default_icon.Clear(); | 90 info->default_icon.Clear(); |
| 90 for (size_t i = 0; i < extension_misc::kNumScriptBadgeIconSizes; ++i) { | 91 for (size_t i = 0; i < extension_misc::kNumScriptBadgeIconSizes; ++i) { |
| 91 std::string path = extension->icons().Get( | 92 std::string path = extension->icons().Get( |
| 92 extension_misc::kScriptBadgeIconSizes[i], | 93 extension_misc::kScriptBadgeIconSizes[i], |
| 93 ExtensionIconSet::MATCH_BIGGER); | 94 ExtensionIconSet::MATCH_BIGGER); |
| 94 if (!path.empty()) { | 95 if (!path.empty()) { |
| 95 info->default_icon.Add( | 96 info->default_icon.Add( |
| 96 extension_misc::kScriptBadgeIconSizes[i], path); | 97 extension_misc::kScriptBadgeIconSizes[i], path); |
| 97 } | 98 } |
| 98 } | 99 } |
| 99 } | 100 } |
| 100 | 101 |
| 101 } // namespace extensions | 102 } // namespace extensions |
| OLD | NEW |