| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 1032 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1043 base::PlatformThread::CurrentId())); | 1043 base::PlatformThread::CurrentId())); |
| 1044 }, | 1044 }, |
| 1045 base::SequencedTaskRunnerHandle::Get(), run_loop.QuitClosure(), | 1045 base::SequencedTaskRunnerHandle::Get(), run_loop.QuitClosure(), |
| 1046 thread_safe_sender); | 1046 thread_safe_sender); |
| 1047 other_thread.message_loop()->task_runner()->PostTask(FROM_HERE, run_method); | 1047 other_thread.message_loop()->task_runner()->PostTask(FROM_HERE, run_method); |
| 1048 | 1048 |
| 1049 // Block until the method callback is called on the background thread. | 1049 // Block until the method callback is called on the background thread. |
| 1050 run_loop.Run(); | 1050 run_loop.Run(); |
| 1051 } | 1051 } |
| 1052 | 1052 |
| 1053 struct ForwarderTestContext { |
| 1054 IntegerSenderConnectionPtr connection_ptr; |
| 1055 std::unique_ptr<IntegerSenderConnectionImpl> interface_impl; |
| 1056 }; |
| 1057 |
| 1058 TEST_F(AssociatedInterfaceTest, BindLaterThreadSafeAssociatedInterfacePtr) { |
| 1059 // Create a ThreadSafeAssociatedPtr that we'll bind from a different thread. |
| 1060 scoped_refptr<ThreadSafeIntegerSenderAssociatedPtr> thread_safe_ptr = |
| 1061 ThreadSafeIntegerSenderAssociatedPtr::CreateUnbound(); |
| 1062 |
| 1063 // Start the thread from where we'll bind the interface pointer. |
| 1064 base::Thread other_thread("service test thread"); |
| 1065 other_thread.Start(); |
| 1066 const scoped_refptr<base::SingleThreadTaskRunner>& other_thread_task_runner = |
| 1067 other_thread.message_loop()->task_runner(); |
| 1068 ForwarderTestContext* context = new ForwarderTestContext(); |
| 1069 { |
| 1070 base::RunLoop run_loop; |
| 1071 auto run_method = base::Bind( |
| 1072 [](const scoped_refptr<base::TaskRunner>& main_task_runner, |
| 1073 const base::Closure& quit_closure, |
| 1074 const scoped_refptr<ThreadSafeIntegerSenderAssociatedPtr>& |
| 1075 thread_safe_ptr, |
| 1076 ForwarderTestContext* context) { |
| 1077 // We are on the background thread, create the interface ptr. |
| 1078 context->interface_impl = |
| 1079 base::MakeUnique<IntegerSenderConnectionImpl>( |
| 1080 GetProxy(&(context->connection_ptr))); |
| 1081 IntegerSenderAssociatedPtr sender; |
| 1082 context->connection_ptr->GetSender( |
| 1083 GetProxy(&sender, context->connection_ptr.associated_group())); |
| 1084 thread_safe_ptr->Bind(std::move(sender)); |
| 1085 main_task_runner->PostTask(FROM_HERE, quit_closure); |
| 1086 }, |
| 1087 base::SequencedTaskRunnerHandle::Get(), run_loop.QuitClosure(), |
| 1088 thread_safe_ptr, context); |
| 1089 |
| 1090 other_thread_task_runner->PostTask(FROM_HERE, run_method); |
| 1091 // Block until the associated pointer is bound. |
| 1092 run_loop.Run(); |
| 1093 } |
| 1094 |
| 1095 { |
| 1096 // Now we can call methods on the interface from the main thread. |
| 1097 auto echo_callback = |
| 1098 base::Bind([](const base::Closure& quit_closure, int32_t result) { |
| 1099 EXPECT_EQ(123, result); |
| 1100 quit_closure.Run(); |
| 1101 }); |
| 1102 base::RunLoop run_loop; |
| 1103 (*thread_safe_ptr) |
| 1104 ->Echo(123, base::Bind(echo_callback, run_loop.QuitClosure())); |
| 1105 // Block until the method callback is called. |
| 1106 run_loop.Run(); |
| 1107 } |
| 1108 |
| 1109 other_thread_task_runner->DeleteSoon(FROM_HERE, context); |
| 1110 |
| 1111 // Reset the pointer now so the InterfacePtr associated resources can be |
| 1112 // deleted before the background thread's message loop is invalidated. |
| 1113 thread_safe_ptr = nullptr; |
| 1114 } |
| 1115 |
| 1053 } // namespace | 1116 } // namespace |
| 1054 } // namespace test | 1117 } // namespace test |
| 1055 } // namespace mojo | 1118 } // namespace mojo |
| OLD | NEW |