blob: d605166f909ef016bf771e671927fb3ac10925cc [file] [log] [blame]
carlosgc@webkit.org8c9a9502017-07-18 07:20:33 +00001/*
2 * Copyright (C) 2017 Igalia S.L.
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 "SessionHost.h"
28
carlosgc@webkit.org8c9a9502017-07-18 07:20:33 +000029#include <wtf/text/StringBuilder.h>
30
carlosgc@webkit.org8c9a9502017-07-18 07:20:33 +000031namespace WebDriver {
32
33void SessionHost::inspectorDisconnected()
34{
carlosgc@webkit.org53c5c782017-08-16 11:38:22 +000035 // Browser closed or crashed, finish all pending commands with error.
commit-queue@webkit.org8631a872017-10-10 22:13:07 +000036 for (auto messageID : copyToVector(m_commandRequests.keys())) {
carlosgc@webkit.org53c5c782017-08-16 11:38:22 +000037 auto responseHandler = m_commandRequests.take(messageID);
38 responseHandler({ nullptr, true });
39 }
carlosgc@webkit.org8c9a9502017-07-18 07:20:33 +000040}
41
bburg@apple.com61f04d62017-11-28 19:58:16 +000042long SessionHost::sendCommandToBackend(const String& command, RefPtr<JSON::Object>&& parameters, Function<void (CommandResponse&&)>&& responseHandler)
carlosgc@webkit.org8c9a9502017-07-18 07:20:33 +000043{
44 static long lastSequenceID = 0;
45 long sequenceID = ++lastSequenceID;
46 m_commandRequests.add(sequenceID, WTFMove(responseHandler));
carlosgc@webkit.org8c9a9502017-07-18 07:20:33 +000047 StringBuilder messageBuilder;
darin@apple.com26437052021-05-13 16:18:32 +000048 messageBuilder.append("{\"id\":", sequenceID, ",\"method\":\"Automation.", command, '"');
49 if (parameters)
50 messageBuilder.append(",\"params\":", parameters->toJSONString());
carlosgc@webkit.org8c9a9502017-07-18 07:20:33 +000051 messageBuilder.append('}');
carlosgc@webkit.orgc9894a92019-11-22 14:22:34 +000052 sendMessageToBackend(messageBuilder.toString());
carlosgc@webkit.org8c9a9502017-07-18 07:20:33 +000053
54 return sequenceID;
55}
56
57void SessionHost::dispatchMessage(const String& message)
58{
drousso@apple.com7b9e7092020-09-10 19:23:17 +000059 auto messageValue = JSON::Value::parseJSON(message);
60 if (!messageValue)
carlosgc@webkit.org8c9a9502017-07-18 07:20:33 +000061 return;
62
drousso@apple.com7b9e7092020-09-10 19:23:17 +000063 auto messageObject = messageValue->asObject();
64 if (!messageObject)
carlosgc@webkit.org8c9a9502017-07-18 07:20:33 +000065 return;
66
drousso@apple.com7b9e7092020-09-10 19:23:17 +000067 auto sequenceID = messageObject->getInteger("id"_s);
68 if (!sequenceID)
carlosgc@webkit.org8c9a9502017-07-18 07:20:33 +000069 return;
70
drousso@apple.com7b9e7092020-09-10 19:23:17 +000071 auto responseHandler = m_commandRequests.take(*sequenceID);
carlosgc@webkit.org8c9a9502017-07-18 07:20:33 +000072 ASSERT(responseHandler);
73
74 CommandResponse response;
drousso@apple.com7b9e7092020-09-10 19:23:17 +000075 if (auto errorObject = messageObject->getObject("error"_s)) {
carlosgc@webkit.org8c9a9502017-07-18 07:20:33 +000076 response.responseObject = WTFMove(errorObject);
77 response.isError = true;
carlosgc@webkit.org48158f12020-09-15 06:25:04 +000078 } else if (auto resultObject = messageObject->getObject("result"_s)) {
79 if (resultObject->size())
80 response.responseObject = WTFMove(resultObject);
81 }
carlosgc@webkit.org8c9a9502017-07-18 07:20:33 +000082
83 responseHandler(WTFMove(response));
84}
85
86} // namespace WebDriver