blob: b291394c562ebab192f5965302f104e6d7554b23 [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)) {
39 dispatcher_.dispatch(call_id, method, std::move(protocolCommand), data);
40 }
mhashmi1ec338a2016-10-25 22:13:3441}
42
43bool UiDevToolsClient::connected() const {
44 return connection_id_ != kNotConnected;
45}
46
47void UiDevToolsClient::set_connection_id(int connection_id) {
48 connection_id_ = connection_id;
49}
50
51const std::string& UiDevToolsClient::name() const {
52 return name_;
53}
54
mhashmica5eebc2016-11-03 15:17:4555void UiDevToolsClient::DisableAllAgents() {
56 for (std::unique_ptr<UiDevToolsAgent>& agent : agents_)
57 agent->Disable();
58}
59
Johannes Henkel99cc4c92019-06-05 01:02:0660namespace {
61std::string SerializeToJSON(std::unique_ptr<protocol::Serializable> message) {
Johannes Henkel24ac8a82019-10-29 19:45:3962 std::vector<uint8_t> cbor = std::move(*message).TakeSerialized();
Johannes Henkel99cc4c92019-06-05 01:02:0663 std::string json;
Johannes Henkelcf98c792019-11-23 02:45:3364 crdtp::Status status =
65 crdtp::json::ConvertCBORToJSON(crdtp::SpanFrom(cbor), &json);
Johannes Henkel99cc4c92019-06-05 01:02:0666 LOG_IF(ERROR, !status.ok()) << status.ToASCIIString();
67 return json;
68}
69} // namespace
70
kozyatinskiy2dfeded2016-11-23 17:39:5471void UiDevToolsClient::sendProtocolResponse(
72 int callId,
73 std::unique_ptr<protocol::Serializable> message) {
Johannes Henkel99cc4c92019-06-05 01:02:0674 if (connected()) {
75 server_->SendOverWebSocket(
76 connection_id_, base::StringPiece(SerializeToJSON(std::move(message))));
77 }
mhashmi1ec338a2016-10-25 22:13:3478}
79
kozyatinskiy2dfeded2016-11-23 17:39:5480void UiDevToolsClient::sendProtocolNotification(
81 std::unique_ptr<protocol::Serializable> message) {
Johannes Henkel99cc4c92019-06-05 01:02:0682 if (connected()) {
83 server_->SendOverWebSocket(
84 connection_id_, base::StringPiece(SerializeToJSON(std::move(message))));
85 }
mhashmi1ec338a2016-10-25 22:13:3486}
87
88void UiDevToolsClient::flushProtocolNotifications() {
89 NOTIMPLEMENTED();
90}
91
Andrey Lushnikovcde71c12018-07-25 00:50:4992void UiDevToolsClient::fallThrough(int call_id,
93 const std::string& method,
94 const std::string& message) {
95 NOTIMPLEMENTED();
96}
97
thanhph3f3968512017-06-21 00:37:2398} // namespace ui_devtools