Cleanup code
[platform/framework/web/nwrt.git] / src / bundle / runtime_ipc_client.h
1 // Copyright 2015 Samsung Electronics Co, Ltd. 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 WRT_BUNDLE_RUNTIME_IPC_CLIENT_H_
6 #define WRT_BUNDLE_RUNTIME_IPC_CLIENT_H_
7
8 #include <v8/v8.h>
9 #include <ewk_ipc_message.h>
10 #include <functional>
11 #include <map>
12 #include <string>
13
14 namespace wrt {
15
16 class RuntimeIPCClient {
17  public:
18   class JSCallback {
19    public:
20     explicit JSCallback(v8::Isolate* isolate,
21                         v8::Handle<v8::Function> callback);
22     ~JSCallback();
23
24     v8::Isolate* isolate() const { return isolate_; }
25
26     void Call(v8::Handle<v8::Value> args[]);
27    private:
28     v8::Isolate* isolate_;
29     v8::Persistent<v8::Function> callback_;
30   };
31
32   typedef std::function<void(const std::string& type,
33                              const std::string& value,
34                              JSCallback* js_callback)> ReplyCallback;
35
36   static RuntimeIPCClient* GetInstance();
37
38   // Send message to BrowserProcess without reply
39   void SendMessage(const std::string& type, const std::string& value);
40
41   // Send message to BrowserProcess synchronous with reply
42   std::string SendSyncMessage(const std::string& type,
43                               const std::string& value);
44
45   // Send message to BrowserProcess asynchronous,
46   // reply message will be passed to callback function.
47   void SendAsyncMessage(const std::string& type, const std::string& value,
48                         ReplyCallback callback, JSCallback* js_callback);
49
50   void HandleMessageFromRuntime(const Ewk_IPC_Wrt_Message_Data* msg);
51
52   int routing_id() const { return routing_id_; }
53   void set_routing_id(int routing_id) { routing_id_ = routing_id; }
54
55  private:
56   class AsyncData {
57    public:
58     ~AsyncData() {
59       if (js_callback) delete js_callback;
60     }
61
62     ReplyCallback callback;
63     JSCallback* js_callback;
64   };
65
66   RuntimeIPCClient();
67
68   int routing_id_;
69   std::map<std::string, AsyncData> callbacks_;
70 };
71
72 }  // namespace wrt
73
74 #endif  // WRT_BUNDLE_RUNTIME_IPC_CLIENT_H_