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