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/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 835 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
846 base::PlatformThread::CurrentId())); | 846 base::PlatformThread::CurrentId())); |
847 }, | 847 }, |
848 base::SequencedTaskRunnerHandle::Get(), run_loop.QuitClosure(), | 848 base::SequencedTaskRunnerHandle::Get(), run_loop.QuitClosure(), |
849 thread_safe_ptr); | 849 thread_safe_ptr); |
850 other_thread.message_loop()->task_runner()->PostTask(FROM_HERE, run_method); | 850 other_thread.message_loop()->task_runner()->PostTask(FROM_HERE, run_method); |
851 | 851 |
852 // Block until the method callback is called on the background thread. | 852 // Block until the method callback is called on the background thread. |
853 run_loop.Run(); | 853 run_loop.Run(); |
854 } | 854 } |
855 | 855 |
856 TEST_F(InterfacePtrTest, BindLaterThreadSafeInterfacePointer) { | 856 TEST_F(InterfacePtrTest, ThreadSafeInterfacePointerWithTaskRunner) { |
857 // Create and start the thread from where we'll bind the interface pointer. | 857 // Create and start the thread from where we'll bind the interface pointer. |
858 base::Thread other_thread("service test thread"); | 858 base::Thread other_thread("service test thread"); |
859 other_thread.Start(); | 859 other_thread.Start(); |
860 const scoped_refptr<base::SingleThreadTaskRunner>& other_thread_task_runner = | 860 const scoped_refptr<base::SingleThreadTaskRunner>& other_thread_task_runner = |
861 other_thread.message_loop()->task_runner(); | 861 other_thread.message_loop()->task_runner(); |
862 | 862 |
| 863 math::CalculatorPtr ptr; |
| 864 math::CalculatorRequest request(&ptr); |
| 865 |
863 // Create a ThreadSafeInterfacePtr that we'll bind from a different thread. | 866 // Create a ThreadSafeInterfacePtr that we'll bind from a different thread. |
864 scoped_refptr<math::ThreadSafeCalculatorPtr> thread_safe_ptr = | 867 scoped_refptr<math::ThreadSafeCalculatorPtr> thread_safe_ptr = |
865 math::ThreadSafeCalculatorPtr::CreateUnbound(other_thread_task_runner); | 868 math::ThreadSafeCalculatorPtr::Create(ptr.PassInterface(), |
| 869 other_thread_task_runner); |
866 ASSERT_TRUE(thread_safe_ptr); | 870 ASSERT_TRUE(thread_safe_ptr); |
867 | 871 |
868 MathCalculatorImpl* math_calc_impl = nullptr; | 872 MathCalculatorImpl* math_calc_impl = nullptr; |
869 { | 873 { |
870 base::RunLoop run_loop; | 874 base::RunLoop run_loop; |
871 auto run_method = base::Bind( | 875 auto run_method = base::Bind( |
872 [](const scoped_refptr<base::TaskRunner>& main_task_runner, | 876 [](const scoped_refptr<base::TaskRunner>& main_task_runner, |
873 const base::Closure& quit_closure, | 877 const base::Closure& quit_closure, |
874 const scoped_refptr<math::ThreadSafeCalculatorPtr>& thread_safe_ptr, | 878 const scoped_refptr<math::ThreadSafeCalculatorPtr>& thread_safe_ptr, |
| 879 math::CalculatorRequest request, |
875 MathCalculatorImpl** math_calc_impl) { | 880 MathCalculatorImpl** math_calc_impl) { |
876 math::CalculatorPtr ptr; | 881 math::CalculatorPtr ptr; |
877 // In real life, the implementation would have a legitimate owner. | 882 // In real life, the implementation would have a legitimate owner. |
878 *math_calc_impl = new MathCalculatorImpl(MakeRequest(&ptr)); | 883 *math_calc_impl = new MathCalculatorImpl(std::move(request)); |
879 thread_safe_ptr->Bind(std::move(ptr)); | |
880 main_task_runner->PostTask(FROM_HERE, quit_closure); | 884 main_task_runner->PostTask(FROM_HERE, quit_closure); |
881 }, | 885 }, |
882 base::SequencedTaskRunnerHandle::Get(), run_loop.QuitClosure(), | 886 base::SequencedTaskRunnerHandle::Get(), run_loop.QuitClosure(), |
883 thread_safe_ptr, &math_calc_impl); | 887 thread_safe_ptr, base::Passed(&request), &math_calc_impl); |
884 other_thread.message_loop()->task_runner()->PostTask(FROM_HERE, run_method); | 888 other_thread.message_loop()->task_runner()->PostTask(FROM_HERE, run_method); |
885 run_loop.Run(); | 889 run_loop.Run(); |
886 } | 890 } |
887 | 891 |
888 { | 892 { |
889 // The interface ptr is bound, we can call methods on it. | 893 // The interface ptr is bound, we can call methods on it. |
890 auto calc_callback = | 894 auto calc_callback = |
891 base::Bind([](const base::Closure& quit_closure, double result) { | 895 base::Bind([](const base::Closure& quit_closure, double result) { |
892 EXPECT_EQ(123, result); | 896 EXPECT_EQ(123, result); |
893 quit_closure.Run(); | 897 quit_closure.Run(); |
894 }); | 898 }); |
895 base::RunLoop run_loop; | 899 base::RunLoop run_loop; |
896 (*thread_safe_ptr) | 900 (*thread_safe_ptr) |
897 ->Add(123, base::Bind(calc_callback, run_loop.QuitClosure())); | 901 ->Add(123, base::Bind(calc_callback, run_loop.QuitClosure())); |
898 // Block until the method callback is called. | 902 // Block until the method callback is called. |
899 run_loop.Run(); | 903 run_loop.Run(); |
900 } | 904 } |
901 | 905 |
902 other_thread_task_runner->DeleteSoon(FROM_HERE, math_calc_impl); | 906 other_thread_task_runner->DeleteSoon(FROM_HERE, math_calc_impl); |
903 | 907 |
904 // Reset the pointer now so the InterfacePtr associated resources can be | 908 // Reset the pointer now so the InterfacePtr associated resources can be |
905 // deleted before the background thread's message loop is invalidated. | 909 // deleted before the background thread's message loop is invalidated. |
906 thread_safe_ptr = nullptr; | 910 thread_safe_ptr = nullptr; |
907 } | 911 } |
908 | 912 |
909 } // namespace | 913 } // namespace |
910 } // namespace test | 914 } // namespace test |
911 } // namespace mojo | 915 } // namespace mojo |
OLD | NEW |