electron/native_mate/wrappable.h

134 lines
3.9 KiB
C
Raw Normal View History

2014-04-15 03:04:36 +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.
#ifndef NATIVE_MATE_WRAPPABLE_H_
#define NATIVE_MATE_WRAPPABLE_H_
#include "native_mate/compat.h"
#include "native_mate/converter.h"
#include "native_mate/template_util.h"
2014-04-15 03:04:36 +00:00
namespace mate {
namespace internal {
struct Destroyable;
2015-05-22 11:11:02 +00:00
void* FromV8Impl(v8::Isolate* isolate, v8::Local<v8::Value> val);
2014-04-15 03:04:36 +00:00
} // namespace internal
2014-04-16 03:58:17 +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.
2014-04-15 03:04:36 +00:00
//
// USAGE:
// // my_class.h
2014-04-16 03:58:17 +00:00
// class MyClass : Wrappable<MyClass> {
2014-04-15 03:04:36 +00:00
// public:
// ...
// };
//
// Subclasses should also typically have private constructors and expose a
// static Create function that returns a mate::Handle. Forcing creators through
// 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.
2016-04-25 01:17:39 +00:00
class WrappableBase {
public:
2016-04-25 01:17:39 +00:00
WrappableBase();
virtual ~WrappableBase();
// Retrieve the v8 wrapper object cooresponding to this object.
v8::Local<v8::Object> GetWrapper();
2014-04-16 03:58:17 +00:00
2015-06-23 09:13:02 +00:00
// Returns the Isolate this object is created in.
v8::Isolate* isolate() const { return isolate_; }
2014-04-15 03:04:36 +00:00
protected:
2015-02-13 03:37:55 +00:00
// Called after the "_init" method gets called in JavaScript.
virtual void AfterInit(v8::Isolate* isolate) {}
2016-08-02 06:14:48 +00:00
// Bind the C++ class to the JS wrapper.
// This method should only be called by classes using Constructor.
2016-08-02 06:28:09 +00:00
virtual void InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
2016-08-02 06:14:48 +00:00
2014-04-15 03:04:36 +00:00
private:
friend struct internal::Destroyable;
2016-04-25 01:17:39 +00:00
static void FirstWeakCallback(
const v8::WeakCallbackInfo<WrappableBase>& data);
static void SecondWeakCallback(
const v8::WeakCallbackInfo<WrappableBase>& data);
2014-04-15 03:04:36 +00:00
2015-06-23 09:08:52 +00:00
v8::Isolate* isolate_;
2016-04-25 01:17:39 +00:00
v8::Global<v8::Object> wrapper_; // Weak
DISALLOW_COPY_AND_ASSIGN(WrappableBase);
};
template<typename T>
class Wrappable : public WrappableBase {
public:
Wrappable() {}
2016-08-02 06:14:48 +00:00
protected:
2016-04-25 01:17:39 +00:00
// Init the class with T::BuildPrototype.
void Init(v8::Isolate* isolate) {
// Fill the object template.
if (!templ_) {
v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolate);
T::BuildPrototype(isolate, templ->PrototypeTemplate());
templ_ = new v8::Global<v8::FunctionTemplate>(isolate, templ);
2016-04-25 01:17:39 +00:00
}
v8::Local<v8::Object> wrapper;
v8::Local<v8::FunctionTemplate> templ =
v8::Local<v8::FunctionTemplate>::New(isolate, *templ_);
2016-04-25 01:17:39 +00:00
// |wrapper| may be empty in some extreme cases, e.g., when
// Object.prototype.constructor is overwritten.
if (!templ->PrototypeTemplate()->NewInstance(
isolate->GetCurrentContext()).ToLocal(&wrapper)) {
2016-04-25 01:17:39 +00:00
// The current wrappable object will be no longer managed by V8. Delete
// this now.
delete this;
return;
}
InitWith(isolate, wrapper);
}
private:
static v8::Global<v8::FunctionTemplate>* templ_; // Leaked on purpose
2014-04-15 03:04:36 +00:00
DISALLOW_COPY_AND_ASSIGN(Wrappable);
};
2016-04-25 01:17:39 +00:00
// static
template<typename T>
v8::Global<v8::FunctionTemplate>* Wrappable<T>::templ_ = nullptr;
2014-04-15 03:04:36 +00:00
2014-04-16 03:58:17 +00:00
// This converter handles any subclass of Wrappable.
2016-04-25 01:17:39 +00:00
template <typename T>
struct Converter<T*,
typename std::enable_if<
std::is_convertible<T*, WrappableBase*>::value>::type> {
2015-05-22 11:11:02 +00:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, T* val) {
if (val)
2016-04-25 01:17:39 +00:00
return val->GetWrapper();
else
return v8::Null(isolate);
2014-04-15 03:04:36 +00:00
}
2015-05-22 11:11:02 +00:00
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, T** out) {
2016-04-25 01:17:39 +00:00
*out = static_cast<T*>(static_cast<WrappableBase*>(
2014-04-15 03:04:36 +00:00
internal::FromV8Impl(isolate, val)));
2016-04-25 01:17:39 +00:00
return *out != nullptr;
2014-04-15 03:04:36 +00:00
}
};
} // namespace mate
#endif // NATIVE_MATE_WRAPPABLE_H_