2019-05-02 02:45:23 +00:00
|
|
|
// Copyright 2013 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE.chromium file.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_WRAPPABLE_BASE_H_
|
|
|
|
#define ELECTRON_SHELL_COMMON_GIN_HELPER_WRAPPABLE_BASE_H_
|
2019-12-05 09:46:34 +00:00
|
|
|
|
|
|
|
#include "v8/include/v8.h"
|
2016-08-02 08:01:19 +00:00
|
|
|
|
2019-09-04 02:14:16 +00:00
|
|
|
namespace gin {
|
2019-10-15 01:15:23 +00:00
|
|
|
class Arguments;
|
2021-07-02 00:51:37 +00:00
|
|
|
}
|
2016-08-02 08:01:19 +00:00
|
|
|
|
2019-12-05 09:46:34 +00:00
|
|
|
namespace gin_helper {
|
2019-09-04 02:14:16 +00:00
|
|
|
|
2016-08-02 08:01:19 +00:00
|
|
|
// Wrappable is a base class for C++ objects that have corresponding v8 wrapper
|
|
|
|
// objects. To retain a Wrappable object on the stack, use a gin::Handle.
|
|
|
|
//
|
|
|
|
// USAGE:
|
|
|
|
// // my_class.h
|
|
|
|
// class MyClass : Wrappable<MyClass> {
|
|
|
|
// public:
|
|
|
|
// ...
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// Subclasses should also typically have private constructors and expose a
|
2019-12-05 09:46:34 +00:00
|
|
|
// static Create function that returns a gin::Handle. Forcing creators through
|
2016-08-02 08:01:19 +00:00
|
|
|
// this static Create function will enforce that clients actually create a
|
|
|
|
// wrapper for the object. If clients fail to create a wrapper for a wrappable
|
|
|
|
// object, the object will leak because we use the weak callback from the
|
|
|
|
// wrapper as the signal to delete the wrapped object.
|
|
|
|
class WrappableBase {
|
|
|
|
public:
|
|
|
|
WrappableBase();
|
2020-07-14 01:13:34 +00:00
|
|
|
WrappableBase(const WrappableBase&) = delete;
|
|
|
|
WrappableBase& operator=(const WrappableBase&) = delete;
|
2016-08-02 08:01:19 +00:00
|
|
|
virtual ~WrappableBase();
|
|
|
|
|
2021-05-27 18:48:03 +00:00
|
|
|
// Retrieve the v8 wrapper object corresponding to this object.
|
2018-02-20 00:29:02 +00:00
|
|
|
v8::Local<v8::Object> GetWrapper() const;
|
2019-04-30 13:45:05 +00:00
|
|
|
v8::MaybeLocal<v8::Object> GetWrapper(v8::Isolate* isolate) const;
|
2016-08-02 08:01:19 +00:00
|
|
|
|
|
|
|
// Returns the Isolate this object is created in.
|
|
|
|
v8::Isolate* isolate() const { return isolate_; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Called after the "_init" method gets called in JavaScript.
|
|
|
|
virtual void AfterInit(v8::Isolate* isolate) {}
|
|
|
|
|
|
|
|
// Bind the C++ class to the JS wrapper.
|
|
|
|
// This method should only be called by classes using Constructor.
|
|
|
|
virtual void InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
|
|
|
|
|
2019-12-05 09:46:34 +00:00
|
|
|
// Helper to init with arguments.
|
2019-10-15 01:15:23 +00:00
|
|
|
void InitWithArgs(gin::Arguments* args);
|
|
|
|
|
2020-03-11 01:16:58 +00:00
|
|
|
v8::Global<v8::Object> wrapper_; // Weak
|
|
|
|
|
2016-08-02 08:01:19 +00:00
|
|
|
private:
|
|
|
|
static void FirstWeakCallback(
|
|
|
|
const v8::WeakCallbackInfo<WrappableBase>& data);
|
|
|
|
static void SecondWeakCallback(
|
|
|
|
const v8::WeakCallbackInfo<WrappableBase>& data);
|
|
|
|
|
2019-08-26 00:16:28 +00:00
|
|
|
v8::Isolate* isolate_ = nullptr;
|
2016-08-02 08:01:19 +00:00
|
|
|
};
|
|
|
|
|
2019-12-05 09:46:34 +00:00
|
|
|
} // namespace gin_helper
|
2016-08-02 08:01:19 +00:00
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_COMMON_GIN_HELPER_WRAPPABLE_BASE_H_
|