OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef IPC_ATTACHMENT_BROKER_H_ | 5 #ifndef IPC_ATTACHMENT_BROKER_H_ |
6 #define IPC_ATTACHMENT_BROKER_H_ | 6 #define IPC_ATTACHMENT_BROKER_H_ |
7 | 7 |
8 #include "base/gtest_prod_util.h" | 8 #include "base/gtest_prod_util.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/process/process_handle.h" | 10 #include "base/process/process_handle.h" |
11 #include "ipc/brokerable_attachment.h" | 11 #include "ipc/brokerable_attachment.h" |
12 #include "ipc/ipc_export.h" | 12 #include "ipc/ipc_export.h" |
13 #include "ipc/ipc_listener.h" | 13 #include "ipc/ipc_listener.h" |
14 | 14 |
15 // If the platform has no attachments that need brokering, then it shouldn't | 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 | 16 // compile any code that calls member functions of AttachmentBroker. This |
17 // prevents symbols only used by AttachmentBroker and its subclasses from | 17 // prevents symbols only used by AttachmentBroker and its subclasses from |
18 // making it into the binary. | 18 // making it into the binary. |
19 #if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) | 19 #if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) |
20 #define USE_ATTACHMENT_BROKER 1 | 20 #define USE_ATTACHMENT_BROKER 1 |
21 #else | 21 #else |
22 #define USE_ATTACHMENT_BROKER 0 | 22 #define USE_ATTACHMENT_BROKER 0 |
23 #endif // defined(OS_WIN) | 23 #endif // defined(OS_WIN) |
24 | 24 |
| 25 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 26 namespace base { |
| 27 class PortProvider; |
| 28 } // namespace base |
| 29 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 30 |
25 namespace IPC { | 31 namespace IPC { |
26 | 32 |
27 class AttachmentBroker; | 33 class AttachmentBroker; |
| 34 class Endpoint; |
| 35 |
28 // Classes that inherit from this abstract base class are capable of | 36 // Classes that inherit from this abstract base class are capable of |
29 // communicating with a broker to send and receive attachments to Chrome IPC | 37 // communicating with a broker to send and receive attachments to Chrome IPC |
30 // messages. | 38 // messages. |
31 class IPC_EXPORT SupportsAttachmentBrokering { | 39 class IPC_EXPORT SupportsAttachmentBrokering { |
32 public: | 40 public: |
33 // Returns an AttachmentBroker used to broker attachments of IPC messages to | 41 // Returns an AttachmentBroker used to broker attachments of IPC messages to |
34 // other processes. There must be exactly one AttachmentBroker per process. | 42 // other processes. There must be exactly one AttachmentBroker per process. |
35 virtual AttachmentBroker* GetAttachmentBroker() = 0; | 43 virtual AttachmentBroker* GetAttachmentBroker() = 0; |
36 }; | 44 }; |
37 | 45 |
(...skipping 30 matching lines...) Expand all Loading... |
68 | 76 |
69 // Returns whether the attachment was available. If the attachment was | 77 // Returns whether the attachment was available. If the attachment was |
70 // available, populates the output parameter |attachment|. | 78 // available, populates the output parameter |attachment|. |
71 bool GetAttachmentWithId(BrokerableAttachment::AttachmentId id, | 79 bool GetAttachmentWithId(BrokerableAttachment::AttachmentId id, |
72 scoped_refptr<BrokerableAttachment>* attachment); | 80 scoped_refptr<BrokerableAttachment>* attachment); |
73 | 81 |
74 // Any given observer should only ever add itself once to the observer list. | 82 // Any given observer should only ever add itself once to the observer list. |
75 void AddObserver(Observer* observer); | 83 void AddObserver(Observer* observer); |
76 void RemoveObserver(Observer* observer); | 84 void RemoveObserver(Observer* observer); |
77 | 85 |
| 86 // These two methods should only be called by the broker process. |
| 87 // |
| 88 // Each unprivileged process should have one IPC channel on which it |
| 89 // communicates attachment information with the broker process. In the broker |
| 90 // process, these channels must be registered and deregistered with the |
| 91 // Attachment Broker as they are created and destroyed. |
| 92 virtual void RegisterCommunicationChannel(Endpoint* endpoint); |
| 93 virtual void DeregisterCommunicationChannel(Endpoint* endpoint); |
| 94 |
| 95 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 96 // This method should only be called by the broker process. |
| 97 // |
| 98 // The port provider must live as long as the AttachmentBroker. A port |
| 99 // provider must be set before any attachment brokering occurs. |
| 100 void set_port_provider(base::PortProvider* port_provider) { |
| 101 DCHECK(!port_provider_); |
| 102 port_provider_ = port_provider; |
| 103 } |
| 104 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 105 |
78 protected: | 106 protected: |
79 using AttachmentVector = std::vector<scoped_refptr<BrokerableAttachment>>; | 107 using AttachmentVector = std::vector<scoped_refptr<BrokerableAttachment>>; |
80 | 108 |
81 // Adds |attachment| to |attachments_|, and notifies the observers. | 109 // Adds |attachment| to |attachments_|, and notifies the observers. |
82 void HandleReceivedAttachment( | 110 void HandleReceivedAttachment( |
83 const scoped_refptr<BrokerableAttachment>& attachment); | 111 const scoped_refptr<BrokerableAttachment>& attachment); |
84 | 112 |
85 // Informs the observers that a new BrokerableAttachment has been received. | 113 // Informs the observers that a new BrokerableAttachment has been received. |
86 void NotifyObservers(const BrokerableAttachment::AttachmentId& id); | 114 void NotifyObservers(const BrokerableAttachment::AttachmentId& id); |
87 | 115 |
88 // This method is exposed for testing only. | 116 // This method is exposed for testing only. |
89 AttachmentVector* get_attachments() { return &attachments_; } | 117 AttachmentVector* get_attachments() { return &attachments_; } |
90 | 118 |
| 119 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 120 base::PortProvider* port_provider() const { return port_provider_; } |
| 121 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 122 |
91 private: | 123 private: |
92 #if defined(OS_WIN) | 124 #if defined(OS_WIN) |
93 FRIEND_TEST_ALL_PREFIXES(AttachmentBrokerUnprivilegedWinTest, | 125 FRIEND_TEST_ALL_PREFIXES(AttachmentBrokerUnprivilegedWinTest, |
94 ReceiveValidMessage); | 126 ReceiveValidMessage); |
95 FRIEND_TEST_ALL_PREFIXES(AttachmentBrokerUnprivilegedWinTest, | 127 FRIEND_TEST_ALL_PREFIXES(AttachmentBrokerUnprivilegedWinTest, |
96 ReceiveInvalidMessage); | 128 ReceiveInvalidMessage); |
97 #endif // defined(OS_WIN) | 129 #endif // defined(OS_WIN) |
98 | 130 |
| 131 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 132 base::PortProvider* port_provider_; |
| 133 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 134 |
99 // A vector of BrokerableAttachments that have been received, but not yet | 135 // A vector of BrokerableAttachments that have been received, but not yet |
100 // consumed. | 136 // consumed. |
101 // A std::vector is used instead of a std::map because this container is | 137 // A std::vector is used instead of a std::map because this container is |
102 // expected to have few elements, for which a std::vector is expected to have | 138 // expected to have few elements, for which a std::vector is expected to have |
103 // better performance. | 139 // better performance. |
104 AttachmentVector attachments_; | 140 AttachmentVector attachments_; |
105 | 141 |
106 std::vector<Observer*> observers_; | 142 std::vector<Observer*> observers_; |
107 DISALLOW_COPY_AND_ASSIGN(AttachmentBroker); | 143 DISALLOW_COPY_AND_ASSIGN(AttachmentBroker); |
108 }; | 144 }; |
109 | 145 |
110 } // namespace IPC | 146 } // namespace IPC |
111 | 147 |
112 #endif // IPC_ATTACHMENT_BROKER_H_ | 148 #endif // IPC_ATTACHMENT_BROKER_H_ |
OLD | NEW |