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

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

Issue 2725133002: Mojo: Armed Watchers (Closed)
Patch Set: . Created 3 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 unified diff | Download patch
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/trace_event/heap_profiler.h" 10 #include "base/trace_event/heap_profiler.h"
11 #include "mojo/public/c/system/functions.h" 11 #include "mojo/public/c/system/functions.h"
12 12
13 namespace mojo { 13 namespace mojo {
14 14
15 Watcher::Watcher(const tracked_objects::Location& from_here, 15 Watcher::Watcher(const tracked_objects::Location& from_here,
16 ArmingPolicy arming_policy,
16 scoped_refptr<base::SingleThreadTaskRunner> runner) 17 scoped_refptr<base::SingleThreadTaskRunner> runner)
17 : task_runner_(std::move(runner)), 18 : arming_policy_(arming_policy),
19 task_runner_(std::move(runner)),
18 is_default_task_runner_(task_runner_ == 20 is_default_task_runner_(task_runner_ ==
19 base::ThreadTaskRunnerHandle::Get()), 21 base::ThreadTaskRunnerHandle::Get()),
20 heap_profiler_tag_(from_here.file_name()), 22 heap_profiler_tag_(from_here.file_name()),
21 weak_factory_(this) { 23 weak_factory_(this) {
22 DCHECK(task_runner_->BelongsToCurrentThread()); 24 DCHECK(task_runner_->BelongsToCurrentThread());
23 weak_self_ = weak_factory_.GetWeakPtr(); 25 weak_self_ = weak_factory_.GetWeakPtr();
24 } 26 }
25 27
26 Watcher::~Watcher() { 28 Watcher::~Watcher() {
27 if(IsWatching()) 29 if(IsWatching())
28 Cancel(); 30 Cancel();
31
32 if (was_deleted_flag_)
33 *was_deleted_flag_ = true;
29 } 34 }
30 35
31 bool Watcher::IsWatching() const { 36 bool Watcher::IsWatching() const {
32 DCHECK(thread_checker_.CalledOnValidThread()); 37 DCHECK(thread_checker_.CalledOnValidThread());
33 return handle_.is_valid(); 38 return handle_.is_valid();
34 } 39 }
35 40
36 MojoResult Watcher::Start(Handle handle, 41 MojoResult Watcher::Start(Handle handle,
37 MojoHandleSignals signals, 42 MojoHandleSignals signals,
38 const ReadyCallback& callback) { 43 const ReadyCallback& callback) {
39 DCHECK(thread_checker_.CalledOnValidThread()); 44 DCHECK(thread_checker_.CalledOnValidThread());
40 DCHECK(!IsWatching()); 45 DCHECK(!IsWatching());
41 DCHECK(!callback.is_null()); 46 DCHECK(!callback.is_null());
42 47
43 callback_ = callback; 48 callback_ = callback;
44 handle_ = handle; 49 handle_ = handle;
45 MojoResult result = MojoWatch(handle_.value(), signals, 50 MojoResult result =
46 &Watcher::CallOnHandleReady, 51 MojoRegisterWatcher(handle_.value(), signals, &Watcher::CallOnHandleReady,
47 reinterpret_cast<uintptr_t>(this)); 52 reinterpret_cast<uintptr_t>(this));
48 if (result != MOJO_RESULT_OK) { 53 if (result != MOJO_RESULT_OK) {
49 handle_.set_value(kInvalidHandleValue); 54 handle_.set_value(kInvalidHandleValue);
50 callback_.Reset(); 55 callback_.Reset();
51 DCHECK(result == MOJO_RESULT_FAILED_PRECONDITION || 56 DCHECK(result == MOJO_RESULT_FAILED_PRECONDITION ||
52 result == MOJO_RESULT_INVALID_ARGUMENT); 57 result == MOJO_RESULT_INVALID_ARGUMENT);
53 return result; 58 return result;
54 } 59 }
55 60
61 if (arming_policy_ == ArmingPolicy::AUTOMATIC)
62 ArmOrNotify();
63
56 return MOJO_RESULT_OK; 64 return MOJO_RESULT_OK;
57 } 65 }
58 66
59 void Watcher::Cancel() { 67 void Watcher::Cancel() {
60 DCHECK(thread_checker_.CalledOnValidThread()); 68 DCHECK(thread_checker_.CalledOnValidThread());
61 69
62 // The watch may have already been cancelled if the handle was closed. 70 // The watcher may have already been cancelled if the handle was closed.
63 if (!handle_.is_valid()) 71 if (!handle_.is_valid())
64 return; 72 return;
65 73
66 MojoResult result = 74 MojoResult result =
67 MojoCancelWatch(handle_.value(), reinterpret_cast<uintptr_t>(this)); 75 MojoUnregisterWatcher(handle_.value(), reinterpret_cast<uintptr_t>(this));
68 // |result| may be MOJO_RESULT_INVALID_ARGUMENT if |handle_| has closed, but 76 // |result| may be MOJO_RESULT_INVALID_ARGUMENT if |handle_| has closed, but
69 // OnHandleReady has not yet been called. 77 // OnHandleReady has not yet been called.
70 DCHECK(result == MOJO_RESULT_INVALID_ARGUMENT || result == MOJO_RESULT_OK); 78 DCHECK(result == MOJO_RESULT_INVALID_ARGUMENT || result == MOJO_RESULT_OK);
71 handle_.set_value(kInvalidHandleValue); 79 handle_.set_value(kInvalidHandleValue);
72 callback_.Reset(); 80 callback_.Reset();
73 } 81 }
74 82
83 MojoResult Watcher::Arm() {
84 DCHECK(thread_checker_.CalledOnValidThread());
85 return MojoArmWatcher(handle_.value(), reinterpret_cast<uintptr_t>(this));
86 }
87
88 void Watcher::ArmOrNotify() {
89 DCHECK(thread_checker_.CalledOnValidThread());
90
91 // Already cancelled, nothing to do.
92 if (!IsWatching())
93 return;
94
95 MojoResult result = Arm();
96 switch (result) {
97 case MOJO_RESULT_OK:
98 // We're armed. Nothing else to do.
99 return;
100 case MOJO_RESULT_ALREADY_EXISTS:
101 // Signals are already satisfied. We post a notification of success.
102 result = MOJO_RESULT_OK;
103 break;
104 case MOJO_RESULT_FAILED_PRECONDITION:
105 // Signals are unsatisfiable. We post a notification to indicate this.
106 break;
107 default:
108 NOTREACHED();
109 return;
110 }
111
112 task_runner_->PostTask(
113 FROM_HERE, base::Bind(&Watcher::OnHandleReady, weak_self_, result));
114 }
115
75 void Watcher::OnHandleReady(MojoResult result) { 116 void Watcher::OnHandleReady(MojoResult result) {
76 DCHECK(thread_checker_.CalledOnValidThread()); 117 DCHECK(thread_checker_.CalledOnValidThread());
77 118
78 ReadyCallback callback = callback_; 119 ReadyCallback callback = callback_;
79 if (result == MOJO_RESULT_CANCELLED) { 120 if (result == MOJO_RESULT_CANCELLED) {
80 handle_.set_value(kInvalidHandleValue); 121 handle_.set_value(kInvalidHandleValue);
81 callback_.Reset(); 122 callback_.Reset();
82 } 123 }
83 124
84 // NOTE: It's legal for |callback| to delete |this|. 125 // NOTE: It's legal for |callback| to delete |this|.
85 if (!callback.is_null()) { 126 if (!callback.is_null()) {
86 TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION event(heap_profiler_tag_); 127 TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION event(heap_profiler_tag_);
128
129 bool was_deleted = false;
130 was_deleted_flag_ = &was_deleted;
87 callback.Run(result); 131 callback.Run(result);
132 if (was_deleted)
133 return;
134 was_deleted_flag_ = nullptr;
135 if (arming_policy_ == ArmingPolicy::AUTOMATIC && IsWatching())
136 ArmOrNotify();
88 } 137 }
89 } 138 }
90 139
140 void Watcher::OnHandleReadyFromAnyThread(MojoResult result,
141 MojoWatchNotificationFlags flags) {
142 if ((flags & MOJO_WATCH_NOTIFICATION_FLAG_FROM_SYSTEM) &&
143 task_runner_->RunsTasksOnCurrentThread() && is_default_task_runner_) {
144 // System notifications will trigger from the task runner passed to
145 // mojo::edk::InitIPCSupport(). In Chrome this happens to always be the
146 // default task runner for the IO thread.
147 OnHandleReady(result);
148 } else {
149 task_runner_->PostTask(
150 FROM_HERE, base::Bind(&Watcher::OnHandleReady, weak_self_, result));
151 }
152 }
153
91 // static 154 // static
92 void Watcher::CallOnHandleReady(uintptr_t context, 155 void Watcher::CallOnHandleReady(uintptr_t context,
93 MojoResult result, 156 MojoResult result,
94 MojoHandleSignalsState signals_state, 157 MojoHandleSignalsState signals_state,
95 MojoWatchNotificationFlags flags) { 158 MojoWatchNotificationFlags flags) {
96 // NOTE: It is safe to assume the Watcher still exists because this callback 159 // NOTE: It is safe to assume the Watcher still exists because this callback
97 // will never be run after the Watcher's destructor. 160 // will never be run after the Watcher's destructor.
98 // 161 //
99 // TODO: Maybe we should also expose |signals_state| through the Watcher API. 162 // TODO: Maybe we should also expose |signals_state| through the Watcher API.
100 // Current HandleWatcher users have no need for it, so it's omitted here. 163 // Current users have no need for it, so it's omitted here.
101 Watcher* watcher = reinterpret_cast<Watcher*>(context); 164 auto* watcher = reinterpret_cast<Watcher*>(context);
102 165 watcher->OnHandleReadyFromAnyThread(result, flags);
103 if ((flags & MOJO_WATCH_NOTIFICATION_FLAG_FROM_SYSTEM) &&
104 watcher->task_runner_->RunsTasksOnCurrentThread() &&
105 watcher->is_default_task_runner_) {
106 // System notifications will trigger from the task runner passed to
107 // mojo::edk::InitIPCSupport(). In Chrome this happens to always be the
108 // default task runner for the IO thread.
109 watcher->OnHandleReady(result);
110 } else {
111 watcher->task_runner_->PostTask(
112 FROM_HERE,
113 base::Bind(&Watcher::OnHandleReady, watcher->weak_self_, result));
114 }
115 } 166 }
116 167
117 } // namespace mojo 168 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698