1 | /* |
2 | * Copyright (C) 2010 Apple Inc. 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 "InjectedBundlePageFormClient.h" |
28 | |
29 | #include "APIArray.h" |
30 | #include "APIDictionary.h" |
31 | #include "InjectedBundleNodeHandle.h" |
32 | #include "WKAPICast.h" |
33 | #include "WKBundleAPICast.h" |
34 | #include "WebFrame.h" |
35 | #include "WebPage.h" |
36 | #include <WebCore/HTMLFormElement.h> |
37 | #include <WebCore/HTMLInputElement.h> |
38 | #include <WebCore/HTMLTextAreaElement.h> |
39 | |
40 | namespace WebKit { |
41 | using namespace WebCore; |
42 | |
43 | InjectedBundlePageFormClient::InjectedBundlePageFormClient(const WKBundlePageFormClientBase* client) |
44 | { |
45 | initialize(client); |
46 | } |
47 | |
48 | void InjectedBundlePageFormClient::didFocusTextField(WebPage* page, HTMLInputElement* inputElement, WebFrame* frame) |
49 | { |
50 | if (!m_client.didFocusTextField) |
51 | return; |
52 | |
53 | RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::getOrCreate(inputElement); |
54 | m_client.didFocusTextField(toAPI(page), toAPI(nodeHandle.get()), toAPI(frame), m_client.base.clientInfo); |
55 | } |
56 | |
57 | void InjectedBundlePageFormClient::textFieldDidBeginEditing(WebPage* page, HTMLInputElement* inputElement, WebFrame* frame) |
58 | { |
59 | if (!m_client.textFieldDidBeginEditing) |
60 | return; |
61 | |
62 | RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::getOrCreate(inputElement); |
63 | m_client.textFieldDidBeginEditing(toAPI(page), toAPI(nodeHandle.get()), toAPI(frame), m_client.base.clientInfo); |
64 | } |
65 | |
66 | void InjectedBundlePageFormClient::textFieldDidEndEditing(WebPage* page, HTMLInputElement* inputElement, WebFrame* frame) |
67 | { |
68 | if (!m_client.textFieldDidEndEditing) |
69 | return; |
70 | |
71 | RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::getOrCreate(inputElement); |
72 | m_client.textFieldDidEndEditing(toAPI(page), toAPI(nodeHandle.get()), toAPI(frame), m_client.base.clientInfo); |
73 | } |
74 | |
75 | void InjectedBundlePageFormClient::textDidChangeInTextField(WebPage* page, HTMLInputElement* inputElement, WebFrame* frame, bool initiatedByUserTyping) |
76 | { |
77 | if (!m_client.textDidChangeInTextField) |
78 | return; |
79 | |
80 | if (!initiatedByUserTyping) |
81 | return; |
82 | |
83 | RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::getOrCreate(inputElement); |
84 | m_client.textDidChangeInTextField(toAPI(page), toAPI(nodeHandle.get()), toAPI(frame), m_client.base.clientInfo); |
85 | } |
86 | |
87 | void InjectedBundlePageFormClient::textDidChangeInTextArea(WebPage* page, HTMLTextAreaElement* textAreaElement, WebFrame* frame) |
88 | { |
89 | if (!m_client.textDidChangeInTextArea) |
90 | return; |
91 | |
92 | RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::getOrCreate(textAreaElement); |
93 | m_client.textDidChangeInTextArea(toAPI(page), toAPI(nodeHandle.get()), toAPI(frame), m_client.base.clientInfo); |
94 | } |
95 | |
96 | static WKInputFieldActionType toWKInputFieldActionType(API::InjectedBundle::FormClient::InputFieldAction action) |
97 | { |
98 | switch (action) { |
99 | case API::InjectedBundle::FormClient::InputFieldAction::MoveUp: |
100 | return WKInputFieldActionTypeMoveUp; |
101 | case API::InjectedBundle::FormClient::InputFieldAction::MoveDown: |
102 | return WKInputFieldActionTypeMoveDown; |
103 | case API::InjectedBundle::FormClient::InputFieldAction::Cancel: |
104 | return WKInputFieldActionTypeCancel; |
105 | case API::InjectedBundle::FormClient::InputFieldAction::InsertTab: |
106 | return WKInputFieldActionTypeInsertTab; |
107 | case API::InjectedBundle::FormClient::InputFieldAction::InsertBacktab: |
108 | return WKInputFieldActionTypeInsertBacktab; |
109 | case API::InjectedBundle::FormClient::InputFieldAction::InsertNewline: |
110 | return WKInputFieldActionTypeInsertNewline; |
111 | case API::InjectedBundle::FormClient::InputFieldAction::InsertDelete: |
112 | return WKInputFieldActionTypeInsertDelete; |
113 | } |
114 | |
115 | ASSERT_NOT_REACHED(); |
116 | return WKInputFieldActionTypeCancel; |
117 | } |
118 | |
119 | bool InjectedBundlePageFormClient::shouldPerformActionInTextField(WebPage* page, HTMLInputElement* inputElement, API::InjectedBundle::FormClient::InputFieldAction actionType, WebFrame* frame) |
120 | { |
121 | if (!m_client.shouldPerformActionInTextField) |
122 | return false; |
123 | |
124 | RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::getOrCreate(inputElement); |
125 | return m_client.shouldPerformActionInTextField(toAPI(page), toAPI(nodeHandle.get()), toWKInputFieldActionType(actionType), toAPI(frame), m_client.base.clientInfo); |
126 | } |
127 | |
128 | void InjectedBundlePageFormClient::willSendSubmitEvent(WebPage* page, HTMLFormElement* formElement, WebFrame* frame, WebFrame* sourceFrame, const Vector<std::pair<String, String>>& values) |
129 | { |
130 | if (!m_client.willSendSubmitEvent) |
131 | return; |
132 | |
133 | RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::getOrCreate(formElement); |
134 | |
135 | API::Dictionary::MapType map; |
136 | for (size_t i = 0; i < values.size(); ++i) |
137 | map.set(values[i].first, API::String::create(values[i].second)); |
138 | auto textFieldsMap = API::Dictionary::create(WTFMove(map)); |
139 | |
140 | m_client.willSendSubmitEvent(toAPI(page), toAPI(nodeHandle.get()), toAPI(frame), toAPI(sourceFrame), toAPI(textFieldsMap.ptr()), m_client.base.clientInfo); |
141 | } |
142 | |
143 | void InjectedBundlePageFormClient::willSubmitForm(WebPage* page, HTMLFormElement* formElement, WebFrame* frame, WebFrame* sourceFrame, const Vector<std::pair<String, String>>& values, RefPtr<API::Object>& userData) |
144 | { |
145 | if (!m_client.willSubmitForm) |
146 | return; |
147 | |
148 | RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::getOrCreate(formElement); |
149 | |
150 | API::Dictionary::MapType map; |
151 | for (size_t i = 0; i < values.size(); ++i) |
152 | map.set(values[i].first, API::String::create(values[i].second)); |
153 | auto textFieldsMap = API::Dictionary::create(WTFMove(map)); |
154 | |
155 | WKTypeRef userDataToPass = 0; |
156 | m_client.willSubmitForm(toAPI(page), toAPI(nodeHandle.get()), toAPI(frame), toAPI(sourceFrame), toAPI(textFieldsMap.ptr()), &userDataToPass, m_client.base.clientInfo); |
157 | userData = adoptRef(toImpl(userDataToPass)); |
158 | } |
159 | |
160 | void InjectedBundlePageFormClient::didAssociateFormControls(WebPage* page, const Vector<RefPtr<WebCore::Element>>& elements, WebFrame* frame) |
161 | { |
162 | if (!m_client.didAssociateFormControls && !m_client.didAssociateFormControlsForFrame) |
163 | return; |
164 | |
165 | Vector<RefPtr<API::Object>> elementHandles; |
166 | elementHandles.reserveInitialCapacity(elements.size()); |
167 | |
168 | for (const auto& element : elements) |
169 | elementHandles.uncheckedAppend(InjectedBundleNodeHandle::getOrCreate(element.get())); |
170 | |
171 | if (!m_client.didAssociateFormControlsForFrame) { |
172 | m_client.didAssociateFormControls(toAPI(page), toAPI(API::Array::create(WTFMove(elementHandles)).ptr()), m_client.base.clientInfo); |
173 | return; |
174 | } |
175 | |
176 | m_client.didAssociateFormControlsForFrame(toAPI(page), toAPI(API::Array::create(WTFMove(elementHandles)).ptr()), toAPI(frame), m_client.base.clientInfo); |
177 | } |
178 | |
179 | bool InjectedBundlePageFormClient::shouldNotifyOnFormChanges(WebPage* page) |
180 | { |
181 | if (!m_client.shouldNotifyOnFormChanges) |
182 | return false; |
183 | |
184 | return m_client.shouldNotifyOnFormChanges(toAPI(page), m_client.base.clientInfo); |
185 | } |
186 | |
187 | } // namespace WebKit |
188 | |