refactor: EventEmitters without gin_helper (#22726)
This commit is contained in:
parent
fc661ec56b
commit
232ca8af39
12 changed files with 277 additions and 49 deletions
|
@ -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)
|
||||
|
|
|
@ -12,9 +12,11 @@
|
|||
#include "base/values.h"
|
||||
#include "content/public/browser/devtools_agent_host_client.h"
|
||||
#include "content/public/browser/web_contents_observer.h"
|
||||
#include "gin/arguments.h"
|
||||
#include "gin/handle.h"
|
||||
#include "gin/wrappable.h"
|
||||
#include "shell/browser/event_emitter_mixin.h"
|
||||
#include "shell/common/gin_helper/promise.h"
|
||||
#include "shell/common/gin_helper/trackable_object.h"
|
||||
|
||||
namespace content {
|
||||
class DevToolsAgentHost;
|
||||
|
@ -25,16 +27,19 @@ namespace electron {
|
|||
|
||||
namespace api {
|
||||
|
||||
class Debugger : public gin_helper::TrackableObject<Debugger>,
|
||||
class Debugger : public gin::Wrappable<Debugger>,
|
||||
public gin_helper::EventEmitterMixin<Debugger>,
|
||||
public content::DevToolsAgentHostClient,
|
||||
public content::WebContentsObserver {
|
||||
public:
|
||||
static gin::Handle<Debugger> Create(v8::Isolate* isolate,
|
||||
content::WebContents* web_contents);
|
||||
|
||||
// gin_helper::TrackableObject:
|
||||
static void BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Local<v8::FunctionTemplate> prototype);
|
||||
// gin::Wrappable
|
||||
static gin::WrapperInfo kWrapperInfo;
|
||||
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
||||
v8::Isolate* isolate) override;
|
||||
const char* GetTypeName() override;
|
||||
|
||||
protected:
|
||||
Debugger(v8::Isolate* isolate, content::WebContents* web_contents);
|
||||
|
@ -53,10 +58,10 @@ class Debugger : public gin_helper::TrackableObject<Debugger>,
|
|||
using PendingRequestMap =
|
||||
std::map<int, gin_helper::Promise<base::DictionaryValue>>;
|
||||
|
||||
void Attach(gin_helper::Arguments* args);
|
||||
void Attach(gin::Arguments* args);
|
||||
bool IsAttached();
|
||||
void Detach();
|
||||
v8::Local<v8::Promise> SendCommand(gin_helper::Arguments* args);
|
||||
v8::Local<v8::Promise> SendCommand(gin::Arguments* args);
|
||||
void ClearPendingRequests();
|
||||
|
||||
content::WebContents* web_contents_; // Weak Reference.
|
||||
|
|
45
shell/browser/api/electron_api_event_emitter.cc
Normal file
45
shell/browser/api/electron_api_event_emitter.cc
Normal file
|
@ -0,0 +1,45 @@
|
|||
// Copyright (c) 2019 Slack Technologies, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/browser/api/electron_api_event_emitter.h"
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "base/callback.h"
|
||||
#include "gin/dictionary.h"
|
||||
#include "shell/common/gin_converters/callback_converter.h"
|
||||
#include "shell/common/node_includes.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace {
|
||||
|
||||
v8::Global<v8::Object> event_emitter_prototype;
|
||||
|
||||
void SetEventEmitterPrototype(v8::Isolate* isolate,
|
||||
v8::Local<v8::Object> proto) {
|
||||
event_emitter_prototype.Reset(isolate, proto);
|
||||
}
|
||||
|
||||
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::Dictionary dict(isolate, exports);
|
||||
dict.Set("setEventEmitterPrototype",
|
||||
base::BindRepeating(&SetEventEmitterPrototype));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace electron {
|
||||
|
||||
v8::Local<v8::Object> GetEventEmitterPrototype(v8::Isolate* isolate) {
|
||||
CHECK(!event_emitter_prototype.IsEmpty());
|
||||
return event_emitter_prototype.Get(isolate);
|
||||
}
|
||||
|
||||
} // namespace electron
|
||||
|
||||
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_browser_event_emitter, Initialize)
|
21
shell/browser/api/electron_api_event_emitter.h
Normal file
21
shell/browser/api/electron_api_event_emitter.h
Normal file
|
@ -0,0 +1,21 @@
|
|||
// Copyright (c) 2019 Slack Technologies, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_BROWSER_API_ELECTRON_API_EVENT_EMITTER_H_
|
||||
#define SHELL_BROWSER_API_ELECTRON_API_EVENT_EMITTER_H_
|
||||
|
||||
namespace v8 {
|
||||
template <typename T>
|
||||
class Local;
|
||||
class Object;
|
||||
class Isolate;
|
||||
} // namespace v8
|
||||
|
||||
namespace electron {
|
||||
|
||||
v8::Local<v8::Object> GetEventEmitterPrototype(v8::Isolate* isolate);
|
||||
|
||||
} // namespace electron
|
||||
|
||||
#endif // SHELL_BROWSER_API_ELECTRON_API_EVENT_EMITTER_H_
|
Loading…
Add table
Add a link
Reference in a new issue