| 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/iq_sender.h" | 5 #include "remoting/signaling/iq_sender.h" |
| 6 | 6 |
| 7 #include <memory> |
| 7 #include <utility> | 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
| 11 #include "base/location.h" | 12 #include "base/location.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "remoting/signaling/jid_util.h" | 18 #include "remoting/signaling/jid_util.h" |
| 19 #include "remoting/signaling/signal_strategy.h" | 19 #include "remoting/signaling/signal_strategy.h" |
| 20 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" | 20 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
| 21 #include "third_party/webrtc/libjingle/xmpp/constants.h" | 21 #include "third_party/webrtc/libjingle/xmpp/constants.h" |
| 22 | 22 |
| 23 namespace remoting { | 23 namespace remoting { |
| 24 | 24 |
| 25 // static | 25 // static |
| 26 scoped_ptr<buzz::XmlElement> IqSender::MakeIqStanza( | 26 std::unique_ptr<buzz::XmlElement> IqSender::MakeIqStanza( |
| 27 const std::string& type, | 27 const std::string& type, |
| 28 const std::string& addressee, | 28 const std::string& addressee, |
| 29 scoped_ptr<buzz::XmlElement> iq_body) { | 29 std::unique_ptr<buzz::XmlElement> iq_body) { |
| 30 scoped_ptr<buzz::XmlElement> stanza(new buzz::XmlElement(buzz::QN_IQ)); | 30 std::unique_ptr<buzz::XmlElement> stanza(new buzz::XmlElement(buzz::QN_IQ)); |
| 31 stanza->AddAttr(buzz::QN_TYPE, type); | 31 stanza->AddAttr(buzz::QN_TYPE, type); |
| 32 if (!addressee.empty()) | 32 if (!addressee.empty()) |
| 33 stanza->AddAttr(buzz::QN_TO, addressee); | 33 stanza->AddAttr(buzz::QN_TO, addressee); |
| 34 stanza->AddElement(iq_body.release()); | 34 stanza->AddElement(iq_body.release()); |
| 35 return stanza; | 35 return stanza; |
| 36 } | 36 } |
| 37 | 37 |
| 38 IqSender::IqSender(SignalStrategy* signal_strategy) | 38 IqSender::IqSender(SignalStrategy* signal_strategy) |
| 39 : signal_strategy_(signal_strategy) { | 39 : signal_strategy_(signal_strategy) { |
| 40 signal_strategy_->AddListener(this); | 40 signal_strategy_->AddListener(this); |
| 41 } | 41 } |
| 42 | 42 |
| 43 IqSender::~IqSender() { | 43 IqSender::~IqSender() { |
| 44 signal_strategy_->RemoveListener(this); | 44 signal_strategy_->RemoveListener(this); |
| 45 } | 45 } |
| 46 | 46 |
| 47 scoped_ptr<IqRequest> IqSender::SendIq(scoped_ptr<buzz::XmlElement> stanza, | 47 std::unique_ptr<IqRequest> IqSender::SendIq( |
| 48 const ReplyCallback& callback) { | 48 std::unique_ptr<buzz::XmlElement> stanza, |
| 49 const ReplyCallback& callback) { |
| 49 std::string addressee = stanza->Attr(buzz::QN_TO); | 50 std::string addressee = stanza->Attr(buzz::QN_TO); |
| 50 std::string id = signal_strategy_->GetNextId(); | 51 std::string id = signal_strategy_->GetNextId(); |
| 51 stanza->AddAttr(buzz::QN_ID, id); | 52 stanza->AddAttr(buzz::QN_ID, id); |
| 52 if (!signal_strategy_->SendStanza(std::move(stanza))) { | 53 if (!signal_strategy_->SendStanza(std::move(stanza))) { |
| 53 return nullptr; | 54 return nullptr; |
| 54 } | 55 } |
| 55 DCHECK(requests_.find(id) == requests_.end()); | 56 DCHECK(requests_.find(id) == requests_.end()); |
| 56 scoped_ptr<IqRequest> request(new IqRequest(this, callback, addressee)); | 57 std::unique_ptr<IqRequest> request(new IqRequest(this, callback, addressee)); |
| 57 if (!callback.is_null()) | 58 if (!callback.is_null()) |
| 58 requests_[id] = request.get(); | 59 requests_[id] = request.get(); |
| 59 return request; | 60 return request; |
| 60 } | 61 } |
| 61 | 62 |
| 62 scoped_ptr<IqRequest> IqSender::SendIq(const std::string& type, | 63 std::unique_ptr<IqRequest> IqSender::SendIq( |
| 63 const std::string& addressee, | 64 const std::string& type, |
| 64 scoped_ptr<buzz::XmlElement> iq_body, | 65 const std::string& addressee, |
| 65 const ReplyCallback& callback) { | 66 std::unique_ptr<buzz::XmlElement> iq_body, |
| 67 const ReplyCallback& callback) { |
| 66 return SendIq(MakeIqStanza(type, addressee, std::move(iq_body)), callback); | 68 return SendIq(MakeIqStanza(type, addressee, std::move(iq_body)), callback); |
| 67 } | 69 } |
| 68 | 70 |
| 69 void IqSender::RemoveRequest(IqRequest* request) { | 71 void IqSender::RemoveRequest(IqRequest* request) { |
| 70 IqRequestMap::iterator it = requests_.begin(); | 72 IqRequestMap::iterator it = requests_.begin(); |
| 71 while (it != requests_.end()) { | 73 while (it != requests_.end()) { |
| 72 IqRequestMap::iterator cur = it; | 74 IqRequestMap::iterator cur = it; |
| 73 ++it; | 75 ++it; |
| 74 if (cur->second == request) { | 76 if (cur->second == request) { |
| 75 requests_.erase(cur); | 77 requests_.erase(cur); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 base::ResetAndReturn(&callback_).Run(this, stanza); | 148 base::ResetAndReturn(&callback_).Run(this, stanza); |
| 147 } | 149 } |
| 148 | 150 |
| 149 void IqRequest::OnTimeout() { | 151 void IqRequest::OnTimeout() { |
| 150 CallCallback(nullptr); | 152 CallCallback(nullptr); |
| 151 } | 153 } |
| 152 | 154 |
| 153 void IqRequest::OnResponse(const buzz::XmlElement* stanza) { | 155 void IqRequest::OnResponse(const buzz::XmlElement* stanza) { |
| 154 // It's unsafe to delete signal strategy here, and the callback may | 156 // It's unsafe to delete signal strategy here, and the callback may |
| 155 // want to do that, so we post task to invoke the callback later. | 157 // want to do that, so we post task to invoke the callback later. |
| 156 scoped_ptr<buzz::XmlElement> stanza_copy(new buzz::XmlElement(*stanza)); | 158 std::unique_ptr<buzz::XmlElement> stanza_copy(new buzz::XmlElement(*stanza)); |
| 157 base::ThreadTaskRunnerHandle::Get()->PostTask( | 159 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 158 FROM_HERE, base::Bind(&IqRequest::DeliverResponse, AsWeakPtr(), | 160 FROM_HERE, base::Bind(&IqRequest::DeliverResponse, AsWeakPtr(), |
| 159 base::Passed(&stanza_copy))); | 161 base::Passed(&stanza_copy))); |
| 160 } | 162 } |
| 161 | 163 |
| 162 void IqRequest::DeliverResponse(scoped_ptr<buzz::XmlElement> stanza) { | 164 void IqRequest::DeliverResponse(std::unique_ptr<buzz::XmlElement> stanza) { |
| 163 CallCallback(stanza.get()); | 165 CallCallback(stanza.get()); |
| 164 } | 166 } |
| 165 | 167 |
| 166 } // namespace remoting | 168 } // namespace remoting |
| OLD | NEW |