| OLD | NEW |
| 1 // Copyright 2013 Google Inc. | 1 // Copyright 2013 Google Inc. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not | 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 // use this file except in compliance with the License. You may obtain a copy of | 4 // use this file except in compliance with the License. You may obtain a copy of |
| 5 // the License at | 5 // the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 static const char kTablesDirDefault[] = "tables"; | 102 static const char kTablesDirDefault[] = "tables"; |
| 103 | 103 |
| 104 // Well-known strings used in JSON messages. | 104 // Well-known strings used in JSON messages. |
| 105 static const char kCommandKey[] = "command"; | 105 static const char kCommandKey[] = "command"; |
| 106 static const char kMessageIdKey[] = "message_id"; | 106 static const char kMessageIdKey[] = "message_id"; |
| 107 static const char kInReplyToKey[] = "in_reply_to"; | 107 static const char kInReplyToKey[] = "in_reply_to"; |
| 108 static const char kErrorKey[] = "error"; | 108 static const char kErrorKey[] = "error"; |
| 109 static const char kTableNamesKey[] = "table_names"; | 109 static const char kTableNamesKey[] = "table_names"; |
| 110 static const char kSuccessKey[] = "success"; | 110 static const char kSuccessKey[] = "success"; |
| 111 static const char kTextKey[] = "text"; | 111 static const char kTextKey[] = "text"; |
| 112 static const char kFormTypeMapKey[] = "form_type_map"; |
| 112 static const char kCellsKey[] = "cells"; | 113 static const char kCellsKey[] = "cells"; |
| 113 static const char kCursorPositionKey[] = "cursor_position"; | 114 static const char kCursorPositionKey[] = "cursor_position"; |
| 114 static const char kTextToBrailleKey[] = "text_to_braille"; | 115 static const char kTextToBrailleKey[] = "text_to_braille"; |
| 115 static const char kBrailleToTextKey[] = "braille_to_text"; | 116 static const char kBrailleToTextKey[] = "braille_to_text"; |
| 116 static const char kCheckTableCommand[] = "CheckTable"; | 117 static const char kCheckTableCommand[] = "CheckTable"; |
| 117 static const char kTranslateCommand[] = "Translate"; | 118 static const char kTranslateCommand[] = "Translate"; |
| 118 static const char kBackTranslateCommand[] = "BackTranslate"; | 119 static const char kBackTranslateCommand[] = "BackTranslate"; |
| 119 | 120 |
| 120 LibLouisInstance::LibLouisInstance(PP_Instance instance) | 121 LibLouisInstance::LibLouisInstance(PP_Instance instance) |
| 121 : pp::Instance(instance), liblouis_thread_(this), cc_factory_(this) {} | 122 : pp::Instance(instance), liblouis_thread_(this), cc_factory_(this) {} |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 Json::Value reply(Json::objectValue); | 231 Json::Value reply(Json::objectValue); |
| 231 reply[kSuccessKey] = success; | 232 reply[kSuccessKey] = success; |
| 232 PostReply(reply, message_id); | 233 PostReply(reply, message_id); |
| 233 } | 234 } |
| 234 | 235 |
| 235 void LibLouisInstance::HandleTranslate(const Json::Value& message, | 236 void LibLouisInstance::HandleTranslate(const Json::Value& message, |
| 236 const std::string& message_id) { | 237 const std::string& message_id) { |
| 237 Json::Value table_names = message[kTableNamesKey]; | 238 Json::Value table_names = message[kTableNamesKey]; |
| 238 Json::Value text = message[kTextKey]; | 239 Json::Value text = message[kTextKey]; |
| 239 Json::Value cursor_position = message[kCursorPositionKey]; | 240 Json::Value cursor_position = message[kCursorPositionKey]; |
| 241 Json::Value form_type_map = message[kFormTypeMapKey]; |
| 240 if (!table_names.isString()) { | 242 if (!table_names.isString()) { |
| 241 PostError("expected table_names to be a string", message_id); | 243 PostError("expected table_names to be a string", message_id); |
| 242 return; | 244 return; |
| 243 } else if (!text.isString()) { | 245 } else if (!text.isString()) { |
| 244 PostError("expected text to be a string", message_id); | 246 PostError("expected text to be a string", message_id); |
| 245 return; | 247 return; |
| 246 } else if (!cursor_position.isNull() && !cursor_position.isIntegral()) { | 248 } else if (!cursor_position.isNull() && !cursor_position.isIntegral()) { |
| 247 PostError("expected cursor_position to be null or integral", message_id); | 249 PostError("expected cursor_position to be null or integral", message_id); |
| 248 return; | 250 return; |
| 251 } else if (!form_type_map.isArray()) { |
| 252 PostError("expected form_type_map to be an array", message_id); |
| 253 return; |
| 249 } | 254 } |
| 250 TranslationParams params; | 255 TranslationParams params; |
| 251 params.table_names = table_names.asString(); | 256 params.table_names = table_names.asString(); |
| 252 params.text = text.asString(); | 257 params.text = text.asString(); |
| 253 params.cursor_position = cursor_position.isIntegral() ? | 258 params.cursor_position = cursor_position.isIntegral() ? |
| 254 cursor_position.asInt() : -1; | 259 cursor_position.asInt() : -1; |
| 260 for (size_t i = 0; i < form_type_map.size(); ++i) { |
| 261 Json::Value val = form_type_map[i]; |
| 262 if (!val.isIntegral()) { |
| 263 PostError("expected form_type_map to be an integral array", message_id); |
| 264 return; |
| 265 } |
| 266 params.form_type_map.push_back(val.asInt()); |
| 267 } |
| 255 PostWorkToBackground(cc_factory_.NewCallback( | 268 PostWorkToBackground(cc_factory_.NewCallback( |
| 256 &LibLouisInstance::TranslateInBackground, | 269 &LibLouisInstance::TranslateInBackground, |
| 257 params, message_id)); | 270 params, message_id)); |
| 258 } | 271 } |
| 259 | 272 |
| 260 void LibLouisInstance::TranslateInBackground(int32_t result, | 273 void LibLouisInstance::TranslateInBackground(int32_t result, |
| 261 const TranslationParams& params, const std::string& message_id) { | 274 const TranslationParams& params, const std::string& message_id) { |
| 262 if (result != PP_OK) { | 275 if (result != PP_OK) { |
| 263 PostError("failed to transfer call to background thread", message_id); | 276 PostError("failed to transfer call to background thread", message_id); |
| 264 return; | 277 return; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 bool success = liblouis_.BackTranslate(table_names, cells, &text); | 329 bool success = liblouis_.BackTranslate(table_names, cells, &text); |
| 317 Json::Value reply(Json::objectValue); | 330 Json::Value reply(Json::objectValue); |
| 318 reply[kSuccessKey] = success; | 331 reply[kSuccessKey] = success; |
| 319 if (success) { | 332 if (success) { |
| 320 reply[kTextKey] = text; | 333 reply[kTextKey] = text; |
| 321 } | 334 } |
| 322 PostReply(reply, message_id); | 335 PostReply(reply, message_id); |
| 323 } | 336 } |
| 324 | 337 |
| 325 } // namespace liblouis_nacl | 338 } // namespace liblouis_nacl |
| OLD | NEW |