Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Unified Diff: gin/wrappable.h

Issue 107113005: Gin: Consolidate all the Converter<Wrappable> subclasses. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove unneeded friend declaration Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « gin/function_template.h.pump ('k') | gin/wrappable.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gin/wrappable.h
diff --git a/gin/wrappable.h b/gin/wrappable.h
index 0fa3697863fde2c453e3077c515e89dcc6386471..165705536653c58321ba5c7426dd7c1c896fee5b 100644
--- a/gin/wrappable.h
+++ b/gin/wrappable.h
@@ -5,6 +5,7 @@
#ifndef GIN_WRAPPABLE_H_
#define GIN_WRAPPABLE_H_
+#include "base/template_util.h"
#include "gin/converter.h"
#include "gin/public/wrapper_info.h"
@@ -53,8 +54,6 @@ class Wrappable {
virtual ~Wrappable();
private:
- friend struct Converter<Wrappable*>;
-
static void WeakCallback(
const v8::WeakCallbackData<v8::Object, Wrappable>& data);
v8::Handle<v8::Object> CreateWrapper(v8::Isolate* isolate);
@@ -64,28 +63,25 @@ class Wrappable {
DISALLOW_COPY_AND_ASSIGN(Wrappable);
};
-template<>
-struct Converter<Wrappable*> {
- static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
- Wrappable* val);
- static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
- Wrappable** out);
-};
-
+// This converter handles any subclass of Wrappable.
template<typename T>
-struct WrappableConverter {
- static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T* val) {
- return Converter<Wrappable*>::ToV8(isolate, val);
+struct Converter<T, typename base::enable_if<
+ base::is_convertible<T, Wrappable*>::value>::type> {
+ static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T val) {
+ return val->GetWrapper(isolate);
}
- static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, T** out) {
- Wrappable* wrappable = NULL;
- if (!Converter<Wrappable*>::FromV8(isolate, val, &wrappable)
- || wrappable->GetWrapperInfo() != &T::kWrapperInfo)
abarth-chromium 2013/12/06 01:50:55 I think we're missing this security check. You ne
+
+ static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, T* out) {
+ if (!val->IsObject())
+ return false;
+ v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(val);
+ WrapperInfo* info = WrapperInfo::From(obj);
+ if (!info)
return false;
- // Currently we require that you unwrap to the exact runtime type of the
- // wrapped object.
- // TODO(abarth): Support unwrapping to a base class.
- *out = static_cast<T*>(wrappable);
+ void* pointer = obj->GetAlignedPointerFromInternalField(kEncodedValueIndex);
+ T result = static_cast<T>(pointer);
+ CHECK(result->GetWrapperInfo() == info); // Security check for cast above.
+ *out = result;
return true;
}
};
« no previous file with comments | « gin/function_template.h.pump ('k') | gin/wrappable.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698