| 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" |
| 8 #include "base/message_loop/message_loop.h" |
| 7 #include "gin/per_context_data.h" | 9 #include "gin/per_context_data.h" |
| 8 #include "mojo/public/cpp/environment/environment.h" | 10 #include "mojo/public/cpp/environment/environment.h" |
| 9 | 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"); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 43 | 45 |
| 44 handle_wrapper_->RemoveCloseObserver(this); | 46 handle_wrapper_->RemoveCloseObserver(this); |
| 45 handle_wrapper_ = NULL; | 47 handle_wrapper_ = NULL; |
| 46 Environment::GetDefaultAsyncWaiter()->CancelWait(wait_id_); | 48 Environment::GetDefaultAsyncWaiter()->CancelWait(wait_id_); |
| 47 wait_id_ = 0; | 49 wait_id_ = 0; |
| 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 gin::Handle<HandleWrapper> handle_wrapper) |
| 53 : wait_id_(0), handle_wrapper_(handle_wrapper.get()) { | 55 : wait_id_(0), handle_wrapper_(handle_wrapper.get()), weak_factory_(this) { |
| 54 handle_wrapper_->AddCloseObserver(this); | 56 handle_wrapper_->AddCloseObserver(this); |
| 55 v8::Handle<v8::Context> context = isolate->GetCurrentContext(); | 57 v8::Handle<v8::Context> context = isolate->GetCurrentContext(); |
| 56 runner_ = gin::PerContextData::From(context)->runner()->GetWeakPtr(); | 58 runner_ = gin::PerContextData::From(context)->runner()->GetWeakPtr(); |
| 57 GetWrapper(isolate)->SetHiddenValue(GetHiddenPropertyName(isolate), callback); | 59 GetWrapper(isolate)->SetHiddenValue(GetHiddenPropertyName(isolate), callback); |
| 58 } | 60 } |
| 59 | 61 |
| 60 WaitingCallback::~WaitingCallback() { | 62 WaitingCallback::~WaitingCallback() { |
| 61 Cancel(); | 63 Cancel(); |
| 62 } | 64 } |
| 63 | 65 |
| 64 // static | 66 // static |
| 65 void WaitingCallback::CallOnHandleReady(void* closure, MojoResult result) { | 67 void WaitingCallback::CallOnHandleReady(void* closure, MojoResult result) { |
| 66 static_cast<WaitingCallback*>(closure)->OnHandleReady(result); | 68 static_cast<WaitingCallback*>(closure)->OnHandleReady(result); |
| 67 } | 69 } |
| 68 | 70 |
| 69 void WaitingCallback::OnHandleReady(MojoResult result) { | 71 void WaitingCallback::ClearWaitId() { |
| 70 wait_id_ = 0; | 72 wait_id_ = 0; |
| 71 handle_wrapper_->RemoveCloseObserver(this); | 73 handle_wrapper_->RemoveCloseObserver(this); |
| 72 handle_wrapper_ = NULL; | 74 handle_wrapper_ = nullptr; |
| 75 } |
| 76 |
| 77 void WaitingCallback::OnHandleReady(MojoResult result) { |
| 78 ClearWaitId(); |
| 79 CallCallback(result); |
| 80 } |
| 81 |
| 82 void WaitingCallback::CallCallback(MojoResult result) { |
| 83 // ClearWaitId must already have been called. |
| 84 DCHECK(!wait_id_); |
| 85 DCHECK(!handle_wrapper_); |
| 73 | 86 |
| 74 if (!runner_) | 87 if (!runner_) |
| 75 return; | 88 return; |
| 76 | 89 |
| 77 gin::Runner::Scope scope(runner_.get()); | 90 gin::Runner::Scope scope(runner_.get()); |
| 78 v8::Isolate* isolate = runner_->GetContextHolder()->isolate(); | 91 v8::Isolate* isolate = runner_->GetContextHolder()->isolate(); |
| 79 | 92 |
| 80 v8::Handle<v8::Value> hidden_value = | 93 v8::Handle<v8::Value> hidden_value = |
| 81 GetWrapper(isolate)->GetHiddenValue(GetHiddenPropertyName(isolate)); | 94 GetWrapper(isolate)->GetHiddenValue(GetHiddenPropertyName(isolate)); |
| 82 v8::Handle<v8::Function> callback; | 95 v8::Handle<v8::Function> callback; |
| 83 CHECK(gin::ConvertFromV8(isolate, hidden_value, &callback)); | 96 CHECK(gin::ConvertFromV8(isolate, hidden_value, &callback)); |
| 84 | 97 |
| 85 v8::Handle<v8::Value> args[] = { gin::ConvertToV8(isolate, result) }; | 98 v8::Handle<v8::Value> args[] = { gin::ConvertToV8(isolate, result) }; |
| 86 runner_->Call(callback, runner_->global(), 1, args); | 99 runner_->Call(callback, runner_->global(), 1, args); |
| 87 } | 100 } |
| 88 | 101 |
| 89 void WaitingCallback::OnWillCloseHandle() { | 102 void WaitingCallback::OnWillCloseHandle() { |
| 90 Environment::GetDefaultAsyncWaiter()->CancelWait(wait_id_); | 103 Environment::GetDefaultAsyncWaiter()->CancelWait(wait_id_); |
| 91 OnHandleReady(MOJO_RESULT_INVALID_ARGUMENT); | 104 |
| 105 // This may be called from GC, so we can't execute Javascript now, call |
| 106 // ClearWaitId explicitly, and CallCallback asynchronously. |
| 107 ClearWaitId(); |
| 108 base::MessageLoop::current()->PostTask( |
| 109 FROM_HERE, |
| 110 base::Bind(&WaitingCallback::CallCallback, weak_factory_.GetWeakPtr(), |
| 111 MOJO_RESULT_INVALID_ARGUMENT)); |
| 92 } | 112 } |
| 93 | 113 |
| 94 } // namespace js | 114 } // namespace js |
| 95 } // namespace mojo | 115 } // namespace mojo |
| OLD | NEW |