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"
|
2014-04-15 03:15:19 +00:00
|
|
|
#include "native_mate/template_util.h"
|
2014-04-15 03:04:36 +00:00
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
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:
|
|
|
|
// // Optional, only required if non-empty template should be used.
|
2014-04-16 03:58:17 +00:00
|
|
|
// virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
2014-04-15 03:04:36 +00:00
|
|
|
// v8::Isolate* isolate);
|
|
|
|
// ...
|
|
|
|
// };
|
|
|
|
//
|
2014-04-16 03:58:17 +00:00
|
|
|
// // my_class.cc
|
|
|
|
// gin::ObjectTemplateBuilder MyClass::GetObjectTemplateBuilder(
|
2014-04-15 03:04:36 +00:00
|
|
|
// v8::Isolate* isolate) {
|
2014-04-16 03:58:17 +00:00
|
|
|
// return Wrappable<MyClass>::GetObjectTemplateBuilder(isolate)
|
|
|
|
// .SetValue("foobar", 42);
|
2014-04-15 03:04:36 +00:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
class ObjectTemplateBuilder;
|
|
|
|
|
2014-04-16 03:58:17 +00:00
|
|
|
class Wrappable {
|
2014-04-16 02:36:54 +00:00
|
|
|
public:
|
2014-04-16 03:58:17 +00:00
|
|
|
// Retrieve (or create) the v8 wrapper object cooresponding to this object.
|
|
|
|
// If the type is created via the Constructor, then the GetWrapper would
|
|
|
|
// return the constructed object, otherwise it would try to create a new
|
|
|
|
// object constructed by GetObjectTemplateBuilder.
|
2015-05-22 11:11:02 +00:00
|
|
|
v8::Local<v8::Object> GetWrapper(v8::Isolate* isolate);
|
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-16 03:58:17 +00:00
|
|
|
// Bind the C++ class to the JS wrapper.
|
2015-05-22 11:11:02 +00:00
|
|
|
void Wrap(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
|
2014-04-16 02:36:54 +00:00
|
|
|
|
2014-04-16 03:58:17 +00:00
|
|
|
// The user should define T::BuildPrototype if they want to use Constructor
|
|
|
|
// to build a constructor function for this type.
|
|
|
|
static void BuildPrototype(v8::Isolate* isolate,
|
2015-05-22 11:11:02 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> prototype);
|
2014-04-16 03:58:17 +00:00
|
|
|
|
2014-04-15 03:04:36 +00:00
|
|
|
protected:
|
2014-04-16 03:58:17 +00:00
|
|
|
Wrappable();
|
|
|
|
virtual ~Wrappable();
|
2014-04-15 03:04:36 +00:00
|
|
|
|
|
|
|
virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate);
|
|
|
|
|
2015-02-13 03:37:55 +00:00
|
|
|
// Called after the "_init" method gets called in JavaScript.
|
|
|
|
virtual void AfterInit(v8::Isolate* isolate) {}
|
|
|
|
|
2014-04-15 03:04:36 +00:00
|
|
|
private:
|
2014-04-16 03:58:17 +00:00
|
|
|
static MATE_WEAK_CALLBACK(WeakCallback, v8::Object, Wrappable);
|
2014-04-15 03:04:36 +00:00
|
|
|
|
2015-06-23 09:08:52 +00:00
|
|
|
v8::Isolate* isolate_;
|
2015-03-23 08:19:55 +00:00
|
|
|
v8::UniquePersistent<v8::Object> wrapper_; // Weak
|
2014-04-15 03:04:36 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(Wrappable);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-04-16 03:58:17 +00:00
|
|
|
// This converter handles any subclass of Wrappable.
|
2014-04-15 03:04:36 +00:00
|
|
|
template<typename T>
|
2014-04-15 03:15:19 +00:00
|
|
|
struct Converter<T*, typename enable_if<
|
2014-04-16 03:58:17 +00:00
|
|
|
is_convertible<T*, Wrappable*>::value>::type> {
|
2015-05-22 11:11:02 +00:00
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, T* val) {
|
2015-06-24 08:35:37 +00:00
|
|
|
if (val)
|
|
|
|
return val->GetWrapper(isolate);
|
|
|
|
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) {
|
2014-04-16 03:58:17 +00:00
|
|
|
*out = static_cast<T*>(static_cast<Wrappable*>(
|
2014-04-15 03:04:36 +00:00
|
|
|
internal::FromV8Impl(isolate, val)));
|
|
|
|
return *out != NULL;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mate
|
|
|
|
|
|
|
|
#endif // NATIVE_MATE_WRAPPABLE_H_
|