[email protected] | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef GIN_OBJECT_TEMPLATE_BUILDER_H_ |
| 6 | #define GIN_OBJECT_TEMPLATE_BUILDER_H_ |
| 7 | |
| 8 | #include "base/bind.h" |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 9 | #include "base/callback.h" |
[email protected] | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 10 | #include "base/strings/string_piece.h" |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 11 | #include "base/template_util.h" |
[email protected] | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 12 | #include "gin/converter.h" |
| 13 | #include "gin/function_template.h" |
| 14 | #include "v8/include/v8.h" |
| 15 | |
| 16 | namespace gin { |
| 17 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | // Base template - used only for non-member function pointers. Other types |
| 21 | // either go to one of the below specializations, or go here and fail to compile |
| 22 | // because of base::Bind(). |
| 23 | template<typename T, typename Enable = void> |
| 24 | struct CallbackTraits { |
| 25 | static v8::Handle<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate, |
| 26 | T callback) { |
| 27 | return CreateFunctionTemplate(isolate, base::Bind(callback)); |
| 28 | } |
| 29 | }; |
| 30 | |
| 31 | // Specialization for base::Callback. |
| 32 | template<typename T> |
| 33 | struct CallbackTraits<base::Callback<T> > { |
| 34 | static v8::Handle<v8::FunctionTemplate> CreateTemplate( |
| 35 | v8::Isolate* isolate, const base::Callback<T>& callback) { |
| 36 | return CreateFunctionTemplate(isolate, callback); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | // Specialization for member function pointers. We need to handle this case |
| 41 | // specially because the first parameter for callbacks to MFP should typically |
| 42 | // come from the the JavaScript "this" object the function was called on, not |
| 43 | // from the first normal parameter. |
| 44 | template<typename T> |
| 45 | struct CallbackTraits<T, typename base::enable_if< |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 46 | base::is_member_function_pointer<T>::value>::type> { |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 47 | static v8::Handle<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate, |
| 48 | T callback) { |
| 49 | return CreateFunctionTemplate(isolate, base::Bind(callback), |
| 50 | HolderIsFirstArgument); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | // This specialization allows people to construct function templates directly if |
| 55 | // they need to do fancier stuff. |
| 56 | template<> |
| 57 | struct CallbackTraits<v8::Handle<v8::FunctionTemplate> > { |
| 58 | static v8::Handle<v8::FunctionTemplate> CreateTemplate( |
| 59 | v8::Handle<v8::FunctionTemplate> templ) { |
| 60 | return templ; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | } // namespace |
| 65 | |
| 66 | |
[email protected] | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 67 | // ObjectTemplateBuilder provides a handy interface to creating |
| 68 | // v8::ObjectTemplate instances with various sorts of properties. |
| 69 | class ObjectTemplateBuilder { |
| 70 | public: |
| 71 | explicit ObjectTemplateBuilder(v8::Isolate* isolate); |
| 72 | ~ObjectTemplateBuilder(); |
| 73 | |
| 74 | // It's against Google C++ style to return a non-const ref, but we take some |
| 75 | // poetic license here in order that all calls to Set() can be via the '.' |
| 76 | // operator and line up nicely. |
| 77 | template<typename T> |
| 78 | ObjectTemplateBuilder& SetValue(const base::StringPiece& name, T val) { |
| 79 | return SetImpl(name, ConvertToV8(isolate_, val)); |
| 80 | } |
| 81 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 82 | // In the following methods, T and U can be function pointer, member function |
| 83 | // pointer, base::Callback, or v8::FunctionTemplate. Most clients will want to |
| 84 | // use one of the first two options. Also see gin::CreateFunctionTemplate() |
| 85 | // for creating raw function templates. |
[email protected] | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 86 | template<typename T> |
| 87 | ObjectTemplateBuilder& SetMethod(const base::StringPiece& name, |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 88 | const T& callback) { |
| 89 | return SetImpl(name, CallbackTraits<T>::CreateTemplate(isolate_, callback)); |
| 90 | } |
| 91 | template<typename T> |
| 92 | ObjectTemplateBuilder& SetProperty(const base::StringPiece& name, |
| 93 | const T& getter) { |
| 94 | return SetPropertyImpl(name, |
| 95 | CallbackTraits<T>::CreateTemplate(isolate_, getter), |
| 96 | v8::Local<v8::FunctionTemplate>()); |
| 97 | } |
| 98 | template<typename T, typename U> |
| 99 | ObjectTemplateBuilder& SetProperty(const base::StringPiece& name, |
| 100 | const T& getter, const U& setter) { |
| 101 | return SetPropertyImpl(name, |
| 102 | CallbackTraits<T>::CreateTemplate(isolate_, getter), |
| 103 | CallbackTraits<U>::CreateTemplate(isolate_, setter)); |
[email protected] | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | v8::Local<v8::ObjectTemplate> Build(); |
| 107 | |
| 108 | private: |
| 109 | ObjectTemplateBuilder& SetImpl(const base::StringPiece& name, |
| 110 | v8::Handle<v8::Data> val); |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 111 | ObjectTemplateBuilder& SetPropertyImpl( |
| 112 | const base::StringPiece& name, v8::Handle<v8::FunctionTemplate> getter, |
| 113 | v8::Handle<v8::FunctionTemplate> setter); |
[email protected] | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 114 | |
| 115 | v8::Isolate* isolate_; |
| 116 | |
| 117 | // ObjectTemplateBuilder should only be used on the stack. |
| 118 | v8::Local<v8::ObjectTemplate> template_; |
| 119 | }; |
| 120 | |
| 121 | } // namespace gin |
| 122 | |
| 123 | #endif // GIN_OBJECT_TEMPLATE_BUILDER_H_ |