blob: 50e7fd21eb954f4d18a57097b4f23213b7856bf2 [file] [log] [blame]
erikchen151b2f92015-06-16 20:20:511// 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
avi246998d82015-12-22 02:39:048#include <stddef.h>
erikchen151b2f92015-06-16 20:20:519#include <stdint.h>
10
jsbellcc7382512015-11-17 18:16:3711#include <algorithm>
12
erikchen151b2f92015-06-16 20:20:5113#include "base/macros.h"
avi246998d82015-12-22 02:39:0414#include "build/build_config.h"
erikchen151b2f92015-06-16 20:20:5115#include "ipc/ipc_export.h"
16#include "ipc/ipc_message_attachment.h"
17
18namespace IPC {
19
20// This subclass of MessageAttachment requires an AttachmentBroker to be
21// attached to a Chrome IPC message.
22class IPC_EXPORT BrokerableAttachment : public MessageAttachment {
23 public:
erikcheneece6c32015-07-07 22:13:1124 static const size_t kNonceSize = 16;
erikchen151b2f92015-06-16 20:20:5125 // An id uniquely identifies an attachment sent via a broker.
26 struct IPC_EXPORT AttachmentId {
erikcheneece6c32015-07-07 22:13:1127 uint8_t nonce[kNonceSize];
erikchende9412b82015-07-27 18:26:1428
erikchen28299a12015-09-24 00:10:2729 // 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.
erikchena5085cda2015-09-15 17:26:2735 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
erikchende9412b82015-07-27 18:26:1443 bool operator==(const AttachmentId& rhs) const {
jsbellcc7382512015-11-17 18:16:3744 return std::equal(nonce, nonce + kNonceSize, rhs.nonce);
erikchende9412b82015-07-27 18:26:1445 }
46
47 bool operator<(const AttachmentId& rhs) const {
jsbellcc7382512015-11-17 18:16:3748 return std::lexicographical_compare(nonce, nonce + kNonceSize, rhs.nonce,
49 rhs.nonce + kNonceSize);
erikchende9412b82015-07-27 18:26:1450 }
erikcheneece6c32015-07-07 22:13:1151 };
52
53 enum BrokerableType {
erikchen87351da2015-09-15 19:11:0954 PLACEHOLDER,
erikcheneece6c32015-07-07 22:13:1155 WIN_HANDLE,
erikchen1c1e6652015-10-01 18:51:3256 MACH_PORT,
erikchen151b2f92015-06-16 20:20:5157 };
58
59 // The identifier is unique across all Chrome processes.
60 AttachmentId GetIdentifier() const;
61
erikchende9412b82015-07-27 18:26:1462 // Whether the attachment still needs information from the broker before it
63 // can be used.
64 bool NeedsBrokering() const;
65
erikcheneece6c32015-07-07 22:13:1166 // Returns TYPE_BROKERABLE_ATTACHMENT
67 Type GetType() const override;
68
69 virtual BrokerableType GetBrokerableType() const = 0;
70
erikchen87351da2015-09-15 19:11:0971// MessageAttachment override.
72#if defined(OS_POSIX)
73 base::PlatformFile TakePlatformFile() override;
74#endif // OS_POSIX
75
erikchen151b2f92015-06-16 20:20:5176 protected:
erikchena5085cda2015-09-15 17:26:2777 BrokerableAttachment();
erikchen87351da2015-09-15 19:11:0978 BrokerableAttachment(const AttachmentId& id);
erikchen151b2f92015-06-16 20:20:5179 ~BrokerableAttachment() override;
80
81 private:
82 // This member uniquely identifies a BrokerableAttachment across all Chrome
83 // processes.
erikcheneece6c32015-07-07 22:13:1184 const AttachmentId id_;
erikchende9412b82015-07-27 18:26:1485
erikchen151b2f92015-06-16 20:20:5186 DISALLOW_COPY_AND_ASSIGN(BrokerableAttachment);
87};
88
89} // namespace IPC
90
91#endif // IPC_BROKERABLE_ATTACHMENT_H_