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

Side by Side Diff: cc/tiles/image_controller_unittest.cc

Issue 2726523002: Pass Callback to TaskRunner by value and consume it on invocation (1) (Closed)
Patch Set: s/base::ResetAndReturn/std::move/ 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 "cc/tiles/image_controller.h" 5 #include "cc/tiles/image_controller.h"
6
7 #include <utility>
8
6 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback_helpers.h"
7 #include "base/optional.h" 11 #include "base/optional.h"
8 #include "base/run_loop.h" 12 #include "base/run_loop.h"
9 #include "base/test/test_simple_task_runner.h" 13 #include "base/test/test_simple_task_runner.h"
10 #include "base/threading/sequenced_task_runner_handle.h" 14 #include "base/threading/sequenced_task_runner_handle.h"
11 #include "base/threading/thread_checker_impl.h" 15 #include "base/threading/thread_checker_impl.h"
12 #include "cc/test/skia_common.h" 16 #include "cc/test/skia_common.h"
13 #include "cc/tiles/image_decode_cache.h" 17 #include "cc/tiles/image_decode_cache.h"
14 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
15 19
16 namespace cc { 20 namespace cc {
(...skipping 10 matching lines...) Expand all
27 { 31 {
28 base::AutoLock hold(lock_); 32 base::AutoLock hold(lock_);
29 if (shutdown_) 33 if (shutdown_)
30 break; 34 break;
31 35
32 if (queue_.empty()) { 36 if (queue_.empty()) {
33 condition_.Wait(); 37 condition_.Wait();
34 continue; 38 continue;
35 } 39 }
36 40
37 task = queue_.front(); 41 task = std::move(queue_.front());
38 queue_.erase(queue_.begin()); 42 queue_.erase(queue_.begin());
39 } 43 }
40 task.Run(); 44 std::move(task).Run();
41 } 45 }
42 } 46 }
43 47
44 void Shutdown() { 48 void Shutdown() {
45 base::AutoLock hold(lock_); 49 base::AutoLock hold(lock_);
46 shutdown_ = true; 50 shutdown_ = true;
47 condition_.Signal(); 51 condition_.Signal();
48 } 52 }
49 53
50 void PostTask(const base::Closure& task) { 54 void PostTask(base::Closure task) {
51 base::AutoLock hold(lock_); 55 base::AutoLock hold(lock_);
52 queue_.push_back(task); 56 queue_.push_back(std::move(task));
53 condition_.Signal(); 57 condition_.Signal();
54 } 58 }
55 59
56 private: 60 private:
57 base::Lock lock_; 61 base::Lock lock_;
58 base::ConditionVariable condition_; 62 base::ConditionVariable condition_;
59 std::vector<base::Closure> queue_; 63 std::vector<base::Closure> queue_;
60 bool shutdown_ = false; 64 bool shutdown_ = false;
61 }; 65 };
62 66
63 class WorkerTaskRunner : public base::SequencedTaskRunner { 67 class WorkerTaskRunner : public base::SequencedTaskRunner {
64 public: 68 public:
65 WorkerTaskRunner() { thread_.Start(); } 69 WorkerTaskRunner() { thread_.Start(); }
66 70
67 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, 71 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
68 const base::Closure& task, 72 base::Closure task,
69 base::TimeDelta delay) override { 73 base::TimeDelta delay) override {
70 return PostDelayedTask(from_here, task, delay); 74 return PostDelayedTask(from_here, std::move(task), delay);
71 } 75 }
72 76
73 bool PostDelayedTask(const tracked_objects::Location& from_here, 77 bool PostDelayedTask(const tracked_objects::Location& from_here,
74 const base::Closure& task, 78 base::Closure task,
75 base::TimeDelta delay) override { 79 base::TimeDelta delay) override {
76 thread_.PostTask(task); 80 thread_.PostTask(std::move(task));
77 return true; 81 return true;
78 } 82 }
79 83
80 bool RunsTasksOnCurrentThread() const override { return false; } 84 bool RunsTasksOnCurrentThread() const override { return false; }
81 85
82 protected: 86 protected:
83 ~WorkerTaskRunner() override { 87 ~WorkerTaskRunner() override {
84 thread_.Shutdown(); 88 thread_.Shutdown();
85 thread_.Join(); 89 thread_.Join();
86 } 90 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 135
132 private: 136 private:
133 int number_of_refs_ = 0; 137 int number_of_refs_ = 0;
134 scoped_refptr<TileTask> task_to_use_; 138 scoped_refptr<TileTask> task_to_use_;
135 }; 139 };
136 140
137 // A simple class that can receive decode callbacks. 141 // A simple class that can receive decode callbacks.
138 class DecodeClient { 142 class DecodeClient {
139 public: 143 public:
140 DecodeClient() {} 144 DecodeClient() {}
141 void Callback(const base::Closure& quit_closure, 145 void Callback(base::Closure quit_closure,
142 ImageController::ImageDecodeRequestId id, 146 ImageController::ImageDecodeRequestId id,
143 ImageController::ImageDecodeResult result) { 147 ImageController::ImageDecodeResult result) {
144 id_ = id; 148 id_ = id;
145 result_ = result; 149 result_ = result;
146 quit_closure.Run(); 150 std::move(quit_closure).Run();
147 } 151 }
148 152
149 ImageController::ImageDecodeRequestId id() { return id_; } 153 ImageController::ImageDecodeRequestId id() { return id_; }
150 ImageController::ImageDecodeResult result() { return result_; } 154 ImageController::ImageDecodeResult result() { return result_; }
151 155
152 private: 156 private:
153 ImageController::ImageDecodeRequestId id_ = 0; 157 ImageController::ImageDecodeRequestId id_ = 0;
154 ImageController::ImageDecodeResult result_ = 158 ImageController::ImageDecodeResult result_ =
155 ImageController::ImageDecodeResult::FAILURE; 159 ImageController::ImageDecodeResult::FAILURE;
156 }; 160 };
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 decode_client2.result()); 587 decode_client2.result());
584 588
585 // Reset the controller since the order of destruction is wrong in this test 589 // Reset the controller since the order of destruction is wrong in this test
586 // (|other_cache| should outlive the controller. This is normally done via 590 // (|other_cache| should outlive the controller. This is normally done via
587 // SetImageDecodeCache(nullptr) or it can be done in the dtor of the cache.) 591 // SetImageDecodeCache(nullptr) or it can be done in the dtor of the cache.)
588 ResetController(); 592 ResetController();
589 } 593 }
590 594
591 } // namespace 595 } // namespace
592 } // namespace cc 596 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698