OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef IPC_IPC_CHANNEL_NACL_H_ | 5 #ifndef IPC_IPC_CHANNEL_NACL_H_ |
6 #define IPC_IPC_CHANNEL_NACL_H_ | 6 #define IPC_IPC_CHANNEL_NACL_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
| 9 #include <deque> |
| 10 #include <string> |
| 11 |
| 12 #include "base/memory/linked_ptr.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/threading/simple_thread.h" |
9 #include "ipc/ipc_channel.h" | 16 #include "ipc/ipc_channel.h" |
10 #include "ipc/ipc_channel_reader.h" | 17 #include "ipc/ipc_channel_reader.h" |
11 | 18 |
12 namespace IPC { | 19 namespace IPC { |
13 | 20 |
14 // Similar to the Posix version of ChannelImpl but for Native Client code. | 21 // Similar to the Posix version of ChannelImpl but for Native Client code. |
15 // This is somewhat different because NaCl's send/recvmsg is slightly different | 22 // This is somewhat different because NaCl's send/recvmsg is slightly different |
16 // and we don't need to worry about complicated set up and READWRITE mode for | 23 // and we don't need to worry about complicated set up and READWRITE mode for |
17 // sharing handles. | 24 // sharing handles. We also currently do not support passing file descriptors or |
| 25 // named pipes, and we use background threads to emulate signaling when we can |
| 26 // read or write without blocking. |
18 class Channel::ChannelImpl : public internal::ChannelReader { | 27 class Channel::ChannelImpl : public internal::ChannelReader { |
19 public: | 28 public: |
| 29 // Mirror methods of Channel, see ipc_channel.h for description. |
20 ChannelImpl(const IPC::ChannelHandle& channel_handle, | 30 ChannelImpl(const IPC::ChannelHandle& channel_handle, |
21 Mode mode, | 31 Mode mode, |
22 Listener* listener); | 32 Listener* listener); |
23 virtual ~ChannelImpl(); | 33 virtual ~ChannelImpl(); |
24 | 34 |
25 // Channel implementation. | 35 // Channel implementation. |
26 bool Connect(); | 36 bool Connect(); |
27 void Close(); | 37 void Close(); |
28 bool Send(Message* message); | 38 bool Send(Message* message); |
29 int GetClientFileDescriptor() const; | |
30 int TakeClientFileDescriptor(); | |
31 bool AcceptsConnections() const; | |
32 bool HasAcceptedConnection() const; | |
33 bool GetClientEuid(uid_t* client_euid) const; | |
34 void ResetToAcceptingConnectionState(); | |
35 static bool IsNamedServerInitialized(const std::string& channel_id); | |
36 | 39 |
| 40 // Posted to the main thread by ReaderThreadRunner. |
| 41 void DidRecvMsg(scoped_ptr<std::vector<char> > buffer); |
| 42 void ReadDidFail(); |
| 43 |
| 44 private: |
| 45 class ReaderThreadRunner; |
| 46 |
| 47 bool CreatePipe(const IPC::ChannelHandle& channel_handle); |
| 48 bool ProcessOutgoingMessages(); |
| 49 int GetHelloMessageProcId(); |
| 50 void QueueHelloMessage(); |
| 51 |
| 52 // ChannelReader implementation. |
37 virtual ReadState ReadData(char* buffer, | 53 virtual ReadState ReadData(char* buffer, |
38 int buffer_len, | 54 int buffer_len, |
39 int* bytes_read) OVERRIDE; | 55 int* bytes_read) OVERRIDE; |
40 virtual bool WillDispatchInputMessage(Message* msg) OVERRIDE; | 56 virtual bool WillDispatchInputMessage(Message* msg) OVERRIDE; |
41 virtual bool DidEmptyInputBuffers() OVERRIDE; | 57 virtual bool DidEmptyInputBuffers() OVERRIDE; |
42 virtual void HandleHelloMessage(const Message& msg) OVERRIDE; | 58 virtual void HandleHelloMessage(const Message& msg) OVERRIDE; |
43 | 59 |
44 private: | 60 Mode mode_; |
| 61 base::ProcessId peer_pid_; |
| 62 bool waiting_connect_; |
| 63 |
| 64 // The pipe used for communication. |
| 65 int pipe_; |
| 66 |
| 67 // The "name" of our pipe. On Windows this is the global identifier for |
| 68 // the pipe. On POSIX it's used as a key in a local map of file descriptors. |
| 69 // For NaCl, we don't actually support looking up file descriptors by name, |
| 70 // and it's only used for debug information. |
| 71 std::string pipe_name_; |
| 72 |
| 73 // We use a thread for reading, so that we can simply block on reading and |
| 74 // post the received data back to the main thread to be properly interleaved |
| 75 // with other tasks in the MessagePump. |
| 76 // |
| 77 // imc_recvmsg supports non-blocking reads, but there's no easy way to be |
| 78 // informed when a write or read can be done without blocking (this is handled |
| 79 // by libevent in Posix). |
| 80 scoped_ptr<ReaderThreadRunner> reader_thread_runner_; |
| 81 scoped_ptr<base::DelegateSimpleThread> reader_thread_; |
| 82 |
| 83 // IPC::ChannelReader expects to be able to call ReadData on us to |
| 84 // synchronously read data waiting in the pipe's buffer without blocking. |
| 85 // Since we can't do that (see 1 and 2 above), the reader thread does blocking |
| 86 // reads and posts the data over to the main thread in byte vectors. Each byte |
| 87 // vector is the result of one call to "recvmsg". When ReadData is called, it |
| 88 // pulls the bytes out of these vectors in order. |
| 89 // TODO(dmichael): There's probably a more efficient way to emulate this with |
| 90 // a circular buffer or something, so we don't have to do so |
| 91 // many heap allocations. But it maybe isn't worth |
| 92 // the trouble given that we probably want to implement 1 and |
| 93 // 2 above in NaCl eventually. |
| 94 std::deque<linked_ptr<std::vector<char> > > read_queue_; |
| 95 |
| 96 // This queue is used when a message is sent prior to Connect having been |
| 97 // called. Normally after we're connected, the queue is empty. |
| 98 std::deque<linked_ptr<Message> > output_queue_; |
| 99 |
| 100 base::WeakPtrFactory<ChannelImpl> weak_ptr_factory_; |
| 101 |
45 DISALLOW_IMPLICIT_CONSTRUCTORS(ChannelImpl); | 102 DISALLOW_IMPLICIT_CONSTRUCTORS(ChannelImpl); |
46 }; | 103 }; |
47 | 104 |
48 } // namespace IPC | 105 } // namespace IPC |
49 | 106 |
50 #endif // IPC_IPC_CHANNEL_NACL_H_ | 107 #endif // IPC_IPC_CHANNEL_NACL_H_ |
OLD | NEW |