OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "base/threading/simple_thread.h" | 10 #include "base/threading/simple_thread.h" |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 CHECK(thread_checker_->CalledOnValidThread()); | 113 CHECK(thread_checker_->CalledOnValidThread()); |
114 if (!completion_task_run_count_) | 114 if (!completion_task_run_count_) |
115 completion_task_result_ = result; | 115 completion_task_result_ = result; |
116 ++completion_task_run_count_; | 116 ++completion_task_run_count_; |
117 } | 117 } |
118 void WaitUntilCompleted() { callback_did_run_event_.Wait(); } | 118 void WaitUntilCompleted() { callback_did_run_event_.Wait(); } |
119 unsigned run_count() { return run_count_; } | 119 unsigned run_count() { return run_count_; } |
120 int32_t result() { return result_; } | 120 int32_t result() { return result_; } |
121 unsigned completion_task_run_count() { return completion_task_run_count_; } | 121 unsigned completion_task_run_count() { return completion_task_run_count_; } |
122 int32_t completion_task_result() { return completion_task_result_; } | 122 int32_t completion_task_result() { return completion_task_result_; } |
123 | |
124 private: | 123 private: |
125 unsigned run_count_; | 124 unsigned run_count_; |
126 int32_t result_; | 125 int32_t result_; |
127 unsigned completion_task_run_count_; | 126 unsigned completion_task_run_count_; |
128 int32_t completion_task_result_; | 127 int32_t completion_task_result_; |
129 // Weak; owned by the creator of CallbackRunInfo. | 128 // Weak; owned by the creator of CallbackRunInfo. |
130 base::ThreadChecker* thread_checker_; | 129 base::ThreadChecker* thread_checker_; |
131 | 130 |
132 base::WaitableEvent callback_did_run_event_; | 131 base::WaitableEvent callback_did_run_event_; |
133 }; | 132 }; |
134 | 133 |
135 void TestCallback(void* user_data, int32_t result) { | 134 void TestCallback(void* user_data, int32_t result) { |
136 CallbackRunInfo* info = static_cast<CallbackRunInfo*>(user_data); | 135 CallbackRunInfo* info = static_cast<CallbackRunInfo*>(user_data); |
137 info->CallbackDidRun(result); | 136 info->CallbackDidRun(result); |
138 } | 137 } |
139 | 138 |
140 // CallbackShutdownTest -------------------------------------------------------- | 139 // CallbackShutdownTest -------------------------------------------------------- |
141 | 140 |
142 class CallbackShutdownTest : public TrackedCallbackTest { | 141 class CallbackShutdownTest : public TrackedCallbackTest { |
143 public: | 142 public: |
144 CallbackShutdownTest() | 143 CallbackShutdownTest() : info_did_run_(&thread_checker_), |
145 : info_did_run_(&thread_checker_), | 144 info_did_abort_(&thread_checker_), |
146 info_did_abort_(&thread_checker_), | 145 info_didnt_run_(&thread_checker_) {} |
147 info_didnt_run_(&thread_checker_) {} | |
148 | 146 |
149 // Cases: | 147 // Cases: |
150 // (1) A callback which is run (so shouldn't be aborted on shutdown). | 148 // (1) A callback which is run (so shouldn't be aborted on shutdown). |
151 // (2) A callback which is aborted (so shouldn't be aborted on shutdown). | 149 // (2) A callback which is aborted (so shouldn't be aborted on shutdown). |
152 // (3) A callback which isn't run (so should be aborted on shutdown). | 150 // (3) A callback which isn't run (so should be aborted on shutdown). |
153 CallbackRunInfo& info_did_run() { return info_did_run_; } // (1) | 151 CallbackRunInfo& info_did_run() { return info_did_run_; } // (1) |
154 CallbackRunInfo& info_did_abort() { return info_did_abort_; } // (2) | 152 CallbackRunInfo& info_did_abort() { return info_did_abort_; } // (2) |
155 CallbackRunInfo& info_didnt_run() { return info_didnt_run_; } // (3) | 153 CallbackRunInfo& info_didnt_run() { return info_didnt_run_; } // (3) |
156 | 154 |
157 private: | 155 private: |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 ProxyAutoLock acquire; | 238 ProxyAutoLock acquire; |
241 reference_holder_ = 0; | 239 reference_holder_ = 0; |
242 } | 240 } |
243 | 241 |
244 // Create the test callbacks on a background thread, so that we can verify | 242 // Create the test callbacks on a background thread, so that we can verify |
245 // they are run on the same thread where they were created. | 243 // they are run on the same thread where they were created. |
246 void CreateCallbacksOnLoop(MessageLoopResource* loop_resource) { | 244 void CreateCallbacksOnLoop(MessageLoopResource* loop_resource) { |
247 ProxyAutoLock acquire; | 245 ProxyAutoLock acquire; |
248 // |thread_checker_| will bind to the background thread. | 246 // |thread_checker_| will bind to the background thread. |
249 thread_checker_.DetachFromThread(); | 247 thread_checker_.DetachFromThread(); |
250 loop_resource->message_loop_proxy()->PostTask( | 248 loop_resource->message_loop_proxy()->PostTask(FROM_HERE, |
251 FROM_HERE, RunWhileLocked(base::Bind( | 249 RunWhileLocked( |
252 &CallbackMockResource::CreateCallbacks, this))); | 250 base::Bind(&CallbackMockResource::CreateCallbacks, this))); |
253 } | 251 } |
254 | 252 |
255 int32_t CompletionTask(CallbackRunInfo* info, int32_t result) { | 253 int32_t CompletionTask(CallbackRunInfo* info, int32_t result) { |
256 // The completion task must run on the thread where the callback was | 254 // The completion task must run on the thread where the callback was |
257 // created, and must hold the proxy lock. | 255 // created, and must hold the proxy lock. |
258 CHECK(thread_checker_.CalledOnValidThread()); | 256 CHECK(thread_checker_.CalledOnValidThread()); |
259 ProxyLock::AssertAcquired(); | 257 ProxyLock::AssertAcquired(); |
260 | 258 |
261 // We should run before the callback. | 259 // We should run before the callback. |
262 CHECK_EQ(0U, info->run_count()); | 260 CHECK_EQ(0U, info->run_count()); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 // Later, when the callback runs, it will check that it was invoked on this | 341 // Later, when the callback runs, it will check that it was invoked on this |
344 // same thread. | 342 // same thread. |
345 CHECK(thread_checker_.CalledOnValidThread()); | 343 CHECK(thread_checker_.CalledOnValidThread()); |
346 | 344 |
347 callback_did_run_ = new TrackedCallback( | 345 callback_did_run_ = new TrackedCallback( |
348 this, PP_MakeCompletionCallback(&TestCallback, &info_did_run_)); | 346 this, PP_MakeCompletionCallback(&TestCallback, &info_did_run_)); |
349 | 347 |
350 // In order to test that the completion task can override the callback | 348 // In order to test that the completion task can override the callback |
351 // result, we need to test callbacks with and without a completion task. | 349 // result, we need to test callbacks with and without a completion task. |
352 callback_did_run_with_completion_task_ = new TrackedCallback( | 350 callback_did_run_with_completion_task_ = new TrackedCallback( |
353 this, PP_MakeCompletionCallback(&TestCallback, | 351 this, |
354 &info_did_run_with_completion_task_)); | 352 PP_MakeCompletionCallback(&TestCallback, |
| 353 &info_did_run_with_completion_task_)); |
355 callback_did_run_with_completion_task_->set_completion_task( | 354 callback_did_run_with_completion_task_->set_completion_task( |
356 Bind(&CallbackMockResource::CompletionTask, this, | 355 Bind(&CallbackMockResource::CompletionTask, |
| 356 this, |
357 &info_did_run_with_completion_task_)); | 357 &info_did_run_with_completion_task_)); |
358 | 358 |
359 callback_did_abort_ = new TrackedCallback( | 359 callback_did_abort_ = new TrackedCallback( |
360 this, PP_MakeCompletionCallback(&TestCallback, &info_did_abort_)); | 360 this, PP_MakeCompletionCallback(&TestCallback, &info_did_abort_)); |
361 callback_did_abort_->set_completion_task( | 361 callback_did_abort_->set_completion_task( |
362 Bind(&CallbackMockResource::CompletionTask, this, &info_did_abort_)); | 362 Bind(&CallbackMockResource::CompletionTask, this, &info_did_abort_)); |
363 | 363 |
364 callback_didnt_run_ = new TrackedCallback( | 364 callback_didnt_run_ = new TrackedCallback( |
365 this, PP_MakeCompletionCallback(&TestCallback, &info_didnt_run_)); | 365 this, PP_MakeCompletionCallback(&TestCallback, &info_didnt_run_)); |
366 callback_didnt_run_->set_completion_task( | 366 callback_didnt_run_->set_completion_task( |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 resource->ReleaseRef(); | 462 resource->ReleaseRef(); |
463 resource->CheckFinalState(); | 463 resource->CheckFinalState(); |
464 { | 464 { |
465 ProxyAutoLock lock; | 465 ProxyAutoLock lock; |
466 resource = nullptr; | 466 resource = nullptr; |
467 } | 467 } |
468 } | 468 } |
469 | 469 |
470 } // namespace proxy | 470 } // namespace proxy |
471 } // namespace ppapi | 471 } // namespace ppapi |
OLD | NEW |