1 // Copyright 2013 Google Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
15 #ifndef LIBLOUIS_NACL_LIBLOUIS_INSTANCE_H_
16 #define LIBLOUIS_NACL_LIBLOUIS_INSTANCE_H_
20 #include "base/basictypes.h"
21 #include "liblouis_wrapper.h"
22 #include "ppapi/c/pp_instance.h"
23 #include "ppapi/cpp/completion_callback.h"
24 #include "ppapi/cpp/instance.h"
25 #include "ppapi/cpp/message_loop.h"
26 #include "ppapi/cpp/var.h"
27 #include "ppapi/utility/completion_callback_factory.h"
28 #include "ppapi/utility/threading/simple_thread.h"
29 #include "third_party/jsoncpp/source/include/json/json.h"
30 #include "translation_params.h"
32 namespace liblouis_nacl
{
34 class LibLouisInstance
: public pp::Instance
{
36 explicit LibLouisInstance(PP_Instance instance
);
37 virtual ~LibLouisInstance();
39 virtual bool Init(uint32_t argc
, const char* argn
[], const char* argv
[]);
40 virtual void HandleMessage(const pp::Var
& var_message
);
43 // Post work to the background (liblouis) thread.
44 int32_t PostWorkToBackground(const pp::CompletionCallback
& callback
) {
45 return liblouis_thread_
.message_loop().PostWork(callback
);
48 // Encodes a message as JSON and sends it over to JavaScript.
49 void PostReply(Json::Value reply
, const std::string
& in_reply_to
);
51 // Posts an error response to JavaScript.
52 void PostError(const std::string
& error
);
54 // Posts an error response to JavaScript, with the message ID of the call
56 void PostError(const std::string
& error
, const std::string
& in_reply_to
);
58 // Parses and executes a table check command.
59 void HandleCheckTable(const Json::Value
& message
,
60 const std::string
& message_id
);
62 // Called to check a table on a background thread.
63 void CheckTableInBackground(int32_t result
, const std::string
& table_names
,
64 const std::string
& message_id
);
66 // Parses and executes a translation command.
67 void HandleTranslate(const Json::Value
& message
,
68 const std::string
& message_id
);
70 // Called to translate text on a background thread.
71 void TranslateInBackground(int32_t result
, const TranslationParams
& params
,
72 const std::string
& message_id
);
74 // Parses and executes a back translation command.
75 void HandleBackTranslate(const Json::Value
& message
,
76 const std::string
& message_id
);
78 // Called to back-translate text on a background thread.
79 void BackTranslateInBackground(int32_t result
,
80 const std::string
& table_names
, const std::vector
<unsigned char>& cells
,
81 const std::string
& message_id
);
83 LibLouisWrapper liblouis_
;
84 pp::SimpleThread liblouis_thread_
;
85 pp::CompletionCallbackFactory
<LibLouisInstance
> cc_factory_
;
87 DISALLOW_COPY_AND_ASSIGN(LibLouisInstance
);
90 } // namespace liblouis_nacl
92 #endif // LIBLOUIS_NACL_LIBLOUIS_INSTANCE_H_