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 3 // found in the LICENSE file. 4 5 #include "remoting/signaling/log_to_server.h" 6 7 #include "remoting/base/constants.h" 8 #include "remoting/signaling/iq_sender.h" 9 #include "remoting/signaling/signal_strategy.h" 10 #include "third_party/libjingle/source/talk/xmpp/constants.h" 11 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" 12 13 using buzz::QName; 14 using buzz::XmlElement; 15 16 namespace remoting { 17 18 LogToServer::LogToServer(ServerLogEntry::Mode mode, 19 SignalStrategy* signal_strategy, 20 const std::string& directory_bot_jid) 21 : mode_(mode), 22 signal_strategy_(signal_strategy), 23 directory_bot_jid_(directory_bot_jid) { 24 signal_strategy_->AddListener(this); 25 } 26 27 LogToServer::~LogToServer() { 28 signal_strategy_->RemoveListener(this); 29 } 30 31 void LogToServer::OnSignalStrategyStateChange(SignalStrategy::State state) { 32 DCHECK(CalledOnValidThread()); 33 34 if (state == SignalStrategy::CONNECTED) { 35 iq_sender_.reset(new IqSender(signal_strategy_)); 36 SendPendingEntries(); 37 } else if (state == SignalStrategy::DISCONNECTED) { 38 iq_sender_.reset(); 39 } 40 } 41 42 bool LogToServer::OnSignalStrategyIncomingStanza( 43 const buzz::XmlElement* stanza) { 44 return false; 45 } 46 47 void LogToServer::Log(const ServerLogEntry& entry) { 48 pending_entries_.push_back(entry); 49 SendPendingEntries(); 50 } 51 52 void LogToServer::SendPendingEntries() { 53 if (iq_sender_ == NULL) { 54 return; 55 } 56 if (pending_entries_.empty()) { 57 return; 58 } 59 // Make one stanza containing all the pending entries. 60 scoped_ptr<XmlElement> stanza(ServerLogEntry::MakeStanza()); 61 while (!pending_entries_.empty()) { 62 ServerLogEntry& entry = pending_entries_.front(); 63 stanza->AddElement(entry.ToStanza().release()); 64 pending_entries_.pop_front(); 65 } 66 // Send the stanza to the server. 67 scoped_ptr<IqRequest> req = iq_sender_->SendIq( 68 buzz::STR_SET, directory_bot_jid_, stanza.Pass(), 69 IqSender::ReplyCallback()); 70 // We ignore any response, so let the IqRequest be destroyed. 71 return; 72 } 73 74 } // namespace remoting 75