OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/host/host_user_interface.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "remoting/host/chromoting_host.h" |
| 9 #include "remoting/host/chromoting_host_context.h" |
| 10 #include "remoting/host/disconnect_window.h" |
| 11 #include "remoting/host/local_input_monitor.h" |
| 12 |
| 13 namespace remoting { |
| 14 |
| 15 HostUserInterface::HostUserInterface(ChromotingHostContext* context) |
| 16 : host_(NULL), |
| 17 context_(context), |
| 18 is_monitoring_local_inputs_(false), |
| 19 ui_thread_proxy_(context->ui_message_loop()) { |
| 20 } |
| 21 |
| 22 HostUserInterface::~HostUserInterface() { |
| 23 DCHECK(ui_message_loop()->BelongsToCurrentThread()); |
| 24 |
| 25 MonitorLocalInputs(false); |
| 26 ShowDisconnectWindow(false, std::string()); |
| 27 |
| 28 ui_thread_proxy_.Detach(); |
| 29 } |
| 30 |
| 31 void HostUserInterface::Start(ChromotingHost* host, |
| 32 const base::Closure& disconnect_callback) { |
| 33 DCHECK(network_message_loop()->BelongsToCurrentThread()); |
| 34 DCHECK(host_ == NULL); |
| 35 DCHECK(disconnect_callback_.is_null()); |
| 36 |
| 37 host_ = host; |
| 38 disconnect_callback_ = disconnect_callback; |
| 39 disconnect_window_ = DisconnectWindow::Create(); |
| 40 local_input_monitor_ = LocalInputMonitor::Create(); |
| 41 host_->AddStatusObserver(this); |
| 42 } |
| 43 |
| 44 void HostUserInterface::OnClientAuthenticated(const std::string& jid) { |
| 45 authenticated_jid_ = jid; |
| 46 |
| 47 std::string username = jid.substr(0, jid.find('/')); |
| 48 ui_thread_proxy_.PostTask(FROM_HERE, base::Bind( |
| 49 &HostUserInterface::ProcessOnClientAuthenticated, |
| 50 base::Unretained(this), username)); |
| 51 } |
| 52 |
| 53 void HostUserInterface::OnClientDisconnected(const std::string& jid) { |
| 54 if (jid == authenticated_jid_) { |
| 55 ui_thread_proxy_.PostTask(FROM_HERE, base::Bind( |
| 56 &HostUserInterface::ProcessOnClientDisconnected, |
| 57 base::Unretained(this))); |
| 58 } |
| 59 } |
| 60 |
| 61 void HostUserInterface::OnAccessDenied(const std::string& jid) { |
| 62 } |
| 63 |
| 64 void HostUserInterface::OnShutdown() { |
| 65 // Host status observers must be removed on the network thread, so |
| 66 // it must happen here instead of in the destructor. |
| 67 host_->RemoveStatusObserver(this); |
| 68 host_ = NULL; |
| 69 disconnect_callback_ = base::Closure(); |
| 70 } |
| 71 |
| 72 void HostUserInterface::OnDisconnectCallback() { |
| 73 DCHECK(ui_message_loop()->BelongsToCurrentThread()); |
| 74 DCHECK(!disconnect_callback_.is_null()); |
| 75 |
| 76 MonitorLocalInputs(false); |
| 77 ShowDisconnectWindow(false, std::string()); |
| 78 disconnect_callback_.Run(); |
| 79 } |
| 80 |
| 81 base::MessageLoopProxy* HostUserInterface::network_message_loop() const { |
| 82 return context_->network_message_loop(); |
| 83 } |
| 84 base::MessageLoopProxy* HostUserInterface::ui_message_loop() const { |
| 85 return context_->ui_message_loop(); |
| 86 } |
| 87 |
| 88 void HostUserInterface::DisconnectSession() const { |
| 89 return disconnect_callback_.Run(); |
| 90 } |
| 91 |
| 92 void HostUserInterface::ProcessOnClientAuthenticated( |
| 93 const std::string& username) { |
| 94 DCHECK(ui_message_loop()->BelongsToCurrentThread()); |
| 95 |
| 96 MonitorLocalInputs(true); |
| 97 ShowDisconnectWindow(true, username); |
| 98 } |
| 99 |
| 100 void HostUserInterface::ProcessOnClientDisconnected() { |
| 101 DCHECK(ui_message_loop()->BelongsToCurrentThread()); |
| 102 |
| 103 MonitorLocalInputs(false); |
| 104 ShowDisconnectWindow(false, std::string()); |
| 105 } |
| 106 |
| 107 void HostUserInterface::StartForTest( |
| 108 ChromotingHost* host, |
| 109 const base::Closure& disconnect_callback, |
| 110 scoped_ptr<DisconnectWindow> disconnect_window, |
| 111 scoped_ptr<LocalInputMonitor> local_input_monitor) { |
| 112 DCHECK(network_message_loop()->BelongsToCurrentThread()); |
| 113 DCHECK(host_ == NULL); |
| 114 DCHECK(disconnect_callback_.is_null()); |
| 115 |
| 116 host_ = host; |
| 117 disconnect_callback_ = disconnect_callback; |
| 118 disconnect_window_ = disconnect_window.Pass(); |
| 119 local_input_monitor_ = local_input_monitor.Pass(); |
| 120 } |
| 121 |
| 122 void HostUserInterface::MonitorLocalInputs(bool enable) { |
| 123 DCHECK(ui_message_loop()->BelongsToCurrentThread()); |
| 124 |
| 125 if (enable != is_monitoring_local_inputs_) { |
| 126 if (enable) { |
| 127 local_input_monitor_->Start(host_); |
| 128 } else { |
| 129 local_input_monitor_->Stop(); |
| 130 } |
| 131 is_monitoring_local_inputs_ = enable; |
| 132 } |
| 133 } |
| 134 |
| 135 void HostUserInterface::ShowDisconnectWindow(bool show, |
| 136 const std::string& username) { |
| 137 DCHECK(ui_message_loop()->BelongsToCurrentThread()); |
| 138 |
| 139 if (show) { |
| 140 disconnect_window_->Show( |
| 141 host_, |
| 142 base::Bind(&HostUserInterface::OnDisconnectCallback, |
| 143 base::Unretained(this)), |
| 144 username); |
| 145 } else { |
| 146 disconnect_window_->Hide(); |
| 147 } |
| 148 } |
| 149 |
| 150 } // namespace remoting |
OLD | NEW |