Support cookie clear IPC
[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 const char* const TIZEN_GET_WINDOW_HANDLE = "tizen://getWindowHandle";
37 const char* const TIZEN_CLEAR_ALL_COOKIES = "tizen://clearAllCookies";
38
39 std::string toString(WKStringRef str)
40 {
41     if (WKStringIsEmpty(str)) {
42         return std::string();
43     }
44     size_t size = WKStringGetMaximumUTF8CStringSize(str);
45     char buffer[size + 1];
46     WKStringGetUTF8CString(str, buffer, size + 1);
47     return buffer;
48 }
49
50 std::string sendSyncMessage(const char* name, const char* body)
51 {
52     WKStringRef nameWKString = WKStringCreateWithUTF8CString(name);
53     WKStringRef bodyWKString = NULL;
54     if (body) {
55         bodyWKString = WKStringCreateWithUTF8CString(body);
56     }
57     WKTypeRef retWKType = NULL;
58     WKBundlePostSynchronousMessage(s_injectedBundleRef,
59                                    nameWKString,
60                                    bodyWKString,
61                                    &retWKType);
62     WKRelease(nameWKString);
63     if (bodyWKString) {
64         WKRelease(bodyWKString);
65     }
66     if (retWKType) {
67         std::string retString = toString(static_cast<WKStringRef>(retWKType));
68         WKRelease(retWKType);
69         return retString;
70     } else {
71         return std::string();
72     }
73 }
74
75 void sendAsyncMessage(const char* name, const char* body)
76 {
77     WKStringRef nameWKString = WKStringCreateWithUTF8CString(name);
78     WKStringRef bodyWKString = NULL;
79     if (body) {
80         bodyWKString = WKStringCreateWithUTF8CString(body);
81     }
82     WKTypeRef retWKType = NULL;
83     WKBundlePostMessage(s_injectedBundleRef,
84                         nameWKString,
85                         bodyWKString);
86     WKRelease(nameWKString);
87     if (bodyWKString) {
88         WKRelease(bodyWKString);
89     }
90 }
91 }
92
93 void IPCMessageSupport::setWKBundleRef(WKBundleRef bundleRef)
94 {
95     LogDebug("setWKBundleRef called");
96     s_injectedBundleRef = bundleRef;
97 }
98
99 void IPCMessageSupport::setXwindowHandle(unsigned int handle)
100 {
101     LogDebug("setXwindowHandle called");
102     s_xWindowHandle = handle;
103 }
104
105 const char* IPCMessageSupport::sendMessageToUiProcess(
106     const char* name,
107     const char* body)
108 {
109     LogDebug("sendMessageToUiProcess called");
110     if (s_injectedBundleRef == NULL) {
111         LogError("UI Process information isn't set");
112         return NULL;
113     }
114     LogDebug("name = [" << name << "]");
115     if (body) {
116         LogDebug("body = [" << body << "]");
117     }
118
119     if (!name) {
120         return NULL;
121     }
122
123     // tizen://getWindowHandle
124     if (!strcmp(name, TIZEN_GET_WINDOW_HANDLE)) {
125         if (s_xWindowHandle == 0) {
126             return NULL;
127         } else {
128             std::stringstream ss;
129             ss << s_xWindowHandle;
130             std::string ret  = ss.str();
131             return strdup(ret.c_str());
132         }
133     }
134
135     // tizen://clearAllCookies
136     if (!strcmp(name, TIZEN_CLEAR_ALL_COOKIES)) {
137         sendAsyncMessage(name, body);
138         return NULL;
139     }
140
141     return NULL;
142 }