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/macros.h" | 8 #include "base/macros.h" |
9 #include "base/process/process_handle.h" | 9 #include "base/process/process_handle.h" |
10 #include "ipc/brokerable_attachment.h" | 10 #include "ipc/brokerable_attachment.h" |
11 #include "ipc/ipc_export.h" | 11 #include "ipc/ipc_export.h" |
12 #include "ipc/ipc_listener.h" | 12 #include "ipc/ipc_listener.h" |
13 | 13 |
| 14 // If the platform has no attachments that need brokering, then it shouldn't |
| 15 // compile any code that calls member functions of AttachmentBroker. This |
| 16 // prevents symbols only used by AttachmentBroker and its subclasses from |
| 17 // making it into the binary. |
| 18 #if defined(OS_WIN) |
| 19 #define USE_ATTACHMENT_BROKER 1 |
| 20 #else |
| 21 #define USE_ATTACHMENT_BROKER 0 |
| 22 #endif // defined(OS_WIN) |
| 23 |
14 namespace IPC { | 24 namespace IPC { |
15 | 25 |
16 class AttachmentBroker; | 26 class AttachmentBroker; |
17 // Classes that inherit from this abstract base class are capable of | 27 // Classes that inherit from this abstract base class are capable of |
18 // communicating with a broker to send and receive attachments to Chrome IPC | 28 // communicating with a broker to send and receive attachments to Chrome IPC |
19 // messages. | 29 // messages. |
20 class IPC_EXPORT SupportsAttachmentBrokering { | 30 class IPC_EXPORT SupportsAttachmentBrokering { |
21 public: | 31 public: |
22 // Returns an AttachmentBroker used to broker attachments of IPC messages to | 32 // Returns an AttachmentBroker used to broker attachments of IPC messages to |
23 // other processes. There must be exactly one AttachmentBroker per process. | 33 // other processes. There must be exactly one AttachmentBroker per process. |
24 virtual AttachmentBroker* GetAttachmentBroker() = 0; | 34 virtual AttachmentBroker* GetAttachmentBroker() = 0; |
25 }; | 35 }; |
26 | 36 |
27 // Responsible for brokering attachments to Chrome IPC messages. On platforms | 37 // Responsible for brokering attachments to Chrome IPC messages. On platforms |
28 // that support attachment brokering, every IPC channel should have a reference | 38 // that support attachment brokering, every IPC channel should have a reference |
29 // to a AttachmentBroker. | 39 // to a AttachmentBroker. |
| 40 // This class is not thread safe. The implementation of this class assumes that |
| 41 // it is only ever used on the same thread as its consumers. |
30 class IPC_EXPORT AttachmentBroker : public Listener { | 42 class IPC_EXPORT AttachmentBroker : public Listener { |
31 public: | 43 public: |
32 AttachmentBroker() {} | 44 // A standard observer interface that allows consumers of the AttachmentBroker |
33 ~AttachmentBroker() override {} | 45 // to be notified when a new attachment has been received. |
| 46 class Observer { |
| 47 public: |
| 48 virtual void ReceivedBrokerableAttachmentWithId( |
| 49 const BrokerableAttachment::AttachmentId& id) = 0; |
| 50 }; |
| 51 |
| 52 AttachmentBroker(); |
| 53 ~AttachmentBroker() override; |
34 | 54 |
35 // Sends |attachment| to |destination_process|. The implementation uses an | 55 // Sends |attachment| to |destination_process|. The implementation uses an |
36 // IPC::Channel to communicate with the broker process. This may be the same | 56 // IPC::Channel to communicate with the broker process. This may be the same |
37 // IPC::Channel that is requesting the brokering of an attachment. | 57 // IPC::Channel that is requesting the brokering of an attachment. |
38 // Returns true on success and false otherwise. | 58 // Returns true on success and false otherwise. |
39 virtual bool SendAttachmentToProcess(const BrokerableAttachment* attachment, | 59 virtual bool SendAttachmentToProcess(const BrokerableAttachment* attachment, |
40 base::ProcessId destination_process) = 0; | 60 base::ProcessId destination_process) = 0; |
41 | 61 |
42 // Returns whether the attachment was available. If the attachment was | 62 // Returns whether the attachment was available. If the attachment was |
43 // available, populates the output parameter |attachment|. The caller then | 63 // available, populates the output parameter |attachment|. |
44 // becomes the owner of |attachment|. | 64 virtual bool GetAttachmentWithId( |
45 virtual bool GetAttachmentWithId(BrokerableAttachment::AttachmentId id, | 65 BrokerableAttachment::AttachmentId id, |
46 BrokerableAttachment* attachment) = 0; | 66 scoped_refptr<BrokerableAttachment>* attachment) = 0; |
| 67 |
| 68 // Any given observer should only ever add itself once to the observer list. |
| 69 void AddObserver(Observer* observer); |
| 70 void RemoveObserver(Observer* observer); |
| 71 |
| 72 protected: |
| 73 void NotifyObservers(const BrokerableAttachment::AttachmentId& id); |
47 | 74 |
48 private: | 75 private: |
49 DISALLOW_COPY_AND_ASSIGN(AttachmentBroker); | 76 DISALLOW_COPY_AND_ASSIGN(AttachmentBroker); |
| 77 |
| 78 std::vector<Observer*> observers_; |
50 }; | 79 }; |
51 | 80 |
52 } // namespace IPC | 81 } // namespace IPC |
53 | 82 |
54 #endif // IPC_ATTACHMENT_BROKER_H_ | 83 #endif // IPC_ATTACHMENT_BROKER_H_ |
OLD | NEW |