fake_session.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (c) 2012 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/protocol/fake_session.h"
  5. #include <memory>
  6. #include "base/bind.h"
  7. #include "base/check.h"
  8. #include "base/location.h"
  9. #include "base/threading/thread_task_runner_handle.h"
  10. #include "remoting/protocol/fake_authenticator.h"
  11. #include "remoting/protocol/session_plugin.h"
  12. #include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
  13. namespace remoting {
  14. namespace protocol {
  15. const char kTestJid[] = "host1@gmail.com/chromoting123";
  16. const char kTestAuthKey[] = "test_auth_key";
  17. FakeSession::FakeSession()
  18. : config_(SessionConfig::ForTest()), jid_(kTestJid) {}
  19. FakeSession::~FakeSession() = default;
  20. void FakeSession::SimulateConnection(FakeSession* peer) {
  21. peer_ = peer->weak_factory_.GetWeakPtr();
  22. peer->peer_ = weak_factory_.GetWeakPtr();
  23. event_handler_->OnSessionStateChange(CONNECTING);
  24. peer->event_handler_->OnSessionStateChange(ACCEPTING);
  25. peer->event_handler_->OnSessionStateChange(ACCEPTED);
  26. event_handler_->OnSessionStateChange(ACCEPTED);
  27. event_handler_->OnSessionStateChange(AUTHENTICATING);
  28. peer->event_handler_->OnSessionStateChange(AUTHENTICATING);
  29. // Initialize transport and authenticator on the client.
  30. authenticator_ =
  31. std::make_unique<FakeAuthenticator>(FakeAuthenticator::ACCEPT);
  32. authenticator_->set_auth_key(kTestAuthKey);
  33. transport_->Start(authenticator_.get(),
  34. base::BindRepeating(&FakeSession::SendTransportInfo,
  35. weak_factory_.GetWeakPtr()));
  36. // Initialize transport and authenticator on the host.
  37. peer->authenticator_ =
  38. std::make_unique<FakeAuthenticator>(FakeAuthenticator::ACCEPT);
  39. peer->authenticator_->set_auth_key(kTestAuthKey);
  40. peer->transport_->Start(
  41. peer->authenticator_.get(),
  42. base::BindRepeating(&FakeSession::SendTransportInfo, peer_));
  43. peer->event_handler_->OnSessionStateChange(AUTHENTICATED);
  44. event_handler_->OnSessionStateChange(AUTHENTICATED);
  45. }
  46. void FakeSession::SetEventHandler(EventHandler* event_handler) {
  47. event_handler_ = event_handler;
  48. }
  49. ErrorCode FakeSession::error() {
  50. return error_;
  51. }
  52. const std::string& FakeSession::jid() {
  53. return jid_;
  54. }
  55. const SessionConfig& FakeSession::config() {
  56. return *config_;
  57. }
  58. void FakeSession::SetTransport(Transport* transport) {
  59. transport_ = transport;
  60. }
  61. void FakeSession::Close(ErrorCode error) {
  62. closed_ = true;
  63. error_ = error;
  64. event_handler_->OnSessionStateChange(CLOSED);
  65. base::WeakPtr<FakeSession> peer = peer_;
  66. if (peer) {
  67. peer->peer_.reset();
  68. peer_.reset();
  69. if (signaling_delay_.is_zero()) {
  70. peer->Close(error);
  71. } else {
  72. base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
  73. FROM_HERE, base::BindOnce(&FakeSession::Close, peer, error),
  74. signaling_delay_);
  75. }
  76. }
  77. }
  78. void FakeSession::SendTransportInfo(
  79. std::unique_ptr<jingle_xmpp::XmlElement> transport_info) {
  80. if (!peer_)
  81. return;
  82. if (signaling_delay_.is_zero()) {
  83. peer_->ProcessTransportInfo(std::move(transport_info));
  84. } else {
  85. base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
  86. FROM_HERE,
  87. base::BindOnce(&FakeSession::ProcessTransportInfo, peer_,
  88. std::move(transport_info)),
  89. signaling_delay_);
  90. }
  91. }
  92. void FakeSession::ProcessTransportInfo(
  93. std::unique_ptr<jingle_xmpp::XmlElement> transport_info) {
  94. transport_->ProcessTransportInfo(transport_info.get());
  95. }
  96. void FakeSession::AddPlugin(SessionPlugin* plugin) {
  97. DCHECK(plugin);
  98. for (const auto& message : attachments_) {
  99. if (message) {
  100. JingleMessage jingle_message;
  101. jingle_message.AddAttachment(
  102. std::make_unique<jingle_xmpp::XmlElement>(*message));
  103. plugin->OnIncomingMessage(*(jingle_message.attachments));
  104. }
  105. }
  106. }
  107. void FakeSession::SetAttachment(size_t round,
  108. std::unique_ptr<jingle_xmpp::XmlElement> attachment) {
  109. while (attachments_.size() <= round) {
  110. attachments_.emplace_back();
  111. }
  112. attachments_[round] = std::move(attachment);
  113. }
  114. } // namespace protocol
  115. } // namespace remoting