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