OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GIN_OBJECT_TEMPLATE_BUILDER_H_ | 5 #ifndef GIN_OBJECT_TEMPLATE_BUILDER_H_ |
6 #define GIN_OBJECT_TEMPLATE_BUILDER_H_ | 6 #define GIN_OBJECT_TEMPLATE_BUILDER_H_ |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
11 #include "base/template_util.h" | 11 #include "base/template_util.h" |
12 #include "gin/converter.h" | 12 #include "gin/converter.h" |
13 #include "gin/function_template.h" | 13 #include "gin/function_template.h" |
14 #include "gin/gin_export.h" | 14 #include "gin/gin_export.h" |
15 #include "v8/include/v8.h" | 15 #include "v8/include/v8.h" |
16 | 16 |
17 namespace gin { | 17 namespace gin { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // Base template - used only for non-member function pointers. Other types | 21 // Base template - used only for non-member function pointers. Other types |
22 // either go to one of the below specializations, or go here and fail to compile | 22 // either go to one of the below specializations, or go here and fail to compile |
23 // because of base::Bind(). | 23 // because of base::Bind(). |
24 template<typename T, typename Enable = void> | 24 template<typename T, typename Enable = void> |
25 struct CallbackTraits { | 25 struct CallbackTraits { |
26 static v8::Handle<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate, | 26 static v8::Local<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate, |
27 T callback) { | 27 T callback) { |
28 return CreateFunctionTemplate(isolate, base::Bind(callback)); | 28 return CreateFunctionTemplate(isolate, base::Bind(callback)); |
29 } | 29 } |
30 static void SetAsFunctionHandler(v8::Isolate* isolate, | 30 static void SetAsFunctionHandler(v8::Isolate* isolate, |
31 v8::Local<v8::ObjectTemplate> tmpl, | 31 v8::Local<v8::ObjectTemplate> tmpl, |
32 T callback) { | 32 T callback) { |
33 CreateFunctionHandler(isolate, tmpl, base::Bind(callback)); | 33 CreateFunctionHandler(isolate, tmpl, base::Bind(callback)); |
34 } | 34 } |
35 }; | 35 }; |
36 | 36 |
37 // Specialization for base::Callback. | 37 // Specialization for base::Callback. |
38 template<typename T> | 38 template<typename T> |
39 struct CallbackTraits<base::Callback<T> > { | 39 struct CallbackTraits<base::Callback<T> > { |
40 static v8::Handle<v8::FunctionTemplate> CreateTemplate( | 40 static v8::Local<v8::FunctionTemplate> CreateTemplate( |
41 v8::Isolate* isolate, const base::Callback<T>& callback) { | 41 v8::Isolate* isolate, const base::Callback<T>& callback) { |
42 return CreateFunctionTemplate(isolate, callback); | 42 return CreateFunctionTemplate(isolate, callback); |
43 } | 43 } |
44 static void SetAsFunctionHandler(v8::Isolate* isolate, | 44 static void SetAsFunctionHandler(v8::Isolate* isolate, |
45 v8::Local<v8::ObjectTemplate> tmpl, | 45 v8::Local<v8::ObjectTemplate> tmpl, |
46 const base::Callback<T>& callback) { | 46 const base::Callback<T>& callback) { |
47 CreateFunctionHandler(isolate, tmpl, callback); | 47 CreateFunctionHandler(isolate, tmpl, callback); |
48 } | 48 } |
49 }; | 49 }; |
50 | 50 |
51 // Specialization for member function pointers. We need to handle this case | 51 // Specialization for member function pointers. We need to handle this case |
52 // specially because the first parameter for callbacks to MFP should typically | 52 // specially because the first parameter for callbacks to MFP should typically |
53 // come from the the JavaScript "this" object the function was called on, not | 53 // come from the the JavaScript "this" object the function was called on, not |
54 // from the first normal parameter. | 54 // from the first normal parameter. |
55 template<typename T> | 55 template<typename T> |
56 struct CallbackTraits<T, typename base::enable_if< | 56 struct CallbackTraits<T, typename base::enable_if< |
57 base::is_member_function_pointer<T>::value>::type> { | 57 base::is_member_function_pointer<T>::value>::type> { |
58 static v8::Handle<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate, | 58 static v8::Local<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate, |
59 T callback) { | 59 T callback) { |
60 return CreateFunctionTemplate(isolate, base::Bind(callback), | 60 return CreateFunctionTemplate(isolate, base::Bind(callback), |
61 HolderIsFirstArgument); | 61 HolderIsFirstArgument); |
62 } | 62 } |
63 static void SetAsFunctionHandler(v8::Isolate* isolate, | 63 static void SetAsFunctionHandler(v8::Isolate* isolate, |
64 v8::Local<v8::ObjectTemplate> tmpl, | 64 v8::Local<v8::ObjectTemplate> tmpl, |
65 T callback) { | 65 T callback) { |
66 CreateFunctionHandler( | 66 CreateFunctionHandler( |
67 isolate, tmpl, base::Bind(callback), HolderIsFirstArgument); | 67 isolate, tmpl, base::Bind(callback), HolderIsFirstArgument); |
68 } | 68 } |
69 }; | 69 }; |
70 | 70 |
71 // This specialization allows people to construct function templates directly if | 71 // This specialization allows people to construct function templates directly if |
72 // they need to do fancier stuff. | 72 // they need to do fancier stuff. |
73 template<> | 73 template<> |
74 struct CallbackTraits<v8::Handle<v8::FunctionTemplate> > { | 74 struct CallbackTraits<v8::Local<v8::FunctionTemplate> > { |
75 static v8::Handle<v8::FunctionTemplate> CreateTemplate( | 75 static v8::Local<v8::FunctionTemplate> CreateTemplate( |
76 v8::Handle<v8::FunctionTemplate> templ) { | 76 v8::Local<v8::FunctionTemplate> templ) { |
77 return templ; | 77 return templ; |
78 } | 78 } |
79 }; | 79 }; |
80 | 80 |
81 } // namespace | 81 } // namespace |
82 | 82 |
83 | 83 |
84 // ObjectTemplateBuilder provides a handy interface to creating | 84 // ObjectTemplateBuilder provides a handy interface to creating |
85 // v8::ObjectTemplate instances with various sorts of properties. | 85 // v8::ObjectTemplate instances with various sorts of properties. |
86 class GIN_EXPORT ObjectTemplateBuilder { | 86 class GIN_EXPORT ObjectTemplateBuilder { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 CallbackTraits<T>::SetAsFunctionHandler(isolate_, template_, callback); | 124 CallbackTraits<T>::SetAsFunctionHandler(isolate_, template_, callback); |
125 return *this; | 125 return *this; |
126 } | 126 } |
127 ObjectTemplateBuilder& AddNamedPropertyInterceptor(); | 127 ObjectTemplateBuilder& AddNamedPropertyInterceptor(); |
128 ObjectTemplateBuilder& AddIndexedPropertyInterceptor(); | 128 ObjectTemplateBuilder& AddIndexedPropertyInterceptor(); |
129 | 129 |
130 v8::Local<v8::ObjectTemplate> Build(); | 130 v8::Local<v8::ObjectTemplate> Build(); |
131 | 131 |
132 private: | 132 private: |
133 ObjectTemplateBuilder& SetImpl(const base::StringPiece& name, | 133 ObjectTemplateBuilder& SetImpl(const base::StringPiece& name, |
134 v8::Handle<v8::Data> val); | 134 v8::Local<v8::Data> val); |
135 ObjectTemplateBuilder& SetPropertyImpl( | 135 ObjectTemplateBuilder& SetPropertyImpl( |
136 const base::StringPiece& name, v8::Handle<v8::FunctionTemplate> getter, | 136 const base::StringPiece& name, v8::Local<v8::FunctionTemplate> getter, |
137 v8::Handle<v8::FunctionTemplate> setter); | 137 v8::Local<v8::FunctionTemplate> setter); |
138 | 138 |
139 v8::Isolate* isolate_; | 139 v8::Isolate* isolate_; |
140 | 140 |
141 // ObjectTemplateBuilder should only be used on the stack. | 141 // ObjectTemplateBuilder should only be used on the stack. |
142 v8::Local<v8::ObjectTemplate> template_; | 142 v8::Local<v8::ObjectTemplate> template_; |
143 }; | 143 }; |
144 | 144 |
145 } // namespace gin | 145 } // namespace gin |
146 | 146 |
147 #endif // GIN_OBJECT_TEMPLATE_BUILDER_H_ | 147 #endif // GIN_OBJECT_TEMPLATE_BUILDER_H_ |
OLD | NEW |