ipc: Make sure that ChannelReader is destroyed correctly.
[chromium-blink-merge.git] / ipc / attachment_broker.h
blob5b65289393706545e221d7f05b2df9f615b88068
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.
19 #if defined(OS_WIN)
20 #define USE_ATTACHMENT_BROKER 1
21 #else
22 #define USE_ATTACHMENT_BROKER 0
23 #endif // defined(OS_WIN)
25 namespace IPC {
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
30 // messages.
31 class IPC_EXPORT SupportsAttachmentBrokering {
32 public:
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 {
44 public:
45 // A standard observer interface that allows consumers of the AttachmentBroker
46 // to be notified when a new attachment has been received.
47 class Observer {
48 public:
49 virtual void ReceivedBrokerableAttachmentWithId(
50 const BrokerableAttachment::AttachmentId& id) = 0;
53 AttachmentBroker();
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);
72 protected:
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_; }
85 private:
86 #if defined(OS_WIN)
87 FRIEND_TEST_ALL_PREFIXES(AttachmentBrokerUnprivilegedWinTest,
88 ReceiveValidMessage);
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
94 // consumed.
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);
104 } // namespace IPC
106 #endif // IPC_ATTACHMENT_BROKER_H_