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 #include "ipc/attachment_broker.h"
10 IPC::AttachmentBroker
* g_attachment_broker
= nullptr;
16 void AttachmentBroker::SetGlobal(AttachmentBroker
* broker
) {
17 CHECK(!g_attachment_broker
);
18 g_attachment_broker
= broker
;
22 AttachmentBroker
* AttachmentBroker::GetGlobal() {
23 return g_attachment_broker
;
26 AttachmentBroker::AttachmentBroker() {}
27 AttachmentBroker::~AttachmentBroker() {}
29 bool AttachmentBroker::GetAttachmentWithId(
30 BrokerableAttachment::AttachmentId id
,
31 scoped_refptr
<BrokerableAttachment
>* out_attachment
) {
32 for (AttachmentVector::iterator it
= attachments_
.begin();
33 it
!= attachments_
.end(); ++it
) {
34 if ((*it
)->GetIdentifier() == id
) {
35 *out_attachment
= *it
;
36 attachments_
.erase(it
);
43 void AttachmentBroker::AddObserver(AttachmentBroker::Observer
* observer
) {
44 auto it
= std::find(observers_
.begin(), observers_
.end(), observer
);
45 if (it
== observers_
.end())
46 observers_
.push_back(observer
);
49 void AttachmentBroker::RemoveObserver(AttachmentBroker::Observer
* observer
) {
50 auto it
= std::find(observers_
.begin(), observers_
.end(), observer
);
51 if (it
!= observers_
.end())
55 void AttachmentBroker::HandleReceivedAttachment(
56 const scoped_refptr
<BrokerableAttachment
>& attachment
) {
57 attachments_
.push_back(attachment
);
58 NotifyObservers(attachment
->GetIdentifier());
61 void AttachmentBroker::NotifyObservers(
62 const BrokerableAttachment::AttachmentId
& id
) {
63 // Make a copy of observers_ to avoid mutations during iteration.
64 std::vector
<Observer
*> observers
= observers_
;
65 for (Observer
* observer
: observers
) {
66 observer
->ReceivedBrokerableAttachmentWithId(id
);