ipc_message_attachment_set.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (c) 2011 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_MESSAGE_ATTACHMENT_SET_H_
  5. #define IPC_IPC_MESSAGE_ATTACHMENT_SET_H_
  6. #include <stddef.h>
  7. #include <vector>
  8. #include "base/memory/ref_counted.h"
  9. #include "build/build_config.h"
  10. #include "ipc/ipc_message_support_export.h"
  11. namespace IPC {
  12. class MessageAttachment;
  13. // -----------------------------------------------------------------------------
  14. // A MessageAttachmentSet is an ordered set of MessageAttachment objects
  15. // associated with an IPC message. All attachments are wrapped in a mojo handle
  16. // if necessary and sent over the mojo message pipe.
  17. //
  18. // For ChannelNacl under SFI NaCl, only Type::PLATFORM_FILE is supported. In
  19. // that case, the FD is sent over socket.
  20. // -----------------------------------------------------------------------------
  21. class IPC_MESSAGE_SUPPORT_EXPORT MessageAttachmentSet
  22. : public base::RefCountedThreadSafe<MessageAttachmentSet> {
  23. public:
  24. MessageAttachmentSet();
  25. MessageAttachmentSet(const MessageAttachmentSet&) = delete;
  26. MessageAttachmentSet& operator=(const MessageAttachmentSet&) = delete;
  27. // Return the number of attachments
  28. unsigned size() const;
  29. // Return true if no unconsumed descriptors remain
  30. bool empty() const { return attachments_.empty(); }
  31. // Returns whether the attachment was successfully added.
  32. // |index| is an output variable. On success, it contains the index of the
  33. // newly added attachment.
  34. bool AddAttachment(scoped_refptr<MessageAttachment> attachment,
  35. size_t* index);
  36. // Similar to the above method, but without output variables.
  37. bool AddAttachment(scoped_refptr<MessageAttachment> attachment);
  38. // Take the nth from the beginning of the vector, Code using this /must/
  39. // access the attachments in order, and must do it at most once.
  40. //
  41. // This interface is designed for the deserialising code as it doesn't
  42. // support close flags.
  43. // returns: an attachment, or nullptr on error
  44. scoped_refptr<MessageAttachment> GetAttachmentAt(unsigned index);
  45. // Marks all the descriptors as consumed and closes those which are
  46. // auto-close.
  47. void CommitAllDescriptors();
  48. #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  49. // This is the maximum number of descriptors per message. We need to know this
  50. // because the control message kernel interface has to be given a buffer which
  51. // is large enough to store all the descriptor numbers. Otherwise the kernel
  52. // tells us that it truncated the control data and the extra descriptors are
  53. // lost.
  54. //
  55. // In debugging mode, it's a fatal error to try and add more than this number
  56. // of descriptors to a MessageAttachmentSet.
  57. static const size_t kMaxDescriptorsPerMessage = 7;
  58. #endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  59. // ---------------------------------------------------------------------------
  60. private:
  61. friend class base::RefCountedThreadSafe<MessageAttachmentSet>;
  62. ~MessageAttachmentSet();
  63. // Return the number of file descriptors
  64. unsigned num_descriptors() const;
  65. std::vector<scoped_refptr<MessageAttachment>> attachments_;
  66. // This contains the index of the next descriptor which should be consumed.
  67. // It's used in a couple of ways. Firstly, at destruction we can check that
  68. // all the descriptors have been read (with GetNthDescriptor). Secondly, we
  69. // can check that they are read in order.
  70. unsigned consumed_descriptor_highwater_;
  71. };
  72. } // namespace IPC
  73. #endif // IPC_IPC_MESSAGE_ATTACHMENT_SET_H_