blob: 148e0d5b45883f8a26a7ca17372de75b6160f0ea (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ipc/attachment_broker.h"
#include <algorithm>
namespace {
IPC::AttachmentBroker* g_attachment_broker = nullptr;
}
namespace IPC {
// static
void AttachmentBroker::SetGlobal(AttachmentBroker* broker) {
CHECK(!g_attachment_broker || !broker)
<< "Global attachment broker address: " << broker
<< ". New attachment broker address: " << broker;
g_attachment_broker = broker;
}
// static
AttachmentBroker* AttachmentBroker::GetGlobal() {
return g_attachment_broker;
}
AttachmentBroker::AttachmentBroker() {}
AttachmentBroker::~AttachmentBroker() {}
bool AttachmentBroker::GetAttachmentWithId(
BrokerableAttachment::AttachmentId id,
scoped_refptr<BrokerableAttachment>* out_attachment) {
base::AutoLock auto_lock(*get_lock());
for (AttachmentVector::iterator it = attachments_.begin();
it != attachments_.end(); ++it) {
if ((*it)->GetIdentifier() == id) {
*out_attachment = *it;
attachments_.erase(it);
return true;
}
}
return false;
}
void AttachmentBroker::AddObserver(AttachmentBroker::Observer* observer) {
base::AutoLock auto_lock(*get_lock());
auto it = std::find(observers_.begin(), observers_.end(), observer);
if (it == observers_.end())
observers_.push_back(observer);
}
void AttachmentBroker::RemoveObserver(AttachmentBroker::Observer* observer) {
base::AutoLock auto_lock(*get_lock());
auto it = std::find(observers_.begin(), observers_.end(), observer);
if (it != observers_.end())
observers_.erase(it);
}
void AttachmentBroker::RegisterCommunicationChannel(Endpoint* endpoint) {
NOTREACHED();
}
void AttachmentBroker::DeregisterCommunicationChannel(Endpoint* endpoint) {
NOTREACHED();
}
void AttachmentBroker::HandleReceivedAttachment(
const scoped_refptr<BrokerableAttachment>& attachment) {
{
base::AutoLock auto_lock(*get_lock());
attachments_.push_back(attachment);
}
NotifyObservers(attachment->GetIdentifier());
}
void AttachmentBroker::NotifyObservers(
const BrokerableAttachment::AttachmentId& id) {
// Make a copy of observers_ to avoid mutations during iteration.
std::vector<Observer*> observers;
{
base::AutoLock auto_lock(*get_lock());
observers = observers_;
}
for (Observer* observer : observers) {
observer->ReceivedBrokerableAttachmentWithId(id);
}
}
} // namespace IPC
|