| 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 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 } // namespace | 22 } // namespace |
| 23 | 23 |
| 24 gin::WrapperInfo WaitingCallback::kWrapperInfo = { gin::kEmbedderNativeGin }; | 24 gin::WrapperInfo WaitingCallback::kWrapperInfo = { gin::kEmbedderNativeGin }; |
| 25 | 25 |
| 26 // static | 26 // static |
| 27 gin::Handle<WaitingCallback> WaitingCallback::Create( | 27 gin::Handle<WaitingCallback> WaitingCallback::Create( |
| 28 v8::Isolate* isolate, | 28 v8::Isolate* isolate, |
| 29 v8::Handle<v8::Function> callback, | 29 v8::Handle<v8::Function> callback, |
| 30 gin::Handle<HandleWrapper> handle_wrapper, | 30 gin::Handle<HandleWrapper> handle_wrapper, |
| 31 MojoHandleSignals signals) { | 31 MojoHandleSignals signals, |
| 32 bool one_shot) { |
| 32 gin::Handle<WaitingCallback> waiting_callback = gin::CreateHandle( | 33 gin::Handle<WaitingCallback> waiting_callback = gin::CreateHandle( |
| 33 isolate, new WaitingCallback(isolate, callback, handle_wrapper)); | 34 isolate, new WaitingCallback(isolate, callback, one_shot)); |
| 34 | 35 MojoResult result = waiting_callback->watcher_.Start( |
| 35 waiting_callback->handle_watcher_.Start( | 36 handle_wrapper->get(), signals, |
| 36 handle_wrapper->get(), signals, MOJO_DEADLINE_INDEFINITE, | |
| 37 base::Bind(&WaitingCallback::OnHandleReady, | 37 base::Bind(&WaitingCallback::OnHandleReady, |
| 38 base::Unretained(waiting_callback.get()))); | 38 base::Unretained(waiting_callback.get()))); |
| 39 |
| 40 // The signals may already be unsatisfiable. |
| 41 if (result == MOJO_RESULT_FAILED_PRECONDITION) |
| 42 waiting_callback->OnHandleReady(MOJO_RESULT_FAILED_PRECONDITION); |
| 43 |
| 39 return waiting_callback; | 44 return waiting_callback; |
| 40 } | 45 } |
| 41 | 46 |
| 42 void WaitingCallback::Cancel() { | 47 void WaitingCallback::Cancel() { |
| 43 if (!handle_watcher_.is_watching()) | 48 if (watcher_.IsWatching()) |
| 44 return; | 49 watcher_.Cancel(); |
| 45 | |
| 46 RemoveHandleCloseObserver(); | |
| 47 handle_watcher_.Stop(); | |
| 48 } | 50 } |
| 49 | 51 |
| 50 WaitingCallback::WaitingCallback(v8::Isolate* isolate, | 52 WaitingCallback::WaitingCallback(v8::Isolate* isolate, |
| 51 v8::Handle<v8::Function> callback, | 53 v8::Handle<v8::Function> callback, |
| 52 gin::Handle<HandleWrapper> handle_wrapper) | 54 bool one_shot) |
| 53 : handle_wrapper_(handle_wrapper.get()), | 55 : one_shot_(one_shot), |
| 54 weak_factory_(this) { | 56 weak_factory_(this) { |
| 55 handle_wrapper_->AddCloseObserver(this); | |
| 56 v8::Handle<v8::Context> context = isolate->GetCurrentContext(); | 57 v8::Handle<v8::Context> context = isolate->GetCurrentContext(); |
| 57 runner_ = gin::PerContextData::From(context)->runner()->GetWeakPtr(); | 58 runner_ = gin::PerContextData::From(context)->runner()->GetWeakPtr(); |
| 58 GetWrapper(isolate) | 59 GetWrapper(isolate) |
| 59 ->SetPrivate(context, GetHiddenPropertyName(isolate), callback) | 60 ->SetPrivate(context, GetHiddenPropertyName(isolate), callback) |
| 60 .FromJust(); | 61 .FromJust(); |
| 61 } | 62 } |
| 62 | 63 |
| 63 WaitingCallback::~WaitingCallback() { | 64 WaitingCallback::~WaitingCallback() { |
| 64 Cancel(); | 65 Cancel(); |
| 65 } | 66 } |
| 66 | 67 |
| 67 void WaitingCallback::RemoveHandleCloseObserver() { | |
| 68 handle_wrapper_->RemoveCloseObserver(this); | |
| 69 handle_wrapper_ = nullptr; | |
| 70 } | |
| 71 | |
| 72 void WaitingCallback::OnHandleReady(MojoResult result) { | 68 void WaitingCallback::OnHandleReady(MojoResult result) { |
| 73 RemoveHandleCloseObserver(); | |
| 74 CallCallback(result); | |
| 75 } | |
| 76 | |
| 77 void WaitingCallback::CallCallback(MojoResult result) { | |
| 78 DCHECK(!handle_watcher_.is_watching()); | |
| 79 DCHECK(!handle_wrapper_); | |
| 80 | |
| 81 if (!runner_) | 69 if (!runner_) |
| 82 return; | 70 return; |
| 83 | 71 |
| 84 gin::Runner::Scope scope(runner_.get()); | 72 gin::Runner::Scope scope(runner_.get()); |
| 85 v8::Isolate* isolate = runner_->GetContextHolder()->isolate(); | 73 v8::Isolate* isolate = runner_->GetContextHolder()->isolate(); |
| 86 | 74 |
| 87 v8::Handle<v8::Value> hidden_value = | 75 v8::Handle<v8::Value> hidden_value = |
| 88 GetWrapper(isolate) | 76 GetWrapper(isolate) |
| 89 ->GetPrivate(runner_->GetContextHolder()->context(), | 77 ->GetPrivate(runner_->GetContextHolder()->context(), |
| 90 GetHiddenPropertyName(isolate)) | 78 GetHiddenPropertyName(isolate)) |
| 91 .ToLocalChecked(); | 79 .ToLocalChecked(); |
| 92 v8::Handle<v8::Function> callback; | 80 v8::Handle<v8::Function> callback; |
| 93 CHECK(gin::ConvertFromV8(isolate, hidden_value, &callback)); | 81 CHECK(gin::ConvertFromV8(isolate, hidden_value, &callback)); |
| 94 | 82 |
| 95 v8::Handle<v8::Value> args[] = { gin::ConvertToV8(isolate, result) }; | 83 v8::Handle<v8::Value> args[] = { gin::ConvertToV8(isolate, result) }; |
| 96 runner_->Call(callback, runner_->global(), 1, args); | 84 runner_->Call(callback, runner_->global(), 1, args); |
| 97 } | |
| 98 | 85 |
| 99 void WaitingCallback::OnWillCloseHandle() { | 86 if (one_shot_ || result == MOJO_RESULT_CANCELLED) { |
| 100 handle_watcher_.Stop(); | 87 runner_.reset(); |
| 101 | 88 Cancel(); |
| 102 // This may be called from GC, so we can't execute Javascript now, call | 89 } |
| 103 // RemoveHandleCloseObserver explicitly, and CallCallback asynchronously. | |
| 104 RemoveHandleCloseObserver(); | |
| 105 base::MessageLoop::current()->PostTask( | |
| 106 FROM_HERE, | |
| 107 base::Bind(&WaitingCallback::CallCallback, weak_factory_.GetWeakPtr(), | |
| 108 MOJO_RESULT_INVALID_ARGUMENT)); | |
| 109 } | 90 } |
| 110 | 91 |
| 111 } // namespace js | 92 } // namespace js |
| 112 } // namespace edk | 93 } // namespace edk |
| 113 } // namespace mojo | 94 } // namespace mojo |
| OLD | NEW |