Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / ui / exclusive_access / fullscreen_controller.cc
blob8f0f7fc7c516fc87e68f13b2a119d8f80e7d7e80
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.
5 #include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/app_mode/app_mode_utils.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/exclusive_access/exclusive_access_context.h"
14 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
15 #include "chrome/browser/ui/exclusive_access/fullscreen_within_tab_helper.h"
16 #include "chrome/browser/ui/status_bubble.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/browser/ui/web_contents_sizer.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "components/content_settings/core/browser/host_content_settings_map.h"
21 #include "content/public/browser/navigation_details.h"
22 #include "content/public/browser/navigation_entry.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/browser/render_view_host.h"
25 #include "content/public/browser/render_widget_host_view.h"
26 #include "content/public/browser/user_metrics.h"
27 #include "content/public/browser/web_contents.h"
28 #include "extensions/common/extension.h"
30 #if !defined(OS_MACOSX)
31 #include "base/prefs/pref_service.h"
32 #include "chrome/common/pref_names.h"
33 #endif
35 using base::UserMetricsAction;
36 using content::RenderViewHost;
37 using content::WebContents;
39 FullscreenController::FullscreenController(ExclusiveAccessManager* manager)
40 : ExclusiveAccessControllerBase(manager),
41 state_prior_to_tab_fullscreen_(STATE_INVALID),
42 tab_fullscreen_accepted_(false),
43 toggled_into_fullscreen_(false),
44 reentrant_window_state_change_call_check_(false),
45 is_privileged_fullscreen_for_testing_(false),
46 ptr_factory_(this) {
49 FullscreenController::~FullscreenController() {
52 bool FullscreenController::IsFullscreenForBrowser() const {
53 return exclusive_access_manager()->context()->IsFullscreen() &&
54 !IsFullscreenCausedByTab();
57 void FullscreenController::ToggleBrowserFullscreenMode() {
58 extension_caused_fullscreen_ = GURL();
59 ToggleFullscreenModeInternal(BROWSER);
62 void FullscreenController::ToggleBrowserFullscreenWithToolbar() {
63 ToggleFullscreenModeInternal(BROWSER_WITH_TOOLBAR);
66 void FullscreenController::ToggleBrowserFullscreenModeWithExtension(
67 const GURL& extension_url) {
68 // |extension_caused_fullscreen_| will be reset if this causes fullscreen to
69 // exit.
70 extension_caused_fullscreen_ = extension_url;
71 ToggleFullscreenModeInternal(BROWSER);
74 bool FullscreenController::IsWindowFullscreenForTabOrPending() const {
75 return exclusive_access_tab() != nullptr;
78 bool FullscreenController::IsExtensionFullscreenOrPending() const {
79 return !extension_caused_fullscreen_.is_empty();
82 bool FullscreenController::IsControllerInitiatedFullscreen() const {
83 return toggled_into_fullscreen_;
86 bool FullscreenController::IsUserAcceptedFullscreen() const {
87 return tab_fullscreen_accepted_;
90 bool FullscreenController::IsFullscreenForTabOrPending(
91 const WebContents* web_contents) const {
92 if (web_contents == exclusive_access_tab()) {
93 DCHECK(web_contents ==
94 exclusive_access_manager()->context()->GetActiveWebContents());
95 DCHECK(web_contents->GetCapturerCount() == 0);
96 return true;
98 return IsFullscreenForCapturedTab(web_contents);
101 bool FullscreenController::IsFullscreenCausedByTab() const {
102 return state_prior_to_tab_fullscreen_ == STATE_NORMAL;
105 void FullscreenController::EnterFullscreenModeForTab(WebContents* web_contents,
106 const GURL& origin) {
107 DCHECK(web_contents);
109 if (MaybeToggleFullscreenForCapturedTab(web_contents, true)) {
110 // During tab capture of fullscreen-within-tab views, the browser window
111 // fullscreen state is unchanged, so return now.
112 return;
115 if (web_contents !=
116 exclusive_access_manager()->context()->GetActiveWebContents() ||
117 IsWindowFullscreenForTabOrPending()) {
118 return;
121 #if defined(OS_WIN)
122 // For now, avoid breaking when initiating full screen tab mode while in
123 // a metro snap.
124 // TODO(robertshield): Find a way to reconcile tab-initiated fullscreen
125 // modes with metro snap.
126 if (IsInMetroSnapMode())
127 return;
128 #endif
130 SetTabWithExclusiveAccess(web_contents);
131 fullscreened_origin_ = origin;
133 ExclusiveAccessContext* exclusive_access_context =
134 exclusive_access_manager()->context();
136 if (!exclusive_access_context->IsFullscreen()) {
137 // Normal -> Tab Fullscreen.
138 state_prior_to_tab_fullscreen_ = STATE_NORMAL;
139 ToggleFullscreenModeInternal(TAB);
140 return;
143 if (exclusive_access_context->IsFullscreenWithToolbar()) {
144 // Browser Fullscreen with Toolbar -> Tab Fullscreen (no toolbar).
145 exclusive_access_context->UpdateFullscreenWithToolbar(false);
146 state_prior_to_tab_fullscreen_ = STATE_BROWSER_FULLSCREEN_WITH_TOOLBAR;
147 } else {
148 // Browser Fullscreen without Toolbar -> Tab Fullscreen.
149 state_prior_to_tab_fullscreen_ = STATE_BROWSER_FULLSCREEN_NO_TOOLBAR;
152 // We need to update the fullscreen exit bubble, e.g., going from browser
153 // fullscreen to tab fullscreen will need to show different content.
154 if (!tab_fullscreen_accepted_) {
155 tab_fullscreen_accepted_ = GetFullscreenSetting() == CONTENT_SETTING_ALLOW;
157 exclusive_access_manager()->UpdateExclusiveAccessExitBubbleContent();
159 // This is only a change between Browser and Tab fullscreen. We generate
160 // a fullscreen notification now because there is no window change.
161 PostFullscreenChangeNotification(true);
164 void FullscreenController::ExitFullscreenModeForTab(WebContents* web_contents) {
165 if (MaybeToggleFullscreenForCapturedTab(web_contents, false)) {
166 // During tab capture of fullscreen-within-tab views, the browser window
167 // fullscreen state is unchanged, so return now.
168 return;
171 if (!IsWindowFullscreenForTabOrPending() ||
172 web_contents != exclusive_access_tab()) {
173 return;
176 #if defined(OS_WIN)
177 // For now, avoid breaking when initiating full screen tab mode while in
178 // a metro snap.
179 // TODO(robertshield): Find a way to reconcile tab-initiated fullscreen
180 // modes with metro snap.
181 if (IsInMetroSnapMode())
182 return;
183 #endif
185 ExclusiveAccessContext* exclusive_access_context =
186 exclusive_access_manager()->context();
188 if (!exclusive_access_context->IsFullscreen())
189 return;
191 if (IsFullscreenCausedByTab()) {
192 // Tab Fullscreen -> Normal.
193 ToggleFullscreenModeInternal(TAB);
194 return;
197 // Tab Fullscreen -> Browser Fullscreen (with or without toolbar).
198 if (state_prior_to_tab_fullscreen_ == STATE_BROWSER_FULLSCREEN_WITH_TOOLBAR) {
199 // Tab Fullscreen (no toolbar) -> Browser Fullscreen with Toolbar.
200 exclusive_access_context->UpdateFullscreenWithToolbar(true);
203 #if defined(OS_MACOSX)
204 // Clear the bubble URL, which forces the Mac UI to redraw.
205 exclusive_access_manager()->UpdateExclusiveAccessExitBubbleContent();
206 #endif // defined(OS_MACOSX)
208 // If currently there is a tab in "tab fullscreen" mode and fullscreen
209 // was not caused by it (i.e., previously it was in "browser fullscreen"
210 // mode), we need to switch back to "browser fullscreen" mode. In this
211 // case, all we have to do is notifying the tab that it has exited "tab
212 // fullscreen" mode.
213 NotifyTabExclusiveAccessLost();
215 // This is only a change between Browser and Tab fullscreen. We generate
216 // a fullscreen notification now because there is no window change.
217 PostFullscreenChangeNotification(true);
220 #if defined(OS_WIN)
221 bool FullscreenController::IsInMetroSnapMode() {
222 return exclusive_access_manager()->context()->IsInMetroSnapMode();
225 void FullscreenController::SetMetroSnapMode(bool enable) {
226 reentrant_window_state_change_call_check_ = false;
228 toggled_into_fullscreen_ = false;
229 exclusive_access_manager()->context()->SetMetroSnapMode(enable);
231 // FullscreenController unit tests for metro snap assume that on Windows calls
232 // to WindowFullscreenStateChanged are reentrant. If that assumption is
233 // invalidated, the tests must be updated to maintain coverage.
234 CHECK(reentrant_window_state_change_call_check_);
236 #endif // defined(OS_WIN)
238 void FullscreenController::OnTabDetachedFromView(WebContents* old_contents) {
239 if (!IsFullscreenForCapturedTab(old_contents))
240 return;
242 // A fullscreen-within-tab view undergoing screen capture has been detached
243 // and is no longer visible to the user. Set it to exactly the WebContents'
244 // preferred size. See 'FullscreenWithinTab Note'.
246 // When the user later selects the tab to show |old_contents| again, UI code
247 // elsewhere (e.g., views::WebView) will resize the view to fit within the
248 // browser window once again.
250 // If the view has been detached from the browser window (e.g., to drag a tab
251 // off into a new browser window), return immediately to avoid an unnecessary
252 // resize.
253 if (!old_contents->GetDelegate())
254 return;
256 // Do nothing if tab capture ended after toggling fullscreen, or a preferred
257 // size was never specified by the capturer.
258 if (old_contents->GetCapturerCount() == 0 ||
259 old_contents->GetPreferredSize().IsEmpty()) {
260 return;
263 content::RenderWidgetHostView* const current_fs_view =
264 old_contents->GetFullscreenRenderWidgetHostView();
265 if (current_fs_view)
266 current_fs_view->SetSize(old_contents->GetPreferredSize());
267 ResizeWebContents(old_contents, old_contents->GetPreferredSize());
270 void FullscreenController::OnTabClosing(WebContents* web_contents) {
271 if (IsFullscreenForCapturedTab(web_contents)) {
272 web_contents->ExitFullscreen();
273 } else {
274 ExclusiveAccessControllerBase::OnTabClosing(web_contents);
278 void FullscreenController::WindowFullscreenStateChanged() {
279 reentrant_window_state_change_call_check_ = true;
280 ExclusiveAccessContext* const exclusive_access_context =
281 exclusive_access_manager()->context();
282 bool exiting_fullscreen = !exclusive_access_context->IsFullscreen();
284 PostFullscreenChangeNotification(!exiting_fullscreen);
285 if (exiting_fullscreen) {
286 toggled_into_fullscreen_ = false;
287 extension_caused_fullscreen_ = GURL();
288 NotifyTabExclusiveAccessLost();
289 exclusive_access_context->UnhideDownloadShelf();
290 } else {
291 exclusive_access_context->HideDownloadShelf();
295 bool FullscreenController::HandleUserPressedEscape() {
296 WebContents* const active_web_contents =
297 exclusive_access_manager()->context()->GetActiveWebContents();
298 if (IsFullscreenForCapturedTab(active_web_contents)) {
299 active_web_contents->ExitFullscreen();
300 return true;
301 } else if (IsWindowFullscreenForTabOrPending()) {
302 ExitExclusiveAccessIfNecessary();
303 return true;
306 return false;
309 void FullscreenController::ExitExclusiveAccessToPreviousState() {
310 if (IsWindowFullscreenForTabOrPending())
311 ExitFullscreenModeForTab(exclusive_access_tab());
312 else if (IsFullscreenForBrowser())
313 ExitFullscreenModeInternal();
316 bool FullscreenController::OnAcceptExclusiveAccessPermission() {
317 ExclusiveAccessBubbleType bubble_type =
318 exclusive_access_manager()->GetExclusiveAccessExitBubbleType();
319 bool fullscreen = false;
320 exclusive_access_bubble::PermissionRequestedByType(bubble_type, &fullscreen,
321 nullptr);
322 DCHECK(!(fullscreen && tab_fullscreen_accepted_));
324 if (fullscreen && !tab_fullscreen_accepted_) {
325 DCHECK(exclusive_access_tab());
326 // Origins can enter fullscreen even when embedded in other origins.
327 // Permission is tracked based on the combinations of requester and
328 // embedder. Thus, even if a requesting origin has been previously approved
329 // for embedder A, it will not be approved when embedded in a different
330 // origin B.
332 // However, an exception is made when a requester and an embedder are the
333 // same origin. In other words, if the requester is the top-level frame. If
334 // that combination is ALLOWED, then future requests from that origin will
335 // succeed no matter what the embedder is. For example, if youtube.com
336 // is visited and user selects ALLOW. Later user visits example.com which
337 // embeds youtube.com in an iframe, which is then ALLOWED to go fullscreen.
338 GURL requester = GetRequestingOrigin();
339 GURL embedder = GetEmbeddingOrigin();
340 ContentSettingsPattern primary_pattern =
341 ContentSettingsPattern::FromURLNoWildcard(requester);
342 ContentSettingsPattern secondary_pattern =
343 ContentSettingsPattern::FromURLNoWildcard(embedder);
345 // ContentSettings requires valid patterns and the patterns might be invalid
346 // in some edge cases like if the current frame is about:blank.
348 // Do not store preference on file:// URLs, they don't have a clean
349 // origin policy.
350 // TODO(estark): Revisit this when crbug.com/455882 is fixed.
351 if (!requester.SchemeIsFile() && !embedder.SchemeIsFile() &&
352 primary_pattern.IsValid() && secondary_pattern.IsValid()) {
353 HostContentSettingsMap* settings_map = exclusive_access_manager()
354 ->context()
355 ->GetProfile()
356 ->GetHostContentSettingsMap();
357 settings_map->SetContentSetting(
358 primary_pattern, secondary_pattern, CONTENT_SETTINGS_TYPE_FULLSCREEN,
359 std::string(), CONTENT_SETTING_ALLOW);
361 tab_fullscreen_accepted_ = true;
362 return true;
365 return false;
368 bool FullscreenController::OnDenyExclusiveAccessPermission() {
369 if (IsWindowFullscreenForTabOrPending()) {
370 ExitExclusiveAccessIfNecessary();
371 return true;
374 return false;
377 GURL FullscreenController::GetURLForExclusiveAccessBubble() const {
378 if (exclusive_access_tab())
379 return GetRequestingOrigin();
380 return extension_caused_fullscreen_;
383 void FullscreenController::ExitExclusiveAccessIfNecessary() {
384 if (IsWindowFullscreenForTabOrPending())
385 ExitFullscreenModeForTab(exclusive_access_tab());
386 else
387 NotifyTabExclusiveAccessLost();
390 void FullscreenController::PostFullscreenChangeNotification(
391 bool is_fullscreen) {
392 base::MessageLoop::current()->PostTask(
393 FROM_HERE,
394 base::Bind(&FullscreenController::NotifyFullscreenChange,
395 ptr_factory_.GetWeakPtr(),
396 is_fullscreen));
399 void FullscreenController::NotifyFullscreenChange(bool is_fullscreen) {
400 content::NotificationService::current()->Notify(
401 chrome::NOTIFICATION_FULLSCREEN_CHANGED,
402 content::Source<FullscreenController>(this),
403 content::Details<bool>(&is_fullscreen));
406 void FullscreenController::NotifyTabExclusiveAccessLost() {
407 if (exclusive_access_tab()) {
408 WebContents* web_contents = exclusive_access_tab();
409 SetTabWithExclusiveAccess(nullptr);
410 fullscreened_origin_ = GURL();
411 state_prior_to_tab_fullscreen_ = STATE_INVALID;
412 tab_fullscreen_accepted_ = false;
413 web_contents->ExitFullscreen();
414 exclusive_access_manager()->UpdateExclusiveAccessExitBubbleContent();
418 void FullscreenController::ToggleFullscreenModeInternal(
419 FullscreenInternalOption option) {
420 #if defined(OS_WIN)
421 // When in Metro snap mode, toggling in and out of fullscreen is prevented.
422 if (IsInMetroSnapMode())
423 return;
424 #endif
426 ExclusiveAccessContext* const exclusive_access_context =
427 exclusive_access_manager()->context();
428 bool enter_fullscreen = !exclusive_access_context->IsFullscreen();
430 // When a Mac user requests a toggle they may be toggling between
431 // FullscreenWithoutChrome and FullscreenWithToolbar.
432 if (exclusive_access_context->IsFullscreen() &&
433 !IsWindowFullscreenForTabOrPending() &&
434 exclusive_access_context->SupportsFullscreenWithToolbar()) {
435 if (option == BROWSER_WITH_TOOLBAR) {
436 enter_fullscreen = enter_fullscreen ||
437 !exclusive_access_context->IsFullscreenWithToolbar();
438 } else {
439 enter_fullscreen = enter_fullscreen ||
440 exclusive_access_context->IsFullscreenWithToolbar();
444 // In kiosk mode, we always want to be fullscreen. When the browser first
445 // starts we're not yet fullscreen, so let the initial toggle go through.
446 if (chrome::IsRunningInAppMode() && exclusive_access_context->IsFullscreen())
447 return;
449 #if !defined(OS_MACOSX)
450 // Do not enter fullscreen mode if disallowed by pref. This prevents the user
451 // from manually entering fullscreen mode and also disables kiosk mode on
452 // desktop platforms.
453 if (enter_fullscreen &&
454 !exclusive_access_context->GetProfile()->GetPrefs()->GetBoolean(
455 prefs::kFullscreenAllowed)) {
456 return;
458 #endif
460 if (enter_fullscreen)
461 EnterFullscreenModeInternal(option);
462 else
463 ExitFullscreenModeInternal();
466 void FullscreenController::EnterFullscreenModeInternal(
467 FullscreenInternalOption option) {
468 toggled_into_fullscreen_ = true;
469 GURL url;
470 if (option == TAB) {
471 url = GetRequestingOrigin();
472 tab_fullscreen_accepted_ = GetFullscreenSetting() == CONTENT_SETTING_ALLOW;
473 } else {
474 if (!extension_caused_fullscreen_.is_empty())
475 url = extension_caused_fullscreen_;
478 if (option == BROWSER)
479 content::RecordAction(UserMetricsAction("ToggleFullscreen"));
480 // TODO(scheib): Record metrics for WITH_TOOLBAR, without counting transitions
481 // from tab fullscreen out to browser with toolbar.
483 exclusive_access_manager()->context()->EnterFullscreen(
484 url, exclusive_access_manager()->GetExclusiveAccessExitBubbleType(),
485 option == BROWSER_WITH_TOOLBAR);
487 exclusive_access_manager()->UpdateExclusiveAccessExitBubbleContent();
489 // Once the window has become fullscreen it'll call back to
490 // WindowFullscreenStateChanged(). We don't do this immediately as
491 // BrowserWindow::EnterFullscreen() asks for bookmark_bar_state_, so we let
492 // the BrowserWindow invoke WindowFullscreenStateChanged when appropriate.
495 void FullscreenController::ExitFullscreenModeInternal() {
496 toggled_into_fullscreen_ = false;
497 #if defined(OS_MACOSX)
498 // Mac windows report a state change instantly, and so we must also clear
499 // state_prior_to_tab_fullscreen_ to match them else other logic using
500 // state_prior_to_tab_fullscreen_ will be incorrect.
501 NotifyTabExclusiveAccessLost();
502 #endif
503 exclusive_access_manager()->context()->ExitFullscreen();
504 extension_caused_fullscreen_ = GURL();
506 exclusive_access_manager()->UpdateExclusiveAccessExitBubbleContent();
509 ContentSetting FullscreenController::GetFullscreenSetting() const {
510 DCHECK(exclusive_access_tab());
512 GURL url = GetRequestingOrigin();
514 // Always ask on file:// URLs, since we can't meaningfully make the
515 // decision stick for a particular origin.
516 // TODO(estark): Revisit this when crbug.com/455882 is fixed.
517 if (url.SchemeIsFile())
518 return CONTENT_SETTING_ASK;
520 if (IsPrivilegedFullscreenForTab())
521 return CONTENT_SETTING_ALLOW;
523 // If the permission was granted to the website with no embedder, it should
524 // always be allowed, even if embedded.
525 if (exclusive_access_manager()
526 ->context()
527 ->GetProfile()
528 ->GetHostContentSettingsMap()
529 ->GetContentSetting(url, url, CONTENT_SETTINGS_TYPE_FULLSCREEN,
530 std::string()) == CONTENT_SETTING_ALLOW) {
531 return CONTENT_SETTING_ALLOW;
534 // See the comment above the call to |SetContentSetting()| for how the
535 // requesting and embedding origins interact with each other wrt permissions.
536 return exclusive_access_manager()
537 ->context()
538 ->GetProfile()
539 ->GetHostContentSettingsMap()
540 ->GetContentSetting(url, GetEmbeddingOrigin(),
541 CONTENT_SETTINGS_TYPE_FULLSCREEN, std::string());
544 bool FullscreenController::IsPrivilegedFullscreenForTab() const {
545 const bool embedded_widget_present =
546 exclusive_access_tab() &&
547 exclusive_access_tab()->GetFullscreenRenderWidgetHostView();
548 return embedded_widget_present || is_privileged_fullscreen_for_testing_;
551 void FullscreenController::SetPrivilegedFullscreenForTesting(
552 bool is_privileged) {
553 is_privileged_fullscreen_for_testing_ = is_privileged;
556 bool FullscreenController::MaybeToggleFullscreenForCapturedTab(
557 WebContents* web_contents, bool enter_fullscreen) {
558 if (enter_fullscreen) {
559 if (web_contents->GetCapturerCount() > 0) {
560 FullscreenWithinTabHelper::CreateForWebContents(web_contents);
561 FullscreenWithinTabHelper::FromWebContents(web_contents)->
562 SetIsFullscreenForCapturedTab(true);
563 return true;
565 } else {
566 if (IsFullscreenForCapturedTab(web_contents)) {
567 FullscreenWithinTabHelper::RemoveForWebContents(web_contents);
568 return true;
572 return false;
575 bool FullscreenController::IsFullscreenForCapturedTab(
576 const WebContents* web_contents) const {
577 // Note: On Mac, some of the OnTabXXX() methods get called with a nullptr
578 // value
579 // for web_contents. Check for that here.
580 const FullscreenWithinTabHelper* const helper =
581 web_contents ? FullscreenWithinTabHelper::FromWebContents(web_contents)
582 : nullptr;
583 if (helper && helper->is_fullscreen_for_captured_tab()) {
584 DCHECK_NE(exclusive_access_tab(), web_contents);
585 return true;
587 return false;
590 GURL FullscreenController::GetRequestingOrigin() const {
591 DCHECK(exclusive_access_tab());
593 if (!fullscreened_origin_.is_empty())
594 return fullscreened_origin_;
596 return exclusive_access_tab()->GetLastCommittedURL();
599 GURL FullscreenController::GetEmbeddingOrigin() const {
600 DCHECK(exclusive_access_tab());
602 return exclusive_access_tab()->GetLastCommittedURL();