| 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_handler_helpers.h" | 15 #include "chrome/common/extensions/manifest_handler_helpers.h" |
| 15 | 16 |
| 16 namespace errors = extension_manifest_errors; | 17 namespace errors = extension_manifest_errors; |
| 17 | 18 |
| 18 namespace extensions { | 19 namespace extensions { |
| 19 | 20 |
| 20 ScriptBadgeHandler::ScriptBadgeHandler() { | 21 ScriptBadgeHandler::ScriptBadgeHandler() { |
| 21 } | 22 } |
| 22 | 23 |
| 23 ScriptBadgeHandler::~ScriptBadgeHandler() { | 24 ScriptBadgeHandler::~ScriptBadgeHandler() { |
| 24 } | 25 } |
| 25 | 26 |
| 26 bool ScriptBadgeHandler::Parse(const base::Value* value, | 27 bool ScriptBadgeHandler::Parse(Extension* extension, string16* error) { |
| 27 Extension* extension, | |
| 28 string16* error) { | |
| 29 scoped_ptr<ActionInfo> action_info(new ActionInfo); | 28 scoped_ptr<ActionInfo> action_info(new ActionInfo); |
| 30 | 29 |
| 30 // Provide a default script badge if one isn't declared in the manifest. |
| 31 if (!extension->manifest()->HasKey(extension_manifest_keys::kScriptBadge)) { |
| 32 SetActionInfoDefaults(extension, action_info.get()); |
| 33 ActionInfo::SetScriptBadgeInfo(extension, action_info.release()); |
| 34 return true; |
| 35 } |
| 36 |
| 31 // So as to not confuse developers if they specify a script badge section | 37 // 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 | 38 // in the manifest, show a warning if the script badge declaration isn't |
| 33 // going to have any effect. | 39 // going to have any effect. |
| 34 if (!FeatureSwitch::script_badges()->IsEnabled()) { | 40 if (!FeatureSwitch::script_badges()->IsEnabled()) { |
| 35 extension->AddInstallWarning( | 41 extension->AddInstallWarning( |
| 36 Extension::InstallWarning(Extension::InstallWarning::FORMAT_TEXT, | 42 Extension::InstallWarning(Extension::InstallWarning::FORMAT_TEXT, |
| 37 errors::kScriptBadgeRequiresFlag)); | 43 errors::kScriptBadgeRequiresFlag)); |
| 38 } | 44 } |
| 39 | 45 |
| 40 const DictionaryValue* dict = NULL; | 46 const DictionaryValue* dict = NULL; |
| 41 if (!value->GetAsDictionary(&dict)) { | 47 if (!extension->manifest()->GetDictionary( |
| 42 *error = ASCIIToUTF16(extension_manifest_errors::kInvalidScriptBadge); | 48 extension_manifest_keys::kScriptBadge, &dict)) { |
| 49 *error = ASCIIToUTF16(errors::kInvalidScriptBadge); |
| 43 return false; | 50 return false; |
| 44 } | 51 } |
| 45 | 52 |
| 46 action_info = | 53 action_info = |
| 47 manifest_handler_helpers::LoadActionInfo(extension, dict, error); | 54 manifest_handler_helpers::LoadActionInfo(extension, dict, error); |
| 48 | 55 |
| 49 if (!action_info.get()) | 56 if (!action_info.get()) |
| 50 return false; // Failed to parse script badge definition. | 57 return false; // Failed to parse script badge definition. |
| 51 | 58 |
| 52 // Script badges always use their extension's title and icon so users can rely | 59 // Script badges always use their extension's title and icon so users can rely |
| (...skipping 12 matching lines...) Expand all Loading... |
| 65 extension->AddInstallWarning( | 72 extension->AddInstallWarning( |
| 66 Extension::InstallWarning(Extension::InstallWarning::FORMAT_TEXT, | 73 Extension::InstallWarning(Extension::InstallWarning::FORMAT_TEXT, |
| 67 errors::kScriptBadgeIconIgnored)); | 74 errors::kScriptBadgeIconIgnored)); |
| 68 } | 75 } |
| 69 | 76 |
| 70 SetActionInfoDefaults(extension, action_info.get()); | 77 SetActionInfoDefaults(extension, action_info.get()); |
| 71 ActionInfo::SetScriptBadgeInfo(extension, action_info.release()); | 78 ActionInfo::SetScriptBadgeInfo(extension, action_info.release()); |
| 72 return true; | 79 return true; |
| 73 } | 80 } |
| 74 | 81 |
| 75 bool ScriptBadgeHandler::HasNoKey(Extension* extension, string16* error) { | 82 bool ScriptBadgeHandler::AlwaysParseForType(Extension::Type type) { |
| 76 scoped_ptr<ActionInfo> action_info(new ActionInfo); | 83 return type == Extension::TYPE_EXTENSION; |
| 77 SetActionInfoDefaults(extension, action_info.get()); | |
| 78 ActionInfo::SetScriptBadgeInfo(extension, action_info.release()); | |
| 79 return true; | |
| 80 } | 84 } |
| 81 | 85 |
| 82 void ScriptBadgeHandler::SetActionInfoDefaults(const Extension* extension, | 86 void ScriptBadgeHandler::SetActionInfoDefaults(const Extension* extension, |
| 83 ActionInfo* info) { | 87 ActionInfo* info) { |
| 84 info->default_title = extension->name(); | 88 info->default_title = extension->name(); |
| 85 info->default_icon.Clear(); | 89 info->default_icon.Clear(); |
| 86 for (size_t i = 0; i < extension_misc::kNumScriptBadgeIconSizes; ++i) { | 90 for (size_t i = 0; i < extension_misc::kNumScriptBadgeIconSizes; ++i) { |
| 87 std::string path = extension->icons().Get( | 91 std::string path = extension->icons().Get( |
| 88 extension_misc::kScriptBadgeIconSizes[i], | 92 extension_misc::kScriptBadgeIconSizes[i], |
| 89 ExtensionIconSet::MATCH_BIGGER); | 93 ExtensionIconSet::MATCH_BIGGER); |
| 90 if (!path.empty()) { | 94 if (!path.empty()) { |
| 91 info->default_icon.Add( | 95 info->default_icon.Add( |
| 92 extension_misc::kScriptBadgeIconSizes[i], path); | 96 extension_misc::kScriptBadgeIconSizes[i], path); |
| 93 } | 97 } |
| 94 } | 98 } |
| 95 } | 99 } |
| 96 | 100 |
| 97 } // namespace extensions | 101 } // namespace extensions |
| OLD | NEW |