ipc_channel_nacl.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #ifndef IPC_IPC_CHANNEL_NACL_H_
  5. #define IPC_IPC_CHANNEL_NACL_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/containers/circular_deque.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/process/process.h"
  11. #include "base/threading/simple_thread.h"
  12. #include "ipc/ipc_channel.h"
  13. #include "ipc/ipc_channel_reader.h"
  14. namespace IPC {
  15. class MessageAttachment;
  16. // Contains the results from one call to imc_recvmsg (data and file
  17. // descriptors).
  18. struct MessageContents;
  19. // Similar to the ChannelPosix but for Native Client code.
  20. // This is somewhat different because sendmsg/recvmsg here do not follow POSIX
  21. // semantics. Instead, they are implemented by a custom embedding of
  22. // NaClDescCustom. See NaClIPCAdapter for the trusted-side implementation.
  23. //
  24. // We don't need to worry about complicated set up and READWRITE mode for
  25. // sharing handles. We also currently do not support passing file descriptors or
  26. // named pipes, and we use background threads to emulate signaling when we can
  27. // read or write without blocking.
  28. class ChannelNacl : public Channel,
  29. public internal::ChannelReader {
  30. public:
  31. ChannelNacl() = delete;
  32. // Mirror methods of Channel, see ipc_channel.h for description.
  33. ChannelNacl(const IPC::ChannelHandle& channel_handle,
  34. Mode mode,
  35. Listener* listener);
  36. ChannelNacl(const ChannelNacl&) = delete;
  37. ChannelNacl& operator=(const ChannelNacl&) = delete;
  38. ~ChannelNacl() override;
  39. // Channel implementation.
  40. bool Connect() override;
  41. void Close() override;
  42. bool Send(Message* message) override;
  43. // Posted to the main thread by ReaderThreadRunner.
  44. void DidRecvMsg(std::unique_ptr<MessageContents> contents);
  45. void ReadDidFail();
  46. private:
  47. class ReaderThreadRunner;
  48. bool CreatePipe(const IPC::ChannelHandle& channel_handle);
  49. bool ProcessOutgoingMessages();
  50. void CallOnChannelConnected();
  51. // ChannelReader implementation.
  52. ReadState ReadData(char* buffer,
  53. int buffer_len,
  54. int* bytes_read) override;
  55. bool ShouldDispatchInputMessage(Message* msg) override;
  56. bool GetAttachments(Message* msg) override;
  57. bool DidEmptyInputBuffers() override;
  58. void HandleInternalMessage(const Message& msg) override;
  59. Mode mode_;
  60. bool waiting_connect_;
  61. // The pipe used for communication.
  62. int pipe_;
  63. // We use a thread for reading, so that we can simply block on reading and
  64. // post the received data back to the main thread to be properly interleaved
  65. // with other tasks in the MessagePump.
  66. //
  67. // imc_recvmsg supports non-blocking reads, but there's no easy way to be
  68. // informed when a write or read can be done without blocking (this is handled
  69. // by libevent in Posix).
  70. std::unique_ptr<ReaderThreadRunner> reader_thread_runner_;
  71. std::unique_ptr<base::DelegateSimpleThread> reader_thread_;
  72. // IPC::ChannelReader expects to be able to call ReadData on us to
  73. // synchronously read data waiting in the pipe's buffer without blocking.
  74. // Since we can't do that (see 1 and 2 above), the reader thread does blocking
  75. // reads and posts the data over to the main thread in MessageContents. Each
  76. // MessageContents object is the result of one call to "imc_recvmsg".
  77. // DidRecvMsg breaks the MessageContents out in to the data and the file
  78. // descriptors, and puts them on these two queues.
  79. // TODO(dmichael): There's probably a more efficient way to emulate this with
  80. // a circular buffer or something, so we don't have to do so
  81. // many heap allocations. But it maybe isn't worth
  82. // the trouble given that we probably want to implement 1 and
  83. // 2 above in NaCl eventually.
  84. // When ReadData is called, it pulls the bytes out of this queue in order.
  85. base::circular_deque<std::unique_ptr<std::vector<char>>> read_queue_;
  86. // Queue of file descriptor attachments extracted from imc_recvmsg messages.
  87. std::vector<scoped_refptr<MessageAttachment>> input_attachments_;
  88. // This queue is used when a message is sent prior to Connect having been
  89. // called. Normally after we're connected, the queue is empty.
  90. base::circular_deque<std::unique_ptr<Message>> output_queue_;
  91. base::WeakPtrFactory<ChannelNacl> weak_ptr_factory_;
  92. };
  93. } // namespace IPC
  94. #endif // IPC_IPC_CHANNEL_NACL_H_