Skip to content

Commit

Permalink
[gin] Declare a wrappable's kWrapperInfo on the class deriving from W…
Browse files Browse the repository at this point in the history
…rappable

Otherwise, we need to put INIT_WRAPPABLE() in namespace gin no matter
where the derived class is declared.

I pulled this out from https://codereview.chromium.org/111083005/

BUG=none
R=dcarney@chromium.org
TBR=aa@chromium.org

Review URL: https://codereview.chromium.org/113163007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@240271 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
jochen@chromium.org committed Dec 12, 2013
1 parent ab3ed7e commit b4acaf8
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 29 deletions.
22 changes: 10 additions & 12 deletions content/renderer/gin_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
#include "third_party/WebKit/public/web/WebKit.h"
#include "third_party/WebKit/public/web/WebView.h"

namespace gin {
namespace content {

namespace {

class TestGinObject : public Wrappable<TestGinObject> {
class TestGinObject : public gin::Wrappable<TestGinObject> {
public:
static Handle<TestGinObject> Create(v8::Isolate* isolate, bool* alive) {
static gin::WrapperInfo kWrapperInfo;

static gin::Handle<TestGinObject> Create(v8::Isolate* isolate, bool* alive) {
return CreateHandle(isolate, new TestGinObject(alive));
}

Expand All @@ -31,20 +35,14 @@ class TestGinObject : public Wrappable<TestGinObject> {
DISALLOW_COPY_AND_ASSIGN(TestGinObject);
};

INIT_WRAPPABLE(TestGinObject);

} // namespace gin

namespace content {

namespace {
gin::WrapperInfo TestGinObject::kWrapperInfo = { gin::kEmbedderNativeGin };

void RegisterTemplates(v8::Isolate* isolate) {
gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);

v8::Handle<v8::ObjectTemplate> templ = v8::ObjectTemplate::New(isolate);
templ->SetInternalFieldCount(gin::kNumberOfInternalFields);
data->SetObjectTemplate(&gin::TestGinObject::kWrapperInfo, templ);
data->SetObjectTemplate(&TestGinObject::kWrapperInfo, templ);
}

class GinBrowserTest : public RenderViewTest {
Expand Down Expand Up @@ -81,7 +79,7 @@ TEST_F(GinBrowserTest, GinAndGarbageCollection) {

// We create the object inside a scope so it's not kept alive by a handle
// on the stack.
gin::TestGinObject::Create(isolate, &alive);
TestGinObject::Create(isolate, &alive);
}

CHECK(alive);
Expand Down
2 changes: 1 addition & 1 deletion gin/function_template.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace gin {

INIT_WRAPPABLE(internal::CallbackHolderBase);
WrapperInfo internal::CallbackHolderBase::kWrapperInfo = { kEmbedderNativeGin };

void InitFunctionTemplates(PerIsolateData* isolate_data) {
if (!isolate_data->GetObjectTemplate(
Expand Down
3 changes: 3 additions & 0 deletions gin/function_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ struct CallbackParamTraits<const T*> {
// This simple base class is used so that we can share a single object template
// among every CallbackHolder instance.
class CallbackHolderBase : public Wrappable<CallbackHolderBase> {
public:
static WrapperInfo kWrapperInfo;

protected:
virtual ~CallbackHolderBase() {}
};
Expand Down
12 changes: 1 addition & 11 deletions gin/wrappable.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,11 @@ class WrappableBase {
template<typename T>
class Wrappable : public WrappableBase {
public:
static WrapperInfo kWrapperInfo;

// Retrieve (or create) the v8 wrapper object cooresponding to this object.
// To customize the wrapper created for a subclass, override GetWrapperInfo()
// instead of overriding this function.
v8::Handle<v8::Object> GetWrapper(v8::Isolate* isolate) {
return GetWrapperImpl(isolate, &kWrapperInfo);
return GetWrapperImpl(isolate, &T::kWrapperInfo);
}

protected:
Expand All @@ -81,14 +79,6 @@ class Wrappable : public WrappableBase {
};


// Subclasses of Wrappable must call this within a cc file to initialize their
// WrapperInfo. This template must be used inside namespace gin.
#define INIT_WRAPPABLE(TYPE) \
template <> \
gin::WrapperInfo gin::Wrappable<TYPE>::kWrapperInfo = { \
gin::kEmbedderNativeGin \
}

// This converter handles any subclass of Wrappable.
template<typename T>
struct Converter<T*, typename base::enable_if<
Expand Down
12 changes: 9 additions & 3 deletions gin/wrappable_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace gin {

class MyObject : public Wrappable<MyObject> {
public:
static WrapperInfo kWrapperInfo;

static gin::Handle<MyObject> Create(v8::Isolate* isolate) {
return CreateHandle(isolate, new MyObject());
}
Expand All @@ -32,14 +34,18 @@ class MyObject : public Wrappable<MyObject> {
};

class MyObject2 : public Wrappable<MyObject2> {
public:
static WrapperInfo kWrapperInfo;
};

class MyObjectBlink : public Wrappable<MyObjectBlink> {
public:
static WrapperInfo kWrapperInfo;
};

INIT_WRAPPABLE(gin::MyObject);
INIT_WRAPPABLE(gin::MyObject2);
INIT_WRAPPABLE(gin::MyObjectBlink);
WrapperInfo MyObject::kWrapperInfo = { kEmbedderNativeGin };
WrapperInfo MyObject2::kWrapperInfo = { kEmbedderNativeGin };
WrapperInfo MyObjectBlink::kWrapperInfo = { kEmbedderNativeGin };

void RegisterTemplates(v8::Isolate* isolate) {
PerIsolateData* data = PerIsolateData::From(isolate);
Expand Down
4 changes: 2 additions & 2 deletions mojo/apps/js/bindings/waiting_callback.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include "gin/per_context_data.h"
#include "gin/per_isolate_data.h"

INIT_WRAPPABLE(mojo::js::WaitingCallback);

namespace mojo {
namespace js {

Expand All @@ -20,6 +18,8 @@ v8::Handle<v8::String> GetHiddenPropertyName(v8::Isolate* isolate) {

} // namespace

gin::WrapperInfo WaitingCallback::kWrapperInfo = { gin::kEmbedderNativeGin };

WaitingCallback::WaitingCallback(v8::Isolate* isolate,
v8::Handle<v8::Function> callback)
: wait_id_() {
Expand Down
2 changes: 2 additions & 0 deletions mojo/apps/js/bindings/waiting_callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace js {
class WaitingCallback : public gin::Wrappable<WaitingCallback>,
public BindingsSupport::AsyncWaitCallback {
public:
static gin::WrapperInfo kWrapperInfo;

static gin::Handle<WaitingCallback> Create(
v8::Isolate* isolate, v8::Handle<v8::Function> callback);

Expand Down

0 comments on commit b4acaf8

Please sign in to comment.