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_ATTACHMENT_BROKER_H_
6 #define IPC_ATTACHMENT_BROKER_H_
8 #include "base/gtest_prod_util.h"
9 #include "base/macros.h"
10 #include "base/process/process_handle.h"
11 #include "ipc/brokerable_attachment.h"
12 #include "ipc/ipc_export.h"
13 #include "ipc/ipc_listener.h"
15 // If the platform has no attachments that need brokering, then it shouldn't
16 // compile any code that calls member functions of AttachmentBroker. This
17 // prevents symbols only used by AttachmentBroker and its subclasses from
18 // making it into the binary.
20 #define USE_ATTACHMENT_BROKER 1
22 #define USE_ATTACHMENT_BROKER 0
23 #endif // defined(OS_WIN)
27 class AttachmentBroker
;
28 // Classes that inherit from this abstract base class are capable of
29 // communicating with a broker to send and receive attachments to Chrome IPC
31 class IPC_EXPORT SupportsAttachmentBrokering
{
33 // Returns an AttachmentBroker used to broker attachments of IPC messages to
34 // other processes. There must be exactly one AttachmentBroker per process.
35 virtual AttachmentBroker
* GetAttachmentBroker() = 0;
38 // Responsible for brokering attachments to Chrome IPC messages. On platforms
39 // that support attachment brokering, every IPC channel should have a reference
40 // to a AttachmentBroker.
41 // This class is not thread safe. The implementation of this class assumes that
42 // it is only ever used on the same thread as its consumers.
43 class IPC_EXPORT AttachmentBroker
: public Listener
{
45 // A standard observer interface that allows consumers of the AttachmentBroker
46 // to be notified when a new attachment has been received.
49 virtual void ReceivedBrokerableAttachmentWithId(
50 const BrokerableAttachment::AttachmentId
& id
) = 0;
54 ~AttachmentBroker() override
;
56 // Sends |attachment| to |destination_process|. The implementation uses an
57 // IPC::Channel to communicate with the broker process. This may be the same
58 // IPC::Channel that is requesting the brokering of an attachment.
59 // Returns true on success and false otherwise.
60 virtual bool SendAttachmentToProcess(const BrokerableAttachment
* attachment
,
61 base::ProcessId destination_process
) = 0;
63 // Returns whether the attachment was available. If the attachment was
64 // available, populates the output parameter |attachment|.
65 bool GetAttachmentWithId(BrokerableAttachment::AttachmentId id
,
66 scoped_refptr
<BrokerableAttachment
>* attachment
);
68 // Any given observer should only ever add itself once to the observer list.
69 void AddObserver(Observer
* observer
);
70 void RemoveObserver(Observer
* observer
);
73 using AttachmentVector
= std::vector
<scoped_refptr
<BrokerableAttachment
>>;
75 // Adds |attachment| to |attachments_|, and notifies the observers.
76 void HandleReceivedAttachment(
77 const scoped_refptr
<BrokerableAttachment
>& attachment
);
79 // Informs the observers that a new BrokerableAttachment has been received.
80 void NotifyObservers(const BrokerableAttachment::AttachmentId
& id
);
82 // This method is exposed for testing only.
83 AttachmentVector
* get_attachments() { return &attachments_
; }
87 FRIEND_TEST_ALL_PREFIXES(AttachmentBrokerUnprivilegedWinTest
,
89 FRIEND_TEST_ALL_PREFIXES(AttachmentBrokerUnprivilegedWinTest
,
90 ReceiveInvalidMessage
);
91 #endif // defined(OS_WIN)
93 // A vector of BrokerableAttachments that have been received, but not yet
95 // A std::vector is used instead of a std::map because this container is
96 // expected to have few elements, for which a std::vector is expected to have
97 // better performance.
98 AttachmentVector attachments_
;
100 std::vector
<Observer
*> observers_
;
101 DISALLOW_COPY_AND_ASSIGN(AttachmentBroker
);
106 #endif // IPC_ATTACHMENT_BROKER_H_