devtools_client.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // 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. #include "components/ui_devtools/devtools_client.h"
  5. #include "components/ui_devtools/devtools_server.h"
  6. #include "third_party/inspector_protocol/crdtp/dispatch.h"
  7. #include "third_party/inspector_protocol/crdtp/json.h"
  8. namespace ui_devtools {
  9. UiDevToolsClient::UiDevToolsClient(const std::string& name,
  10. UiDevToolsServer* server)
  11. : name_(name),
  12. connection_id_(kNotConnected),
  13. dispatcher_(this),
  14. server_(server) {
  15. DCHECK(server_);
  16. }
  17. UiDevToolsClient::~UiDevToolsClient() {}
  18. void UiDevToolsClient::AddAgent(std::unique_ptr<UiDevToolsAgent> agent) {
  19. agent->Init(&dispatcher_);
  20. agents_.push_back(std::move(agent));
  21. }
  22. void UiDevToolsClient::Disconnect() {
  23. connection_id_ = kNotConnected;
  24. DisableAllAgents();
  25. }
  26. void UiDevToolsClient::Dispatch(const std::string& json) {
  27. std::vector<uint8_t> cbor;
  28. crdtp::Status status =
  29. crdtp::json::ConvertJSONToCBOR(crdtp::SpanFrom(json), &cbor);
  30. if (!status.ok()) {
  31. dispatcher_.channel()->SendProtocolNotification(
  32. crdtp::CreateErrorNotification(
  33. crdtp::DispatchResponse::ParseError(status.ToASCIIString())));
  34. return;
  35. }
  36. crdtp::Dispatchable dispatchable(crdtp::SpanFrom(cbor));
  37. if (dispatchable.ok()) {
  38. dispatcher_.Dispatch(dispatchable).Run();
  39. return;
  40. }
  41. if (dispatchable.HasCallId()) {
  42. dispatcher_.channel()->SendProtocolResponse(
  43. dispatchable.CallId(),
  44. crdtp::CreateErrorResponse(dispatchable.CallId(),
  45. dispatchable.DispatchError()));
  46. } else {
  47. dispatcher_.channel()->SendProtocolNotification(
  48. crdtp::CreateErrorNotification(dispatchable.DispatchError()));
  49. }
  50. }
  51. bool UiDevToolsClient::connected() const {
  52. return connection_id_ != kNotConnected;
  53. }
  54. void UiDevToolsClient::set_connection_id(int connection_id) {
  55. connection_id_ = connection_id;
  56. }
  57. const std::string& UiDevToolsClient::name() const {
  58. return name_;
  59. }
  60. void UiDevToolsClient::DisableAllAgents() {
  61. for (std::unique_ptr<UiDevToolsAgent>& agent : agents_)
  62. agent->Disable();
  63. }
  64. void UiDevToolsClient::MaybeSendProtocolResponseOrNotification(
  65. std::unique_ptr<protocol::Serializable> message) {
  66. if (!connected())
  67. return;
  68. std::string json;
  69. crdtp::Status status = crdtp::json::ConvertCBORToJSON(
  70. crdtp::SpanFrom(message->Serialize()), &json);
  71. DCHECK(status.ok()); // CBOR was generated by Chrome, so we expect it's ok.
  72. server_->SendOverWebSocket(connection_id_, base::StringPiece(json));
  73. }
  74. void UiDevToolsClient::SendProtocolResponse(
  75. int callId,
  76. std::unique_ptr<protocol::Serializable> message) {
  77. MaybeSendProtocolResponseOrNotification(std::move(message));
  78. }
  79. void UiDevToolsClient::SendProtocolNotification(
  80. std::unique_ptr<protocol::Serializable> message) {
  81. MaybeSendProtocolResponseOrNotification(std::move(message));
  82. }
  83. void UiDevToolsClient::FlushProtocolNotifications() {
  84. NOTIMPLEMENTED();
  85. }
  86. void UiDevToolsClient::FallThrough(int call_id,
  87. crdtp::span<uint8_t> method,
  88. crdtp::span<uint8_t> message) {
  89. NOTIMPLEMENTED();
  90. }
  91. } // namespace ui_devtools