1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/protocol/fake_session.h"
#include <memory>
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/task/single_thread_task_runner.h"
#include "remoting/protocol/authenticator.h"
#include "remoting/protocol/fake_authenticator.h"
#include "remoting/protocol/session_plugin.h"
#include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
namespace remoting::protocol {
const char kTestJid[] = "host1@gmail.com/chromoting123";
const char kTestAuthKey[] = "test_auth_key";
FakeSession::FakeSession()
: config_(SessionConfig::ForTest()), jid_(kTestJid) {}
FakeSession::~FakeSession() = default;
void FakeSession::SimulateConnection(FakeSession* peer) {
peer_ = peer->weak_factory_.GetWeakPtr();
peer->peer_ = weak_factory_.GetWeakPtr();
event_handler_->OnSessionStateChange(CONNECTING);
peer->event_handler_->OnSessionStateChange(ACCEPTING);
peer->event_handler_->OnSessionStateChange(ACCEPTED);
event_handler_->OnSessionStateChange(ACCEPTED);
event_handler_->OnSessionStateChange(AUTHENTICATING);
peer->event_handler_->OnSessionStateChange(AUTHENTICATING);
// Initialize transport and authenticator on the client.
authenticator_ =
std::make_unique<FakeAuthenticator>(FakeAuthenticator::ACCEPT);
authenticator_->set_auth_key(kTestAuthKey);
transport_->Start(authenticator_.get(),
base::BindRepeating(&FakeSession::SendTransportInfo,
weak_factory_.GetWeakPtr()));
// Initialize transport and authenticator on the host.
peer->authenticator_ =
std::make_unique<FakeAuthenticator>(FakeAuthenticator::ACCEPT);
peer->authenticator_->set_auth_key(kTestAuthKey);
peer->transport_->Start(
peer->authenticator_.get(),
base::BindRepeating(&FakeSession::SendTransportInfo, peer_));
peer->event_handler_->OnSessionStateChange(AUTHENTICATED);
event_handler_->OnSessionStateChange(AUTHENTICATED);
}
void FakeSession::SetEventHandler(EventHandler* event_handler) {
event_handler_ = event_handler;
}
ErrorCode FakeSession::error() const {
return error_;
}
const std::string& FakeSession::jid() {
return jid_;
}
const SessionConfig& FakeSession::config() {
return *config_;
}
const Authenticator& FakeSession::authenticator() const {
return *authenticator_;
}
void FakeSession::SetTransport(Transport* transport) {
transport_ = transport;
}
void FakeSession::Close(ErrorCode error,
std::string_view error_details,
const SourceLocation& error_location) {
closed_ = true;
error_ = error;
event_handler_->OnSessionStateChange(CLOSED);
base::WeakPtr<FakeSession> peer = peer_;
if (peer) {
peer->peer_.reset();
peer_.reset();
if (signaling_delay_.is_zero()) {
peer->Close(error, error_details, error_location);
} else {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
// Cannot just bind `error_details` as a string view, since the
// underlying data could be invalidated before the callback is run.
// See: crbug.com/376675478
base::BindOnce(&FakeSession::Close, peer, error,
std::string(error_details), error_location),
signaling_delay_);
}
}
}
void FakeSession::SendTransportInfo(
std::unique_ptr<jingle_xmpp::XmlElement> transport_info) {
if (!peer_) {
return;
}
if (signaling_delay_.is_zero()) {
peer_->ProcessTransportInfo(std::move(transport_info));
} else {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&FakeSession::ProcessTransportInfo, peer_,
std::move(transport_info)),
signaling_delay_);
}
}
void FakeSession::ProcessTransportInfo(
std::unique_ptr<jingle_xmpp::XmlElement> transport_info) {
transport_->ProcessTransportInfo(transport_info.get());
}
void FakeSession::AddPlugin(SessionPlugin* plugin) {
DCHECK(plugin);
for (const auto& message : attachments_) {
if (message) {
JingleMessage jingle_message;
jingle_message.AddAttachment(
std::make_unique<jingle_xmpp::XmlElement>(*message));
plugin->OnIncomingMessage(*(jingle_message.attachments));
}
}
}
void FakeSession::SetAttachment(
size_t round,
std::unique_ptr<jingle_xmpp::XmlElement> attachment) {
while (attachments_.size() <= round) {
attachments_.emplace_back();
}
attachments_[round] = std::move(attachment);
}
} // namespace remoting::protocol
|