| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/delegating_signal_strategy.h" | 5 #include "remoting/signaling/delegating_signal_strategy.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "third_party/libjingle_xmpp/xmllite/xmlelement.h" | 11 #include "third_party/libjingle_xmpp/xmllite/xmlelement.h" |
| 12 #include "third_party/libjingle_xmpp/xmpp/constants.h" | 12 #include "third_party/libjingle_xmpp/xmpp/constants.h" |
| 13 | 13 |
| 14 namespace remoting { | 14 namespace remoting { |
| 15 | 15 |
| 16 DelegatingSignalStrategy::DelegatingSignalStrategy( | 16 DelegatingSignalStrategy::DelegatingSignalStrategy( |
| 17 std::string local_jid, | 17 std::string local_jid, |
| 18 scoped_refptr<base::SingleThreadTaskRunner> client_task_runner, | 18 scoped_refptr<base::SingleThreadTaskRunner> client_task_runner, |
| 19 const SendIqCallback& send_iq_callback) | 19 const IqCallback& send_iq_callback) |
| 20 : local_jid_(local_jid), | 20 : local_jid_(local_jid), |
| 21 delegate_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 21 delegate_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 22 client_task_runner_(client_task_runner), | 22 client_task_runner_(client_task_runner), |
| 23 send_iq_callback_(send_iq_callback), | 23 send_iq_callback_(send_iq_callback), |
| 24 weak_factory_(this) {} | 24 weak_factory_(this) { |
| 25 incoming_iq_callback_ = |
| 26 base::BindRepeating(&OnIncomingMessageFromDelegate, |
| 27 weak_factory_.GetWeakPtr(), client_task_runner_); |
| 28 } |
| 25 | 29 |
| 26 DelegatingSignalStrategy::~DelegatingSignalStrategy() {} | 30 DelegatingSignalStrategy::~DelegatingSignalStrategy() {} |
| 27 | 31 |
| 28 void DelegatingSignalStrategy::OnIncomingMessage(const std::string& message) { | 32 DelegatingSignalStrategy::IqCallback |
| 29 if (!client_task_runner_->BelongsToCurrentThread()) { | 33 DelegatingSignalStrategy::GetIncomingMessageCallback() { |
| 30 client_task_runner_->PostTask( | 34 return incoming_iq_callback_; |
| 31 FROM_HERE, base::Bind(&DelegatingSignalStrategy::OnIncomingMessage, | 35 } |
| 32 weak_factory_.GetWeakPtr(), message)); | 36 |
| 37 // static |
| 38 void DelegatingSignalStrategy::OnIncomingMessageFromDelegate( |
| 39 base::WeakPtr<DelegatingSignalStrategy> weak_ptr, |
| 40 scoped_refptr<base::SingleThreadTaskRunner> client_task_runner, |
| 41 const std::string& message) { |
| 42 if (client_task_runner->BelongsToCurrentThread()) { |
| 43 weak_ptr->OnIncomingMessage(message); |
| 33 return; | 44 return; |
| 34 } | 45 } |
| 35 | 46 |
| 47 client_task_runner->PostTask( |
| 48 FROM_HERE, base::Bind(&DelegatingSignalStrategy::OnIncomingMessage, |
| 49 weak_ptr, message)); |
| 50 } |
| 51 |
| 52 void DelegatingSignalStrategy::OnIncomingMessage(const std::string& message) { |
| 53 DCHECK(client_task_runner_->BelongsToCurrentThread()); |
| 36 std::unique_ptr<buzz::XmlElement> stanza(buzz::XmlElement::ForStr(message)); | 54 std::unique_ptr<buzz::XmlElement> stanza(buzz::XmlElement::ForStr(message)); |
| 37 if (!stanza.get()) { | 55 if (!stanza.get()) { |
| 38 LOG(WARNING) << "Malformed XMPP stanza received: " << message; | 56 LOG(WARNING) << "Malformed XMPP stanza received: " << message; |
| 39 return; | 57 return; |
| 40 } | 58 } |
| 41 | 59 |
| 42 for (auto& listener : listeners_) { | 60 for (auto& listener : listeners_) { |
| 43 if (listener.OnSignalStrategyIncomingStanza(stanza.get())) | 61 if (listener.OnSignalStrategyIncomingStanza(stanza.get())) |
| 44 break; | 62 break; |
| 45 } | 63 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 delegate_task_runner_->PostTask(FROM_HERE, | 105 delegate_task_runner_->PostTask(FROM_HERE, |
| 88 base::Bind(send_iq_callback_, stanza->Str())); | 106 base::Bind(send_iq_callback_, stanza->Str())); |
| 89 return true; | 107 return true; |
| 90 } | 108 } |
| 91 | 109 |
| 92 std::string DelegatingSignalStrategy::GetNextId() { | 110 std::string DelegatingSignalStrategy::GetNextId() { |
| 93 return base::Uint64ToString(base::RandUint64()); | 111 return base::Uint64ToString(base::RandUint64()); |
| 94 } | 112 } |
| 95 | 113 |
| 96 } // namespace remoting | 114 } // namespace remoting |
| OLD | NEW |