Home | History | Annotate | Download | only in input_ime
      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/input_ime/input_components_handler.h"
      6 
      7 #include "base/memory/scoped_ptr.h"
      8 #include "base/strings/string_number_conversions.h"
      9 #include "base/strings/string_util.h"
     10 #include "base/strings/utf_string_conversions.h"
     11 #include "base/values.h"
     12 #include "chrome/common/extensions/manifest_url_handler.h"
     13 #include "extensions/common/error_utils.h"
     14 #include "extensions/common/extension.h"
     15 #include "extensions/common/manifest.h"
     16 #include "extensions/common/manifest_constants.h"
     17 
     18 namespace extensions {
     19 
     20 namespace keys = manifest_keys;
     21 namespace errors = manifest_errors;
     22 
     23 InputComponentInfo::InputComponentInfo()
     24     : type(INPUT_COMPONENT_TYPE_NONE),
     25       shortcut_alt(false),
     26       shortcut_ctrl(false),
     27       shortcut_shift(false) {
     28 }
     29 
     30 InputComponentInfo::~InputComponentInfo() {}
     31 
     32 InputComponents::InputComponents() {}
     33 InputComponents::~InputComponents() {}
     34 
     35 // static
     36 const std::vector<InputComponentInfo>* InputComponents::GetInputComponents(
     37     const Extension* extension) {
     38   InputComponents* info = static_cast<InputComponents*>(
     39       extension->GetManifestData(keys::kInputComponents));
     40   return info ? &info->input_components : NULL;
     41 }
     42 
     43 InputComponentsHandler::InputComponentsHandler() {
     44 }
     45 
     46 InputComponentsHandler::~InputComponentsHandler() {
     47 }
     48 
     49 bool InputComponentsHandler::Parse(Extension* extension,
     50                                    base::string16* error) {
     51   scoped_ptr<InputComponents> info(new InputComponents);
     52   const base::ListValue* list_value = NULL;
     53   if (!extension->manifest()->GetList(keys::kInputComponents, &list_value)) {
     54     *error = ASCIIToUTF16(errors::kInvalidInputComponents);
     55     return false;
     56   }
     57   for (size_t i = 0; i < list_value->GetSize(); ++i) {
     58     const base::DictionaryValue* module_value = NULL;
     59     std::string name_str;
     60     InputComponentType type;
     61     std::string id_str;
     62     std::string description_str;
     63     std::set<std::string> languages;
     64     std::set<std::string> layouts;
     65     std::string shortcut_keycode_str;
     66     GURL input_view_url;
     67     bool shortcut_alt = false;
     68     bool shortcut_ctrl = false;
     69     bool shortcut_shift = false;
     70 
     71     if (!list_value->GetDictionary(i, &module_value)) {
     72       *error = ASCIIToUTF16(errors::kInvalidInputComponents);
     73       return false;
     74     }
     75 
     76     // Get input_components[i].name.
     77     if (!module_value->GetString(keys::kName, &name_str)) {
     78       *error = ErrorUtils::FormatErrorMessageUTF16(
     79           errors::kInvalidInputComponentName,
     80           base::IntToString(i));
     81       return false;
     82     }
     83 
     84     // Get input_components[i].type.
     85     std::string type_str;
     86     if (module_value->GetString(keys::kType, &type_str)) {
     87       if (type_str == "ime") {
     88         type = INPUT_COMPONENT_TYPE_IME;
     89       } else {
     90         *error = ErrorUtils::FormatErrorMessageUTF16(
     91             errors::kInvalidInputComponentType,
     92             base::IntToString(i));
     93         return false;
     94       }
     95     } else {
     96       *error = ErrorUtils::FormatErrorMessageUTF16(
     97           errors::kInvalidInputComponentType,
     98           base::IntToString(i));
     99       return false;
    100     }
    101 
    102     // Get input_components[i].id.
    103     if (!module_value->GetString(keys::kId, &id_str)) {
    104       id_str = "";
    105     }
    106 
    107     // Get input_components[i].description.
    108     if (!module_value->GetString(keys::kDescription, &description_str)) {
    109       *error = ErrorUtils::FormatErrorMessageUTF16(
    110           errors::kInvalidInputComponentDescription,
    111           base::IntToString(i));
    112       return false;
    113     }
    114 
    115     // Get input_components[i].language.
    116     // Both string and list of string are allowed to be compatibile with old
    117     // input_ime manifest specification.
    118     const base::Value* language_value = NULL;
    119     if (module_value->Get(keys::kLanguage, &language_value)) {
    120       if (language_value->GetType() == base::Value::TYPE_STRING) {
    121         std::string language_str;
    122         language_value->GetAsString(&language_str);
    123         languages.insert(language_str);
    124       } else if (language_value->GetType() == base::Value::TYPE_LIST) {
    125         const base::ListValue* language_list = NULL;
    126         language_value->GetAsList(&language_list);
    127         for (size_t j = 0; j < language_list->GetSize(); ++j) {
    128           std::string language_str;
    129           if (language_list->GetString(j, &language_str))
    130             languages.insert(language_str);
    131         }
    132       }
    133     }
    134 
    135     // Get input_components[i].layouts.
    136     const base::ListValue* layouts_value = NULL;
    137     if (module_value->GetList(keys::kLayouts, &layouts_value)) {
    138       for (size_t j = 0; j < layouts_value->GetSize(); ++j) {
    139         std::string layout_name_str;
    140         if (!layouts_value->GetString(j, &layout_name_str)) {
    141           *error = ErrorUtils::FormatErrorMessageUTF16(
    142               errors::kInvalidInputComponentLayoutName,
    143               base::IntToString(i), base::IntToString(j));
    144           return false;
    145         }
    146         layouts.insert(layout_name_str);
    147       }
    148     }
    149 
    150     if (module_value->HasKey(keys::kShortcutKey)) {
    151       const DictionaryValue* shortcut_value = NULL;
    152       if (!module_value->GetDictionary(keys::kShortcutKey,
    153           &shortcut_value)) {
    154         *error = ErrorUtils::FormatErrorMessageUTF16(
    155             errors::kInvalidInputComponentShortcutKey,
    156             base::IntToString(i));
    157         return false;
    158       }
    159 
    160       // Get input_components[i].shortcut_keycode.
    161       if (!shortcut_value->GetString(keys::kKeycode, &shortcut_keycode_str)) {
    162         *error = ErrorUtils::FormatErrorMessageUTF16(
    163             errors::kInvalidInputComponentShortcutKeycode,
    164             base::IntToString(i));
    165         return false;
    166       }
    167 
    168       // Get input_components[i].shortcut_alt.
    169       if (!shortcut_value->GetBoolean(keys::kAltKey, &shortcut_alt)) {
    170         shortcut_alt = false;
    171       }
    172 
    173       // Get input_components[i].shortcut_ctrl.
    174       if (!shortcut_value->GetBoolean(keys::kCtrlKey, &shortcut_ctrl)) {
    175         shortcut_ctrl = false;
    176       }
    177 
    178       // Get input_components[i].shortcut_shift.
    179       if (!shortcut_value->GetBoolean(keys::kShiftKey, &shortcut_shift)) {
    180         shortcut_shift = false;
    181       }
    182     }
    183 
    184     // Get input_components[i].input_view_url.
    185     // Note: 'input_view' is optional in manifest.
    186     std::string input_view_str;
    187     if (module_value->GetString(keys::kInputView, &input_view_str)) {
    188       input_view_url = extension->GetResourceURL(input_view_str);
    189       if (!input_view_url.is_valid()) {
    190         *error = ErrorUtils::FormatErrorMessageUTF16(
    191             errors::kInvalidInputView,
    192             base::IntToString(i));
    193         return false;
    194       }
    195     }
    196 
    197     info->input_components.push_back(InputComponentInfo());
    198     info->input_components.back().name = name_str;
    199     info->input_components.back().type = type;
    200     info->input_components.back().id = id_str;
    201     info->input_components.back().description = description_str;
    202     info->input_components.back().languages = languages;
    203     info->input_components.back().layouts.insert(layouts.begin(),
    204         layouts.end());
    205     info->input_components.back().shortcut_keycode = shortcut_keycode_str;
    206     info->input_components.back().shortcut_alt = shortcut_alt;
    207     info->input_components.back().shortcut_ctrl = shortcut_ctrl;
    208     info->input_components.back().shortcut_shift = shortcut_shift;
    209     info->input_components.back().options_page_url =
    210         extensions::ManifestURL::GetOptionsPage(extension);
    211     info->input_components.back().input_view_url = input_view_url;
    212   }
    213   extension->SetManifestData(keys::kInputComponents, info.release());
    214   return true;
    215 }
    216 
    217 const std::vector<std::string>
    218 InputComponentsHandler::PrerequisiteKeys() const {
    219   return SingleKey(keys::kOptionsPage);
    220 }
    221 
    222 const std::vector<std::string> InputComponentsHandler::Keys() const {
    223   return SingleKey(keys::kInputComponents);
    224 }
    225 
    226 }  // namespace extensions
    227