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.
4
5#include "mojo/bindings/js/support.h"
6
7#include "base/bind.h"
8#include "gin/arguments.h"
9#include "gin/converter.h"
10#include "gin/function_template.h"
11#include "gin/object_template_builder.h"
12#include "gin/per_isolate_data.h"
13#include "gin/public/wrapper_info.h"
14#include "gin/wrappable.h"
15#include "mojo/bindings/js/handle.h"
16#include "mojo/bindings/js/waiting_callback.h"
17#include "mojo/public/cpp/system/core.h"
18
19namespace mojo {
20namespace js {
21
22namespace {
23
24WaitingCallback* AsyncWait(const gin::Arguments& args,
25                           gin::Handle<gin::HandleWrapper> handle,
26                           MojoHandleSignals signals,
27                           v8::Handle<v8::Function> callback) {
28  return WaitingCallback::Create(args.isolate(), callback, handle, signals)
29             .get();
30}
31
32void CancelWait(WaitingCallback* waiting_callback) {
33  waiting_callback->Cancel();
34}
35
36gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin };
37
38}  // namespace
39
40const char Support::kModuleName[] = "mojo/public/js/bindings/support";
41
42v8::Local<v8::Value> Support::GetModule(v8::Isolate* isolate) {
43  gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
44  v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate(
45      &g_wrapper_info);
46
47  if (templ.IsEmpty()) {
48    templ = gin::ObjectTemplateBuilder(isolate)
49                .SetMethod("asyncWait", AsyncWait)
50                .SetMethod("cancelWait", CancelWait)
51                .Build();
52
53    data->SetObjectTemplate(&g_wrapper_info, templ);
54  }
55
56  return templ->NewInstance();
57}
58
59}  // namespace js
60}  // namespace mojo
61