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::Handle<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, |
| 31 v8::Local<v8::ObjectTemplate> tmpl, |
| 32 T callback) { |
| 33 CreateFunctionHandler(isolate, tmpl, base::Bind(callback)); |
| 34 } |
30 }; | 35 }; |
31 | 36 |
32 // Specialization for base::Callback. | 37 // Specialization for base::Callback. |
33 template<typename T> | 38 template<typename T> |
34 struct CallbackTraits<base::Callback<T> > { | 39 struct CallbackTraits<base::Callback<T> > { |
35 static v8::Handle<v8::FunctionTemplate> CreateTemplate( | 40 static v8::Handle<v8::FunctionTemplate> CreateTemplate( |
36 v8::Isolate* isolate, const base::Callback<T>& callback) { | 41 v8::Isolate* isolate, const base::Callback<T>& callback) { |
37 return CreateFunctionTemplate(isolate, callback); | 42 return CreateFunctionTemplate(isolate, callback); |
38 } | 43 } |
| 44 static void SetAsFunctionHandler(v8::Isolate* isolate, |
| 45 v8::Local<v8::ObjectTemplate> tmpl, |
| 46 const base::Callback<T>& callback) { |
| 47 CreateFunctionHandler(isolate, tmpl, callback); |
| 48 } |
39 }; | 49 }; |
40 | 50 |
41 // Specialization for member function pointers. We need to handle this case | 51 // Specialization for member function pointers. We need to handle this case |
42 // specially because the first parameter for callbacks to MFP should typically | 52 // specially because the first parameter for callbacks to MFP should typically |
43 // 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 |
44 // from the first normal parameter. | 54 // from the first normal parameter. |
45 template<typename T> | 55 template<typename T> |
46 struct CallbackTraits<T, typename base::enable_if< | 56 struct CallbackTraits<T, typename base::enable_if< |
47 base::is_member_function_pointer<T>::value>::type> { | 57 base::is_member_function_pointer<T>::value>::type> { |
48 static v8::Handle<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate, | 58 static v8::Handle<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate, |
49 T callback) { | 59 T callback) { |
50 return CreateFunctionTemplate(isolate, base::Bind(callback), | 60 return CreateFunctionTemplate(isolate, base::Bind(callback), |
51 HolderIsFirstArgument); | 61 HolderIsFirstArgument); |
52 } | 62 } |
| 63 static void SetAsFunctionHandler(v8::Isolate* isolate, |
| 64 v8::Local<v8::ObjectTemplate> tmpl, |
| 65 T callback) { |
| 66 CreateFunctionHandler( |
| 67 isolate, tmpl, base::Bind(callback), HolderIsFirstArgument); |
| 68 } |
53 }; | 69 }; |
54 | 70 |
55 // This specialization allows people to construct function templates directly if | 71 // This specialization allows people to construct function templates directly if |
56 // they need to do fancier stuff. | 72 // they need to do fancier stuff. |
57 template<> | 73 template<> |
58 struct GIN_EXPORT CallbackTraits<v8::Handle<v8::FunctionTemplate> > { | 74 struct GIN_EXPORT CallbackTraits<v8::Handle<v8::FunctionTemplate> > { |
59 static v8::Handle<v8::FunctionTemplate> CreateTemplate( | 75 static v8::Handle<v8::FunctionTemplate> CreateTemplate( |
60 v8::Handle<v8::FunctionTemplate> templ) { | 76 v8::Handle<v8::FunctionTemplate> templ) { |
61 return templ; | 77 return templ; |
62 } | 78 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 CallbackTraits<T>::CreateTemplate(isolate_, getter), | 112 CallbackTraits<T>::CreateTemplate(isolate_, getter), |
97 v8::Local<v8::FunctionTemplate>()); | 113 v8::Local<v8::FunctionTemplate>()); |
98 } | 114 } |
99 template<typename T, typename U> | 115 template<typename T, typename U> |
100 ObjectTemplateBuilder& SetProperty(const base::StringPiece& name, | 116 ObjectTemplateBuilder& SetProperty(const base::StringPiece& name, |
101 const T& getter, const U& setter) { | 117 const T& getter, const U& setter) { |
102 return SetPropertyImpl(name, | 118 return SetPropertyImpl(name, |
103 CallbackTraits<T>::CreateTemplate(isolate_, getter), | 119 CallbackTraits<T>::CreateTemplate(isolate_, getter), |
104 CallbackTraits<U>::CreateTemplate(isolate_, setter)); | 120 CallbackTraits<U>::CreateTemplate(isolate_, setter)); |
105 } | 121 } |
| 122 template<typename T> |
| 123 ObjectTemplateBuilder& SetCallAsFunctionHandler(const T& callback) { |
| 124 CallbackTraits<T>::SetAsFunctionHandler(isolate_, template_, callback); |
| 125 return *this; |
| 126 } |
106 | 127 |
107 v8::Local<v8::ObjectTemplate> Build(); | 128 v8::Local<v8::ObjectTemplate> Build(); |
108 | 129 |
109 private: | 130 private: |
110 ObjectTemplateBuilder& SetImpl(const base::StringPiece& name, | 131 ObjectTemplateBuilder& SetImpl(const base::StringPiece& name, |
111 v8::Handle<v8::Data> val); | 132 v8::Handle<v8::Data> val); |
112 ObjectTemplateBuilder& SetPropertyImpl( | 133 ObjectTemplateBuilder& SetPropertyImpl( |
113 const base::StringPiece& name, v8::Handle<v8::FunctionTemplate> getter, | 134 const base::StringPiece& name, v8::Handle<v8::FunctionTemplate> getter, |
114 v8::Handle<v8::FunctionTemplate> setter); | 135 v8::Handle<v8::FunctionTemplate> setter); |
115 | 136 |
116 v8::Isolate* isolate_; | 137 v8::Isolate* isolate_; |
117 | 138 |
118 // ObjectTemplateBuilder should only be used on the stack. | 139 // ObjectTemplateBuilder should only be used on the stack. |
119 v8::Local<v8::ObjectTemplate> template_; | 140 v8::Local<v8::ObjectTemplate> template_; |
120 }; | 141 }; |
121 | 142 |
122 } // namespace gin | 143 } // namespace gin |
123 | 144 |
124 #endif // GIN_OBJECT_TEMPLATE_BUILDER_H_ | 145 #endif // GIN_OBJECT_TEMPLATE_BUILDER_H_ |
OLD | NEW |