Cleanup code
[platform/framework/web/nwrt.git] / src / bundle / runtime_ipc_client.cc
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 #include "bundle/runtime_ipc_client.h"
6
7 #include "common/logger.h"
8 #include "common/string_utils.h"
9
10 namespace wrt {
11
12 RuntimeIPCClient::JSCallback::JSCallback(v8::Isolate* isolate,
13                                          v8::Handle<v8::Function> callback)
14     : isolate_(isolate) {
15   callback_.Reset(isolate, callback);
16 }
17
18 RuntimeIPCClient::JSCallback::~JSCallback() {
19   callback_.Reset();
20 }
21
22 void RuntimeIPCClient::JSCallback::Call(v8::Handle<v8::Value> args[]) {
23   if (!callback_.IsEmpty()) {
24     v8::HandleScope handle_scope(isolate_);
25     v8::Handle<v8::Function> func =
26         v8::Local<v8::Function>::New(isolate_, callback_);
27     func->Call(func, 1, args);
28   }
29 }
30
31 // static
32 RuntimeIPCClient* RuntimeIPCClient::GetInstance() {
33   static RuntimeIPCClient self;
34   return &self;
35 }
36
37 RuntimeIPCClient::RuntimeIPCClient() : routing_id_(0) {
38 }
39
40 void RuntimeIPCClient::SendMessage(const std::string& type,
41                                    const std::string& value) {
42   Ewk_IPC_Wrt_Message_Data* msg = ewk_ipc_wrt_message_data_new();
43   ewk_ipc_wrt_message_data_id_set(msg, "");
44   ewk_ipc_wrt_message_data_type_set(msg, type.c_str());
45   ewk_ipc_wrt_message_data_value_set(msg, value.c_str());
46
47   if (routing_id_ > 0) {
48     if (!ewk_ipc_plugins_message_send(routing_id_, msg)) {
49       LOGGER(ERROR) << "Failed to send message to runtime using ewk_ipc.";
50     }
51   }
52
53   ewk_ipc_wrt_message_data_del(msg);
54 }
55
56 std::string RuntimeIPCClient::SendSyncMessage(const std::string& type,
57                                               const std::string& value) {
58   Ewk_IPC_Wrt_Message_Data* msg = ewk_ipc_wrt_message_data_new();
59   ewk_ipc_wrt_message_data_type_set(msg, type.c_str());
60   ewk_ipc_wrt_message_data_value_set(msg, value.c_str());
61
62   if (routing_id_ > 0) {
63     if (!ewk_ipc_plugins_sync_message_send(routing_id_, msg)) {
64       LOGGER(ERROR) << "Failed to send message to runtime using ewk_ipc.";
65       ewk_ipc_wrt_message_data_del(msg);
66       return std::string();
67     }
68   }
69
70   Eina_Stringshare* msg_value = ewk_ipc_wrt_message_data_value_get(msg);
71
72   std::string result(msg_value);
73   eina_stringshare_del(msg_value);
74   ewk_ipc_wrt_message_data_del(msg);
75
76   return result;
77 }
78
79 void RuntimeIPCClient::SendAsyncMessage(const std::string& type,
80                                         const std::string& value,
81                                         ReplyCallback callback,
82                                         JSCallback* js_callback) {
83   std::string msg_id = utils::GenerateUUID();
84
85   Ewk_IPC_Wrt_Message_Data* msg = ewk_ipc_wrt_message_data_new();
86   ewk_ipc_wrt_message_data_id_set(msg, msg_id.c_str());
87   ewk_ipc_wrt_message_data_type_set(msg, type.c_str());
88   ewk_ipc_wrt_message_data_value_set(msg, value.c_str());
89
90   if (routing_id_ > 0) {
91     if (!ewk_ipc_plugins_message_send(routing_id_, msg)) {
92       LOGGER(ERROR) << "Failed to send message to runtime using ewk_ipc.";
93       ewk_ipc_wrt_message_data_del(msg);
94       return;
95     }
96   }
97
98   callbacks_[msg_id].callback = callback;
99   callbacks_[msg_id].js_callback = js_callback;
100
101   ewk_ipc_wrt_message_data_del(msg);
102 }
103
104 void RuntimeIPCClient::HandleMessageFromRuntime(
105     const Ewk_IPC_Wrt_Message_Data* msg) {
106   if (msg == NULL) {
107     LOGGER(ERROR) << "received message is NULL";
108     return;
109   }
110
111   Eina_Stringshare* msg_refid = ewk_ipc_wrt_message_data_reference_id_get(msg);
112
113   if (msg_refid == NULL || !strcmp(msg_refid, "")) {
114     if (msg_refid) eina_stringshare_del(msg_refid);
115     LOGGER(ERROR) << "No reference id of received message.";
116     return;
117   }
118
119   auto it = callbacks_.find(msg_refid);
120   if (it == callbacks_.end()) {
121     eina_stringshare_del(msg_refid);
122     LOGGER(ERROR) << "No registered callback with reference id : " << msg_refid;
123     return;
124   }
125
126   Eina_Stringshare* msg_type = ewk_ipc_wrt_message_data_type_get(msg);
127   Eina_Stringshare* msg_value = ewk_ipc_wrt_message_data_value_get(msg);
128
129   const AsyncData& async_data = it->second;
130   async_data.callback(msg_type, msg_value, async_data.js_callback);
131   callbacks_.erase(it);
132
133   eina_stringshare_del(msg_refid);
134   eina_stringshare_del(msg_type);
135   eina_stringshare_del(msg_value);
136 }
137
138 }  // namespace wrt