erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 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 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 8 | #include <stddef.h> |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | |
jsbell | cc738251 | 2015-11-17 18:16:37 | [diff] [blame] | 11 | #include <algorithm> |
| 12 | |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 13 | #include "base/macros.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 14 | #include "build/build_config.h" |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 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: |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 24 | static const size_t kNonceSize = 16; |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 25 | // An id uniquely identifies an attachment sent via a broker. |
| 26 | struct IPC_EXPORT AttachmentId { |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 27 | uint8_t nonce[kNonceSize]; |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 28 | |
erikchen | 28299a1 | 2015-09-24 00:10:27 | [diff] [blame] | 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. |
erikchen | a5085cda | 2015-09-15 17:26:27 | [diff] [blame] | 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 | |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 43 | bool operator==(const AttachmentId& rhs) const { |
jsbell | cc738251 | 2015-11-17 18:16:37 | [diff] [blame] | 44 | return std::equal(nonce, nonce + kNonceSize, rhs.nonce); |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | bool operator<(const AttachmentId& rhs) const { |
jsbell | cc738251 | 2015-11-17 18:16:37 | [diff] [blame] | 48 | return std::lexicographical_compare(nonce, nonce + kNonceSize, rhs.nonce, |
| 49 | rhs.nonce + kNonceSize); |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 50 | } |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | enum BrokerableType { |
erikchen | 87351da | 2015-09-15 19:11:09 | [diff] [blame] | 54 | PLACEHOLDER, |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 55 | WIN_HANDLE, |
erikchen | 1c1e665 | 2015-10-01 18:51:32 | [diff] [blame] | 56 | MACH_PORT, |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | // The identifier is unique across all Chrome processes. |
| 60 | AttachmentId GetIdentifier() const; |
| 61 | |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 62 | // Whether the attachment still needs information from the broker before it |
| 63 | // can be used. |
| 64 | bool NeedsBrokering() const; |
| 65 | |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 66 | // Returns TYPE_BROKERABLE_ATTACHMENT |
| 67 | Type GetType() const override; |
| 68 | |
| 69 | virtual BrokerableType GetBrokerableType() const = 0; |
| 70 | |
erikchen | 87351da | 2015-09-15 19:11:09 | [diff] [blame] | 71 | // MessageAttachment override. |
| 72 | #if defined(OS_POSIX) |
| 73 | base::PlatformFile TakePlatformFile() override; |
| 74 | #endif // OS_POSIX |
| 75 | |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 76 | protected: |
erikchen | a5085cda | 2015-09-15 17:26:27 | [diff] [blame] | 77 | BrokerableAttachment(); |
erikchen | 87351da | 2015-09-15 19:11:09 | [diff] [blame] | 78 | BrokerableAttachment(const AttachmentId& id); |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 79 | ~BrokerableAttachment() override; |
| 80 | |
| 81 | private: |
| 82 | // This member uniquely identifies a BrokerableAttachment across all Chrome |
| 83 | // processes. |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 84 | const AttachmentId id_; |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 85 | |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 86 | DISALLOW_COPY_AND_ASSIGN(BrokerableAttachment); |
| 87 | }; |
| 88 | |
| 89 | } // namespace IPC |
| 90 | |
| 91 | #endif // IPC_BROKERABLE_ATTACHMENT_H_ |