Create string tightly when retrive string from cbhm callback event
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / ewk_hit_test.cpp
1 /*
2    Copyright (C) 2011 Samsung Electronics
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library General Public
6     License as published by the Free Software Foundation; either
7     version 2 of the License, or (at your option) any later version.
8
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Library General Public License for more details.
13
14     You should have received a copy of the GNU Library General Public License
15     along with this library; see the file COPYING.LIB.  If not, write to
16     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17     Boston, MA 02110-1301, USA.
18 */
19
20 #include "config.h"
21 #include "ewk_hit_test.h"
22
23 #include "ewk_hit_test_private.h"
24 #include <wtf/text/CString.h>
25 #include <cairo.h>
26
27 using namespace WebKit;
28 using namespace WebCore;
29
30 /** Structure used to report hit test result */
31 struct _Ewk_Hit_Test {
32     Ewk_Hit_Test_Result_Context context;
33
34     CString linkURI;
35     CString linkTitle; /**< the title of link */
36     CString linkLabel; /**< the text of the link */
37     CString imageURI;
38     CString mediaURI;
39
40     struct {
41         CString tagName; /**<tag name for hit element */
42         CString nodeValue; /**<node value for hit element */
43         Eina_Hash* attributeHash; /**<attribute data for hit element */
44     } nodeData;
45
46     struct {
47         void* buffer; /**<image buffer for hit element */
48         unsigned int length; /**<image buffer length for hit element */
49         CString fileNameExtension; /**<image filename extension for hit element */
50     } imageData;
51 };
52
53 #if ENABLE(TIZEN_WEBKIT2_HIT_TEST)
54 static void freeHitTestAttributeHashData(void* data)
55 {
56     EINA_SAFETY_ON_NULL_RETURN(data);
57     eina_stringshare_del(static_cast<Eina_Stringshare*>(data));
58 }
59
60 Ewk_Hit_Test* ewkHitTestCreate(WebHitTestResult::Data& hitTestResultData)
61 {
62     Ewk_Hit_Test* hitTest = new Ewk_Hit_Test;
63
64     hitTest->linkURI = hitTestResultData.absoluteLinkURL.utf8();
65     hitTest->linkTitle = hitTestResultData.linkTitle.utf8();
66     hitTest->linkLabel = hitTestResultData.linkLabel.utf8();
67     hitTest->imageURI = hitTestResultData.absoluteImageURL.utf8();
68     hitTest->mediaURI = hitTestResultData.absoluteMediaURL.utf8();
69
70     int context = hitTestResultData.context;
71     hitTest->context = static_cast<Ewk_Hit_Test_Result_Context>(context);
72
73
74     hitTest->nodeData.attributeHash = 0;
75     if (hitTestResultData.hitTestMode & EWK_HIT_TEST_MODE_NODE_DATA) {
76         hitTest->nodeData.tagName = hitTestResultData.nodeData.tagName.utf8();
77         hitTest->nodeData.nodeValue = hitTestResultData.nodeData.nodeValue.utf8();
78
79         if (!hitTestResultData.nodeData.attributeMap.isEmpty()) {
80             hitTest->nodeData.attributeHash = eina_hash_string_superfast_new(freeHitTestAttributeHashData);
81
82             WebHitTestResult::Data::NodeData::AttributeMap::iterator attributeMapEnd = hitTestResultData.nodeData.attributeMap.end();
83             for (WebHitTestResult::Data::NodeData::AttributeMap::iterator it = hitTestResultData.nodeData.attributeMap.begin(); it != attributeMapEnd; ++it) {
84                 eina_hash_add(hitTest->nodeData.attributeHash, it->first.utf8().data(), eina_stringshare_add(it->second.utf8().data()));
85             }
86         }
87     }
88
89     hitTest->imageData.buffer = 0;
90     hitTest->imageData.length = 0;
91     if ((hitTestResultData.hitTestMode & EWK_HIT_TEST_MODE_IMAGE_DATA) && (context & EWK_HIT_TEST_RESULT_CONTEXT_IMAGE)) {
92         if (!hitTestResultData.imageData.data.isEmpty()) {
93             hitTest->imageData.length = hitTestResultData.imageData.data.size();
94             hitTest->imageData.fileNameExtension = hitTestResultData.imageData.fileNameExtension.utf8();
95
96             if (hitTest->imageData.length > 0) {
97                 hitTest->imageData.buffer = calloc(1, hitTest->imageData.length);
98                 if (hitTest->imageData.buffer)
99                     memcpy(hitTest->imageData.buffer, hitTestResultData.imageData.data.data(), hitTest->imageData.length);
100             }
101         }
102     }
103
104     return hitTest;
105 }
106 #endif
107
108 void ewk_hit_test_free(Ewk_Hit_Test* hitTest)
109 {
110 #if ENABLE(TIZEN_WEBKIT2_HIT_TEST)
111     EINA_SAFETY_ON_NULL_RETURN(hitTest);
112
113     if (hitTest->nodeData.attributeHash)
114         eina_hash_free(hitTest->nodeData.attributeHash);
115
116     if (hitTest->imageData.buffer) {
117         free(hitTest->imageData.buffer);
118         hitTest->imageData.buffer = 0;
119         hitTest->imageData.length = 0;
120     }
121
122     delete hitTest;
123 #endif
124 }
125
126 Ewk_Hit_Test_Result_Context ewk_hit_test_result_context_get(Ewk_Hit_Test* hitTest)
127 {
128 #if ENABLE(TIZEN_WEBKIT2_HIT_TEST)
129     EINA_SAFETY_ON_NULL_RETURN_VAL(hitTest, EWK_HIT_TEST_RESULT_CONTEXT_DOCUMENT);
130
131     return hitTest->context;
132 #else
133     return EWK_HIT_TEST_RESULT_CONTEXT_DOCUMENT;
134 #endif
135 }
136
137 const char* ewk_hit_test_link_uri_get(Ewk_Hit_Test* hitTest)
138 {
139 #if ENABLE(TIZEN_WEBKIT2_HIT_TEST)
140     EINA_SAFETY_ON_NULL_RETURN_VAL(hitTest, 0);
141
142     return hitTest->linkURI.data();
143 #else
144     return 0;
145 #endif
146 }
147
148 const char* ewk_hit_test_link_title_get(Ewk_Hit_Test* hitTest)
149 {
150 #if ENABLE(TIZEN_WEBKIT2_HIT_TEST)
151     EINA_SAFETY_ON_NULL_RETURN_VAL(hitTest, 0);
152
153     return hitTest->linkTitle.data();
154 #else
155     return 0;
156 #endif
157 }
158
159 const char* ewk_hit_test_link_label_get(Ewk_Hit_Test* hitTest)
160 {
161 #if ENABLE(TIZEN_WEBKIT2_HIT_TEST)
162     EINA_SAFETY_ON_NULL_RETURN_VAL(hitTest, 0);
163
164     return hitTest->linkLabel.data();
165 #else
166     return 0;
167 #endif
168 }
169
170 const char* ewk_hit_test_image_uri_get(Ewk_Hit_Test* hitTest)
171 {
172 #if ENABLE(TIZEN_WEBKIT2_HIT_TEST)
173     EINA_SAFETY_ON_NULL_RETURN_VAL(hitTest, 0);
174
175     return hitTest->imageURI.data();
176 #else
177     return 0;
178 #endif
179 }
180
181 const char* ewk_hit_test_media_uri_get(Ewk_Hit_Test* hitTest)
182 {
183 #if ENABLE(TIZEN_WEBKIT2_HIT_TEST)
184     EINA_SAFETY_ON_NULL_RETURN_VAL(hitTest, 0);
185
186     return hitTest->mediaURI.data();
187 #else
188     return 0;
189 #endif
190 }
191
192 const char* ewk_hit_test_tag_name_get(Ewk_Hit_Test* hitTest)
193 {
194 #if ENABLE(TIZEN_WEBKIT2_HIT_TEST)
195     EINA_SAFETY_ON_NULL_RETURN_VAL(hitTest, 0);
196
197     return hitTest->nodeData.tagName.data();
198 #else
199     return 0;
200 #endif
201 }
202
203 const char* ewk_hit_test_node_value_get(Ewk_Hit_Test* hitTest)
204 {
205 #if ENABLE(TIZEN_WEBKIT2_HIT_TEST)
206     EINA_SAFETY_ON_NULL_RETURN_VAL(hitTest, 0);
207
208     return hitTest->nodeData.nodeValue.data();
209 #else
210     return 0;
211 #endif
212 }
213
214 Eina_Hash* ewk_hit_test_attribute_hash_get(Ewk_Hit_Test* hitTest)
215 {
216 #if ENABLE(TIZEN_WEBKIT2_HIT_TEST)
217     EINA_SAFETY_ON_NULL_RETURN_VAL(hitTest, 0);
218
219     return hitTest->nodeData.attributeHash;
220 #else
221     return 0;
222 #endif
223 }
224
225 void* ewk_hit_test_image_buffer_get(Ewk_Hit_Test* hitTest)
226 {
227 #if ENABLE(TIZEN_WEBKIT2_HIT_TEST)
228     EINA_SAFETY_ON_NULL_RETURN_VAL(hitTest, 0);
229
230     return hitTest->imageData.buffer;
231 #else
232     return 0;
233 #endif
234 }
235
236 unsigned int ewk_hit_test_image_buffer_length_get(Ewk_Hit_Test* hitTest)
237 {
238 #if ENABLE(TIZEN_WEBKIT2_HIT_TEST)
239     EINA_SAFETY_ON_NULL_RETURN_VAL(hitTest, 0);
240
241     return hitTest->imageData.length;
242 #else
243     return 0;
244 #endif
245 }
246
247 const char* ewk_hit_test_image_file_name_extension_get(Ewk_Hit_Test* hitTest)
248 {
249 #if ENABLE(TIZEN_WEBKIT2_HIT_TEST)
250     EINA_SAFETY_ON_NULL_RETURN_VAL(hitTest, 0);
251
252     return hitTest->imageData.fileNameExtension.data();
253 #else
254     return 0;
255 #endif
256 }
257