[WK2] Add support for Web Intents MessagePorts
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / efl / ewk_intent.cpp
1 /*
2  * Copyright (C) 2012 Intel Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "ewk_intent.h"
28
29 #include "WKAPICast.h"
30 #include "WKArray.h"
31 #include "WKDictionary.h"
32 #include "WKIntentData.h"
33 #include "WKRetainPtr.h"
34 #include "WKString.h"
35 #include "WKURL.h"
36 #include "ewk_intent_private.h"
37 #include <wtf/text/CString.h>
38
39 using namespace WebKit;
40
41 /**
42  * \struct  _Ewk_Intent
43  * @brief   Contains the intent data.
44  */
45 struct _Ewk_Intent {
46     unsigned int __ref; /**< the reference count of the object */
47 #if ENABLE(WEB_INTENTS)
48     WKRetainPtr<WKIntentDataRef> wkIntent;
49 #endif
50     const char* action;
51     const char* type;
52     const char* service;
53
54     _Ewk_Intent(WKIntentDataRef intentRef)
55         : __ref(1)
56 #if ENABLE(WEB_INTENTS)
57         , wkIntent(intentRef)
58 #endif
59         , action(0)
60         , type(0)
61         , service(0)
62     { }
63
64     ~_Ewk_Intent()
65     {
66         ASSERT(!__ref);
67         eina_stringshare_del(action);
68         eina_stringshare_del(type);
69         eina_stringshare_del(service);
70     }
71 };
72
73 #define EWK_INTENT_WK_GET_OR_RETURN(intent, wkIntent_, ...)    \
74     if (!(intent)) {                                           \
75         EINA_LOG_CRIT("intent is NULL.");                      \
76         return __VA_ARGS__;                                    \
77     }                                                          \
78     if (!(intent)->wkIntent) {                                 \
79         EINA_LOG_CRIT("intent->wkIntent is NULL.");            \
80         return __VA_ARGS__;                                    \
81     }                                                          \
82     WKIntentDataRef wkIntent_ = (intent)->wkIntent.get()
83
84 void ewk_intent_ref(Ewk_Intent* intent)
85 {
86 #if ENABLE(WEB_INTENTS)
87     EINA_SAFETY_ON_NULL_RETURN(intent);
88     ++intent->__ref;
89 #endif
90 }
91
92 void ewk_intent_unref(Ewk_Intent* intent)
93 {
94 #if ENABLE(WEB_INTENTS)
95     EINA_SAFETY_ON_NULL_RETURN(intent);
96
97     if (--intent->__ref)
98         return;
99
100     delete intent;
101 #endif
102 }
103
104 const char* ewk_intent_action_get(const Ewk_Intent* intent)
105 {
106 #if ENABLE(WEB_INTENTS)
107     EWK_INTENT_WK_GET_OR_RETURN(intent, wkIntent, 0);
108
109     WKRetainPtr<WKStringRef> wkAction(AdoptWK, WKIntentDataCopyAction(wkIntent));
110     Ewk_Intent* ewkIntent = const_cast<Ewk_Intent*>(intent);
111     eina_stringshare_replace(&ewkIntent->action, toImpl(wkAction.get())->string().utf8().data());
112
113     return intent->action;
114 #else
115     return 0;
116 #endif
117 }
118
119 const char* ewk_intent_type_get(const Ewk_Intent* intent)
120 {
121 #if ENABLE(WEB_INTENTS)
122     EWK_INTENT_WK_GET_OR_RETURN(intent, wkIntent, 0);
123
124     WKRetainPtr<WKStringRef> wkType(AdoptWK, WKIntentDataCopyType(wkIntent));
125     Ewk_Intent* ewkIntent = const_cast<Ewk_Intent*>(intent);
126     eina_stringshare_replace(&ewkIntent->type, toImpl(wkType.get())->string().utf8().data());
127
128     return intent->type;
129 #else
130     return 0;
131 #endif
132 }
133
134 const char* ewk_intent_service_get(const Ewk_Intent* intent)
135 {
136 #if ENABLE(WEB_INTENTS)
137     EWK_INTENT_WK_GET_OR_RETURN(intent, wkIntent, 0);
138
139     WKRetainPtr<WKURLRef> wkService(AdoptWK, WKIntentDataCopyService(wkIntent));
140     Ewk_Intent* ewkIntent = const_cast<Ewk_Intent*>(intent);
141     eina_stringshare_replace(&ewkIntent->service, toImpl(wkService.get())->string().utf8().data());
142
143     return intent->service;
144 #else
145     return 0;
146 #endif
147 }
148
149 Eina_List* ewk_intent_suggestions_get(const Ewk_Intent* intent)
150 {
151 #if ENABLE(WEB_INTENTS)
152     EWK_INTENT_WK_GET_OR_RETURN(intent, wkIntent, 0);
153
154     Eina_List* listOfSuggestions = 0;
155     WKRetainPtr<WKArrayRef> wkSuggestions(AdoptWK, WKIntentDataCopySuggestions(wkIntent));
156     const size_t numSuggestions = WKArrayGetSize(wkSuggestions.get());
157     for (size_t i = 0; i < numSuggestions; ++i) {
158         WKURLRef wkSuggestion = static_cast<WKURLRef>(WKArrayGetItemAtIndex(wkSuggestions.get(), i));
159         listOfSuggestions = eina_list_append(listOfSuggestions, eina_stringshare_add(toImpl(wkSuggestion)->string().utf8().data()));
160     }
161
162     return listOfSuggestions;
163 #else
164     return 0;
165 #endif
166 }
167
168 const char* ewk_intent_extra_get(const Ewk_Intent* intent, const char* key)
169 {
170 #if ENABLE(WEB_INTENTS)
171     EWK_INTENT_WK_GET_OR_RETURN(intent, wkIntent, 0);
172
173     WKRetainPtr<WKStringRef> keyRef = adoptWK(WKStringCreateWithUTF8CString(key));
174     WKRetainPtr<WKStringRef> wkValue(AdoptWK, WKIntentDataCopyExtraValue(wkIntent, keyRef.get()));
175     String value = toImpl(wkValue.get())->string();
176     if (value.isEmpty())
177         return 0;
178
179     return eina_stringshare_add(value.utf8().data());
180 #else
181     return 0;
182 #endif
183 }
184
185 Eina_List* ewk_intent_extra_names_get(const Ewk_Intent* intent)
186 {
187 #if ENABLE(WEB_INTENTS)
188     EWK_INTENT_WK_GET_OR_RETURN(intent, wkIntent, 0);
189
190     Eina_List* listOfKeys = 0;
191     WKRetainPtr<WKDictionaryRef> wkExtras(AdoptWK, WKIntentDataCopyExtras(wkIntent));
192     WKRetainPtr<WKArrayRef> wkKeys(AdoptWK, WKDictionaryCopyKeys(wkExtras.get()));
193     const size_t numKeys = WKArrayGetSize(wkKeys.get());
194     for (size_t i = 0; i < numKeys; ++i) {
195         WKStringRef wkKey = static_cast<WKStringRef>(WKArrayGetItemAtIndex(wkKeys.get(), i));
196         listOfKeys = eina_list_append(listOfKeys, eina_stringshare_add(toImpl(wkKey)->string().utf8().data()));
197     }
198
199     return listOfKeys;
200 #else
201     return 0;
202 #endif
203 }
204
205 #if ENABLE(WEB_INTENTS)
206 Ewk_Intent* ewk_intent_new(WKIntentDataRef intentData)
207 {
208     EINA_SAFETY_ON_NULL_RETURN_VAL(intentData, 0);
209
210     return new Ewk_Intent(intentData);
211 }
212
213 WKIntentDataRef ewk_intent_WKIntentDataRef_get(const Ewk_Intent* intent)
214 {
215     EINA_SAFETY_ON_NULL_RETURN_VAL(intent, 0);
216     return intent->wkIntent.get();
217 }
218 #endif