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

Side by Side Diff: mojo/public/cpp/system/watcher.cc

Issue 2540903002: Remove MessageLoop destruction observer from mojo::Watcher. (Closed)
Patch Set: . Created 4 years 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 unified diff | Download patch
« no previous file with comments | « mojo/public/cpp/system/watcher.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "mojo/public/c/system/functions.h" 10 #include "mojo/public/c/system/functions.h"
12 11
13 namespace mojo { 12 namespace mojo {
14 13
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
49 Watcher::Watcher(scoped_refptr<base::SingleThreadTaskRunner> runner) 14 Watcher::Watcher(scoped_refptr<base::SingleThreadTaskRunner> runner)
50 : task_runner_(std::move(runner)), 15 : task_runner_(std::move(runner)),
51 is_default_task_runner_(task_runner_ == 16 is_default_task_runner_(task_runner_ ==
52 base::ThreadTaskRunnerHandle::Get()), 17 base::ThreadTaskRunnerHandle::Get()),
53 weak_factory_(this) { 18 weak_factory_(this) {
54 DCHECK(task_runner_->BelongsToCurrentThread()); 19 DCHECK(task_runner_->BelongsToCurrentThread());
55 weak_self_ = weak_factory_.GetWeakPtr(); 20 weak_self_ = weak_factory_.GetWeakPtr();
56 } 21 }
57 22
58 Watcher::~Watcher() { 23 Watcher::~Watcher() {
59 if(IsWatching()) 24 if(IsWatching())
60 Cancel(); 25 Cancel();
61 } 26 }
62 27
63 bool Watcher::IsWatching() const { 28 bool Watcher::IsWatching() const {
64 DCHECK(thread_checker_.CalledOnValidThread()); 29 DCHECK(thread_checker_.CalledOnValidThread());
65 return handle_.is_valid(); 30 return handle_.is_valid();
66 } 31 }
67 32
68 MojoResult Watcher::Start(Handle handle, 33 MojoResult Watcher::Start(Handle handle,
69 MojoHandleSignals signals, 34 MojoHandleSignals signals,
70 const ReadyCallback& callback) { 35 const ReadyCallback& callback) {
71 DCHECK(thread_checker_.CalledOnValidThread()); 36 DCHECK(thread_checker_.CalledOnValidThread());
72 DCHECK(!IsWatching()); 37 DCHECK(!IsWatching());
73 DCHECK(!callback.is_null()); 38 DCHECK(!callback.is_null());
74 39
75 message_loop_observer_.reset(new MessageLoopObserver(this));
76 callback_ = callback; 40 callback_ = callback;
77 handle_ = handle; 41 handle_ = handle;
78 MojoResult result = MojoWatch(handle_.value(), signals, 42 MojoResult result = MojoWatch(handle_.value(), signals,
79 &Watcher::CallOnHandleReady, 43 &Watcher::CallOnHandleReady,
80 reinterpret_cast<uintptr_t>(this)); 44 reinterpret_cast<uintptr_t>(this));
81 if (result != MOJO_RESULT_OK) { 45 if (result != MOJO_RESULT_OK) {
82 handle_.set_value(kInvalidHandleValue); 46 handle_.set_value(kInvalidHandleValue);
83 callback_.Reset(); 47 callback_.Reset();
84 message_loop_observer_.reset();
85 DCHECK(result == MOJO_RESULT_FAILED_PRECONDITION || 48 DCHECK(result == MOJO_RESULT_FAILED_PRECONDITION ||
86 result == MOJO_RESULT_INVALID_ARGUMENT); 49 result == MOJO_RESULT_INVALID_ARGUMENT);
87 return result; 50 return result;
88 } 51 }
89 52
90 return MOJO_RESULT_OK; 53 return MOJO_RESULT_OK;
91 } 54 }
92 55
93 void Watcher::Cancel() { 56 void Watcher::Cancel() {
94 DCHECK(thread_checker_.CalledOnValidThread()); 57 DCHECK(thread_checker_.CalledOnValidThread());
95 58
96 // The watch may have already been cancelled if the handle was closed. 59 // The watch may have already been cancelled if the handle was closed.
97 if (!handle_.is_valid()) 60 if (!handle_.is_valid())
98 return; 61 return;
99 62
100 MojoResult result = 63 MojoResult result =
101 MojoCancelWatch(handle_.value(), reinterpret_cast<uintptr_t>(this)); 64 MojoCancelWatch(handle_.value(), reinterpret_cast<uintptr_t>(this));
102 message_loop_observer_.reset();
103 // |result| may be MOJO_RESULT_INVALID_ARGUMENT if |handle_| has closed, but 65 // |result| may be MOJO_RESULT_INVALID_ARGUMENT if |handle_| has closed, but
104 // OnHandleReady has not yet been called. 66 // OnHandleReady has not yet been called.
105 DCHECK(result == MOJO_RESULT_INVALID_ARGUMENT || result == MOJO_RESULT_OK); 67 DCHECK(result == MOJO_RESULT_INVALID_ARGUMENT || result == MOJO_RESULT_OK);
106 handle_.set_value(kInvalidHandleValue); 68 handle_.set_value(kInvalidHandleValue);
107 callback_.Reset(); 69 callback_.Reset();
108 } 70 }
109 71
110 void Watcher::OnHandleReady(MojoResult result) { 72 void Watcher::OnHandleReady(MojoResult result) {
111 DCHECK(thread_checker_.CalledOnValidThread()); 73 DCHECK(thread_checker_.CalledOnValidThread());
112 74
113 ReadyCallback callback = callback_; 75 ReadyCallback callback = callback_;
114 if (result == MOJO_RESULT_CANCELLED) { 76 if (result == MOJO_RESULT_CANCELLED) {
115 message_loop_observer_.reset();
116 handle_.set_value(kInvalidHandleValue); 77 handle_.set_value(kInvalidHandleValue);
117 callback_.Reset(); 78 callback_.Reset();
118 } 79 }
119 80
120 // NOTE: It's legal for |callback| to delete |this|. 81 // NOTE: It's legal for |callback| to delete |this|.
121 if (!callback.is_null()) 82 if (!callback.is_null())
122 callback.Run(result); 83 callback.Run(result);
123 } 84 }
124 85
125 // static 86 // static
(...skipping 15 matching lines...) Expand all
141 // default task runner for the IO thread. 102 // default task runner for the IO thread.
142 watcher->OnHandleReady(result); 103 watcher->OnHandleReady(result);
143 } else { 104 } else {
144 watcher->task_runner_->PostTask( 105 watcher->task_runner_->PostTask(
145 FROM_HERE, 106 FROM_HERE,
146 base::Bind(&Watcher::OnHandleReady, watcher->weak_self_, result)); 107 base::Bind(&Watcher::OnHandleReady, watcher->weak_self_, result));
147 } 108 }
148 } 109 }
149 110
150 } // namespace mojo 111 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/system/watcher.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698