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/threading/thread_task_runner_handle.h" |
10 #include "base/trace_event/heap_profiler.h" | 11 #include "base/trace_event/heap_profiler.h" |
11 #include "mojo/public/c/system/functions.h" | 12 #include "mojo/public/c/system/functions.h" |
12 | 13 |
13 namespace mojo { | 14 namespace mojo { |
14 | 15 |
15 Watcher::Watcher(const tracked_objects::Location& from_here, | 16 Watcher::Watcher(const tracked_objects::Location& from_here, |
16 scoped_refptr<base::SingleThreadTaskRunner> runner) | 17 scoped_refptr<base::SequencedTaskRunner> runner) |
17 : task_runner_(std::move(runner)), | 18 : task_runner_(std::move(runner)), |
18 is_default_task_runner_(task_runner_ == | 19 is_default_task_runner_(base::ThreadTaskRunnerHandle::IsSet() && |
19 base::ThreadTaskRunnerHandle::Get()), | 20 task_runner_ == |
| 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_->RunsTasksOnCurrentThread()); |
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(); |
29 } | 31 } |
30 | 32 |
31 bool Watcher::IsWatching() const { | 33 bool Watcher::IsWatching() const { |
32 DCHECK(thread_checker_.CalledOnValidThread()); | 34 DCHECK(sequence_checker_.CalledOnValidSequence()); |
33 return handle_.is_valid(); | 35 return handle_.is_valid(); |
34 } | 36 } |
35 | 37 |
36 MojoResult Watcher::Start(Handle handle, | 38 MojoResult Watcher::Start(Handle handle, |
37 MojoHandleSignals signals, | 39 MojoHandleSignals signals, |
38 const ReadyCallback& callback) { | 40 const ReadyCallback& callback) { |
39 DCHECK(thread_checker_.CalledOnValidThread()); | 41 DCHECK(sequence_checker_.CalledOnValidSequence()); |
40 DCHECK(!IsWatching()); | 42 DCHECK(!IsWatching()); |
41 DCHECK(!callback.is_null()); | 43 DCHECK(!callback.is_null()); |
42 | 44 |
43 callback_ = callback; | 45 callback_ = callback; |
44 handle_ = handle; | 46 handle_ = handle; |
45 MojoResult result = MojoWatch(handle_.value(), signals, | 47 MojoResult result = MojoWatch(handle_.value(), signals, |
46 &Watcher::CallOnHandleReady, | 48 &Watcher::CallOnHandleReady, |
47 reinterpret_cast<uintptr_t>(this)); | 49 reinterpret_cast<uintptr_t>(this)); |
48 if (result != MOJO_RESULT_OK) { | 50 if (result != MOJO_RESULT_OK) { |
49 handle_.set_value(kInvalidHandleValue); | 51 handle_.set_value(kInvalidHandleValue); |
50 callback_.Reset(); | 52 callback_.Reset(); |
51 DCHECK(result == MOJO_RESULT_FAILED_PRECONDITION || | 53 DCHECK(result == MOJO_RESULT_FAILED_PRECONDITION || |
52 result == MOJO_RESULT_INVALID_ARGUMENT); | 54 result == MOJO_RESULT_INVALID_ARGUMENT); |
53 return result; | 55 return result; |
54 } | 56 } |
55 | 57 |
56 return MOJO_RESULT_OK; | 58 return MOJO_RESULT_OK; |
57 } | 59 } |
58 | 60 |
59 void Watcher::Cancel() { | 61 void Watcher::Cancel() { |
60 DCHECK(thread_checker_.CalledOnValidThread()); | 62 DCHECK(sequence_checker_.CalledOnValidSequence()); |
61 | 63 |
62 // The watch may have already been cancelled if the handle was closed. | 64 // The watch may have already been cancelled if the handle was closed. |
63 if (!handle_.is_valid()) | 65 if (!handle_.is_valid()) |
64 return; | 66 return; |
65 | 67 |
66 MojoResult result = | 68 MojoResult result = |
67 MojoCancelWatch(handle_.value(), reinterpret_cast<uintptr_t>(this)); | 69 MojoCancelWatch(handle_.value(), reinterpret_cast<uintptr_t>(this)); |
68 // |result| may be MOJO_RESULT_INVALID_ARGUMENT if |handle_| has closed, but | 70 // |result| may be MOJO_RESULT_INVALID_ARGUMENT if |handle_| has closed, but |
69 // OnHandleReady has not yet been called. | 71 // OnHandleReady has not yet been called. |
70 DCHECK(result == MOJO_RESULT_INVALID_ARGUMENT || result == MOJO_RESULT_OK); | 72 DCHECK(result == MOJO_RESULT_INVALID_ARGUMENT || result == MOJO_RESULT_OK); |
71 handle_.set_value(kInvalidHandleValue); | 73 handle_.set_value(kInvalidHandleValue); |
72 callback_.Reset(); | 74 callback_.Reset(); |
73 } | 75 } |
74 | 76 |
75 void Watcher::OnHandleReady(MojoResult result) { | 77 void Watcher::OnHandleReady(MojoResult result) { |
76 DCHECK(thread_checker_.CalledOnValidThread()); | 78 DCHECK(sequence_checker_.CalledOnValidSequence()); |
77 | 79 |
78 ReadyCallback callback = callback_; | 80 ReadyCallback callback = callback_; |
79 if (result == MOJO_RESULT_CANCELLED) { | 81 if (result == MOJO_RESULT_CANCELLED) { |
80 handle_.set_value(kInvalidHandleValue); | 82 handle_.set_value(kInvalidHandleValue); |
81 callback_.Reset(); | 83 callback_.Reset(); |
82 } | 84 } |
83 | 85 |
84 // NOTE: It's legal for |callback| to delete |this|. | 86 // NOTE: It's legal for |callback| to delete |this|. |
85 if (!callback.is_null()) { | 87 if (!callback.is_null()) { |
86 TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION event(heap_profiler_tag_); | 88 TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION event(heap_profiler_tag_); |
(...skipping 21 matching lines...) Expand all Loading... |
108 // default task runner for the IO thread. | 110 // default task runner for the IO thread. |
109 watcher->OnHandleReady(result); | 111 watcher->OnHandleReady(result); |
110 } else { | 112 } else { |
111 watcher->task_runner_->PostTask( | 113 watcher->task_runner_->PostTask( |
112 FROM_HERE, | 114 FROM_HERE, |
113 base::Bind(&Watcher::OnHandleReady, watcher->weak_self_, result)); | 115 base::Bind(&Watcher::OnHandleReady, watcher->weak_self_, result)); |
114 } | 116 } |
115 } | 117 } |
116 | 118 |
117 } // namespace mojo | 119 } // namespace mojo |
OLD | NEW |