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

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

Issue 2725133002: Mojo: Armed Watchers (Closed)
Patch Set: rebase 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"
8 #include "base/location.h"
9 #include "base/macros.h"
10 #include "base/trace_event/heap_profiler.h"
11 #include "mojo/public/c/system/functions.h" 7 #include "mojo/public/c/system/functions.h"
12 8
13 namespace mojo { 9 namespace mojo {
14 10
15 Watcher::Watcher(const tracked_objects::Location& from_here, 11 MojoResult CreateWatcher(MojoWatcherCallback callback,
16 scoped_refptr<base::SingleThreadTaskRunner> runner) 12 ScopedWatcherHandle* watcher_handle) {
17 : task_runner_(std::move(runner)), 13 MojoHandle handle;
18 is_default_task_runner_(task_runner_ == 14 MojoResult rv = MojoCreateWatcher(callback, &handle);
19 base::ThreadTaskRunnerHandle::Get()), 15 if (rv == MOJO_RESULT_OK)
20 heap_profiler_tag_(from_here.file_name()), 16 watcher_handle->reset(WatcherHandle(handle));
21 weak_factory_(this) { 17 return rv;
22 DCHECK(task_runner_->BelongsToCurrentThread());
23 weak_self_ = weak_factory_.GetWeakPtr();
24 }
25
26 Watcher::~Watcher() {
27 if(IsWatching())
28 Cancel();
29 }
30
31 bool Watcher::IsWatching() const {
32 DCHECK(thread_checker_.CalledOnValidThread());
33 return handle_.is_valid();
34 }
35
36 MojoResult Watcher::Start(Handle handle,
37 MojoHandleSignals signals,
38 const ReadyCallback& callback) {
39 DCHECK(thread_checker_.CalledOnValidThread());
40 DCHECK(!IsWatching());
41 DCHECK(!callback.is_null());
42
43 callback_ = callback;
44 handle_ = handle;
45 MojoResult result = MojoWatch(handle_.value(), signals,
46 &Watcher::CallOnHandleReady,
47 reinterpret_cast<uintptr_t>(this));
48 if (result != MOJO_RESULT_OK) {
49 handle_.set_value(kInvalidHandleValue);
50 callback_.Reset();
51 DCHECK(result == MOJO_RESULT_FAILED_PRECONDITION ||
52 result == MOJO_RESULT_INVALID_ARGUMENT);
53 return result;
54 }
55
56 return MOJO_RESULT_OK;
57 }
58
59 void Watcher::Cancel() {
60 DCHECK(thread_checker_.CalledOnValidThread());
61
62 // The watch may have already been cancelled if the handle was closed.
63 if (!handle_.is_valid())
64 return;
65
66 MojoResult result =
67 MojoCancelWatch(handle_.value(), reinterpret_cast<uintptr_t>(this));
68 // |result| may be MOJO_RESULT_INVALID_ARGUMENT if |handle_| has closed, but
69 // OnHandleReady has not yet been called.
70 DCHECK(result == MOJO_RESULT_INVALID_ARGUMENT || result == MOJO_RESULT_OK);
71 handle_.set_value(kInvalidHandleValue);
72 callback_.Reset();
73 }
74
75 void Watcher::OnHandleReady(MojoResult result) {
76 DCHECK(thread_checker_.CalledOnValidThread());
77
78 ReadyCallback callback = callback_;
79 if (result == MOJO_RESULT_CANCELLED) {
80 handle_.set_value(kInvalidHandleValue);
81 callback_.Reset();
82 }
83
84 // NOTE: It's legal for |callback| to delete |this|.
85 if (!callback.is_null()) {
86 TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION event(heap_profiler_tag_);
87 callback.Run(result);
88 }
89 }
90
91 // static
92 void Watcher::CallOnHandleReady(uintptr_t context,
93 MojoResult result,
94 MojoHandleSignalsState signals_state,
95 MojoWatchNotificationFlags flags) {
96 // NOTE: It is safe to assume the Watcher still exists because this callback
97 // will never be run after the Watcher's destructor.
98 //
99 // 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.
101 Watcher* watcher = reinterpret_cast<Watcher*>(context);
102
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 } 18 }
116 19
117 } // namespace mojo 20 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698