| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/apps/js/bindings/support.h" | 5 #include "mojo/apps/js/bindings/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/apps/js/bindings/handle.h" | 15 #include "mojo/apps/js/bindings/handle.h" |
| 16 #include "mojo/apps/js/bindings/waiting_callback.h" | 16 #include "mojo/apps/js/bindings/waiting_callback.h" |
| 17 #include "mojo/public/bindings/lib/bindings_support.h" | 17 #include "mojo/public/environment/default_async_waiter.h" |
| 18 #include "mojo/public/system/core_cpp.h" |
| 18 | 19 |
| 19 namespace mojo { | 20 namespace mojo { |
| 20 namespace js { | 21 namespace js { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 WaitingCallback* AsyncWait(const gin::Arguments& args, mojo::Handle handle, | 25 WaitingCallback* AsyncWait(const gin::Arguments& args, mojo::Handle handle, |
| 25 MojoWaitFlags flags, | 26 MojoWaitFlags flags, |
| 26 v8::Handle<v8::Function> callback) { | 27 v8::Handle<v8::Function> callback) { |
| 27 gin::Handle<WaitingCallback> waiting_callback = | 28 gin::Handle<WaitingCallback> waiting_callback = |
| 28 WaitingCallback::Create(args.isolate(), callback); | 29 WaitingCallback::Create(args.isolate(), callback); |
| 29 | 30 |
| 30 BindingsSupport::AsyncWaitID wait_id = BindingsSupport::Get()->AsyncWait( | 31 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter(); |
| 31 handle, flags, waiting_callback.get()); | 32 MojoAsyncWaitID wait_id = waiter->AsyncWait( |
| 33 waiter, |
| 34 handle.value(), |
| 35 flags, |
| 36 MOJO_DEADLINE_INDEFINITE, |
| 37 &WaitingCallback::CallOnHandleReady, |
| 38 waiting_callback.get()); |
| 32 | 39 |
| 33 waiting_callback->set_wait_id(wait_id); | 40 waiting_callback->set_wait_id(wait_id); |
| 34 | 41 |
| 35 return waiting_callback.get(); | 42 return waiting_callback.get(); |
| 36 } | 43 } |
| 37 | 44 |
| 38 void CancelWait(WaitingCallback* waiting_callback) { | 45 void CancelWait(WaitingCallback* waiting_callback) { |
| 39 if (!waiting_callback->wait_id()) | 46 if (!waiting_callback->wait_id()) |
| 40 return; | 47 return; |
| 41 BindingsSupport::Get()->CancelWait(waiting_callback->wait_id()); | 48 |
| 42 waiting_callback->set_wait_id(NULL); | 49 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter(); |
| 50 waiter->CancelWait(waiter, waiting_callback->wait_id()); |
| 51 waiting_callback->set_wait_id(0); |
| 43 } | 52 } |
| 44 | 53 |
| 45 gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin }; | 54 gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin }; |
| 46 | 55 |
| 47 } // namespace | 56 } // namespace |
| 48 | 57 |
| 49 const char Support::kModuleName[] = "mojo/apps/js/bindings/support"; | 58 const char Support::kModuleName[] = "mojo/apps/js/bindings/support"; |
| 50 | 59 |
| 51 v8::Local<v8::Value> Support::GetModule(v8::Isolate* isolate) { | 60 v8::Local<v8::Value> Support::GetModule(v8::Isolate* isolate) { |
| 52 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); | 61 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); |
| 53 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate( | 62 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate( |
| 54 &g_wrapper_info); | 63 &g_wrapper_info); |
| 55 | 64 |
| 56 if (templ.IsEmpty()) { | 65 if (templ.IsEmpty()) { |
| 57 templ = gin::ObjectTemplateBuilder(isolate) | 66 templ = gin::ObjectTemplateBuilder(isolate) |
| 58 .SetMethod("asyncWait", AsyncWait) | 67 .SetMethod("asyncWait", AsyncWait) |
| 59 .SetMethod("cancelWait", CancelWait) | 68 .SetMethod("cancelWait", CancelWait) |
| 60 .Build(); | 69 .Build(); |
| 61 | 70 |
| 62 data->SetObjectTemplate(&g_wrapper_info, templ); | 71 data->SetObjectTemplate(&g_wrapper_info, templ); |
| 63 } | 72 } |
| 64 | 73 |
| 65 return templ->NewInstance(); | 74 return templ->NewInstance(); |
| 66 } | 75 } |
| 67 | 76 |
| 68 } // namespace js | 77 } // namespace js |
| 69 } // namespace mojo | 78 } // namespace mojo |
| OLD | NEW |