Add exception case of NULL for ipc between plugin and UI Process
[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
34 namespace {
35 std::string toString(WKStringRef str)
36 {
37     if (WKStringIsEmpty(str)) {
38         return std::string();
39     }
40     size_t size = WKStringGetMaximumUTF8CStringSize(str);
41     char buffer[size + 1];
42     WKStringGetUTF8CString(str, buffer, size + 1);
43     return buffer;
44 }
45 }
46
47 void IPCMessageSupport::setWKBundleRef(WKBundleRef bundleRef)
48 {
49     LogDebug("setWKBundleRef called");
50     s_injectedBundleRef = bundleRef;
51 }
52
53 const char* IPCMessageSupport::sendMessageToUiProcess(
54     const char* name,
55     const char* body)
56 {
57     LogDebug("sendMessageToUiProcess called");
58     if (s_injectedBundleRef == NULL) {
59         LogError("UI Process information isn't set");
60         return NULL;
61     }
62     LogDebug("name = [" << name << "]");
63     LogDebug("body = [" << body << "]");
64
65     if (!name) {
66         return NULL;
67     }
68     WKStringRef bodyWKString = NULL;
69     WKStringRef nameWKString = WKStringCreateWithUTF8CString(name);
70     if (body) {
71         bodyWKString = WKStringCreateWithUTF8CString(body);
72     }
73     WKTypeRef retWKType = NULL;
74     WKBundlePostSynchronousMessage(s_injectedBundleRef,
75                                    nameWKString,
76                                    bodyWKString,
77                                    &retWKType);
78     WKRelease(nameWKString);
79     if (bodyWKString) {
80         WKRelease(bodyWKString);
81     }
82     if (retWKType) {
83         std::string retString = toString(static_cast<WKStringRef>(retWKType));
84         WKRelease(retWKType);
85         return strdup(retString.c_str());
86     } else {
87         return NULL;
88     }
89     return NULL;
90 }