OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/browser/ui/fullscreen_controller.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/browser_window.h" |
| 12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "content/browser/user_metrics.h" |
| 15 #include "content/public/browser/notification_service.h" |
| 16 |
| 17 FullscreenController::FullscreenController(BrowserWindow* window, |
| 18 Profile* profile, |
| 19 Browser* browser) |
| 20 : window_(window), |
| 21 profile_(profile), |
| 22 browser_(browser), |
| 23 fullscreened_tab_(NULL), |
| 24 tab_caused_fullscreen_(false), |
| 25 tab_fullscreen_accepted_(false), |
| 26 mouse_lock_state_(MOUSELOCK_NOT_REQUESTED), |
| 27 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 28 } |
| 29 |
| 30 FullscreenController::~FullscreenController() {} |
| 31 |
| 32 bool FullscreenController::IsFullscreenForTab() const { |
| 33 return fullscreened_tab_ != NULL; |
| 34 } |
| 35 |
| 36 bool FullscreenController::IsFullscreenForTab(const TabContents* tab) const { |
| 37 const TabContentsWrapper* wrapper = |
| 38 TabContentsWrapper::GetCurrentWrapperForContents(tab); |
| 39 if (!wrapper || (wrapper != fullscreened_tab_)) |
| 40 return false; |
| 41 DCHECK(tab == browser_->GetSelectedTabContents()); |
| 42 DCHECK(window_->IsFullscreen()); |
| 43 return true; |
| 44 } |
| 45 |
| 46 void FullscreenController::RequestToLockMouse(TabContents* tab) { |
| 47 // Mouse Lock is only permitted when browser is in tab fullscreen. |
| 48 if (!IsFullscreenForTab(tab)) { |
| 49 tab->GotResponseToLockMouseRequest(false); |
| 50 return; |
| 51 } |
| 52 |
| 53 if (mouse_lock_state_ == MOUSELOCK_ACCEPTED) { |
| 54 tab->GotResponseToLockMouseRequest(true); |
| 55 return; |
| 56 } |
| 57 |
| 58 switch (GetMouseLockSetting(tab->GetURL())) { |
| 59 case CONTENT_SETTING_ALLOW: |
| 60 mouse_lock_state_ = MOUSELOCK_ACCEPTED; |
| 61 tab->GotResponseToLockMouseRequest(true); |
| 62 break; |
| 63 case CONTENT_SETTING_BLOCK: |
| 64 mouse_lock_state_ = MOUSELOCK_NOT_REQUESTED; |
| 65 tab->GotResponseToLockMouseRequest(false); |
| 66 break; |
| 67 case CONTENT_SETTING_ASK: |
| 68 mouse_lock_state_ = MOUSELOCK_REQUESTED; |
| 69 break; |
| 70 default: |
| 71 NOTREACHED(); |
| 72 } |
| 73 UpdateFullscreenExitBubbleContent(); |
| 74 } |
| 75 |
| 76 void FullscreenController::ToggleFullscreenModeForTab(TabContents* tab, |
| 77 bool enter_fullscreen) { |
| 78 if (tab != browser_->GetSelectedTabContents()) |
| 79 return; |
| 80 |
| 81 bool in_browser_or_tab_fullscreen_mode; |
| 82 #if defined(OS_MACOSX) |
| 83 in_browser_or_tab_fullscreen_mode = window_->InPresentationMode(); |
| 84 #else |
| 85 in_browser_or_tab_fullscreen_mode = window_->IsFullscreen(); |
| 86 #endif |
| 87 |
| 88 if (enter_fullscreen) { |
| 89 fullscreened_tab_ = TabContentsWrapper::GetCurrentWrapperForContents(tab); |
| 90 if (!in_browser_or_tab_fullscreen_mode) { |
| 91 tab_caused_fullscreen_ = true; |
| 92 #if defined(OS_MACOSX) |
| 93 TogglePresentationMode(true); |
| 94 #else |
| 95 ToggleFullscreenMode(true); |
| 96 #endif |
| 97 } else { |
| 98 // We need to update the fullscreen exit bubble, e.g., going from browser |
| 99 // fullscreen to tab fullscreen will need to show different content. |
| 100 const GURL& url = tab->GetURL(); |
| 101 if (!tab_fullscreen_accepted_) { |
| 102 tab_fullscreen_accepted_ = |
| 103 GetFullscreenSetting(url) == CONTENT_SETTING_ALLOW; |
| 104 } |
| 105 UpdateFullscreenExitBubbleContent(); |
| 106 } |
| 107 } else { |
| 108 if (in_browser_or_tab_fullscreen_mode) { |
| 109 if (tab_caused_fullscreen_) { |
| 110 #if defined(OS_MACOSX) |
| 111 TogglePresentationMode(true); |
| 112 #else |
| 113 ToggleFullscreenMode(true); |
| 114 #endif |
| 115 } else { |
| 116 // If currently there is a tab in "tab fullscreen" mode and fullscreen |
| 117 // was not caused by it (i.e., previously it was in "browser fullscreen" |
| 118 // mode), we need to switch back to "browser fullscreen" mode. In this |
| 119 // case, all we have to do is notifying the tab that it has exited "tab |
| 120 // fullscreen" mode. |
| 121 NotifyTabOfFullscreenExitIfNecessary(); |
| 122 } |
| 123 } |
| 124 } |
| 125 } |
| 126 |
| 127 #if defined(OS_MACOSX) |
| 128 void FullscreenController::TogglePresentationMode(bool for_tab) { |
| 129 bool entering_fullscreen = !window_->InPresentationMode(); |
| 130 GURL url; |
| 131 bool ask_permission = false; |
| 132 if (for_tab) { |
| 133 url = browser_->GetSelectedTabContents()->GetURL(); |
| 134 ask_permission = GetFullscreenSetting(url) != CONTENT_SETTING_ALLOW; |
| 135 } |
| 136 if (entering_fullscreen) { |
| 137 FullscreenExitBubbleType type = |
| 138 FEB_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION; |
| 139 if (for_tab) { |
| 140 type = ask_permission ? FEB_TYPE_FULLSCREEN_BUTTONS : |
| 141 FEB_TYPE_FULLSCREEN_EXIT_INSTRUCTION; |
| 142 } |
| 143 window_->EnterPresentationMode(url, type); |
| 144 } else { |
| 145 window_->ExitPresentationMode(); |
| 146 } |
| 147 WindowFullscreenStateChanged(); |
| 148 } |
| 149 #endif |
| 150 |
| 151 // TODO(koz): Change |for_tab| to an enum. |
| 152 void FullscreenController::ToggleFullscreenMode(bool for_tab) { |
| 153 bool entering_fullscreen = !window_->IsFullscreen(); |
| 154 |
| 155 #if !defined(OS_MACOSX) |
| 156 // In kiosk mode, we always want to be fullscreen. When the browser first |
| 157 // starts we're not yet fullscreen, so let the initial toggle go through. |
| 158 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kKioskMode) && |
| 159 window_->IsFullscreen()) |
| 160 return; |
| 161 #endif |
| 162 |
| 163 GURL url; |
| 164 if (for_tab) { |
| 165 url = browser_->GetSelectedTabContents()->GetURL(); |
| 166 tab_fullscreen_accepted_ = entering_fullscreen && |
| 167 GetFullscreenSetting(url) == CONTENT_SETTING_ALLOW; |
| 168 } else { |
| 169 UserMetrics::RecordAction(UserMetricsAction("ToggleFullscreen")); |
| 170 } |
| 171 if (entering_fullscreen) |
| 172 window_->EnterFullscreen(url, GetFullscreenExitBubbleType()); |
| 173 else |
| 174 window_->ExitFullscreen(); |
| 175 |
| 176 // Once the window has become fullscreen it'll call back to |
| 177 // WindowFullscreenStateChanged(). We don't do this immediately as |
| 178 // BrowserWindow::EnterFullscreen() asks for bookmark_bar_state_, so we let |
| 179 // the BrowserWindow invoke WindowFullscreenStateChanged when appropriate. |
| 180 |
| 181 // TODO: convert mac to invoke WindowFullscreenStateChanged once it updates |
| 182 // the necessary state of the frame. |
| 183 #if defined(OS_MACOSX) |
| 184 WindowFullscreenStateChanged(); |
| 185 #endif |
| 186 } |
| 187 |
| 188 void FullscreenController::LostMouseLock() { |
| 189 mouse_lock_state_ = MOUSELOCK_NOT_REQUESTED; |
| 190 UpdateFullscreenExitBubbleContent(); |
| 191 } |
| 192 |
| 193 void FullscreenController::OnTabClosing(TabContents* tab_contents) { |
| 194 if (IsFullscreenForTab(tab_contents)) { |
| 195 ExitTabbedFullscreenModeIfNecessary(); |
| 196 // The call to exit fullscreen may result in asynchronous notification of |
| 197 // fullscreen state change (e.g., on Linux). We don't want to rely on it |
| 198 // to call NotifyTabOfFullscreenExitIfNecessary(), because at that point |
| 199 // |fullscreen_tab_| may not be valid. Instead, we call it here to clean up |
| 200 // tab fullscreen related state. |
| 201 NotifyTabOfFullscreenExitIfNecessary(); |
| 202 } |
| 203 } |
| 204 |
| 205 void FullscreenController::OnTabDeactivated(TabContentsWrapper* contents) { |
| 206 if (contents == fullscreened_tab_) |
| 207 ExitTabbedFullscreenModeIfNecessary(); |
| 208 } |
| 209 |
| 210 void FullscreenController::OnAcceptFullscreenPermission( |
| 211 const GURL& url, |
| 212 FullscreenExitBubbleType bubble_type) { |
| 213 bool mouse_lock = false; |
| 214 bool fullscreen = false; |
| 215 fullscreen_bubble::PermissionRequestedByType(bubble_type, &fullscreen, |
| 216 &mouse_lock); |
| 217 DCHECK(fullscreened_tab_); |
| 218 DCHECK_NE(tab_fullscreen_accepted_, fullscreen); |
| 219 |
| 220 HostContentSettingsMap* settings_map = profile_->GetHostContentSettingsMap(); |
| 221 if (mouse_lock) { |
| 222 DCHECK_EQ(mouse_lock_state_, MOUSELOCK_REQUESTED); |
| 223 settings_map->SetContentSetting(ContentSettingsPattern::FromURL(url), |
| 224 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_MOUSELOCK, |
| 225 std::string(), CONTENT_SETTING_ALLOW); |
| 226 mouse_lock_state_ = |
| 227 fullscreened_tab_->tab_contents()->GotResponseToLockMouseRequest(true) ? |
| 228 MOUSELOCK_ACCEPTED : MOUSELOCK_NOT_REQUESTED; |
| 229 } |
| 230 if (!tab_fullscreen_accepted_) { |
| 231 settings_map->SetContentSetting(ContentSettingsPattern::FromURL(url), |
| 232 ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_FULLSCREEN, |
| 233 std::string(), CONTENT_SETTING_ALLOW); |
| 234 tab_fullscreen_accepted_ = true; |
| 235 } |
| 236 UpdateFullscreenExitBubbleContent(); |
| 237 } |
| 238 |
| 239 void FullscreenController::OnDenyFullscreenPermission( |
| 240 FullscreenExitBubbleType bubble_type) { |
| 241 bool mouse_lock = false; |
| 242 bool fullscreen = false; |
| 243 fullscreen_bubble::PermissionRequestedByType(bubble_type, &fullscreen, |
| 244 &mouse_lock); |
| 245 DCHECK(fullscreened_tab_); |
| 246 DCHECK_NE(tab_fullscreen_accepted_, fullscreen); |
| 247 |
| 248 if (mouse_lock) { |
| 249 DCHECK_EQ(mouse_lock_state_, MOUSELOCK_REQUESTED); |
| 250 mouse_lock_state_ = MOUSELOCK_NOT_REQUESTED; |
| 251 fullscreened_tab_->tab_contents()->GotResponseToLockMouseRequest(false); |
| 252 if (!fullscreen) |
| 253 UpdateFullscreenExitBubbleContent(); |
| 254 } |
| 255 |
| 256 if (fullscreen) |
| 257 ExitTabbedFullscreenModeIfNecessary(); |
| 258 } |
| 259 |
| 260 void FullscreenController::WindowFullscreenStateChanged() { |
| 261 MessageLoop::current()->PostTask(FROM_HERE, |
| 262 base::Bind(&FullscreenController::NotifyFullscreenChange, this)); |
| 263 bool notify_tab_of_exit; |
| 264 #if defined(OS_MACOSX) |
| 265 notify_tab_of_exit = !window_->InPresentationMode(); |
| 266 #else |
| 267 notify_tab_of_exit = !window_->IsFullscreen(); |
| 268 #endif |
| 269 if (notify_tab_of_exit) |
| 270 NotifyTabOfFullscreenExitIfNecessary(); |
| 271 } |
| 272 |
| 273 bool FullscreenController::HandleUserPressedEscape() { |
| 274 if (!IsFullscreenForTab()) |
| 275 return false; |
| 276 ExitTabbedFullscreenModeIfNecessary(); |
| 277 return true; |
| 278 } |
| 279 |
| 280 void FullscreenController::NotifyTabOfFullscreenExitIfNecessary() { |
| 281 if (fullscreened_tab_) |
| 282 fullscreened_tab_->ExitFullscreenMode(); |
| 283 else |
| 284 DCHECK_EQ(mouse_lock_state_, MOUSELOCK_NOT_REQUESTED); |
| 285 |
| 286 fullscreened_tab_ = NULL; |
| 287 tab_caused_fullscreen_ = false; |
| 288 tab_fullscreen_accepted_ = false; |
| 289 mouse_lock_state_ = MOUSELOCK_NOT_REQUESTED; |
| 290 |
| 291 UpdateFullscreenExitBubbleContent(); |
| 292 } |
| 293 |
| 294 void FullscreenController::ExitTabbedFullscreenModeIfNecessary() { |
| 295 if (tab_caused_fullscreen_) |
| 296 ToggleFullscreenMode(false); |
| 297 else |
| 298 NotifyTabOfFullscreenExitIfNecessary(); |
| 299 } |
| 300 |
| 301 void FullscreenController::UpdateFullscreenExitBubbleContent() { |
| 302 GURL url; |
| 303 if (fullscreened_tab_) |
| 304 url = fullscreened_tab_->tab_contents()->GetURL(); |
| 305 |
| 306 window_->UpdateFullscreenExitBubbleContent(url, |
| 307 GetFullscreenExitBubbleType()); |
| 308 } |
| 309 |
| 310 void FullscreenController::NotifyFullscreenChange() { |
| 311 content::NotificationService::current()->Notify( |
| 312 chrome::NOTIFICATION_FULLSCREEN_CHANGED, |
| 313 content::Source<FullscreenController>(this), |
| 314 content::NotificationService::NoDetails()); |
| 315 } |
| 316 |
| 317 FullscreenExitBubbleType |
| 318 FullscreenController::GetFullscreenExitBubbleType() const { |
| 319 if (!fullscreened_tab_) { |
| 320 DCHECK_EQ(MOUSELOCK_NOT_REQUESTED, mouse_lock_state_); |
| 321 return FEB_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION; |
| 322 } |
| 323 if (fullscreened_tab_ && !tab_fullscreen_accepted_) { |
| 324 DCHECK_NE(MOUSELOCK_NOT_REQUESTED, mouse_lock_state_); |
| 325 return mouse_lock_state_ == MOUSELOCK_REQUESTED ? |
| 326 FEB_TYPE_FULLSCREEN_MOUSELOCK_BUTTONS : FEB_TYPE_FULLSCREEN_BUTTONS; |
| 327 } |
| 328 if (mouse_lock_state_ == MOUSELOCK_REQUESTED) |
| 329 return FEB_TYPE_MOUSELOCK_BUTTONS; |
| 330 return mouse_lock_state_ == MOUSELOCK_ACCEPTED ? |
| 331 FEB_TYPE_FULLSCREEN_MOUSELOCK_EXIT_INSTRUCTION : |
| 332 FEB_TYPE_FULLSCREEN_EXIT_INSTRUCTION; |
| 333 } |
| 334 |
| 335 ContentSetting |
| 336 FullscreenController::GetFullscreenSetting(const GURL& url) const { |
| 337 if (url.SchemeIsFile()) |
| 338 return CONTENT_SETTING_ALLOW; |
| 339 |
| 340 return profile_->GetHostContentSettingsMap()->GetContentSetting(url, url, |
| 341 CONTENT_SETTINGS_TYPE_FULLSCREEN, std::string()); |
| 342 |
| 343 } |
| 344 |
| 345 ContentSetting |
| 346 FullscreenController::GetMouseLockSetting(const GURL& url) const { |
| 347 if (url.SchemeIsFile()) |
| 348 return CONTENT_SETTING_ALLOW; |
| 349 |
| 350 HostContentSettingsMap* settings_map = profile_->GetHostContentSettingsMap(); |
| 351 return settings_map->GetContentSetting(url, url, |
| 352 CONTENT_SETTINGS_TYPE_MOUSELOCK, std::string()); |
| 353 } |
OLD | NEW |