1/*
2 * Copyright (C) 2010-2018 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'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "config.h"
26
27#include "WebAutomationSessionProxy.h"
28
29#include "ArgumentCoders.h"
30#include "CoordinateSystem.h"
31#include "Decoder.h"
32#include "HandleMessage.h"
33#include "WebAutomationSessionProxyMessages.h"
34#include "WebCoreArgumentCoders.h"
35#include <WebCore/Cookie.h>
36#include <WebCore/IntPoint.h>
37#include <WebCore/IntRect.h>
38#include <wtf/Optional.h>
39#include <wtf/Vector.h>
40#include <wtf/text/WTFString.h>
41
42namespace Messages {
43
44namespace WebAutomationSessionProxy {
45
46void ResolveChildFrameWithOrdinal::callReply(IPC::Decoder& decoder, CompletionHandler<void(Optional<String>&&, uint64_t&&)>&& completionHandler)
47{
48 Optional<Optional<String>> errorType;
49 decoder >> errorType;
50 if (!errorType) {
51 ASSERT_NOT_REACHED();
52 return;
53 }
54 Optional<uint64_t> frameID;
55 decoder >> frameID;
56 if (!frameID) {
57 ASSERT_NOT_REACHED();
58 return;
59 }
60 completionHandler(WTFMove(*errorType), WTFMove(*frameID));
61}
62
63void ResolveChildFrameWithOrdinal::cancelReply(CompletionHandler<void(Optional<String>&&, uint64_t&&)>&& completionHandler)
64{
65 completionHandler({ }, { });
66}
67
68void ResolveChildFrameWithOrdinal::send(std::unique_ptr<IPC::Encoder>&& encoder, IPC::Connection& connection, const Optional<String>& errorType, uint64_t frameID)
69{
70 *encoder << errorType;
71 *encoder << frameID;
72 connection.sendSyncReply(WTFMove(encoder));
73}
74
75void ResolveChildFrameWithNodeHandle::callReply(IPC::Decoder& decoder, CompletionHandler<void(Optional<String>&&, uint64_t&&)>&& completionHandler)
76{
77 Optional<Optional<String>> errorType;
78 decoder >> errorType;
79 if (!errorType) {
80 ASSERT_NOT_REACHED();
81 return;
82 }
83 Optional<uint64_t> frameID;
84 decoder >> frameID;
85 if (!frameID) {
86 ASSERT_NOT_REACHED();
87 return;
88 }
89 completionHandler(WTFMove(*errorType), WTFMove(*frameID));
90}
91
92void ResolveChildFrameWithNodeHandle::cancelReply(CompletionHandler<void(Optional<String>&&, uint64_t&&)>&& completionHandler)
93{
94 completionHandler({ }, { });
95}
96
97void ResolveChildFrameWithNodeHandle::send(std::unique_ptr<IPC::Encoder>&& encoder, IPC::Connection& connection, const Optional<String>& errorType, uint64_t frameID)
98{
99 *encoder << errorType;
100 *encoder << frameID;
101 connection.sendSyncReply(WTFMove(encoder));
102}
103
104void ResolveChildFrameWithName::callReply(IPC::Decoder& decoder, CompletionHandler<void(Optional<String>&&, uint64_t&&)>&& completionHandler)
105{
106 Optional<Optional<String>> errorType;
107 decoder >> errorType;
108 if (!errorType) {
109 ASSERT_NOT_REACHED();
110 return;
111 }
112 Optional<uint64_t> frameID;
113 decoder >> frameID;
114 if (!frameID) {
115 ASSERT_NOT_REACHED();
116 return;
117 }
118 completionHandler(WTFMove(*errorType), WTFMove(*frameID));
119}
120
121void ResolveChildFrameWithName::cancelReply(CompletionHandler<void(Optional<String>&&, uint64_t&&)>&& completionHandler)
122{
123 completionHandler({ }, { });
124}
125
126void ResolveChildFrameWithName::send(std::unique_ptr<IPC::Encoder>&& encoder, IPC::Connection& connection, const Optional<String>& errorType, uint64_t frameID)
127{
128 *encoder << errorType;
129 *encoder << frameID;
130 connection.sendSyncReply(WTFMove(encoder));
131}
132
133void ResolveParentFrame::callReply(IPC::Decoder& decoder, CompletionHandler<void(Optional<String>&&, uint64_t&&)>&& completionHandler)
134{
135 Optional<Optional<String>> errorType;
136 decoder >> errorType;
137 if (!errorType) {
138 ASSERT_NOT_REACHED();
139 return;
140 }
141 Optional<uint64_t> frameID;
142 decoder >> frameID;
143 if (!frameID) {
144 ASSERT_NOT_REACHED();
145 return;
146 }
147 completionHandler(WTFMove(*errorType), WTFMove(*frameID));
148}
149
150void ResolveParentFrame::cancelReply(CompletionHandler<void(Optional<String>&&, uint64_t&&)>&& completionHandler)
151{
152 completionHandler({ }, { });
153}
154
155void ResolveParentFrame::send(std::unique_ptr<IPC::Encoder>&& encoder, IPC::Connection& connection, const Optional<String>& errorType, uint64_t frameID)
156{
157 *encoder << errorType;
158 *encoder << frameID;
159 connection.sendSyncReply(WTFMove(encoder));
160}
161
162void ComputeElementLayout::callReply(IPC::Decoder& decoder, CompletionHandler<void(Optional<String>&&, WebCore::IntRect&&, Optional<WebCore::IntPoint>&&, bool&&)>&& completionHandler)
163{
164 Optional<Optional<String>> errorType;
165 decoder >> errorType;
166 if (!errorType) {
167 ASSERT_NOT_REACHED();
168 return;
169 }
170 Optional<WebCore::IntRect> rect;
171 decoder >> rect;
172 if (!rect) {
173 ASSERT_NOT_REACHED();
174 return;
175 }
176 Optional<Optional<WebCore::IntPoint>> inViewCenterPoint;
177 decoder >> inViewCenterPoint;
178 if (!inViewCenterPoint) {
179 ASSERT_NOT_REACHED();
180 return;
181 }
182 Optional<bool> isObscured;
183 decoder >> isObscured;
184 if (!isObscured) {
185 ASSERT_NOT_REACHED();
186 return;
187 }
188 completionHandler(WTFMove(*errorType), WTFMove(*rect), WTFMove(*inViewCenterPoint), WTFMove(*isObscured));
189}
190
191void ComputeElementLayout::cancelReply(CompletionHandler<void(Optional<String>&&, WebCore::IntRect&&, Optional<WebCore::IntPoint>&&, bool&&)>&& completionHandler)
192{
193 completionHandler({ }, { }, { }, { });
194}
195
196void ComputeElementLayout::send(std::unique_ptr<IPC::Encoder>&& encoder, IPC::Connection& connection, const Optional<String>& errorType, const WebCore::IntRect& rect, const Optional<WebCore::IntPoint>& inViewCenterPoint, bool isObscured)
197{
198 *encoder << errorType;
199 *encoder << rect;
200 *encoder << inViewCenterPoint;
201 *encoder << isObscured;
202 connection.sendSyncReply(WTFMove(encoder));
203}
204
205void SelectOptionElement::callReply(IPC::Decoder& decoder, CompletionHandler<void(Optional<String>&&)>&& completionHandler)
206{
207 Optional<Optional<String>> errorType;
208 decoder >> errorType;
209 if (!errorType) {
210 ASSERT_NOT_REACHED();
211 return;
212 }
213 completionHandler(WTFMove(*errorType));
214}
215
216void SelectOptionElement::cancelReply(CompletionHandler<void(Optional<String>&&)>&& completionHandler)
217{
218 completionHandler({ });
219}
220
221void SelectOptionElement::send(std::unique_ptr<IPC::Encoder>&& encoder, IPC::Connection& connection, const Optional<String>& errorType)
222{
223 *encoder << errorType;
224 connection.sendSyncReply(WTFMove(encoder));
225}
226
227void GetCookiesForFrame::callReply(IPC::Decoder& decoder, CompletionHandler<void(Optional<String>&&, Vector<WebCore::Cookie>&&)>&& completionHandler)
228{
229 Optional<Optional<String>> errorType;
230 decoder >> errorType;
231 if (!errorType) {
232 ASSERT_NOT_REACHED();
233 return;
234 }
235 Optional<Vector<WebCore::Cookie>> cookies;
236 decoder >> cookies;
237 if (!cookies) {
238 ASSERT_NOT_REACHED();
239 return;
240 }
241 completionHandler(WTFMove(*errorType), WTFMove(*cookies));
242}
243
244void GetCookiesForFrame::cancelReply(CompletionHandler<void(Optional<String>&&, Vector<WebCore::Cookie>&&)>&& completionHandler)
245{
246 completionHandler({ }, { });
247}
248
249void GetCookiesForFrame::send(std::unique_ptr<IPC::Encoder>&& encoder, IPC::Connection& connection, const Optional<String>& errorType, const Vector<WebCore::Cookie>& cookies)
250{
251 *encoder << errorType;
252 *encoder << cookies;
253 connection.sendSyncReply(WTFMove(encoder));
254}
255
256void DeleteCookie::callReply(IPC::Decoder& decoder, CompletionHandler<void(Optional<String>&&)>&& completionHandler)
257{
258 Optional<Optional<String>> errorType;
259 decoder >> errorType;
260 if (!errorType) {
261 ASSERT_NOT_REACHED();
262 return;
263 }
264 completionHandler(WTFMove(*errorType));
265}
266
267void DeleteCookie::cancelReply(CompletionHandler<void(Optional<String>&&)>&& completionHandler)
268{
269 completionHandler({ });
270}
271
272void DeleteCookie::send(std::unique_ptr<IPC::Encoder>&& encoder, IPC::Connection& connection, const Optional<String>& errorType)
273{
274 *encoder << errorType;
275 connection.sendSyncReply(WTFMove(encoder));
276}
277
278} // namespace WebAutomationSessionProxy
279
280} // namespace Messages
281
282namespace WebKit {
283
284void WebAutomationSessionProxy::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
285{
286 if (decoder.messageName() == Messages::WebAutomationSessionProxy::EvaluateJavaScriptFunction::name()) {
287 IPC::handleMessage<Messages::WebAutomationSessionProxy::EvaluateJavaScriptFunction>(decoder, this, &WebAutomationSessionProxy::evaluateJavaScriptFunction);
288 return;
289 }
290 if (decoder.messageName() == Messages::WebAutomationSessionProxy::ResolveChildFrameWithOrdinal::name()) {
291 IPC::handleMessageAsync<Messages::WebAutomationSessionProxy::ResolveChildFrameWithOrdinal>(connection, decoder, this, &WebAutomationSessionProxy::resolveChildFrameWithOrdinal);
292 return;
293 }
294 if (decoder.messageName() == Messages::WebAutomationSessionProxy::ResolveChildFrameWithNodeHandle::name()) {
295 IPC::handleMessageAsync<Messages::WebAutomationSessionProxy::ResolveChildFrameWithNodeHandle>(connection, decoder, this, &WebAutomationSessionProxy::resolveChildFrameWithNodeHandle);
296 return;
297 }
298 if (decoder.messageName() == Messages::WebAutomationSessionProxy::ResolveChildFrameWithName::name()) {
299 IPC::handleMessageAsync<Messages::WebAutomationSessionProxy::ResolveChildFrameWithName>(connection, decoder, this, &WebAutomationSessionProxy::resolveChildFrameWithName);
300 return;
301 }
302 if (decoder.messageName() == Messages::WebAutomationSessionProxy::ResolveParentFrame::name()) {
303 IPC::handleMessageAsync<Messages::WebAutomationSessionProxy::ResolveParentFrame>(connection, decoder, this, &WebAutomationSessionProxy::resolveParentFrame);
304 return;
305 }
306 if (decoder.messageName() == Messages::WebAutomationSessionProxy::FocusFrame::name()) {
307 IPC::handleMessage<Messages::WebAutomationSessionProxy::FocusFrame>(decoder, this, &WebAutomationSessionProxy::focusFrame);
308 return;
309 }
310 if (decoder.messageName() == Messages::WebAutomationSessionProxy::ComputeElementLayout::name()) {
311 IPC::handleMessageAsync<Messages::WebAutomationSessionProxy::ComputeElementLayout>(connection, decoder, this, &WebAutomationSessionProxy::computeElementLayout);
312 return;
313 }
314 if (decoder.messageName() == Messages::WebAutomationSessionProxy::SelectOptionElement::name()) {
315 IPC::handleMessageAsync<Messages::WebAutomationSessionProxy::SelectOptionElement>(connection, decoder, this, &WebAutomationSessionProxy::selectOptionElement);
316 return;
317 }
318 if (decoder.messageName() == Messages::WebAutomationSessionProxy::TakeScreenshot::name()) {
319 IPC::handleMessage<Messages::WebAutomationSessionProxy::TakeScreenshot>(decoder, this, &WebAutomationSessionProxy::takeScreenshot);
320 return;
321 }
322 if (decoder.messageName() == Messages::WebAutomationSessionProxy::GetCookiesForFrame::name()) {
323 IPC::handleMessageAsync<Messages::WebAutomationSessionProxy::GetCookiesForFrame>(connection, decoder, this, &WebAutomationSessionProxy::getCookiesForFrame);
324 return;
325 }
326 if (decoder.messageName() == Messages::WebAutomationSessionProxy::DeleteCookie::name()) {
327 IPC::handleMessageAsync<Messages::WebAutomationSessionProxy::DeleteCookie>(connection, decoder, this, &WebAutomationSessionProxy::deleteCookie);
328 return;
329 }
330 UNUSED_PARAM(connection);
331 UNUSED_PARAM(decoder);
332 ASSERT_NOT_REACHED();
333}
334
335} // namespace WebKit
336
337