Skip to content

Commit

Permalink
[remoting] Add cross-platform code for sending ActiveDisplay messages.
Browse files Browse the repository at this point in the history
This adds a stub class (ActiveDisplayMonitor) and cross-platform code
for sending ActiveDisplay messages to the client. This will allow
per-platform implementations to be added in future.

Bug: 1447942
Change-Id: Ibdad7ebbcbefd57654f751214a20769393e99d07
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4568890
Reviewed-by: Joe Downing <joedow@chromium.org>
Auto-Submit: Lambros Lambrou <lambroslambrou@chromium.org>
Commit-Queue: Lambros Lambrou <lambroslambrou@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1150857}
  • Loading branch information
Lambros Lambrou authored and Chromium LUCI CQ committed May 30, 2023
1 parent 66992df commit 2623af3
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 2 deletions.
4 changes: 4 additions & 0 deletions remoting/host/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ static_library("common") {
"action_executor.cc",
"action_message_handler.cc",
"action_message_handler.h",
"active_display_monitor.cc",
"active_display_monitor.h",
"audio_capturer.cc",
"audio_silence_detector.cc",
"audio_volume_filter.cc",
Expand Down Expand Up @@ -715,6 +717,8 @@ static_library("test_support") {
testonly = true

sources = [
"fake_active_display_monitor.cc",
"fake_active_display_monitor.h",
"fake_desktop_environment.cc",
"fake_desktop_environment.h",
"fake_host_extension.cc",
Expand Down
21 changes: 21 additions & 0 deletions remoting/host/active_display_monitor.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2023 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/host/active_display_monitor.h"

#include "base/functional/callback.h"
#include "base/notreached.h"
#include "base/task/single_thread_task_runner.h"

namespace remoting {

// static
std::unique_ptr<ActiveDisplayMonitor> ActiveDisplayMonitor::Create(
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
ActiveDisplayMonitor::Callback active_display_callback) {
NOTIMPLEMENTED();
return nullptr;
}

} // namespace remoting
33 changes: 33 additions & 0 deletions remoting/host/active_display_monitor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_HOST_ACTIVE_DISPLAY_MONITOR_H_
#define REMOTING_HOST_ACTIVE_DISPLAY_MONITOR_H_

#include <memory>

#include "base/functional/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_capture_types.h"

namespace base {
class SingleThreadTaskRunner;
} // namespace base

namespace remoting {

class ActiveDisplayMonitor {
public:
using Callback = base::RepeatingCallback<void(webrtc::ScreenId)>;

virtual ~ActiveDisplayMonitor() = default;

static std::unique_ptr<ActiveDisplayMonitor> Create(
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
Callback active_display_callback);
};

} // namespace remoting

#endif // REMOTING_HOST_ACTIVE_DISPLAY_MONITOR_H_
7 changes: 7 additions & 0 deletions remoting/host/basic_desktop_environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "remoting/host/action_executor.h"
#include "remoting/host/active_display_monitor.h"
#include "remoting/host/audio_capturer.h"
#include "remoting/host/base/screen_controls.h"
#include "remoting/host/client_session_control.h"
Expand Down Expand Up @@ -155,6 +156,12 @@ BasicDesktopEnvironment::CreateKeyboardLayoutMonitor(
return KeyboardLayoutMonitor::Create(std::move(callback), input_task_runner_);
}

std::unique_ptr<ActiveDisplayMonitor>
BasicDesktopEnvironment::CreateActiveDisplayMonitor(
ActiveDisplayMonitor::Callback callback) {
return ActiveDisplayMonitor::Create(ui_task_runner_, std::move(callback));
}

std::unique_ptr<FileOperations>
BasicDesktopEnvironment::CreateFileOperations() {
return std::make_unique<LocalFileOperations>(ui_task_runner_);
Expand Down
2 changes: 2 additions & 0 deletions remoting/host/basic_desktop_environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class BasicDesktopEnvironment : public DesktopEnvironment {
std::unique_ptr<KeyboardLayoutMonitor> CreateKeyboardLayoutMonitor(
base::RepeatingCallback<void(const protocol::KeyboardLayout&)> callback)
override;
std::unique_ptr<ActiveDisplayMonitor> CreateActiveDisplayMonitor(
base::RepeatingCallback<void(webrtc::ScreenId)> callback) override;
std::unique_ptr<FileOperations> CreateFileOperations() override;
std::unique_ptr<UrlForwarderConfigurator> CreateUrlForwarderConfigurator()
override;
Expand Down
11 changes: 11 additions & 0 deletions remoting/host/client_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "remoting/base/session_options.h"
#include "remoting/host/action_executor.h"
#include "remoting/host/action_message_handler.h"
#include "remoting/host/active_display_monitor.h"
#include "remoting/host/audio_capturer.h"
#include "remoting/host/base/screen_controls.h"
#include "remoting/host/base/screen_resolution.h"
Expand Down Expand Up @@ -327,6 +328,10 @@ void ClientSession::SetCapabilities(
monitor->Start();
}

active_display_monitor_ =
desktop_environment_->CreateActiveDisplayMonitor(base::BindRepeating(
&ClientSession::OnActiveDisplayChanged, base::Unretained(this)));

// Re-send the extended layout information so the client has information
// needed to identify each stream.
if (desktop_display_info_.NumDisplays() != 0) {
Expand Down Expand Up @@ -1301,4 +1306,10 @@ void ClientSession::BoostFramerateOnInput(
}
}

void ClientSession::OnActiveDisplayChanged(webrtc::ScreenId display) {
protocol::ActiveDisplay active_display;
active_display.set_screen_id(display);
connection_->client_stub()->SetActiveDisplay(active_display);
}

} // namespace remoting
7 changes: 7 additions & 0 deletions remoting/host/client_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

namespace remoting {

class ActiveDisplayMonitor;
class AudioStream;
class DesktopEnvironment;
class DesktopEnvironmentFactory;
Expand Down Expand Up @@ -269,6 +270,10 @@ class ClientSession : public protocol::HostStub,
bool& mouse_button_down,
protocol::ObservingInputFilter::Event event);

// Sends the new active display to the client. Called by ActiveDisplayMonitor
// whenever the screen id associated with the active window changes.
void OnActiveDisplayChanged(webrtc::ScreenId display);

raw_ptr<EventHandler> event_handler_;

// Used to create a DesktopEnvironment instance for this session.
Expand Down Expand Up @@ -419,6 +424,8 @@ class ClientSession : public protocol::HostStub,
mojo::ReceiverSet<mojom::ChromotingSessionServices>
session_services_receivers_;

std::unique_ptr<ActiveDisplayMonitor> active_display_monitor_;

SEQUENCE_CHECKER(sequence_checker_);

// Used to disable callbacks to |this| once DisconnectSession() has been
Expand Down
25 changes: 25 additions & 0 deletions remoting/host/client_session_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "remoting/host/host_extension.h"
#include "remoting/host/host_extension_session.h"
#include "remoting/host/host_mock_objects.h"
#include "remoting/protocol/capability_names.h"
#include "remoting/protocol/fake_connection_to_client.h"
#include "remoting/protocol/fake_desktop_capturer.h"
#include "remoting/protocol/fake_message_pipe.h"
Expand Down Expand Up @@ -86,6 +87,10 @@ MATCHER_P(IncludesCapabilities, expected_capabilities, "") {
return true;
}

MATCHER_P(ScreenIdMatches, expected_id, "") {
return arg.screen_id() == expected_id;
}

protocol::MouseEvent MakeMouseMoveEvent(int x, int y) {
protocol::MouseEvent result;
result.set_x(x);
Expand Down Expand Up @@ -834,6 +839,26 @@ TEST_F(ClientSessionTest, ForwardHostSessionOptions2) {
->detect_updated_region());
}

TEST_F(ClientSessionTest, ActiveDisplayMessageSent) {
EXPECT_CALL(client_stub_, SetActiveDisplay(ScreenIdMatches(kDisplay1Id)));

// The ActiveDisplayMonitor only gets created after negotiating this
// capability with the client.
desktop_environment_factory_->set_capabilities(
protocol::kMultiStreamCapability);
CreateClientSession();
ConnectClientSession();

protocol::Capabilities client_capabilities;
client_capabilities.set_capabilities(protocol::kMultiStreamCapability);
client_session_->SetCapabilities(client_capabilities);

auto monitor = desktop_environment_factory_->last_desktop_environment()
->last_active_display_monitor();
ASSERT_TRUE(monitor);
monitor->SetActiveDisplay(static_cast<webrtc::ScreenId>(kDisplay1Id));
}

// Display selection behaves quite differently if capturing of the full desktop
// is enabled or not. To simplify things these tests only handle the ChromeOS
// situation, where full desktop capturing is disabled.
Expand Down
4 changes: 4 additions & 0 deletions remoting/host/desktop_environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "base/memory/weak_ptr.h"
#include "remoting/host/base/desktop_environment_options.h"
#include "remoting/protocol/desktop_capturer.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_capture_types.h"

namespace webrtc {
class DesktopCapturer;
Expand All @@ -22,6 +23,7 @@ class MouseCursorMonitor;
namespace remoting {

class ActionExecutor;
class ActiveDisplayMonitor;
class AudioCapturer;
class ClientSessionControl;
class ClientSessionEvents;
Expand Down Expand Up @@ -61,6 +63,8 @@ class DesktopEnvironment {
virtual std::unique_ptr<KeyboardLayoutMonitor> CreateKeyboardLayoutMonitor(
base::RepeatingCallback<void(const protocol::KeyboardLayout&)>
callback) = 0;
virtual std::unique_ptr<ActiveDisplayMonitor> CreateActiveDisplayMonitor(
base::RepeatingCallback<void(webrtc::ScreenId)> callback) = 0;
virtual std::unique_ptr<FileOperations> CreateFileOperations() = 0;
virtual std::unique_ptr<UrlForwarderConfigurator>
CreateUrlForwarderConfigurator() = 0;
Expand Down
23 changes: 23 additions & 0 deletions remoting/host/fake_active_display_monitor.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2023 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/host/fake_active_display_monitor.h"

namespace remoting {

FakeActiveDisplayMonitor::FakeActiveDisplayMonitor(
ActiveDisplayMonitor::Callback callback)
: callback_(callback) {}

FakeActiveDisplayMonitor::~FakeActiveDisplayMonitor() = default;

base::WeakPtr<FakeActiveDisplayMonitor> FakeActiveDisplayMonitor::GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}

void FakeActiveDisplayMonitor::SetActiveDisplay(webrtc::ScreenId display) {
callback_.Run(display);
}

} // namespace remoting
34 changes: 34 additions & 0 deletions remoting/host/fake_active_display_monitor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_HOST_FAKE_ACTIVE_DISPLAY_MONITOR_H_
#define REMOTING_HOST_FAKE_ACTIVE_DISPLAY_MONITOR_H_

#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "remoting/host/active_display_monitor.h"

namespace remoting {

class FakeActiveDisplayMonitor : public ActiveDisplayMonitor {
public:
explicit FakeActiveDisplayMonitor(ActiveDisplayMonitor::Callback callback);

FakeActiveDisplayMonitor(const FakeActiveDisplayMonitor&) = delete;
FakeActiveDisplayMonitor& operator=(const FakeActiveDisplayMonitor&) = delete;

~FakeActiveDisplayMonitor() override;

base::WeakPtr<FakeActiveDisplayMonitor> GetWeakPtr();
void SetActiveDisplay(webrtc::ScreenId display);

private:
ActiveDisplayMonitor::Callback callback_;

base::WeakPtrFactory<FakeActiveDisplayMonitor> weak_factory_{this};
};

} // namespace remoting

#endif // REMOTING_HOST_FAKE_ACTIVE_DISPLAY_MONITOR_H_
15 changes: 13 additions & 2 deletions remoting/host/fake_desktop_environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ FakeDesktopEnvironment::CreateKeyboardLayoutMonitor(
return std::make_unique<FakeKeyboardLayoutMonitor>();
}

std::unique_ptr<ActiveDisplayMonitor>
FakeDesktopEnvironment::CreateActiveDisplayMonitor(
ActiveDisplayMonitor::Callback callback) {
auto result = std::make_unique<FakeActiveDisplayMonitor>(callback);
last_active_display_monitor_ = result->GetWeakPtr();
return result;
}

std::unique_ptr<FileOperations> FakeDesktopEnvironment::CreateFileOperations() {
return nullptr;
}
Expand All @@ -132,10 +140,12 @@ FakeDesktopEnvironment::CreateUrlForwarderConfigurator() {
}

std::string FakeDesktopEnvironment::GetCapabilities() const {
return std::string();
return capabilities_;
}

void FakeDesktopEnvironment::SetCapabilities(const std::string& capabilities) {}
void FakeDesktopEnvironment::SetCapabilities(const std::string& capabilities) {
capabilities_ = capabilities;
}

uint32_t FakeDesktopEnvironment::GetDesktopSessionId() const {
return desktop_session_id_;
Expand Down Expand Up @@ -165,6 +175,7 @@ std::unique_ptr<DesktopEnvironment> FakeDesktopEnvironmentFactory::Create(
new FakeDesktopEnvironment(capture_thread_, options));
result->set_frame_generator(frame_generator_);
result->set_desktop_session_id(desktop_session_id_);
result->SetCapabilities(capabilities_);
last_desktop_environment_ = result->weak_factory_.GetWeakPtr();
return std::move(result);
}
Expand Down
18 changes: 18 additions & 0 deletions remoting/host/fake_desktop_environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "remoting/host/base/desktop_environment_options.h"
#include "remoting/host/base/screen_controls.h"
#include "remoting/host/desktop_environment.h"
#include "remoting/host/fake_active_display_monitor.h"
#include "remoting/host/fake_mouse_cursor_monitor.h"
#include "remoting/host/input_injector.h"
#include "remoting/protocol/fake_desktop_capturer.h"
Expand Down Expand Up @@ -115,6 +116,8 @@ class FakeDesktopEnvironment : public DesktopEnvironment {
std::unique_ptr<KeyboardLayoutMonitor> CreateKeyboardLayoutMonitor(
base::RepeatingCallback<void(const protocol::KeyboardLayout&)> callback)
override;
std::unique_ptr<ActiveDisplayMonitor> CreateActiveDisplayMonitor(
ActiveDisplayMonitor::Callback callback) override;
std::unique_ptr<FileOperations> CreateFileOperations() override;
std::unique_ptr<UrlForwarderConfigurator> CreateUrlForwarderConfigurator()
override;
Expand All @@ -128,6 +131,10 @@ class FakeDesktopEnvironment : public DesktopEnvironment {
return last_input_injector_;
}

base::WeakPtr<FakeActiveDisplayMonitor> last_active_display_monitor() {
return last_active_display_monitor_;
}

private:
friend class FakeDesktopEnvironmentFactory;

Expand All @@ -136,9 +143,12 @@ class FakeDesktopEnvironment : public DesktopEnvironment {
uint32_t desktop_session_id_ = UINT32_MAX;

base::WeakPtr<FakeInputInjector> last_input_injector_;
base::WeakPtr<FakeActiveDisplayMonitor> last_active_display_monitor_;

const DesktopEnvironmentOptions options_;

std::string capabilities_;

base::WeakPtrFactory<FakeDesktopEnvironment> weak_factory_{this};
};

Expand All @@ -164,6 +174,13 @@ class FakeDesktopEnvironmentFactory : public DesktopEnvironmentFactory {
desktop_session_id_ = desktop_session_id;
}

// Sets the capabilities that the FakeDesktopEnvironment will claim to
// support. Useful for testing functionality that is triggered after
// negotiating a capability with a client.
void set_capabilities(const std::string& capabilities) {
capabilities_ = capabilities;
}

// DesktopEnvironmentFactory implementation.
std::unique_ptr<DesktopEnvironment> Create(
base::WeakPtr<ClientSessionControl> client_session_control,
Expand All @@ -179,6 +196,7 @@ class FakeDesktopEnvironmentFactory : public DesktopEnvironmentFactory {
scoped_refptr<base::SingleThreadTaskRunner> capture_thread_;
protocol::FakeDesktopCapturer::FrameGenerator frame_generator_;
uint32_t desktop_session_id_ = UINT32_MAX;
std::string capabilities_;

base::WeakPtr<FakeDesktopEnvironment> last_desktop_environment_;
};
Expand Down

0 comments on commit 2623af3

Please sign in to comment.