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_form_submission_request.h"
28
29#include "WKAPICast.h"
30#include "WKArray.h"
31#include "WKBase.h"
32#include "WKEinaSharedString.h"
33#include "WKString.h"
34#include "ewk_form_submission_request_private.h"
35
36using namespace WebKit;
37
38EwkFormSubmissionRequest::EwkFormSubmissionRequest(WKDictionaryRef values, WKFormSubmissionListenerRef listener)
39    : m_wkValues(values)
40    , m_wkListener(listener)
41    , m_handledRequest(false)
42{ }
43
44EwkFormSubmissionRequest::~EwkFormSubmissionRequest()
45{
46    // Make sure the request is always handled before destroying.
47    if (!m_handledRequest)
48        WKFormSubmissionListenerContinue(m_wkListener.get());
49}
50
51Eina_Stringshare* EwkFormSubmissionRequest::copyFieldValue(const char* fieldName) const
52{
53    ASSERT(fieldName);
54    WKRetainPtr<WKStringRef> wkFieldName = adoptWK(WKStringCreateWithUTF8CString(fieldName));
55    WKStringRef wkValue = static_cast<WKStringRef>(WKDictionaryGetItemForKey(m_wkValues.get(), wkFieldName.get()));
56
57    return WKEinaSharedString(wkValue).leakString();
58}
59
60WKRetainPtr<WKArrayRef> EwkFormSubmissionRequest::fieldNames() const
61{
62    return adoptWK(WKDictionaryCopyKeys(m_wkValues.get()));
63}
64
65void EwkFormSubmissionRequest::submit()
66{
67    WKFormSubmissionListenerContinue(m_wkListener.get());
68    m_handledRequest = true;
69}
70
71Eina_List* ewk_form_submission_request_field_names_get(Ewk_Form_Submission_Request* request)
72{
73    EWK_OBJ_GET_IMPL_OR_RETURN(EwkFormSubmissionRequest, request, impl, nullptr);
74
75    Eina_List* fieldNames = 0;
76
77    WKRetainPtr<WKArrayRef> wkFieldNames = impl->fieldNames();
78    const size_t numKeys = WKArrayGetSize(wkFieldNames.get());
79    for (size_t i = 0; i < numKeys; ++i) {
80        WKStringRef wkFieldName = static_cast<WKStringRef>(WKArrayGetItemAtIndex(wkFieldNames.get(), i));
81        fieldNames = eina_list_append(fieldNames, WKEinaSharedString(wkFieldName).leakString());
82    }
83
84    return fieldNames;
85}
86
87const char* ewk_form_submission_request_field_value_get(Ewk_Form_Submission_Request* request, const char* name)
88{
89    EWK_OBJ_GET_IMPL_OR_RETURN(EwkFormSubmissionRequest, request, impl, nullptr);
90    EINA_SAFETY_ON_NULL_RETURN_VAL(name, nullptr);
91
92    return impl->copyFieldValue(name);
93}
94
95Eina_Bool ewk_form_submission_request_submit(Ewk_Form_Submission_Request* request)
96{
97    EWK_OBJ_GET_IMPL_OR_RETURN(EwkFormSubmissionRequest, request, impl, false);
98
99    impl->submit();
100
101    return true;
102}
103