blob: c02437ae91935142edecae91ba83aa7776a48150 [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
mhashmi1ec338a2016-10-25 22:13:3433void UiDevToolsClient::Dispatch(const std::string& data) {
Andrey Lushnikovcde71c12018-07-25 00:50:4934 int call_id;
35 std::string method;
36 std::unique_ptr<protocol::Value> protocolCommand =
Pavel Feldmanbfcc79d2019-02-13 16:45:0437 protocol::StringUtil::parseMessage(data, false);
Andrey Lushnikovcde71c12018-07-25 00:50:4938 if (dispatcher_.parseCommand(protocolCommand.get(), &call_id, &method)) {
Johannes Henkel784311c2019-12-05 23:24:3539 dispatcher_.dispatch(call_id, method, std::move(protocolCommand),
40 crdtp::SpanFrom(data));
Andrey Lushnikovcde71c12018-07-25 00:50:4941 }
mhashmi1ec338a2016-10-25 22:13:3442}
43
44bool UiDevToolsClient::connected() const {
45 return connection_id_ != kNotConnected;
46}
47
48void UiDevToolsClient::set_connection_id(int connection_id) {
49 connection_id_ = connection_id;
50}
51
52const std::string& UiDevToolsClient::name() const {
53 return name_;
54}
55
mhashmica5eebc2016-11-03 15:17:4556void UiDevToolsClient::DisableAllAgents() {
57 for (std::unique_ptr<UiDevToolsAgent>& agent : agents_)
58 agent->Disable();
59}
60
Johannes Henkel99cc4c92019-06-05 01:02:0661namespace {
62std::string SerializeToJSON(std::unique_ptr<protocol::Serializable> message) {
Johannes Henkel24ac8a82019-10-29 19:45:3963 std::vector<uint8_t> cbor = std::move(*message).TakeSerialized();
Johannes Henkel99cc4c92019-06-05 01:02:0664 std::string json;
Johannes Henkelcf98c792019-11-23 02:45:3365 crdtp::Status status =
66 crdtp::json::ConvertCBORToJSON(crdtp::SpanFrom(cbor), &json);
Johannes Henkel99cc4c92019-06-05 01:02:0667 LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
68 return json;
69}
70} // namespace
71
kozyatinskiy2dfeded2016-11-23 17:39:5472void UiDevToolsClient::sendProtocolResponse(
73 int callId,
74 std::unique_ptr<protocol::Serializable> message) {
Johannes Henkel99cc4c92019-06-05 01:02:0675 if (connected()) {
76 server_->SendOverWebSocket(
77 connection_id_, base::StringPiece(SerializeToJSON(std::move(message))));
78 }
mhashmi1ec338a2016-10-25 22:13:3479}
80
kozyatinskiy2dfeded2016-11-23 17:39:5481void UiDevToolsClient::sendProtocolNotification(
82 std::unique_ptr<protocol::Serializable> message) {
Johannes Henkel99cc4c92019-06-05 01:02:0683 if (connected()) {
84 server_->SendOverWebSocket(
85 connection_id_, base::StringPiece(SerializeToJSON(std::move(message))));
86 }
mhashmi1ec338a2016-10-25 22:13:3487}
88
89void UiDevToolsClient::flushProtocolNotifications() {
90 NOTIMPLEMENTED();
91}
92
Andrey Lushnikovcde71c12018-07-25 00:50:4993void UiDevToolsClient::fallThrough(int call_id,
94 const std::string& method,
Johannes Henkel784311c2019-12-05 23:24:3595 crdtp::span<uint8_t> message) {
Andrey Lushnikovcde71c12018-07-25 00:50:4996 NOTIMPLEMENTED();
97}
98
thanhph3f3968512017-06-21 00:37:2399} // namespace ui_devtools