Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Unified Diff: mojo/edk/js/waiting_callback.cc

Issue 1777673003: [mojo-bindings] Use Watcher API for JS bindings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/edk/js/waiting_callback.h ('k') | mojo/public/js/connector.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/edk/js/waiting_callback.cc
diff --git a/mojo/edk/js/waiting_callback.cc b/mojo/edk/js/waiting_callback.cc
index af34d87ef801c3fcd1a5d2331595b040de975b70..4ba2c61f32cc7625e52fba6bbb12ca208f87af74 100644
--- a/mojo/edk/js/waiting_callback.cc
+++ b/mojo/edk/js/waiting_callback.cc
@@ -28,31 +28,32 @@ gin::Handle<WaitingCallback> WaitingCallback::Create(
v8::Isolate* isolate,
v8::Handle<v8::Function> callback,
gin::Handle<HandleWrapper> handle_wrapper,
- MojoHandleSignals signals) {
+ MojoHandleSignals signals,
+ bool one_shot) {
gin::Handle<WaitingCallback> waiting_callback = gin::CreateHandle(
- isolate, new WaitingCallback(isolate, callback, handle_wrapper));
-
- waiting_callback->handle_watcher_.Start(
- handle_wrapper->get(), signals, MOJO_DEADLINE_INDEFINITE,
+ isolate, new WaitingCallback(isolate, callback, one_shot));
+ MojoResult result = waiting_callback->watcher_.Start(
+ handle_wrapper->get(), signals,
base::Bind(&WaitingCallback::OnHandleReady,
base::Unretained(waiting_callback.get())));
+
+ // The signals may already be unsatisfiable.
+ if (result == MOJO_RESULT_FAILED_PRECONDITION)
+ waiting_callback->OnHandleReady(MOJO_RESULT_FAILED_PRECONDITION);
+
return waiting_callback;
}
void WaitingCallback::Cancel() {
- if (!handle_watcher_.is_watching())
- return;
-
- RemoveHandleCloseObserver();
- handle_watcher_.Stop();
+ if (watcher_.IsWatching())
+ watcher_.Cancel();
}
WaitingCallback::WaitingCallback(v8::Isolate* isolate,
v8::Handle<v8::Function> callback,
- gin::Handle<HandleWrapper> handle_wrapper)
- : handle_wrapper_(handle_wrapper.get()),
+ bool one_shot)
+ : one_shot_(one_shot),
weak_factory_(this) {
- handle_wrapper_->AddCloseObserver(this);
v8::Handle<v8::Context> context = isolate->GetCurrentContext();
runner_ = gin::PerContextData::From(context)->runner()->GetWeakPtr();
GetWrapper(isolate)
@@ -64,20 +65,7 @@ WaitingCallback::~WaitingCallback() {
Cancel();
}
-void WaitingCallback::RemoveHandleCloseObserver() {
- handle_wrapper_->RemoveCloseObserver(this);
- handle_wrapper_ = nullptr;
-}
-
void WaitingCallback::OnHandleReady(MojoResult result) {
- RemoveHandleCloseObserver();
- CallCallback(result);
-}
-
-void WaitingCallback::CallCallback(MojoResult result) {
- DCHECK(!handle_watcher_.is_watching());
- DCHECK(!handle_wrapper_);
-
if (!runner_)
return;
@@ -94,18 +82,11 @@ void WaitingCallback::CallCallback(MojoResult result) {
v8::Handle<v8::Value> args[] = { gin::ConvertToV8(isolate, result) };
runner_->Call(callback, runner_->global(), 1, args);
-}
-
-void WaitingCallback::OnWillCloseHandle() {
- handle_watcher_.Stop();
- // This may be called from GC, so we can't execute Javascript now, call
- // RemoveHandleCloseObserver explicitly, and CallCallback asynchronously.
- RemoveHandleCloseObserver();
- base::MessageLoop::current()->PostTask(
- FROM_HERE,
- base::Bind(&WaitingCallback::CallCallback, weak_factory_.GetWeakPtr(),
- MOJO_RESULT_INVALID_ARGUMENT));
+ if (one_shot_ || result == MOJO_RESULT_CANCELLED) {
+ runner_.reset();
+ Cancel();
+ }
}
} // namespace js
« no previous file with comments | « mojo/edk/js/waiting_callback.h ('k') | mojo/public/js/connector.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698