OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "remoting/signaling/fake_signal_strategy.h" | 5 #include "remoting/signaling/fake_signal_strategy.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
14 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 14 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
15 #include "third_party/libjingle/source/talk/xmpp/constants.h" | 15 #include "third_party/libjingle/source/talk/xmpp/constants.h" |
16 | 16 |
17 namespace remoting { | 17 namespace remoting { |
18 | 18 |
19 // static | 19 // static |
20 void FakeSignalStrategy::Connect(FakeSignalStrategy* peer1, | 20 void FakeSignalStrategy::Connect(FakeSignalStrategy* peer1, |
21 FakeSignalStrategy* peer2) { | 21 FakeSignalStrategy* peer2) { |
22 peer1->peer_ = peer2; | 22 DCHECK(peer1->main_thread_->BelongsToCurrentThread()); |
23 peer2->peer_ = peer1; | 23 DCHECK(peer2->main_thread_->BelongsToCurrentThread()); |
24 peer1->ConnectTo(peer2); | |
25 peer2->ConnectTo(peer1); | |
24 } | 26 } |
25 | 27 |
26 FakeSignalStrategy::FakeSignalStrategy(const std::string& jid) | 28 FakeSignalStrategy::FakeSignalStrategy(const std::string& jid) |
27 : jid_(jid), | 29 : main_thread_(base::ThreadTaskRunnerHandle::Get()), |
28 peer_(NULL), | 30 jid_(jid), |
29 last_id_(0), | 31 last_id_(0), |
30 weak_factory_(this) { | 32 weak_factory_(this) { |
31 | 33 |
32 } | 34 } |
33 | 35 |
34 FakeSignalStrategy::~FakeSignalStrategy() { | 36 FakeSignalStrategy::~FakeSignalStrategy() { |
35 while (!received_messages_.empty()) { | 37 while (!received_messages_.empty()) { |
36 delete received_messages_.front(); | 38 delete received_messages_.front(); |
37 received_messages_.pop_front(); | 39 received_messages_.pop_front(); |
38 } | 40 } |
39 } | 41 } |
40 | 42 |
43 void FakeSignalStrategy::ConnectTo(FakeSignalStrategy* peer) { | |
44 PeerCallback peer_callback = | |
45 base::Bind(&FakeSignalStrategy::DeliverMessageOnThread, | |
46 main_thread_, | |
47 weak_factory_.GetWeakPtr()); | |
48 if (peer->main_thread_->BelongsToCurrentThread()) { | |
49 peer->SetPeerCallback(peer_callback); | |
50 } else { | |
51 peer->main_thread_->PostTask( | |
52 FROM_HERE, | |
53 base::Bind(&FakeSignalStrategy::SetPeerCallback, | |
54 base::Unretained(peer), | |
55 peer_callback)); | |
56 } | |
57 } | |
58 | |
41 void FakeSignalStrategy::Connect() { | 59 void FakeSignalStrategy::Connect() { |
42 DCHECK(CalledOnValidThread()); | 60 DCHECK(CalledOnValidThread()); |
43 FOR_EACH_OBSERVER(Listener, listeners_, | 61 FOR_EACH_OBSERVER(Listener, listeners_, |
44 OnSignalStrategyStateChange(CONNECTED)); | 62 OnSignalStrategyStateChange(CONNECTED)); |
45 } | 63 } |
46 | 64 |
47 void FakeSignalStrategy::Disconnect() { | 65 void FakeSignalStrategy::Disconnect() { |
48 DCHECK(CalledOnValidThread()); | 66 DCHECK(CalledOnValidThread()); |
49 FOR_EACH_OBSERVER(Listener, listeners_, | 67 FOR_EACH_OBSERVER(Listener, listeners_, |
50 OnSignalStrategyStateChange(DISCONNECTED)); | 68 OnSignalStrategyStateChange(DISCONNECTED)); |
(...skipping 20 matching lines...) Expand all Loading... | |
71 void FakeSignalStrategy::RemoveListener(Listener* listener) { | 89 void FakeSignalStrategy::RemoveListener(Listener* listener) { |
72 DCHECK(CalledOnValidThread()); | 90 DCHECK(CalledOnValidThread()); |
73 listeners_.RemoveObserver(listener); | 91 listeners_.RemoveObserver(listener); |
74 } | 92 } |
75 | 93 |
76 bool FakeSignalStrategy::SendStanza(scoped_ptr<buzz::XmlElement> stanza) { | 94 bool FakeSignalStrategy::SendStanza(scoped_ptr<buzz::XmlElement> stanza) { |
77 DCHECK(CalledOnValidThread()); | 95 DCHECK(CalledOnValidThread()); |
78 | 96 |
79 stanza->SetAttr(buzz::QN_FROM, jid_); | 97 stanza->SetAttr(buzz::QN_FROM, jid_); |
80 | 98 |
81 if (peer_) { | 99 if (!peer_callback_.is_null()) { |
82 peer_->OnIncomingMessage(stanza.Pass()); | 100 peer_callback_.Run(stanza.Pass()); |
83 return true; | 101 return true; |
84 } else { | 102 } else { |
85 return false; | 103 return false; |
86 } | 104 } |
87 } | 105 } |
88 | 106 |
89 std::string FakeSignalStrategy::GetNextId() { | 107 std::string FakeSignalStrategy::GetNextId() { |
90 ++last_id_; | 108 ++last_id_; |
91 return base::IntToString(last_id_); | 109 return base::IntToString(last_id_); |
92 } | 110 } |
93 | 111 |
112 // static | |
113 void FakeSignalStrategy::DeliverMessageOnThread( | |
114 scoped_refptr<base::SingleThreadTaskRunner> thread, | |
115 base::WeakPtr<FakeSignalStrategy> target, | |
116 scoped_ptr<buzz::XmlElement> stanza) { | |
117 thread->PostTask(FROM_HERE, | |
118 base::Bind(&FakeSignalStrategy::OnIncomingMessage, | |
119 target, base::Passed(&stanza))); | |
120 } | |
121 | |
94 void FakeSignalStrategy::OnIncomingMessage( | 122 void FakeSignalStrategy::OnIncomingMessage( |
95 scoped_ptr<buzz::XmlElement> stanza) { | 123 scoped_ptr<buzz::XmlElement> stanza) { |
96 pending_messages_.push(stanza.get()); | 124 buzz::XmlElement* stanza_ptr = stanza.get(); |
rmsousa
2014/07/23 00:49:37
nit: DCHECK current thread?
Sergey Ulanov
2014/07/25 18:23:15
Done.
| |
97 received_messages_.push_back(stanza.release()); | 125 received_messages_.push_back(stanza.release()); |
98 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
99 FROM_HERE, base::Bind(&FakeSignalStrategy::DeliverIncomingMessages, | |
100 weak_factory_.GetWeakPtr())); | |
101 } | |
102 | 126 |
103 void FakeSignalStrategy::DeliverIncomingMessages() { | 127 const std::string& to_field = stanza_ptr->Attr(buzz::QN_TO); |
104 while (!pending_messages_.empty()) { | 128 if (to_field != jid_) { |
105 buzz::XmlElement* stanza = pending_messages_.front(); | 129 LOG(WARNING) << "Dropping stanza that is addressed to " << to_field |
106 const std::string& to_field = stanza->Attr(buzz::QN_TO); | 130 << ". Local jid: " << jid_ |
107 if (to_field != jid_) { | 131 << ". Message content: " << stanza_ptr->Str(); |
108 LOG(WARNING) << "Dropping stanza that is addressed to " << to_field | 132 return; |
109 << ". Local jid: " << jid_ | 133 } |
110 << ". Message content: " << stanza->Str(); | |
111 return; | |
112 } | |
113 | 134 |
114 ObserverListBase<Listener>::Iterator it(listeners_); | 135 ObserverListBase<Listener>::Iterator it(listeners_); |
115 Listener* listener; | 136 Listener* listener; |
116 while ((listener = it.GetNext()) != NULL) { | 137 while ((listener = it.GetNext()) != NULL) { |
117 if (listener->OnSignalStrategyIncomingStanza(stanza)) | 138 if (listener->OnSignalStrategyIncomingStanza(stanza_ptr)) |
118 break; | 139 break; |
119 } | |
120 | |
121 pending_messages_.pop(); | |
122 } | 140 } |
123 } | 141 } |
124 | 142 |
143 void FakeSignalStrategy::SetPeerCallback(const PeerCallback& peer_callback) { | |
144 peer_callback_ = peer_callback; | |
145 } | |
146 | |
125 } // namespace remoting | 147 } // namespace remoting |
OLD | NEW |