OLD | NEW |
---|---|
(Empty) | |
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 #include "base/callback.h" | |
6 #include "base/logging.h" | |
7 #include "gin/arguments.h" | |
8 #include "gin/converter.h" | |
9 #include "gin/public/gin_embedders.h" | |
10 #include "gin/public/wrapper_info.h" | |
11 #include "gin/wrappable.h" | |
12 | |
abarth-chromium
2013/11/23 00:39:05
extra blank line here.
| |
13 #include "v8/include/v8.h" | |
14 | |
15 namespace gin { | |
16 | |
17 class PerIsolateData; | |
18 | |
19 template<typename T> | |
20 struct RemoveConstRef { | |
21 typedef T Type; | |
22 }; | |
23 template<typename T> | |
24 struct RemoveConstRef<const T&> { | |
25 typedef T Type; | |
26 }; | |
27 | |
28 class CallbackHolderBase : public Wrappable { | |
29 public: | |
30 static void EnsureRegistered(PerIsolateData* isolate_data); | |
31 virtual WrapperInfo* GetWrapperInfo() OVERRIDE; | |
32 static WrapperInfo kWrapperInfo; | |
33 protected: | |
34 virtual ~CallbackHolderBase() {} | |
35 }; | |
36 | |
37 template<> | |
38 struct Converter<CallbackHolderBase*> | |
39 : public WrappableConverter<CallbackHolderBase> {}; | |
40 | |
41 template<typename Sig> | |
42 class CallbackHolder : public CallbackHolderBase { | |
43 public: | |
44 CallbackHolder(const base::Callback<Sig>& callback) | |
45 : callback(callback) {} | |
46 base::Callback<Sig> callback; | |
47 private: | |
48 virtual ~CallbackHolder() {} | |
49 }; | |
50 | |
51 // TODO(aa): Generate the overloads below with pump. | |
52 | |
53 template<typename P1, typename P2> | |
54 static void DispatchToCallback( | |
55 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
56 Arguments args(info); | |
57 CallbackHolderBase* holder_base = NULL; | |
58 CHECK(args.GetData(&holder_base)); | |
59 | |
60 typedef CallbackHolder<void(P1, P2)> HolderT; | |
61 HolderT* holder = static_cast<HolderT*>(holder_base); | |
62 | |
63 typename RemoveConstRef<P1>::Type a1; | |
64 typename RemoveConstRef<P2>::Type a2; | |
65 if (!args.GetNext(&a1) || | |
66 !args.GetNext(&a2)) { | |
67 args.ThrowError(); | |
68 return; | |
69 } | |
70 | |
71 holder->callback.Run(a1, a2); | |
72 } | |
73 | |
74 template<typename P1, typename P2> | |
75 v8::Local<v8::FunctionTemplate> CreateFunctionTempate( | |
76 v8::Isolate* isolate, | |
77 const base::Callback<void(P1, P2)>& callback) { | |
78 typedef CallbackHolder<void(P1, P2)> HolderT; | |
79 scoped_refptr<HolderT> holder(new HolderT(callback)); | |
80 return v8::FunctionTemplate::New( | |
81 &DispatchToCallback<P1, P2>, | |
82 ConvertToV8<CallbackHolderBase*>(isolate, holder.get())); | |
83 } | |
84 | |
85 } // namespace gin | |
OLD | NEW |