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 #include "ipc/attachment_broker.h" | 5 #include "ipc/attachment_broker.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 namespace { | 9 namespace { |
10 IPC::AttachmentBroker* g_attachment_broker = nullptr; | 10 IPC::AttachmentBroker* g_attachment_broker = nullptr; |
(...skipping 13 matching lines...) Expand all Loading... |
24 AttachmentBroker* AttachmentBroker::GetGlobal() { | 24 AttachmentBroker* AttachmentBroker::GetGlobal() { |
25 return g_attachment_broker; | 25 return g_attachment_broker; |
26 } | 26 } |
27 | 27 |
28 AttachmentBroker::AttachmentBroker() {} | 28 AttachmentBroker::AttachmentBroker() {} |
29 AttachmentBroker::~AttachmentBroker() {} | 29 AttachmentBroker::~AttachmentBroker() {} |
30 | 30 |
31 bool AttachmentBroker::GetAttachmentWithId( | 31 bool AttachmentBroker::GetAttachmentWithId( |
32 BrokerableAttachment::AttachmentId id, | 32 BrokerableAttachment::AttachmentId id, |
33 scoped_refptr<BrokerableAttachment>* out_attachment) { | 33 scoped_refptr<BrokerableAttachment>* out_attachment) { |
| 34 base::AutoLock auto_lock(*get_lock()); |
34 for (AttachmentVector::iterator it = attachments_.begin(); | 35 for (AttachmentVector::iterator it = attachments_.begin(); |
35 it != attachments_.end(); ++it) { | 36 it != attachments_.end(); ++it) { |
36 if ((*it)->GetIdentifier() == id) { | 37 if ((*it)->GetIdentifier() == id) { |
37 *out_attachment = *it; | 38 *out_attachment = *it; |
38 attachments_.erase(it); | 39 attachments_.erase(it); |
39 return true; | 40 return true; |
40 } | 41 } |
41 } | 42 } |
42 return false; | 43 return false; |
43 } | 44 } |
44 | 45 |
45 void AttachmentBroker::AddObserver(AttachmentBroker::Observer* observer) { | 46 void AttachmentBroker::AddObserver(AttachmentBroker::Observer* observer) { |
| 47 base::AutoLock auto_lock(*get_lock()); |
46 auto it = std::find(observers_.begin(), observers_.end(), observer); | 48 auto it = std::find(observers_.begin(), observers_.end(), observer); |
47 if (it == observers_.end()) | 49 if (it == observers_.end()) |
48 observers_.push_back(observer); | 50 observers_.push_back(observer); |
49 } | 51 } |
50 | 52 |
51 void AttachmentBroker::RemoveObserver(AttachmentBroker::Observer* observer) { | 53 void AttachmentBroker::RemoveObserver(AttachmentBroker::Observer* observer) { |
| 54 base::AutoLock auto_lock(*get_lock()); |
52 auto it = std::find(observers_.begin(), observers_.end(), observer); | 55 auto it = std::find(observers_.begin(), observers_.end(), observer); |
53 if (it != observers_.end()) | 56 if (it != observers_.end()) |
54 observers_.erase(it); | 57 observers_.erase(it); |
55 } | 58 } |
56 | 59 |
57 void AttachmentBroker::RegisterCommunicationChannel(Endpoint* endpoint) { | 60 void AttachmentBroker::RegisterCommunicationChannel(Endpoint* endpoint) { |
58 NOTREACHED(); | 61 NOTREACHED(); |
59 } | 62 } |
60 | 63 |
61 void AttachmentBroker::DeregisterCommunicationChannel(Endpoint* endpoint) { | 64 void AttachmentBroker::DeregisterCommunicationChannel(Endpoint* endpoint) { |
62 NOTREACHED(); | 65 NOTREACHED(); |
63 } | 66 } |
64 | 67 |
65 void AttachmentBroker::HandleReceivedAttachment( | 68 void AttachmentBroker::HandleReceivedAttachment( |
66 const scoped_refptr<BrokerableAttachment>& attachment) { | 69 const scoped_refptr<BrokerableAttachment>& attachment) { |
67 attachments_.push_back(attachment); | 70 { |
| 71 base::AutoLock auto_lock(*get_lock()); |
| 72 attachments_.push_back(attachment); |
| 73 } |
68 NotifyObservers(attachment->GetIdentifier()); | 74 NotifyObservers(attachment->GetIdentifier()); |
69 } | 75 } |
70 | 76 |
71 void AttachmentBroker::NotifyObservers( | 77 void AttachmentBroker::NotifyObservers( |
72 const BrokerableAttachment::AttachmentId& id) { | 78 const BrokerableAttachment::AttachmentId& id) { |
73 // Make a copy of observers_ to avoid mutations during iteration. | 79 // Make a copy of observers_ to avoid mutations during iteration. |
74 std::vector<Observer*> observers = observers_; | 80 std::vector<Observer*> observers; |
| 81 { |
| 82 base::AutoLock auto_lock(*get_lock()); |
| 83 observers = observers_; |
| 84 } |
| 85 |
75 for (Observer* observer : observers) { | 86 for (Observer* observer : observers) { |
76 observer->ReceivedBrokerableAttachmentWithId(id); | 87 observer->ReceivedBrokerableAttachmentWithId(id); |
77 } | 88 } |
78 } | 89 } |
79 | 90 |
80 } // namespace IPC | 91 } // namespace IPC |
OLD | NEW |