blob: d70231f36741c1e9d57dfeca02d72baf584e1565 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2016 The Chromium Authors
mhashmi1ec338a2016-10-25 22:13:342// 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 Januschka58a4b28c2024-05-01 23:41:027#include <string_view>
8
mhashmi1ec338a2016-10-25 22:13:349#include "components/ui_devtools/devtools_server.h"
Johannes Henkeld5fead72020-03-20 02:09:0110#include "third_party/inspector_protocol/crdtp/dispatch.h"
Johannes Henkelcf98c792019-11-23 02:45:3311#include "third_party/inspector_protocol/crdtp/json.h"
mhashmi1ec338a2016-10-25 22:13:3412
thanhph3f3968512017-06-21 00:37:2313namespace ui_devtools {
mhashmi1ec338a2016-10-25 22:13:3414
15UiDevToolsClient::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 Jianu64e6e7592024-10-09 14:15:2624UiDevToolsClient::~UiDevToolsClient() = default;
mhashmi1ec338a2016-10-25 22:13:3425
mhashmie2445fe732016-10-29 20:46:3226void UiDevToolsClient::AddAgent(std::unique_ptr<UiDevToolsAgent> agent) {
27 agent->Init(&dispatcher_);
28 agents_.push_back(std::move(agent));
29}
30
mhashmica5eebc2016-11-03 15:17:4531void UiDevToolsClient::Disconnect() {
32 connection_id_ = kNotConnected;
33 DisableAllAgents();
34}
35
Johannes Henkel69fcc612019-12-06 21:14:0536void UiDevToolsClient::Dispatch(const std::string& json) {
Johannes Henkel93ce94ad62019-12-13 00:30:1637 std::vector<uint8_t> cbor;
Johannes Henkel69fcc612019-12-06 21:14:0538 crdtp::Status status =
39 crdtp::json::ConvertJSONToCBOR(crdtp::SpanFrom(json), &cbor);
Johannes Henkeld5fead72020-03-20 02:09:0140 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 Lushnikovcde71c12018-07-25 00:50:4959 }
mhashmi1ec338a2016-10-25 22:13:3460}
61
62bool UiDevToolsClient::connected() const {
63 return connection_id_ != kNotConnected;
64}
65
66void UiDevToolsClient::set_connection_id(int connection_id) {
67 connection_id_ = connection_id;
68}
69
70const std::string& UiDevToolsClient::name() const {
71 return name_;
72}
73
mhashmica5eebc2016-11-03 15:17:4574void UiDevToolsClient::DisableAllAgents() {
75 for (std::unique_ptr<UiDevToolsAgent>& agent : agents_)
76 agent->Disable();
77}
78
Johannes Henkel69fcc612019-12-06 21:14:0579void UiDevToolsClient::MaybeSendProtocolResponseOrNotification(
80 std::unique_ptr<protocol::Serializable> message) {
81 if (!connected())
82 return;
83
Johannes Henkel99cc4c92019-06-05 01:02:0684 std::string json;
Johannes Henkela9b71102020-02-10 20:20:1785 crdtp::Status status = crdtp::json::ConvertCBORToJSON(
86 crdtp::SpanFrom(message->Serialize()), &json);
Johannes Henkeld5fead72020-03-20 02:09:0187 DCHECK(status.ok()); // CBOR was generated by Chrome, so we expect it's ok.
Helmut Januschka58a4b28c2024-05-01 23:41:0288 server_->SendOverWebSocket(connection_id_, std::string_view(json));
Johannes Henkel99cc4c92019-06-05 01:02:0689}
Johannes Henkel99cc4c92019-06-05 01:02:0690
Johannes Henkeld5fead72020-03-20 02:09:0191void UiDevToolsClient::SendProtocolResponse(
kozyatinskiy2dfeded2016-11-23 17:39:5492 int callId,
93 std::unique_ptr<protocol::Serializable> message) {
Johannes Henkel69fcc612019-12-06 21:14:0594 MaybeSendProtocolResponseOrNotification(std::move(message));
mhashmi1ec338a2016-10-25 22:13:3495}
96
Johannes Henkeld5fead72020-03-20 02:09:0197void UiDevToolsClient::SendProtocolNotification(
kozyatinskiy2dfeded2016-11-23 17:39:5498 std::unique_ptr<protocol::Serializable> message) {
Johannes Henkel69fcc612019-12-06 21:14:0599 MaybeSendProtocolResponseOrNotification(std::move(message));
mhashmi1ec338a2016-10-25 22:13:34100}
101
Johannes Henkeld5fead72020-03-20 02:09:01102void UiDevToolsClient::FlushProtocolNotifications() {
mhashmi1ec338a2016-10-25 22:13:34103 NOTIMPLEMENTED();
104}
105
Johannes Henkeld5fead72020-03-20 02:09:01106void UiDevToolsClient::FallThrough(int call_id,
107 crdtp::span<uint8_t> method,
Johannes Henkel784311c2019-12-05 23:24:35108 crdtp::span<uint8_t> message) {
Andrey Lushnikovcde71c12018-07-25 00:50:49109 NOTIMPLEMENTED();
110}
111
thanhph3f3968512017-06-21 00:37:23112} // namespace ui_devtools