1/*
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
29#include <wtf/text/StringBuilder.h>
30
31namespace WebDriver {
32
33void SessionHost::inspectorDisconnected()
34{
35 // Browser closed or crashed, finish all pending commands with error.
36 for (auto messageID : copyToVector(m_commandRequests.keys())) {
37 auto responseHandler = m_commandRequests.take(messageID);
38 responseHandler({ nullptr, true });
39 }
40}
41
42long SessionHost::sendCommandToBackend(const String& command, RefPtr<JSON::Object>&& parameters, Function<void (CommandResponse&&)>&& responseHandler)
43{
44 static long lastSequenceID = 0;
45 long sequenceID = ++lastSequenceID;
46 m_commandRequests.add(sequenceID, WTFMove(responseHandler));
47 StringBuilder messageBuilder;
48 messageBuilder.appendLiteral("{\"id\":");
49 messageBuilder.appendNumber(sequenceID);
50 messageBuilder.appendLiteral(",\"method\":\"Automation.");
51 messageBuilder.append(command);
52 messageBuilder.append('"');
53 if (parameters) {
54 messageBuilder.appendLiteral(",\"params\":");
55 messageBuilder.append(parameters->toJSONString());
56 }
57 messageBuilder.append('}');
58 sendMessageToBackend(sequenceID, messageBuilder.toString());
59
60 return sequenceID;
61}
62
63void SessionHost::dispatchMessage(const String& message)
64{
65 RefPtr<JSON::Value> messageValue;
66 if (!JSON::Value::parseJSON(message, messageValue))
67 return;
68
69 RefPtr<JSON::Object> messageObject;
70 if (!messageValue->asObject(messageObject))
71 return;
72
73 long sequenceID;
74 if (!messageObject->getInteger("id"_s, sequenceID))
75 return;
76
77 auto responseHandler = m_commandRequests.take(sequenceID);
78 ASSERT(responseHandler);
79
80 CommandResponse response;
81 RefPtr<JSON::Object> errorObject;
82 if (messageObject->getObject("error"_s, errorObject)) {
83 response.responseObject = WTFMove(errorObject);
84 response.isError = true;
85 } else {
86 RefPtr<JSON::Object> resultObject;
87 if (messageObject->getObject("result"_s, resultObject) && resultObject->size())
88 response.responseObject = WTFMove(resultObject);
89 }
90
91 responseHandler(WTFMove(response));
92}
93
94} // namespace WebDriver
95