OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ui/accelerated_widget_mac/window_resize_helper_mac.h" | 5 #include "ui/accelerated_widget_mac/window_resize_helper_mac.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <list> | 9 #include <list> |
| 10 #include <utility> |
10 | 11 |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/callback_helpers.h" |
13 #include "base/macros.h" | 15 #include "base/macros.h" |
14 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
15 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
16 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
17 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
18 | 20 |
19 namespace ui { | 21 namespace ui { |
20 namespace { | 22 namespace { |
21 | 23 |
22 class WrappedTask; | 24 class WrappedTask; |
23 class PumpableTaskRunner; | 25 class PumpableTaskRunner; |
24 typedef std::list<WrappedTask*> WrappedTaskQueue; | 26 typedef std::list<WrappedTask*> WrappedTaskQueue; |
25 typedef base::Callback<void(base::WaitableEvent*, base::TimeDelta)> | 27 typedef base::Callback<void(base::WaitableEvent*, base::TimeDelta)> |
26 EventTimedWaitCallback; | 28 EventTimedWaitCallback; |
27 | 29 |
28 // A wrapper for IPCs and tasks that we may potentially execute in | 30 // A wrapper for IPCs and tasks that we may potentially execute in |
29 // WaitForSingleTaskToRun. Because these tasks are sent to two places to run, | 31 // WaitForSingleTaskToRun. Because these tasks are sent to two places to run, |
30 // we to wrap them in this structure and track whether or not they have run | 32 // we to wrap them in this structure and track whether or not they have run |
31 // yet, to avoid running them twice. | 33 // yet, to avoid running them twice. |
32 class WrappedTask { | 34 class WrappedTask { |
33 public: | 35 public: |
34 WrappedTask(const base::Closure& closure, base::TimeDelta delay); | 36 WrappedTask(base::Closure closure, base::TimeDelta delay); |
35 ~WrappedTask(); | 37 ~WrappedTask(); |
36 bool ShouldRunBefore(const WrappedTask& other); | 38 bool ShouldRunBefore(const WrappedTask& other); |
37 void Run(); | 39 void Run(); |
38 void AddToTaskRunnerQueue(PumpableTaskRunner* pumpable_task_runner); | 40 void AddToTaskRunnerQueue(PumpableTaskRunner* pumpable_task_runner); |
39 void RemoveFromTaskRunnerQueue(); | 41 void RemoveFromTaskRunnerQueue(); |
40 const base::TimeTicks& can_run_time() const { return can_run_time_; } | 42 const base::TimeTicks& can_run_time() const { return can_run_time_; } |
41 | 43 |
42 private: | 44 private: |
43 base::Closure closure_; | 45 base::Closure closure_; |
44 base::TimeTicks can_run_time_; | 46 base::TimeTicks can_run_time_; |
(...skipping 21 matching lines...) Expand all Loading... |
66 // Enqueue WrappedTask and post it to |target_task_runner_|. | 68 // Enqueue WrappedTask and post it to |target_task_runner_|. |
67 bool EnqueueAndPostWrappedTask(const tracked_objects::Location& from_here, | 69 bool EnqueueAndPostWrappedTask(const tracked_objects::Location& from_here, |
68 WrappedTask* task, | 70 WrappedTask* task, |
69 base::TimeDelta delay); | 71 base::TimeDelta delay); |
70 | 72 |
71 // Wait at most |max_delay| to run an enqueued task. | 73 // Wait at most |max_delay| to run an enqueued task. |
72 bool WaitForSingleWrappedTaskToRun(const base::TimeDelta& max_delay); | 74 bool WaitForSingleWrappedTaskToRun(const base::TimeDelta& max_delay); |
73 | 75 |
74 // base::SingleThreadTaskRunner implementation: | 76 // base::SingleThreadTaskRunner implementation: |
75 bool PostDelayedTask(const tracked_objects::Location& from_here, | 77 bool PostDelayedTask(const tracked_objects::Location& from_here, |
76 const base::Closure& task, | 78 base::Closure task, |
77 base::TimeDelta delay) override; | 79 base::TimeDelta delay) override; |
78 | 80 |
79 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 81 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
80 const base::Closure& task, | 82 base::Closure task, |
81 base::TimeDelta delay) override; | 83 base::TimeDelta delay) override; |
82 | 84 |
83 bool RunsTasksOnCurrentThread() const override; | 85 bool RunsTasksOnCurrentThread() const override; |
84 | 86 |
85 private: | 87 private: |
86 friend class WrappedTask; | 88 friend class WrappedTask; |
87 | 89 |
88 ~PumpableTaskRunner() override; | 90 ~PumpableTaskRunner() override; |
89 | 91 |
90 // A queue of live messages. Must hold |task_queue_lock_| to access. Tasks | 92 // A queue of live messages. Must hold |task_queue_lock_| to access. Tasks |
(...skipping 15 matching lines...) Expand all Loading... |
106 | 108 |
107 DISALLOW_COPY_AND_ASSIGN(PumpableTaskRunner); | 109 DISALLOW_COPY_AND_ASSIGN(PumpableTaskRunner); |
108 }; | 110 }; |
109 | 111 |
110 base::LazyInstance<WindowResizeHelperMac>::Leaky g_window_resize_helper = | 112 base::LazyInstance<WindowResizeHelperMac>::Leaky g_window_resize_helper = |
111 LAZY_INSTANCE_INITIALIZER; | 113 LAZY_INSTANCE_INITIALIZER; |
112 | 114 |
113 //////////////////////////////////////////////////////////////////////////////// | 115 //////////////////////////////////////////////////////////////////////////////// |
114 // WrappedTask | 116 // WrappedTask |
115 | 117 |
116 WrappedTask::WrappedTask(const base::Closure& closure, base::TimeDelta delay) | 118 WrappedTask::WrappedTask(base::Closure closure, base::TimeDelta delay) |
117 : closure_(closure), | 119 : closure_(std::move(closure)), |
118 can_run_time_(base::TimeTicks::Now() + delay), | 120 can_run_time_(base::TimeTicks::Now() + delay), |
119 has_run_(false), | 121 has_run_(false), |
120 sequence_number_(0) {} | 122 sequence_number_(0) {} |
121 | 123 |
122 WrappedTask::~WrappedTask() { | 124 WrappedTask::~WrappedTask() { |
123 RemoveFromTaskRunnerQueue(); | 125 RemoveFromTaskRunnerQueue(); |
124 } | 126 } |
125 | 127 |
126 bool WrappedTask::ShouldRunBefore(const WrappedTask& other) { | 128 bool WrappedTask::ShouldRunBefore(const WrappedTask& other) { |
127 if (can_run_time_ < other.can_run_time_) | 129 if (can_run_time_ < other.can_run_time_) |
128 return true; | 130 return true; |
129 if (can_run_time_ > other.can_run_time_) | 131 if (can_run_time_ > other.can_run_time_) |
130 return false; | 132 return false; |
131 if (sequence_number_ < other.sequence_number_) | 133 if (sequence_number_ < other.sequence_number_) |
132 return true; | 134 return true; |
133 if (sequence_number_ > other.sequence_number_) | 135 if (sequence_number_ > other.sequence_number_) |
134 return false; | 136 return false; |
135 // Sequence numbers are unique, so this should never happen. | 137 // Sequence numbers are unique, so this should never happen. |
136 NOTREACHED(); | 138 NOTREACHED(); |
137 return false; | 139 return false; |
138 } | 140 } |
139 | 141 |
140 void WrappedTask::Run() { | 142 void WrappedTask::Run() { |
141 if (has_run_) | 143 if (has_run_) |
142 return; | 144 return; |
143 RemoveFromTaskRunnerQueue(); | 145 RemoveFromTaskRunnerQueue(); |
144 has_run_ = true; | 146 has_run_ = true; |
145 closure_.Run(); | 147 std::move(closure_).Run(); |
146 } | 148 } |
147 | 149 |
148 void WrappedTask::AddToTaskRunnerQueue( | 150 void WrappedTask::AddToTaskRunnerQueue( |
149 PumpableTaskRunner* pumpable_task_runner) { | 151 PumpableTaskRunner* pumpable_task_runner) { |
150 pumpable_task_runner_ = pumpable_task_runner; | 152 pumpable_task_runner_ = pumpable_task_runner; |
151 base::AutoLock lock(pumpable_task_runner_->task_queue_lock_); | 153 base::AutoLock lock(pumpable_task_runner_->task_queue_lock_); |
152 static uint64_t last_sequence_number = 0; | 154 static uint64_t last_sequence_number = 0; |
153 last_sequence_number += 1; | 155 last_sequence_number += 1; |
154 sequence_number_ = last_sequence_number; | 156 sequence_number_ = last_sequence_number; |
155 iterator_ = pumpable_task_runner_->task_queue_.insert( | 157 iterator_ = pumpable_task_runner_->task_queue_.insert( |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 | 254 |
253 return target_task_runner_->PostDelayedTask( | 255 return target_task_runner_->PostDelayedTask( |
254 from_here, base::Bind(&WrappedTask::Run, base::Owned(task)), delay); | 256 from_here, base::Bind(&WrappedTask::Run, base::Owned(task)), delay); |
255 } | 257 } |
256 | 258 |
257 //////////////////////////////////////////////////////////////////////////////// | 259 //////////////////////////////////////////////////////////////////////////////// |
258 // PumpableTaskRunner, base::SingleThreadTaskRunner implementation: | 260 // PumpableTaskRunner, base::SingleThreadTaskRunner implementation: |
259 | 261 |
260 bool PumpableTaskRunner::PostDelayedTask( | 262 bool PumpableTaskRunner::PostDelayedTask( |
261 const tracked_objects::Location& from_here, | 263 const tracked_objects::Location& from_here, |
262 const base::Closure& task, | 264 base::Closure task, |
263 base::TimeDelta delay) { | 265 base::TimeDelta delay) { |
264 return EnqueueAndPostWrappedTask(from_here, new WrappedTask(task, delay), | 266 return EnqueueAndPostWrappedTask( |
265 delay); | 267 from_here, new WrappedTask(std::move(task), delay), delay); |
266 } | 268 } |
267 | 269 |
268 bool PumpableTaskRunner::PostNonNestableDelayedTask( | 270 bool PumpableTaskRunner::PostNonNestableDelayedTask( |
269 const tracked_objects::Location& from_here, | 271 const tracked_objects::Location& from_here, |
270 const base::Closure& task, | 272 base::Closure task, |
271 base::TimeDelta delay) { | 273 base::TimeDelta delay) { |
272 // The correctness of non-nestable events hasn't been proven for this | 274 // The correctness of non-nestable events hasn't been proven for this |
273 // structure. | 275 // structure. |
274 NOTREACHED(); | 276 NOTREACHED(); |
275 return false; | 277 return false; |
276 } | 278 } |
277 | 279 |
278 bool PumpableTaskRunner::RunsTasksOnCurrentThread() const { | 280 bool PumpableTaskRunner::RunsTasksOnCurrentThread() const { |
279 return target_task_runner_->RunsTasksOnCurrentThread(); | 281 return target_task_runner_->RunsTasksOnCurrentThread(); |
280 } | 282 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 WindowResizeHelperMac::WindowResizeHelperMac() {} | 319 WindowResizeHelperMac::WindowResizeHelperMac() {} |
318 WindowResizeHelperMac::~WindowResizeHelperMac() {} | 320 WindowResizeHelperMac::~WindowResizeHelperMac() {} |
319 | 321 |
320 void WindowResizeHelperMac::EventTimedWait(base::WaitableEvent* event, | 322 void WindowResizeHelperMac::EventTimedWait(base::WaitableEvent* event, |
321 base::TimeDelta delay) { | 323 base::TimeDelta delay) { |
322 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 324 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
323 event->TimedWait(delay); | 325 event->TimedWait(delay); |
324 } | 326 } |
325 | 327 |
326 } // namespace ui | 328 } // namespace ui |
OLD | NEW |