<commit_before>//
// PROJECT:         Aspia Remote Desktop
// FILE:            host/host_session.cc
// LICENSE:         Mozilla Public License Version 2.0
// PROGRAMMERS:     <NAME> (<EMAIL>)
//

#include "host/host_session.h"
#include "host/console_session_launcher.h"
#include "base/version_helpers.h"
#include "base/scoped_privilege.h"

namespace aspia {

static const std::chrono::seconds kAttachTimeout{ 30 };

HostSession::HostSession(
    proto::SessionType session_type,
    std::shared_ptr<NetworkChannelProxy> channel_proxy)
    : session_type_(session_type),
      channel_proxy_(channel_proxy),
      process_connector_(channel_proxy)
{
    ui_thread_.Start(MessageLoop::TYPE_UI, this);
}

HostSession::~HostSession()
{
    ui_thread_.Stop();
}

void HostSession::OnBeforeThreadRunning()
{
    runner_ = ui_thread_.message_loop_proxy();
    DCHECK(runner_);

    OnSessionAttached(WTSGetActiveConsoleSessionId());

    // In Windows XP/2003, the console session always has a ID equal to 0.
    // We do not need to watch the session.
    if (IsWindowsVistaOrGreater())
    {
        bool ret = session_watcher_.StartWatching(this);
        DCHECK(ret);
    }
}

void HostSession::OnAfterThreadRunning()
{
    session_watcher_.StopWatching();
    OnSessionDetached();
    timer_.Stop();
    channel_proxy_->Disconnect();
}

void HostSession::OnSessionAttached(uint32_t session_id)
{
    DCHECK(runner_->BelongsToCurrentThread());
    DCHECK(state_ == State::DETACHED);
    DCHECK(!process_.IsValid());

    state_ = State::STARTING;

    std::wstring channel_id;

    if (process_connector_.StartServer(channel_id))
    {
        bool launched;

        switch (session_type_)
        {
            case proto::SESSION_TYPE_DESKTOP_MANAGE:
            case proto::SESSION_TYPE_DESKTOP_VIEW:
                launched = LaunchDesktopSession(session_id, channel_id);
                break;

            case proto::SESSION_TYPE_FILE_TRANSFER:
                launched = LaunchFileTransferSession(session_id, channel_id);
                break;

            case proto::SESSION_TYPE_POWER_MANAGE:
                launched = LaunchPowerManageSession(session_id, channel_id);
                break;

            default:
                launched = false;
                break;
        }

        PipeChannel::DisconnectHandler disconnect_handler =
            std::bind(&HostSession::OnIpcChannelDisconnect, this);

        uint32_t user_data = static_cast<uint32_t>(session_type_);
<commit_msg>- Added debug message<commit_after>//
// PROJECT:         Aspia Remote Desktop
// FILE:            host/host_session.cc
// LICENSE:         Mozilla Public License Version 2.0
// PROGRAMMERS:     <NAME> (<EMAIL>)
//

#include "host/host_session.h"
#include "host/console_session_launcher.h"
#include "base/version_helpers.h"
#include "base/scoped_privilege.h"

namespace aspia {

static const std::chrono::seconds kAttachTimeout{ 30 };

HostSession::HostSession(
    proto::SessionType session_type,
    std::shared_ptr<NetworkChannelProxy> channel_proxy)
    : session_type_(session_type),
      channel_proxy_(channel_proxy),
      process_connector_(channel_proxy)
{
    ui_thread_.Start(MessageLoop::TYPE_UI, this);
}

HostSession::~HostSession()
{
    ui_thread_.Stop();
}

void HostSession::OnBeforeThreadRunning()
{
    runner_ = ui_thread_.message_loop_proxy();
    DCHECK(runner_);

    OnSessionAttached(WTSGetActiveConsoleSessionId());

    // In Windows XP/2003, the console session always has a ID equal to 0.
    // We do not need to watch the session.
    if (IsWindowsVistaOrGreater())
    {
        bool ret = session_watcher_.StartWatching(this);
        DCHECK(ret);
    }
}

void HostSession::OnAfterThreadRunning()
{
    session_watcher_.StopWatching();
    OnSessionDetached();
    timer_.Stop();
    channel_proxy_->Disconnect();
}

void HostSession::OnSessionAttached(uint32_t session_id)
{
    DCHECK(runner_->BelongsToCurrentThread());
    DCHECK(state_ == State::DETACHED);
    DCHECK(!process_.IsValid());

    state_ = State::STARTING;

    std::wstring channel_id;

    if (process_connector_.StartServer(channel_id))
    {
        bool launched;

        switch (session_type_)
        {
            case proto::SESSION_TYPE_DESKTOP_MANAGE:
            case proto::SESSION_TYPE_DESKTOP_VIEW:
                launched = LaunchDesktopSession(session_id, channel_id);
                break;

            case proto::SESSION_TYPE_FILE_TRANSFER:
                launched = LaunchFileTransferSession(session_id, channel_id);
                break;

            case proto::SESSION_TYPE_POWER_MANAGE:
                launched = LaunchPowerManageSession(session_id, channel_id);
                break;

            default:
                DLOG(ERROR) << "Unknown session type: " << session_type_;
                launched = false;
                break;
        }

        PipeChannel::DisconnectHandler disconnect_handler =
            std::bind(&HostSession::OnIpcChannelDisconnect, this);

        uint32_t user_data = static_cast<uint32_t>(session_type_);

{'fraction_non_alphanumeric': 0.10247836349331235, 'fraction_numerical': 0.005900865460267506, 'mean_word_length': 2.969555035128806, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6245781', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1491, 'n_words': 268}