// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "base/callback.h" #include "base/logging.h" #include "gin/arguments.h" #include "gin/converter.h" #include "gin/public/gin_embedders.h" #include "gin/public/wrapper_info.h" #include "gin/wrappable.h" #include "v8/include/v8.h" namespace gin { class PerIsolateData; template struct RemoveConstRef { typedef T Type; }; template struct RemoveConstRef { typedef T Type; }; class CallbackHolderBase : public Wrappable { public: static void EnsureRegistered(PerIsolateData* isolate_data); virtual WrapperInfo* GetWrapperInfo() OVERRIDE; static WrapperInfo kWrapperInfo; protected: virtual ~CallbackHolderBase() {} }; template<> struct Converter : public WrappableConverter {}; template class CallbackHolder : public CallbackHolderBase { public: CallbackHolder(const base::Callback& callback) : callback(callback) {} base::Callback callback; private: virtual ~CallbackHolder() {} }; // TODO(aa): Generate the overloads below with pump. template static void DispatchToCallback( const v8::FunctionCallbackInfo& info) { Arguments args(info); CallbackHolderBase* holder_base = NULL; CHECK(args.GetData(&holder_base)); typedef CallbackHolder HolderT; HolderT* holder = static_cast(holder_base); typename RemoveConstRef::Type a1; typename RemoveConstRef::Type a2; if (!args.GetNext(&a1) || !args.GetNext(&a2)) { args.ThrowError(); return; } holder->callback.Run(a1, a2); } template v8::Local CreateFunctionTempate( v8::Isolate* isolate, const base::Callback& callback) { typedef CallbackHolder HolderT; scoped_refptr holder(new HolderT(callback)); return v8::FunctionTemplate::New( &DispatchToCallback, ConvertToV8(isolate, holder.get())); } } // namespace gin