File: exclusive_access_manager.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (208 lines) | stat: -rw-r--r-- 7,566 bytes parent folder | download
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
// Copyright (c) 2015 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 "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"

#include "base/command_line.h"
#include "build/build_config.h"
#include "chrome/browser/app_mode/app_mode_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/exclusive_access/exclusive_access_context.h"
#include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
#include "chrome/browser/ui/exclusive_access/mouse_lock_controller.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/native_web_keyboard_event.h"
#include "ui/events/keycodes/keyboard_codes.h"

using content::WebContents;

namespace {

// Time in milliseconds to hold the Esc key in order to exit full screen.
// TODO(dominickn) refactor the way timings/input handling works so this
// constant doesn't have to be in this file.
const int kHoldEscapeTimeMs = 1500;

}

ExclusiveAccessManager::ExclusiveAccessManager(
    ExclusiveAccessContext* exclusive_access_context)
    : exclusive_access_context_(exclusive_access_context),
      fullscreen_controller_(this),
      mouse_lock_controller_(this) {
}

ExclusiveAccessManager::~ExclusiveAccessManager() {
}

ExclusiveAccessBubbleType
ExclusiveAccessManager::GetExclusiveAccessExitBubbleType() const {
  // In kiosk and exclusive app mode we always want to be fullscreen and do not
  // want to show exit instructions for browser mode fullscreen.
  bool app_mode = false;
#if !defined(OS_MACOSX)  // App mode (kiosk) is not available on Mac yet.
  app_mode = chrome::IsRunningInAppMode();
#endif

  if (fullscreen_controller_.IsWindowFullscreenForTabOrPending()) {
    if (!fullscreen_controller_.IsTabFullscreen())
      return EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_EXIT_INSTRUCTION;

    if (mouse_lock_controller_.IsMouseLockedSilently() ||
        fullscreen_controller_.IsPrivilegedFullscreenForTab()) {
      return EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE;
    }

    if (IsExperimentalKeyboardLockUIEnabled())
      return EXCLUSIVE_ACCESS_BUBBLE_TYPE_KEYBOARD_LOCK_EXIT_INSTRUCTION;

    if (mouse_lock_controller_.IsMouseLocked())
      return EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_MOUSELOCK_EXIT_INSTRUCTION;

    return EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_EXIT_INSTRUCTION;
  }

  if (mouse_lock_controller_.IsMouseLockedSilently())
    return EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE;

  if (mouse_lock_controller_.IsMouseLocked())
    return EXCLUSIVE_ACCESS_BUBBLE_TYPE_MOUSELOCK_EXIT_INSTRUCTION;

  if (fullscreen_controller_.IsExtensionFullscreenOrPending())
    return EXCLUSIVE_ACCESS_BUBBLE_TYPE_EXTENSION_FULLSCREEN_EXIT_INSTRUCTION;

  if (fullscreen_controller_.IsControllerInitiatedFullscreen() && !app_mode)
    return EXCLUSIVE_ACCESS_BUBBLE_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION;

  return EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE;
}

void ExclusiveAccessManager::UpdateExclusiveAccessExitBubbleContent() {
  GURL url = GetExclusiveAccessBubbleURL();
  ExclusiveAccessBubbleType bubble_type = GetExclusiveAccessExitBubbleType();

  exclusive_access_context_->UpdateExclusiveAccessExitBubbleContent(
      url, bubble_type);
}

GURL ExclusiveAccessManager::GetExclusiveAccessBubbleURL() const {
  GURL result = fullscreen_controller_.GetURLForExclusiveAccessBubble();
  if (!result.is_valid())
    result = mouse_lock_controller_.GetURLForExclusiveAccessBubble();
  return result;
}

// static
bool ExclusiveAccessManager::IsExperimentalKeyboardLockUIEnabled() {
  return base::FeatureList::IsEnabled(features::kExperimentalKeyboardLockUI);
}

// static
bool ExclusiveAccessManager::IsSimplifiedFullscreenUIEnabled() {
#if defined(OS_MACOSX)
  // Always enabled on Mac (the mouse cursor tracking required to implement the
  // non-simplified version is not implemented).
  return true;
#else
  return base::FeatureList::IsEnabled(features::kSimplifiedFullscreenUI);
#endif
}

