Fix some interleaving issues in EmbeddedWorkerInstance.
[chromium-blink-merge.git] / ipc / attachment_broker.cc
blob7450ec37e43edafbb1b43ed807c1df7b89f7598f
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"
7 #include <algorithm>
9 namespace IPC {
11 AttachmentBroker::AttachmentBroker() {}
12 AttachmentBroker::~AttachmentBroker() {}
14 bool AttachmentBroker::GetAttachmentWithId(
15 BrokerableAttachment::AttachmentId id,
16 scoped_refptr<BrokerableAttachment>* out_attachment) {
17 for (AttachmentVector::iterator it = attachments_.begin();
18 it != attachments_.end(); ++it) {
19 if ((*it)->GetIdentifier() == id) {
20 *out_attachment = *it;
21 attachments_.erase(it);
22 return true;
25 return false;
28 void AttachmentBroker::AddObserver(AttachmentBroker::Observer* observer) {
29 auto it = std::find(observers_.begin(), observers_.end(), observer);
30 if (it == observers_.end())
31 observers_.push_back(observer);
34 void AttachmentBroker::RemoveObserver(AttachmentBroker::Observer* observer) {
35 auto it = std::find(observers_.begin(), observers_.end(), observer);
36 if (it != observers_.end())
37 observers_.erase(it);
40 void AttachmentBroker::HandleReceivedAttachment(
41 const scoped_refptr<BrokerableAttachment>& attachment) {
42 attachments_.push_back(attachment);
43 NotifyObservers(attachment->GetIdentifier());
46 void AttachmentBroker::NotifyObservers(
47 const BrokerableAttachment::AttachmentId& id) {
48 // Make a copy of observers_ to avoid mutations during iteration.
49 std::vector<Observer*> observers = observers_;
50 for (Observer* observer : observers) {
51 observer->ReceivedBrokerableAttachmentWithId(id);
55 } // namespace IPC