blob: c957682cc84a9d54314b31b4a0368dfe3ffa8db9 [file] [log] [blame]
mhashmi1ec338a2016-10-25 22:13:341// 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
mhashmi1ec338a2016-10-25 22:13:347#include "components/ui_devtools/devtools_server.h"
Johannes Henkelcf98c792019-11-23 02:45:338#include "third_party/inspector_protocol/crdtp/json.h"
mhashmi1ec338a2016-10-25 22:13:349
thanhph3f3968512017-06-21 00:37:2310namespace ui_devtools {
mhashmi1ec338a2016-10-25 22:13:3411
12UiDevToolsClient::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
21UiDevToolsClient::~UiDevToolsClient() {}
22
mhashmie2445fe732016-10-29 20:46:3223void UiDevToolsClient::AddAgent(std::unique_ptr<UiDevToolsAgent> agent) {
24 agent->Init(&dispatcher_);
25 agents_.push_back(std::move(agent));
26}
27
mhashmica5eebc2016-11-03 15:17:4528void UiDevToolsClient::Disconnect() {
29 connection_id_ = kNotConnected;
30 DisableAllAgents();
31}
32
Johannes Henkel69fcc612019-12-06 21:14:0533void UiDevToolsClient::Dispatch(const std::string& json) {
Johannes Henkel93ce94ad62019-12-13 00:30:1634 std::vector<uint8_t> cbor;
Johannes Henkel69fcc612019-12-06 21:14:0535 crdtp::Status status =
36 crdtp::json::ConvertJSONToCBOR(crdtp::SpanFrom(json), &cbor);
37 LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
38
Andrey Lushnikovcde71c12018-07-25 00:50:4939 int call_id;
40 std::string method;
41 std::unique_ptr<protocol::Value> protocolCommand =
Johannes Henkel93ce94ad62019-12-13 00:30:1642 protocol::Value::parseBinary(cbor.data(), cbor.size());
Andrey Lushnikovcde71c12018-07-25 00:50:4943 if (dispatcher_.parseCommand(protocolCommand.get(), &call_id, &method)) {
Johannes Henkel784311c2019-12-05 23:24:3544 dispatcher_.dispatch(call_id, method, std::move(protocolCommand),
Johannes Henkel69fcc612019-12-06 21:14:0545 crdtp::SpanFrom(cbor));
Andrey Lushnikovcde71c12018-07-25 00:50:4946 }
mhashmi1ec338a2016-10-25 22:13:3447}
48
49bool UiDevToolsClient::connected() const {
50 return connection_id_ != kNotConnected;
51}
52
53void UiDevToolsClient::set_connection_id(int connection_id) {
54 connection_id_ = connection_id;
55}
56
57const std::string& UiDevToolsClient::name() const {
58 return name_;
59}
60
mhashmica5eebc2016-11-03 15:17:4561void UiDevToolsClient::DisableAllAgents() {
62 for (std::unique_ptr<UiDevToolsAgent>& agent : agents_)
63 agent->Disable();
64}
65
Johannes Henkel69fcc612019-12-06 21:14:0566void UiDevToolsClient::MaybeSendProtocolResponseOrNotification(
67 std::unique_ptr<protocol::Serializable> message) {
68 if (!connected())
69 return;
70
Johannes Henkel24ac8a82019-10-29 19:45:3971 std::vector<uint8_t> cbor = std::move(*message).TakeSerialized();
Johannes Henkel99cc4c92019-06-05 01:02:0672 std::string json;
Johannes Henkelcf98c792019-11-23 02:45:3373 crdtp::Status status =
74 crdtp::json::ConvertCBORToJSON(crdtp::SpanFrom(cbor), &json);
Johannes Henkel99cc4c92019-06-05 01:02:0675 LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
Johannes Henkel69fcc612019-12-06 21:14:0576 server_->SendOverWebSocket(connection_id_, base::StringPiece(json));
Johannes Henkel99cc4c92019-06-05 01:02:0677}
Johannes Henkel99cc4c92019-06-05 01:02:0678
kozyatinskiy2dfeded2016-11-23 17:39:5479void UiDevToolsClient::sendProtocolResponse(
80 int callId,
81 std::unique_ptr<protocol::Serializable> message) {
Johannes Henkel69fcc612019-12-06 21:14:0582 MaybeSendProtocolResponseOrNotification(std::move(message));
mhashmi1ec338a2016-10-25 22:13:3483}
84
kozyatinskiy2dfeded2016-11-23 17:39:5485void UiDevToolsClient::sendProtocolNotification(
86 std::unique_ptr<protocol::Serializable> message) {
Johannes Henkel69fcc612019-12-06 21:14:0587 MaybeSendProtocolResponseOrNotification(std::move(message));
mhashmi1ec338a2016-10-25 22:13:3488}
89
90void UiDevToolsClient::flushProtocolNotifications() {
91 NOTIMPLEMENTED();
92}
93
Andrey Lushnikovcde71c12018-07-25 00:50:4994void UiDevToolsClient::fallThrough(int call_id,
95 const std::string& method,
Johannes Henkel784311c2019-12-05 23:24:3596 crdtp::span<uint8_t> message) {
Andrey Lushnikovcde71c12018-07-25 00:50:4997 NOTIMPLEMENTED();
98}
99
thanhph3f3968512017-06-21 00:37:23100} // namespace ui_devtools