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/edk/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/edk/js/handle.h"
16 #include "mojo/edk/js/waiting_callback.h"
17 #include "mojo/public/cpp/system/core.h"
18
19 namespace mojo {
20 namespace edk {
21 namespace js {
22
23 namespace {
24
AsyncWait(const gin::Arguments & args,gin::Handle<HandleWrapper> handle,MojoHandleSignals signals,v8::Handle<v8::Function> callback)25 WaitingCallback* AsyncWait(const gin::Arguments& args,
26 gin::Handle<HandleWrapper> handle,
27 MojoHandleSignals signals,
28 v8::Handle<v8::Function> callback) {
29 return WaitingCallback::Create(
30 args.isolate(), callback, handle, signals, true /* one_shot */).get();
31 }
32
CancelWait(WaitingCallback * waiting_callback)33 void CancelWait(WaitingCallback* waiting_callback) {
34 waiting_callback->Cancel();
35 }
36
Watch(const gin::Arguments & args,gin::Handle<HandleWrapper> handle,MojoHandleSignals signals,v8::Handle<v8::Function> callback)37 WaitingCallback* Watch(const gin::Arguments& args,
38 gin::Handle<HandleWrapper> handle,
39 MojoHandleSignals signals,
40 v8::Handle<v8::Function> callback) {
41 return WaitingCallback::Create(
42 args.isolate(), callback, handle, signals, false /* one_shot */).get();
43 }
44
CancelWatch(WaitingCallback * waiting_callback)45 void CancelWatch(WaitingCallback* waiting_callback) {
46 waiting_callback->Cancel();
47 }
48
49 gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin };
50
51 } // namespace
52
53 const char Support::kModuleName[] = "mojo/public/js/support";
54
GetModule(v8::Isolate * isolate)55 v8::Local<v8::Value> Support::GetModule(v8::Isolate* isolate) {
56 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
57 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate(
58 &g_wrapper_info);
59
60 if (templ.IsEmpty()) {
61 templ = gin::ObjectTemplateBuilder(isolate)
62 // TODO(rockot): Remove asyncWait and cancelWait.
63 .SetMethod("asyncWait", AsyncWait)
64 .SetMethod("cancelWait", CancelWait)
65 .SetMethod("watch", Watch)
66 .SetMethod("cancelWatch", CancelWatch)
67 .Build();
68
69 data->SetObjectTemplate(&g_wrapper_info, templ);
70 }
71
72 return templ->NewInstance();
73 }
74
75 } // namespace js
76 } // namespace edk
77 } // namespace mojo
78