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 |
123 private: | 124 private: |
124 unsigned run_count_; | 125 unsigned run_count_; |
125 int32_t result_; | 126 int32_t result_; |
126 unsigned completion_task_run_count_; | 127 unsigned completion_task_run_count_; |
127 int32_t completion_task_result_; | 128 int32_t completion_task_result_; |
128 // Weak; owned by the creator of CallbackRunInfo. | 129 // Weak; owned by the creator of CallbackRunInfo. |
129 base::ThreadChecker* thread_checker_; | 130 base::ThreadChecker* thread_checker_; |
130 | 131 |
131 base::WaitableEvent callback_did_run_event_; | 132 base::WaitableEvent callback_did_run_event_; |
132 }; | 133 }; |
133 | 134 |
134 void TestCallback(void* user_data, int32_t result) { | 135 void TestCallback(void* user_data, int32_t result) { |
135 CallbackRunInfo* info = static_cast<CallbackRunInfo*>(user_data); | 136 CallbackRunInfo* info = static_cast<CallbackRunInfo*>(user_data); |
136 info->CallbackDidRun(result); | 137 info->CallbackDidRun(result); |
137 } | 138 } |
138 | 139 |
139 // CallbackShutdownTest -------------------------------------------------------- | 140 // CallbackShutdownTest -------------------------------------------------------- |
140 | 141 |
141 class CallbackShutdownTest : public TrackedCallbackTest { | 142 class CallbackShutdownTest : public TrackedCallbackTest { |
142 public: | 143 public: |
143 CallbackShutdownTest() : info_did_run_(&thread_checker_), | 144 CallbackShutdownTest() |
144 info_did_abort_(&thread_checker_), | 145 : info_did_run_(&thread_checker_), |
145 info_didnt_run_(&thread_checker_) {} | 146 info_did_abort_(&thread_checker_), |
| 147 info_didnt_run_(&thread_checker_) {} |
146 | 148 |
147 // Cases: | 149 // Cases: |
148 // (1) A callback which is run (so shouldn't be aborted on shutdown). | 150 // (1) A callback which is run (so shouldn't be aborted on shutdown). |
149 // (2) A callback which is aborted (so shouldn't be aborted on shutdown). | 151 // (2) A callback which is aborted (so shouldn't be aborted on shutdown). |
150 // (3) A callback which isn't run (so should be aborted on shutdown). | 152 // (3) A callback which isn't run (so should be aborted on shutdown). |
151 CallbackRunInfo& info_did_run() { return info_did_run_; } // (1) | 153 CallbackRunInfo& info_did_run() { return info_did_run_; } // (1) |
152 CallbackRunInfo& info_did_abort() { return info_did_abort_; } // (2) | 154 CallbackRunInfo& info_did_abort() { return info_did_abort_; } // (2) |
153 CallbackRunInfo& info_didnt_run() { return info_didnt_run_; } // (3) | 155 CallbackRunInfo& info_didnt_run() { return info_didnt_run_; } // (3) |
154 | 156 |
155 private: | 157 private: |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 ProxyAutoLock acquire; | 240 ProxyAutoLock acquire; |
239 reference_holder_ = 0; | 241 reference_holder_ = 0; |
240 } | 242 } |
241 | 243 |
242 // Create the test callbacks on a background thread, so that we can verify | 244 // Create the test callbacks on a background thread, so that we can verify |
243 // they are run on the same thread where they were created. | 245 // they are run on the same thread where they were created. |
244 void CreateCallbacksOnLoop(MessageLoopResource* loop_resource) { | 246 void CreateCallbacksOnLoop(MessageLoopResource* loop_resource) { |
245 ProxyAutoLock acquire; | 247 ProxyAutoLock acquire; |
246 // |thread_checker_| will bind to the background thread. | 248 // |thread_checker_| will bind to the background thread. |
247 thread_checker_.DetachFromThread(); | 249 thread_checker_.DetachFromThread(); |
248 loop_resource->message_loop_proxy()->PostTask(FROM_HERE, | 250 loop_resource->message_loop_proxy()->PostTask( |
249 RunWhileLocked( | 251 FROM_HERE, RunWhileLocked(base::Bind( |
250 base::Bind(&CallbackMockResource::CreateCallbacks, this))); | 252 &CallbackMockResource::CreateCallbacks, this))); |
251 } | 253 } |
252 | 254 |
253 int32_t CompletionTask(CallbackRunInfo* info, int32_t result) { | 255 int32_t CompletionTask(CallbackRunInfo* info, int32_t result) { |
254 // The completion task must run on the thread where the callback was | 256 // The completion task must run on the thread where the callback was |
255 // created, and must hold the proxy lock. | 257 // created, and must hold the proxy lock. |
256 CHECK(thread_checker_.CalledOnValidThread()); | 258 CHECK(thread_checker_.CalledOnValidThread()); |
257 ProxyLock::AssertAcquired(); | 259 ProxyLock::AssertAcquired(); |
258 | 260 |
259 // We should run before the callback. | 261 // We should run before the callback. |
260 CHECK_EQ(0U, info->run_count()); | 262 CHECK_EQ(0U, info->run_count()); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 // Later, when the callback runs, it will check that it was invoked on this | 343 // Later, when the callback runs, it will check that it was invoked on this |
342 // same thread. | 344 // same thread. |
343 CHECK(thread_checker_.CalledOnValidThread()); | 345 CHECK(thread_checker_.CalledOnValidThread()); |
344 | 346 |
345 callback_did_run_ = new TrackedCallback( | 347 callback_did_run_ = new TrackedCallback( |
346 this, PP_MakeCompletionCallback(&TestCallback, &info_did_run_)); | 348 this, PP_MakeCompletionCallback(&TestCallback, &info_did_run_)); |
347 | 349 |
348 // In order to test that the completion task can override the callback | 350 // In order to test that the completion task can override the callback |
349 // result, we need to test callbacks with and without a completion task. | 351 // result, we need to test callbacks with and without a completion task. |
350 callback_did_run_with_completion_task_ = new TrackedCallback( | 352 callback_did_run_with_completion_task_ = new TrackedCallback( |
351 this, | 353 this, PP_MakeCompletionCallback(&TestCallback, |
352 PP_MakeCompletionCallback(&TestCallback, | 354 &info_did_run_with_completion_task_)); |
353 &info_did_run_with_completion_task_)); | |
354 callback_did_run_with_completion_task_->set_completion_task( | 355 callback_did_run_with_completion_task_->set_completion_task( |
355 Bind(&CallbackMockResource::CompletionTask, | 356 Bind(&CallbackMockResource::CompletionTask, this, |
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 |