xmpp_log_to_server.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include "remoting/signaling/xmpp_log_to_server.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "remoting/base/constants.h"
  9. #include "remoting/signaling/iq_sender.h"
  10. #include "remoting/signaling/signal_strategy.h"
  11. #include "remoting/signaling/xmpp_constants.h"
  12. #include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
  13. using jingle_xmpp::QName;
  14. using jingle_xmpp::XmlElement;
  15. namespace remoting {
  16. XmppLogToServer::XmppLogToServer(
  17. ServerLogEntry::Mode mode,
  18. SignalStrategy* signal_strategy,
  19. const std::string& directory_bot_jid,
  20. scoped_refptr<base::SequencedTaskRunner> caller_task_runner)
  21. : mode_(mode),
  22. signal_strategy_(signal_strategy),
  23. directory_bot_jid_(directory_bot_jid) {
  24. DETACH_FROM_SEQUENCE(sequence_checker_);
  25. if (!caller_task_runner || caller_task_runner->RunsTasksInCurrentSequence()) {
  26. Init();
  27. return;
  28. }
  29. caller_task_runner->PostTask(
  30. FROM_HERE,
  31. base::BindOnce(&XmppLogToServer::Init, weak_factory_.GetWeakPtr()));
  32. }
  33. XmppLogToServer::~XmppLogToServer() {
  34. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  35. signal_strategy_->RemoveListener(this);
  36. }
  37. void XmppLogToServer::OnSignalStrategyStateChange(SignalStrategy::State state) {
  38. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  39. if (state == SignalStrategy::CONNECTED) {
  40. iq_sender_ = std::make_unique<IqSender>(signal_strategy_);
  41. SendPendingEntries();
  42. } else if (state == SignalStrategy::DISCONNECTED) {
  43. iq_sender_.reset();
  44. }
  45. }
  46. bool XmppLogToServer::OnSignalStrategyIncomingStanza(
  47. const jingle_xmpp::XmlElement* stanza) {
  48. return false;
  49. }
  50. void XmppLogToServer::Log(const ServerLogEntry& entry) {
  51. pending_entries_.push_back(entry);
  52. SendPendingEntries();
  53. }
  54. void XmppLogToServer::Init() {
  55. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  56. signal_strategy_->AddListener(this);
  57. }
  58. void XmppLogToServer::SendPendingEntries() {
  59. if (iq_sender_ == nullptr) {
  60. return;
  61. }
  62. if (pending_entries_.empty()) {
  63. return;
  64. }
  65. // Make one stanza containing all the pending entries.
  66. std::unique_ptr<XmlElement> stanza(ServerLogEntry::MakeStanza());
  67. while (!pending_entries_.empty()) {
  68. ServerLogEntry& entry = pending_entries_.front();
  69. stanza->AddElement(entry.ToStanza().release());
  70. pending_entries_.pop_front();
  71. }
  72. // Send the stanza to the server and ignore the response.
  73. iq_sender_->SendIq(kIqTypeSet, directory_bot_jid_, std::move(stanza),
  74. IqSender::ReplyCallback());
  75. }
  76. ServerLogEntry::Mode XmppLogToServer::mode() const {
  77. return mode_;
  78. }
  79. } // namespace remoting