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 // TODO(erikchen): There should be a CHECK here to make sure that
18 // |g_attachment_broker| is nullptr. Right now, this causes problems with
19 // --single_process and similar testing conditions. I'm temporarily removing
20 // this CHECK. http://crbug.com/534539.
21 g_attachment_broker
= broker
;
25 AttachmentBroker
* AttachmentBroker::GetGlobal() {
26 return g_attachment_broker
;
29 AttachmentBroker::AttachmentBroker() {}
30 AttachmentBroker::~AttachmentBroker() {}
32 bool AttachmentBroker::GetAttachmentWithId(
33 BrokerableAttachment::AttachmentId id
,
34 scoped_refptr
<BrokerableAttachment
>* out_attachment
) {
35 for (AttachmentVector::iterator it
= attachments_
.begin();
36 it
!= attachments_
.end(); ++it
) {
37 if ((*it
)->GetIdentifier() == id
) {
38 *out_attachment
= *it
;
39 attachments_
.erase(it
);
46 void AttachmentBroker::AddObserver(AttachmentBroker::Observer
* observer
) {
47 auto it
= std::find(observers_
.begin(), observers_
.end(), observer
);
48 if (it
== observers_
.end())
49 observers_
.push_back(observer
);
52 void AttachmentBroker::RemoveObserver(AttachmentBroker::Observer
* observer
) {
53 auto it
= std::find(observers_
.begin(), observers_
.end(), observer
);
54 if (it
!= observers_
.end())
58 void AttachmentBroker::HandleReceivedAttachment(
59 const scoped_refptr
<BrokerableAttachment
>& attachment
) {
60 attachments_
.push_back(attachment
);
61 NotifyObservers(attachment
->GetIdentifier());
64 void AttachmentBroker::NotifyObservers(
65 const BrokerableAttachment::AttachmentId
& id
) {
66 // Make a copy of observers_ to avoid mutations during iteration.
67 std::vector
<Observer
*> observers
= observers_
;
68 for (Observer
* observer
: observers
) {
69 observer
->ReceivedBrokerableAttachmentWithId(id
);