Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 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 | |
Helmut Januschka | 58a4b28c | 2024-05-01 23:41:02 | [diff] [blame] | 7 | #include <string_view> |
| 8 | |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 9 | #include "components/ui_devtools/devtools_server.h" |
Johannes Henkel | d5fead7 | 2020-03-20 02:09:01 | [diff] [blame] | 10 | #include "third_party/inspector_protocol/crdtp/dispatch.h" |
Johannes Henkel | cf98c79 | 2019-11-23 02:45:33 | [diff] [blame] | 11 | #include "third_party/inspector_protocol/crdtp/json.h" |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 12 | |
thanhph | 3f396851 | 2017-06-21 00:37:23 | [diff] [blame] | 13 | namespace ui_devtools { |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 14 | |
| 15 | UiDevToolsClient::UiDevToolsClient(const std::string& name, |
| 16 | UiDevToolsServer* server) |
| 17 | : name_(name), |
| 18 | connection_id_(kNotConnected), |
| 19 | dispatcher_(this), |
| 20 | server_(server) { |
| 21 | DCHECK(server_); |
| 22 | } |
| 23 | |
Sorin Jianu | 64e6e759 | 2024-10-09 14:15:26 | [diff] [blame] | 24 | UiDevToolsClient::~UiDevToolsClient() = default; |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 25 | |
mhashmi | e2445fe73 | 2016-10-29 20:46:32 | [diff] [blame] | 26 | void UiDevToolsClient::AddAgent(std::unique_ptr<UiDevToolsAgent> agent) { |
| 27 | agent->Init(&dispatcher_); |
| 28 | agents_.push_back(std::move(agent)); |
| 29 | } |
| 30 | |
mhashmi | ca5eebc | 2016-11-03 15:17:45 | [diff] [blame] | 31 | void UiDevToolsClient::Disconnect() { |
| 32 | connection_id_ = kNotConnected; |
| 33 | DisableAllAgents(); |
| 34 | } |
| 35 | |
Johannes Henkel | 69fcc61 | 2019-12-06 21:14:05 | [diff] [blame] | 36 | void UiDevToolsClient::Dispatch(const std::string& json) { |
Johannes Henkel | 93ce94ad6 | 2019-12-13 00:30:16 | [diff] [blame] | 37 | std::vector<uint8_t> cbor; |
Johannes Henkel | 69fcc61 | 2019-12-06 21:14:05 | [diff] [blame] | 38 | crdtp::Status status = |
| 39 | crdtp::json::ConvertJSONToCBOR(crdtp::SpanFrom(json), &cbor); |
Johannes Henkel | d5fead7 | 2020-03-20 02:09:01 | [diff] [blame] | 40 | if (!status.ok()) { |
| 41 | dispatcher_.channel()->SendProtocolNotification( |
| 42 | crdtp::CreateErrorNotification( |
| 43 | crdtp::DispatchResponse::ParseError(status.ToASCIIString()))); |
| 44 | return; |
| 45 | } |
| 46 | crdtp::Dispatchable dispatchable(crdtp::SpanFrom(cbor)); |
| 47 | if (dispatchable.ok()) { |
| 48 | dispatcher_.Dispatch(dispatchable).Run(); |
| 49 | return; |
| 50 | } |
| 51 | if (dispatchable.HasCallId()) { |
| 52 | dispatcher_.channel()->SendProtocolResponse( |
| 53 | dispatchable.CallId(), |
| 54 | crdtp::CreateErrorResponse(dispatchable.CallId(), |
| 55 | dispatchable.DispatchError())); |
| 56 | } else { |
| 57 | dispatcher_.channel()->SendProtocolNotification( |
| 58 | crdtp::CreateErrorNotification(dispatchable.DispatchError())); |
Andrey Lushnikov | cde71c1 | 2018-07-25 00:50:49 | [diff] [blame] | 59 | } |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | bool UiDevToolsClient::connected() const { |
| 63 | return connection_id_ != kNotConnected; |
| 64 | } |
| 65 | |
| 66 | void UiDevToolsClient::set_connection_id(int connection_id) { |
| 67 | connection_id_ = connection_id; |
| 68 | } |
| 69 | |
| 70 | const std::string& UiDevToolsClient::name() const { |
| 71 | return name_; |
| 72 | } |
| 73 | |
mhashmi | ca5eebc | 2016-11-03 15:17:45 | [diff] [blame] | 74 | void UiDevToolsClient::DisableAllAgents() { |
| 75 | for (std::unique_ptr<UiDevToolsAgent>& agent : agents_) |
| 76 | agent->Disable(); |
| 77 | } |
| 78 | |
Johannes Henkel | 69fcc61 | 2019-12-06 21:14:05 | [diff] [blame] | 79 | void UiDevToolsClient::MaybeSendProtocolResponseOrNotification( |
| 80 | std::unique_ptr<protocol::Serializable> message) { |
| 81 | if (!connected()) |
| 82 | return; |
| 83 | |
Johannes Henkel | 99cc4c9 | 2019-06-05 01:02:06 | [diff] [blame] | 84 | std::string json; |
Johannes Henkel | a9b7110 | 2020-02-10 20:20:17 | [diff] [blame] | 85 | crdtp::Status status = crdtp::json::ConvertCBORToJSON( |
| 86 | crdtp::SpanFrom(message->Serialize()), &json); |
Johannes Henkel | d5fead7 | 2020-03-20 02:09:01 | [diff] [blame] | 87 | DCHECK(status.ok()); // CBOR was generated by Chrome, so we expect it's ok. |
Helmut Januschka | 58a4b28c | 2024-05-01 23:41:02 | [diff] [blame] | 88 | server_->SendOverWebSocket(connection_id_, std::string_view(json)); |
Johannes Henkel | 99cc4c9 | 2019-06-05 01:02:06 | [diff] [blame] | 89 | } |
Johannes Henkel | 99cc4c9 | 2019-06-05 01:02:06 | [diff] [blame] | 90 | |
Johannes Henkel | d5fead7 | 2020-03-20 02:09:01 | [diff] [blame] | 91 | void UiDevToolsClient::SendProtocolResponse( |
kozyatinskiy | 2dfeded | 2016-11-23 17:39:54 | [diff] [blame] | 92 | int callId, |
| 93 | std::unique_ptr<protocol::Serializable> message) { |
Johannes Henkel | 69fcc61 | 2019-12-06 21:14:05 | [diff] [blame] | 94 | MaybeSendProtocolResponseOrNotification(std::move(message)); |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 95 | } |
| 96 | |
Johannes Henkel | d5fead7 | 2020-03-20 02:09:01 | [diff] [blame] | 97 | void UiDevToolsClient::SendProtocolNotification( |
kozyatinskiy | 2dfeded | 2016-11-23 17:39:54 | [diff] [blame] | 98 | std::unique_ptr<protocol::Serializable> message) { |
Johannes Henkel | 69fcc61 | 2019-12-06 21:14:05 | [diff] [blame] | 99 | MaybeSendProtocolResponseOrNotification(std::move(message)); |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 100 | } |
| 101 | |
Johannes Henkel | d5fead7 | 2020-03-20 02:09:01 | [diff] [blame] | 102 | void UiDevToolsClient::FlushProtocolNotifications() { |
mhashmi | 1ec338a | 2016-10-25 22:13:34 | [diff] [blame] | 103 | NOTIMPLEMENTED(); |
| 104 | } |
| 105 | |
Johannes Henkel | d5fead7 | 2020-03-20 02:09:01 | [diff] [blame] | 106 | void UiDevToolsClient::FallThrough(int call_id, |
| 107 | crdtp::span<uint8_t> method, |
Johannes Henkel | 784311c | 2019-12-05 23:24:35 | [diff] [blame] | 108 | crdtp::span<uint8_t> message) { |
Andrey Lushnikov | cde71c1 | 2018-07-25 00:50:49 | [diff] [blame] | 109 | NOTIMPLEMENTED(); |
| 110 | } |
| 111 | |
thanhph | 3f396851 | 2017-06-21 00:37:23 | [diff] [blame] | 112 | } // namespace ui_devtools |