Create string tightly when retrive string from cbhm callback event
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / ewk_form_data.cpp
1 /*
2    Copyright (C) 2012 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_form_data.h"
22
23 #if OS(TIZEN)
24 #include "WKAPICast.h"
25 #include "WKArray.h"
26 #include "WKDictionary.h"
27 #include "WKFrame.h"
28 #include "WKRetainPtr.h"
29 #include "WKString.h"
30 #include "WKURL.h"
31 #include <wtf/OwnArrayPtr.h>
32 #include <wtf/text/CString.h>
33
34 using namespace WebKit;
35
36 struct _Ewk_Form_Data {
37     Eina_Hash* values;
38     const char* url;
39
40     _Ewk_Form_Data()
41         : values(NULL)
42         , url(NULL)
43     {
44
45     }
46 };
47
48 static void freeFormValues(void* data)
49 {
50     EINA_SAFETY_ON_NULL_RETURN(data);
51     eina_stringshare_del(static_cast<char*>(data));
52 }
53
54 Ewk_Form_Data* ewkFormDataCreate(WKFrameRef frame, WKDictionaryRef values)
55 {
56     WKRetainPtr<WKArrayRef> keys(AdoptWK, WKDictionaryCopyKeys(values));
57     int size = WKArrayGetSize(keys.get());
58     Eina_Hash* formValues = eina_hash_string_small_new(freeFormValues);
59     for (int i = 0; i < size; i++) {
60         WKStringRef key = static_cast<WKStringRef>(WKArrayGetItemAtIndex(keys.get(), i));
61         if (WKGetTypeID(key) == WKStringGetTypeID()) {
62             WKStringRef value = static_cast<WKStringRef>(WKDictionaryGetItemForKey(values, key));
63             if (WKGetTypeID(value) == WKStringGetTypeID()) {
64                 int length = WKStringGetMaximumUTF8CStringSize(key);
65                 if (length <= 0)
66                     continue;
67                 OwnArrayPtr<char> keyBuffer = adoptArrayPtr(new char[length]);
68                 WKStringGetUTF8CString(key, keyBuffer.get(), length);
69
70                 length = WKStringGetMaximumUTF8CStringSize(value);
71                 if (length <= 0)
72                     continue;
73                 OwnArrayPtr<char> valueBuffer = adoptArrayPtr(new char[length]);
74                 WKStringGetUTF8CString(value, valueBuffer.get(), length);
75                 eina_hash_add(formValues, keyBuffer.get(), eina_stringshare_add(valueBuffer.get()));
76             }
77         }
78     }
79
80
81     Ewk_Form_Data* formData = new Ewk_Form_Data;
82     formData->values = formValues;
83     WKRetainPtr<WKURLRef> wkUrl(AdoptWK, WKFrameCopyURL(frame));
84     if (wkUrl)
85         formData->url = eina_stringshare_add(toImpl(wkUrl.get())->string().utf8().data());
86
87     return formData;
88 }
89
90 void ewkFormDataDelete(Ewk_Form_Data* formData)
91 {
92     EINA_SAFETY_ON_NULL_RETURN(formData);
93
94     eina_stringshare_del(formData->url);
95     eina_hash_free(formData->values);
96
97     delete formData;
98 }
99
100 const char* ewk_form_data_url_get(Ewk_Form_Data* formData)
101 {
102     EINA_SAFETY_ON_NULL_RETURN_VAL(formData, 0);
103     return formData->url;
104 }
105
106 Eina_Hash* ewk_form_data_values_get(Ewk_Form_Data* formData)
107 {
108     EINA_SAFETY_ON_NULL_RETURN_VAL(formData, 0);
109     return formData->values;
110 }
111
112 #endif // #if OS(TIZEN)