void ExclusiveAccessManager::OnTabDeactivated(WebContents* web_contents) {
  fullscreen_controller_.OnTabDeactivated(web_contents);
  mouse_lock_controller_.OnTabDeactivated(web_contents);
}

void ExclusiveAccessManager::OnTabDetachedFromView(WebContents* web_contents) {
  fullscreen_controller_.OnTabDetachedFromView(web_contents);
  mouse_lock_controller_.OnTabDetachedFromView(web_contents);
}

void ExclusiveAccessManager::OnTabClosing(WebContents* web_contents) {
  fullscreen_controller_.OnTabClosing(web_contents);
  mouse_lock_controller_.OnTabClosing(web_contents);
}

bool ExclusiveAccessManager::HandleUserKeyPress(
    const content::NativeWebKeyboardEvent& event) {
  if (event.windowsKeyCode != ui::VKEY_ESCAPE) {
    OnUserInput();
    return false;
  }

  if (IsExperimentalKeyboardLockUIEnabled()) {
    if (event.type() == content::NativeWebKeyboardEvent::KeyUp &&
        hold_timer_.IsRunning()) {
      // Seeing a key up event on Esc with the hold timer running cancels the
      // timer and doesn't exit. This means the user pressed Esc, but not long
      // enough to trigger an exit
      hold_timer_.Stop();
    } else if (event.type() == content::NativeWebKeyboardEvent::RawKeyDown &&
               !hold_timer_.IsRunning()) {
      // Seeing a key down event on Esc when the hold timer is stopped starts
      // the timer. When the timer reaches 0, the callback will trigger an exit
      // from fullscreen/mouselock.
      hold_timer_.Start(
          FROM_HERE, base::TimeDelta::FromMilliseconds(kHoldEscapeTimeMs),
          base::Bind(&ExclusiveAccessManager::HandleUserHeldEscape,
                     base::Unretained(this)));
    }
    // We never handle the keyboard event.
    return false;
  }

  bool handled = false;
  handled = fullscreen_controller_.HandleUserPressedEscape();
  handled |= mouse_lock_controller_.HandleUserPressedEscape();
  return handled;
}

void ExclusiveAccessManager::OnUserInput() {
  exclusive_access_context_->OnExclusiveAccessUserInput();
}

void ExclusiveAccessManager::ExitExclusiveAccess() {
  fullscreen_controller_.ExitExclusiveAccessToPreviousState();
  mouse_lock_controller_.LostMouseLock();
}

void ExclusiveAccessManager::RecordBubbleReshownUMA(
    ExclusiveAccessBubbleType type) {
  // Figure out whether each of fullscreen, mouselock is in effect.
  bool fullscreen = false;
  bool mouselock = false;
  switch (type) {
    case EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE:
      // None in effect.
      break;
    case EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_EXIT_INSTRUCTION:
    case EXCLUSIVE_ACCESS_BUBBLE_TYPE_KEYBOARD_LOCK_EXIT_INSTRUCTION:
    case EXCLUSIVE_ACCESS_BUBBLE_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION:
    case EXCLUSIVE_ACCESS_BUBBLE_TYPE_EXTENSION_FULLSCREEN_EXIT_INSTRUCTION:
      // Only fullscreen in effect.
      fullscreen = true;
      break;
    case EXCLUSIVE_ACCESS_BUBBLE_TYPE_MOUSELOCK_EXIT_INSTRUCTION:
      // Only mouselock in effect.
      mouselock = true;
      break;
    case EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_MOUSELOCK_EXIT_INSTRUCTION:
      // Both in effect.
      fullscreen = true;
      mouselock = true;
      break;
  }

  if (fullscreen)
    fullscreen_controller_.RecordBubbleReshownUMA();
  if (mouselock)
    mouse_lock_controller_.RecordBubbleReshownUMA();
}

void ExclusiveAccessManager::HandleUserHeldEscape() {
  fullscreen_controller_.HandleUserPressedEscape();
  mouse_lock_controller_.HandleUserPressedEscape();
}