blob: 70383edf71368737164e2fbcd0415ed8bcb0f945 [file] [log] [blame]
erikchen62d38132015-06-17 05:20:51 +09001// 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
erikchen3155dee2015-07-28 05:28:20 +09008#include "base/gtest_prod_util.h"
erikchen62d38132015-06-17 05:20:51 +09009#include "base/macros.h"
erikchen32e67f32015-10-30 07:37:04 +090010#include "base/memory/ref_counted.h"
erikchen62d38132015-06-17 05:20:51 +090011#include "base/process/process_handle.h"
erikchen5f9320c2015-10-30 07:54:42 +090012#include "base/synchronization/lock.h"
avi42ebda42015-12-22 11:39:04 +090013#include "build/build_config.h"
erikchen62d38132015-06-17 05:20:51 +090014#include "ipc/brokerable_attachment.h"
15#include "ipc/ipc_export.h"
erikchenda5404b2015-07-15 10:35:39 +090016#include "ipc/ipc_listener.h"
erikchen62d38132015-06-17 05:20:51 +090017
erikchenf295bbc2015-07-28 03:26:14 +090018// 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.
erikchena4229822015-10-10 08:20:49 +090022#if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS))
erikchenf295bbc2015-07-28 03:26:14 +090023#define USE_ATTACHMENT_BROKER 1
24#else
25#define USE_ATTACHMENT_BROKER 0
26#endif // defined(OS_WIN)
27
erikchen0bf12402015-11-07 06:12:36 +090028namespace base {
29class SequencedTaskRunner;
erikchen0209d142016-03-22 08:19:40 +090030class SingleThreadTaskRunner;
erikchen0bf12402015-11-07 06:12:36 +090031};
32
erikchen62d38132015-06-17 05:20:51 +090033namespace IPC {
34
35class AttachmentBroker;
erikchen51921402015-10-22 04:18:25 +090036class Endpoint;
37
erikchen62d38132015-06-17 05:20:51 +090038// 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.
41class 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.
erikchenf295bbc2015-07-28 03:26:14 +090051// 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.
erikchenda5404b2015-07-15 10:35:39 +090053class IPC_EXPORT AttachmentBroker : public Listener {
erikchen62d38132015-06-17 05:20:51 +090054 public:
erikchenf295bbc2015-07-28 03:26:14 +090055 // 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
erikchen2ffe51b2015-09-15 02:45:12 +090063 // 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
erikchenf295bbc2015-07-28 03:26:14 +090069 AttachmentBroker();
70 ~AttachmentBroker() override;
erikchen62d38132015-06-17 05:20:51 +090071
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.
erikchenfdd43fe2015-07-08 07:13:11 +090075 // Returns true on success and false otherwise.
erikchen32e67f32015-10-30 07:37:04 +090076 virtual bool SendAttachmentToProcess(
77 const scoped_refptr<BrokerableAttachment>& attachment,
78 base::ProcessId destination_process) = 0;
erikchen62d38132015-06-17 05:20:51 +090079
80 // Returns whether the attachment was available. If the attachment was
erikchenf295bbc2015-07-28 03:26:14 +090081 // available, populates the output parameter |attachment|.
erikchen3155dee2015-07-28 05:28:20 +090082 bool GetAttachmentWithId(BrokerableAttachment::AttachmentId id,
83 scoped_refptr<BrokerableAttachment>* attachment);
erikchenf295bbc2015-07-28 03:26:14 +090084
85 // Any given observer should only ever add itself once to the observer list.
erikchen0bf12402015-11-07 06:12:36 +090086 // 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);
erikchenf295bbc2015-07-28 03:26:14 +090090 void RemoveObserver(Observer* observer);
91
erikchen51921402015-10-22 04:18:25 +090092 // 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.
erikchen0209d142016-03-22 08:19:40 +090098 //
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);
erikchen51921402015-10-22 04:18:25 +0900103 virtual void DeregisterCommunicationChannel(Endpoint* endpoint);
104
erikchend790b952016-02-17 13:09:14 +0900105 // 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
erikchenf295bbc2015-07-28 03:26:14 +0900113 protected:
erikchen3155dee2015-07-28 05:28:20 +0900114 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.
erikchenf295bbc2015-07-28 03:26:14 +0900121 void NotifyObservers(const BrokerableAttachment::AttachmentId& id);
erikchen62d38132015-06-17 05:20:51 +0900122
erikchen0bf12402015-11-07 06:12:36 +0900123 // 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
erikchen3155dee2015-07-28 05:28:20 +0900128 // This method is exposed for testing only.
129 AttachmentVector* get_attachments() { return &attachments_; }
130
erikchen5f9320c2015-10-30 07:54:42 +0900131 base::Lock* get_lock() { return &lock_; }
132
erikchen62d38132015-06-17 05:20:51 +0900133 private:
erikchen3155dee2015-07-28 05:28:20 +0900134#if defined(OS_WIN)
erikchen7870b092015-07-29 08:25:44 +0900135 FRIEND_TEST_ALL_PREFIXES(AttachmentBrokerUnprivilegedWinTest,
136 ReceiveValidMessage);
137 FRIEND_TEST_ALL_PREFIXES(AttachmentBrokerUnprivilegedWinTest,
138 ReceiveInvalidMessage);
erikchen3155dee2015-07-28 05:28:20 +0900139#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_;
erikchenf295bbc2015-07-28 03:26:14 +0900147
erikchen0bf12402015-11-07 06:12:36 +0900148 struct ObserverInfo {
149 ObserverInfo();
vmpstr65104a82016-02-25 06:26:11 +0900150 ObserverInfo(const ObserverInfo& other);
erikchen0bf12402015-11-07 06:12:36 +0900151 ~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_;
erikchen5f9320c2015-10-30 07:54:42 +0900163
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_;
erikchen3155dee2015-07-28 05:28:20 +0900167 DISALLOW_COPY_AND_ASSIGN(AttachmentBroker);
erikchen62d38132015-06-17 05:20:51 +0900168};
169
170} // namespace IPC
171
172#endif // IPC_ATTACHMENT_BROKER_H_