| OLD | NEW |
| (Empty) |
| 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/public/bindings/js/waiting_callback.h" | |
| 6 | |
| 7 #include "gin/per_context_data.h" | |
| 8 #include "gin/per_isolate_data.h" | |
| 9 | |
| 10 namespace mojo { | |
| 11 namespace js { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 v8::Handle<v8::String> GetHiddenPropertyName(v8::Isolate* isolate) { | |
| 16 return gin::StringToSymbol(isolate, "::mojo::js::WaitingCallback"); | |
| 17 } | |
| 18 | |
| 19 } // namespace | |
| 20 | |
| 21 WaitingCallback::WaitingCallback(v8::Isolate* isolate, | |
| 22 v8::Handle<v8::Function> callback) | |
| 23 : wait_id_() { | |
| 24 v8::Handle<v8::Context> context = isolate->GetCurrentContext(); | |
| 25 runner_ = gin::PerContextData::From(context)->runner()->GetWeakPtr(); | |
| 26 GetWrapper(isolate)->SetHiddenValue(GetHiddenPropertyName(isolate), callback); | |
| 27 } | |
| 28 | |
| 29 WaitingCallback::~WaitingCallback() { | |
| 30 DCHECK(!wait_id_) << "Waiting callback was destroyed before being cancelled."; | |
| 31 } | |
| 32 | |
| 33 scoped_refptr<WaitingCallback> WaitingCallback::Create( | |
| 34 v8::Isolate* isolate, v8::Handle<v8::Function> callback) { | |
| 35 return make_scoped_refptr(new WaitingCallback(isolate, callback)); | |
| 36 } | |
| 37 | |
| 38 gin::WrapperInfo WaitingCallback::kWrapperInfo = { gin::kEmbedderNativeGin }; | |
| 39 | |
| 40 gin::WrapperInfo* WaitingCallback::GetWrapperInfo() { | |
| 41 return &kWrapperInfo; | |
| 42 } | |
| 43 | |
| 44 void WaitingCallback::EnsureRegistered(v8::Isolate* isolate) { | |
| 45 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); | |
| 46 if (!data->GetObjectTemplate(&kWrapperInfo).IsEmpty()) | |
| 47 return; | |
| 48 v8::Handle<v8::ObjectTemplate> templ = v8::ObjectTemplate::New(isolate); | |
| 49 templ->SetInternalFieldCount(gin::kNumberOfInternalFields); | |
| 50 data->SetObjectTemplate(&kWrapperInfo, templ); | |
| 51 } | |
| 52 | |
| 53 void WaitingCallback::OnHandleReady(MojoResult result) { | |
| 54 wait_id_ = NULL; | |
| 55 | |
| 56 if (!runner_) | |
| 57 return; | |
| 58 | |
| 59 gin::Runner::Scope scope(runner_.get()); | |
| 60 v8::Isolate* isolate = runner_->isolate(); | |
| 61 | |
| 62 v8::Handle<v8::Value> hidden_value = | |
| 63 GetWrapper(isolate)->GetHiddenValue(GetHiddenPropertyName(isolate)); | |
| 64 v8::Handle<v8::Function> callback; | |
| 65 CHECK(gin::ConvertFromV8(isolate, hidden_value, &callback)); | |
| 66 | |
| 67 v8::Handle<v8::Value> args[] = { gin::ConvertToV8(isolate, result) }; | |
| 68 runner_->Call(callback, runner_->global(), 1, args); | |
| 69 } | |
| 70 | |
| 71 } // namespace js | |
| 72 } // namespace mojo | |
| OLD | NEW |