| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <stdint.h> | 5 #include <stdint.h> |
| 6 #include <utility> | 6 #include <utility> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/threading/sequenced_task_runner_handle.h" |
| 13 #include "base/threading/thread.h" |
| 12 #include "mojo/public/cpp/bindings/binding.h" | 14 #include "mojo/public/cpp/bindings/binding.h" |
| 13 #include "mojo/public/cpp/bindings/strong_binding.h" | 15 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 16 #include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h" |
| 14 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h" | 17 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h" |
| 15 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" | 18 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" |
| 16 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" | 19 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" |
| 17 #include "mojo/public/interfaces/bindings/tests/scoping.mojom.h" | 20 #include "mojo/public/interfaces/bindings/tests/scoping.mojom.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 22 |
| 20 namespace mojo { | 23 namespace mojo { |
| 21 namespace test { | 24 namespace test { |
| 22 namespace { | 25 namespace { |
| 23 | 26 |
| (...skipping 778 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 802 raw_proxy->Ping( | 805 raw_proxy->Ping( |
| 803 base::Bind([](sample::PingTestPtr ptr) {}, base::Passed(&ptr))); | 806 base::Bind([](sample::PingTestPtr ptr) {}, base::Passed(&ptr))); |
| 804 | 807 |
| 805 // Trigger an error on |ptr|. This will ultimately lead to the proxy's | 808 // Trigger an error on |ptr|. This will ultimately lead to the proxy's |
| 806 // response callbacks being destroyed, which will in turn lead to the proxy | 809 // response callbacks being destroyed, which will in turn lead to the proxy |
| 807 // being destroyed. This should not crash. | 810 // being destroyed. This should not crash. |
| 808 request.PassMessagePipe(); | 811 request.PassMessagePipe(); |
| 809 run_loop.Run(); | 812 run_loop.Run(); |
| 810 } | 813 } |
| 811 | 814 |
| 815 TEST_F(InterfacePtrTest, ThreadSafeInterfacePointer) { |
| 816 math::CalculatorPtr ptr; |
| 817 MathCalculatorImpl calc_impl(GetProxy(&ptr)); |
| 818 scoped_refptr<math::ThreadSafeCalculatorPtr> thread_safe_ptr = |
| 819 math::ThreadSafeCalculatorPtr::Create(std::move(ptr)); |
| 820 |
| 821 base::RunLoop run_loop; |
| 822 |
| 823 // Create and start the thread from where we'll call the interface pointer. |
| 824 base::Thread other_thread("service test thread"); |
| 825 other_thread.Start(); |
| 826 |
| 827 auto run_method = base::Bind( |
| 828 [](const scoped_refptr<base::TaskRunner>& main_task_runner, |
| 829 const base::Closure& quit_closure, |
| 830 const scoped_refptr<math::ThreadSafeCalculatorPtr>& thread_safe_ptr) { |
| 831 auto calc_callback = base::Bind( |
| 832 [](const scoped_refptr<base::TaskRunner>& main_task_runner, |
| 833 const base::Closure& quit_closure, |
| 834 base::PlatformThreadId thread_id, |
| 835 double result) { |
| 836 EXPECT_EQ(123, result); |
| 837 // Validate the callback is invoked on the calling thread. |
| 838 EXPECT_EQ(thread_id, base::PlatformThread::CurrentId()); |
| 839 // Notify the run_loop to quit. |
| 840 main_task_runner->PostTask(FROM_HERE, quit_closure); |
| 841 }); |
| 842 (*thread_safe_ptr)->Add( |
| 843 123, base::Bind(calc_callback, main_task_runner, quit_closure, |
| 844 base::PlatformThread::CurrentId())); |
| 845 }, |
| 846 base::SequencedTaskRunnerHandle::Get(), run_loop.QuitClosure(), |
| 847 thread_safe_ptr); |
| 848 other_thread.message_loop()->task_runner()->PostTask(FROM_HERE, run_method); |
| 849 |
| 850 // Block until the method callback is called on the background thread. |
| 851 run_loop.Run(); |
| 852 } |
| 853 |
| 812 } // namespace | 854 } // namespace |
| 813 } // namespace test | 855 } // namespace test |
| 814 } // namespace mojo | 856 } // namespace mojo |
| OLD | NEW |