carlosgc@webkit.org | 8c9a950 | 2017-07-18 07:20:33 +0000 | [diff] [blame] | 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 | |
carlosgc@webkit.org | 8c9a950 | 2017-07-18 07:20:33 +0000 | [diff] [blame] | 29 | #include <wtf/text/StringBuilder.h> |
| 30 | |
carlosgc@webkit.org | 8c9a950 | 2017-07-18 07:20:33 +0000 | [diff] [blame] | 31 | namespace WebDriver { |
| 32 | |
| 33 | void SessionHost::inspectorDisconnected() |
| 34 | { |
carlosgc@webkit.org | 53c5c78 | 2017-08-16 11:38:22 +0000 | [diff] [blame] | 35 | // Browser closed or crashed, finish all pending commands with error. |
commit-queue@webkit.org | 8631a87 | 2017-10-10 22:13:07 +0000 | [diff] [blame] | 36 | for (auto messageID : copyToVector(m_commandRequests.keys())) { |
carlosgc@webkit.org | 53c5c78 | 2017-08-16 11:38:22 +0000 | [diff] [blame] | 37 | auto responseHandler = m_commandRequests.take(messageID); |
| 38 | responseHandler({ nullptr, true }); |
| 39 | } |
carlosgc@webkit.org | 8c9a950 | 2017-07-18 07:20:33 +0000 | [diff] [blame] | 40 | } |
| 41 | |
bburg@apple.com | 61f04d6 | 2017-11-28 19:58:16 +0000 | [diff] [blame] | 42 | long SessionHost::sendCommandToBackend(const String& command, RefPtr<JSON::Object>&& parameters, Function<void (CommandResponse&&)>&& responseHandler) |
carlosgc@webkit.org | 8c9a950 | 2017-07-18 07:20:33 +0000 | [diff] [blame] | 43 | { |
| 44 | static long lastSequenceID = 0; |
| 45 | long sequenceID = ++lastSequenceID; |
| 46 | m_commandRequests.add(sequenceID, WTFMove(responseHandler)); |
carlosgc@webkit.org | 8c9a950 | 2017-07-18 07:20:33 +0000 | [diff] [blame] | 47 | StringBuilder messageBuilder; |
darin@apple.com | 2643705 | 2021-05-13 16:18:32 +0000 | [diff] [blame] | 48 | messageBuilder.append("{\"id\":", sequenceID, ",\"method\":\"Automation.", command, '"'); |
| 49 | if (parameters) |
| 50 | messageBuilder.append(",\"params\":", parameters->toJSONString()); |
carlosgc@webkit.org | 8c9a950 | 2017-07-18 07:20:33 +0000 | [diff] [blame] | 51 | messageBuilder.append('}'); |
carlosgc@webkit.org | c9894a9 | 2019-11-22 14:22:34 +0000 | [diff] [blame] | 52 | sendMessageToBackend(messageBuilder.toString()); |
carlosgc@webkit.org | 8c9a950 | 2017-07-18 07:20:33 +0000 | [diff] [blame] | 53 | |
| 54 | return sequenceID; |
| 55 | } |
| 56 | |
| 57 | void SessionHost::dispatchMessage(const String& message) |
| 58 | { |
drousso@apple.com | 7b9e709 | 2020-09-10 19:23:17 +0000 | [diff] [blame] | 59 | auto messageValue = JSON::Value::parseJSON(message); |
| 60 | if (!messageValue) |
carlosgc@webkit.org | 8c9a950 | 2017-07-18 07:20:33 +0000 | [diff] [blame] | 61 | return; |
| 62 | |
drousso@apple.com | 7b9e709 | 2020-09-10 19:23:17 +0000 | [diff] [blame] | 63 | auto messageObject = messageValue->asObject(); |
| 64 | if (!messageObject) |
carlosgc@webkit.org | 8c9a950 | 2017-07-18 07:20:33 +0000 | [diff] [blame] | 65 | return; |
| 66 | |
drousso@apple.com | 7b9e709 | 2020-09-10 19:23:17 +0000 | [diff] [blame] | 67 | auto sequenceID = messageObject->getInteger("id"_s); |
| 68 | if (!sequenceID) |
carlosgc@webkit.org | 8c9a950 | 2017-07-18 07:20:33 +0000 | [diff] [blame] | 69 | return; |
| 70 | |
drousso@apple.com | 7b9e709 | 2020-09-10 19:23:17 +0000 | [diff] [blame] | 71 | auto responseHandler = m_commandRequests.take(*sequenceID); |
carlosgc@webkit.org | 8c9a950 | 2017-07-18 07:20:33 +0000 | [diff] [blame] | 72 | ASSERT(responseHandler); |
| 73 | |
| 74 | CommandResponse response; |
drousso@apple.com | 7b9e709 | 2020-09-10 19:23:17 +0000 | [diff] [blame] | 75 | if (auto errorObject = messageObject->getObject("error"_s)) { |
carlosgc@webkit.org | 8c9a950 | 2017-07-18 07:20:33 +0000 | [diff] [blame] | 76 | response.responseObject = WTFMove(errorObject); |
| 77 | response.isError = true; |
carlosgc@webkit.org | 48158f1 | 2020-09-15 06:25:04 +0000 | [diff] [blame] | 78 | } else if (auto resultObject = messageObject->getObject("result"_s)) { |
| 79 | if (resultObject->size()) |
| 80 | response.responseObject = WTFMove(resultObject); |
| 81 | } |
carlosgc@webkit.org | 8c9a950 | 2017-07-18 07:20:33 +0000 | [diff] [blame] | 82 | |
| 83 | responseHandler(WTFMove(response)); |
| 84 | } |
| 85 | |
| 86 | } // namespace WebDriver |