kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 1 | // Copyright 2014 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. |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 4 | |
[email protected] | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 5 | #ifndef GIN_FUNCTION_TEMPLATE_H_ |
| 6 | #define GIN_FUNCTION_TEMPLATE_H_ |
| 7 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 8 | #include "base/callback.h" |
| 9 | #include "base/logging.h" |
| 10 | #include "gin/arguments.h" |
| 11 | #include "gin/converter.h" |
[email protected] | 48c2163 | 2013-12-12 21:32:34 | [diff] [blame] | 12 | #include "gin/gin_export.h" |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 13 | #include "v8/include/v8.h" |
| 14 | |
| 15 | namespace gin { |
| 16 | |
| 17 | class PerIsolateData; |
| 18 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 19 | enum CreateFunctionTemplateFlags { |
| 20 | HolderIsFirstArgument = 1 << 0, |
| 21 | }; |
| 22 | |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 23 | namespace internal { |
| 24 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 25 | template<typename T> |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 26 | struct CallbackParamTraits { |
| 27 | typedef T LocalType; |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 28 | }; |
| 29 | template<typename T> |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 30 | struct CallbackParamTraits<const T&> { |
| 31 | typedef T LocalType; |
| 32 | }; |
| 33 | template<typename T> |
| 34 | struct CallbackParamTraits<const T*> { |
| 35 | typedef T* LocalType; |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 36 | }; |
| 37 | |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 38 | |
| 39 | // CallbackHolder and CallbackHolderBase are used to pass a base::Callback from |
| 40 | // CreateFunctionTemplate through v8 (via v8::FunctionTemplate) to |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 41 | // DispatchToCallback, where it is invoked. |
[email protected] | 6fe5610 | 2013-12-08 07:10:58 | [diff] [blame] | 42 | |
| 43 | // This simple base class is used so that we can share a single object template |
| 44 | // among every CallbackHolder instance. |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 45 | class GIN_EXPORT CallbackHolderBase { |
[email protected] | b4acaf8 | 2013-12-12 09:40:50 | [diff] [blame] | 46 | public: |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 47 | v8::Local<v8::External> GetHandle(v8::Isolate* isolate); |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 48 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 49 | protected: |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 50 | explicit CallbackHolderBase(v8::Isolate* isolate); |
| 51 | virtual ~CallbackHolderBase(); |
| 52 | |
| 53 | private: |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 54 | static void FirstWeakCallback( |
| 55 | const v8::WeakCallbackInfo<CallbackHolderBase>& data); |
| 56 | static void SecondWeakCallback( |
| 57 | const v8::WeakCallbackInfo<CallbackHolderBase>& data); |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 58 | |
dcarney | 99ade908 | 2015-04-22 09:55:42 | [diff] [blame] | 59 | v8::Global<v8::External> v8_ref_; |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 60 | |
| 61 | DISALLOW_COPY_AND_ASSIGN(CallbackHolderBase); |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 62 | }; |
| 63 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 64 | template<typename Sig> |
| 65 | class CallbackHolder : public CallbackHolderBase { |
| 66 | public: |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 67 | CallbackHolder(v8::Isolate* isolate, |
| 68 | const base::Callback<Sig>& callback, |
| 69 | int flags) |
| 70 | : CallbackHolderBase(isolate), callback(callback), flags(flags) {} |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 71 | base::Callback<Sig> callback; |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 72 | int flags; |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 73 | private: |
[email protected] | 695af7f5 | 2013-12-11 19:41:01 | [diff] [blame] | 74 | virtual ~CallbackHolder() {} |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 75 | |
| 76 | DISALLOW_COPY_AND_ASSIGN(CallbackHolder); |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 77 | }; |
| 78 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 79 | template<typename T> |
| 80 | bool GetNextArgument(Arguments* args, int create_flags, bool is_first, |
| 81 | T* result) { |
| 82 | if (is_first && (create_flags & HolderIsFirstArgument) != 0) { |
| 83 | return args->GetHolder(result); |
| 84 | } else { |
| 85 | return args->GetNext(result); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // For advanced use cases, we allow callers to request the unparsed Arguments |
| 90 | // object and poke around in it directly. |
| 91 | inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first, |
| 92 | Arguments* result) { |
| 93 | *result = *args; |
| 94 | return true; |
| 95 | } |
[email protected] | 5b971af | 2014-01-06 22:20:54 | [diff] [blame] | 96 | inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first, |
| 97 | Arguments** result) { |
| 98 | *result = args; |
| 99 | return true; |
| 100 | } |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 101 | |
[email protected] | 2491f14 | 2013-12-21 17:54:37 | [diff] [blame] | 102 | // It's common for clients to just need the isolate, so we make that easy. |
| 103 | inline bool GetNextArgument(Arguments* args, int create_flags, |
| 104 | bool is_first, v8::Isolate** result) { |
| 105 | *result = args->isolate(); |
| 106 | return true; |
| 107 | } |
| 108 | |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 109 | // Classes for generating and storing an argument pack of integer indices |
| 110 | // (based on well-known "indices trick", see: http://goo.gl/bKKojn): |
| 111 | template <size_t... indices> |
| 112 | struct IndicesHolder {}; |
| 113 | |
| 114 | template <size_t requested_index, size_t... indices> |
| 115 | struct IndicesGenerator { |
| 116 | using type = typename IndicesGenerator<requested_index - 1, |
| 117 | requested_index - 1, |
| 118 | indices...>::type; |
| 119 | }; |
| 120 | template <size_t... indices> |
| 121 | struct IndicesGenerator<0, indices...> { |
| 122 | using type = IndicesHolder<indices...>; |
| 123 | }; |
| 124 | |
| 125 | // Class template for extracting and storing single argument for callback |
| 126 | // at position |index|. |
| 127 | template <size_t index, typename ArgType> |
| 128 | struct ArgumentHolder { |
| 129 | using ArgLocalType = typename CallbackParamTraits<ArgType>::LocalType; |
| 130 | |
| 131 | ArgLocalType value; |
| 132 | bool ok; |
| 133 | |
| 134 | ArgumentHolder(Arguments* args, int create_flags) |
| 135 | : ok(GetNextArgument(args, create_flags, index == 0, &value)) { |
eseidel | 2584930f | 2014-12-15 22:06:44 | [diff] [blame] | 136 | if (!ok) { |
| 137 | // Ideally we would include the expected c++ type in the error |
| 138 | // message which we can access via typeid(ArgType).name() |
| 139 | // however we compile with no-rtti, which disables typeid. |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 140 | args->ThrowError(); |
eseidel | 2584930f | 2014-12-15 22:06:44 | [diff] [blame] | 141 | } |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 142 | } |
| 143 | }; |
| 144 | |
| 145 | // Class template for converting arguments from JavaScript to C++ and running |
| 146 | // the callback with them. |
| 147 | template <typename IndicesType, typename... ArgTypes> |
| 148 | class Invoker {}; |
| 149 | |
| 150 | template <size_t... indices, typename... ArgTypes> |
| 151 | class Invoker<IndicesHolder<indices...>, ArgTypes...> |
| 152 | : public ArgumentHolder<indices, ArgTypes>... { |
| 153 | public: |
| 154 | // Invoker<> inherits from ArgumentHolder<> for each argument. |
| 155 | // C++ has always been strict about the class initialization order, |
| 156 | // so it is guaranteed ArgumentHolders will be initialized (and thus, will |
| 157 | // extract arguments from Arguments) in the right order. |
| 158 | Invoker(Arguments* args, int create_flags) |
cmasone | a34136a | 2014-11-01 00:10:47 | [diff] [blame] | 159 | : ArgumentHolder<indices, ArgTypes>(args, create_flags)..., args_(args) { |
| 160 | // GCC thinks that create_flags is going unused, even though the |
| 161 | // expansion above clearly makes use of it. Per jyasskin@, casting |
| 162 | // to void is the commonly accepted way to convince the compiler |
| 163 | // that you're actually using a parameter/varible. |
| 164 | (void)create_flags; |
| 165 | } |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 166 | |
| 167 | bool IsOK() { |
| 168 | return And(ArgumentHolder<indices, ArgTypes>::ok...); |
| 169 | } |
| 170 | |
| 171 | template <typename ReturnType> |
| 172 | void DispatchToCallback(base::Callback<ReturnType(ArgTypes...)> callback) { |
| 173 | args_->Return(callback.Run(ArgumentHolder<indices, ArgTypes>::value...)); |
| 174 | } |
| 175 | |
| 176 | // In C++, you can declare the function foo(void), but you can't pass a void |
| 177 | // expression to foo. As a result, we must specialize the case of Callbacks |
| 178 | // that have the void return type. |
| 179 | void DispatchToCallback(base::Callback<void(ArgTypes...)> callback) { |
| 180 | callback.Run(ArgumentHolder<indices, ArgTypes>::value...); |
| 181 | } |
| 182 | |
| 183 | private: |
| 184 | static bool And() { return true; } |
| 185 | template <typename... T> |
| 186 | static bool And(bool arg1, T... args) { |
| 187 | return arg1 && And(args...); |
| 188 | } |
| 189 | |
| 190 | Arguments* args_; |
| 191 | }; |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 192 | |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 193 | // DispatchToCallback converts all the JavaScript arguments to C++ types and |
| 194 | // invokes the base::Callback. |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 195 | template <typename Sig> |
| 196 | struct Dispatcher {}; |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 197 | |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 198 | template <typename ReturnType, typename... ArgTypes> |
| 199 | struct Dispatcher<ReturnType(ArgTypes...)> { |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 200 | static void DispatchToCallback( |
| 201 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 202 | Arguments args(info); |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 203 | v8::Local<v8::External> v8_holder; |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 204 | CHECK(args.GetData(&v8_holder)); |
| 205 | CallbackHolderBase* holder_base = reinterpret_cast<CallbackHolderBase*>( |
| 206 | v8_holder->Value()); |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 207 | |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 208 | typedef CallbackHolder<ReturnType(ArgTypes...)> HolderT; |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 209 | HolderT* holder = static_cast<HolderT*>(holder_base); |
[email protected] | 37dacfa | 2013-11-26 03:31:04 | [diff] [blame] | 210 | |
kolczyk | 735c49b6 | 2014-10-24 13:06:04 | [diff] [blame] | 211 | using Indices = typename IndicesGenerator<sizeof...(ArgTypes)>::type; |
| 212 | Invoker<Indices, ArgTypes...> invoker(&args, holder->flags); |
| 213 | if (invoker.IsOK()) |
| 214 | invoker.DispatchToCallback(holder->callback); |
[email protected] | d73341d1 | 2013-12-21 00:48:46 | [diff] [blame] | 215 | } |
| 216 | }; |
| 217 | |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 218 | } // namespace internal |
| 219 | |
| 220 | |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 221 | // CreateFunctionTemplate creates a v8::FunctionTemplate that will create |
| 222 | // JavaScript functions that execute a provided C++ function or base::Callback. |
| 223 | // JavaScript arguments are automatically converted via gin::Converter, as is |
| 224 | // the return value of the C++ function, if any. |
mnaganov | 098ba6f | 2015-03-03 16:34:48 | [diff] [blame] | 225 | // |
| 226 | // NOTE: V8 caches FunctionTemplates for a lifetime of a web page for its own |
| 227 | // internal reasons, thus it is generally a good idea to cache the template |
| 228 | // returned by this function. Otherwise, repeated method invocations from JS |
| 229 | // will create substantial memory leaks. See http://crbug.com/463487. |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 230 | template<typename Sig> |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 231 | v8::Local<v8::FunctionTemplate> CreateFunctionTemplate( |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 232 | v8::Isolate* isolate, const base::Callback<Sig> callback, |
| 233 | int callback_flags = 0) { |
| 234 | typedef internal::CallbackHolder<Sig> HolderT; |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 235 | HolderT* holder = new HolderT(isolate, callback, callback_flags); |
| 236 | |
[email protected] | 81f8b91b | 2013-11-26 21:02:51 | [diff] [blame] | 237 | return v8::FunctionTemplate::New( |
[email protected] | 505ffde | 2013-12-19 15:47:13 | [diff] [blame] | 238 | isolate, |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 239 | &internal::Dispatcher<Sig>::DispatchToCallback, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 240 | ConvertToV8<v8::Local<v8::External> >(isolate, |
[email protected] | bf014290 | 2014-02-11 15:06:12 | [diff] [blame] | 241 | holder->GetHandle(isolate))); |
[email protected] | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 242 | } |
| 243 | |
[email protected] | 777183d0 | 2014-03-10 12:55:19 | [diff] [blame] | 244 | // CreateFunctionHandler installs a CallAsFunction handler on the given |
| 245 | // object template that forwards to a provided C++ function or base::Callback. |
| 246 | template<typename Sig> |
| 247 | void CreateFunctionHandler(v8::Isolate* isolate, |
| 248 | v8::Local<v8::ObjectTemplate> tmpl, |
| 249 | const base::Callback<Sig> callback, |
| 250 | int callback_flags = 0) { |
| 251 | typedef internal::CallbackHolder<Sig> HolderT; |
| 252 | HolderT* holder = new HolderT(isolate, callback, callback_flags); |
| 253 | tmpl->SetCallAsFunctionHandler(&internal::Dispatcher<Sig>::DispatchToCallback, |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 254 | ConvertToV8<v8::Local<v8::External> >( |
[email protected] | 777183d0 | 2014-03-10 12:55:19 | [diff] [blame] | 255 | isolate, holder->GetHandle(isolate))); |
| 256 | } |
| 257 | |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 258 | } // namespace gin |
[email protected] | b520e13 | 2013-11-29 03:21:48 | [diff] [blame] | 259 | |
| 260 | #endif // GIN_FUNCTION_TEMPLATE_H_ |