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.
5 #include "bundle/runtime_ipc_client.h"
7 #include "common/logger.h"
8 #include "common/string_utils.h"
12 RuntimeIPCClient::JSCallback::JSCallback(v8::Isolate* isolate,
13 v8::Handle<v8::Function> callback)
15 callback_.Reset(isolate, callback);
18 RuntimeIPCClient::JSCallback::~JSCallback() {
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);
32 RuntimeIPCClient* RuntimeIPCClient::GetInstance() {
33 static RuntimeIPCClient self;
37 RuntimeIPCClient::RuntimeIPCClient() : routing_id_(0) {
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());
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.";
53 ewk_ipc_wrt_message_data_del(msg);
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());
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);
70 Eina_Stringshare* msg_value = ewk_ipc_wrt_message_data_value_get(msg);
72 std::string result(msg_value);
73 eina_stringshare_del(msg_value);
74 ewk_ipc_wrt_message_data_del(msg);
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();
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());
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);
98 callbacks_[msg_id].callback = callback;
99 callbacks_[msg_id].js_callback = js_callback;
101 ewk_ipc_wrt_message_data_del(msg);
104 void RuntimeIPCClient::HandleMessageFromRuntime(
105 const Ewk_IPC_Wrt_Message_Data* msg) {
107 LOGGER(ERROR) << "received message is NULL";
111 Eina_Stringshare* msg_refid = ewk_ipc_wrt_message_data_reference_id_get(msg);
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.";
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;
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);
129 const AsyncData& async_data = it->second;
130 async_data.callback(msg_type, msg_value, async_data.js_callback);
131 callbacks_.erase(it);
133 eina_stringshare_del(msg_refid);
134 eina_stringshare_del(msg_type);
135 eina_stringshare_del(msg_value);