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