refactor: ginify Tray (#22822)

* refactor: ginify Tray

* lint

* improve argument parsing logic

* remove redundant imports from tray.js

* new Tray produces an instanceof Tray

* make Constructible generic

* lint

* clean up on exit
This commit is contained in:
Jeremy Apthorp 2020-03-29 18:32:02 -07:00 committed by GitHub
parent 76ae3b7ecb
commit a3e28788ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 380 additions and 88 deletions

View file

@ -0,0 +1,33 @@
// Copyright (c) 2020 Slack Technologies, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/gin_helper/cleaned_up_at_exit.h"
#include "base/containers/flat_set.h"
#include "base/no_destructor.h"
namespace gin_helper {
base::flat_set<CleanedUpAtExit*>& GetDoomed() {
static base::NoDestructor<base::flat_set<CleanedUpAtExit*>> doomed;
return *doomed;
}
CleanedUpAtExit::CleanedUpAtExit() {
GetDoomed().insert(this);
}
CleanedUpAtExit::~CleanedUpAtExit() {
GetDoomed().erase(this);
}
// static
void CleanedUpAtExit::DoCleanup() {
auto& doomed = GetDoomed();
while (!doomed.empty()) {
auto iter = doomed.begin();
delete *iter;
// It removed itself from the list.
}
}
} // namespace gin_helper

View file

@ -0,0 +1,27 @@
// Copyright (c) 2020 Slack Technologies, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef SHELL_COMMON_GIN_HELPER_CLEANED_UP_AT_EXIT_H_
#define SHELL_COMMON_GIN_HELPER_CLEANED_UP_AT_EXIT_H_
namespace gin_helper {
// Objects of this type will be destroyed immediately prior to disposing the V8
// Isolate. This should only be used for gin::Wrappable objects, whose lifetime
// is otherwise managed by V8.
//
// NB. This is only needed because v8::Global objects that have SetWeak
// finalization callbacks do not have their finalization callbacks invoked at
// Isolate teardown.
class CleanedUpAtExit {
public:
CleanedUpAtExit();
virtual ~CleanedUpAtExit();
static void DoCleanup();
};
} // namespace gin_helper
#endif // SHELL_COMMON_GIN_HELPER_CLEANED_UP_AT_EXIT_H_

View file

@ -0,0 +1,67 @@
// Copyright (c) 2020 Slack Technologies, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef SHELL_COMMON_GIN_HELPER_CONSTRUCTIBLE_H_
#define SHELL_COMMON_GIN_HELPER_CONSTRUCTIBLE_H_
#include "gin/wrappable.h"
#include "shell/common/gin_helper/function_template_extensions.h"
namespace gin_helper {
template <typename T>
class EventEmitterMixin;
// Helper class for Wrappable objects which should be constructible with 'new'
// in JavaScript.
//
// To use, inherit from gin::Wrappable and gin_helper::Constructible, and
// define the static methods New and FillObjectTemplate:
//
// class Example : public gin::Wrappable<Example>,
// public gin_helper::Constructible<Example> {
// public:
// static gin::Handle<Tray> New(...usual gin method arguments...);
// static v8::Local<v8::ObjectTemplate> FillObjectTemplate(
// v8::Isolate*,
// v8::Local<v8::ObjectTemplate>);
// }
//
// Do NOT define the usual gin::Wrappable::GetObjectTemplateBuilder. It will
// not be called for Constructible classes.
//
// To expose the constructor, call GetConstructor:
//
// gin::Dictionary dict(isolate, exports);
// dict.Set("Example", Example::GetConstructor(context));
template <typename T>
class Constructible {
public:
static v8::Local<v8::Function> GetConstructor(
v8::Local<v8::Context> context) {
v8::Isolate* isolate = context->GetIsolate();
gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
auto* wrapper_info = &T::kWrapperInfo;
v8::Local<v8::FunctionTemplate> constructor =
data->GetFunctionTemplate(wrapper_info);
if (constructor.IsEmpty()) {
constructor = gin::CreateConstructorFunctionTemplate(
isolate, base::BindRepeating(&T::New));
if (std::is_base_of<EventEmitterMixin<T>, T>::value) {
constructor->Inherit(
gin_helper::internal::GetEventEmitterTemplate(isolate));
}
constructor->InstanceTemplate()->SetInternalFieldCount(
gin::kNumberOfInternalFields);
v8::Local<v8::ObjectTemplate> obj_templ =
T::FillObjectTemplate(isolate, constructor->InstanceTemplate());
data->SetObjectTemplate(wrapper_info, obj_templ);
data->SetFunctionTemplate(wrapper_info, constructor);
}
return constructor->GetFunction(context).ToLocalChecked();
}
};
} // namespace gin_helper
#endif // SHELL_COMMON_GIN_HELPER_CONSTRUCTIBLE_H_

View file

@ -8,7 +8,6 @@
#include "shell/browser/api/event.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "ui/events/event_constants.h"
namespace gin_helper {
@ -50,21 +49,6 @@ v8::Local<v8::Object> CreateEvent(v8::Isolate* isolate,
return event;
}
v8::Local<v8::Object> CreateEventFromFlags(v8::Isolate* isolate, int flags) {
const int mouse_button_flags =
(ui::EF_RIGHT_MOUSE_BUTTON | ui::EF_LEFT_MOUSE_BUTTON |
ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_BACK_MOUSE_BUTTON |
ui::EF_FORWARD_MOUSE_BUTTON);
const int is_mouse_click = static_cast<bool>(flags & mouse_button_flags);
Dictionary obj = gin::Dictionary::CreateEmpty(isolate);
obj.Set("shiftKey", static_cast<bool>(flags & ui::EF_SHIFT_DOWN));
obj.Set("ctrlKey", static_cast<bool>(flags & ui::EF_CONTROL_DOWN));
obj.Set("altKey", static_cast<bool>(flags & ui::EF_ALT_DOWN));
obj.Set("metaKey", static_cast<bool>(flags & ui::EF_COMMAND_DOWN));
obj.Set("triggeredByAccelerator", !is_mouse_click);
return obj.GetHandle();
}
v8::Local<v8::Object> CreateNativeEvent(
v8::Isolate* isolate,
v8::Local<v8::Object> sender,

View file

@ -25,7 +25,6 @@ v8::Local<v8::Object> CreateEvent(
v8::Isolate* isolate,
v8::Local<v8::Object> sender = v8::Local<v8::Object>(),
v8::Local<v8::Object> custom_event = v8::Local<v8::Object>());
v8::Local<v8::Object> CreateEventFromFlags(v8::Isolate* isolate, int flags);
v8::Local<v8::Object> CreateNativeEvent(
v8::Isolate* isolate,
v8::Local<v8::Object> sender,
@ -59,16 +58,6 @@ class EventEmitter : public gin_helper::Wrappable<T> {
std::forward<Args>(args)...);
}
// this.emit(name, new Event(flags), args...);
template <typename... Args>
bool EmitWithFlags(base::StringPiece name, int flags, Args&&... args) {
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
return EmitCustomEvent(name,
internal::CreateEventFromFlags(isolate(), flags),
std::forward<Args>(args)...);
}
// this.emit(name, new Event(), args...);
template <typename... Args>
bool Emit(base::StringPiece name, Args&&... args) {

View file

@ -37,6 +37,24 @@ inline bool GetNextArgument(Arguments* args,
return true;
}
// Like gin::CreateFunctionTemplate, but doesn't remove the template's
// prototype.
template <typename Sig>
v8::Local<v8::FunctionTemplate> CreateConstructorFunctionTemplate(
v8::Isolate* isolate,
base::RepeatingCallback<Sig> callback,
InvokerOptions invoker_options = {}) {
typedef internal::CallbackHolder<Sig> HolderT;
HolderT* holder =
new HolderT(isolate, std::move(callback), std::move(invoker_options));
v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New(
isolate, &internal::Dispatcher<Sig>::DispatchToCallback,
ConvertToV8<v8::Local<v8::External>>(isolate,
holder->GetHandle(isolate)));
return tmpl;
}
} // namespace gin
#endif // SHELL_COMMON_GIN_HELPER_FUNCTION_TEMPLATE_EXTENSIONS_H_