From 49c4757170b6044fb1c8ce04486ec8aef6f886ce Mon Sep 17 00:00:00 2001 From: "garykac@chromium.org" Date: Thu, 7 Jul 2011 21:57:11 +0000 Subject: [PATCH] Chromoting: Move host input and window mgmt into DesktopEnvironment Hide details of window management and continue timers in the DesktopEnvironment class and expose OnConnect, OnLastDisconnect and OnPause methods. Move all host input monitor code into DesktopEnvironment as well. BUG=none TEST=none Review URL: http://codereview.chromium.org/7312019 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@91764 0039d316-1c4b-4281-b951-d872f2087c98 --- remoting/host/chromoting_host.cc | 105 +++------------------------ remoting/host/chromoting_host.h | 20 ------ remoting/host/chromoting_host_unittest.cc | 2 +- remoting/host/desktop_environment.cc | 115 +++++++++++++++++++++++++++++- remoting/host/desktop_environment.h | 60 ++++++++++++++-- remoting/host/simple_host_process.cc | 2 +- 6 files changed, 175 insertions(+), 129 deletions(-) diff --git a/remoting/host/chromoting_host.cc b/remoting/host/chromoting_host.cc index a81788fe4e7a..87902727b59c 100644 --- a/remoting/host/chromoting_host.cc +++ b/remoting/host/chromoting_host.cc @@ -11,16 +11,12 @@ #include "remoting/base/encoder.h" #include "remoting/base/encoder_row_based.h" #include "remoting/base/encoder_vp8.h" -#include "remoting/host/capturer.h" #include "remoting/host/chromoting_host_context.h" -#include "remoting/host/continue_window.h" #include "remoting/host/curtain.h" #include "remoting/host/desktop_environment.h" -#include "remoting/host/disconnect_window.h" #include "remoting/host/event_executor.h" #include "remoting/host/host_config.h" #include "remoting/host/host_key_pair.h" -#include "remoting/host/local_input_monitor.h" #include "remoting/host/screen_recorder.h" #include "remoting/host/user_authenticator.h" #include "remoting/jingle_glue/xmpp_signal_strategy.h" @@ -35,26 +31,14 @@ using remoting::protocol::ConnectionToClient; using remoting::protocol::InputStub; -static const int kContinueWindowTimeoutSecs = 10 * 60; - namespace remoting { // static ChromotingHost* ChromotingHost::Create(ChromotingHostContext* context, MutableHostConfig* config, AccessVerifier* access_verifier) { - Capturer* capturer = Capturer::Create(); - EventExecutor* event_executor = - EventExecutor::Create(context->desktop_message_loop(), capturer); - Curtain* curtain = Curtain::Create(); - DisconnectWindow* disconnect_window = DisconnectWindow::Create(); - ContinueWindow* continue_window = ContinueWindow::Create(); - LocalInputMonitor* local_input_monitor = LocalInputMonitor::Create(); - return Create(context, config, - new DesktopEnvironment(capturer, event_executor, curtain, - disconnect_window, continue_window, - local_input_monitor), - access_verifier); + DesktopEnvironment* desktop_env = DesktopEnvironment::Create(context); + return Create(context, config, desktop_env, access_verifier); } // static @@ -76,9 +60,9 @@ ChromotingHost::ChromotingHost(ChromotingHostContext* context, state_(kInitial), protocol_config_(protocol::CandidateSessionConfig::CreateDefault()), is_curtained_(false), - is_monitoring_local_inputs_(false), is_it2me_(false) { DCHECK(desktop_environment_.get()); + desktop_environment_->set_host(this); } ChromotingHost::~ChromotingHost() { @@ -150,8 +134,8 @@ void ChromotingHost::Shutdown(Task* shutdown_task) { recorder_->RemoveAllConnections(); } - // Stop local inputs monitor. - MonitorLocalInputs(false); + // Stop all desktop interaction. + desktop_environment_->OnLastDisconnect(); // Disconnect the clients. for (size_t i = 0; i < clients_.size(); i++) { @@ -362,7 +346,7 @@ void ChromotingHost::PauseSession(bool pause) { for (client = clients_.begin(); client != clients_.end(); ++client) { client->get()->set_awaiting_continue_approval(pause); } - StartContinueWindowTimer(!pause); + desktop_environment_->OnPause(!pause); } void ChromotingHost::OnClientDisconnected(ConnectionToClient* connection) { @@ -409,10 +393,7 @@ void ChromotingHost::OnClientDisconnected(ConnectionToClient* connection) { if (AuthenticatedClientsCount() == 0) { EnableCurtainMode(false); if (is_it2me_) { - MonitorLocalInputs(false); - ShowDisconnectWindow(false, std::string()); - ShowContinueWindow(false); - StartContinueWindowTimer(false); + desktop_environment_->OnLastDisconnect(); } } } @@ -462,17 +443,6 @@ void ChromotingHost::EnableCurtainMode(bool enable) { is_curtained_ = enable; } -void ChromotingHost::MonitorLocalInputs(bool enable) { - if (enable == is_monitoring_local_inputs_) - return; - if (enable) { - desktop_environment_->local_input_monitor()->Start(this); - } else { - desktop_environment_->local_input_monitor()->Stop(); - } - is_monitoring_local_inputs_ = enable; -} - void ChromotingHost::LocalLoginSucceeded( scoped_refptr connection) { if (MessageLoop::current() != context_->main_message_loop()) { @@ -521,13 +491,11 @@ void ChromotingHost::LocalLoginSucceeded( // including closing the connection on failure of a critical operation. EnableCurtainMode(true); if (is_it2me_) { - MonitorLocalInputs(true); std::string username = connection->session()->jid(); size_t pos = username.find('/'); if (pos != std::string::npos) username.replace(pos, std::string::npos, ""); - ShowDisconnectWindow(true, username); - StartContinueWindowTimer(true); + desktop_environment_->OnConnect(username); } // Notify observers that there is at least one authenticated client. @@ -565,63 +533,6 @@ void ChromotingHost::ProcessPreAuthentication( client->get()->OnAuthorizationComplete(true); } -void ChromotingHost::ShowDisconnectWindow(bool show, - const std::string& username) { - if (!context_->IsUIThread()) { - context_->PostToUIThread( - FROM_HERE, - NewRunnableMethod(this, &ChromotingHost::ShowDisconnectWindow, - show, username)); - return; - } - - if (show) { - desktop_environment_->disconnect_window()->Show(this, username); - } else { - desktop_environment_->disconnect_window()->Hide(); - } -} - -void ChromotingHost::ShowContinueWindow(bool show) { - if (!context_->IsUIThread()) { - context_->PostToUIThread( - FROM_HERE, - NewRunnableMethod(this, &ChromotingHost::ShowContinueWindow, show)); - return; - } - - if (show) { - desktop_environment_->continue_window()->Show(this); - } else { - desktop_environment_->continue_window()->Hide(); - } -} - -void ChromotingHost::StartContinueWindowTimer(bool start) { - if (context_->main_message_loop() != MessageLoop::current()) { - context_->main_message_loop()->PostTask( - FROM_HERE, - NewRunnableMethod(this, - &ChromotingHost::StartContinueWindowTimer, - start)); - return; - } - if (continue_window_timer_.IsRunning() == start) - return; - if (start) { - continue_window_timer_.Start( - base::TimeDelta::FromSeconds(kContinueWindowTimeoutSecs), - this, &ChromotingHost::ContinueWindowTimerFunc); - } else { - continue_window_timer_.Stop(); - } -} - -void ChromotingHost::ContinueWindowTimerFunc() { - PauseSession(true); - ShowContinueWindow(true); -} - void ChromotingHost::ShutdownNetwork() { if (MessageLoop::current() != context_->network_message_loop()) { context_->network_message_loop()->PostTask( diff --git a/remoting/host/chromoting_host.h b/remoting/host/chromoting_host.h index 0635dcc9c264..709a2acec735 100644 --- a/remoting/host/chromoting_host.h +++ b/remoting/host/chromoting_host.h @@ -9,7 +9,6 @@ #include "base/memory/scoped_ptr.h" #include "base/threading/thread.h" -#include "base/timer.h" #include "remoting/base/encoder.h" #include "remoting/host/access_verifier.h" #include "remoting/host/capturer.h" @@ -175,22 +174,9 @@ class ChromotingHost : public base::RefCountedThreadSafe, void EnableCurtainMode(bool enable); - void MonitorLocalInputs(bool enable); - void ProcessPreAuthentication( const scoped_refptr& connection); - // Show or hide the Disconnect window on the UI thread. If |show| is false, - // hide the window, ignoring the |username| parameter. - void ShowDisconnectWindow(bool show, const std::string& username); - - // Show or hide the Continue Sharing window on the UI thread. - void ShowContinueWindow(bool show); - - void StartContinueWindowTimer(bool start); - - void ContinueWindowTimerFunc(); - // The following methods are called during shutdown. void ShutdownNetwork(); void ShutdownRecorder(); @@ -231,12 +217,6 @@ class ChromotingHost : public base::RefCountedThreadSafe, scoped_ptr protocol_config_; bool is_curtained_; - bool is_monitoring_local_inputs_; - - // Timer controlling the "continue session" dialog. The timer is started when - // a connection is made or re-confirmed. On expiry, inputs to the host are - // blocked and the dialog is shown. - base::OneShotTimer continue_window_timer_; // Whether or not the host is running in "IT2Me" mode, in which connections // are pre-authenticated, and hence the local login challenge can be bypassed. diff --git a/remoting/host/chromoting_host_unittest.cc b/remoting/host/chromoting_host_unittest.cc index 29a7f7bf4ee1..be6565ac1424 100644 --- a/remoting/host/chromoting_host_unittest.cc +++ b/remoting/host/chromoting_host_unittest.cc @@ -98,7 +98,7 @@ class ChromotingHostTest : public testing::Test { continue_window_ = new MockContinueWindow(); local_input_monitor_ = new MockLocalInputMonitor(); DesktopEnvironment* desktop = - new DesktopEnvironment(capturer, event_executor_, curtain_, + new DesktopEnvironment(&context_, capturer, event_executor_, curtain_, disconnect_window_, continue_window_, local_input_monitor_); MockAccessVerifier* access_verifier = new MockAccessVerifier(); diff --git a/remoting/host/desktop_environment.cc b/remoting/host/desktop_environment.cc index 84e6d6d79270..6082acf80743 100644 --- a/remoting/host/desktop_environment.cc +++ b/remoting/host/desktop_environment.cc @@ -5,29 +5,138 @@ #include "remoting/host/desktop_environment.h" #include "remoting/host/capturer.h" +#include "remoting/host/chromoting_host.h" +#include "remoting/host/chromoting_host_context.h" #include "remoting/host/continue_window.h" #include "remoting/host/curtain.h" #include "remoting/host/disconnect_window.h" #include "remoting/host/event_executor.h" #include "remoting/host/local_input_monitor.h" +static const int kContinueWindowTimeoutSecs = 10 * 60; + namespace remoting { -DesktopEnvironment::DesktopEnvironment(Capturer* capturer, +// static +DesktopEnvironment* DesktopEnvironment::Create(ChromotingHostContext* context) { + Capturer* capturer = Capturer::Create(); + EventExecutor* event_executor = + EventExecutor::Create(context->desktop_message_loop(), capturer); + Curtain* curtain = Curtain::Create(); + DisconnectWindow* disconnect_window = DisconnectWindow::Create(); + ContinueWindow* continue_window = ContinueWindow::Create(); + LocalInputMonitor* local_input_monitor = LocalInputMonitor::Create(); + + return new DesktopEnvironment(context, capturer, event_executor, curtain, + disconnect_window, continue_window, + local_input_monitor); +} + +DesktopEnvironment::DesktopEnvironment(ChromotingHostContext* context, + Capturer* capturer, EventExecutor* event_executor, Curtain* curtain, DisconnectWindow* disconnect_window, ContinueWindow* continue_window, LocalInputMonitor* local_input_monitor) - : capturer_(capturer), + : host_(NULL), + context_(context), + capturer_(capturer), event_executor_(event_executor), curtain_(curtain), disconnect_window_(disconnect_window), continue_window_(continue_window), - local_input_monitor_(local_input_monitor) { + local_input_monitor_(local_input_monitor), + is_monitoring_local_inputs_(false) { } DesktopEnvironment::~DesktopEnvironment() { } +void DesktopEnvironment::OnConnect(const std::string& username) { + MonitorLocalInputs(true); + ShowDisconnectWindow(true, username); + StartContinueWindowTimer(true); +} + +void DesktopEnvironment::OnLastDisconnect() { + MonitorLocalInputs(false); + ShowDisconnectWindow(false, std::string()); + ShowContinueWindow(false); + StartContinueWindowTimer(false); +} + +void DesktopEnvironment::OnPause(bool pause) { + StartContinueWindowTimer(!pause); +} + +void DesktopEnvironment::MonitorLocalInputs(bool enable) { + if (enable == is_monitoring_local_inputs_) + return; + if (enable) { + local_input_monitor_->Start(host_); + } else { + local_input_monitor_->Stop(); + } + is_monitoring_local_inputs_ = enable; +} + +void DesktopEnvironment::ShowDisconnectWindow(bool show, + const std::string& username) { + if (!context_->IsUIThread()) { + context_->PostToUIThread( + FROM_HERE, + NewRunnableMethod(this, &DesktopEnvironment::ShowDisconnectWindow, + show, username)); + return; + } + + if (show) { + disconnect_window_->Show(host_, username); + } else { + disconnect_window_->Hide(); + } +} + +void DesktopEnvironment::ShowContinueWindow(bool show) { + if (!context_->IsUIThread()) { + context_->PostToUIThread( + FROM_HERE, + NewRunnableMethod(this, &DesktopEnvironment::ShowContinueWindow, show)); + return; + } + + if (show) { + continue_window_->Show(host_); + } else { + continue_window_->Hide(); + } +} + +void DesktopEnvironment::StartContinueWindowTimer(bool start) { + if (context_->main_message_loop() != MessageLoop::current()) { + context_->main_message_loop()->PostTask( + FROM_HERE, + NewRunnableMethod(this, + &DesktopEnvironment::StartContinueWindowTimer, + start)); + return; + } + if (continue_window_timer_.IsRunning() == start) + return; + if (start) { + continue_window_timer_.Start( + base::TimeDelta::FromSeconds(kContinueWindowTimeoutSecs), + this, &DesktopEnvironment::ContinueWindowTimerFunc); + } else { + continue_window_timer_.Stop(); + } +} + +void DesktopEnvironment::ContinueWindowTimerFunc() { + host_->PauseSession(true); + ShowContinueWindow(true); +} + + } // namespace remoting diff --git a/remoting/host/desktop_environment.h b/remoting/host/desktop_environment.h index b029a2a74037..0f473e32b142 100644 --- a/remoting/host/desktop_environment.h +++ b/remoting/host/desktop_environment.h @@ -5,12 +5,18 @@ #ifndef REMOTING_HOST_DESKTOP_ENVIRONMENT_H_ #define REMOTING_HOST_DESKTOP_ENVIRONMENT_H_ +#include + #include "base/basictypes.h" #include "base/memory/scoped_ptr.h" +#include "base/threading/thread.h" +#include "base/timer.h" namespace remoting { class Capturer; +class ChromotingHost; +class ChromotingHostContext; class ContinueWindow; class Curtain; class DisconnectWindow; @@ -19,23 +25,54 @@ class LocalInputMonitor; class DesktopEnvironment { public: + static DesktopEnvironment* Create(ChromotingHostContext* context); + // DesktopEnvironment takes ownership of all the objects passed the ctor. - DesktopEnvironment(Capturer* capturer, EventExecutor* event_executor, - Curtain* curtain, DisconnectWindow* disconnect_window, + DesktopEnvironment(ChromotingHostContext* context, + Capturer* capturer, + EventExecutor* event_executor, + Curtain* curtain, + DisconnectWindow* disconnect_window, ContinueWindow* continue_window, LocalInputMonitor* monitor); virtual ~DesktopEnvironment(); + void set_host(ChromotingHost* host) { host_ = host; } + Capturer* capturer() const { return capturer_.get(); } EventExecutor* event_executor() const { return event_executor_.get(); } Curtain* curtain() const { return curtain_.get(); } - DisconnectWindow* disconnect_window() { return disconnect_window_.get(); } - ContinueWindow* continue_window() { return continue_window_.get(); } - LocalInputMonitor* local_input_monitor() { - return local_input_monitor_.get(); - } + + // Called whenever a new client has connected. + void OnConnect(const std::string& username); + + // Called when the last client has disconnected. + void OnLastDisconnect(); + + // Called when the remote connection has been paused/unpaused. + void OnPause(bool pause); private: + void MonitorLocalInputs(bool enable); + + // Show or hide the Disconnect window on the UI thread. If |show| is false, + // hide the window, ignoring the |username| parameter. + void ShowDisconnectWindow(bool show, const std::string& username); + + // Show or hide the Continue Sharing window on the UI thread. + void ShowContinueWindow(bool show); + + void StartContinueWindowTimer(bool start); + + void ContinueWindowTimerFunc(); + + // The host that owns this DesktopEnvironment. + ChromotingHost* host_; + + // Host context used to make sure operations are run on the correct thread. + // This is owned by the ChromotingHost. + ChromotingHostContext* context_; + // Capturer to be used by ScreenRecorder. scoped_ptr capturer_; @@ -56,9 +93,18 @@ class DesktopEnvironment { // user is trying to do something. scoped_ptr local_input_monitor_; + bool is_monitoring_local_inputs_; + + // Timer controlling the "continue session" dialog. The timer is started when + // a connection is made or re-confirmed. On expiry, inputs to the host are + // blocked and the dialog is shown. + base::OneShotTimer continue_window_timer_; + DISALLOW_COPY_AND_ASSIGN(DesktopEnvironment); }; } // namespace remoting +DISABLE_RUNNABLE_METHOD_REFCOUNT(remoting::DesktopEnvironment); + #endif // REMOTING_HOST_DESKTOP_ENVIRONMENT_H_ diff --git a/remoting/host/simple_host_process.cc b/remoting/host/simple_host_process.cc index 0c4ff262d688..9ecb5234b368 100644 --- a/remoting/host/simple_host_process.cc +++ b/remoting/host/simple_host_process.cc @@ -190,7 +190,7 @@ class SimpleHost { remoting::LocalInputMonitor::Create(); host = ChromotingHost::Create( &context, config, - new DesktopEnvironment(capturer, event_executor, curtain, + new DesktopEnvironment(&context, capturer, event_executor, curtain, disconnect_window, continue_window, local_input_monitor), access_verifier.release()); -- 2.11.4.GIT