| 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/edk/js/waiting_callback.h" | 5 #include "mojo/edk/js/waiting_callback.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "gin/per_context_data.h" | 9 #include "gin/per_context_data.h" |
| 10 #include "mojo/public/cpp/environment/environment.h" | |
| 11 | 10 |
| 12 namespace mojo { | 11 namespace mojo { |
| 13 namespace edk { | 12 namespace edk { |
| 14 namespace js { | 13 namespace js { |
| 15 | 14 |
| 16 namespace { | 15 namespace { |
| 17 | 16 |
| 18 v8::Handle<v8::Private> GetHiddenPropertyName(v8::Isolate* isolate) { | 17 v8::Handle<v8::Private> GetHiddenPropertyName(v8::Isolate* isolate) { |
| 19 return v8::Private::ForApi( | 18 return v8::Private::ForApi( |
| 20 isolate, gin::StringToV8(isolate, "::mojo::js::WaitingCallback")); | 19 isolate, gin::StringToV8(isolate, "::mojo::js::WaitingCallback")); |
| 21 } | 20 } |
| 22 | 21 |
| 23 } // namespace | 22 } // namespace |
| 24 | 23 |
| 25 gin::WrapperInfo WaitingCallback::kWrapperInfo = { gin::kEmbedderNativeGin }; | 24 gin::WrapperInfo WaitingCallback::kWrapperInfo = { gin::kEmbedderNativeGin }; |
| 26 | 25 |
| 27 // static | 26 // static |
| 28 gin::Handle<WaitingCallback> WaitingCallback::Create( | 27 gin::Handle<WaitingCallback> WaitingCallback::Create( |
| 29 v8::Isolate* isolate, | 28 v8::Isolate* isolate, |
| 30 v8::Handle<v8::Function> callback, | 29 v8::Handle<v8::Function> callback, |
| 31 gin::Handle<HandleWrapper> handle_wrapper, | 30 gin::Handle<HandleWrapper> handle_wrapper, |
| 32 MojoHandleSignals signals) { | 31 MojoHandleSignals signals) { |
| 33 gin::Handle<WaitingCallback> waiting_callback = gin::CreateHandle( | 32 gin::Handle<WaitingCallback> waiting_callback = gin::CreateHandle( |
| 34 isolate, new WaitingCallback(isolate, callback, handle_wrapper)); | 33 isolate, new WaitingCallback(isolate, callback, handle_wrapper)); |
| 35 waiting_callback->wait_id_ = Environment::GetDefaultAsyncWaiter()->AsyncWait( | 34 |
| 36 handle_wrapper->get().value(), | 35 waiting_callback->handle_watcher_.Start( |
| 37 signals, | 36 handle_wrapper->get(), signals, MOJO_DEADLINE_INDEFINITE, |
| 38 MOJO_DEADLINE_INDEFINITE, | 37 base::Bind(&WaitingCallback::OnHandleReady, |
| 39 &WaitingCallback::CallOnHandleReady, | 38 base::Unretained(waiting_callback.get()))); |
| 40 waiting_callback.get()); | |
| 41 return waiting_callback; | 39 return waiting_callback; |
| 42 } | 40 } |
| 43 | 41 |
| 44 void WaitingCallback::Cancel() { | 42 void WaitingCallback::Cancel() { |
| 45 if (!wait_id_) | 43 if (!handle_watcher_.is_watching()) |
| 46 return; | 44 return; |
| 47 | 45 |
| 48 handle_wrapper_->RemoveCloseObserver(this); | 46 RemoveHandleCloseObserver(); |
| 49 handle_wrapper_ = NULL; | 47 handle_watcher_.Stop(); |
| 50 Environment::GetDefaultAsyncWaiter()->CancelWait(wait_id_); | |
| 51 wait_id_ = 0; | |
| 52 } | 48 } |
| 53 | 49 |
| 54 WaitingCallback::WaitingCallback(v8::Isolate* isolate, | 50 WaitingCallback::WaitingCallback(v8::Isolate* isolate, |
| 55 v8::Handle<v8::Function> callback, | 51 v8::Handle<v8::Function> callback, |
| 56 gin::Handle<HandleWrapper> handle_wrapper) | 52 gin::Handle<HandleWrapper> handle_wrapper) |
| 57 : wait_id_(0), handle_wrapper_(handle_wrapper.get()), weak_factory_(this) { | 53 : handle_wrapper_(handle_wrapper.get()), |
| 54 weak_factory_(this) { |
| 58 handle_wrapper_->AddCloseObserver(this); | 55 handle_wrapper_->AddCloseObserver(this); |
| 59 v8::Handle<v8::Context> context = isolate->GetCurrentContext(); | 56 v8::Handle<v8::Context> context = isolate->GetCurrentContext(); |
| 60 runner_ = gin::PerContextData::From(context)->runner()->GetWeakPtr(); | 57 runner_ = gin::PerContextData::From(context)->runner()->GetWeakPtr(); |
| 61 GetWrapper(isolate) | 58 GetWrapper(isolate) |
| 62 ->SetPrivate(context, GetHiddenPropertyName(isolate), callback) | 59 ->SetPrivate(context, GetHiddenPropertyName(isolate), callback) |
| 63 .FromJust(); | 60 .FromJust(); |
| 64 } | 61 } |
| 65 | 62 |
| 66 WaitingCallback::~WaitingCallback() { | 63 WaitingCallback::~WaitingCallback() { |
| 67 Cancel(); | 64 Cancel(); |
| 68 } | 65 } |
| 69 | 66 |
| 70 // static | 67 void WaitingCallback::RemoveHandleCloseObserver() { |
| 71 void WaitingCallback::CallOnHandleReady(void* closure, MojoResult result) { | |
| 72 static_cast<WaitingCallback*>(closure)->OnHandleReady(result); | |
| 73 } | |
| 74 | |
| 75 void WaitingCallback::ClearWaitId() { | |
| 76 wait_id_ = 0; | |
| 77 handle_wrapper_->RemoveCloseObserver(this); | 68 handle_wrapper_->RemoveCloseObserver(this); |
| 78 handle_wrapper_ = nullptr; | 69 handle_wrapper_ = nullptr; |
| 79 } | 70 } |
| 80 | 71 |
| 81 void WaitingCallback::OnHandleReady(MojoResult result) { | 72 void WaitingCallback::OnHandleReady(MojoResult result) { |
| 82 ClearWaitId(); | 73 RemoveHandleCloseObserver(); |
| 83 CallCallback(result); | 74 CallCallback(result); |
| 84 } | 75 } |
| 85 | 76 |
| 86 void WaitingCallback::CallCallback(MojoResult result) { | 77 void WaitingCallback::CallCallback(MojoResult result) { |
| 87 // ClearWaitId must already have been called. | 78 DCHECK(!handle_watcher_.is_watching()); |
| 88 DCHECK(!wait_id_); | |
| 89 DCHECK(!handle_wrapper_); | 79 DCHECK(!handle_wrapper_); |
| 90 | 80 |
| 91 if (!runner_) | 81 if (!runner_) |
| 92 return; | 82 return; |
| 93 | 83 |
| 94 gin::Runner::Scope scope(runner_.get()); | 84 gin::Runner::Scope scope(runner_.get()); |
| 95 v8::Isolate* isolate = runner_->GetContextHolder()->isolate(); | 85 v8::Isolate* isolate = runner_->GetContextHolder()->isolate(); |
| 96 | 86 |
| 97 v8::Handle<v8::Value> hidden_value = | 87 v8::Handle<v8::Value> hidden_value = |
| 98 GetWrapper(isolate) | 88 GetWrapper(isolate) |
| 99 ->GetPrivate(runner_->GetContextHolder()->context(), | 89 ->GetPrivate(runner_->GetContextHolder()->context(), |
| 100 GetHiddenPropertyName(isolate)) | 90 GetHiddenPropertyName(isolate)) |
| 101 .ToLocalChecked(); | 91 .ToLocalChecked(); |
| 102 v8::Handle<v8::Function> callback; | 92 v8::Handle<v8::Function> callback; |
| 103 CHECK(gin::ConvertFromV8(isolate, hidden_value, &callback)); | 93 CHECK(gin::ConvertFromV8(isolate, hidden_value, &callback)); |
| 104 | 94 |
| 105 v8::Handle<v8::Value> args[] = { gin::ConvertToV8(isolate, result) }; | 95 v8::Handle<v8::Value> args[] = { gin::ConvertToV8(isolate, result) }; |
| 106 runner_->Call(callback, runner_->global(), 1, args); | 96 runner_->Call(callback, runner_->global(), 1, args); |
| 107 } | 97 } |
| 108 | 98 |
| 109 void WaitingCallback::OnWillCloseHandle() { | 99 void WaitingCallback::OnWillCloseHandle() { |
| 110 Environment::GetDefaultAsyncWaiter()->CancelWait(wait_id_); | 100 handle_watcher_.Stop(); |
| 111 | 101 |
| 112 // This may be called from GC, so we can't execute Javascript now, call | 102 // This may be called from GC, so we can't execute Javascript now, call |
| 113 // ClearWaitId explicitly, and CallCallback asynchronously. | 103 // RemoveHandleCloseObserver explicitly, and CallCallback asynchronously. |
| 114 ClearWaitId(); | 104 RemoveHandleCloseObserver(); |
| 115 base::MessageLoop::current()->PostTask( | 105 base::MessageLoop::current()->PostTask( |
| 116 FROM_HERE, | 106 FROM_HERE, |
| 117 base::Bind(&WaitingCallback::CallCallback, weak_factory_.GetWeakPtr(), | 107 base::Bind(&WaitingCallback::CallCallback, weak_factory_.GetWeakPtr(), |
| 118 MOJO_RESULT_INVALID_ARGUMENT)); | 108 MOJO_RESULT_INVALID_ARGUMENT)); |
| 119 } | 109 } |
| 120 | 110 |
| 121 } // namespace js | 111 } // namespace js |
| 122 } // namespace edk | 112 } // namespace edk |
| 123 } // namespace mojo | 113 } // namespace mojo |
| OLD | NEW |