[Release] wrt-plugins-common_0.3.94
[platform/framework/web/wrt-plugins-common.git] / src / plugins-ipc-message / ipc_message_support.cpp
1 /*
2  * Copyright (c) 2011 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  * @file        ipc_message_support.cpp
18  * @author      Jihoon Chung (jihoon.chung@samsung.com)
19  * @version     1.0
20  * @brief       Implementation of IPC between plugins and UI Process
21  */
22 #include "ipc_message_support.h"
23
24 #include <string>
25 #include <sstream>
26 #include <WKBundle.h>
27 #include <WKString.h>
28 #include <WKType.h>
29 #include <dpl/log/log.h>
30 #include <dpl/assert.h>
31
32 static WKBundleRef s_injectedBundleRef = NULL;
33 static unsigned  int s_xWindowHandle = 0;
34
35 namespace {
36 std::string toString(WKStringRef str)
37 {
38     if (WKStringIsEmpty(str)) {
39         return std::string();
40     }
41     size_t size = WKStringGetMaximumUTF8CStringSize(str);
42     char buffer[size + 1];
43     WKStringGetUTF8CString(str, buffer, size + 1);
44     return buffer;
45 }
46 }
47
48 void IPCMessageSupport::setWKBundleRef(WKBundleRef bundleRef)
49 {
50     LogDebug("setWKBundleRef called");
51     s_injectedBundleRef = bundleRef;
52 }
53
54 void IPCMessageSupport::setXwindowHandle(unsigned int handle)
55 {
56     LogDebug("setXwindowHandle called");
57     s_xWindowHandle = handle;
58 }
59
60 const char* IPCMessageSupport::sendMessageToUiProcess(
61     const char* name,
62     const char* body)
63 {
64     LogDebug("sendMessageToUiProcess called");
65     if (s_injectedBundleRef == NULL) {
66         LogError("UI Process information isn't set");
67         return NULL;
68     }
69     LogDebug("name = [" << name << "]");
70     if (body) {
71         LogDebug("body = [" << body << "]");
72     }
73
74     if (!name) {
75         return NULL;
76     }
77
78     if (!strcmp(name, "tizen://getWindowHandle")) {
79         if (s_xWindowHandle == 0) {
80             return NULL;
81         } else {
82             std::stringstream ss;
83             ss << s_xWindowHandle;
84             std::string ret  = ss.str();
85             return strdup(ret.c_str());
86         }
87     }
88
89     WKStringRef bodyWKString = NULL;
90     WKStringRef nameWKString = WKStringCreateWithUTF8CString(name);
91     if (body) {
92         bodyWKString = WKStringCreateWithUTF8CString(body);
93     }
94     WKTypeRef retWKType = NULL;
95     WKBundlePostSynchronousMessage(s_injectedBundleRef,
96                                    nameWKString,
97                                    bodyWKString,
98                                    &retWKType);
99     WKRelease(nameWKString);
100     if (bodyWKString) {
101         WKRelease(bodyWKString);
102     }
103     if (retWKType) {
104         std::string retString = toString(static_cast<WKStringRef>(retWKType));
105         WKRelease(retWKType);
106         return strdup(retString.c_str());
107     } else {
108         return NULL;
109     }
110     return NULL;
111 }