refactor: EventEmitters without gin_helper (#22726)

This commit is contained in:
Jeremy Apthorp 2020-03-18 12:57:08 -07:00 committed by GitHub
parent fc661ec56b
commit 232ca8af39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 277 additions and 49 deletions

View file

@ -12,9 +12,9 @@
#include "base/json/json_writer.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/web_contents.h"
#include "gin/object_template_builder.h"
#include "gin/per_isolate_data.h"
#include "shell/common/gin_converters/value_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/node_includes.h"
using content::DevToolsAgentHost;
@ -23,10 +23,10 @@ namespace electron {
namespace api {
gin::WrapperInfo Debugger::kWrapperInfo = {gin::kEmbedderNativeGin};
Debugger::Debugger(v8::Isolate* isolate, content::WebContents* web_contents)
: content::WebContentsObserver(web_contents), web_contents_(web_contents) {
Init(isolate);
}
: content::WebContentsObserver(web_contents), web_contents_(web_contents) {}
Debugger::~Debugger() = default;
@ -41,8 +41,10 @@ void Debugger::DispatchProtocolMessage(DevToolsAgentHost* agent_host,
base::span<const uint8_t> message) {
DCHECK(agent_host == agent_host_);
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
base::StringPiece message_str(reinterpret_cast<const char*>(message.data()),
message.size());
@ -96,24 +98,24 @@ void Debugger::RenderFrameHostChanged(content::RenderFrameHost* old_rfh,
}
}
void Debugger::Attach(gin_helper::Arguments* args) {
void Debugger::Attach(gin::Arguments* args) {
std::string protocol_version;
args->GetNext(&protocol_version);
if (agent_host_) {
args->ThrowError("Debugger is already attached to the target");
args->ThrowTypeError("Debugger is already attached to the target");
return;
}
if (!protocol_version.empty() &&
!DevToolsAgentHost::IsSupportedProtocolVersion(protocol_version)) {
args->ThrowError("Requested protocol version is not supported");
args->ThrowTypeError("Requested protocol version is not supported");
return;
}
agent_host_ = DevToolsAgentHost::GetOrCreateFor(web_contents_);
if (!agent_host_) {
args->ThrowError("No target available");
args->ThrowTypeError("No target available");
return;
}
@ -131,8 +133,9 @@ void Debugger::Detach() {
AgentHostClosed(agent_host_.get());
}
v8::Local<v8::Promise> Debugger::SendCommand(gin_helper::Arguments* args) {
gin_helper::Promise<base::DictionaryValue> promise(isolate());
v8::Local<v8::Promise> Debugger::SendCommand(gin::Arguments* args) {
v8::Isolate* isolate = args->isolate();
gin_helper::Promise<base::DictionaryValue> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();
if (!agent_host_) {
@ -177,36 +180,20 @@ gin::Handle<Debugger> Debugger::Create(v8::Isolate* isolate,
return gin::CreateHandle(isolate, new Debugger(isolate, web_contents));
}
// static
void Debugger::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(gin::StringToV8(isolate, "Debugger"));
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
gin::ObjectTemplateBuilder Debugger::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return gin_helper::EventEmitterMixin<Debugger>::GetObjectTemplateBuilder(
isolate)
.SetMethod("attach", &Debugger::Attach)
.SetMethod("isAttached", &Debugger::IsAttached)
.SetMethod("detach", &Debugger::Detach)
.SetMethod("sendCommand", &Debugger::SendCommand);
}
const char* Debugger::GetTypeName() {
return "Debugger";
}
} // namespace api
} // namespace electron
namespace {
using electron::api::Debugger;
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
gin_helper::Dictionary(isolate, exports)
.Set("Debugger", Debugger::GetConstructor(isolate)
->GetFunction(context)
.ToLocalChecked());
}
} // namespace
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_browser_debugger, Initialize)