| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/public/cpp/system/watcher.h" | 5 #include "mojo/public/cpp/system/watcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 10 #include "mojo/public/c/system/functions.h" | 11 #include "mojo/public/c/system/functions.h" |
| 11 | 12 |
| 12 namespace mojo { | 13 namespace mojo { |
| 13 | 14 |
| 15 class Watcher::MessageLoopObserver |
| 16 : public base::MessageLoop::DestructionObserver { |
| 17 public: |
| 18 explicit MessageLoopObserver(Watcher* watcher) : watcher_(watcher) { |
| 19 base::MessageLoop::current()->AddDestructionObserver(this); |
| 20 } |
| 21 |
| 22 ~MessageLoopObserver() override { |
| 23 StopObservingIfNecessary(); |
| 24 } |
| 25 |
| 26 private: |
| 27 // base::MessageLoop::DestructionObserver: |
| 28 void WillDestroyCurrentMessageLoop() override { |
| 29 StopObservingIfNecessary(); |
| 30 if (watcher_->IsWatching()) { |
| 31 // TODO(yzshen): Remove this notification. crbug.com/604762 |
| 32 watcher_->OnHandleReady(MOJO_RESULT_ABORTED); |
| 33 } |
| 34 } |
| 35 |
| 36 void StopObservingIfNecessary() { |
| 37 if (is_observing_) { |
| 38 is_observing_ = false; |
| 39 base::MessageLoop::current()->RemoveDestructionObserver(this); |
| 40 } |
| 41 } |
| 42 |
| 43 bool is_observing_ = true; |
| 44 Watcher* watcher_; |
| 45 |
| 46 DISALLOW_COPY_AND_ASSIGN(MessageLoopObserver); |
| 47 }; |
| 48 |
| 14 Watcher::Watcher(scoped_refptr<base::SingleThreadTaskRunner> runner) | 49 Watcher::Watcher(scoped_refptr<base::SingleThreadTaskRunner> runner) |
| 15 : task_runner_(std::move(runner)), | 50 : task_runner_(std::move(runner)), |
| 16 is_default_task_runner_(task_runner_ == | 51 is_default_task_runner_(task_runner_ == |
| 17 base::ThreadTaskRunnerHandle::Get()), | 52 base::ThreadTaskRunnerHandle::Get()), |
| 18 weak_factory_(this) { | 53 weak_factory_(this) { |
| 19 DCHECK(task_runner_->BelongsToCurrentThread()); | 54 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 20 weak_self_ = weak_factory_.GetWeakPtr(); | 55 weak_self_ = weak_factory_.GetWeakPtr(); |
| 21 } | 56 } |
| 22 | 57 |
| 23 Watcher::~Watcher() { | 58 Watcher::~Watcher() { |
| 24 if(IsWatching()) | 59 if(IsWatching()) |
| 25 Cancel(); | 60 Cancel(); |
| 26 } | 61 } |
| 27 | 62 |
| 28 bool Watcher::IsWatching() const { | 63 bool Watcher::IsWatching() const { |
| 29 DCHECK(thread_checker_.CalledOnValidThread()); | 64 DCHECK(thread_checker_.CalledOnValidThread()); |
| 30 return handle_.is_valid(); | 65 return handle_.is_valid(); |
| 31 } | 66 } |
| 32 | 67 |
| 33 MojoResult Watcher::Start(Handle handle, | 68 MojoResult Watcher::Start(Handle handle, |
| 34 MojoHandleSignals signals, | 69 MojoHandleSignals signals, |
| 35 const ReadyCallback& callback) { | 70 const ReadyCallback& callback) { |
| 36 DCHECK(thread_checker_.CalledOnValidThread()); | 71 DCHECK(thread_checker_.CalledOnValidThread()); |
| 37 DCHECK(!IsWatching()); | 72 DCHECK(!IsWatching()); |
| 38 DCHECK(!callback.is_null()); | 73 DCHECK(!callback.is_null()); |
| 39 | 74 |
| 75 message_loop_observer_.reset(new MessageLoopObserver(this)); |
| 40 callback_ = callback; | 76 callback_ = callback; |
| 41 handle_ = handle; | 77 handle_ = handle; |
| 42 MojoResult result = MojoWatch(handle_.value(), signals, | 78 MojoResult result = MojoWatch(handle_.value(), signals, |
| 43 &Watcher::CallOnHandleReady, | 79 &Watcher::CallOnHandleReady, |
| 44 reinterpret_cast<uintptr_t>(this)); | 80 reinterpret_cast<uintptr_t>(this)); |
| 45 if (result != MOJO_RESULT_OK) { | 81 if (result != MOJO_RESULT_OK) { |
| 46 handle_.set_value(kInvalidHandleValue); | 82 handle_.set_value(kInvalidHandleValue); |
| 47 callback_.Reset(); | 83 callback_.Reset(); |
| 84 message_loop_observer_.reset(); |
| 48 DCHECK(result == MOJO_RESULT_FAILED_PRECONDITION || | 85 DCHECK(result == MOJO_RESULT_FAILED_PRECONDITION || |
| 49 result == MOJO_RESULT_INVALID_ARGUMENT); | 86 result == MOJO_RESULT_INVALID_ARGUMENT); |
| 50 return result; | 87 return result; |
| 51 } | 88 } |
| 52 | 89 |
| 53 return MOJO_RESULT_OK; | 90 return MOJO_RESULT_OK; |
| 54 } | 91 } |
| 55 | 92 |
| 56 void Watcher::Cancel() { | 93 void Watcher::Cancel() { |
| 57 DCHECK(thread_checker_.CalledOnValidThread()); | 94 DCHECK(thread_checker_.CalledOnValidThread()); |
| 58 | 95 |
| 59 // The watch may have already been cancelled if the handle was closed. | 96 // The watch may have already been cancelled if the handle was closed. |
| 60 if (!handle_.is_valid()) | 97 if (!handle_.is_valid()) |
| 61 return; | 98 return; |
| 62 | 99 |
| 63 MojoResult result = | 100 MojoResult result = |
| 64 MojoCancelWatch(handle_.value(), reinterpret_cast<uintptr_t>(this)); | 101 MojoCancelWatch(handle_.value(), reinterpret_cast<uintptr_t>(this)); |
| 102 message_loop_observer_.reset(); |
| 65 // |result| may be MOJO_RESULT_INVALID_ARGUMENT if |handle_| has closed, but | 103 // |result| may be MOJO_RESULT_INVALID_ARGUMENT if |handle_| has closed, but |
| 66 // OnHandleReady has not yet been called. | 104 // OnHandleReady has not yet been called. |
| 67 DCHECK(result == MOJO_RESULT_INVALID_ARGUMENT || result == MOJO_RESULT_OK); | 105 DCHECK(result == MOJO_RESULT_INVALID_ARGUMENT || result == MOJO_RESULT_OK); |
| 68 handle_.set_value(kInvalidHandleValue); | 106 handle_.set_value(kInvalidHandleValue); |
| 69 callback_.Reset(); | 107 callback_.Reset(); |
| 70 } | 108 } |
| 71 | 109 |
| 72 void Watcher::OnHandleReady(MojoResult result) { | 110 void Watcher::OnHandleReady(MojoResult result) { |
| 73 DCHECK(thread_checker_.CalledOnValidThread()); | 111 DCHECK(thread_checker_.CalledOnValidThread()); |
| 74 | 112 |
| 75 ReadyCallback callback = callback_; | 113 ReadyCallback callback = callback_; |
| 76 if (result == MOJO_RESULT_CANCELLED) { | 114 if (result == MOJO_RESULT_CANCELLED) { |
| 115 message_loop_observer_.reset(); |
| 77 handle_.set_value(kInvalidHandleValue); | 116 handle_.set_value(kInvalidHandleValue); |
| 78 callback_.Reset(); | 117 callback_.Reset(); |
| 79 } | 118 } |
| 80 | 119 |
| 81 // NOTE: It's legal for |callback| to delete |this|. | 120 // NOTE: It's legal for |callback| to delete |this|. |
| 82 if (!callback.is_null()) | 121 if (!callback.is_null()) |
| 83 callback.Run(result); | 122 callback.Run(result); |
| 84 } | 123 } |
| 85 | 124 |
| 86 // static | 125 // static |
| (...skipping 15 matching lines...) Expand all Loading... |
| 102 // default task runner for the IO thread. | 141 // default task runner for the IO thread. |
| 103 watcher->OnHandleReady(result); | 142 watcher->OnHandleReady(result); |
| 104 } else { | 143 } else { |
| 105 watcher->task_runner_->PostTask( | 144 watcher->task_runner_->PostTask( |
| 106 FROM_HERE, | 145 FROM_HERE, |
| 107 base::Bind(&Watcher::OnHandleReady, watcher->weak_self_, result)); | 146 base::Bind(&Watcher::OnHandleReady, watcher->weak_self_, result)); |
| 108 } | 147 } |
| 109 } | 148 } |
| 110 | 149 |
| 111 } // namespace mojo | 150 } // namespace mojo |
| OLD | NEW |