diff options
author | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-21 18:38:51 +0000 |
---|---|---|
committer | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-21 18:38:51 +0000 |
commit | 93f9f360fc88ea3bae8e74b7b22ad25d1ddaec85 (patch) | |
tree | 35d4b253f7d95749f54c3ed179bb8c99834a3f79 /gin/wrappable.h | |
parent | 69ef3fe6c0e79036f8e6f1b740b0cd96137e461e (diff) | |
download | chromium_src-93f9f360fc88ea3bae8e74b7b22ad25d1ddaec85.zip chromium_src-93f9f360fc88ea3bae8e74b7b22ad25d1ddaec85.tar.gz chromium_src-93f9f360fc88ea3bae8e74b7b22ad25d1ddaec85.tar.bz2 |
[Gin] Add a mechanism for wrapping C++ object
This CL adds a mechanism for wrapping C++ objects to Gin. The approach in this
CL is similar to Blink's ScriptWrappable class, with a couple of differences:
1) gin::Wrappable has a vtable whereas Blink's ScriptWrappable class does not.
Having a vtable in this base class lets us simplify a large number of
concerns. We've talked about adding a vtable to ScriptWrappable but have
avoided it because Blink creates many thousands of wrapped objects. When we
refactor Blink to use Gin, we can still support the non-vtable approach, but
most clients of Gin will want the simpler approach.
2) In Gin, we've bound together the notion of being reference counted with the
notion of being wrappable from JavaScript. In Blink, those concepts are
separate because we don't want to introduce a virtual destructor for
ScriptWrappable. However, because gin::Wrappable already has a vtable,
adding a virtual destructor is relatively cheap.
Actually wrapping a C++ object still takes too much typing, but we can improve
that in future CLs.
R=jochen@chromium.org
BUG=317398
Review URL: https://codereview.chromium.org/79203004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@236555 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gin/wrappable.h')
-rw-r--r-- | gin/wrappable.h | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/gin/wrappable.h b/gin/wrappable.h new file mode 100644 index 0000000..66c2389 --- /dev/null +++ b/gin/wrappable.h @@ -0,0 +1,60 @@ +// 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 file. + +#ifndef GIN_WRAPPABLE_H_ +#define GIN_WRAPPABLE_H_ + +#include "base/memory/ref_counted.h" +#include "gin/converter.h" +#include "gin/public/wrapper_info.h" + +namespace gin { + +class Wrappable : public base::RefCounted<Wrappable> { + public: + virtual WrapperInfo* GetWrapperInfo() = 0; + + protected: + Wrappable(); + virtual ~Wrappable(); + + private: + friend class base::RefCounted<Wrappable>; + friend struct Converter<Wrappable*>; + + static void WeakCallback( + const v8::WeakCallbackData<v8::Object, Wrappable>& data); + v8::Handle<v8::Object> CreateWrapper(v8::Isolate* isolate); + + v8::Persistent<v8::Object> wrapper_; // Weak + + DISALLOW_COPY_AND_ASSIGN(Wrappable); +}; + +template<> +struct Converter<Wrappable*> { + static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, + Wrappable* val); + static bool FromV8(v8::Handle<v8::Value> val, + Wrappable** out); +}; + +template<typename T> +struct WrappableConverter { + static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T* val) { + return Converter<Wrappable*>::ToV8(isolate, val); + } + static bool FromV8(v8::Handle<v8::Value> val, T** out) { + Wrappable* wrappable = 0; + if (!Converter<Wrappable*>::FromV8(val, &wrappable) + || wrappable->GetWrapperInfo() != &T::kWrapperInfo) + return false; + *out = static_cast<T*>(wrappable); + return true; + } +}; + +} // namespace gin + +#endif // GIN_WRAPPABLE_H_ |