OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ipc/ipc_channel_mojo.h" | 5 #include "ipc/ipc_channel_mojo.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 830 matching lines...) Loading... |
841 | 841 |
842 class ListenerWithIndirectProxyAssociatedInterface | 842 class ListenerWithIndirectProxyAssociatedInterface |
843 : public IPC::Listener, | 843 : public IPC::Listener, |
844 public IPC::mojom::IndirectTestDriver, | 844 public IPC::mojom::IndirectTestDriver, |
845 public IPC::mojom::PingReceiver { | 845 public IPC::mojom::PingReceiver { |
846 public: | 846 public: |
847 ListenerWithIndirectProxyAssociatedInterface() | 847 ListenerWithIndirectProxyAssociatedInterface() |
848 : driver_binding_(this), ping_receiver_binding_(this) {} | 848 : driver_binding_(this), ping_receiver_binding_(this) {} |
849 ~ListenerWithIndirectProxyAssociatedInterface() override {} | 849 ~ListenerWithIndirectProxyAssociatedInterface() override {} |
850 | 850 |
| 851 // IPC::Listener: |
851 bool OnMessageReceived(const IPC::Message& message) override { return true; } | 852 bool OnMessageReceived(const IPC::Message& message) override { return true; } |
852 | 853 |
853 void RegisterInterfaceFactory(IPC::ChannelProxy* proxy) { | 854 void OnAssociatedInterfaceRequest( |
854 proxy->AddAssociatedInterface( | 855 const std::string& interface_name, |
855 base::Bind(&ListenerWithIndirectProxyAssociatedInterface::BindRequest, | 856 mojo::ScopedInterfaceEndpointHandle handle) override { |
856 base::Unretained(this))); | 857 DCHECK(!driver_binding_.is_bound()); |
| 858 DCHECK_EQ(interface_name, IPC::mojom::IndirectTestDriver::Name_); |
| 859 IPC::mojom::IndirectTestDriverAssociatedRequest request; |
| 860 request.Bind(std::move(handle)); |
| 861 driver_binding_.Bind(std::move(request)); |
857 } | 862 } |
858 | 863 |
859 void set_ping_handler(const base::Closure& handler) { | 864 void set_ping_handler(const base::Closure& handler) { |
860 ping_handler_ = handler; | 865 ping_handler_ = handler; |
861 } | 866 } |
862 | 867 |
863 private: | 868 private: |
864 // IPC::mojom::IndirectTestDriver: | 869 // IPC::mojom::IndirectTestDriver: |
865 void GetPingReceiver( | 870 void GetPingReceiver( |
866 IPC::mojom::PingReceiverAssociatedRequest request) override { | 871 IPC::mojom::PingReceiverAssociatedRequest request) override { |
867 ping_receiver_binding_.Bind(std::move(request)); | 872 ping_receiver_binding_.Bind(std::move(request)); |
868 } | 873 } |
869 | 874 |
870 // IPC::mojom::PingReceiver: | 875 // IPC::mojom::PingReceiver: |
871 void Ping(const PingCallback& callback) override { | 876 void Ping(const PingCallback& callback) override { |
872 callback.Run(); | 877 callback.Run(); |
873 ping_handler_.Run(); | 878 ping_handler_.Run(); |
874 } | 879 } |
875 | 880 |
876 void BindRequest(IPC::mojom::IndirectTestDriverAssociatedRequest request) { | |
877 DCHECK(!driver_binding_.is_bound()); | |
878 driver_binding_.Bind(std::move(request)); | |
879 } | |
880 | |
881 mojo::AssociatedBinding<IPC::mojom::IndirectTestDriver> driver_binding_; | 881 mojo::AssociatedBinding<IPC::mojom::IndirectTestDriver> driver_binding_; |
882 mojo::AssociatedBinding<IPC::mojom::PingReceiver> ping_receiver_binding_; | 882 mojo::AssociatedBinding<IPC::mojom::PingReceiver> ping_receiver_binding_; |
883 | 883 |
884 base::Closure ping_handler_; | 884 base::Closure ping_handler_; |
885 }; | 885 }; |
886 | 886 |
887 TEST_F(IPCChannelProxyMojoTest, ProxyThreadAssociatedInterfaceIndirect) { | 887 TEST_F(IPCChannelProxyMojoTest, ProxyThreadAssociatedInterfaceIndirect) { |
888 // Tests that we can pipeline interface requests and subsequent messages | 888 // Tests that we can pipeline interface requests and subsequent messages |
889 // targeting proxy thread bindings, and the channel will still dispatch | 889 // targeting proxy thread bindings, and the channel will still dispatch |
890 // messages appropriately. | 890 // messages appropriately. |
891 | 891 |
892 Init("ProxyThreadAssociatedInterfaceIndirectClient"); | 892 Init("ProxyThreadAssociatedInterfaceIndirectClient"); |
893 | 893 |
894 ListenerWithIndirectProxyAssociatedInterface listener; | 894 ListenerWithIndirectProxyAssociatedInterface listener; |
895 CreateProxy(&listener); | 895 CreateProxy(&listener); |
896 listener.RegisterInterfaceFactory(proxy()); | |
897 RunProxy(); | 896 RunProxy(); |
898 | 897 |
899 base::RunLoop loop; | 898 base::RunLoop loop; |
900 listener.set_ping_handler(loop.QuitClosure()); | 899 listener.set_ping_handler(loop.QuitClosure()); |
901 loop.Run(); | 900 loop.Run(); |
902 | 901 |
903 EXPECT_TRUE(WaitForClientShutdown()); | 902 EXPECT_TRUE(WaitForClientShutdown()); |
904 | 903 |
905 DestroyProxy(); | 904 DestroyProxy(); |
906 } | 905 } |
(...skipping 25 matching lines...) Loading... |
932 | 931 |
933 class ListenerWithSyncAssociatedInterface | 932 class ListenerWithSyncAssociatedInterface |
934 : public IPC::Listener, | 933 : public IPC::Listener, |
935 public IPC::mojom::SimpleTestDriver { | 934 public IPC::mojom::SimpleTestDriver { |
936 public: | 935 public: |
937 ListenerWithSyncAssociatedInterface() : binding_(this) {} | 936 ListenerWithSyncAssociatedInterface() : binding_(this) {} |
938 ~ListenerWithSyncAssociatedInterface() override {} | 937 ~ListenerWithSyncAssociatedInterface() override {} |
939 | 938 |
940 void set_sync_sender(IPC::Sender* sync_sender) { sync_sender_ = sync_sender; } | 939 void set_sync_sender(IPC::Sender* sync_sender) { sync_sender_ = sync_sender; } |
941 | 940 |
942 void RegisterInterfaceFactory(IPC::ChannelProxy* proxy) { | |
943 proxy->AddAssociatedInterface( | |
944 base::Bind(&ListenerWithSyncAssociatedInterface::BindRequest, | |
945 base::Unretained(this))); | |
946 } | |
947 | |
948 void RunUntilQuitRequested() { | 941 void RunUntilQuitRequested() { |
949 base::RunLoop loop; | 942 base::RunLoop loop; |
950 quit_closure_ = loop.QuitClosure(); | 943 quit_closure_ = loop.QuitClosure(); |
951 loop.Run(); | 944 loop.Run(); |
952 } | 945 } |
953 | 946 |
954 void CloseBinding() { binding_.Close(); } | 947 void CloseBinding() { binding_.Close(); } |
955 | 948 |
956 void set_response_value(int32_t response) { | 949 void set_response_value(int32_t response) { |
957 response_value_ = response; | 950 response_value_ = response; |
(...skipping 24 matching lines...) Loading... |
982 EXPECT_TRUE(message.is_sync()); | 975 EXPECT_TRUE(message.is_sync()); |
983 EXPECT_TRUE(message.should_unblock()); | 976 EXPECT_TRUE(message.should_unblock()); |
984 std::unique_ptr<IPC::Message> reply( | 977 std::unique_ptr<IPC::Message> reply( |
985 IPC::SyncMessage::GenerateReply(&message)); | 978 IPC::SyncMessage::GenerateReply(&message)); |
986 reply->WriteInt(response_value_); | 979 reply->WriteInt(response_value_); |
987 DCHECK(sync_sender_); | 980 DCHECK(sync_sender_); |
988 EXPECT_TRUE(sync_sender_->Send(reply.release())); | 981 EXPECT_TRUE(sync_sender_->Send(reply.release())); |
989 return true; | 982 return true; |
990 } | 983 } |
991 | 984 |
| 985 void OnAssociatedInterfaceRequest( |
| 986 const std::string& interface_name, |
| 987 mojo::ScopedInterfaceEndpointHandle handle) override { |
| 988 DCHECK(!binding_.is_bound()); |
| 989 DCHECK_EQ(interface_name, IPC::mojom::SimpleTestDriver::Name_); |
| 990 |
| 991 IPC::mojom::SimpleTestDriverAssociatedRequest request; |
| 992 request.Bind(std::move(handle)); |
| 993 binding_.Bind(std::move(request)); |
| 994 } |
| 995 |
992 void BindRequest(IPC::mojom::SimpleTestDriverAssociatedRequest request) { | 996 void BindRequest(IPC::mojom::SimpleTestDriverAssociatedRequest request) { |
993 DCHECK(!binding_.is_bound()); | 997 DCHECK(!binding_.is_bound()); |
994 binding_.Bind(std::move(request)); | 998 binding_.Bind(std::move(request)); |
995 } | 999 } |
996 | 1000 |
997 IPC::Sender* sync_sender_ = nullptr; | 1001 IPC::Sender* sync_sender_ = nullptr; |
998 int32_t next_expected_value_ = 0; | 1002 int32_t next_expected_value_ = 0; |
999 int32_t response_value_ = 0; | 1003 int32_t response_value_ = 0; |
1000 base::Closure quit_closure_; | 1004 base::Closure quit_closure_; |
1001 | 1005 |
(...skipping 18 matching lines...) Loading... |
1020 | 1024 |
1021 DISALLOW_COPY_AND_ASSIGN(SyncReplyReader); | 1025 DISALLOW_COPY_AND_ASSIGN(SyncReplyReader); |
1022 }; | 1026 }; |
1023 | 1027 |
1024 TEST_F(IPCChannelProxyMojoTest, SyncAssociatedInterface) { | 1028 TEST_F(IPCChannelProxyMojoTest, SyncAssociatedInterface) { |
1025 Init("SyncAssociatedInterface"); | 1029 Init("SyncAssociatedInterface"); |
1026 | 1030 |
1027 ListenerWithSyncAssociatedInterface listener; | 1031 ListenerWithSyncAssociatedInterface listener; |
1028 CreateProxy(&listener); | 1032 CreateProxy(&listener); |
1029 listener.set_sync_sender(proxy()); | 1033 listener.set_sync_sender(proxy()); |
1030 listener.RegisterInterfaceFactory(proxy()); | |
1031 RunProxy(); | 1034 RunProxy(); |
1032 | 1035 |
1033 // Run the client's simple sanity check to completion. | 1036 // Run the client's simple sanity check to completion. |
1034 listener.RunUntilQuitRequested(); | 1037 listener.RunUntilQuitRequested(); |
1035 | 1038 |
1036 // Verify that we can send a sync IPC and service an incoming sync request | 1039 // Verify that we can send a sync IPC and service an incoming sync request |
1037 // while waiting on it | 1040 // while waiting on it |
1038 listener.set_response_value(42); | 1041 listener.set_response_value(42); |
1039 IPC::mojom::SimpleTestClientAssociatedPtr client; | 1042 IPC::mojom::SimpleTestClientAssociatedPtr client; |
1040 proxy()->GetRemoteAssociatedInterface(&client); | 1043 proxy()->GetRemoteAssociatedInterface(&client); |
(...skipping 23 matching lines...) Loading... |
1064 } | 1067 } |
1065 | 1068 |
1066 class SimpleTestClientImpl : public IPC::mojom::SimpleTestClient, | 1069 class SimpleTestClientImpl : public IPC::mojom::SimpleTestClient, |
1067 public IPC::Listener { | 1070 public IPC::Listener { |
1068 public: | 1071 public: |
1069 SimpleTestClientImpl() : binding_(this) {} | 1072 SimpleTestClientImpl() : binding_(this) {} |
1070 | 1073 |
1071 void set_driver(IPC::mojom::SimpleTestDriver* driver) { driver_ = driver; } | 1074 void set_driver(IPC::mojom::SimpleTestDriver* driver) { driver_ = driver; } |
1072 void set_sync_sender(IPC::Sender* sync_sender) { sync_sender_ = sync_sender; } | 1075 void set_sync_sender(IPC::Sender* sync_sender) { sync_sender_ = sync_sender; } |
1073 | 1076 |
1074 void BindRequest(IPC::mojom::SimpleTestClientAssociatedRequest request) { | |
1075 DCHECK(!binding_.is_bound()); | |
1076 binding_.Bind(std::move(request)); | |
1077 } | |
1078 | |
1079 void WaitForValueRequest() { | 1077 void WaitForValueRequest() { |
1080 run_loop_.reset(new base::RunLoop); | 1078 run_loop_.reset(new base::RunLoop); |
1081 run_loop_->Run(); | 1079 run_loop_->Run(); |
1082 } | 1080 } |
1083 | 1081 |
1084 void UseSyncSenderForRequest(bool use_sync_sender) { | 1082 void UseSyncSenderForRequest(bool use_sync_sender) { |
1085 use_sync_sender_ = use_sync_sender; | 1083 use_sync_sender_ = use_sync_sender; |
1086 } | 1084 } |
1087 | 1085 |
1088 private: | 1086 private: |
(...skipping 23 matching lines...) Loading... |
1112 std::unique_ptr<IPC::Message> reply( | 1110 std::unique_ptr<IPC::Message> reply( |
1113 IPC::SyncMessage::GenerateReply(&message)); | 1111 IPC::SyncMessage::GenerateReply(&message)); |
1114 reply->WriteInt(response); | 1112 reply->WriteInt(response); |
1115 EXPECT_TRUE(sync_sender_->Send(reply.release())); | 1113 EXPECT_TRUE(sync_sender_->Send(reply.release())); |
1116 | 1114 |
1117 DCHECK(run_loop_); | 1115 DCHECK(run_loop_); |
1118 run_loop_->Quit(); | 1116 run_loop_->Quit(); |
1119 return true; | 1117 return true; |
1120 } | 1118 } |
1121 | 1119 |
| 1120 void OnAssociatedInterfaceRequest( |
| 1121 const std::string& interface_name, |
| 1122 mojo::ScopedInterfaceEndpointHandle handle) override { |
| 1123 DCHECK(!binding_.is_bound()); |
| 1124 DCHECK_EQ(interface_name, IPC::mojom::SimpleTestClient::Name_); |
| 1125 |
| 1126 IPC::mojom::SimpleTestClientAssociatedRequest request; |
| 1127 request.Bind(std::move(handle)); |
| 1128 binding_.Bind(std::move(request)); |
| 1129 } |
| 1130 |
1122 bool use_sync_sender_ = false; | 1131 bool use_sync_sender_ = false; |
1123 mojo::AssociatedBinding<IPC::mojom::SimpleTestClient> binding_; | 1132 mojo::AssociatedBinding<IPC::mojom::SimpleTestClient> binding_; |
1124 IPC::Sender* sync_sender_ = nullptr; | 1133 IPC::Sender* sync_sender_ = nullptr; |
1125 IPC::mojom::SimpleTestDriver* driver_ = nullptr; | 1134 IPC::mojom::SimpleTestDriver* driver_ = nullptr; |
1126 std::unique_ptr<base::RunLoop> run_loop_; | 1135 std::unique_ptr<base::RunLoop> run_loop_; |
1127 | 1136 |
1128 DISALLOW_COPY_AND_ASSIGN(SimpleTestClientImpl); | 1137 DISALLOW_COPY_AND_ASSIGN(SimpleTestClientImpl); |
1129 }; | 1138 }; |
1130 | 1139 |
1131 DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(SyncAssociatedInterface, | 1140 DEFINE_IPC_CHANNEL_MOJO_TEST_CLIENT_WITH_CUSTOM_FIXTURE(SyncAssociatedInterface, |
1132 ChannelProxyClient) { | 1141 ChannelProxyClient) { |
1133 SimpleTestClientImpl client_impl; | 1142 SimpleTestClientImpl client_impl; |
1134 CreateProxy(&client_impl); | 1143 CreateProxy(&client_impl); |
1135 client_impl.set_sync_sender(proxy()); | 1144 client_impl.set_sync_sender(proxy()); |
1136 proxy()->AddAssociatedInterface(base::Bind(&SimpleTestClientImpl::BindRequest, | |
1137 base::Unretained(&client_impl))); | |
1138 RunProxy(); | 1145 RunProxy(); |
1139 | 1146 |
1140 IPC::mojom::SimpleTestDriverAssociatedPtr driver; | 1147 IPC::mojom::SimpleTestDriverAssociatedPtr driver; |
1141 proxy()->GetRemoteAssociatedInterface(&driver); | 1148 proxy()->GetRemoteAssociatedInterface(&driver); |
1142 client_impl.set_driver(driver.get()); | 1149 client_impl.set_driver(driver.get()); |
1143 | 1150 |
1144 // Simple sync message sanity check. | 1151 // Simple sync message sanity check. |
1145 driver->ExpectValue(42); | 1152 driver->ExpectValue(42); |
1146 int32_t expected_value = 0; | 1153 int32_t expected_value = 0; |
1147 EXPECT_TRUE(driver->GetExpectedValue(&expected_value)); | 1154 EXPECT_TRUE(driver->GetExpectedValue(&expected_value)); |
(...skipping 262 matching lines...) Loading... |
1410 Connect(&listener); | 1417 Connect(&listener); |
1411 | 1418 |
1412 base::RunLoop().Run(); | 1419 base::RunLoop().Run(); |
1413 | 1420 |
1414 Close(); | 1421 Close(); |
1415 } | 1422 } |
1416 | 1423 |
1417 #endif // OS_LINUX | 1424 #endif // OS_LINUX |
1418 | 1425 |
1419 } // namespace | 1426 } // namespace |
OLD | NEW |