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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
// 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/host/basic_desktop_environment.h"
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/notreached.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.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/desktop_environment_options.h"
#include "remoting/host/base/screen_controls.h"
#include "remoting/host/client_session_control.h"
#include "remoting/host/desktop_capturer_proxy.h"
#include "remoting/host/desktop_capturer_wrapper.h"
#include "remoting/host/desktop_display_info_monitor.h"
#include "remoting/host/desktop_interaction_strategy.h"
#include "remoting/host/file_transfer/file_operations.h"
#include "remoting/host/file_transfer/local_file_operations.h"
#include "remoting/host/input_injector.h"
#include "remoting/host/keyboard_layout_monitor.h"
#include "remoting/host/mouse_cursor_monitor_proxy.h"
#include "remoting/host/remote_open_url/url_forwarder_configurator.h"
#include "remoting/host/resizing_host_observer.h"
#include "remoting/host/webauthn/remote_webauthn_extension_notifier.h"
#include "remoting/host/webauthn/remote_webauthn_state_change_notifier.h"
#include "remoting/protocol/desktop_capturer.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_capture_options.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_capture_types.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
#include "third_party/webrtc/modules/desktop_capture/mouse_cursor_monitor.h"
#if BUILDFLAG(IS_WIN)
#include "remoting/host/win/evaluate_d3d.h"
#endif
#if defined(REMOTING_USE_X11)
#include "base/threading/watchdog.h"
#endif
namespace remoting {
#if defined(REMOTING_USE_X11)
namespace {
// The maximum amount of time we will wait for the IgnoreXServerGrabs() to
// return before we crash the host.
constexpr base::TimeDelta kWaitForIgnoreXServerGrabsTimeout = base::Seconds(30);
// Helper class to monitor the call to
// webrtc::SharedXDisplay::IgnoreXServerGrabs() (on a temporary thread), which
// has been observed to occasionally hang forever and zombify the host.
// This class crashes the host if the IgnoreXServerGrabs() call takes too long,
// so that the ME2ME daemon process can respawn the host.
// See: crbug.com/1130090
class IgnoreXServerGrabsWatchdog : public base::Watchdog::Delegate {
public:
IgnoreXServerGrabsWatchdog()
: watchdog_(kWaitForIgnoreXServerGrabsTimeout,
"IgnoreXServerGrabs Watchdog",
/*enabled=*/true,
this) {}
~IgnoreXServerGrabsWatchdog() override = default;
void Arm() { watchdog_.Arm(); }
void Alarm() override {
// Crash the host if IgnoreXServerGrabs() takes too long.
NOTREACHED() << "IgnoreXServerGrabs() timed out.";
}
private:
base::Watchdog watchdog_;
};
} // namespace
#endif // defined(REMOTING_USE_X11)
BasicDesktopEnvironment::~BasicDesktopEnvironment() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
}
std::unique_ptr<ActionExecutor>
BasicDesktopEnvironment::CreateActionExecutor() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
// Connection mode derivations (It2Me/Me2Me) should override this method and
// return an executor instance if applicable.
return nullptr;
}
std::unique_ptr<AudioCapturer> BasicDesktopEnvironment::CreateAudioCapturer() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
return interaction_strategy_->CreateAudioCapturer();
}
std::unique_ptr<InputInjector> BasicDesktopEnvironment::CreateInputInjector() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
return interaction_strategy_->CreateInputInjector();
}
std::unique_ptr<ScreenControls>
BasicDesktopEnvironment::CreateScreenControls() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
return nullptr;
}
DesktopDisplayInfoMonitor* BasicDesktopEnvironment::GetDisplayInfoMonitor() {
if (!display_info_monitor_) {
using VideoLayoutCallback =
base::RepeatingCallback<void(std::unique_ptr<protocol::VideoLayout>)>;
VideoLayoutCallback video_layout_callback =
base::BindRepeating(&ClientSessionControl::OnDesktopDisplayChanged,
client_session_control_);
// |video_layout_callback| is bound to |client_session_control_| which is a
// WeakPtr, but it accepts a VideoLayout proto as the parameter. DDIM needs
// a callback that accepts a DesktopDisplayInfo& instead.
auto converting_callback =
base::BindRepeating([](const DesktopDisplayInfo& info) {
return info.GetVideoLayoutProto();
});
DesktopDisplayInfoMonitor::Callback callback =
std::move(converting_callback).Then(std::move(video_layout_callback));
display_info_monitor_ = interaction_strategy_->CreateDisplayInfoMonitor();
display_info_monitor_->AddCallback(std::move(callback));
}
return display_info_monitor_.get();
}
std::unique_ptr<webrtc::MouseCursorMonitor>
BasicDesktopEnvironment::CreateMouseCursorMonitor() {
return interaction_strategy_->CreateMouseCursorMonitor();
}
std::unique_ptr<KeyboardLayoutMonitor>
BasicDesktopEnvironment::CreateKeyboardLayoutMonitor(
base::RepeatingCallback<void(const protocol::KeyboardLayout&)> callback) {
return interaction_strategy_->CreateKeyboardLayoutMonitor(
std::move(callback));
}
std::unique_ptr<ActiveDisplayMonitor>
BasicDesktopEnvironment::CreateActiveDisplayMonitor(
ActiveDisplayMonitor::Callback callback) {
return interaction_strategy_->CreateActiveDisplayMonitor(std::move(callback));
}
std::unique_ptr<FileOperations>
BasicDesktopEnvironment::CreateFileOperations() {
return std::make_unique<LocalFileOperations>(ui_task_runner_);
}
std::unique_ptr<UrlForwarderConfigurator>
BasicDesktopEnvironment::CreateUrlForwarderConfigurator() {
return UrlForwarderConfigurator::Create();
}
std::string BasicDesktopEnvironment::GetCapabilities() const {
return std::string();
}
void BasicDesktopEnvironment::SetCapabilities(const std::string& capabilities) {
}
std::uint32_t BasicDesktopEnvironment::GetDesktopSessionId() const {
return UINT32_MAX;
}
std::unique_ptr<RemoteWebAuthnStateChangeNotifier>
BasicDesktopEnvironment::CreateRemoteWebAuthnStateChangeNotifier() {
return std::make_unique<RemoteWebAuthnExtensionNotifier>();
}
std::unique_ptr<DesktopCapturer> BasicDesktopEnvironment::CreateVideoCapturer(
webrtc::ScreenId id) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
return interaction_strategy_->CreateVideoCapturer(id);
}
BasicDesktopEnvironment::BasicDesktopEnvironment(
scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
std::unique_ptr<DesktopInteractionStrategy> interaction_strategy,
base::WeakPtr<ClientSessionControl> client_session_control,
const DesktopEnvironmentOptions& options)
: caller_task_runner_(caller_task_runner),
ui_task_runner_(ui_task_runner),
interaction_strategy_(std::move(interaction_strategy)),
client_session_control_(client_session_control),
options_(options) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
#if defined(REMOTING_USE_X11)
// TODO(yuweih): The watchdog is just to test the hypothesis.
// The IgnoreXServerGrabs() call should probably be moved to whichever
// thread that created desktop_capture_options().x_display().
IgnoreXServerGrabsWatchdog watchdog;
watchdog.Arm();
desktop_capture_options().x_display()->IgnoreXServerGrabs();
#elif BUILDFLAG(IS_WIN)
// Check whether D3D is available as long as the DirectX capturer wasn't
// explicitly disabled. This check is necessary because the network process
// runs in Session 0 and cannot check whether D3D is available or not so the
// default value is set to true but can be overridden by the client.
if (options_.desktop_capture_options()->allow_directx_capturer()) {
options_.desktop_capture_options()->set_allow_directx_capturer(
IsD3DAvailable());
}
#endif
}
BasicDesktopEnvironmentFactory::BasicDesktopEnvironmentFactory(
scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
std::unique_ptr<DesktopInteractionStrategyFactory>
interaction_strategy_factory)
: caller_task_runner_(std::move(caller_task_runner)),
ui_task_runner_(std::move(ui_task_runner)),
interaction_strategy_factory_(std::move(interaction_strategy_factory)) {}
BasicDesktopEnvironmentFactory::~BasicDesktopEnvironmentFactory() = default;
bool BasicDesktopEnvironmentFactory::SupportsAudioCapture() const {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
return AudioCapturer::IsSupported();
}
void BasicDesktopEnvironmentFactory::CreateInteractionStrategy(
const DesktopEnvironmentOptions& options,
DesktopInteractionStrategyFactory::CreateCallback callback) {
interaction_strategy_factory_->Create(options, std::move(callback));
}
} // namespace remoting
|