1 // Copyright 2015 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 #ifndef IPC_BROKERABLE_ATTACHMENT_H_ 6 #define IPC_BROKERABLE_ATTACHMENT_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <algorithm> 12 13 #include "base/macros.h" 14 #include "build/build_config.h" 15 #include "ipc/ipc_export.h" 16 #include "ipc/ipc_message_attachment.h" 17 18 namespace IPC { 19 20 // This subclass of MessageAttachment requires an AttachmentBroker to be 21 // attached to a Chrome IPC message. 22 class IPC_EXPORT BrokerableAttachment : public MessageAttachment { 23 public: 24 static const size_t kNonceSize = 16; 25 // An id uniquely identifies an attachment sent via a broker. 26 struct IPC_EXPORT AttachmentId { 27 uint8_t nonce[kNonceSize]; 28 29 // Generates an AttachmentId with an unguessable, random nonce. 30 static AttachmentId CreateIdWithRandomNonce(); 31 32 // Creates an AttachmentId with a zeroed nonce. This should only be used by 33 // the IPC translation system, which requires that classes have a default 34 // constructor. 35 AttachmentId(); 36 37 // Constructs an AttachmentId from a buffer. 38 AttachmentId(const char* start_address, size_t size); 39 40 // Writes the nonce into a buffer. 41 void SerializeToBuffer(char* start_address, size_t size); 42 43 bool operator==(const AttachmentId& rhs) const { 44 return std::equal(nonce, nonce + kNonceSize, rhs.nonce); 45 } 46 47 bool operator<(const AttachmentId& rhs) const { 48 return std::lexicographical_compare(nonce, nonce + kNonceSize, rhs.nonce, 49 rhs.nonce + kNonceSize); 50 } 51 }; 52 53 enum BrokerableType { 54 PLACEHOLDER, 55 WIN_HANDLE, 56 MACH_PORT, 57 }; 58 59 // The identifier is unique across all Chrome processes. 60 AttachmentId GetIdentifier() const; 61 62 // Whether the attachment still needs information from the broker before it 63 // can be used. 64 bool NeedsBrokering() const; 65 66 // Returns TYPE_BROKERABLE_ATTACHMENT 67 Type GetType() const override; 68 69 virtual BrokerableType GetBrokerableType() const = 0; 70 71 // MessageAttachment override. 72 #if defined(OS_POSIX) 73 base::PlatformFile TakePlatformFile() override; 74 #endif // OS_POSIX 75 76 protected: 77 BrokerableAttachment(); 78 BrokerableAttachment(const AttachmentId& id); 79 ~BrokerableAttachment() override; 80 81 private: 82 // This member uniquely identifies a BrokerableAttachment across all Chrome 83 // processes. 84 const AttachmentId id_; 85 86 DISALLOW_COPY_AND_ASSIGN(BrokerableAttachment); 87 }; 88 89 } // namespace IPC 90 91 #endif // IPC_BROKERABLE_ATTACHMENT_H_ 92