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