Fix invalid licenses
[platform/framework/web/crosswalk-tizen.git] / src / bundle / runtime_ipc_client.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "bundle/runtime_ipc_client.h"
18
19 #include "common/logger.h"
20 #include "common/string_utils.h"
21
22 namespace wrt {
23
24 RuntimeIPCClient::JSCallback::JSCallback(v8::Isolate* isolate,
25                                          v8::Handle<v8::Function> callback) {
26   callback_.Reset(isolate, callback);
27 }
28
29 RuntimeIPCClient::JSCallback::~JSCallback() {
30   callback_.Reset();
31 }
32
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);
40   }
41 }
42
43 // static
44 RuntimeIPCClient* RuntimeIPCClient::GetInstance() {
45   static RuntimeIPCClient self;
46   return &self;
47 }
48
49 RuntimeIPCClient::RuntimeIPCClient() : routing_id_(0) {
50 }
51
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());
58
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.";
62     }
63   }
64
65   ewk_ipc_wrt_message_data_del(msg);
66 }
67
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());
73
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);
78       return std::string();
79     }
80   }
81
82   Eina_Stringshare* msg_value = ewk_ipc_wrt_message_data_value_get(msg);
83
84   std::string result(msg_value);
85   eina_stringshare_del(msg_value);
86   ewk_ipc_wrt_message_data_del(msg);
87
88   return result;
89 }
90
91 void RuntimeIPCClient::SendAsyncMessage(const std::string& type,
92                                         const std::string& value,
93                                         ReplyCallback callback) {
94   std::string msg_id = utils::GenerateUUID();
95
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());
100
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);
105       return;
106     }
107   }
108
109   callbacks_[msg_id] = callback;
110
111   ewk_ipc_wrt_message_data_del(msg);
112 }
113
114 void RuntimeIPCClient::HandleMessageFromRuntime(
115     const Ewk_IPC_Wrt_Message_Data* msg) {
116   if (msg == NULL) {
117     LOGGER(ERROR) << "received message is NULL";
118     return;
119   }
120
121   Eina_Stringshare* msg_refid = ewk_ipc_wrt_message_data_reference_id_get(msg);
122
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.";
126     return;
127   }
128
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;
133     return;
134   }
135
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);
138
139   ReplyCallback func = it->second;
140   if (func) {
141     func(msg_type, msg_value);
142   }
143
144   callbacks_.erase(it);
145
146   eina_stringshare_del(msg_refid);
147   eina_stringshare_del(msg_type);
148   eina_stringshare_del(msg_value);
149 }
150
151 }  // namespace wrt