OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/bindings/js/support.h" | 5 #include "mojo/bindings/js/support.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "gin/arguments.h" | 8 #include "gin/arguments.h" |
9 #include "gin/converter.h" | 9 #include "gin/converter.h" |
10 #include "gin/function_template.h" | 10 #include "gin/function_template.h" |
11 #include "gin/object_template_builder.h" | 11 #include "gin/object_template_builder.h" |
12 #include "gin/per_isolate_data.h" | 12 #include "gin/per_isolate_data.h" |
13 #include "gin/public/wrapper_info.h" | 13 #include "gin/public/wrapper_info.h" |
14 #include "gin/wrappable.h" | 14 #include "gin/wrappable.h" |
15 #include "mojo/bindings/js/handle.h" | 15 #include "mojo/bindings/js/handle.h" |
16 #include "mojo/bindings/js/waiting_callback.h" | 16 #include "mojo/bindings/js/waiting_callback.h" |
17 #include "mojo/public/cpp/environment/default_async_waiter.h" | 17 #include "mojo/public/cpp/environment/default_async_waiter.h" |
18 #include "mojo/public/cpp/system/core.h" | 18 #include "mojo/public/cpp/system/core.h" |
19 | 19 |
20 namespace mojo { | 20 namespace mojo { |
21 namespace js { | 21 namespace js { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 WaitingCallback* AsyncWait(const gin::Arguments& args, mojo::Handle handle, | 25 WaitingCallback* AsyncWait(const gin::Arguments& args, mojo::Handle handle, |
26 MojoWaitFlags flags, | 26 MojoWaitFlags flags, |
27 v8::Handle<v8::Function> callback) { | 27 v8::Handle<v8::Function> callback) { |
28 gin::Handle<WaitingCallback> waiting_callback = | 28 return WaitingCallback::Create(args.isolate(), callback, handle, flags).get(); |
29 WaitingCallback::Create(args.isolate(), callback); | |
30 | |
31 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter(); | |
32 MojoAsyncWaitID wait_id = waiter->AsyncWait( | |
33 waiter, | |
34 handle.value(), | |
35 flags, | |
36 MOJO_DEADLINE_INDEFINITE, | |
37 &WaitingCallback::CallOnHandleReady, | |
38 waiting_callback.get()); | |
39 | |
40 waiting_callback->set_wait_id(wait_id); | |
41 | |
42 return waiting_callback.get(); | |
43 } | 29 } |
44 | 30 |
45 void CancelWait(WaitingCallback* waiting_callback) { | 31 void CancelWait(WaitingCallback* waiting_callback) { |
46 if (!waiting_callback->wait_id()) | 32 waiting_callback->Cancel(); |
47 return; | |
48 | |
49 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter(); | |
50 waiter->CancelWait(waiter, waiting_callback->wait_id()); | |
51 waiting_callback->set_wait_id(0); | |
52 } | 33 } |
53 | 34 |
54 gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin }; | 35 gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin }; |
55 | 36 |
56 } // namespace | 37 } // namespace |
57 | 38 |
58 const char Support::kModuleName[] = "mojo/public/js/bindings/support"; | 39 const char Support::kModuleName[] = "mojo/public/js/bindings/support"; |
59 | 40 |
60 v8::Local<v8::Value> Support::GetModule(v8::Isolate* isolate) { | 41 v8::Local<v8::Value> Support::GetModule(v8::Isolate* isolate) { |
61 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); | 42 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); |
62 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate( | 43 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate( |
63 &g_wrapper_info); | 44 &g_wrapper_info); |
64 | 45 |
65 if (templ.IsEmpty()) { | 46 if (templ.IsEmpty()) { |
66 templ = gin::ObjectTemplateBuilder(isolate) | 47 templ = gin::ObjectTemplateBuilder(isolate) |
67 .SetMethod("asyncWait", AsyncWait) | 48 .SetMethod("asyncWait", AsyncWait) |
68 .SetMethod("cancelWait", CancelWait) | 49 .SetMethod("cancelWait", CancelWait) |
69 .Build(); | 50 .Build(); |
70 | 51 |
71 data->SetObjectTemplate(&g_wrapper_info, templ); | 52 data->SetObjectTemplate(&g_wrapper_info, templ); |
72 } | 53 } |
73 | 54 |
74 return templ->NewInstance(); | 55 return templ->NewInstance(); |
75 } | 56 } |
76 | 57 |
77 } // namespace js | 58 } // namespace js |
78 } // namespace mojo | 59 } // namespace mojo |
OLD | NEW |