erikchen | 62d3813 | 2015-06-17 05:20:51 +0900 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef IPC_ATTACHMENT_BROKER_H_ |
| 6 | #define IPC_ATTACHMENT_BROKER_H_ |
| 7 | |
erikchen | 3155dee | 2015-07-28 05:28:20 +0900 | [diff] [blame] | 8 | #include "base/gtest_prod_util.h" |
erikchen | 62d3813 | 2015-06-17 05:20:51 +0900 | [diff] [blame] | 9 | #include "base/macros.h" |
erikchen | 32e67f3 | 2015-10-30 07:37:04 +0900 | [diff] [blame] | 10 | #include "base/memory/ref_counted.h" |
erikchen | 62d3813 | 2015-06-17 05:20:51 +0900 | [diff] [blame] | 11 | #include "base/process/process_handle.h" |
erikchen | 5f9320c | 2015-10-30 07:54:42 +0900 | [diff] [blame] | 12 | #include "base/synchronization/lock.h" |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 13 | #include "build/build_config.h" |
erikchen | 62d3813 | 2015-06-17 05:20:51 +0900 | [diff] [blame] | 14 | #include "ipc/brokerable_attachment.h" |
| 15 | #include "ipc/ipc_export.h" |
erikchen | da5404b | 2015-07-15 10:35:39 +0900 | [diff] [blame] | 16 | #include "ipc/ipc_listener.h" |
erikchen | 62d3813 | 2015-06-17 05:20:51 +0900 | [diff] [blame] | 17 | |
erikchen | f295bbc | 2015-07-28 03:26:14 +0900 | [diff] [blame] | 18 | // If the platform has no attachments that need brokering, then it shouldn't |
| 19 | // compile any code that calls member functions of AttachmentBroker. This |
| 20 | // prevents symbols only used by AttachmentBroker and its subclasses from |
| 21 | // making it into the binary. |
erikchen | a422982 | 2015-10-10 08:20:49 +0900 | [diff] [blame] | 22 | #if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) |
erikchen | f295bbc | 2015-07-28 03:26:14 +0900 | [diff] [blame] | 23 | #define USE_ATTACHMENT_BROKER 1 |
| 24 | #else |
| 25 | #define USE_ATTACHMENT_BROKER 0 |
| 26 | #endif // defined(OS_WIN) |
| 27 | |
erikchen | 0bf1240 | 2015-11-07 06:12:36 +0900 | [diff] [blame] | 28 | namespace base { |
| 29 | class SequencedTaskRunner; |
erikchen | 0209d14 | 2016-03-22 08:19:40 +0900 | [diff] [blame] | 30 | class SingleThreadTaskRunner; |
erikchen | 0bf1240 | 2015-11-07 06:12:36 +0900 | [diff] [blame] | 31 | }; |
| 32 | |
erikchen | 62d3813 | 2015-06-17 05:20:51 +0900 | [diff] [blame] | 33 | namespace IPC { |
| 34 | |
| 35 | class AttachmentBroker; |
erikchen | 5192140 | 2015-10-22 04:18:25 +0900 | [diff] [blame] | 36 | class Endpoint; |
| 37 | |
erikchen | 62d3813 | 2015-06-17 05:20:51 +0900 | [diff] [blame] | 38 | // Classes that inherit from this abstract base class are capable of |
| 39 | // communicating with a broker to send and receive attachments to Chrome IPC |
| 40 | // messages. |
| 41 | class IPC_EXPORT SupportsAttachmentBrokering { |
| 42 | public: |
| 43 | // Returns an AttachmentBroker used to broker attachments of IPC messages to |
| 44 | // other processes. There must be exactly one AttachmentBroker per process. |
| 45 | virtual AttachmentBroker* GetAttachmentBroker() = 0; |
| 46 | }; |
| 47 | |
| 48 | // Responsible for brokering attachments to Chrome IPC messages. On platforms |
| 49 | // that support attachment brokering, every IPC channel should have a reference |
| 50 | // to a AttachmentBroker. |
erikchen | f295bbc | 2015-07-28 03:26:14 +0900 | [diff] [blame] | 51 | // This class is not thread safe. The implementation of this class assumes that |
| 52 | // it is only ever used on the same thread as its consumers. |
erikchen | da5404b | 2015-07-15 10:35:39 +0900 | [diff] [blame] | 53 | class IPC_EXPORT AttachmentBroker : public Listener { |
erikchen | 62d3813 | 2015-06-17 05:20:51 +0900 | [diff] [blame] | 54 | public: |
erikchen | f295bbc | 2015-07-28 03:26:14 +0900 | [diff] [blame] | 55 | // A standard observer interface that allows consumers of the AttachmentBroker |
| 56 | // to be notified when a new attachment has been received. |
| 57 | class Observer { |
| 58 | public: |
| 59 | virtual void ReceivedBrokerableAttachmentWithId( |
| 60 | const BrokerableAttachment::AttachmentId& id) = 0; |
| 61 | }; |
| 62 | |
erikchen | 2ffe51b | 2015-09-15 02:45:12 +0900 | [diff] [blame] | 63 | // Each process has at most one attachment broker. The process is responsible |
| 64 | // for ensuring that |broker| stays alive for as long as the process is |
| 65 | // sending/receiving ipc messages. |
| 66 | static void SetGlobal(AttachmentBroker* broker); |
| 67 | static AttachmentBroker* GetGlobal(); |
| 68 | |
erikchen | f295bbc | 2015-07-28 03:26:14 +0900 | [diff] [blame] | 69 | AttachmentBroker(); |
| 70 | ~AttachmentBroker() override; |
erikchen | 62d3813 | 2015-06-17 05:20:51 +0900 | [diff] [blame] | 71 | |
| 72 | // Sends |attachment| to |destination_process|. The implementation uses an |
| 73 | // IPC::Channel to communicate with the broker process. This may be the same |
| 74 | // IPC::Channel that is requesting the brokering of an attachment. |
erikchen | fdd43fe | 2015-07-08 07:13:11 +0900 | [diff] [blame] | 75 | // Returns true on success and false otherwise. |
erikchen | 32e67f3 | 2015-10-30 07:37:04 +0900 | [diff] [blame] | 76 | virtual bool SendAttachmentToProcess( |
| 77 | const scoped_refptr<BrokerableAttachment>& attachment, |
| 78 | base::ProcessId destination_process) = 0; |
erikchen | 62d3813 | 2015-06-17 05:20:51 +0900 | [diff] [blame] | 79 | |
| 80 | // Returns whether the attachment was available. If the attachment was |
erikchen | f295bbc | 2015-07-28 03:26:14 +0900 | [diff] [blame] | 81 | // available, populates the output parameter |attachment|. |
erikchen | 3155dee | 2015-07-28 05:28:20 +0900 | [diff] [blame] | 82 | bool GetAttachmentWithId(BrokerableAttachment::AttachmentId id, |
| 83 | scoped_refptr<BrokerableAttachment>* attachment); |
erikchen | f295bbc | 2015-07-28 03:26:14 +0900 | [diff] [blame] | 84 | |
| 85 | // Any given observer should only ever add itself once to the observer list. |
erikchen | 0bf1240 | 2015-11-07 06:12:36 +0900 | [diff] [blame] | 86 | // Notifications to |observer| will be posted to |runner|. |
| 87 | // The |observer| is expected to call RemoveObserver() before being destroyed. |
| 88 | void AddObserver(Observer* observer, |
| 89 | const scoped_refptr<base::SequencedTaskRunner>& runner); |
erikchen | f295bbc | 2015-07-28 03:26:14 +0900 | [diff] [blame] | 90 | void RemoveObserver(Observer* observer); |
| 91 | |
erikchen | 5192140 | 2015-10-22 04:18:25 +0900 | [diff] [blame] | 92 | // These two methods should only be called by the broker process. |
| 93 | // |
| 94 | // Each unprivileged process should have one IPC channel on which it |
| 95 | // communicates attachment information with the broker process. In the broker |
| 96 | // process, these channels must be registered and deregistered with the |
| 97 | // Attachment Broker as they are created and destroyed. |
erikchen | 0209d14 | 2016-03-22 08:19:40 +0900 | [diff] [blame] | 98 | // |
| 99 | // Invocations of Send() on |endpoint| will occur on thread bound to |runner|. |
| 100 | virtual void RegisterCommunicationChannel( |
| 101 | Endpoint* endpoint, |
| 102 | scoped_refptr<base::SingleThreadTaskRunner> runner); |
erikchen | 5192140 | 2015-10-22 04:18:25 +0900 | [diff] [blame] | 103 | virtual void DeregisterCommunicationChannel(Endpoint* endpoint); |
| 104 | |
erikchen | d790b95 | 2016-02-17 13:09:14 +0900 | [diff] [blame] | 105 | // In each unprivileged process, exactly one channel should be used to |
| 106 | // communicate brokerable attachments with the broker process. |
| 107 | virtual void RegisterBrokerCommunicationChannel(Endpoint* endpoint); |
| 108 | virtual void DeregisterBrokerCommunicationChannel(Endpoint* endpoint); |
| 109 | |
| 110 | // True if and only if this broker is privileged. |
| 111 | virtual bool IsPrivilegedBroker(); |
| 112 | |
erikchen | f295bbc | 2015-07-28 03:26:14 +0900 | [diff] [blame] | 113 | protected: |
erikchen | 3155dee | 2015-07-28 05:28:20 +0900 | [diff] [blame] | 114 | using AttachmentVector = std::vector<scoped_refptr<BrokerableAttachment>>; |
| 115 | |
| 116 | // Adds |attachment| to |attachments_|, and notifies the observers. |
| 117 | void HandleReceivedAttachment( |
| 118 | const scoped_refptr<BrokerableAttachment>& attachment); |
| 119 | |
| 120 | // Informs the observers that a new BrokerableAttachment has been received. |
erikchen | f295bbc | 2015-07-28 03:26:14 +0900 | [diff] [blame] | 121 | void NotifyObservers(const BrokerableAttachment::AttachmentId& id); |
erikchen | 62d3813 | 2015-06-17 05:20:51 +0900 | [diff] [blame] | 122 | |
erikchen | 0bf1240 | 2015-11-07 06:12:36 +0900 | [diff] [blame] | 123 | // Informs the observer identified by |unique_id| that a new |
| 124 | // BrokerableAttachment has been received. |
| 125 | void NotifyObserver(int unique_id, |
| 126 | const BrokerableAttachment::AttachmentId& id); |
| 127 | |
erikchen | 3155dee | 2015-07-28 05:28:20 +0900 | [diff] [blame] | 128 | // This method is exposed for testing only. |
| 129 | AttachmentVector* get_attachments() { return &attachments_; } |
| 130 | |
erikchen | 5f9320c | 2015-10-30 07:54:42 +0900 | [diff] [blame] | 131 | base::Lock* get_lock() { return &lock_; } |
| 132 | |
erikchen | 62d3813 | 2015-06-17 05:20:51 +0900 | [diff] [blame] | 133 | private: |
erikchen | 3155dee | 2015-07-28 05:28:20 +0900 | [diff] [blame] | 134 | #if defined(OS_WIN) |
erikchen | 7870b09 | 2015-07-29 08:25:44 +0900 | [diff] [blame] | 135 | FRIEND_TEST_ALL_PREFIXES(AttachmentBrokerUnprivilegedWinTest, |
| 136 | ReceiveValidMessage); |
| 137 | FRIEND_TEST_ALL_PREFIXES(AttachmentBrokerUnprivilegedWinTest, |
| 138 | ReceiveInvalidMessage); |
erikchen | 3155dee | 2015-07-28 05:28:20 +0900 | [diff] [blame] | 139 | #endif // defined(OS_WIN) |
| 140 | |
| 141 | // A vector of BrokerableAttachments that have been received, but not yet |
| 142 | // consumed. |
| 143 | // A std::vector is used instead of a std::map because this container is |
| 144 | // expected to have few elements, for which a std::vector is expected to have |
| 145 | // better performance. |
| 146 | AttachmentVector attachments_; |
erikchen | f295bbc | 2015-07-28 03:26:14 +0900 | [diff] [blame] | 147 | |
erikchen | 0bf1240 | 2015-11-07 06:12:36 +0900 | [diff] [blame] | 148 | struct ObserverInfo { |
| 149 | ObserverInfo(); |
vmpstr | 65104a8 | 2016-02-25 06:26:11 +0900 | [diff] [blame] | 150 | ObserverInfo(const ObserverInfo& other); |
erikchen | 0bf1240 | 2015-11-07 06:12:36 +0900 | [diff] [blame] | 151 | ~ObserverInfo(); |
| 152 | |
| 153 | Observer* observer; |
| 154 | int unique_id; |
| 155 | |
| 156 | // Notifications must be dispatched onto |runner|. |
| 157 | scoped_refptr<base::SequencedTaskRunner> runner; |
| 158 | }; |
| 159 | std::vector<ObserverInfo> observers_; |
| 160 | |
| 161 | // This member holds the last id given to an ObserverInfo. |
| 162 | int last_unique_id_; |
erikchen | 5f9320c | 2015-10-30 07:54:42 +0900 | [diff] [blame] | 163 | |
| 164 | // The AttachmentBroker can be accessed from any thread, so modifications to |
| 165 | // internal state must be guarded by a lock. |
| 166 | base::Lock lock_; |
erikchen | 3155dee | 2015-07-28 05:28:20 +0900 | [diff] [blame] | 167 | DISALLOW_COPY_AND_ASSIGN(AttachmentBroker); |
erikchen | 62d3813 | 2015-06-17 05:20:51 +0900 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | } // namespace IPC |
| 171 | |
| 172 | #endif // IPC_ATTACHMENT_BROKER_H_ |