summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_input_ime_api.cc
diff options
context:
space:
mode:
authorzork@chromium.org <zork@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-12 07:01:27 +0000
committerzork@chromium.org <zork@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-12 07:01:27 +0000
commit817925aaf716cb623c56e21030650f075e510157 (patch)
tree7fa202867b8025e0556622a5c48143ec256860f3 /chrome/browser/extensions/extension_input_ime_api.cc
parentc24e5d45e49e3241332b7df30d925ba684d35b61 (diff)
downloadchromium_src-817925aaf716cb623c56e21030650f075e510157.zip
chromium_src-817925aaf716cb623c56e21030650f075e510157.tar.gz
chromium_src-817925aaf716cb623c56e21030650f075e510157.tar.bz2
Implement SetMenuItems and UpdateMenuItems in the IME API
BUG=chromium-os:16090 TEST=Manual Review URL: http://codereview.chromium.org/8073022 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@105027 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/extension_input_ime_api.cc')
-rw-r--r--chrome/browser/extensions/extension_input_ime_api.cc301
1 files changed, 234 insertions, 67 deletions
diff --git a/chrome/browser/extensions/extension_input_ime_api.cc b/chrome/browser/extensions/extension_input_ime_api.cc
index 13e59b5..098a894 100644
--- a/chrome/browser/extensions/extension_input_ime_api.cc
+++ b/chrome/browser/extensions/extension_input_ime_api.cc
@@ -14,6 +14,157 @@
namespace keys = extension_input_module_constants;
+namespace {
+
+const char kStyleNone[] = "none";
+const char kStyleCheck[] = "check";
+const char kStyleRadio[] = "radio";
+const char kStyleSeparator[] = "separator";
+
+const char kErrorEngineNotAvailable[] = "Engine is not available";
+const char kErrorBadCandidateList[] = "Invalid candidate list provided";
+const char kErrorSetMenuItemsFail[] = "Could not create menu Items";
+const char kErrorUpdateMenuItemsFail[] = "Could not update menu Items";
+
+bool ReadMenuItems(
+ ListValue* menu_items,
+ std::vector<chromeos::InputMethodEngine::MenuItem>* output) {
+ for (size_t i = 0; i < menu_items->GetSize(); ++i) {
+ DictionaryValue* item_dict;
+ if (!menu_items->GetDictionary(i, &item_dict)) {
+ return false;
+ }
+
+ std::string id;
+ std::string label;
+ int style = chromeos::InputMethodEngine::MENU_ITEM_STYLE_NONE;
+ bool visible = true;
+ bool enabled = true;
+ bool checked = false;
+ std::string icon;
+ chromeos::InputMethodEngine::KeyboardEvent shortcut_key;
+
+ unsigned int modified = 0;
+
+ if (!item_dict->GetString(keys::kIdKey, &id)) {
+ return false;
+ }
+
+ if (item_dict->HasKey(keys::kLabelKey)) {
+ if (!item_dict->GetString(keys::kLabelKey, &label)) {
+ return false;
+ }
+ modified |= chromeos::InputMethodEngine::MENU_ITEM_MODIFIED_LABEL;
+ }
+ if (item_dict->HasKey(keys::kStyleKey)) {
+ std::string style_string;
+ if (!item_dict->GetString(keys::kStyleKey, &style_string)) {
+ return false;
+ }
+
+ if (style_string == kStyleNone) {
+ style = chromeos::InputMethodEngine::MENU_ITEM_STYLE_NONE;
+ } else if (style_string == kStyleCheck) {
+ style = chromeos::InputMethodEngine::MENU_ITEM_STYLE_CHECK;
+ } else if (style_string == kStyleRadio) {
+ style = chromeos::InputMethodEngine::MENU_ITEM_STYLE_RADIO;
+ } else if (style_string == kStyleSeparator) {
+ style = chromeos::InputMethodEngine::MENU_ITEM_STYLE_SEPARATOR;
+ } else {
+ return false;
+ }
+ modified |= chromeos::InputMethodEngine::MENU_ITEM_MODIFIED_STYLE;
+ }
+
+ if (item_dict->HasKey(keys::kVisibleKey)) {
+ if (!item_dict->GetBoolean(keys::kVisibleKey, &visible)) {
+ return false;
+ }
+ modified |= chromeos::InputMethodEngine::MENU_ITEM_MODIFIED_VISIBLE;
+ }
+
+ if (item_dict->HasKey(keys::kCheckedKey)) {
+ if (!item_dict->GetBoolean(keys::kCheckedKey, &checked)) {
+ return false;
+ }
+ modified |= chromeos::InputMethodEngine::MENU_ITEM_MODIFIED_CHECKED;
+ }
+
+ if (item_dict->HasKey(keys::kEnabledKey)) {
+ if (!item_dict->GetBoolean(keys::kEnabledKey, &enabled)) {
+ return false;
+ }
+ modified |= chromeos::InputMethodEngine::MENU_ITEM_MODIFIED_ENABLED;
+ }
+
+ if (item_dict->HasKey(keys::kIconKey)) {
+ if (!item_dict->GetString(keys::kIconKey, &icon)) {
+ return false;
+ }
+ modified |= chromeos::InputMethodEngine::MENU_ITEM_MODIFIED_ICON;
+ }
+
+ if (item_dict->HasKey(keys::kShortcutKey)) {
+ DictionaryValue* shortcut_dict;
+ if (!item_dict->GetDictionary(keys::kShortcutKey, &shortcut_dict)) {
+ return false;
+ }
+
+ if (!shortcut_dict->GetString(keys::kKeyKey, &(shortcut_key.key))) {
+ return false;
+ }
+
+ if (shortcut_dict->HasKey(keys::kAltKeyKey)) {
+ if (!item_dict->GetBoolean(keys::kAltKeyKey, &(shortcut_key.alt_key))) {
+ return false;
+ }
+ }
+
+ if (shortcut_dict->HasKey(keys::kCtrlKeyKey)) {
+ if (!shortcut_dict->GetBoolean(keys::kCtrlKeyKey,
+ &(shortcut_key.ctrl_key))) {
+ return false;
+ }
+ }
+
+ if (shortcut_dict->HasKey(keys::kShiftKeyKey)) {
+ if (!shortcut_dict->GetBoolean(keys::kShiftKeyKey,
+ &(shortcut_key.shift_key))) {
+ return false;
+ }
+ }
+ modified |= chromeos::InputMethodEngine::MENU_ITEM_MODIFIED_SHORTCUT_KEY;
+ }
+
+ output->push_back(chromeos::InputMethodEngine::MenuItem());
+ output->back().id = id;
+ output->back().label = label;
+ output->back().style = style;
+ output->back().visible = visible;
+ output->back().enabled = enabled;
+ output->back().checked = checked;
+ output->back().icon = icon;
+ output->back().shortcut_key = shortcut_key;
+
+ output->back().modified = modified;
+
+ if (item_dict->HasKey(keys::kItemsKey)) {
+ ListValue* sub_list;
+ if (!item_dict->GetList(keys::kItemsKey, &sub_list)) {
+ return false;
+ }
+
+ if (!ReadMenuItems(sub_list, &(output->back().children))) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
+
+}
+
namespace events {
const char kOnActivate[] = "experimental.input.onActivate";
@@ -343,9 +494,7 @@ bool SetCompositionFunction::RunImpl() {
ExtensionInputImeEventRouter::GetInstance()->
GetActiveEngine(extension_id());
if (!engine) {
- if (has_callback()) {
- result_.reset(Value::CreateBooleanValue(false));
- }
+ result_.reset(Value::CreateBooleanValue(false));
return true;
}
@@ -374,6 +523,7 @@ bool SetCompositionFunction::RunImpl() {
}
if (args->HasKey(keys::kSegmentsKey)) {
+ // TODO: Handle segments
ListValue* segment_list;
EXTENSION_FUNCTION_VALIDATE(args->GetList(keys::kSegmentsKey,
&segment_list));
@@ -398,20 +548,13 @@ bool SetCompositionFunction::RunImpl() {
}
}
- std::string error;
-
if (engine->SetComposition(context_id, text.c_str(), selection_start,
- selection_end, segments, &error)) {
- if (has_callback()) {
- result_.reset(Value::CreateBooleanValue(true));
- }
- return true;
+ selection_end, segments, &error_)) {
+ result_.reset(Value::CreateBooleanValue(true));
} else {
- if (has_callback()) {
- result_.reset(Value::CreateBooleanValue(false));
- }
- return false;
+ result_.reset(Value::CreateBooleanValue(false));
}
+ return true;
}
bool ClearCompositionFunction::RunImpl() {
@@ -419,7 +562,8 @@ bool ClearCompositionFunction::RunImpl() {
ExtensionInputImeEventRouter::GetInstance()->
GetActiveEngine(extension_id());
if (!engine) {
- return false;
+ result_.reset(Value::CreateBooleanValue(false));
+ return true;
}
DictionaryValue* args;
@@ -429,18 +573,12 @@ bool ClearCompositionFunction::RunImpl() {
EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kContextIdKey,
&context_id));
- std::string error;
- if (engine->ClearComposition(context_id, &error)) {
- if (has_callback()) {
- result_.reset(Value::CreateBooleanValue(true));
- }
- return true;
+ if (engine->ClearComposition(context_id, &error_)) {
+ result_.reset(Value::CreateBooleanValue(true));
} else {
- if (has_callback()) {
- result_.reset(Value::CreateBooleanValue(false));
- }
- return false;
+ result_.reset(Value::CreateBooleanValue(false));
}
+ return true;
}
bool CommitTextFunction::RunImpl() {
@@ -449,7 +587,8 @@ bool CommitTextFunction::RunImpl() {
ExtensionInputImeEventRouter::GetInstance()->
GetActiveEngine(extension_id());
if (!engine) {
- return false;
+ result_.reset(Value::CreateBooleanValue(false));
+ return true;
}
DictionaryValue* args;
@@ -461,18 +600,12 @@ bool CommitTextFunction::RunImpl() {
&context_id));
EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kTextKey, &text));
- std::string error;
- if (engine->CommitText(context_id, text.c_str(), &error)) {
- if (has_callback()) {
- result_.reset(Value::CreateBooleanValue(true));
- }
- return true;
+ if (engine->CommitText(context_id, text.c_str(), &error_)) {
+ result_.reset(Value::CreateBooleanValue(true));
} else {
- if (has_callback()) {
- result_.reset(Value::CreateBooleanValue(false));
- }
- return false;
+ result_.reset(Value::CreateBooleanValue(false));
}
+ return true;
}
bool SetCandidateWindowPropertiesFunction::RunImpl() {
@@ -486,21 +619,21 @@ bool SetCandidateWindowPropertiesFunction::RunImpl() {
ExtensionInputImeEventRouter::GetInstance()->GetEngine(extension_id(),
engine_id);
if (!engine) {
- return false;
+ result_.reset(Value::CreateBooleanValue(false));
+ return true;
}
DictionaryValue* properties;
EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(keys::kPropertiesKey,
&properties));
- std::string error;
-
if (properties->HasKey(keys::kVisibleKey)) {
bool visible;
EXTENSION_FUNCTION_VALIDATE(properties->GetBoolean(keys::kVisibleKey,
&visible));
- if (!engine->SetCandidateWindowVisible(visible, &error)) {
- return false;
+ if (!engine->SetCandidateWindowVisible(visible, &error_)) {
+ result_.reset(Value::CreateBooleanValue(false));
+ return true;
}
}
@@ -540,9 +673,7 @@ bool SetCandidateWindowPropertiesFunction::RunImpl() {
engine->SetCandidateWindowAuxTextVisible(visible);
}
- if (has_callback()) {
- result_.reset(Value::CreateBooleanValue(true));
- }
+ result_.reset(Value::CreateBooleanValue(true));
return true;
}
@@ -585,6 +716,7 @@ bool SetCandidatesFunction::ReadCandidates(
EXTENSION_FUNCTION_VALIDATE(candidate_dict->GetList(keys::kCandidatesKey,
&sub_list));
if (!ReadCandidates(sub_list, &(output->back().candidates))) {
+ error_ = kErrorBadCandidateList;
return false;
}
}
@@ -598,7 +730,8 @@ bool SetCandidatesFunction::RunImpl() {
ExtensionInputImeEventRouter::GetInstance()->
GetActiveEngine(extension_id());
if (!engine) {
- return false;
+ result_.reset(Value::CreateBooleanValue(false));
+ return true;
}
DictionaryValue* args;
@@ -614,21 +747,17 @@ bool SetCandidatesFunction::RunImpl() {
EXTENSION_FUNCTION_VALIDATE(args->GetList(keys::kCandidatesKey,
&candidate_list));
if (!ReadCandidates(candidate_list, &candidates)) {
+ error_ = kErrorBadCandidateList;
return false;
}
std::string error;
- if (engine->SetCandidates(context_id, candidates, &error)) {
- if (has_callback()) {
- result_.reset(Value::CreateBooleanValue(true));
- }
- return true;
+ if (engine->SetCandidates(context_id, candidates, &error_)) {
+ result_.reset(Value::CreateBooleanValue(true));
} else {
- if (has_callback()) {
- result_.reset(Value::CreateBooleanValue(false));
- }
- return false;
+ result_.reset(Value::CreateBooleanValue(false));
}
+ return true;
}
bool SetCursorPositionFunction::RunImpl() {
@@ -636,7 +765,8 @@ bool SetCursorPositionFunction::RunImpl() {
ExtensionInputImeEventRouter::GetInstance()->
GetActiveEngine(extension_id());
if (!engine) {
- return false;
+ result_.reset(Value::CreateBooleanValue(false));
+ return true;
}
DictionaryValue* args;
@@ -649,28 +779,65 @@ bool SetCursorPositionFunction::RunImpl() {
EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kCandidateIdKey,
&candidate_id));
- std::string error;
- if (engine->SetCursorPosition(context_id, candidate_id, &error)) {
- if (has_callback()) {
- result_.reset(Value::CreateBooleanValue(true));
- }
- return true;
+ if (engine->SetCursorPosition(context_id, candidate_id, &error_)) {
+ result_.reset(Value::CreateBooleanValue(true));
} else {
- if (has_callback()) {
- result_.reset(Value::CreateBooleanValue(false));
- }
- return false;
+ result_.reset(Value::CreateBooleanValue(false));
}
return true;
}
bool SetMenuItemsFunction::RunImpl() {
- // TODO
+ DictionaryValue* args;
+ EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args));
+
+ std::string engine_id;
+ EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kEngineIdKey, &engine_id));
+
+ chromeos::InputMethodEngine* engine =
+ ExtensionInputImeEventRouter::GetInstance()->GetEngine(extension_id(),
+ engine_id);
+ if (!engine) {
+ error_ = kErrorEngineNotAvailable;
+ return false;
+ }
+
+ ListValue* items;
+ EXTENSION_FUNCTION_VALIDATE(args->GetList(keys::kItemsKey, &items));
+
+ std::vector<chromeos::InputMethodEngine::MenuItem> menu_items;
+ EXTENSION_FUNCTION_VALIDATE(ReadMenuItems(items, &menu_items));
+
+ if (!engine->SetMenuItems(menu_items)) {
+ error_ = kErrorSetMenuItemsFail;
+ }
return true;
}
bool UpdateMenuItemsFunction::RunImpl() {
- // TODO
+ DictionaryValue* args;
+ EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args));
+
+ std::string engine_id;
+ EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kEngineIdKey, &engine_id));
+
+ chromeos::InputMethodEngine* engine =
+ ExtensionInputImeEventRouter::GetInstance()->GetEngine(extension_id(),
+ engine_id);
+ if (!engine) {
+ error_ = kErrorEngineNotAvailable;
+ return false;
+ }
+
+ ListValue* items;
+ EXTENSION_FUNCTION_VALIDATE(args->GetList(keys::kItemsKey, &items));
+
+ std::vector<chromeos::InputMethodEngine::MenuItem> menu_items;
+ EXTENSION_FUNCTION_VALIDATE(ReadMenuItems(items, &menu_items));
+
+ if (!engine->UpdateMenuItems(menu_items)) {
+ error_ = kErrorUpdateMenuItemsFail;
+ }
return true;
}