2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "bundle/runtime_ipc_client.h"
19 #include "common/logger.h"
20 #include "common/string_utils.h"
24 RuntimeIPCClient::JSCallback::JSCallback(v8::Isolate* isolate,
25 v8::Handle<v8::Function> callback) {
26 callback_.Reset(isolate, callback);
29 RuntimeIPCClient::JSCallback::~JSCallback() {
33 void RuntimeIPCClient::JSCallback::Call(v8::Isolate* isolate,
34 v8::Handle<v8::Value> args[]) {
35 if (!callback_.IsEmpty()) {
36 v8::HandleScope handle_scope(isolate);
37 v8::Handle<v8::Function> func =
38 v8::Local<v8::Function>::New(isolate, callback_);
39 func->Call(func, 1, args);
44 RuntimeIPCClient* RuntimeIPCClient::GetInstance() {
45 static RuntimeIPCClient self;
49 RuntimeIPCClient::RuntimeIPCClient() : routing_id_(0) {
52 void RuntimeIPCClient::SendMessage(const std::string& type,
53 const std::string& value) {
54 Ewk_IPC_Wrt_Message_Data* msg = ewk_ipc_wrt_message_data_new();
55 ewk_ipc_wrt_message_data_id_set(msg, "");
56 ewk_ipc_wrt_message_data_type_set(msg, type.c_str());
57 ewk_ipc_wrt_message_data_value_set(msg, value.c_str());
59 if (routing_id_ > 0) {
60 if (!ewk_ipc_plugins_message_send(routing_id_, msg)) {
61 LOGGER(ERROR) << "Failed to send message to runtime using ewk_ipc.";
65 ewk_ipc_wrt_message_data_del(msg);
68 std::string RuntimeIPCClient::SendSyncMessage(const std::string& type,
69 const std::string& value) {
70 Ewk_IPC_Wrt_Message_Data* msg = ewk_ipc_wrt_message_data_new();
71 ewk_ipc_wrt_message_data_type_set(msg, type.c_str());
72 ewk_ipc_wrt_message_data_value_set(msg, value.c_str());
74 if (routing_id_ > 0) {
75 if (!ewk_ipc_plugins_sync_message_send(routing_id_, msg)) {
76 LOGGER(ERROR) << "Failed to send message to runtime using ewk_ipc.";
77 ewk_ipc_wrt_message_data_del(msg);
82 Eina_Stringshare* msg_value = ewk_ipc_wrt_message_data_value_get(msg);
84 std::string result(msg_value);
85 eina_stringshare_del(msg_value);
86 ewk_ipc_wrt_message_data_del(msg);
91 void RuntimeIPCClient::SendAsyncMessage(const std::string& type,
92 const std::string& value,
93 ReplyCallback callback) {
94 std::string msg_id = utils::GenerateUUID();
96 Ewk_IPC_Wrt_Message_Data* msg = ewk_ipc_wrt_message_data_new();
97 ewk_ipc_wrt_message_data_id_set(msg, msg_id.c_str());
98 ewk_ipc_wrt_message_data_type_set(msg, type.c_str());
99 ewk_ipc_wrt_message_data_value_set(msg, value.c_str());
101 if (routing_id_ > 0) {
102 if (!ewk_ipc_plugins_message_send(routing_id_, msg)) {
103 LOGGER(ERROR) << "Failed to send message to runtime using ewk_ipc.";
104 ewk_ipc_wrt_message_data_del(msg);
109 callbacks_[msg_id] = callback;
111 ewk_ipc_wrt_message_data_del(msg);
114 void RuntimeIPCClient::HandleMessageFromRuntime(
115 const Ewk_IPC_Wrt_Message_Data* msg) {
117 LOGGER(ERROR) << "received message is NULL";
121 Eina_Stringshare* msg_refid = ewk_ipc_wrt_message_data_reference_id_get(msg);
123 if (msg_refid == NULL || !strcmp(msg_refid, "")) {
124 if (msg_refid) eina_stringshare_del(msg_refid);
125 LOGGER(ERROR) << "No reference id of received message.";
129 auto it = callbacks_.find(msg_refid);
130 if (it == callbacks_.end()) {
131 eina_stringshare_del(msg_refid);
132 LOGGER(ERROR) << "No registered callback with reference id : " << msg_refid;
136 Eina_Stringshare* msg_type = ewk_ipc_wrt_message_data_type_get(msg);
137 Eina_Stringshare* msg_value = ewk_ipc_wrt_message_data_value_get(msg);
139 ReplyCallback func = it->second;
141 func(msg_type, msg_value);
144 callbacks_.erase(it);
146 eina_stringshare_del(msg_refid);
147 eina_stringshare_del(msg_type);
148 eina_stringshare_del(msg_value);