summaryrefslogtreecommitdiffstats
path: root/remoting/host/it2me_host_user_interface.cc
blob: e5cf54eb1b9c87fe92c688c145731f5f93823507 (plain)
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
// Copyright (c) 2012 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.

#include "remoting/host/it2me_host_user_interface.h"

#include "base/bind.h"
#include "remoting/host/chromoting_host.h"
#include "remoting/host/chromoting_host_context.h"
#include "remoting/host/continue_window.h"
#include "remoting/host/disconnect_window.h"
#include "remoting/host/local_input_monitor.h"

namespace {

// Milliseconds before the continue window is shown.
static const int kContinueWindowShowTimeoutMs = 10 * 60 * 1000;

// Milliseconds before the continue window is automatically dismissed and
// the connection is closed.
static const int kContinueWindowHideTimeoutMs = 60 * 1000;

}  // namespace

namespace remoting {

It2MeHostUserInterface::It2MeHostUserInterface(ChromotingHostContext* context)
    : HostUserInterface(context),
      ALLOW_THIS_IN_INITIALIZER_LIST(timer_weak_factory_(this)) {
}

It2MeHostUserInterface::~It2MeHostUserInterface() {
  DCHECK(ui_message_loop()->BelongsToCurrentThread());

  ShowContinueWindow(false);
}

void It2MeHostUserInterface::Start(ChromotingHost* host,
                                   const base::Closure& disconnect_callback) {
  DCHECK(network_message_loop()->BelongsToCurrentThread());

  HostUserInterface::Start(host, disconnect_callback);
  continue_window_ = ContinueWindow::Create();
}

void It2MeHostUserInterface::OnClientAuthenticated(const std::string& jid) {
  if (!get_authenticated_jid().empty()) {
    // If we already authenticated another client then one of the
    // connections may be an attacker, so both are suspect and we have
    // to reject the second connection and shutdown the host.
    get_host()->RejectAuthenticatingClient();
    network_message_loop()->PostTask(FROM_HERE, base::Bind(
        &ChromotingHost::Shutdown, get_host(), base::Closure()));
  } else {
    HostUserInterface::OnClientAuthenticated(jid);
  }
}

void It2MeHostUserInterface::ProcessOnClientAuthenticated(
    const std::string& username) {
  DCHECK(ui_message_loop()->BelongsToCurrentThread());

  HostUserInterface::ProcessOnClientAuthenticated(username);
  StartContinueWindowTimer(true);
}

void It2MeHostUserInterface::ProcessOnClientDisconnected() {
  DCHECK(ui_message_loop()->BelongsToCurrentThread());

  HostUserInterface::ProcessOnClientDisconnected();
  ShowContinueWindow(false);
  StartContinueWindowTimer(false);
}

void It2MeHostUserInterface::StartForTest(
    ChromotingHost* host,
    const base::Closure& disconnect_callback,
    scoped_ptr<DisconnectWindow> disconnect_window,
    scoped_ptr<ContinueWindow> continue_window,
    scoped_ptr<LocalInputMonitor> local_input_monitor) {
  HostUserInterface::StartForTest(host, disconnect_callback,
                                  disconnect_window.Pass(),
                                  local_input_monitor.Pass());
  continue_window_ = continue_window.Pass();
}

void It2MeHostUserInterface::ContinueSession(bool continue_session) {
  DCHECK(ui_message_loop()->BelongsToCurrentThread());

  if (continue_session) {
    get_host()->PauseSession(false);
    StartContinueWindowTimer(true);
  } else {
    DisconnectSession();
  }
}

void It2MeHostUserInterface::OnContinueWindowTimer() {
  DCHECK(ui_message_loop()->BelongsToCurrentThread());

  get_host()->PauseSession(true);
  ShowContinueWindow(true);

  // Cancel any pending timer and post one to hide the continue window.
  timer_weak_factory_.InvalidateWeakPtrs();
  ui_message_loop()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&It2MeHostUserInterface::OnShutdownHostTimer,
                 timer_weak_factory_.GetWeakPtr()),
      base::TimeDelta::FromMilliseconds(kContinueWindowHideTimeoutMs));
}

void It2MeHostUserInterface::OnShutdownHostTimer() {
  DCHECK(ui_message_loop()->BelongsToCurrentThread());

  ShowContinueWindow(false);
  DisconnectSession();
}

void It2MeHostUserInterface::ShowContinueWindow(bool show) {
  DCHECK(ui_message_loop()->BelongsToCurrentThread());

  if (show) {
    continue_window_->Show(get_host(), base::Bind(
        &It2MeHostUserInterface::ContinueSession, base::Unretained(this)));
  } else {
    continue_window_->Hide();
  }
}

void It2MeHostUserInterface::StartContinueWindowTimer(bool start) {
  DCHECK(ui_message_loop()->BelongsToCurrentThread());

  // Abandon previous timer events by invalidating their weak pointer to us.
  timer_weak_factory_.InvalidateWeakPtrs();
  if (start) {
    ui_message_loop()->PostDelayedTask(
        FROM_HERE,
        base::Bind(&It2MeHostUserInterface::OnContinueWindowTimer,
                   timer_weak_factory_.GetWeakPtr()),
        base::TimeDelta::FromMilliseconds(kContinueWindowShowTimeoutMs));
  }
}

}  // namespace remoting