| Index: mojo/public/cpp/system/watcher.cc
|
| diff --git a/mojo/public/cpp/system/watcher.cc b/mojo/public/cpp/system/watcher.cc
|
| index fc03c5a0a0e8b1f4e41c01b29705d743e051d34c..d9319fb5bda1fc368378a77e15de03b941783a9e 100644
|
| --- a/mojo/public/cpp/system/watcher.cc
|
| +++ b/mojo/public/cpp/system/watcher.cc
|
| @@ -7,9 +7,44 @@
|
| #include "base/bind.h"
|
| #include "base/location.h"
|
| #include "base/macros.h"
|
| +#include "base/message_loop/message_loop.h"
|
| #include "mojo/public/c/system/functions.h"
|
|
|
| namespace mojo {
|
| +
|
| +class Watcher::MessageLoopObserver
|
| + : public base::MessageLoop::DestructionObserver {
|
| + public:
|
| + explicit MessageLoopObserver(Watcher* watcher) : watcher_(watcher) {
|
| + base::MessageLoop::current()->AddDestructionObserver(this);
|
| + }
|
| +
|
| + ~MessageLoopObserver() override {
|
| + StopObservingIfNecessary();
|
| + }
|
| +
|
| + private:
|
| + // base::MessageLoop::DestructionObserver:
|
| + void WillDestroyCurrentMessageLoop() override {
|
| + StopObservingIfNecessary();
|
| + if (watcher_->IsWatching()) {
|
| + // TODO(yzshen): Remove this notification. crbug.com/604762
|
| + watcher_->OnHandleReady(MOJO_RESULT_ABORTED);
|
| + }
|
| + }
|
| +
|
| + void StopObservingIfNecessary() {
|
| + if (is_observing_) {
|
| + is_observing_ = false;
|
| + base::MessageLoop::current()->RemoveDestructionObserver(this);
|
| + }
|
| + }
|
| +
|
| + bool is_observing_ = true;
|
| + Watcher* watcher_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(MessageLoopObserver);
|
| +};
|
|
|
| Watcher::Watcher(scoped_refptr<base::SingleThreadTaskRunner> runner)
|
| : task_runner_(std::move(runner)),
|
| @@ -37,6 +72,7 @@
|
| DCHECK(!IsWatching());
|
| DCHECK(!callback.is_null());
|
|
|
| + message_loop_observer_.reset(new MessageLoopObserver(this));
|
| callback_ = callback;
|
| handle_ = handle;
|
| MojoResult result = MojoWatch(handle_.value(), signals,
|
| @@ -45,6 +81,7 @@
|
| if (result != MOJO_RESULT_OK) {
|
| handle_.set_value(kInvalidHandleValue);
|
| callback_.Reset();
|
| + message_loop_observer_.reset();
|
| DCHECK(result == MOJO_RESULT_FAILED_PRECONDITION ||
|
| result == MOJO_RESULT_INVALID_ARGUMENT);
|
| return result;
|
| @@ -62,6 +99,7 @@
|
|
|
| MojoResult result =
|
| MojoCancelWatch(handle_.value(), reinterpret_cast<uintptr_t>(this));
|
| + message_loop_observer_.reset();
|
| // |result| may be MOJO_RESULT_INVALID_ARGUMENT if |handle_| has closed, but
|
| // OnHandleReady has not yet been called.
|
| DCHECK(result == MOJO_RESULT_INVALID_ARGUMENT || result == MOJO_RESULT_OK);
|
| @@ -74,6 +112,7 @@
|
|
|
| ReadyCallback callback = callback_;
|
| if (result == MOJO_RESULT_CANCELLED) {
|
| + message_loop_observer_.reset();
|
| handle_.set_value(kInvalidHandleValue);
|
| callback_.Reset();
|
| }
|
|
|