diff options
author | Andras Becsi <andras.becsi@digia.com> | 2014-03-18 13:16:26 +0100 |
---|---|---|
committer | Frederik Gladhorn <frederik.gladhorn@digia.com> | 2014-03-20 15:55:39 +0100 |
commit | 3f0f86b0caed75241fa71c95a5d73bc0164348c5 (patch) | |
tree | 92b9fb00f2e9e90b0be2262093876d4f43b6cd13 /chromium/gin/object_template_builder.h | |
parent | e90d7c4b152c56919d963987e2503f9909a666d2 (diff) | |
download | qtwebengine-chromium-3f0f86b0caed75241fa71c95a5d73bc0164348c5.tar.gz |
Update to new stable branch 1750
This also includes an updated ninja and chromium dependencies
needed on Windows.
Change-Id: Icd597d80ed3fa4425933c9f1334c3c2e31291c42
Reviewed-by: Zoltan Arvai <zarvai@inf.u-szeged.hu>
Reviewed-by: Zeno Albisser <zeno.albisser@digia.com>
Diffstat (limited to 'chromium/gin/object_template_builder.h')
-rw-r--r-- | chromium/gin/object_template_builder.h | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/chromium/gin/object_template_builder.h b/chromium/gin/object_template_builder.h new file mode 100644 index 00000000000..6367b71245c --- /dev/null +++ b/chromium/gin/object_template_builder.h @@ -0,0 +1,124 @@ +// 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. + +#ifndef GIN_OBJECT_TEMPLATE_BUILDER_H_ +#define GIN_OBJECT_TEMPLATE_BUILDER_H_ + +#include "base/bind.h" +#include "base/callback.h" +#include "base/strings/string_piece.h" +#include "base/template_util.h" +#include "gin/converter.h" +#include "gin/function_template.h" +#include "gin/gin_export.h" +#include "v8/include/v8.h" + +namespace gin { + +namespace { + +// Base template - used only for non-member function pointers. Other types +// either go to one of the below specializations, or go here and fail to compile +// because of base::Bind(). +template<typename T, typename Enable = void> +struct CallbackTraits { + static v8::Handle<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate, + T callback) { + return CreateFunctionTemplate(isolate, base::Bind(callback)); + } +}; + +// Specialization for base::Callback. +template<typename T> +struct CallbackTraits<base::Callback<T> > { + static v8::Handle<v8::FunctionTemplate> CreateTemplate( + v8::Isolate* isolate, const base::Callback<T>& callback) { + return CreateFunctionTemplate(isolate, callback); + } +}; + +// Specialization for member function pointers. We need to handle this case +// specially because the first parameter for callbacks to MFP should typically +// come from the the JavaScript "this" object the function was called on, not +// from the first normal parameter. +template<typename T> +struct CallbackTraits<T, typename base::enable_if< + base::is_member_function_pointer<T>::value>::type> { + static v8::Handle<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate, + T callback) { + return CreateFunctionTemplate(isolate, base::Bind(callback), + HolderIsFirstArgument); + } +}; + +// This specialization allows people to construct function templates directly if +// they need to do fancier stuff. +template<> +struct GIN_EXPORT CallbackTraits<v8::Handle<v8::FunctionTemplate> > { + static v8::Handle<v8::FunctionTemplate> CreateTemplate( + v8::Handle<v8::FunctionTemplate> templ) { + return templ; + } +}; + +} // namespace + + +// ObjectTemplateBuilder provides a handy interface to creating +// v8::ObjectTemplate instances with various sorts of properties. +class GIN_EXPORT ObjectTemplateBuilder { + public: + explicit ObjectTemplateBuilder(v8::Isolate* isolate); + ~ObjectTemplateBuilder(); + + // It's against Google C++ style to return a non-const ref, but we take some + // poetic license here in order that all calls to Set() can be via the '.' + // operator and line up nicely. + template<typename T> + ObjectTemplateBuilder& SetValue(const base::StringPiece& name, T val) { + return SetImpl(name, ConvertToV8(isolate_, val)); + } + + // In the following methods, T and U can be function pointer, member function + // pointer, base::Callback, or v8::FunctionTemplate. Most clients will want to + // use one of the first two options. Also see gin::CreateFunctionTemplate() + // for creating raw function templates. + template<typename T> + ObjectTemplateBuilder& SetMethod(const base::StringPiece& name, + const T& callback) { + return SetImpl(name, CallbackTraits<T>::CreateTemplate(isolate_, callback)); + } + template<typename T> + ObjectTemplateBuilder& SetProperty(const base::StringPiece& name, + const T& getter) { + return SetPropertyImpl(name, + CallbackTraits<T>::CreateTemplate(isolate_, getter), + v8::Local<v8::FunctionTemplate>()); + } + template<typename T, typename U> + ObjectTemplateBuilder& SetProperty(const base::StringPiece& name, + const T& getter, const U& setter) { + return SetPropertyImpl(name, + CallbackTraits<T>::CreateTemplate(isolate_, getter), + CallbackTraits<U>::CreateTemplate(isolate_, setter)); + } + + v8::Local<v8::ObjectTemplate> Build(); + + private: + ObjectTemplateBuilder& SetImpl(const base::StringPiece& name, + v8::Handle<v8::Data> val); + ObjectTemplateBuilder& SetPropertyImpl( + const base::StringPiece& name, v8::Handle<v8::FunctionTemplate> getter, + v8::Handle<v8::FunctionTemplate> setter); + + v8::Isolate* isolate_; + + // ObjectTemplateBuilder should only be used on the stack. + v8::Local<v8::ObjectTemplate> template_; +}; + +} // namespace gin + +#endif // GIN_OBJECT_TEMPLATE_BUILDER_H_ |