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/action_handler_helpers.h
" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/string16.h" |
| 10 #include "base/utf_string_conversions.h" |
| 11 #include "base/values.h" |
| 12 #include "chrome/common/extensions/extension.h" |
| 13 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 14 #include "chrome/common/extensions/manifest_handler_helpers.h" |
| 15 #include "extensions/common/error_utils.h" |
| 16 |
| 17 namespace keys = extension_manifest_keys; |
| 18 namespace errors = extension_manifest_errors; |
| 19 |
| 20 namespace extensions { |
| 21 |
| 22 scoped_ptr<ActionInfo> LoadActionInfo( |
| 23 const Extension* extension, |
| 24 const base::DictionaryValue* extension_action, |
| 25 string16* error) { |
| 26 scoped_ptr<ActionInfo> result(new ActionInfo()); |
| 27 |
| 28 if (extension->manifest_version() == 1) { |
| 29 // kPageActionIcons is obsolete, and used by very few extensions. Continue |
| 30 // loading it, but only take the first icon as the default_icon path. |
| 31 const ListValue* icons = NULL; |
| 32 if (extension_action->HasKey(keys::kPageActionIcons) && |
| 33 extension_action->GetList(keys::kPageActionIcons, &icons)) { |
| 34 for (ListValue::const_iterator iter = icons->begin(); |
| 35 iter != icons->end(); ++iter) { |
| 36 std::string path; |
| 37 if (!(*iter)->GetAsString(&path) || |
| 38 !manifest_handler_helpers::NormalizeAndValidatePath(&path)) { |
| 39 *error = ASCIIToUTF16(errors::kInvalidPageActionIconPath); |
| 40 return scoped_ptr<ActionInfo>(); |
| 41 } |
| 42 |
| 43 result->default_icon.Add(extension_misc::EXTENSION_ICON_ACTION, path); |
| 44 break; |
| 45 } |
| 46 } |
| 47 |
| 48 std::string id; |
| 49 if (extension_action->HasKey(keys::kPageActionId)) { |
| 50 if (!extension_action->GetString(keys::kPageActionId, &id)) { |
| 51 *error = ASCIIToUTF16(errors::kInvalidPageActionId); |
| 52 return scoped_ptr<ActionInfo>(); |
| 53 } |
| 54 result->id = id; |
| 55 } |
| 56 } |
| 57 |
| 58 // Read the page action |default_icon| (optional). |
| 59 // The |default_icon| value can be either dictionary {icon size -> icon path} |
| 60 // or non empty string value. |
| 61 if (extension_action->HasKey(keys::kPageActionDefaultIcon)) { |
| 62 const DictionaryValue* icons_value = NULL; |
| 63 std::string default_icon; |
| 64 if (extension_action->GetDictionary(keys::kPageActionDefaultIcon, |
| 65 &icons_value)) { |
| 66 if (!manifest_handler_helpers::LoadIconsFromDictionary( |
| 67 icons_value, |
| 68 extension_misc::kExtensionActionIconSizes, |
| 69 extension_misc::kNumExtensionActionIconSizes, |
| 70 &result->default_icon, |
| 71 error)) { |
| 72 return scoped_ptr<ActionInfo>(); |
| 73 } |
| 74 } else if (extension_action->GetString(keys::kPageActionDefaultIcon, |
| 75 &default_icon) && |
| 76 manifest_handler_helpers::NormalizeAndValidatePath( |
| 77 &default_icon)) { |
| 78 result->default_icon.Add(extension_misc::EXTENSION_ICON_ACTION, |
| 79 default_icon); |
| 80 } else { |
| 81 *error = ASCIIToUTF16(errors::kInvalidPageActionIconPath); |
| 82 return scoped_ptr<ActionInfo>(); |
| 83 } |
| 84 } |
| 85 |
| 86 // Read the page action title from |default_title| if present, |name| if not |
| 87 // (both optional). |
| 88 if (extension_action->HasKey(keys::kPageActionDefaultTitle)) { |
| 89 if (!extension_action->GetString(keys::kPageActionDefaultTitle, |
| 90 &result->default_title)) { |
| 91 *error = ASCIIToUTF16(errors::kInvalidPageActionDefaultTitle); |
| 92 return scoped_ptr<ActionInfo>(); |
| 93 } |
| 94 } else if (extension->manifest_version() == 1 && |
| 95 extension_action->HasKey(keys::kName)) { |
| 96 if (!extension_action->GetString(keys::kName, &result->default_title)) { |
| 97 *error = ASCIIToUTF16(errors::kInvalidPageActionName); |
| 98 return scoped_ptr<ActionInfo>(); |
| 99 } |
| 100 } |
| 101 |
| 102 // Read the action's |popup| (optional). |
| 103 const char* popup_key = NULL; |
| 104 if (extension_action->HasKey(keys::kPageActionDefaultPopup)) |
| 105 popup_key = keys::kPageActionDefaultPopup; |
| 106 |
| 107 if (extension->manifest_version() == 1 && |
| 108 extension_action->HasKey(keys::kPageActionPopup)) { |
| 109 if (popup_key) { |
| 110 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 111 errors::kInvalidPageActionOldAndNewKeys, |
| 112 keys::kPageActionDefaultPopup, |
| 113 keys::kPageActionPopup); |
| 114 return scoped_ptr<ActionInfo>(); |
| 115 } |
| 116 popup_key = keys::kPageActionPopup; |
| 117 } |
| 118 |
| 119 if (popup_key) { |
| 120 const DictionaryValue* popup = NULL; |
| 121 std::string url_str; |
| 122 |
| 123 if (extension_action->GetString(popup_key, &url_str)) { |
| 124 // On success, |url_str| is set. Nothing else to do. |
| 125 } else if (extension->manifest_version() == 1 && |
| 126 extension_action->GetDictionary(popup_key, &popup)) { |
| 127 if (!popup->GetString(keys::kPageActionPopupPath, &url_str)) { |
| 128 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 129 errors::kInvalidPageActionPopupPath, "<missing>"); |
| 130 return scoped_ptr<ActionInfo>(); |
| 131 } |
| 132 } else { |
| 133 *error = ASCIIToUTF16(errors::kInvalidPageActionPopup); |
| 134 return scoped_ptr<ActionInfo>(); |
| 135 } |
| 136 |
| 137 if (!url_str.empty()) { |
| 138 // An empty string is treated as having no popup. |
| 139 result->default_popup_url = Extension::GetResourceURL(extension->url(), |
| 140 url_str); |
| 141 if (!result->default_popup_url.is_valid()) { |
| 142 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 143 errors::kInvalidPageActionPopupPath, url_str); |
| 144 return scoped_ptr<ActionInfo>(); |
| 145 } |
| 146 } else { |
| 147 DCHECK(result->default_popup_url.is_empty()) |
| 148 << "Shouldn't be possible for the popup to be set."; |
| 149 } |
| 150 } |
| 151 |
| 152 return result.Pass(); |
| 153 } |
| 154 |
| 155 } // namespace extensions |
OLD | NEW |