OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/host/desktop_session_agent.h" |
| 6 |
| 7 #include <fcntl.h> |
| 8 #include <sys/socket.h> |
| 9 #include <sys/types.h> |
| 10 #include <unistd.h> |
| 11 |
| 12 #include "base/eintr_wrapper.h" |
| 13 #include "base/single_thread_task_runner.h" |
| 14 #include "ipc/ipc_channel.h" |
| 15 #include "ipc/ipc_channel_proxy.h" |
| 16 #include "remoting/base/auto_thread_task_runner.h" |
| 17 |
| 18 namespace remoting { |
| 19 |
| 20 // Provides screen/audio capturing and input injection services for |
| 21 // the network process. |
| 22 class DesktopSessionAgentPosix : public DesktopSessionAgent { |
| 23 public: |
| 24 DesktopSessionAgentPosix( |
| 25 scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 26 scoped_refptr<AutoThreadTaskRunner> io_task_runner); |
| 27 virtual ~DesktopSessionAgentPosix(); |
| 28 |
| 29 protected: |
| 30 virtual bool DoCreateNetworkChannel( |
| 31 IPC::PlatformFileForTransit* client_out, |
| 32 scoped_ptr<IPC::ChannelProxy>* server_out) OVERRIDE; |
| 33 |
| 34 private: |
| 35 DISALLOW_COPY_AND_ASSIGN(DesktopSessionAgentPosix); |
| 36 }; |
| 37 |
| 38 DesktopSessionAgentPosix::DesktopSessionAgentPosix( |
| 39 scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 40 scoped_refptr<AutoThreadTaskRunner> io_task_runner) |
| 41 : DesktopSessionAgent(caller_task_runner, io_task_runner) { |
| 42 } |
| 43 |
| 44 DesktopSessionAgentPosix::~DesktopSessionAgentPosix() { |
| 45 } |
| 46 |
| 47 bool DesktopSessionAgentPosix::DoCreateNetworkChannel( |
| 48 IPC::PlatformFileForTransit* client_out, |
| 49 scoped_ptr<IPC::ChannelProxy>* server_out) { |
| 50 // Create a socket pair. |
| 51 int pipe_fds[2]; |
| 52 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds) != 0) { |
| 53 PLOG(ERROR) << "socketpair()"; |
| 54 return false; |
| 55 } |
| 56 |
| 57 // Set both ends to be non-blocking. |
| 58 if (fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK) == -1 || |
| 59 fcntl(pipe_fds[1], F_SETFL, O_NONBLOCK) == -1) { |
| 60 PLOG(ERROR) << "fcntl(O_NONBLOCK)"; |
| 61 if (HANDLE_EINTR(close(pipe_fds[0])) < 0) |
| 62 PLOG(ERROR) << "close()"; |
| 63 if (HANDLE_EINTR(close(pipe_fds[1])) < 0) |
| 64 PLOG(ERROR) << "close()"; |
| 65 return false; |
| 66 } |
| 67 |
| 68 // Generate a unique name for the channel. |
| 69 std::string channel_name = IPC::Channel::GenerateUniqueRandomChannelID(); |
| 70 std::string socket_name = "DesktopSessionAgent socket"; |
| 71 |
| 72 // Wrap the pipe into an IPC channel. |
| 73 base::FileDescriptor fd(pipe_fds[0], false); |
| 74 IPC::ChannelHandle handle(socket_name, fd); |
| 75 server_out->reset(new IPC::ChannelProxy( |
| 76 IPC::ChannelHandle(socket_name, fd), |
| 77 IPC::Channel::MODE_SERVER, |
| 78 this, |
| 79 io_task_runner())); |
| 80 |
| 81 *client_out = base::FileDescriptor(pipe_fds[1], false); |
| 82 return true; |
| 83 } |
| 84 |
| 85 // static |
| 86 scoped_ptr<DesktopSessionAgent> DesktopSessionAgent::Create( |
| 87 scoped_refptr<AutoThreadTaskRunner> caller_task_runner, |
| 88 scoped_refptr<AutoThreadTaskRunner> io_task_runner) { |
| 89 return scoped_ptr<DesktopSessionAgent>(new DesktopSessionAgentPosix( |
| 90 caller_task_runner, io_task_runner)); |
| 91 } |
| 92 |
| 93 } // namespace remoting |
OLD | NEW |