[mojo-bindings] Use Watcher API for JS bindings

BUG=592183

Review URL: https://codereview.chromium.org/1777673003

Cr-Commit-Position: refs/heads/master@{#380092}


CrOS-Libchrome-Original-Commit: b98777bcf33f467bba92cff7de951f0b3fd13b32
diff --git a/mojo/public/js/connector.js b/mojo/public/js/connector.js
index e672b54..674f36b 100644
--- a/mojo/public/js/connector.js
+++ b/mojo/public/js/connector.js
@@ -16,17 +16,20 @@
     this.dropWrites_ = false;
     this.error_ = false;
     this.incomingReceiver_ = null;
-    this.readWaitCookie_ = null;
+    this.readWatcher_ = null;
     this.errorHandler_ = null;
 
-    if (handle)
-      this.waitToReadMore_();
+    if (handle) {
+      this.readWatcher_ = support.watch(handle,
+                                        core.HANDLE_SIGNAL_READABLE,
+                                        this.readMore_.bind(this));
+    }
   }
 
   Connector.prototype.close = function() {
-    if (this.readWaitCookie_) {
-      support.cancelWait(this.readWaitCookie_);
-      this.readWaitCookie_ = null;
+    if (this.readWatcher_) {
+      support.cancelWatch(this.readWatcher_);
+      this.readWatcher_ = null;
     }
     if (this.handle_ != null) {
       core.close(this.handle_);
@@ -79,22 +82,14 @@
     return this.error_;
   };
 
-  Connector.prototype.waitToReadMore_ = function() {
-    this.readWaitCookie_ = support.asyncWait(this.handle_,
-                                             core.HANDLE_SIGNAL_READABLE,
-                                             this.readMore_.bind(this));
-  };
-
   Connector.prototype.readMore_ = function(result) {
     for (;;) {
       var read = core.readMessage(this.handle_,
                                   core.READ_MESSAGE_FLAG_NONE);
       if (this.handle_ == null) // The connector has been closed.
         return;
-      if (read.result == core.RESULT_SHOULD_WAIT) {
-        this.waitToReadMore_();
+      if (read.result == core.RESULT_SHOULD_WAIT)
         return;
-      }
       if (read.result != core.RESULT_OK) {
         this.error_ = true;
         if (this.errorHandler_)
@@ -118,9 +113,6 @@
 
   TestConnector.prototype = Object.create(Connector.prototype);
 
-  TestConnector.prototype.waitToReadMore_ = function() {
-  }
-
   TestConnector.prototype.waitForNextMessage = function() {
     var wait = core.wait(this.handle_, core.HANDLE_SIGNAL_READABLE,
                          core.DEADLINE_INDEFINITE);
diff --git a/mojo/public/js/support.js b/mojo/public/js/support.js
index 025da6c..7e27504 100644
--- a/mojo/public/js/support.js
+++ b/mojo/public/js/support.js
@@ -9,22 +9,45 @@
 
 while (1);
 
-/*
+/* @deprecated Please use watch()/cancelWatch() instead of
+ *     asyncWait()/cancelWait().
+ *
  * Waits on the given handle until the state indicated by |signals| is
  * satisfied.
  *
  * @param {MojoHandle} handle The handle to wait on.
  * @param {MojoHandleSignals} signals Specifies the condition to wait for.
  * @param {function (mojoResult)} callback Called with the result the wait is
- * complete. See MojoWait for possible result codes.
+ *     complete. See MojoWait for possible result codes.
  *
  * @return {MojoWaitId} A waitId that can be passed to cancelWait to cancel the
- * wait.
+ *     wait.
  */
 function asyncWait(handle, signals, callback) { [native code] }
 
-/*
+/* @deprecated Please use watch()/cancelWatch() instead of
+ *     asyncWait()/cancelWait().
+ *
  * Cancels the asyncWait operation specified by the given |waitId|.
+ *
  * @param {MojoWaitId} waitId The waitId returned by asyncWait.
  */
 function cancelWait(waitId) { [native code] }
+
+/* Begins watching a handle for |signals| to be satisfied or unsatisfiable.
+ *
+  * @param {MojoHandle} handle The handle to watch.
+  * @param {MojoHandleSignals} signals The signals to watch.
+  * @param {function (mojoResult)} calback Called with a result any time
+  *     the watched signals become satisfied or unsatisfiable.
+  *
+  * @param {MojoWatchId} watchId An opaque identifier that identifies this
+  *     watch.
+  */
+function watch(handle, signals, callback) { [native code] }
+
+/* Cancels a handle watch initiated by watch().
+ *
+ * @param {MojoWatchId} watchId The watch identifier returned by watch().
+ */
+function cancelWatch(watchId) { [native code] }