| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "remoting/host/security_key/fake_security_key_ipc_client.h" | 5 #include "remoting/host/security_key/fake_security_key_ipc_client.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "ipc/ipc_channel.h" | 14 #include "ipc/ipc_channel.h" |
| 15 #include "ipc/ipc_message.h" | 15 #include "ipc/ipc_message.h" |
| 16 #include "ipc/ipc_message_macros.h" | 16 #include "ipc/ipc_message_macros.h" |
| 17 #include "mojo/edk/embedder/embedder.h" |
| 18 #include "mojo/edk/embedder/named_platform_handle_utils.h" |
| 17 #include "remoting/host/chromoting_messages.h" | 19 #include "remoting/host/chromoting_messages.h" |
| 18 | 20 |
| 19 namespace remoting { | 21 namespace remoting { |
| 20 | 22 |
| 21 FakeSecurityKeyIpcClient::FakeSecurityKeyIpcClient( | 23 FakeSecurityKeyIpcClient::FakeSecurityKeyIpcClient( |
| 22 const base::Closure& channel_event_callback) | 24 const base::Closure& channel_event_callback) |
| 23 : channel_event_callback_(channel_event_callback), weak_factory_(this) { | 25 : channel_event_callback_(channel_event_callback), weak_factory_(this) { |
| 24 DCHECK(!channel_event_callback_.is_null()); | 26 DCHECK(!channel_event_callback_.is_null()); |
| 25 } | 27 } |
| 26 | 28 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 54 } | 56 } |
| 55 | 57 |
| 56 return send_security_request_should_succeed_; | 58 return send_security_request_should_succeed_; |
| 57 } | 59 } |
| 58 | 60 |
| 59 void FakeSecurityKeyIpcClient::CloseIpcConnection() { | 61 void FakeSecurityKeyIpcClient::CloseIpcConnection() { |
| 60 client_channel_.reset(); | 62 client_channel_.reset(); |
| 61 channel_event_callback_.Run(); | 63 channel_event_callback_.Run(); |
| 62 } | 64 } |
| 63 | 65 |
| 64 bool FakeSecurityKeyIpcClient::ConnectViaIpc(const std::string& channel_name) { | 66 bool FakeSecurityKeyIpcClient::ConnectViaIpc( |
| 65 // The retry loop is needed as the IPC Servers we connect to are reset (torn | 67 const mojo::edk::NamedPlatformHandle& channel_handle) { |
| 66 // down and recreated) in some tests and we should be resilient in that case. | 68 mojo::edk::ScopedPlatformHandle handle = |
| 67 IPC::ChannelHandle channel_handle(channel_name); | 69 mojo::edk::CreateClientHandle(channel_handle); |
| 68 for (int i = 0; i < 5; i++) { | 70 if (!handle.is_valid()) |
| 69 client_channel_ = IPC::Channel::CreateNamedClient(channel_handle, this); | 71 return false; |
| 70 if (client_channel_->Connect()) { | |
| 71 return true; | |
| 72 } | |
| 73 | 72 |
| 74 base::RunLoop run_loop; | 73 client_channel_ = IPC::Channel::CreateClient( |
| 75 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 74 mojo::edk::ConnectToPeerProcess(std::move(handle)).release(), this); |
| 76 FROM_HERE, run_loop.QuitClosure(), | 75 return client_channel_->Connect(); |
| 77 base::TimeDelta::FromMilliseconds(100)); | |
| 78 run_loop.Run(); | |
| 79 } | |
| 80 | |
| 81 return false; | |
| 82 } | 76 } |
| 83 | 77 |
| 84 void FakeSecurityKeyIpcClient::SendSecurityKeyRequestViaIpc( | 78 void FakeSecurityKeyIpcClient::SendSecurityKeyRequestViaIpc( |
| 85 const std::string& request_payload) { | 79 const std::string& request_payload) { |
| 86 client_channel_->Send( | 80 client_channel_->Send( |
| 87 new ChromotingRemoteSecurityKeyToNetworkMsg_Request(request_payload)); | 81 new ChromotingRemoteSecurityKeyToNetworkMsg_Request(request_payload)); |
| 88 } | 82 } |
| 89 | 83 |
| 90 bool FakeSecurityKeyIpcClient::OnMessageReceived(const IPC::Message& message) { | 84 bool FakeSecurityKeyIpcClient::OnMessageReceived(const IPC::Message& message) { |
| 91 bool handled = true; | 85 bool handled = true; |
| 92 IPC_BEGIN_MESSAGE_MAP(FakeSecurityKeyIpcClient, message) | 86 IPC_BEGIN_MESSAGE_MAP(FakeSecurityKeyIpcClient, message) |
| 93 IPC_MESSAGE_HANDLER( | |
| 94 ChromotingNetworkToRemoteSecurityKeyMsg_ConnectionDetails, | |
| 95 OnConnectionDetails) | |
| 96 IPC_MESSAGE_HANDLER(ChromotingNetworkToRemoteSecurityKeyMsg_Response, | 87 IPC_MESSAGE_HANDLER(ChromotingNetworkToRemoteSecurityKeyMsg_Response, |
| 97 OnSecurityKeyResponse) | 88 OnSecurityKeyResponse) |
| 98 IPC_MESSAGE_UNHANDLED(handled = false) | 89 IPC_MESSAGE_UNHANDLED(handled = false) |
| 99 IPC_END_MESSAGE_MAP() | 90 IPC_END_MESSAGE_MAP() |
| 100 | 91 |
| 101 CHECK(handled) << "Received unexpected IPC type: " << message.type(); | 92 CHECK(handled) << "Received unexpected IPC type: " << message.type(); |
| 102 return handled; | 93 return handled; |
| 103 } | 94 } |
| 104 | 95 |
| 105 void FakeSecurityKeyIpcClient::OnChannelConnected(int32_t peer_pid) { | 96 void FakeSecurityKeyIpcClient::OnChannelConnected(int32_t peer_pid) { |
| 106 ipc_channel_connected_ = true; | 97 ipc_channel_connected_ = true; |
| 107 channel_event_callback_.Run(); | 98 channel_event_callback_.Run(); |
| 108 } | 99 } |
| 109 | 100 |
| 110 void FakeSecurityKeyIpcClient::OnChannelError() { | 101 void FakeSecurityKeyIpcClient::OnChannelError() { |
| 111 ipc_channel_connected_ = false; | 102 ipc_channel_connected_ = false; |
| 112 channel_event_callback_.Run(); | 103 channel_event_callback_.Run(); |
| 113 } | 104 } |
| 114 | 105 |
| 115 void FakeSecurityKeyIpcClient::OnConnectionDetails( | |
| 116 const std::string& channel_name) { | |
| 117 last_message_received_ = channel_name; | |
| 118 channel_event_callback_.Run(); | |
| 119 } | |
| 120 | |
| 121 void FakeSecurityKeyIpcClient::OnSecurityKeyResponse( | 106 void FakeSecurityKeyIpcClient::OnSecurityKeyResponse( |
| 122 const std::string& request_data) { | 107 const std::string& request_data) { |
| 123 last_message_received_ = request_data; | 108 last_message_received_ = request_data; |
| 124 channel_event_callback_.Run(); | 109 channel_event_callback_.Run(); |
| 125 } | 110 } |
| 126 | 111 |
| 127 } // namespace remoting | 112 } // namespace remoting |
| OLD | NEW |