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 3 // found in the LICENSE file. 4 5 #include "mojo/apps/js/bindings/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/apps/js/bindings/handle.h" 16 #include "mojo/apps/js/bindings/waiting_callback.h" 17 #include "mojo/public/bindings/lib/bindings_support.h" 18 19 namespace mojo { 20 namespace js { 21 22 namespace { 23 24 WaitingCallback* AsyncWait(const gin::Arguments& args, mojo::Handle handle, 25 MojoWaitFlags flags, 26 v8::Handle<v8::Function> callback) { 27 gin::Handle<WaitingCallback> waiting_callback = 28 WaitingCallback::Create(args.isolate(), callback); 29 30 BindingsSupport::AsyncWaitID wait_id = BindingsSupport::Get()->AsyncWait( 31 handle, flags, waiting_callback.get()); 32 33 waiting_callback->set_wait_id(wait_id); 34 35 return waiting_callback.get(); 36 } 37 38 void CancelWait(WaitingCallback* waiting_callback) { 39 if (!waiting_callback->wait_id()) 40 return; 41 BindingsSupport::Get()->CancelWait(waiting_callback->wait_id()); 42 waiting_callback->set_wait_id(NULL); 43 } 44 45 gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin }; 46 47 } // namespace 48 49 const char Support::kModuleName[] = "mojo/apps/js/bindings/support"; 50 51 v8::Local<v8::ObjectTemplate> Support::GetTemplate(v8::Isolate* isolate) { 52 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); 53 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate( 54 &g_wrapper_info); 55 56 if (templ.IsEmpty()) { 57 WaitingCallback::EnsureRegistered(isolate); 58 59 templ = gin::ObjectTemplateBuilder(isolate) 60 .SetMethod("asyncWait", AsyncWait) 61 .SetMethod("cancelWait", CancelWait) 62 .Build(); 63 64 data->SetObjectTemplate(&g_wrapper_info, templ); 65 } 66 67 return templ; 68 } 69 70 } // namespace js 71 } // namespace mojo 72