| 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 "ppapi/shared_impl/thread_aware_callback.h" | 5 #include "ppapi/shared_impl/thread_aware_callback.h" |
| 6 | 6 |
| 7 #include <memory> |
| 8 |
| 7 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 8 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 9 #include "base/location.h" | 11 #include "base/location.h" |
| 10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 13 #include "ppapi/c/pp_errors.h" | 14 #include "ppapi/c/pp_errors.h" |
| 14 #include "ppapi/proxy/ppapi_proxy_test.h" | 15 #include "ppapi/proxy/ppapi_proxy_test.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 namespace ppapi { | 18 namespace ppapi { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 class TestParameter { | 22 class TestParameter { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 | 87 |
| 87 { | 88 { |
| 88 ProxyAutoLock auto_lock; | 89 ProxyAutoLock auto_lock; |
| 89 // We have to destroy it prior to the PluginGlobals instance held by the | 90 // We have to destroy it prior to the PluginGlobals instance held by the |
| 90 // base class. Otherwise it has a ref to Pepper message loop for the main | 91 // base class. Otherwise it has a ref to Pepper message loop for the main |
| 91 // thread and the PluginGlobals destructor will complain. | 92 // thread and the PluginGlobals destructor will complain. |
| 92 thiz->main_thread_callback_.reset(NULL); | 93 thiz->main_thread_callback_.reset(NULL); |
| 93 } | 94 } |
| 94 } | 95 } |
| 95 | 96 |
| 96 scoped_ptr<ThreadAwareCallback<CallbackFunc> > main_thread_callback_; | 97 std::unique_ptr<ThreadAwareCallback<CallbackFunc>> main_thread_callback_; |
| 97 bool main_thread_callback_called_; | 98 bool main_thread_callback_called_; |
| 98 }; | 99 }; |
| 99 | 100 |
| 100 // Test that when a ThreadAwareCallback instance is destroyed, pending tasks to | 101 // Test that when a ThreadAwareCallback instance is destroyed, pending tasks to |
| 101 // run the callback will be ignored. | 102 // run the callback will be ignored. |
| 102 class ThreadAwareCallbackAbortTest : public proxy::PluginProxyMultiThreadTest { | 103 class ThreadAwareCallbackAbortTest : public proxy::PluginProxyMultiThreadTest { |
| 103 public: | 104 public: |
| 104 ThreadAwareCallbackAbortTest() {} | 105 ThreadAwareCallbackAbortTest() {} |
| 105 ~ThreadAwareCallbackAbortTest() override {} | 106 ~ThreadAwareCallbackAbortTest() override {} |
| 106 | 107 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 135 static void MainThreadCallbackBody(ThreadAwareCallbackAbortTest* thiz) { | 136 static void MainThreadCallbackBody(ThreadAwareCallbackAbortTest* thiz) { |
| 136 // The callback should not be called. | 137 // The callback should not be called. |
| 137 ASSERT_TRUE(false); | 138 ASSERT_TRUE(false); |
| 138 } | 139 } |
| 139 | 140 |
| 140 void DeleteCallback() { | 141 void DeleteCallback() { |
| 141 ProxyAutoLock auto_lock; | 142 ProxyAutoLock auto_lock; |
| 142 main_thread_callback_.reset(NULL); | 143 main_thread_callback_.reset(NULL); |
| 143 } | 144 } |
| 144 | 145 |
| 145 scoped_ptr<ThreadAwareCallback<CallbackFunc> > main_thread_callback_; | 146 std::unique_ptr<ThreadAwareCallback<CallbackFunc>> main_thread_callback_; |
| 146 }; | 147 }; |
| 147 | 148 |
| 148 } // namespace | 149 } // namespace |
| 149 | 150 |
| 150 TEST_F(ThreadAwareCallbackTest, Basics) { | 151 TEST_F(ThreadAwareCallbackTest, Basics) { |
| 151 // ThreadAwareCallback should only be used when the proxy lock has been | 152 // ThreadAwareCallback should only be used when the proxy lock has been |
| 152 // acquired. | 153 // acquired. |
| 153 ProxyAutoLock auto_lock; | 154 ProxyAutoLock auto_lock; |
| 154 | 155 |
| 155 double double_arg = 0.0; | 156 double double_arg = 0.0; |
| 156 bool bool_arg = false; | 157 bool bool_arg = false; |
| 157 TestParameter object_arg; | 158 TestParameter object_arg; |
| 158 | 159 |
| 159 // Exercise all the template code. | 160 // Exercise all the template code. |
| 160 called_num = 0; | 161 called_num = 0; |
| 161 typedef void (*FuncType_0)(); | 162 typedef void (*FuncType_0)(); |
| 162 scoped_ptr<ThreadAwareCallback<FuncType_0> > callback_0( | 163 std::unique_ptr<ThreadAwareCallback<FuncType_0>> callback_0( |
| 163 ThreadAwareCallback<FuncType_0>::Create(TestCallback_0)); | 164 ThreadAwareCallback<FuncType_0>::Create(TestCallback_0)); |
| 164 callback_0->RunOnTargetThread(); | 165 callback_0->RunOnTargetThread(); |
| 165 | 166 |
| 166 typedef void (*FuncType_1)(int); | 167 typedef void (*FuncType_1)(int); |
| 167 scoped_ptr<ThreadAwareCallback<FuncType_1> > callback_1( | 168 std::unique_ptr<ThreadAwareCallback<FuncType_1>> callback_1( |
| 168 ThreadAwareCallback<FuncType_1>::Create(TestCallback_1)); | 169 ThreadAwareCallback<FuncType_1>::Create(TestCallback_1)); |
| 169 callback_1->RunOnTargetThread(1); | 170 callback_1->RunOnTargetThread(1); |
| 170 | 171 |
| 171 typedef void (*FuncType_2)(int, const double*); | 172 typedef void (*FuncType_2)(int, const double*); |
| 172 scoped_ptr<ThreadAwareCallback<FuncType_2> > callback_2( | 173 std::unique_ptr<ThreadAwareCallback<FuncType_2>> callback_2( |
| 173 ThreadAwareCallback<FuncType_2>::Create(TestCallback_2)); | 174 ThreadAwareCallback<FuncType_2>::Create(TestCallback_2)); |
| 174 callback_2->RunOnTargetThread(1, &double_arg); | 175 callback_2->RunOnTargetThread(1, &double_arg); |
| 175 | 176 |
| 176 typedef void (*FuncType_3)(int, const double*, bool*); | 177 typedef void (*FuncType_3)(int, const double*, bool*); |
| 177 scoped_ptr<ThreadAwareCallback<FuncType_3> > callback_3( | 178 std::unique_ptr<ThreadAwareCallback<FuncType_3>> callback_3( |
| 178 ThreadAwareCallback<FuncType_3>::Create(TestCallback_3)); | 179 ThreadAwareCallback<FuncType_3>::Create(TestCallback_3)); |
| 179 callback_3->RunOnTargetThread(1, &double_arg, &bool_arg); | 180 callback_3->RunOnTargetThread(1, &double_arg, &bool_arg); |
| 180 | 181 |
| 181 typedef void (*FuncType_4)(int, const double*, bool*, TestParameter); | 182 typedef void (*FuncType_4)(int, const double*, bool*, TestParameter); |
| 182 scoped_ptr<ThreadAwareCallback<FuncType_4> > callback_4( | 183 std::unique_ptr<ThreadAwareCallback<FuncType_4>> callback_4( |
| 183 ThreadAwareCallback<FuncType_4>::Create(TestCallback_4)); | 184 ThreadAwareCallback<FuncType_4>::Create(TestCallback_4)); |
| 184 callback_4->RunOnTargetThread(1, &double_arg, &bool_arg, object_arg); | 185 callback_4->RunOnTargetThread(1, &double_arg, &bool_arg, object_arg); |
| 185 | 186 |
| 186 typedef void (*FuncType_5)( | 187 typedef void (*FuncType_5)( |
| 187 int, const double*, bool*, TestParameter, const TestParameter&); | 188 int, const double*, bool*, TestParameter, const TestParameter&); |
| 188 scoped_ptr<ThreadAwareCallback<FuncType_5> > callback_5( | 189 std::unique_ptr<ThreadAwareCallback<FuncType_5>> callback_5( |
| 189 ThreadAwareCallback<FuncType_5>::Create(TestCallback_5)); | 190 ThreadAwareCallback<FuncType_5>::Create(TestCallback_5)); |
| 190 callback_5->RunOnTargetThread( | 191 callback_5->RunOnTargetThread( |
| 191 1, &double_arg, &bool_arg, object_arg, object_arg); | 192 1, &double_arg, &bool_arg, object_arg, object_arg); |
| 192 | 193 |
| 193 EXPECT_EQ(6, called_num); | 194 EXPECT_EQ(6, called_num); |
| 194 } | 195 } |
| 195 | 196 |
| 196 TEST_F(ThreadAwareCallbackMultiThreadTest, RunOnTargetThread) { RunTest(); } | 197 TEST_F(ThreadAwareCallbackMultiThreadTest, RunOnTargetThread) { RunTest(); } |
| 197 | 198 |
| 198 TEST_F(ThreadAwareCallbackAbortTest, NotRunIfAborted) { RunTest(); } | 199 TEST_F(ThreadAwareCallbackAbortTest, NotRunIfAborted) { RunTest(); } |
| 199 | 200 |
| 200 } // namespace ppapi | 201 } // namespace ppapi |
| OLD | NEW |