Index: chromeos/arc/libarcbridge/arc_bridge.cc |
diff --git a/chromeos/arc/libarcbridge/arc_bridge.cc b/chromeos/arc/libarcbridge/arc_bridge.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..394b7881635bea9706f8bdce04d7441a67150b42 |
--- /dev/null |
+++ b/chromeos/arc/libarcbridge/arc_bridge.cc |
@@ -0,0 +1,102 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "arc_bridge.h" |
+ |
+#include <stdint.h> |
+ |
+#include <base/bind_helpers.h> |
+#include <base/files/file_util.h> |
+#include <ipc/ipc_channel.h> |
+ |
+#include "arc_host_messages.h" |
+#include "arc_instance_messages.h" |
+ |
+namespace arc { |
+ |
+BridgeEndpoint::BridgeEndpoint() : io_thread_("ARC bridge listener") { |
+ io_thread_.StartWithOptions( |
+ base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
+} |
+ |
+BridgeEndpoint::~BridgeEndpoint() {} |
+ |
+bool BridgeEndpoint::Connect(const IPC::ChannelHandle& handle, |
+ IPC::Channel::Mode mode) { |
+ channel_ = IPC::ChannelProxy::Create(handle, mode, this, |
+ io_thread_.task_runner().get()); |
+ if (!channel_) |
+ return false; |
+ |
+ return true; |
+} |
+ |
+BridgeInstanceEndpoint::BridgeInstanceEndpoint() : BridgeEndpoint() {} |
+ |
+BridgeInstanceEndpoint::~BridgeInstanceEndpoint() {} |
+ |
+bool BridgeInstanceEndpoint::SocketConnect(const char* socket_path) { |
+ return BridgeEndpoint::Connect(IPC::ChannelHandle(socket_path), |
+ IPC::Channel::MODE_NAMED_CLIENT); |
+} |
+ |
+bool BridgeInstanceEndpoint::InstanceReady() { |
+ return channel_->Send(new ArcHostMsg_InstanceReady()); |
+} |
+ |
+bool BridgeInstanceEndpoint::OnMessageReceived(const IPC::Message& message) { |
satorux1
2015/10/23 05:16:43
should this function run on the io_thread_? Then y
Luis Héctor Chávez
2015/10/23 17:09:24
No, this is supposed to be processed in the origin
satorux1
2015/10/26 01:58:52
I see. In that case, how about adding ThreadChecke
satorux1
2015/10/27 07:10:16
maybe missed this comment?
lhc(google)
2015/10/27 08:00:23
Forgot to reply, but I did add the DCHECKs that th
satorux1
2015/10/28 04:57:26
oops. I didn't noticed about the DCHECKs. thank yo
|
+ bool handled = true; |
+ |
+ IPC_BEGIN_MESSAGE_MAP(BridgeInstanceEndpoint, message) |
+ IPC_MESSAGE_HANDLER(ArcInstanceMsg_Ping, OnPing) |
+ IPC_MESSAGE_UNHANDLED(handled = false) |
+ IPC_END_MESSAGE_MAP() |
+ |
+ if (!handled) |
+ LOG(ERROR) << "Invalid message with type = " << message.type(); |
+ return handled; |
+} |
+ |
+void BridgeInstanceEndpoint::OnPing() { |
+ channel_->Send(new ArcHostMsg_Pong()); |
+} |
+ |
+BridgeHostEndpoint::BridgeHostEndpoint() : BridgeEndpoint() {} |
+ |
+BridgeHostEndpoint::~BridgeHostEndpoint() {} |
+ |
+bool BridgeHostEndpoint::SocketConnect(const char* socket_path) { |
+ base::FilePath path(socket_path); |
satorux1
2015/10/23 05:16:43
Please use base::FilePath::FromUTF8Unsafe() for co
Luis Héctor Chávez
2015/10/23 17:09:24
Done.
|
+ if (!base::CreateDirectory(path.DirName())) |
+ return false; |
+ |
+ if (!BridgeEndpoint::Connect(IPC::ChannelHandle(path.value()), |
+ IPC::Channel::MODE_OPEN_NAMED_SERVER)) |
+ return false; |
+ // TODO(lhchavez): Tighten the security around the socket by tying it to the |
+ // user the instance will run as. |
+ if (chmod(socket_path, 0777) != 0) |
satorux1
2015/10/23 05:16:43
HANDLE_EINTR?
Luis Héctor Chávez
2015/10/23 17:09:24
Done.
|
+ return false; |
+ return true; |
+} |
+ |
+bool BridgeHostEndpoint::Ping() { |
+ return channel_->Send(new ArcInstanceMsg_Ping()); |
+} |
+ |
+bool BridgeHostEndpoint::OnMessageReceived(const IPC::Message& message) { |
+ bool handled = true; |
+ |
+ IPC_BEGIN_MESSAGE_MAP(BridgeHostEndpoint, message) |
+ IPC_MESSAGE_HANDLER(ArcHostMsg_Pong, OnPong) |
+ IPC_MESSAGE_HANDLER(ArcHostMsg_InstanceReady, OnInstanceReady) |
+ IPC_MESSAGE_UNHANDLED(handled = false) |
+ IPC_END_MESSAGE_MAP() |
+ |
+ if (!handled) |
+ LOG(ERROR) << "Invalid message with type = " << message.type(); |
+ return handled; |
+} |
+ |
+} // namespace arc |