2 * Copyright (c) 2011 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 * @file ipc_message_support.cpp
18 * @author Jihoon Chung (jihoon.chung@samsung.com)
20 * @brief Implementation of IPC between plugins and UI Process
22 #include "ipc_message_support.h"
29 #include <dpl/log/log.h>
30 #include <dpl/assert.h>
32 static WKBundleRef s_injectedBundleRef = NULL;
33 static unsigned int s_xWindowHandle = 0;
36 const char* const TIZEN_GET_WINDOW_HANDLE = "tizen://getWindowHandle";
37 const char* const TIZEN_CLEAR_ALL_COOKIES = "tizen://clearAllCookies";
39 static std::string toString(WKStringRef str)
41 if (WKStringIsEmpty(str)) {
44 size_t size = WKStringGetMaximumUTF8CStringSize(str);
45 char buffer[size + 1];
46 WKStringGetUTF8CString(str, buffer, size + 1);
50 static std::string sendSyncMessage(const char* name, const char* body)
52 WKStringRef nameWKString = WKStringCreateWithUTF8CString(name);
53 WKStringRef bodyWKString = NULL;
55 bodyWKString = WKStringCreateWithUTF8CString(body);
57 WKTypeRef retWKType = NULL;
58 WKBundlePostSynchronousMessage(s_injectedBundleRef,
62 WKRelease(nameWKString);
64 WKRelease(bodyWKString);
67 std::string retString = toString(static_cast<WKStringRef>(retWKType));
75 static void sendPostMessage(const char* name, const char* body)
77 WKStringRef nameWKString = WKStringCreateWithUTF8CString(name);
78 WKStringRef bodyWKString = NULL;
80 bodyWKString = WKStringCreateWithUTF8CString(body);
82 WKTypeRef retWKType = NULL;
83 WKBundlePostMessage(s_injectedBundleRef,
86 WKRelease(nameWKString);
88 WKRelease(bodyWKString);
92 static int sendAsyncMessage(const char* name, const char* body, AsyncReplyCallback replyCallback, void* data)
94 using namespace IPCMessageSupport;
96 int handle = AsyncConnectionManager::instance().addConnection(AsyncConnectionPtr(new AsyncConnection(replyCallback, data)));
98 std::string strBody = body;
102 strBody = ss.str() + "_" + strBody;
104 sendPostMessage(name, strBody.c_str());
112 namespace IPCMessageSupport {
114 AsyncConnectionManager::~AsyncConnectionManager()
116 m_connectionMap.clear();
119 AsyncConnectionManager &AsyncConnectionManager::instance()
121 static AsyncConnectionManager instance;
126 int AsyncConnectionManager::addConnection(AsyncConnectionPtr connection)
128 static int latestHandle = -1;
132 if (latestHandle < 0) latestHandle = 0;
134 newHandle = latestHandle;
136 m_connectionMap.insert(AsyncConnectionMap::value_type(newHandle, connection));
141 bool AsyncConnectionManager::removeConnection(int handle)
143 bool ret = (m_connectionMap.erase(handle) == 1);
148 AsyncConnectionPtr AsyncConnectionManager::getConnection(int handle)
150 AsyncConnectionMap::iterator iter = m_connectionMap.find(handle);
152 if (iter != m_connectionMap.end())
157 return AsyncConnectionPtr();
161 } // namespace IPCMessageSupport
164 void IPCMessageSupport::setWKBundleRef(WKBundleRef bundleRef)
166 LogDebug("setWKBundleRef called");
167 s_injectedBundleRef = bundleRef;
170 void IPCMessageSupport::setXwindowHandle(unsigned int handle)
172 LogDebug("setXwindowHandle called");
173 s_xWindowHandle = handle;
176 const char* IPCMessageSupport::sendMessageToUiProcess(
180 LogDebug("sendMessageToUiProcess called");
181 if (s_injectedBundleRef == NULL) {
182 LogError("UI Process information isn't set");
185 LogDebug("name = [" << name << "]");
187 LogDebug("body = [" << body << "]");
194 // tizen://getWindowHandle
195 if (!strcmp(name, TIZEN_GET_WINDOW_HANDLE)) {
196 if (s_xWindowHandle == 0) {
199 std::stringstream ss;
200 ss << s_xWindowHandle;
201 std::string ret = ss.str();
202 return strdup(ret.c_str());
206 // tizen://clearAllCookies
207 if (!strcmp(name, TIZEN_CLEAR_ALL_COOKIES)) {
208 sendPostMessage(name, body);
215 int IPCMessageSupport::sendAsyncMessageToUiProcess(const char* name, const char* body, AsyncReplyCallback replyCallback, void* data)
217 LogDebug("sendAsyncMessageToUiProcess called");
219 if (s_injectedBundleRef == NULL)
221 LogError("UI Process information isn't set");
227 LogError("msg name is null!");
236 LogDebug("name = [" << name << "]");
237 LogDebug("body = [" << body << "]");
239 return sendAsyncMessage(name, body, replyCallback, data);
242 void* IPCMessageSupport::ignoreAsyncMessageReply(int handle)
244 LogDebug("ignoreAsyncMessageReply called");
246 AsyncConnectionPtr connection = AsyncConnectionManager::instance().getConnection(handle);
253 AsyncConnectionManager::instance().removeConnection(handle);
255 return connection->data;
258 void IPCMessageSupport::replyAsyncMessageToWebProcess(Ewk_Context* ewkContext, int handle, const char* body)
260 LogDebug("replyAsyncMessageToWebProcess called");
262 if (ewkContext == NULL)
267 std::string strBody = (body) ? (body) : "";
268 std::stringstream ss;
271 strBody = ss.str() + "_" + strBody;
273 ewk_context_message_post_to_injected_bundle(ewkContext, REPLY_ASYNC, strBody.c_str());