Commit cc04df95 authored by Sergey Ulanov's avatar Sergey Ulanov Committed by Commit Bot

Cleanup SupportsWeakPtr from remoting

Removed SupportsWeakPtr from all classes under remoting.
Also refactored HostStatusMonitor to make it ref-counted instead of
an abstract interface. This allows it to outlive ChromotingHost, so
the observers don't need to worry about monitor reference being valid.

Change-Id: I5990145d99047466a51ef8fce0873c9a1cdd9cbe
Reviewed-on: https://chromium-review.googlesource.com/551035
Commit-Queue: Sergey Ulanov <sergeyu@chromium.org>
Reviewed-by: default avatarWez <wez@chromium.org>
Reviewed-by: default avatarJoe Downing <joedow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#484993}
parent 5604cb88
......@@ -172,6 +172,7 @@ static_library("host") {
"host_session_options.h",
"host_status_logger.cc",
"host_status_logger.h",
"host_status_monitor.cc",
"host_status_monitor.h",
"host_status_observer.h",
"host_window.cc",
......@@ -411,7 +412,6 @@ static_library("test_support") {
"fake_desktop_environment.h",
"fake_host_extension.cc",
"fake_host_extension.h",
"fake_host_status_monitor.h",
"fake_mouse_cursor_monitor.cc",
"fake_mouse_cursor_monitor.h",
"host_mock_objects.cc",
......
......@@ -75,7 +75,7 @@ ChromotingHost::ChromotingHost(
transport_context_(transport_context),
audio_task_runner_(audio_task_runner),
video_encode_task_runner_(video_encode_task_runner),
started_(false),
status_monitor_(new HostStatusMonitor()),
login_backoff_(&kDefaultBackoffPolicy),
desktop_environment_options_(options),
weak_factory_(this) {
......@@ -96,7 +96,7 @@ ChromotingHost::~ChromotingHost() {
// Notify observers.
if (started_) {
for (auto& observer : status_observers_)
for (auto& observer : status_monitor_->observers())
observer.OnShutdown();
}
}
......@@ -107,23 +107,13 @@ void ChromotingHost::Start(const std::string& host_owner_email) {
HOST_LOG << "Starting host";
started_ = true;
for (auto& observer : status_observers_)
for (auto& observer : status_monitor_->observers())
observer.OnStart(host_owner_email);
session_manager_->AcceptIncoming(
base::Bind(&ChromotingHost::OnIncomingSession, base::Unretained(this)));
}
void ChromotingHost::AddStatusObserver(HostStatusObserver* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
status_observers_.AddObserver(observer);
}
void ChromotingHost::RemoveStatusObserver(HostStatusObserver* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
status_observers_.RemoveObserver(observer);
}
void ChromotingHost::AddExtension(std::unique_ptr<HostExtension> extension) {
extensions_.push_back(std::move(extension));
}
......@@ -176,7 +166,7 @@ void ChromotingHost::OnSessionAuthenticated(ClientSession* client) {
DCHECK(clients_.front().get() == client);
// Notify observers that there is at least one authenticated client.
for (auto& observer : status_observers_)
for (auto& observer : status_monitor_->observers())
observer.OnClientAuthenticated(client->client_jid());
}
......@@ -184,7 +174,7 @@ void ChromotingHost::OnSessionChannelsConnected(ClientSession* client) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Notify observers.
for (auto& observer : status_observers_)
for (auto& observer : status_monitor_->observers())
observer.OnClientConnected(client->client_jid());
}
......@@ -192,7 +182,7 @@ void ChromotingHost::OnSessionAuthenticationFailed(ClientSession* client) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Notify observers.
for (auto& observer : status_observers_)
for (auto& observer : status_monitor_->observers())
observer.OnAccessDenied(client->client_jid());
}
......@@ -211,7 +201,7 @@ void ChromotingHost::OnSessionClosed(ClientSession* client) {
clients_.erase(it);
if (was_authenticated) {
for (auto& observer : status_observers_)
for (auto& observer : status_monitor_->observers())
observer.OnClientDisconnected(jid);
}
}
......@@ -221,7 +211,7 @@ void ChromotingHost::OnSessionRouteChange(
const std::string& channel_name,
const protocol::TransportRoute& route) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (auto& observer : status_observers_)
for (auto& observer : status_monitor_->observers())
observer.OnClientRouteChange(session->client_jid(), channel_name, route);
}
......
......@@ -62,8 +62,7 @@ class DesktopEnvironmentFactory;
// all pending tasks to complete. After all of that completed we
// return to the idle state. We then go to step (2) if there a new
// incoming connection.
class ChromotingHost : public ClientSession::EventHandler,
public HostStatusMonitor {
class ChromotingHost : public ClientSession::EventHandler {
public:
typedef std::vector<std::unique_ptr<ClientSession>> ClientSessions;
......@@ -85,9 +84,7 @@ class ChromotingHost : public ClientSession::EventHandler,
// This method can only be called once during the lifetime of this object.
void Start(const std::string& host_owner);
// HostStatusMonitor interface.
void AddStatusObserver(HostStatusObserver* observer) override;
void RemoveStatusObserver(HostStatusObserver* observer) override;
scoped_refptr<HostStatusMonitor> status_monitor() { return status_monitor_; }
// Registers a host extension.
void AddExtension(std::unique_ptr<HostExtension> extension);
......@@ -133,10 +130,6 @@ class ChromotingHost : public ClientSession::EventHandler,
const ClientSessions& client_sessions_for_tests() { return clients_; }
base::WeakPtr<ChromotingHost> AsWeakPtr() {
return weak_factory_.GetWeakPtr();
}
scoped_refptr<protocol::TransportContext> transport_context_for_tests() {
return transport_context_;
}
......@@ -154,14 +147,13 @@ class ChromotingHost : public ClientSession::EventHandler,
scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner_;
scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner_;
// Must be used on the network thread only.
base::ObserverList<HostStatusObserver> status_observers_;
scoped_refptr<HostStatusMonitor> status_monitor_;
// The connections to remote clients.
ClientSessions clients_;
// True if the host has been started.
bool started_;
bool started_ = false;
// Login backoff state.
net::BackoffEntry login_backoff_;
......
......@@ -70,10 +70,10 @@ class ChromotingHostTest : public testing::Test {
host_.reset(new ChromotingHost(
desktop_environment_factory_.get(), base::WrapUnique(session_manager_),
protocol::TransportContext::ForTests(protocol::TransportRole::SERVER),
task_runner_, // Audio
task_runner_, // Audio
task_runner_,
DesktopEnvironmentOptions::CreateDefault())); // Video encode
host_->AddStatusObserver(&host_status_observer_);
host_->status_monitor()->AddStatusObserver(&host_status_observer_);
xmpp_login_ = "host@domain";
session1_ = new MockSession();
......
......@@ -46,10 +46,8 @@ std::ostream& operator<<(std::ostream& os, const ScreenResolution& resolution) {
DaemonProcess::~DaemonProcess() {
DCHECK(caller_task_runner()->BelongsToCurrentThread());
host_event_logger_.reset();
weak_factory_.InvalidateWeakPtrs();
config_watcher_.reset();
host_event_logger_ = nullptr;
config_watcher_ = nullptr;
DeleteAllDesktopSessions();
}
......@@ -69,18 +67,6 @@ void DaemonProcess::OnConfigWatcherError() {
Stop();
}
void DaemonProcess::AddStatusObserver(HostStatusObserver* observer) {
DCHECK(caller_task_runner()->BelongsToCurrentThread());
status_observers_.AddObserver(observer);
}
void DaemonProcess::RemoveStatusObserver(HostStatusObserver* observer) {
DCHECK(caller_task_runner()->BelongsToCurrentThread());
status_observers_.RemoveObserver(observer);
}
void DaemonProcess::OnChannelConnected(int32_t peer_pid) {
DCHECK(caller_task_runner()->BelongsToCurrentThread());
......@@ -191,8 +177,8 @@ DaemonProcess::DaemonProcess(
io_task_runner_(io_task_runner),
next_terminal_id_(0),
stopped_callback_(stopped_callback),
current_process_stats_("DaemonProcess"),
weak_factory_(this) {
status_monitor_(new HostStatusMonitor()),
current_process_stats_("DaemonProcess") {
DCHECK(caller_task_runner->BelongsToCurrentThread());
// TODO(sammc): On OSX, mojo::edk::SetMachPortProvider() should be called with
// a base::PortProvider implementation. Add it here when this code is used on
......@@ -290,7 +276,7 @@ void DaemonProcess::Initialize() {
caller_task_runner(), io_task_runner(), config_path));
config_watcher_->Watch(this);
host_event_logger_ =
HostEventLogger::Create(weak_factory_.GetWeakPtr(), kApplicationName);
HostEventLogger::Create(status_monitor_, kApplicationName);
// Launch the process.
LaunchNetworkProcess();
......
......@@ -14,7 +14,6 @@
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/process/process.h"
#include "base/time/time.h"
......@@ -46,7 +45,6 @@ class ScreenResolution;
// sessions.
class DaemonProcess
: public ConfigWatcher::Delegate,
public HostStatusMonitor,
public WorkerProcessIpcDelegate,
public protocol::ProcessStatsStub {
public:
......@@ -67,9 +65,7 @@ class DaemonProcess
void OnConfigUpdated(const std::string& serialized_config) override;
void OnConfigWatcherError() override;
// HostStatusMonitor interface.
void AddStatusObserver(HostStatusObserver* observer) override;
void RemoveStatusObserver(HostStatusObserver* observer) override;
scoped_refptr<HostStatusMonitor> status_monitor() { return status_monitor_; }
// WorkerProcessIpcDelegate implementation.
void OnChannelConnected(int32_t peer_pid) override;
......@@ -201,6 +197,8 @@ class DaemonProcess
// Writes host status updates to the system event log.
std::unique_ptr<HostEventLogger> host_event_logger_;
scoped_refptr<HostStatusMonitor> status_monitor_;
// Reports process statistic data to network process.
std::unique_ptr<ProcessStatsSender> stats_sender_;
......@@ -215,8 +213,6 @@ class DaemonProcess
CurrentProcessStatsAgent current_process_stats_;
base::WeakPtrFactory<DaemonProcess> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(DaemonProcess);
};
......
......@@ -38,7 +38,8 @@ DesktopProcess::DesktopProcess(
: caller_task_runner_(caller_task_runner),
input_task_runner_(input_task_runner),
io_task_runner_(io_task_runner),
daemon_channel_handle_(std::move(daemon_channel_handle)) {
daemon_channel_handle_(std::move(daemon_channel_handle)),
weak_factory_(this) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
DCHECK(base::MessageLoopForUI::IsCurrent());
}
......@@ -66,7 +67,7 @@ void DesktopProcess::InjectSas() {
daemon_channel_->Send(new ChromotingDesktopDaemonMsg_InjectSas());
}
void DesktopProcess::LockWorkStation() {
void DesktopProcess::LockWorkstation() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
#if defined(OS_WIN)
if (base::win::OSInfo::GetInstance()->version_type() ==
......@@ -145,7 +146,7 @@ bool DesktopProcess::Start(
// Start the agent and create an IPC channel to talk to it.
mojo::ScopedMessagePipeHandle desktop_pipe =
desktop_agent_->Start(AsWeakPtr());
desktop_agent_->Start(weak_factory_.GetWeakPtr());
// Connect to the daemon.
daemon_channel_ = IPC::ChannelProxy::Create(daemon_channel_handle_.release(),
......
......@@ -30,8 +30,7 @@ class DesktopEnvironmentFactory;
class DesktopSessionAgent;
class DesktopProcess : public DesktopSessionAgent::Delegate,
public IPC::Listener,
public base::SupportsWeakPtr<DesktopProcess> {
public IPC::Listener {
public:
DesktopProcess(scoped_refptr<AutoThreadTaskRunner> caller_task_runner,
scoped_refptr<AutoThreadTaskRunner> input_task_runner,
......@@ -52,7 +51,7 @@ class DesktopProcess : public DesktopSessionAgent::Delegate,
void InjectSas();
// Locks the workstation for the current session.
void LockWorkStation();
void LockWorkstation();
// Creates the desktop agent and required threads and IPC channels. Returns
// true on success.
......@@ -90,6 +89,8 @@ class DesktopProcess : public DesktopSessionAgent::Delegate,
// the network process.
scoped_refptr<DesktopSessionAgent> desktop_agent_;
base::WeakPtrFactory<DesktopProcess> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(DesktopProcess);
};
......
......@@ -79,12 +79,15 @@ int DesktopProcessMain() {
// Create a platform-dependent environment factory.
std::unique_ptr<DesktopEnvironmentFactory> desktop_environment_factory;
#if defined(OS_WIN)
// base::Unretained() is safe here: |desktop_process| outlives run_loop.Run().
auto inject_sas_closure = base::Bind(&DesktopProcess::InjectSas,
base::Unretained(&desktop_process));
auto lock_workstation_closure = base::Bind(
&DesktopProcess::LockWorkstation, base::Unretained(&desktop_process));
desktop_environment_factory.reset(new SessionDesktopEnvironmentFactory(
ui_task_runner, video_capture_task_runner, input_task_runner,
ui_task_runner,
base::Bind(&DesktopProcess::InjectSas, desktop_process.AsWeakPtr()),
base::Bind(&DesktopProcess::LockWorkStation,
desktop_process.AsWeakPtr())));
ui_task_runner, inject_sas_closure, lock_workstation_closure));
#else // !defined(OS_WIN)
desktop_environment_factory.reset(new Me2MeDesktopEnvironmentFactory(
ui_task_runner, video_capture_task_runner, input_task_runner,
......
......@@ -15,7 +15,7 @@
namespace remoting {
FakeInputInjector::FakeInputInjector() {}
FakeInputInjector::FakeInputInjector() : weak_factory_(this) {}
FakeInputInjector::~FakeInputInjector() {}
void FakeInputInjector::Start(
......@@ -57,7 +57,9 @@ void FakeScreenControls::SetScreenResolution(
FakeDesktopEnvironment::FakeDesktopEnvironment(
scoped_refptr<base::SingleThreadTaskRunner> capture_thread,
const DesktopEnvironmentOptions& options)
: capture_thread_(std::move(capture_thread)), options_(options) {}
: capture_thread_(std::move(capture_thread)),
options_(options),
weak_factory_(this) {}
FakeDesktopEnvironment::~FakeDesktopEnvironment() = default;
......@@ -68,7 +70,7 @@ std::unique_ptr<AudioCapturer> FakeDesktopEnvironment::CreateAudioCapturer() {
std::unique_ptr<InputInjector> FakeDesktopEnvironment::CreateInputInjector() {
std::unique_ptr<FakeInputInjector> result(new FakeInputInjector());
last_input_injector_ = result->AsWeakPtr();
last_input_injector_ = result->weak_factory_.GetWeakPtr();
return std::move(result);
}
......@@ -121,7 +123,7 @@ std::unique_ptr<DesktopEnvironment> FakeDesktopEnvironmentFactory::Create(
std::unique_ptr<FakeDesktopEnvironment> result(
new FakeDesktopEnvironment(capture_thread_, options));
result->set_frame_generator(frame_generator_);
last_desktop_environment_ = result->AsWeakPtr();
last_desktop_environment_ = result->weak_factory_.GetWeakPtr();
return std::move(result);
}
......
......@@ -21,8 +21,7 @@
namespace remoting {
class FakeInputInjector : public InputInjector,
public base::SupportsWeakPtr<FakeInputInjector> {
class FakeInputInjector : public InputInjector {
public:
FakeInputInjector();
~FakeInputInjector() override;
......@@ -53,11 +52,17 @@ class FakeInputInjector : public InputInjector,
}
private:
friend class FakeDesktopEnvironment;
std::vector<protocol::KeyEvent>* key_events_ = nullptr;
std::vector<protocol::TextEvent>* text_events_ = nullptr;
std::vector<protocol::MouseEvent>* mouse_events_ = nullptr;
std::vector<protocol::TouchEvent>* touch_events_ = nullptr;
std::vector<protocol::ClipboardEvent>* clipboard_events_ = nullptr;
base::WeakPtrFactory<FakeInputInjector> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(FakeInputInjector);
};
class FakeScreenControls : public ScreenControls {
......@@ -69,9 +74,7 @@ class FakeScreenControls : public ScreenControls {
void SetScreenResolution(const ScreenResolution& resolution) override;
};
class FakeDesktopEnvironment
: public DesktopEnvironment,
public base::SupportsWeakPtr<FakeDesktopEnvironment> {
class FakeDesktopEnvironment : public DesktopEnvironment {
public:
explicit FakeDesktopEnvironment(
scoped_refptr<base::SingleThreadTaskRunner> capture_thread,
......@@ -103,6 +106,8 @@ class FakeDesktopEnvironment
}
private:
friend class FakeDesktopEnvironmentFactory;
scoped_refptr<base::SingleThreadTaskRunner> capture_thread_;
protocol::FakeDesktopCapturer::FrameGenerator frame_generator_;
......@@ -110,6 +115,8 @@ class FakeDesktopEnvironment
const DesktopEnvironmentOptions options_;
base::WeakPtrFactory<FakeDesktopEnvironment> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(FakeDesktopEnvironment);
};
......
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef REMOTING_HOST_FAKE_HOST_STATUS_MONITOR_H_
#define REMOTING_HOST_FAKE_HOST_STATUS_MONITOR_H_
#include "base/memory/weak_ptr.h"
#include "remoting/host/host_status_monitor.h"
namespace remoting {
// An implementation of HostStatusMonitor that does nothing.
class FakeHostStatusMonitor
: public base::SupportsWeakPtr<FakeHostStatusMonitor>,
public HostStatusMonitor {
public:
~FakeHostStatusMonitor() override {}
// HostStatusMonitor interface.
void AddStatusObserver(HostStatusObserver* observer) override {}
void RemoveStatusObserver(HostStatusObserver* observer) override {}
};
} // namespace remoting
#endif // REMOTING_HOST_FAKE_HOST_STATUS_MONITOR_H_
......@@ -23,7 +23,7 @@ class HostEventLogger {
// Creates an event-logger that monitors host status changes and logs
// corresponding events to the OS-specific log (syslog/EventLog).
static std::unique_ptr<HostEventLogger> Create(
base::WeakPtr<HostStatusMonitor> monitor,
scoped_refptr<HostStatusMonitor> monitor,
const std::string& application_name);
protected:
......
......@@ -26,7 +26,7 @@ namespace {
class HostEventLoggerPosix : public HostEventLogger, public HostStatusObserver {
public:
HostEventLoggerPosix(base::WeakPtr<HostStatusMonitor> monitor,
HostEventLoggerPosix(scoped_refptr<HostStatusMonitor> monitor,
const std::string& application_name);
~HostEventLoggerPosix() override;
......@@ -45,7 +45,7 @@ class HostEventLoggerPosix : public HostEventLogger, public HostStatusObserver {
private:
void Log(const std::string& message);
base::WeakPtr<HostStatusMonitor> monitor_;
scoped_refptr<HostStatusMonitor> monitor_;
std::string application_name_;
DISALLOW_COPY_AND_ASSIGN(HostEventLoggerPosix);
......@@ -54,17 +54,15 @@ class HostEventLoggerPosix : public HostEventLogger, public HostStatusObserver {
} //namespace
HostEventLoggerPosix::HostEventLoggerPosix(
base::WeakPtr<HostStatusMonitor> monitor,
scoped_refptr<HostStatusMonitor> monitor,
const std::string& application_name)
: monitor_(monitor),
application_name_(application_name) {
: monitor_(monitor), application_name_(application_name) {
openlog(application_name_.c_str(), 0, LOG_USER);
monitor_->AddStatusObserver(this);
}
HostEventLoggerPosix::~HostEventLoggerPosix() {
if (monitor_.get())
monitor_->RemoveStatusObserver(this);
monitor_->RemoveStatusObserver(this);
closelog();
}
......@@ -106,7 +104,7 @@ void HostEventLoggerPosix::Log(const std::string& message) {
// static
std::unique_ptr<HostEventLogger> HostEventLogger::Create(
base::WeakPtr<HostStatusMonitor> monitor,
scoped_refptr<HostStatusMonitor> monitor,
const std::string& application_name) {
return base::WrapUnique(new HostEventLoggerPosix(monitor, application_name));
}
......
......@@ -28,7 +28,7 @@ namespace {
class HostEventLoggerWin : public HostEventLogger, public HostStatusObserver {
public:
HostEventLoggerWin(base::WeakPtr<HostStatusMonitor> monitor,
HostEventLoggerWin(scoped_refptr<HostStatusMonitor> monitor,
const std::string& application_name);
~HostEventLoggerWin() override;
......@@ -49,20 +49,19 @@ class HostEventLoggerWin : public HostEventLogger, public HostStatusObserver {
void LogString(WORD type, DWORD event_id, const std::string& string);
void Log(WORD type, DWORD event_id, const std::vector<std::string>& strings);
base::WeakPtr<HostStatusMonitor> monitor_;
scoped_refptr<HostStatusMonitor> monitor_;
// The handle of the application event log.
HANDLE event_log_;
HANDLE event_log_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(HostEventLoggerWin);
};
} //namespace
} // namespace
HostEventLoggerWin::HostEventLoggerWin(base::WeakPtr<HostStatusMonitor> monitor,
HostEventLoggerWin::HostEventLoggerWin(scoped_refptr<HostStatusMonitor> monitor,
const std::string& application_name)
: monitor_(monitor),
event_log_(nullptr) {
: monitor_(monitor) {
event_log_ = RegisterEventSourceW(
nullptr, base::UTF8ToUTF16(application_name).c_str());
if (event_log_ != nullptr) {
......@@ -74,8 +73,7 @@ HostEventLoggerWin::HostEventLoggerWin(base::WeakPtr<HostStatusMonitor> monitor,
HostEventLoggerWin::~HostEventLoggerWin() {
if (event_log_ != nullptr) {
if (monitor_)
monitor_->RemoveStatusObserver(this);
monitor_->RemoveStatusObserver(this);
DeregisterEventSource(event_log_);
}
}
......@@ -128,14 +126,8 @@ void HostEventLoggerWin::Log(WORD type,
raw_strings[i] = utf16_strings[i].c_str();
}
if (!ReportEventW(event_log_,
type,
HOST_CATEGORY,
event_id,
nullptr,
static_cast<WORD>(raw_strings.size()),
0,
&raw_strings[0],
if (!ReportEventW(event_log_, type, HOST_CATEGORY, event_id, nullptr,
static_cast<WORD>(raw_strings.size()), 0, &raw_strings[0],
nullptr)) {
PLOG(ERROR) << "Failed to write an event to the event log";
}
......@@ -151,9 +143,9 @@ void HostEventLoggerWin::LogString(WORD type,
// static
std::unique_ptr<HostEventLogger> HostEventLogger::Create(
base::WeakPtr<HostStatusMonitor> monitor,
scoped_refptr<HostStatusMonitor> monitor,
const std::string& application_name) {
return base::WrapUnique(new HostEventLoggerWin(monitor, application_name));
return base::MakeUnique<HostEventLoggerWin>(monitor, application_name);
}
} // namespace remoting
......@@ -13,7 +13,7 @@
namespace remoting {
HostPowerSaveBlocker::HostPowerSaveBlocker(
base::WeakPtr<HostStatusMonitor> monitor,
scoped_refptr<HostStatusMonitor> monitor,
const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner,
const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner)
: monitor_(monitor),
......@@ -24,18 +24,14 @@ HostPowerSaveBlocker::HostPowerSaveBlocker(
}
HostPowerSaveBlocker::~HostPowerSaveBlocker() {
if (monitor_.get()) {
monitor_->RemoveStatusObserver(this);
}
monitor_->RemoveStatusObserver(this);
}
void HostPowerSaveBlocker::OnClientConnected(const std::string& jid) {
blocker_.reset(new device::PowerSaveBlocker(
device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep,
device::PowerSaveBlocker::kReasonOther,
"Remoting session is active",
ui_task_runner_,
file_task_runner_));
device::PowerSaveBlocker::kReasonOther, "Remoting session is active",
ui_task_runner_, file_task_runner_));
}
void HostPowerSaveBlocker::OnClientDisconnected(const std::string& jid) {
......
......@@ -28,7 +28,7 @@ class HostStatusMonitor;
class HostPowerSaveBlocker : public HostStatusObserver {
public:
HostPowerSaveBlocker(
base::WeakPtr<HostStatusMonitor> monitor,
scoped_refptr<HostStatusMonitor> monitor,
const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner,
const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner);
......@@ -40,7 +40,8 @@ class HostPowerSaveBlocker : public HostStatusObserver {
private:
friend class HostPowerSaveBlockerTest;
base::WeakPtr<HostStatusMonitor> monitor_;
scoped_refptr<HostStatusMonitor> monitor_;
scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
std::unique_ptr<device::PowerSaveBlocker> blocker_;
......
......@@ -9,7 +9,7 @@
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/threading/thread.h"
#include "remoting/host/fake_host_status_monitor.h"
#include "remoting/host/host_status_monitor.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace remoting {
......@@ -25,18 +25,18 @@ class HostPowerSaveBlockerTest : public testing::Test {
base::MessageLoopForUI ui_message_loop_;
base::Thread blocking_thread_;
FakeHostStatusMonitor monitor_;
scoped_refptr<HostStatusMonitor> monitor_;
std::unique_ptr<HostPowerSaveBlocker> blocker_;
};
HostPowerSaveBlockerTest::HostPowerSaveBlockerTest()
: blocking_thread_("block-thread") {}
: blocking_thread_("block-thread"), monitor_(new HostStatusMonitor()) {}
void HostPowerSaveBlockerTest::SetUp() {
ASSERT_TRUE(blocking_thread_.StartWithOptions(
base::Thread::Options(base::MessageLoop::TYPE_IO, 0)) &&
blocking_thread_.WaitUntilThreadStarted());
blocker_.reset(new HostPowerSaveBlocker(monitor_.AsWeakPtr(),
blocker_.reset(new HostPowerSaveBlocker(monitor_,
ui_message_loop_.task_runner(),
blocking_thread_.task_runner()));
}
......
......@@ -13,7 +13,7 @@
namespace remoting {
HostStatusLogger::HostStatusLogger(base::WeakPtr<HostStatusMonitor> monitor,
HostStatusLogger::HostStatusLogger(scoped_refptr<HostStatusMonitor> monitor,
ServerLogEntry::Mode mode,
SignalStrategy* signal_strategy,
const std::string& directory_bot_jid)
......@@ -24,8 +24,7 @@ HostStatusLogger::HostStatusLogger(base::WeakPtr<HostStatusMonitor> monitor,