Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: chrome/browser/ui/fullscreen_controller.cc

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

Powered by Google App Engine
This is Rietveld 408576698