Skip to content

Commit

Permalink
Add debug console command support to devtools. (youtube#536)
Browse files Browse the repository at this point in the history
* Add debug console command support to devtools.

This adds support for debug console commands to the devtools backend and to
the Cobalt tab in the frontend.

b/251825168

* Update cobalt_agent.cc

2021 -> 2023
  • Loading branch information
jellefoks authored Jun 6, 2023
1 parent 914749f commit ab9aa01
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cobalt/debug/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ static_library("debug") {
sources = [
"backend/agent_base.cc",
"backend/agent_base.h",
"backend/cobalt_agent.cc",
"backend/cobalt_agent.h",
"backend/command_map.h",
"backend/css_agent.cc",
"backend/css_agent.h",
Expand Down
85 changes: 85 additions & 0 deletions cobalt/debug/backend/cobalt_agent.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2023 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cobalt/debug/backend/cobalt_agent.h"

#include <set>
#include <string>
#include <utility>

#include "base/bind.h"
#include "base/values.h"
#include "cobalt/debug/console/command_manager.h"
#include "cobalt/debug/json_object.h"

namespace cobalt {
namespace debug {
namespace backend {

CobaltAgent::CobaltAgent(DebugDispatcher* dispatcher)
: AgentBase("Cobalt", dispatcher) {
commands_["getConsoleCommands"] =
base::Bind(&CobaltAgent::GetConsoleCommands, base::Unretained(this));
commands_["sendConsoleCommand"] =
base::Bind(&CobaltAgent::SendConsoleCommand, base::Unretained(this));
// dispatcher_->AddDomain(domain_, commands_.Bind());
}

void CobaltAgent::GetConsoleCommands(Command command) {
JSONObject response(new base::DictionaryValue());
JSONList list(new base::ListValue());

console::ConsoleCommandManager* command_manager =
console::ConsoleCommandManager::GetInstance();
DCHECK(command_manager);
if (command_manager) {
std::set<std::string> commands = command_manager->GetRegisteredCommands();
for (auto& command_name : commands) {
JSONObject console_command(new base::DictionaryValue());
console_command->SetString("command", command_name);
console_command->SetString("shortHelp",
command_manager->GetShortHelp(command_name));
console_command->SetString("longHelp",
command_manager->GetLongHelp(command_name));
list->Append(std::move(console_command));
}
}

JSONObject commands(new base::DictionaryValue());
commands->Set("commands", std::move(list));
response->Set("result", std::move(commands));
command.SendResponse(response);
}

void CobaltAgent::SendConsoleCommand(Command command) {
JSONObject params = JSONParse(command.GetParams());
if (params) {
std::string console_command;
if (params->GetString("command", &console_command)) {
std::string message;
params->GetString("message", &message);
console::ConsoleCommandManager* console_command_manager =
console::ConsoleCommandManager::GetInstance();
DCHECK(console_command_manager);
console_command_manager->HandleCommand(console_command, message);
command.SendResponse();
return;
}
}
command.SendErrorResponse(Command::kInvalidParams, "Missing command.");
}

} // namespace backend
} // namespace debug
} // namespace cobalt
47 changes: 47 additions & 0 deletions cobalt/debug/backend/cobalt_agent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2023 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef COBALT_DEBUG_BACKEND_COBALT_AGENT_H_
#define COBALT_DEBUG_BACKEND_COBALT_AGENT_H_

#include "cobalt/debug/backend/agent_base.h"
#include "cobalt/debug/backend/debug_dispatcher.h"
#include "cobalt/debug/command.h"
#include "cobalt/debug/json_object.h"
#include "cobalt/dom/window.h"

namespace cobalt {
namespace debug {
namespace backend {

// Implements a small part of the the "Runtime" inspector protocol domain with
// just enough to support console input. When using the V8 JavaScript engine,
// this class is not needed since the V8 inspector implements the Runtime domain
// for us.
//
// https://chromedevtools.github.io/devtools-protocol/tot/Runtime
class CobaltAgent : public AgentBase {
public:
explicit CobaltAgent(DebugDispatcher* dispatcher);

private:
void GetConsoleCommands(Command command);
void SendConsoleCommand(Command command);
};

} // namespace backend
} // namespace debug
} // namespace cobalt

#endif // COBALT_DEBUG_BACKEND_COBALT_AGENT_H_
4 changes: 4 additions & 0 deletions cobalt/debug/backend/debug_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace debug {
namespace backend {

namespace {
constexpr char kCobaltAgent[] = "CobaltAgent";
constexpr char kScriptDebuggerAgent[] = "ScriptDebuggerAgent";
constexpr char kLogAgent[] = "LogAgent";
constexpr char kDomAgent[] = "DomAgent";
Expand Down Expand Up @@ -140,6 +141,7 @@ void DebugModule::BuildInternal(const ConstructionData& data) {
// directly handle one or more protocol domains.
script_debugger_agent_.reset(
new ScriptDebuggerAgent(debug_dispatcher_.get(), script_debugger_.get()));
cobalt_agent_.reset(new CobaltAgent(debug_dispatcher_.get()));
log_agent_.reset(new LogAgent(debug_dispatcher_.get()));
dom_agent_.reset(new DOMAgent(debug_dispatcher_.get()));
css_agent_ = WrapRefCounted(new CSSAgent(debug_dispatcher_.get()));
Expand Down Expand Up @@ -178,6 +180,7 @@ void DebugModule::BuildInternal(const ConstructionData& data) {
base::DictionaryValue* agents_state =
data.debugger_state == nullptr ? nullptr
: data.debugger_state->agents_state.get();
cobalt_agent_->Thaw(RemoveAgentState(kCobaltAgent, agents_state));
script_debugger_agent_->Thaw(
RemoveAgentState(kScriptDebuggerAgent, agents_state));
log_agent_->Thaw(RemoveAgentState(kLogAgent, agents_state));
Expand All @@ -200,6 +203,7 @@ std::unique_ptr<DebuggerState> DebugModule::Freeze() {

debugger_state->agents_state.reset(new base::DictionaryValue());
base::DictionaryValue* agents_state = debugger_state->agents_state.get();
StoreAgentState(agents_state, kCobaltAgent, cobalt_agent_->Freeze());
StoreAgentState(agents_state, kScriptDebuggerAgent,
script_debugger_agent_->Freeze());
StoreAgentState(agents_state, kLogAgent, log_agent_->Freeze());
Expand Down
2 changes: 2 additions & 0 deletions cobalt/debug/backend/debug_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "base/message_loop/message_loop.h"
#include "cobalt/base/debugger_hooks.h"
#include "cobalt/debug/backend/cobalt_agent.h"
#include "cobalt/debug/backend/css_agent.h"
#include "cobalt/debug/backend/debug_backend.h"
#include "cobalt/debug/backend/debug_dispatcher.h"
Expand Down Expand Up @@ -134,6 +135,7 @@ class DebugModule : public script::ScriptDebugger::Delegate {

// Wrappable object providing native helpers for backend JavaScript.
scoped_refptr<DebugBackend> debug_backend_;
std::unique_ptr<CobaltAgent> cobalt_agent_;
std::unique_ptr<LogAgent> log_agent_;
std::unique_ptr<DOMAgent> dom_agent_;
scoped_refptr<CSSAgent> css_agent_;
Expand Down
25 changes: 24 additions & 1 deletion third_party/devtools/front_end/cobalt/cobalt.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class CobaltPanel extends UI.VBox {

this._target = UI.context.flavor(SDK.Target);
this._runtimeAgent = this._target.runtimeAgent();
this._cobaltAgent = this._target.cobaltAgent();

this.element = this._shadowRoot.createChild('div');
this.element.textContent = 'Cobalt Console';
Expand All @@ -32,7 +33,7 @@ export default class CobaltPanel extends UI.VBox {
this.run(`(function() { window.h5vcc.traceEvent.stop();})()`);
console.log("Stopped Trace");
}));
trace_files.forEach( (file) => {
trace_files.forEach((file) => {
traceContainer.appendChild(UI.createTextButton(Common.UIString('Download ' + file[0]), event => {
console.log("Download Trace");
const filename = file[1];
Expand All @@ -46,6 +47,28 @@ export default class CobaltPanel extends UI.VBox {
});
}));
});
const consoleContainer = this.element.createChild('div', 'console-container');
consoleContainer.appendChild(UI.createTextButton(Common.UIString('DebugCommand'), event => {
const outputElement = document.getElementsByClassName('console-output')[0];
const command = document.getElementsByClassName('debug-command')[0].value;
if (command.length == 0) {
const result = this._cobaltAgent.invoke_getConsoleCommands().then(result => {
outputElement.innerHTML = JSON.stringify(result.commands, undefined, 2);
});
} else {
const result = this._cobaltAgent.invoke_sendConsoleCommand({
command: command,
message: document.getElementsByClassName('debug-message')[0].value
}).then(result => {
outputElement.innerHTML = JSON.stringify(result, undefined, 2);
});
}
}));
consoleContainer.appendChild(UI.createLabel('Command:'));
consoleContainer.appendChild(UI.createInput('debug-command', 'text'));
consoleContainer.appendChild(UI.createLabel('Message:'));
consoleContainer.appendChild(UI.createInput('debug-message', 'text'));
consoleContainer.createChild('pre', 'console-output');
}

async run(expression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ version
major 1
minor 3

# The Cobalt domain defines custom methods and events for Cobalt
experimental domain Cobalt
command disable

command enable

experimental type ConsoleCommand extends object
properties
string command
string shortHelp
string longHelp

experimental command getConsoleCommands
returns
array of ConsoleCommand commands

experimental command sendConsoleCommand
parameters
string command
optional string message

experimental domain Accessibility
depends on DOM

Expand Down

0 comments on commit ab9aa01

Please sign in to comment.