1/*
2 * Copyright (C) 2012 Igalia S.L.
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 "WebKitFormSubmissionRequest.h"
22
23#include "ImmutableDictionary.h"
24#include "WebFormSubmissionListenerProxy.h"
25#include "WebKitFormSubmissionRequestPrivate.h"
26#include <wtf/gobject/GRefPtr.h>
27#include <wtf/text/CString.h>
28
29using namespace WebKit;
30
31/**
32 * SECTION: WebKitFormSubmissionRequest
33 * @Short_description: Represents a form submission request
34 * @Title: WebKitFormSubmissionRequest
35 *
36 * When a form is about to be submitted in a #WebKitWebView, the
37 * #WebKitWebView::submit-form signal is emitted. Its request argument
38 * contains information about the text fields of the form, that are
39 * typically used to store login information, returned in a
40 * #GHashTable by the webkit_form_submission_request_get_text_fields()
41 * method, and you can finally submit the form with
42 * webkit_form_submission_request_submit().
43 *
44 */
45
46struct _WebKitFormSubmissionRequestPrivate {
47    RefPtr<ImmutableDictionary> webValues;
48    RefPtr<WebFormSubmissionListenerProxy> listener;
49    GRefPtr<GHashTable> values;
50    bool handledRequest;
51};
52
53WEBKIT_DEFINE_TYPE(WebKitFormSubmissionRequest, webkit_form_submission_request, G_TYPE_OBJECT)
54
55static void webkitFormSubmissionRequestDispose(GObject* object)
56{
57    WebKitFormSubmissionRequest* request = WEBKIT_FORM_SUBMISSION_REQUEST(object);
58
59    // Make sure the request is always handled before finalizing.
60    if (!request->priv->handledRequest)
61        webkit_form_submission_request_submit(request);
62
63    G_OBJECT_CLASS(webkit_form_submission_request_parent_class)->dispose(object);
64}
65
66static void webkit_form_submission_request_class_init(WebKitFormSubmissionRequestClass* requestClass)
67{
68    GObjectClass* objectClass = G_OBJECT_CLASS(requestClass);
69    objectClass->dispose = webkitFormSubmissionRequestDispose;
70}
71
72WebKitFormSubmissionRequest* webkitFormSubmissionRequestCreate(ImmutableDictionary* values, WebFormSubmissionListenerProxy* listener)
73{
74    WebKitFormSubmissionRequest* request = WEBKIT_FORM_SUBMISSION_REQUEST(g_object_new(WEBKIT_TYPE_FORM_SUBMISSION_REQUEST, NULL));
75    request->priv->webValues = values;
76    request->priv->listener = listener;
77    return request;
78}
79
80/**
81 * webkit_form_submission_request_get_text_fields:
82 * @request: a #WebKitFormSubmissionRequest
83 *
84 * Get a #GHashTable with the values of the text fields contained in the form
85 * associated to @request.
86 *
87 * Returns: (transfer none): a #GHashTable with the form text fields, or %NULL if the
88 *    form doesn't contain text fields.
89 */
90GHashTable* webkit_form_submission_request_get_text_fields(WebKitFormSubmissionRequest* request)
91{
92    g_return_val_if_fail(WEBKIT_IS_FORM_SUBMISSION_REQUEST(request), 0);
93
94    if (request->priv->values)
95        return request->priv->values.get();
96
97    if (!request->priv->webValues->size())
98        return 0;
99
100    request->priv->values = adoptGRef(g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free));
101
102    const ImmutableDictionary::MapType& map = request->priv->webValues->map();
103    ImmutableDictionary::MapType::const_iterator end = map.end();
104    for (ImmutableDictionary::MapType::const_iterator it = map.begin(); it != end; ++it) {
105        WebString* value = static_cast<WebString*>(it->value.get());
106        g_hash_table_insert(request->priv->values.get(), g_strdup(it->key.utf8().data()), g_strdup(value->string().utf8().data()));
107    }
108
109    request->priv->webValues = 0;
110
111    return request->priv->values.get();
112}
113
114/**
115 * webkit_form_submission_request_submit:
116 * @request: a #WebKitFormSubmissionRequest
117 *
118 * Continue the form submission.
119 */
120void webkit_form_submission_request_submit(WebKitFormSubmissionRequest* request)
121{
122    g_return_if_fail(WEBKIT_IS_FORM_SUBMISSION_REQUEST(request));
123
124    request->priv->listener->continueSubmission();
125    request->priv->handledRequest = true;
126}
127