| 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/input_ime/input_components_handler.h" | 5 #include "chrome/common/extensions/api/input_ime/input_components_handler.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/common/extensions/extension.h" | 12 #include "chrome/common/extensions/extension.h" |
| 13 #include "chrome/common/extensions/extension_manifest_constants.h" | 13 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 14 #include "chrome/common/extensions/manifest.h" |
| 14 #include "extensions/common/error_utils.h" | 15 #include "extensions/common/error_utils.h" |
| 15 | 16 |
| 17 namespace keys = extension_manifest_keys; |
| 18 namespace errors = extension_manifest_errors; |
| 19 |
| 16 namespace extensions { | 20 namespace extensions { |
| 17 | 21 |
| 18 InputComponentInfo::InputComponentInfo() | 22 InputComponentInfo::InputComponentInfo() |
| 19 : type(INPUT_COMPONENT_TYPE_NONE), | 23 : type(INPUT_COMPONENT_TYPE_NONE), |
| 20 shortcut_alt(false), | 24 shortcut_alt(false), |
| 21 shortcut_ctrl(false), | 25 shortcut_ctrl(false), |
| 22 shortcut_shift(false) { | 26 shortcut_shift(false) { |
| 23 } | 27 } |
| 24 | 28 |
| 25 InputComponentInfo::~InputComponentInfo() {} | 29 InputComponentInfo::~InputComponentInfo() {} |
| 26 | 30 |
| 27 InputComponents::InputComponents() {} | 31 InputComponents::InputComponents() {} |
| 28 InputComponents::~InputComponents() {} | 32 InputComponents::~InputComponents() {} |
| 29 | 33 |
| 30 // static | 34 // static |
| 31 const std::vector<InputComponentInfo>* InputComponents::GetInputComponents( | 35 const std::vector<InputComponentInfo>* InputComponents::GetInputComponents( |
| 32 const Extension* extension) { | 36 const Extension* extension) { |
| 33 InputComponents* info = static_cast<InputComponents*>( | 37 InputComponents* info = static_cast<InputComponents*>( |
| 34 extension->GetManifestData(extension_manifest_keys::kInputComponents)); | 38 extension->GetManifestData(keys::kInputComponents)); |
| 35 return info ? &info->input_components : NULL; | 39 return info ? &info->input_components : NULL; |
| 36 } | 40 } |
| 37 | 41 |
| 38 InputComponentsHandler::InputComponentsHandler() { | 42 InputComponentsHandler::InputComponentsHandler() { |
| 39 } | 43 } |
| 40 | 44 |
| 41 InputComponentsHandler::~InputComponentsHandler() { | 45 InputComponentsHandler::~InputComponentsHandler() { |
| 42 } | 46 } |
| 43 | 47 |
| 44 bool InputComponentsHandler::Parse(const base::Value* value, | 48 bool InputComponentsHandler::Parse(Extension* extension, |
| 45 Extension* extension, | |
| 46 string16* error) { | 49 string16* error) { |
| 47 scoped_ptr<InputComponents> info(new InputComponents); | 50 scoped_ptr<InputComponents> info(new InputComponents); |
| 48 const ListValue* list_value = NULL; | 51 const ListValue* list_value = NULL; |
| 49 if (!value->GetAsList(&list_value)) { | 52 if (!extension->manifest()->GetList(keys::kInputComponents, &list_value)) { |
| 50 *error = ASCIIToUTF16(extension_manifest_errors::kInvalidInputComponents); | 53 *error = ASCIIToUTF16(errors::kInvalidInputComponents); |
| 51 return false; | 54 return false; |
| 52 } | 55 } |
| 53 for (size_t i = 0; i < list_value->GetSize(); ++i) { | 56 for (size_t i = 0; i < list_value->GetSize(); ++i) { |
| 54 const DictionaryValue* module_value = NULL; | 57 const DictionaryValue* module_value = NULL; |
| 55 std::string name_str; | 58 std::string name_str; |
| 56 InputComponentType type; | 59 InputComponentType type; |
| 57 std::string id_str; | 60 std::string id_str; |
| 58 std::string description_str; | 61 std::string description_str; |
| 59 std::string language_str; | 62 std::string language_str; |
| 60 std::set<std::string> layouts; | 63 std::set<std::string> layouts; |
| 61 std::string shortcut_keycode_str; | 64 std::string shortcut_keycode_str; |
| 62 bool shortcut_alt = false; | 65 bool shortcut_alt = false; |
| 63 bool shortcut_ctrl = false; | 66 bool shortcut_ctrl = false; |
| 64 bool shortcut_shift = false; | 67 bool shortcut_shift = false; |
| 65 | 68 |
| 66 if (!list_value->GetDictionary(i, &module_value)) { | 69 if (!list_value->GetDictionary(i, &module_value)) { |
| 67 *error = ASCIIToUTF16(extension_manifest_errors::kInvalidInputComponents); | 70 *error = ASCIIToUTF16(errors::kInvalidInputComponents); |
| 68 return false; | 71 return false; |
| 69 } | 72 } |
| 70 | 73 |
| 71 // Get input_components[i].name. | 74 // Get input_components[i].name. |
| 72 if (!module_value->GetString(extension_manifest_keys::kName, &name_str)) { | 75 if (!module_value->GetString(keys::kName, &name_str)) { |
| 73 *error = ErrorUtils::FormatErrorMessageUTF16( | 76 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 74 extension_manifest_errors::kInvalidInputComponentName, | 77 errors::kInvalidInputComponentName, |
| 75 base::IntToString(i)); | 78 base::IntToString(i)); |
| 76 return false; | 79 return false; |
| 77 } | 80 } |
| 78 | 81 |
| 79 // Get input_components[i].type. | 82 // Get input_components[i].type. |
| 80 std::string type_str; | 83 std::string type_str; |
| 81 if (module_value->GetString(extension_manifest_keys::kType, &type_str)) { | 84 if (module_value->GetString(keys::kType, &type_str)) { |
| 82 if (type_str == "ime") { | 85 if (type_str == "ime") { |
| 83 type = INPUT_COMPONENT_TYPE_IME; | 86 type = INPUT_COMPONENT_TYPE_IME; |
| 84 } else { | 87 } else { |
| 85 *error = ErrorUtils::FormatErrorMessageUTF16( | 88 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 86 extension_manifest_errors::kInvalidInputComponentType, | 89 errors::kInvalidInputComponentType, |
| 87 base::IntToString(i)); | 90 base::IntToString(i)); |
| 88 return false; | 91 return false; |
| 89 } | 92 } |
| 90 } else { | 93 } else { |
| 91 *error = ErrorUtils::FormatErrorMessageUTF16( | 94 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 92 extension_manifest_errors::kInvalidInputComponentType, | 95 errors::kInvalidInputComponentType, |
| 93 base::IntToString(i)); | 96 base::IntToString(i)); |
| 94 return false; | 97 return false; |
| 95 } | 98 } |
| 96 | 99 |
| 97 // Get input_components[i].id. | 100 // Get input_components[i].id. |
| 98 if (!module_value->GetString(extension_manifest_keys::kId, &id_str)) { | 101 if (!module_value->GetString(keys::kId, &id_str)) { |
| 99 id_str = ""; | 102 id_str = ""; |
| 100 } | 103 } |
| 101 | 104 |
| 102 // Get input_components[i].description. | 105 // Get input_components[i].description. |
| 103 if (!module_value->GetString(extension_manifest_keys::kDescription, | 106 if (!module_value->GetString(keys::kDescription, &description_str)) { |
| 104 &description_str)) { | |
| 105 *error = ErrorUtils::FormatErrorMessageUTF16( | 107 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 106 extension_manifest_errors::kInvalidInputComponentDescription, | 108 errors::kInvalidInputComponentDescription, |
| 107 base::IntToString(i)); | 109 base::IntToString(i)); |
| 108 return false; | 110 return false; |
| 109 } | 111 } |
| 112 |
| 110 // Get input_components[i].language. | 113 // Get input_components[i].language. |
| 111 if (!module_value->GetString(extension_manifest_keys::kLanguage, | 114 if (!module_value->GetString(keys::kLanguage, &language_str)) { |
| 112 &language_str)) { | |
| 113 language_str = ""; | 115 language_str = ""; |
| 114 } | 116 } |
| 115 | 117 |
| 116 // Get input_components[i].layouts. | 118 // Get input_components[i].layouts. |
| 117 const ListValue* layouts_value = NULL; | 119 const ListValue* layouts_value = NULL; |
| 118 if (!module_value->GetList(extension_manifest_keys::kLayouts, | 120 if (!module_value->GetList(keys::kLayouts, &layouts_value)) { |
| 119 &layouts_value)) { | |
| 120 *error = ASCIIToUTF16( | 121 *error = ASCIIToUTF16( |
| 121 extension_manifest_errors::kInvalidInputComponentLayouts); | 122 errors::kInvalidInputComponentLayouts); |
| 122 return false; | 123 return false; |
| 123 } | 124 } |
| 124 | 125 |
| 125 for (size_t j = 0; j < layouts_value->GetSize(); ++j) { | 126 for (size_t j = 0; j < layouts_value->GetSize(); ++j) { |
| 126 std::string layout_name_str; | 127 std::string layout_name_str; |
| 127 if (!layouts_value->GetString(j, &layout_name_str)) { | 128 if (!layouts_value->GetString(j, &layout_name_str)) { |
| 128 *error = ErrorUtils::FormatErrorMessageUTF16( | 129 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 129 extension_manifest_errors::kInvalidInputComponentLayoutName, | 130 errors::kInvalidInputComponentLayoutName, |
| 130 base::IntToString(i), base::IntToString(j)); | 131 base::IntToString(i), base::IntToString(j)); |
| 131 return false; | 132 return false; |
| 132 } | 133 } |
| 133 layouts.insert(layout_name_str); | 134 layouts.insert(layout_name_str); |
| 134 } | 135 } |
| 135 | 136 |
| 136 if (module_value->HasKey(extension_manifest_keys::kShortcutKey)) { | 137 if (module_value->HasKey(keys::kShortcutKey)) { |
| 137 const DictionaryValue* shortcut_value = NULL; | 138 const DictionaryValue* shortcut_value = NULL; |
| 138 if (!module_value->GetDictionary(extension_manifest_keys::kShortcutKey, | 139 if (!module_value->GetDictionary(keys::kShortcutKey, |
| 139 &shortcut_value)) { | 140 &shortcut_value)) { |
| 140 *error = ErrorUtils::FormatErrorMessageUTF16( | 141 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 141 extension_manifest_errors::kInvalidInputComponentShortcutKey, | 142 errors::kInvalidInputComponentShortcutKey, |
| 142 base::IntToString(i)); | 143 base::IntToString(i)); |
| 143 return false; | 144 return false; |
| 144 } | 145 } |
| 145 | 146 |
| 146 // Get input_components[i].shortcut_keycode. | 147 // Get input_components[i].shortcut_keycode. |
| 147 if (!shortcut_value->GetString(extension_manifest_keys::kKeycode, | 148 if (!shortcut_value->GetString(keys::kKeycode, &shortcut_keycode_str)) { |
| 148 &shortcut_keycode_str)) { | |
| 149 *error = ErrorUtils::FormatErrorMessageUTF16( | 149 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 150 extension_manifest_errors::kInvalidInputComponentShortcutKeycode, | 150 errors::kInvalidInputComponentShortcutKeycode, |
| 151 base::IntToString(i)); | 151 base::IntToString(i)); |
| 152 return false; | 152 return false; |
| 153 } | 153 } |
| 154 | 154 |
| 155 // Get input_components[i].shortcut_alt. | 155 // Get input_components[i].shortcut_alt. |
| 156 if (!shortcut_value->GetBoolean(extension_manifest_keys::kAltKey, | 156 if (!shortcut_value->GetBoolean(keys::kAltKey, &shortcut_alt)) { |
| 157 &shortcut_alt)) { | |
| 158 shortcut_alt = false; | 157 shortcut_alt = false; |
| 159 } | 158 } |
| 160 | 159 |
| 161 // Get input_components[i].shortcut_ctrl. | 160 // Get input_components[i].shortcut_ctrl. |
| 162 if (!shortcut_value->GetBoolean(extension_manifest_keys::kCtrlKey, | 161 if (!shortcut_value->GetBoolean(keys::kCtrlKey, &shortcut_ctrl)) { |
| 163 &shortcut_ctrl)) { | |
| 164 shortcut_ctrl = false; | 162 shortcut_ctrl = false; |
| 165 } | 163 } |
| 166 | 164 |
| 167 // Get input_components[i].shortcut_shift. | 165 // Get input_components[i].shortcut_shift. |
| 168 if (!shortcut_value->GetBoolean(extension_manifest_keys::kShiftKey, | 166 if (!shortcut_value->GetBoolean(keys::kShiftKey, &shortcut_shift)) { |
| 169 &shortcut_shift)) { | |
| 170 shortcut_shift = false; | 167 shortcut_shift = false; |
| 171 } | 168 } |
| 172 } | 169 } |
| 173 | 170 |
| 174 info->input_components.push_back(InputComponentInfo()); | 171 info->input_components.push_back(InputComponentInfo()); |
| 175 info->input_components.back().name = name_str; | 172 info->input_components.back().name = name_str; |
| 176 info->input_components.back().type = type; | 173 info->input_components.back().type = type; |
| 177 info->input_components.back().id = id_str; | 174 info->input_components.back().id = id_str; |
| 178 info->input_components.back().description = description_str; | 175 info->input_components.back().description = description_str; |
| 179 info->input_components.back().language = language_str; | 176 info->input_components.back().language = language_str; |
| 180 info->input_components.back().layouts.insert(layouts.begin(), | 177 info->input_components.back().layouts.insert(layouts.begin(), |
| 181 layouts.end()); | 178 layouts.end()); |
| 182 info->input_components.back().shortcut_keycode = shortcut_keycode_str; | 179 info->input_components.back().shortcut_keycode = shortcut_keycode_str; |
| 183 info->input_components.back().shortcut_alt = shortcut_alt; | 180 info->input_components.back().shortcut_alt = shortcut_alt; |
| 184 info->input_components.back().shortcut_ctrl = shortcut_ctrl; | 181 info->input_components.back().shortcut_ctrl = shortcut_ctrl; |
| 185 info->input_components.back().shortcut_shift = shortcut_shift; | 182 info->input_components.back().shortcut_shift = shortcut_shift; |
| 186 } | 183 } |
| 187 extension->SetManifestData(extension_manifest_keys::kInputComponents, | 184 extension->SetManifestData(keys::kInputComponents, info.release()); |
| 188 info.release()); | |
| 189 return true; | 185 return true; |
| 190 } | 186 } |
| 191 | 187 |
| 192 } // namespace extensions | 188 } // namespace extensions |
| OLD | NEW |