Create string tightly when retrive string from cbhm callback event
[framework/web/webkit-efl.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 "WKString.h"
33 #include "WKURL.h"
34 #include "ewk_intent_private.h"
35 #include <wtf/text/CString.h>
36
37 using namespace WebKit;
38
39 #if ENABLE(WEB_INTENTS)
40 Ewk_Intent::Ewk_Intent(WKIntentDataRef intentRef)
41     : m_wkIntent(intentRef)
42     , m_action(AdoptWK, WKIntentDataCopyAction(intentRef))
43     , m_type(AdoptWK, WKIntentDataCopyType(intentRef))
44     , m_service(AdoptWK, WKIntentDataCopyService(intentRef))
45 { }
46
47 WebIntentData* Ewk_Intent::webIntentData() const
48 {
49     return toImpl(m_wkIntent.get());
50 }
51
52 const char* Ewk_Intent::action() const
53 {
54     return m_action;
55 }
56
57 const char* Ewk_Intent::type() const
58 {
59     return m_type;
60 }
61
62 const char* Ewk_Intent::service() const
63 {
64     return m_service;
65 }
66
67 WKRetainPtr<WKArrayRef> Ewk_Intent::suggestions() const
68 {
69     return adoptWK(WKIntentDataCopySuggestions(m_wkIntent.get()));
70 }
71
72 String Ewk_Intent::extra(const char* key) const
73 {
74     WKRetainPtr<WKStringRef> keyRef = adoptWK(WKStringCreateWithUTF8CString(key));
75     WKRetainPtr<WKStringRef> wkValue(AdoptWK, WKIntentDataCopyExtraValue(m_wkIntent.get(), keyRef.get()));
76     return toImpl(wkValue.get())->string();
77 }
78
79 WKRetainPtr<WKArrayRef> Ewk_Intent::extraKeys() const
80 {
81     WKRetainPtr<WKDictionaryRef> wkExtras(AdoptWK, WKIntentDataCopyExtras(m_wkIntent.get()));
82     return adoptWK(WKDictionaryCopyKeys(wkExtras.get()));
83 }
84 #endif
85
86 Ewk_Intent* ewk_intent_ref(Ewk_Intent* intent)
87 {
88 #if ENABLE(WEB_INTENTS)
89     EINA_SAFETY_ON_NULL_RETURN_VAL(intent, 0);
90
91     intent->ref();
92 #endif
93
94     return intent;
95 }
96
97 void ewk_intent_unref(Ewk_Intent* intent)
98 {
99 #if ENABLE(WEB_INTENTS)
100     EINA_SAFETY_ON_NULL_RETURN(intent);
101
102     intent->deref();
103 #endif
104 }
105
106 const char* ewk_intent_action_get(const Ewk_Intent* intent)
107 {
108     EINA_SAFETY_ON_NULL_RETURN_VAL(intent, 0);
109
110 #if ENABLE(WEB_INTENTS)
111     return intent->action();
112 #else
113     return 0;
114 #endif
115 }
116
117 const char* ewk_intent_type_get(const Ewk_Intent* intent)
118 {
119     EINA_SAFETY_ON_NULL_RETURN_VAL(intent, 0);
120
121 #if ENABLE(WEB_INTENTS)
122     return intent->type();
123 #else
124     return 0;
125 #endif
126 }
127
128 const char* ewk_intent_service_get(const Ewk_Intent* intent)
129 {
130     EINA_SAFETY_ON_NULL_RETURN_VAL(intent, 0);
131
132 #if ENABLE(WEB_INTENTS)
133     return intent->service();
134 #else
135     return 0;
136 #endif
137 }
138
139 Eina_List* ewk_intent_suggestions_get(const Ewk_Intent* intent)
140 {
141     EINA_SAFETY_ON_NULL_RETURN_VAL(intent, 0);
142
143 #if ENABLE(WEB_INTENTS)
144     Eina_List* listOfSuggestions = 0;
145     WKRetainPtr<WKArrayRef> wkSuggestions = intent->suggestions();
146     const size_t numSuggestions = WKArrayGetSize(wkSuggestions.get());
147     for (size_t i = 0; i < numSuggestions; ++i) {
148         WKURLRef wkSuggestion = static_cast<WKURLRef>(WKArrayGetItemAtIndex(wkSuggestions.get(), i));
149         listOfSuggestions = eina_list_append(listOfSuggestions, eina_stringshare_add(toImpl(wkSuggestion)->string().utf8().data()));
150     }
151
152     return listOfSuggestions;
153
154 #else
155     return 0;
156 #endif
157 }
158
159 const char* ewk_intent_extra_get(const Ewk_Intent* intent, const char* key)
160 {
161     EINA_SAFETY_ON_NULL_RETURN_VAL(intent, 0);
162
163 #if ENABLE(WEB_INTENTS)
164     String value = intent->extra(key);
165
166     if (value.isEmpty())
167         return 0;
168
169     return eina_stringshare_add(value.utf8().data());
170 #else
171     return 0;
172 #endif
173 }
174
175 Eina_List* ewk_intent_extra_names_get(const Ewk_Intent* intent)
176 {
177     EINA_SAFETY_ON_NULL_RETURN_VAL(intent, 0);
178
179 #if ENABLE(WEB_INTENTS)
180     Eina_List* listOfKeys = 0;
181     WKRetainPtr<WKArrayRef> wkKeys = intent->extraKeys();
182     const size_t numKeys = WKArrayGetSize(wkKeys.get());
183     for (size_t i = 0; i < numKeys; ++i) {
184         WKStringRef wkKey = static_cast<WKStringRef>(WKArrayGetItemAtIndex(wkKeys.get(), i));
185         listOfKeys = eina_list_append(listOfKeys, eina_stringshare_add(toImpl(wkKey)->string().utf8().data()));
186     }
187
188     return listOfKeys;
189 #else
190     return 0;
191 #endif
192 }