[Speech API]: running mainloop in crosswalk extension
[profile/ivi/speech-recognition.git] / src / plugins / client-api / w3c-speech / crosswalk-extension / speech_instance.h
1 // Copyright (c) 2014 Intel Corporation. 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 #ifndef SPEECH_SPEECH_INSTANCE_H_
6 #define SPEECH_SPEECH_INSTANCE_H_
7
8 #include <glib.h>
9 #include <thread>  // NOLINT
10 #include <queue>
11 #include <string>
12
13 #include "common/extension.h"
14 #include "common/picojson.h"
15
16 class SpeechInstance : public common::Instance {
17  public:
18   SpeechInstance();
19   virtual ~SpeechInstance();
20
21  protected:
22   static gboolean IOWatchCb(GIOChannel* source,
23                             GIOCondition condition,
24                             gpointer data);
25   static gboolean ProcessPendingRequestsCb(gpointer data);
26   static gboolean ProcessPendingRepliesCb(gpointer data);
27
28  private:
29   // common::Instance implementation.
30   virtual void HandleMessage(const char* msg);
31   virtual void HandleSyncMessage(const char* message);
32
33   void QueueReply(const std::string& reply);
34   void QueueRequest(const std::string& req);
35   bool SendRequest(const char* message);
36   uint32_t ReadReply(char** reply);
37 #ifdef TIZEN
38   static void SetupMainloop(void *data);
39   GMainLoop* main_loop_;
40   std::thread  thread_;
41 #endif  // TIZEN
42
43   std::queue<std::string> pending_replies_;
44   std::queue<std::string> pending_requests_;
45   int fd_;
46   GIOChannel *channel_;
47   guint watcher_id_;
48   guint pending_request_timer_;
49   guint pending_reply_timer_;
50 };
51 #endif  // SPEECH_SPEECH_INSTANCE_H_