mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "components/ui_devtools/devtools_client.h" |
| 6 | |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 7 | #include "components/ui_devtools/devtools_server.h" |
Johannes Henkel | cf98c79 | 2019-11-23 02:45:33 | [diff] [blame] | 8 | #include "third_party/inspector_protocol/crdtp/json.h" |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 9 | |
thanhph | 3f396851 | 2017-06-21 00:37:23 | [diff] [blame] | 10 | namespace ui_devtools { |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 11 | |
| 12 | UiDevToolsClient::UiDevToolsClient(const std::string& name, |
| 13 | UiDevToolsServer* server) |
| 14 | : name_(name), |
| 15 | connection_id_(kNotConnected), |
| 16 | dispatcher_(this), |
| 17 | server_(server) { |
| 18 | DCHECK(server_); |
| 19 | } |
| 20 | |
| 21 | UiDevToolsClient::~UiDevToolsClient() {} |
| 22 | |
mhashmi | e2445fe73 | 2016-10-29 20:46:32 | [diff] [blame] | 23 | void UiDevToolsClient::AddAgent(std::unique_ptr<UiDevToolsAgent> agent) { |
| 24 | agent->Init(&dispatcher_); |
| 25 | agents_.push_back(std::move(agent)); |
| 26 | } |
| 27 | |
mhashmi | ca5eebc | 2016-11-03 15:17:45 | [diff] [blame] | 28 | void UiDevToolsClient::Disconnect() { |
| 29 | connection_id_ = kNotConnected; |
| 30 | DisableAllAgents(); |
| 31 | } |
| 32 | |
Johannes Henkel | 69fcc61 | 2019-12-06 21:14:05 | [diff] [blame] | 33 | void UiDevToolsClient::Dispatch(const std::string& json) { |
Johannes Henkel | 93ce94ad6 | 2019-12-13 00:30:16 | [diff] [blame^] | 34 | std::vector<uint8_t> cbor; |
Johannes Henkel | 69fcc61 | 2019-12-06 21:14:05 | [diff] [blame] | 35 | crdtp::Status status = |
| 36 | crdtp::json::ConvertJSONToCBOR(crdtp::SpanFrom(json), &cbor); |
| 37 | LOG_IF(ERROR, !status.ok()) << status.ToASCIIString(); |
| 38 | |
Andrey Lushnikov | cde71c1 | 2018-07-25 00:50:49 | [diff] [blame] | 39 | int call_id; |
| 40 | std::string method; |
| 41 | std::unique_ptr<protocol::Value> protocolCommand = |
Johannes Henkel | 93ce94ad6 | 2019-12-13 00:30:16 | [diff] [blame^] | 42 | protocol::Value::parseBinary(cbor.data(), cbor.size()); |
Andrey Lushnikov | cde71c1 | 2018-07-25 00:50:49 | [diff] [blame] | 43 | if (dispatcher_.parseCommand(protocolCommand.get(), &call_id, &method)) { |
Johannes Henkel | 784311c | 2019-12-05 23:24:35 | [diff] [blame] | 44 | dispatcher_.dispatch(call_id, method, std::move(protocolCommand), |
Johannes Henkel | 69fcc61 | 2019-12-06 21:14:05 | [diff] [blame] | 45 | crdtp::SpanFrom(cbor)); |
Andrey Lushnikov | cde71c1 | 2018-07-25 00:50:49 | [diff] [blame] | 46 | } |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | bool UiDevToolsClient::connected() const { |
| 50 | return connection_id_ != kNotConnected; |
| 51 | } |
| 52 | |
| 53 | void UiDevToolsClient::set_connection_id(int connection_id) { |
| 54 | connection_id_ = connection_id; |
| 55 | } |
| 56 | |
| 57 | const std::string& UiDevToolsClient::name() const { |
| 58 | return name_; |
| 59 | } |
| 60 | |
mhashmi | ca5eebc | 2016-11-03 15:17:45 | [diff] [blame] | 61 | void UiDevToolsClient::DisableAllAgents() { |
| 62 | for (std::unique_ptr<UiDevToolsAgent>& agent : agents_) |
| 63 | agent->Disable(); |
| 64 | } |
| 65 | |
Johannes Henkel | 69fcc61 | 2019-12-06 21:14:05 | [diff] [blame] | 66 | void UiDevToolsClient::MaybeSendProtocolResponseOrNotification( |
| 67 | std::unique_ptr<protocol::Serializable> message) { |
| 68 | if (!connected()) |
| 69 | return; |
| 70 | |
Johannes Henkel | 24ac8a8 | 2019-10-29 19:45:39 | [diff] [blame] | 71 | std::vector<uint8_t> cbor = std::move(*message).TakeSerialized(); |
Johannes Henkel | 99cc4c9 | 2019-06-05 01:02:06 | [diff] [blame] | 72 | std::string json; |
Johannes Henkel | cf98c79 | 2019-11-23 02:45:33 | [diff] [blame] | 73 | crdtp::Status status = |
| 74 | crdtp::json::ConvertCBORToJSON(crdtp::SpanFrom(cbor), &json); |
Johannes Henkel | 99cc4c9 | 2019-06-05 01:02:06 | [diff] [blame] | 75 | LOG_IF(ERROR, !status.ok()) << status.ToASCIIString(); |
Johannes Henkel | 69fcc61 | 2019-12-06 21:14:05 | [diff] [blame] | 76 | server_->SendOverWebSocket(connection_id_, base::StringPiece(json)); |
Johannes Henkel | 99cc4c9 | 2019-06-05 01:02:06 | [diff] [blame] | 77 | } |
Johannes Henkel | 99cc4c9 | 2019-06-05 01:02:06 | [diff] [blame] | 78 | |
kozyatinskiy | 2dfeded | 2016-11-23 17:39:54 | [diff] [blame] | 79 | void UiDevToolsClient::sendProtocolResponse( |
| 80 | int callId, |
| 81 | std::unique_ptr<protocol::Serializable> message) { |
Johannes Henkel | 69fcc61 | 2019-12-06 21:14:05 | [diff] [blame] | 82 | MaybeSendProtocolResponseOrNotification(std::move(message)); |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 83 | } |
| 84 | |
kozyatinskiy | 2dfeded | 2016-11-23 17:39:54 | [diff] [blame] | 85 | void UiDevToolsClient::sendProtocolNotification( |
| 86 | std::unique_ptr<protocol::Serializable> message) { |
Johannes Henkel | 69fcc61 | 2019-12-06 21:14:05 | [diff] [blame] | 87 | MaybeSendProtocolResponseOrNotification(std::move(message)); |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void UiDevToolsClient::flushProtocolNotifications() { |
| 91 | NOTIMPLEMENTED(); |
| 92 | } |
| 93 | |
Andrey Lushnikov | cde71c1 | 2018-07-25 00:50:49 | [diff] [blame] | 94 | void UiDevToolsClient::fallThrough(int call_id, |
| 95 | const std::string& method, |
Johannes Henkel | 784311c | 2019-12-05 23:24:35 | [diff] [blame] | 96 | crdtp::span<uint8_t> message) { |
Andrey Lushnikov | cde71c1 | 2018-07-25 00:50:49 | [diff] [blame] | 97 | NOTIMPLEMENTED(); |
| 98 | } |
| 99 | |
thanhph | 3f396851 | 2017-06-21 00:37:23 | [diff] [blame] | 100 | } // namespace ui_devtools |