ipc: Make sure that ChannelReader is destroyed correctly.
[chromium-blink-merge.git] / ipc / brokerable_attachment.h
blob9c2bbbd69ec444ac0770a1df93cf7ae34110be20
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.
5 #ifndef IPC_BROKERABLE_ATTACHMENT_H_
6 #define IPC_BROKERABLE_ATTACHMENT_H_
8 #include <stdint.h>
10 #include "base/macros.h"
11 #include "ipc/ipc_export.h"
12 #include "ipc/ipc_message_attachment.h"
14 namespace IPC {
16 // This subclass of MessageAttachment requires an AttachmentBroker to be
17 // attached to a Chrome IPC message.
18 class IPC_EXPORT BrokerableAttachment : public MessageAttachment {
19 public:
20 static const size_t kNonceSize = 16;
21 // An id uniquely identifies an attachment sent via a broker.
22 struct IPC_EXPORT AttachmentId {
23 uint8_t nonce[kNonceSize];
25 // Default constructor returns an unguessable random nonce.
26 AttachmentId();
28 // Constructs an AttachmentId from a buffer.
29 AttachmentId(const char* start_address, size_t size);
31 // Writes the nonce into a buffer.
32 void SerializeToBuffer(char* start_address, size_t size);
34 bool operator==(const AttachmentId& rhs) const {
35 for (size_t i = 0; i < kNonceSize; ++i) {
36 if (nonce[i] != rhs.nonce[i])
37 return false;
39 return true;
42 bool operator<(const AttachmentId& rhs) const {
43 for (size_t i = 0; i < kNonceSize; ++i) {
44 if (nonce[i] < rhs.nonce[i])
45 return true;
46 if (nonce[i] > rhs.nonce[i])
47 return false;
49 return false;
53 enum BrokerableType {
54 WIN_HANDLE,
57 // The identifier is unique across all Chrome processes.
58 AttachmentId GetIdentifier() const;
60 // Whether the attachment still needs information from the broker before it
61 // can be used.
62 bool NeedsBrokering() const;
64 // Fills in the data of this instance with the data from |attachment|.
65 // This instance must require brokering, |attachment| must be brokered, and
66 // both instances must have the same identifier.
67 virtual void PopulateWithAttachment(
68 const BrokerableAttachment* attachment) = 0;
70 // Returns TYPE_BROKERABLE_ATTACHMENT
71 Type GetType() const override;
73 virtual BrokerableType GetBrokerableType() const = 0;
75 protected:
76 BrokerableAttachment();
77 BrokerableAttachment(const AttachmentId& id, bool needs_brokering);
78 ~BrokerableAttachment() override;
80 void SetNeedsBrokering(bool needs_brokering);
82 private:
83 // This member uniquely identifies a BrokerableAttachment across all Chrome
84 // processes.
85 const AttachmentId id_;
87 // Whether the attachment still needs to be filled in by an AttachmentBroker.
88 bool needs_brokering_;
89 DISALLOW_COPY_AND_ASSIGN(BrokerableAttachment);
92 } // namespace IPC
94 #endif // IPC_BROKERABLE_ATTACHMENT_H_