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/browser_command_controller.h"
7 #include "base/command_line.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/defaults.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_util.h"
15 #include "chrome/browser/lifetime/application_lifetime.h"
16 #include "chrome/browser/prefs/incognito_mode_prefs.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/sessions/tab_restore_service.h"
19 #include "chrome/browser/sessions/tab_restore_service_factory.h"
20 #include "chrome/browser/shell_integration.h"
21 #include "chrome/browser/signin/signin_promo.h"
22 #include "chrome/browser/sync/profile_sync_service.h"
23 #include "chrome/browser/sync/profile_sync_service_factory.h"
24 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
25 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/browser_commands.h"
27 #include "chrome/browser/ui/browser_window.h"
28 #include "chrome/browser/ui/chrome_pages.h"
29 #include "chrome/browser/ui/tabs/tab_strip_model.h"
30 #include "chrome/browser/ui/tabs/tab_strip_model_utils.h"
31 #include "chrome/browser/ui/webui/inspect_ui.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/content_restriction.h"
34 #include "chrome/common/pref_names.h"
35 #include "chrome/common/profiling.h"
36 #include "content/public/browser/native_web_keyboard_event.h"
37 #include "content/public/browser/navigation_controller.h"
38 #include "content/public/browser/navigation_entry.h"
39 #include "content/public/browser/user_metrics.h"
40 #include "content/public/browser/web_contents.h"
41 #include "content/public/browser/web_contents_observer.h"
42 #include "content/public/common/url_constants.h"
43 #include "extensions/browser/extension_system.h"
44 #include "ui/events/keycodes/keyboard_codes.h"
46 #if defined(OS_MACOSX)
47 #include "chrome/browser/ui/browser_commands_mac.h"
51 #include "base/win/metro.h"
52 #include "base/win/windows_version.h"
53 #include "chrome/browser/ui/apps/apps_metro_handler_win.h"
54 #include "content/public/browser/gpu_data_manager.h"
58 #include "ash/accelerators/accelerator_commands.h"
59 #include "chrome/browser/ui/ash/ash_util.h"
62 #if defined(OS_CHROMEOS)
63 #include "ash/multi_profile_uma.h"
64 #include "ash/session/session_state_delegate.h"
65 #include "ash/shell.h"
66 #include "chrome/browser/ui/ash/multi_user/multi_user_context_menu.h"
67 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
68 #include "chrome/browser/ui/browser_commands_chromeos.h"
71 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
72 #include "ui/events/linux/text_edit_key_bindings_delegate_auralinux.h"
75 using content::NavigationEntry
;
76 using content::NavigationController
;
77 using content::WebContents
;
82 // Not in fullscreen mode.
83 WINDOW_STATE_NOT_FULLSCREEN
,
85 // Fullscreen mode, occupying the whole screen.
86 WINDOW_STATE_FULLSCREEN
,
88 // Fullscreen mode for metro snap, occupying the full height and 20% of
90 WINDOW_STATE_METRO_SNAP
,
93 // Returns |true| if entry has an internal chrome:// URL, |false| otherwise.
94 bool HasInternalURL(const NavigationEntry
* entry
) {
98 // Check the |virtual_url()| first. This catches regular chrome:// URLs
99 // including URLs that were rewritten (such as chrome://bookmarks).
100 if (entry
->GetVirtualURL().SchemeIs(content::kChromeUIScheme
))
103 // If the |virtual_url()| isn't a chrome:// URL, check if it's actually
104 // view-source: of a chrome:// URL.
105 if (entry
->GetVirtualURL().SchemeIs(content::kViewSourceScheme
))
106 return entry
->GetURL().SchemeIs(content::kChromeUIScheme
);
112 // Windows 8 specific helper class to manage DefaultBrowserWorker. It does the
113 // following asynchronous actions in order:
114 // 1- Check that chrome is the default browser
115 // 2- If we are the default, restart chrome in metro and exit
116 // 3- If not the default browser show the 'select default browser' system dialog
117 // 4- When dialog dismisses check again who got made the default
118 // 5- If we are the default then restart chrome in metro and exit
119 // 6- If we are not the default exit.
121 // Note: this class deletes itself.
122 class SwitchToMetroUIHandler
123 : public ShellIntegration::DefaultWebClientObserver
{
125 SwitchToMetroUIHandler()
126 : default_browser_worker_(
127 new ShellIntegration::DefaultBrowserWorker(this)),
129 default_browser_worker_
->StartCheckIsDefault();
132 virtual ~SwitchToMetroUIHandler() {
133 default_browser_worker_
->ObserverDestroyed();
137 virtual void SetDefaultWebClientUIState(
138 ShellIntegration::DefaultWebClientUIState state
) OVERRIDE
{
140 case ShellIntegration::STATE_PROCESSING
:
142 case ShellIntegration::STATE_UNKNOWN
:
144 case ShellIntegration::STATE_IS_DEFAULT
:
145 chrome::AttemptRestartToMetroMode();
147 case ShellIntegration::STATE_NOT_DEFAULT
:
149 default_browser_worker_
->StartSetAsDefault();
159 virtual void OnSetAsDefaultConcluded(bool success
) OVERRIDE
{
164 first_check_
= false;
165 default_browser_worker_
->StartCheckIsDefault();
168 virtual bool IsInteractiveSetDefaultPermitted() OVERRIDE
{
172 scoped_refptr
<ShellIntegration::DefaultBrowserWorker
> default_browser_worker_
;
175 DISALLOW_COPY_AND_ASSIGN(SwitchToMetroUIHandler
);
177 #endif // defined(OS_WIN)
183 ///////////////////////////////////////////////////////////////////////////////
184 // BrowserCommandController, public:
186 BrowserCommandController::BrowserCommandController(Browser
* browser
)
188 command_updater_(this),
189 block_command_execution_(false),
190 last_blocked_command_id_(-1),
191 last_blocked_command_disposition_(CURRENT_TAB
) {
192 browser_
->tab_strip_model()->AddObserver(this);
193 PrefService
* local_state
= g_browser_process
->local_state();
195 local_pref_registrar_
.Init(local_state
);
196 local_pref_registrar_
.Add(
197 prefs::kAllowFileSelectionDialogs
,
199 &BrowserCommandController::UpdateCommandsForFileSelectionDialogs
,
200 base::Unretained(this)));
203 profile_pref_registrar_
.Init(profile()->GetPrefs());
204 profile_pref_registrar_
.Add(
205 prefs::kDevToolsDisabled
,
206 base::Bind(&BrowserCommandController::UpdateCommandsForDevTools
,
207 base::Unretained(this)));
208 profile_pref_registrar_
.Add(
209 bookmarks::prefs::kEditBookmarksEnabled
,
210 base::Bind(&BrowserCommandController::UpdateCommandsForBookmarkEditing
,
211 base::Unretained(this)));
212 profile_pref_registrar_
.Add(
213 bookmarks::prefs::kShowBookmarkBar
,
214 base::Bind(&BrowserCommandController::UpdateCommandsForBookmarkBar
,
215 base::Unretained(this)));
216 profile_pref_registrar_
.Add(
217 prefs::kIncognitoModeAvailability
,
219 &BrowserCommandController::UpdateCommandsForIncognitoAvailability
,
220 base::Unretained(this)));
221 profile_pref_registrar_
.Add(
222 prefs::kPrintingEnabled
,
223 base::Bind(&BrowserCommandController::UpdatePrintingState
,
224 base::Unretained(this)));
225 #if !defined(OS_MACOSX)
226 profile_pref_registrar_
.Add(
227 prefs::kFullscreenAllowed
,
228 base::Bind(&BrowserCommandController::UpdateCommandsForFullscreenMode
,
229 base::Unretained(this)));
231 pref_signin_allowed_
.Init(
232 prefs::kSigninAllowed
,
233 profile()->GetOriginalProfile()->GetPrefs(),
234 base::Bind(&BrowserCommandController::OnSigninAllowedPrefChange
,
235 base::Unretained(this)));
239 TabRestoreService
* tab_restore_service
=
240 TabRestoreServiceFactory::GetForProfile(profile());
241 if (tab_restore_service
) {
242 tab_restore_service
->AddObserver(this);
243 TabRestoreServiceChanged(tab_restore_service
);
247 BrowserCommandController::~BrowserCommandController() {
248 // TabRestoreService may have been shutdown by the time we get here. Don't
249 // trigger creating it.
250 TabRestoreService
* tab_restore_service
=
251 TabRestoreServiceFactory::GetForProfileIfExisting(profile());
252 if (tab_restore_service
)
253 tab_restore_service
->RemoveObserver(this);
254 profile_pref_registrar_
.RemoveAll();
255 local_pref_registrar_
.RemoveAll();
256 browser_
->tab_strip_model()->RemoveObserver(this);
259 bool BrowserCommandController::IsReservedCommandOrKey(
261 const content::NativeWebKeyboardEvent
& event
) {
262 // In Apps mode, no keys are reserved.
263 if (browser_
->is_app())
266 #if defined(OS_CHROMEOS)
267 // On Chrome OS, the top row of keys are mapped to browser actions like
268 // back/forward or refresh. We don't want web pages to be able to change the
269 // behavior of these keys. Ash handles F4 and up; this leaves us needing to
270 // reserve browser back/forward and refresh here.
271 ui::KeyboardCode key_code
=
272 static_cast<ui::KeyboardCode
>(event
.windowsKeyCode
);
273 if ((key_code
== ui::VKEY_BROWSER_BACK
&& command_id
== IDC_BACK
) ||
274 (key_code
== ui::VKEY_BROWSER_FORWARD
&& command_id
== IDC_FORWARD
) ||
275 (key_code
== ui::VKEY_BROWSER_REFRESH
&& command_id
== IDC_RELOAD
)) {
280 if (window()->IsFullscreen() && command_id
== IDC_FULLSCREEN
)
283 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
284 // If this key was registered by the user as a content editing hotkey, then
285 // it is not reserved.
286 ui::TextEditKeyBindingsDelegateAuraLinux
* delegate
=
287 ui::GetTextEditKeyBindingsDelegate();
288 if (delegate
&& event
.os_event
&& delegate
->MatchEvent(*event
.os_event
, NULL
))
292 return command_id
== IDC_CLOSE_TAB
||
293 command_id
== IDC_CLOSE_WINDOW
||
294 command_id
== IDC_NEW_INCOGNITO_WINDOW
||
295 command_id
== IDC_NEW_TAB
||
296 command_id
== IDC_NEW_WINDOW
||
297 command_id
== IDC_RESTORE_TAB
||
298 command_id
== IDC_SELECT_NEXT_TAB
||
299 command_id
== IDC_SELECT_PREVIOUS_TAB
||
300 command_id
== IDC_EXIT
;
303 void BrowserCommandController::SetBlockCommandExecution(bool block
) {
304 block_command_execution_
= block
;
306 last_blocked_command_id_
= -1;
307 last_blocked_command_disposition_
= CURRENT_TAB
;
311 int BrowserCommandController::GetLastBlockedCommand(
312 WindowOpenDisposition
* disposition
) {
314 *disposition
= last_blocked_command_disposition_
;
315 return last_blocked_command_id_
;
318 void BrowserCommandController::TabStateChanged() {
319 UpdateCommandsForTabState();
322 void BrowserCommandController::ZoomStateChanged() {
323 UpdateCommandsForZoomState();
326 void BrowserCommandController::ContentRestrictionsChanged() {
327 UpdateCommandsForContentRestrictionState();
330 void BrowserCommandController::FullscreenStateChanged() {
331 UpdateCommandsForFullscreenMode();
334 void BrowserCommandController::PrintingStateChanged() {
335 UpdatePrintingState();
338 void BrowserCommandController::LoadingStateChanged(bool is_loading
,
340 UpdateReloadStopState(is_loading
, force
);
343 ////////////////////////////////////////////////////////////////////////////////
344 // BrowserCommandController, CommandUpdaterDelegate implementation:
346 void BrowserCommandController::ExecuteCommandWithDisposition(
348 WindowOpenDisposition disposition
) {
349 // No commands are enabled if there is not yet any selected tab.
350 // TODO(pkasting): It seems like we should not need this, because either
351 // most/all commands should not have been enabled yet anyway or the ones that
352 // are enabled should be global, or safe themselves against having no selected
353 // tab. However, Ben says he tried removing this before and got lots of
354 // crashes, e.g. from Windows sending WM_COMMANDs at random times during
355 // window construction. This probably could use closer examination someday.
356 if (browser_
->tab_strip_model()->active_index() == TabStripModel::kNoTab
)
359 DCHECK(command_updater_
.IsCommandEnabled(id
)) << "Invalid/disabled command "
362 // If command execution is blocked then just record the command and return.
363 if (block_command_execution_
) {
364 // We actually only allow no more than one blocked command, otherwise some
365 // commands maybe lost.
366 DCHECK_EQ(last_blocked_command_id_
, -1);
367 last_blocked_command_id_
= id
;
368 last_blocked_command_disposition_
= disposition
;
372 // The order of commands in this switch statement must match the function
373 // declaration order in browser.h!
375 // Navigation commands
377 GoBack(browser_
, disposition
);
380 GoForward(browser_
, disposition
);
383 Reload(browser_
, disposition
);
385 case IDC_RELOAD_CLEARING_CACHE
:
386 ClearCache(browser_
);
388 case IDC_RELOAD_IGNORING_CACHE
:
389 ReloadIgnoringCache(browser_
, disposition
);
392 Home(browser_
, disposition
);
394 case IDC_OPEN_CURRENT_URL
:
395 OpenCurrentURL(browser_
);
401 // Window management commands
405 case IDC_NEW_INCOGNITO_WINDOW
:
406 NewIncognitoWindow(browser_
);
408 case IDC_CLOSE_WINDOW
:
409 content::RecordAction(base::UserMetricsAction("CloseWindowByKey"));
410 CloseWindow(browser_
);
416 content::RecordAction(base::UserMetricsAction("CloseTabByKey"));
419 case IDC_SELECT_NEXT_TAB
:
420 content::RecordAction(base::UserMetricsAction("Accel_SelectNextTab"));
421 SelectNextTab(browser_
);
423 case IDC_SELECT_PREVIOUS_TAB
:
424 content::RecordAction(
425 base::UserMetricsAction("Accel_SelectPreviousTab"));
426 SelectPreviousTab(browser_
);
428 case IDC_MOVE_TAB_NEXT
:
429 MoveTabNext(browser_
);
431 case IDC_MOVE_TAB_PREVIOUS
:
432 MoveTabPrevious(browser_
);
434 case IDC_SELECT_TAB_0
:
435 case IDC_SELECT_TAB_1
:
436 case IDC_SELECT_TAB_2
:
437 case IDC_SELECT_TAB_3
:
438 case IDC_SELECT_TAB_4
:
439 case IDC_SELECT_TAB_5
:
440 case IDC_SELECT_TAB_6
:
441 case IDC_SELECT_TAB_7
:
442 SelectNumberedTab(browser_
, id
- IDC_SELECT_TAB_0
);
444 case IDC_SELECT_LAST_TAB
:
445 SelectLastTab(browser_
);
447 case IDC_DUPLICATE_TAB
:
448 DuplicateTab(browser_
);
450 case IDC_RESTORE_TAB
:
451 RestoreTab(browser_
);
453 case IDC_SHOW_AS_TAB
:
454 ConvertPopupToTabbedBrowser(browser_
);
457 #if defined(OS_MACOSX)
458 chrome::ToggleFullscreenWithChromeOrFallback(browser_
);
460 chrome::ToggleFullscreenMode(browser_
);
464 #if defined(OS_CHROMEOS)
465 case IDC_VISIT_DESKTOP_OF_LRU_USER_2
:
466 case IDC_VISIT_DESKTOP_OF_LRU_USER_3
:
467 ExecuteVisitDesktopCommand(id
, browser_
->window()->GetNativeWindow());
471 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
472 case IDC_USE_SYSTEM_TITLE_BAR
: {
473 PrefService
* prefs
= browser_
->profile()->GetPrefs();
474 prefs
->SetBoolean(prefs::kUseCustomChromeFrame
,
475 !prefs
->GetBoolean(prefs::kUseCustomChromeFrame
));
481 // Windows 8 specific commands.
482 case IDC_METRO_SNAP_ENABLE
:
483 browser_
->SetMetroSnapMode(true);
485 case IDC_METRO_SNAP_DISABLE
:
486 browser_
->SetMetroSnapMode(false);
488 case IDC_WIN_DESKTOP_RESTART
:
489 if (!VerifyASHSwitchForApps(window()->GetNativeWindow(), id
))
492 chrome::AttemptRestartToDesktopMode();
493 if (base::win::GetVersion() >= base::win::VERSION_WIN8
) {
494 content::RecordAction(base::UserMetricsAction("Win8DesktopRestart"));
496 content::RecordAction(base::UserMetricsAction("Win7DesktopRestart"));
499 case IDC_WIN8_METRO_RESTART
:
500 case IDC_WIN_CHROMEOS_RESTART
:
501 if (!VerifyASHSwitchForApps(window()->GetNativeWindow(), id
))
503 if (base::win::GetVersion() >= base::win::VERSION_WIN8
) {
504 // SwitchToMetroUIHandler deletes itself.
505 new SwitchToMetroUIHandler
;
506 content::RecordAction(base::UserMetricsAction("Win8MetroRestart"));
508 content::RecordAction(base::UserMetricsAction("Win7ASHRestart"));
509 chrome::AttemptRestartToMetroMode();
514 #if defined(OS_MACOSX)
515 case IDC_PRESENTATION_MODE
:
516 chrome::ToggleFullscreenMode(browser_
);
523 // Page-related commands
527 case IDC_BOOKMARK_PAGE
:
528 BookmarkCurrentPage(browser_
);
530 case IDC_PIN_TO_START_SCREEN
:
531 TogglePagePinnedToStartScreen(browser_
);
533 case IDC_BOOKMARK_ALL_TABS
:
534 BookmarkAllTabs(browser_
);
536 case IDC_VIEW_SOURCE
:
537 ViewSelectedSource(browser_
);
539 case IDC_EMAIL_PAGE_LOCATION
:
540 EmailPageLocation(browser_
);
545 #if !defined(DISABLE_BASIC_PRINTING)
546 case IDC_BASIC_PRINT
:
547 content::RecordAction(base::UserMetricsAction("Accel_Advanced_Print"));
548 BasicPrint(browser_
);
550 #endif // !DISABLE_BASIC_PRINTING
551 case IDC_TRANSLATE_PAGE
:
554 case IDC_MANAGE_PASSWORDS_FOR_PAGE
:
555 ManagePasswordsForPage(browser_
);
558 // Page encoding commands
559 case IDC_ENCODING_AUTO_DETECT
:
560 browser_
->ToggleEncodingAutoDetect();
562 case IDC_ENCODING_UTF8
:
563 case IDC_ENCODING_UTF16LE
:
564 case IDC_ENCODING_ISO88591
:
565 case IDC_ENCODING_WINDOWS1252
:
566 case IDC_ENCODING_GBK
:
567 case IDC_ENCODING_GB18030
:
568 case IDC_ENCODING_BIG5HKSCS
:
569 case IDC_ENCODING_BIG5
:
570 case IDC_ENCODING_KOREAN
:
571 case IDC_ENCODING_SHIFTJIS
:
572 case IDC_ENCODING_ISO2022JP
:
573 case IDC_ENCODING_EUCJP
:
574 case IDC_ENCODING_THAI
:
575 case IDC_ENCODING_ISO885915
:
576 case IDC_ENCODING_MACINTOSH
:
577 case IDC_ENCODING_ISO88592
:
578 case IDC_ENCODING_WINDOWS1250
:
579 case IDC_ENCODING_ISO88595
:
580 case IDC_ENCODING_WINDOWS1251
:
581 case IDC_ENCODING_KOI8R
:
582 case IDC_ENCODING_KOI8U
:
583 case IDC_ENCODING_ISO88597
:
584 case IDC_ENCODING_WINDOWS1253
:
585 case IDC_ENCODING_ISO88594
:
586 case IDC_ENCODING_ISO885913
:
587 case IDC_ENCODING_WINDOWS1257
:
588 case IDC_ENCODING_ISO88593
:
589 case IDC_ENCODING_ISO885910
:
590 case IDC_ENCODING_ISO885914
:
591 case IDC_ENCODING_ISO885916
:
592 case IDC_ENCODING_WINDOWS1254
:
593 case IDC_ENCODING_ISO88596
:
594 case IDC_ENCODING_WINDOWS1256
:
595 case IDC_ENCODING_ISO88598
:
596 case IDC_ENCODING_ISO88598I
:
597 case IDC_ENCODING_WINDOWS1255
:
598 case IDC_ENCODING_WINDOWS1258
:
599 browser_
->OverrideEncoding(id
);
602 // Clipboard commands
620 case IDC_FIND_PREVIOUS
:
621 FindPrevious(browser_
);
626 Zoom(browser_
, content::PAGE_ZOOM_IN
);
628 case IDC_ZOOM_NORMAL
:
629 Zoom(browser_
, content::PAGE_ZOOM_RESET
);
632 Zoom(browser_
, content::PAGE_ZOOM_OUT
);
635 // Focus various bits of UI
636 case IDC_FOCUS_TOOLBAR
:
637 content::RecordAction(base::UserMetricsAction("Accel_Focus_Toolbar"));
638 FocusToolbar(browser_
);
640 case IDC_FOCUS_LOCATION
:
641 content::RecordAction(base::UserMetricsAction("Accel_Focus_Location"));
642 FocusLocationBar(browser_
);
644 case IDC_FOCUS_SEARCH
:
645 content::RecordAction(base::UserMetricsAction("Accel_Focus_Search"));
646 FocusSearch(browser_
);
648 case IDC_FOCUS_MENU_BAR
:
649 FocusAppMenu(browser_
);
651 case IDC_FOCUS_BOOKMARKS
:
652 content::RecordAction(
653 base::UserMetricsAction("Accel_Focus_Bookmarks"));
654 FocusBookmarksToolbar(browser_
);
656 case IDC_FOCUS_INFOBARS
:
657 FocusInfobars(browser_
);
659 case IDC_FOCUS_NEXT_PANE
:
660 FocusNextPane(browser_
);
662 case IDC_FOCUS_PREVIOUS_PANE
:
663 FocusPreviousPane(browser_
);
666 // Show various bits of UI
668 browser_
->OpenFile();
670 case IDC_CREATE_SHORTCUTS
:
671 CreateApplicationShortcuts(browser_
);
673 case IDC_CREATE_HOSTED_APP
:
674 CreateBookmarkAppFromCurrentWebContents(browser_
);
677 ToggleDevToolsWindow(browser_
, DevToolsToggleAction::Show());
679 case IDC_DEV_TOOLS_CONSOLE
:
680 ToggleDevToolsWindow(browser_
, DevToolsToggleAction::ShowConsole());
682 case IDC_DEV_TOOLS_DEVICES
:
683 InspectUI::InspectDevices(browser_
);
685 case IDC_DEV_TOOLS_INSPECT
:
686 ToggleDevToolsWindow(browser_
, DevToolsToggleAction::Inspect());
688 case IDC_DEV_TOOLS_TOGGLE
:
689 ToggleDevToolsWindow(browser_
, DevToolsToggleAction::Toggle());
691 case IDC_TASK_MANAGER
:
692 OpenTaskManager(browser_
);
694 #if defined(OS_CHROMEOS)
695 case IDC_TAKE_SCREENSHOT
:
699 #if defined(GOOGLE_CHROME_BUILD)
701 OpenFeedbackDialog(browser_
);
704 case IDC_SHOW_BOOKMARK_BAR
:
705 ToggleBookmarkBar(browser_
);
707 case IDC_PROFILING_ENABLED
:
711 case IDC_SHOW_BOOKMARK_MANAGER
:
712 ShowBookmarkManager(browser_
);
714 case IDC_SHOW_APP_MENU
:
715 content::RecordAction(base::UserMetricsAction("Accel_Show_App_Menu"));
716 ShowAppMenu(browser_
);
718 case IDC_SHOW_AVATAR_MENU
:
719 ShowAvatarMenu(browser_
);
721 case IDC_SHOW_HISTORY
:
722 ShowHistory(browser_
);
724 case IDC_SHOW_DOWNLOADS
:
725 ShowDownloads(browser_
);
727 case IDC_MANAGE_EXTENSIONS
:
728 ShowExtensions(browser_
, std::string());
731 ShowSettings(browser_
);
733 case IDC_EDIT_SEARCH_ENGINES
:
734 ShowSearchEngineSettings(browser_
);
736 case IDC_VIEW_PASSWORDS
:
737 ShowPasswordManager(browser_
);
739 case IDC_CLEAR_BROWSING_DATA
:
740 ShowClearBrowsingDataDialog(browser_
);
742 case IDC_IMPORT_SETTINGS
:
743 ShowImportDialog(browser_
);
745 case IDC_TOGGLE_REQUEST_TABLET_SITE
:
746 ToggleRequestTabletSite(browser_
);
749 ShowAboutChrome(browser_
);
751 case IDC_UPGRADE_DIALOG
:
752 OpenUpdateChromeDialog(browser_
);
754 case IDC_VIEW_INCOMPATIBILITIES
:
755 ShowConflicts(browser_
);
757 case IDC_HELP_PAGE_VIA_KEYBOARD
:
758 ShowHelp(browser_
, HELP_SOURCE_KEYBOARD
);
760 case IDC_HELP_PAGE_VIA_MENU
:
761 ShowHelp(browser_
, HELP_SOURCE_MENU
);
763 case IDC_SHOW_SIGNIN
:
764 ShowBrowserSignin(browser_
, signin::SOURCE_MENU
);
766 case IDC_TOGGLE_SPEECH_INPUT
:
767 ToggleSpeechInput(browser_
);
769 case IDC_DISTILL_PAGE
:
770 DistillCurrentPage(browser_
);
774 LOG(WARNING
) << "Received Unimplemented Command: " << id
;
779 ////////////////////////////////////////////////////////////////////////////////
780 // BrowserCommandController, SigninPrefObserver implementation:
782 void BrowserCommandController::OnSigninAllowedPrefChange() {
783 // For unit tests, we don't have a window.
786 UpdateShowSyncState(IsShowingMainUI());
789 // BrowserCommandController, TabStripModelObserver implementation:
791 void BrowserCommandController::TabInsertedAt(WebContents
* contents
,
794 AddInterstitialObservers(contents
);
797 void BrowserCommandController::TabDetachedAt(WebContents
* contents
, int index
) {
798 RemoveInterstitialObservers(contents
);
801 void BrowserCommandController::TabReplacedAt(TabStripModel
* tab_strip_model
,
802 WebContents
* old_contents
,
803 WebContents
* new_contents
,
805 RemoveInterstitialObservers(old_contents
);
806 AddInterstitialObservers(new_contents
);
809 void BrowserCommandController::TabBlockedStateChanged(
810 content::WebContents
* contents
,
812 PrintingStateChanged();
813 FullscreenStateChanged();
814 UpdateCommandsForFind();
817 ////////////////////////////////////////////////////////////////////////////////
818 // BrowserCommandController, TabRestoreServiceObserver implementation:
820 void BrowserCommandController::TabRestoreServiceChanged(
821 TabRestoreService
* service
) {
822 UpdateTabRestoreCommandState();
825 void BrowserCommandController::TabRestoreServiceDestroyed(
826 TabRestoreService
* service
) {
827 service
->RemoveObserver(this);
830 void BrowserCommandController::TabRestoreServiceLoaded(
831 TabRestoreService
* service
) {
832 UpdateTabRestoreCommandState();
835 ////////////////////////////////////////////////////////////////////////////////
836 // BrowserCommandController, private:
838 class BrowserCommandController::InterstitialObserver
839 : public content::WebContentsObserver
{
841 InterstitialObserver(BrowserCommandController
* controller
,
842 content::WebContents
* web_contents
)
843 : WebContentsObserver(web_contents
),
844 controller_(controller
) {
847 virtual void DidAttachInterstitialPage() OVERRIDE
{
848 controller_
->UpdateCommandsForTabState();
851 virtual void DidDetachInterstitialPage() OVERRIDE
{
852 controller_
->UpdateCommandsForTabState();
856 BrowserCommandController
* controller_
;
858 DISALLOW_COPY_AND_ASSIGN(InterstitialObserver
);
861 bool BrowserCommandController::IsShowingMainUI() {
862 bool should_hide_ui
= window() && window()->ShouldHideUIForFullscreen();
863 return browser_
->is_type_tabbed() && !should_hide_ui
;
866 void BrowserCommandController::InitCommandState() {
867 // All browser commands whose state isn't set automagically some other way
868 // (like Back & Forward with initial page load) must have their state
869 // initialized here, otherwise they will be forever disabled.
871 // Navigation commands
872 command_updater_
.UpdateCommandEnabled(IDC_RELOAD
, true);
873 command_updater_
.UpdateCommandEnabled(IDC_RELOAD_IGNORING_CACHE
, true);
874 command_updater_
.UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE
, true);
876 // Window management commands
877 command_updater_
.UpdateCommandEnabled(IDC_CLOSE_WINDOW
, true);
878 command_updater_
.UpdateCommandEnabled(IDC_NEW_TAB
, true);
879 command_updater_
.UpdateCommandEnabled(IDC_CLOSE_TAB
, true);
880 command_updater_
.UpdateCommandEnabled(IDC_DUPLICATE_TAB
, true);
881 UpdateTabRestoreCommandState();
882 #if defined(OS_WIN) && defined(USE_ASH)
883 if (browser_
->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH
)
884 command_updater_
.UpdateCommandEnabled(IDC_EXIT
, true);
886 command_updater_
.UpdateCommandEnabled(IDC_EXIT
, true);
888 command_updater_
.UpdateCommandEnabled(IDC_DEBUG_FRAME_TOGGLE
, true);
890 command_updater_
.UpdateCommandEnabled(IDC_MINIMIZE_WINDOW
, true);
892 #if defined(OS_CHROMEOS)
893 command_updater_
.UpdateCommandEnabled(IDC_VISIT_DESKTOP_OF_LRU_USER_2
, true);
894 command_updater_
.UpdateCommandEnabled(IDC_VISIT_DESKTOP_OF_LRU_USER_3
, true);
896 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
897 command_updater_
.UpdateCommandEnabled(IDC_USE_SYSTEM_TITLE_BAR
, true);
900 // Page-related commands
901 command_updater_
.UpdateCommandEnabled(IDC_EMAIL_PAGE_LOCATION
, true);
902 command_updater_
.UpdateCommandEnabled(IDC_MANAGE_PASSWORDS_FOR_PAGE
, true);
903 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_AUTO_DETECT
, true);
904 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_UTF8
, true);
905 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_UTF16LE
, true);
906 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88591
, true);
907 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1252
, true);
908 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_GBK
, true);
909 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_GB18030
, true);
910 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_BIG5HKSCS
, true);
911 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_BIG5
, true);
912 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_THAI
, true);
913 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_KOREAN
, true);
914 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_SHIFTJIS
, true);
915 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO2022JP
, true);
916 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_EUCJP
, true);
917 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO885915
, true);
918 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_MACINTOSH
, true);
919 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88592
, true);
920 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1250
, true);
921 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88595
, true);
922 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1251
, true);
923 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_KOI8R
, true);
924 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_KOI8U
, true);
925 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88597
, true);
926 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1253
, true);
927 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88594
, true);
928 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO885913
, true);
929 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1257
, true);
930 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88593
, true);
931 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO885910
, true);
932 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO885914
, true);
933 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO885916
, true);
934 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1254
, true);
935 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88596
, true);
936 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1256
, true);
937 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88598
, true);
938 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88598I
, true);
939 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1255
, true);
940 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1258
, true);
943 command_updater_
.UpdateCommandEnabled(IDC_ZOOM_MENU
, true);
944 command_updater_
.UpdateCommandEnabled(IDC_ZOOM_PLUS
, true);
945 command_updater_
.UpdateCommandEnabled(IDC_ZOOM_NORMAL
, false);
946 command_updater_
.UpdateCommandEnabled(IDC_ZOOM_MINUS
, true);
948 // Show various bits of UI
949 const bool guest_session
= profile()->IsGuestSession();
950 const bool normal_window
= browser_
->is_type_tabbed();
951 UpdateOpenFileState(&command_updater_
);
952 command_updater_
.UpdateCommandEnabled(IDC_CREATE_SHORTCUTS
, false);
953 UpdateCommandsForDevTools();
954 command_updater_
.UpdateCommandEnabled(IDC_TASK_MANAGER
, CanOpenTaskManager());
955 command_updater_
.UpdateCommandEnabled(IDC_SHOW_HISTORY
, !guest_session
);
956 command_updater_
.UpdateCommandEnabled(IDC_SHOW_DOWNLOADS
, true);
957 command_updater_
.UpdateCommandEnabled(IDC_HELP_MENU
, true);
958 command_updater_
.UpdateCommandEnabled(IDC_HELP_PAGE_VIA_KEYBOARD
, true);
959 command_updater_
.UpdateCommandEnabled(IDC_HELP_PAGE_VIA_MENU
, true);
960 command_updater_
.UpdateCommandEnabled(IDC_BOOKMARKS_MENU
, !guest_session
);
961 command_updater_
.UpdateCommandEnabled(IDC_RECENT_TABS_MENU
,
963 !profile()->IsOffTheRecord());
964 command_updater_
.UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA
, normal_window
);
965 #if defined(OS_CHROMEOS)
966 command_updater_
.UpdateCommandEnabled(IDC_TAKE_SCREENSHOT
, true);
968 // Chrome OS uses the system tray menu to handle multi-profiles.
969 if (normal_window
&& (guest_session
|| !profile()->IsOffTheRecord()))
970 command_updater_
.UpdateCommandEnabled(IDC_SHOW_AVATAR_MENU
, true);
973 UpdateShowSyncState(true);
975 // Navigation commands
976 command_updater_
.UpdateCommandEnabled(
978 normal_window
|| (extensions::util::IsStreamlinedHostedAppsEnabled() &&
979 browser_
->is_app()));
981 // Window management commands
982 command_updater_
.UpdateCommandEnabled(IDC_SELECT_NEXT_TAB
, normal_window
);
983 command_updater_
.UpdateCommandEnabled(IDC_SELECT_PREVIOUS_TAB
,
985 command_updater_
.UpdateCommandEnabled(IDC_MOVE_TAB_NEXT
, normal_window
);
986 command_updater_
.UpdateCommandEnabled(IDC_MOVE_TAB_PREVIOUS
, normal_window
);
987 command_updater_
.UpdateCommandEnabled(IDC_SELECT_TAB_0
, normal_window
);
988 command_updater_
.UpdateCommandEnabled(IDC_SELECT_TAB_1
, normal_window
);
989 command_updater_
.UpdateCommandEnabled(IDC_SELECT_TAB_2
, normal_window
);
990 command_updater_
.UpdateCommandEnabled(IDC_SELECT_TAB_3
, normal_window
);
991 command_updater_
.UpdateCommandEnabled(IDC_SELECT_TAB_4
, normal_window
);
992 command_updater_
.UpdateCommandEnabled(IDC_SELECT_TAB_5
, normal_window
);
993 command_updater_
.UpdateCommandEnabled(IDC_SELECT_TAB_6
, normal_window
);
994 command_updater_
.UpdateCommandEnabled(IDC_SELECT_TAB_7
, normal_window
);
995 command_updater_
.UpdateCommandEnabled(IDC_SELECT_LAST_TAB
, normal_window
);
997 bool metro
= browser_
->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH
;
998 command_updater_
.UpdateCommandEnabled(IDC_METRO_SNAP_ENABLE
, metro
);
999 command_updater_
.UpdateCommandEnabled(IDC_METRO_SNAP_DISABLE
, metro
);
1000 int restart_mode
= metro
? IDC_WIN_DESKTOP_RESTART
:
1001 (base::win::GetVersion() >= base::win::VERSION_WIN8
?
1002 IDC_WIN8_METRO_RESTART
: IDC_WIN_CHROMEOS_RESTART
);
1003 command_updater_
.UpdateCommandEnabled(restart_mode
, normal_window
);
1006 // These are always enabled; the menu determines their menu item visibility.
1007 command_updater_
.UpdateCommandEnabled(IDC_UPGRADE_DIALOG
, true);
1008 command_updater_
.UpdateCommandEnabled(IDC_VIEW_INCOMPATIBILITIES
, true);
1010 // Toggle speech input
1011 command_updater_
.UpdateCommandEnabled(IDC_TOGGLE_SPEECH_INPUT
, true);
1013 // Distill current page.
1014 command_updater_
.UpdateCommandEnabled(
1016 CommandLine::ForCurrentProcess()->HasSwitch(
1017 switches::kEnableDomDistiller
));
1019 // Initialize other commands whose state changes based on various conditions.
1020 UpdateCommandsForFullscreenMode();
1021 UpdateCommandsForContentRestrictionState();
1022 UpdateCommandsForBookmarkEditing();
1023 UpdateCommandsForIncognitoAvailability();
1027 void BrowserCommandController::UpdateSharedCommandsForIncognitoAvailability(
1028 CommandUpdater
* command_updater
,
1030 IncognitoModePrefs::Availability incognito_availability
=
1031 IncognitoModePrefs::GetAvailability(profile
->GetPrefs());
1032 command_updater
->UpdateCommandEnabled(
1034 incognito_availability
!= IncognitoModePrefs::FORCED
);
1035 command_updater
->UpdateCommandEnabled(
1036 IDC_NEW_INCOGNITO_WINDOW
,
1037 incognito_availability
!= IncognitoModePrefs::DISABLED
);
1039 const bool guest_session
= profile
->IsGuestSession();
1040 const bool forced_incognito
=
1041 incognito_availability
== IncognitoModePrefs::FORCED
||
1042 guest_session
; // Guest always runs in Incognito mode.
1043 command_updater
->UpdateCommandEnabled(
1044 IDC_SHOW_BOOKMARK_MANAGER
,
1045 browser_defaults::bookmarks_enabled
&& !forced_incognito
);
1046 ExtensionService
* extension_service
=
1047 extensions::ExtensionSystem::Get(profile
)->extension_service();
1048 const bool enable_extensions
=
1049 extension_service
&& extension_service
->extensions_enabled();
1051 // Bookmark manager and settings page/subpages are forced to open in normal
1052 // mode. For this reason we disable these commands when incognito is forced.
1053 command_updater
->UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS
,
1054 enable_extensions
&& !forced_incognito
);
1056 command_updater
->UpdateCommandEnabled(IDC_IMPORT_SETTINGS
, !forced_incognito
);
1057 command_updater
->UpdateCommandEnabled(IDC_OPTIONS
,
1058 !forced_incognito
|| guest_session
);
1059 command_updater
->UpdateCommandEnabled(IDC_SHOW_SIGNIN
, !forced_incognito
);
1062 void BrowserCommandController::UpdateCommandsForIncognitoAvailability() {
1063 UpdateSharedCommandsForIncognitoAvailability(&command_updater_
, profile());
1065 if (!IsShowingMainUI()) {
1066 command_updater_
.UpdateCommandEnabled(IDC_IMPORT_SETTINGS
, false);
1067 command_updater_
.UpdateCommandEnabled(IDC_OPTIONS
, false);
1071 void BrowserCommandController::UpdateCommandsForTabState() {
1072 WebContents
* current_web_contents
=
1073 browser_
->tab_strip_model()->GetActiveWebContents();
1074 if (!current_web_contents
) // May be NULL during tab restore.
1077 // Navigation commands
1078 command_updater_
.UpdateCommandEnabled(IDC_BACK
, CanGoBack(browser_
));
1079 command_updater_
.UpdateCommandEnabled(IDC_FORWARD
, CanGoForward(browser_
));
1080 command_updater_
.UpdateCommandEnabled(IDC_RELOAD
, CanReload(browser_
));
1081 command_updater_
.UpdateCommandEnabled(IDC_RELOAD_IGNORING_CACHE
,
1082 CanReload(browser_
));
1083 command_updater_
.UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE
,
1084 CanReload(browser_
));
1086 // Window management commands
1087 command_updater_
.UpdateCommandEnabled(IDC_DUPLICATE_TAB
,
1088 !browser_
->is_app() && CanDuplicateTab(browser_
));
1090 // Page-related commands
1091 window()->SetStarredState(
1092 BookmarkTabHelper::FromWebContents(current_web_contents
)->is_starred());
1093 window()->ZoomChangedForActiveTab(false);
1094 command_updater_
.UpdateCommandEnabled(IDC_VIEW_SOURCE
,
1095 CanViewSource(browser_
));
1096 command_updater_
.UpdateCommandEnabled(IDC_EMAIL_PAGE_LOCATION
,
1097 CanEmailPageLocation(browser_
));
1098 if (browser_
->is_devtools())
1099 command_updater_
.UpdateCommandEnabled(IDC_OPEN_FILE
, false);
1101 // Changing the encoding is not possible on Chrome-internal webpages.
1102 NavigationController
& nc
= current_web_contents
->GetController();
1103 bool is_chrome_internal
= HasInternalURL(nc
.GetLastCommittedEntry()) ||
1104 current_web_contents
->ShowingInterstitialPage();
1105 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_MENU
,
1106 !is_chrome_internal
&& current_web_contents
->IsSavable());
1108 // Show various bits of UI
1109 // TODO(pinkerton): Disable app-mode in the model until we implement it
1110 // on the Mac. Be sure to remove both ifdefs. http://crbug.com/13148
1111 #if !defined(OS_MACOSX)
1112 command_updater_
.UpdateCommandEnabled(
1113 IDC_CREATE_SHORTCUTS
,
1114 CanCreateApplicationShortcuts(browser_
));
1115 command_updater_
.UpdateCommandEnabled(IDC_CREATE_HOSTED_APP
,
1116 CanCreateBookmarkApp(browser_
));
1119 command_updater_
.UpdateCommandEnabled(
1120 IDC_TOGGLE_REQUEST_TABLET_SITE
,
1121 CanRequestTabletSite(current_web_contents
));
1123 UpdateCommandsForContentRestrictionState();
1124 UpdateCommandsForBookmarkEditing();
1125 UpdateCommandsForFind();
1126 // Update the zoom commands when an active tab is selected.
1127 UpdateCommandsForZoomState();
1130 void BrowserCommandController::UpdateCommandsForZoomState() {
1131 WebContents
* contents
=
1132 browser_
->tab_strip_model()->GetActiveWebContents();
1135 command_updater_
.UpdateCommandEnabled(IDC_ZOOM_PLUS
, CanZoomIn(contents
));
1136 command_updater_
.UpdateCommandEnabled(IDC_ZOOM_NORMAL
, ActualSize(contents
));
1137 command_updater_
.UpdateCommandEnabled(IDC_ZOOM_MINUS
, CanZoomOut(contents
));
1140 void BrowserCommandController::UpdateCommandsForContentRestrictionState() {
1141 int restrictions
= GetContentRestrictions(browser_
);
1143 command_updater_
.UpdateCommandEnabled(
1144 IDC_COPY
, !(restrictions
& CONTENT_RESTRICTION_COPY
));
1145 command_updater_
.UpdateCommandEnabled(
1146 IDC_CUT
, !(restrictions
& CONTENT_RESTRICTION_CUT
));
1147 command_updater_
.UpdateCommandEnabled(
1148 IDC_PASTE
, !(restrictions
& CONTENT_RESTRICTION_PASTE
));
1149 UpdateSaveAsState();
1150 UpdatePrintingState();
1153 void BrowserCommandController::UpdateCommandsForDevTools() {
1154 bool dev_tools_enabled
=
1155 !profile()->GetPrefs()->GetBoolean(prefs::kDevToolsDisabled
);
1156 command_updater_
.UpdateCommandEnabled(IDC_DEV_TOOLS
,
1158 command_updater_
.UpdateCommandEnabled(IDC_DEV_TOOLS_CONSOLE
,
1160 command_updater_
.UpdateCommandEnabled(IDC_DEV_TOOLS_DEVICES
,
1162 command_updater_
.UpdateCommandEnabled(IDC_DEV_TOOLS_INSPECT
,
1164 command_updater_
.UpdateCommandEnabled(IDC_DEV_TOOLS_TOGGLE
,
1168 void BrowserCommandController::UpdateCommandsForBookmarkEditing() {
1169 command_updater_
.UpdateCommandEnabled(IDC_BOOKMARK_PAGE
,
1170 CanBookmarkCurrentPage(browser_
));
1171 command_updater_
.UpdateCommandEnabled(IDC_BOOKMARK_ALL_TABS
,
1172 CanBookmarkAllTabs(browser_
));
1173 command_updater_
.UpdateCommandEnabled(IDC_PIN_TO_START_SCREEN
,
1177 void BrowserCommandController::UpdateCommandsForBookmarkBar() {
1178 command_updater_
.UpdateCommandEnabled(
1179 IDC_SHOW_BOOKMARK_BAR
,
1180 browser_defaults::bookmarks_enabled
&& !profile()->IsGuestSession() &&
1181 !profile()->GetPrefs()->IsManagedPreference(
1182 bookmarks::prefs::kShowBookmarkBar
) &&
1186 void BrowserCommandController::UpdateCommandsForFileSelectionDialogs() {
1187 UpdateSaveAsState();
1188 UpdateOpenFileState(&command_updater_
);
1191 void BrowserCommandController::UpdateCommandsForFullscreenMode() {
1192 WindowState window_state
= WINDOW_STATE_NOT_FULLSCREEN
;
1193 if (window() && window()->IsFullscreen()) {
1194 window_state
= WINDOW_STATE_FULLSCREEN
;
1196 if (window()->IsInMetroSnapMode())
1197 window_state
= WINDOW_STATE_METRO_SNAP
;
1200 bool show_main_ui
= IsShowingMainUI();
1201 bool main_not_fullscreen
=
1202 show_main_ui
&& window_state
== WINDOW_STATE_NOT_FULLSCREEN
;
1204 // Navigation commands
1205 command_updater_
.UpdateCommandEnabled(IDC_OPEN_CURRENT_URL
, show_main_ui
);
1207 // Window management commands
1208 command_updater_
.UpdateCommandEnabled(
1210 !browser_
->is_type_tabbed() &&
1211 window_state
== WINDOW_STATE_NOT_FULLSCREEN
);
1213 // Focus various bits of UI
1214 command_updater_
.UpdateCommandEnabled(IDC_FOCUS_TOOLBAR
, show_main_ui
);
1215 command_updater_
.UpdateCommandEnabled(IDC_FOCUS_LOCATION
, show_main_ui
);
1216 command_updater_
.UpdateCommandEnabled(IDC_FOCUS_SEARCH
, show_main_ui
);
1217 command_updater_
.UpdateCommandEnabled(
1218 IDC_FOCUS_MENU_BAR
, main_not_fullscreen
);
1219 command_updater_
.UpdateCommandEnabled(
1220 IDC_FOCUS_NEXT_PANE
, main_not_fullscreen
);
1221 command_updater_
.UpdateCommandEnabled(
1222 IDC_FOCUS_PREVIOUS_PANE
, main_not_fullscreen
);
1223 command_updater_
.UpdateCommandEnabled(
1224 IDC_FOCUS_BOOKMARKS
, main_not_fullscreen
);
1225 command_updater_
.UpdateCommandEnabled(
1226 IDC_FOCUS_INFOBARS
, main_not_fullscreen
);
1228 // Show various bits of UI
1229 command_updater_
.UpdateCommandEnabled(IDC_DEVELOPER_MENU
, show_main_ui
);
1230 #if defined(GOOGLE_CHROME_BUILD)
1231 command_updater_
.UpdateCommandEnabled(IDC_FEEDBACK
, show_main_ui
);
1233 UpdateShowSyncState(show_main_ui
);
1235 // Settings page/subpages are forced to open in normal mode. We disable these
1236 // commands for guest sessions and when incognito is forced.
1237 const bool options_enabled
= show_main_ui
&&
1238 IncognitoModePrefs::GetAvailability(
1239 profile()->GetPrefs()) != IncognitoModePrefs::FORCED
;
1240 const bool guest_session
= profile()->IsGuestSession();
1241 command_updater_
.UpdateCommandEnabled(IDC_OPTIONS
, options_enabled
);
1242 command_updater_
.UpdateCommandEnabled(IDC_IMPORT_SETTINGS
,
1243 options_enabled
&& !guest_session
);
1245 command_updater_
.UpdateCommandEnabled(IDC_EDIT_SEARCH_ENGINES
, show_main_ui
);
1246 command_updater_
.UpdateCommandEnabled(IDC_VIEW_PASSWORDS
, show_main_ui
);
1247 command_updater_
.UpdateCommandEnabled(IDC_ABOUT
, show_main_ui
);
1248 command_updater_
.UpdateCommandEnabled(IDC_SHOW_APP_MENU
, show_main_ui
);
1249 #if defined (ENABLE_PROFILING) && !defined(NO_TCMALLOC)
1250 command_updater_
.UpdateCommandEnabled(IDC_PROFILING_ENABLED
, show_main_ui
);
1253 // Disable explicit fullscreen toggling when in metro snap mode.
1254 bool fullscreen_enabled
= window_state
!= WINDOW_STATE_METRO_SNAP
;
1255 #if !defined(OS_MACOSX)
1256 if (window_state
== WINDOW_STATE_NOT_FULLSCREEN
&&
1257 !profile()->GetPrefs()->GetBoolean(prefs::kFullscreenAllowed
)) {
1258 // Disable toggling into fullscreen mode if disallowed by pref.
1259 fullscreen_enabled
= false;
1263 command_updater_
.UpdateCommandEnabled(IDC_FULLSCREEN
, fullscreen_enabled
);
1264 command_updater_
.UpdateCommandEnabled(IDC_PRESENTATION_MODE
,
1265 fullscreen_enabled
);
1267 UpdateCommandsForBookmarkBar();
1270 void BrowserCommandController::UpdatePrintingState() {
1271 bool print_enabled
= CanPrint(browser_
);
1272 command_updater_
.UpdateCommandEnabled(IDC_PRINT
, print_enabled
);
1273 #if !defined(DISABLE_BASIC_PRINTING)
1274 command_updater_
.UpdateCommandEnabled(IDC_BASIC_PRINT
,
1275 CanBasicPrint(browser_
));
1276 #endif // !DISABLE_BASIC_PRINTING
1279 void BrowserCommandController::UpdateSaveAsState() {
1280 command_updater_
.UpdateCommandEnabled(IDC_SAVE_PAGE
, CanSavePage(browser_
));
1283 void BrowserCommandController::UpdateShowSyncState(bool show_main_ui
) {
1284 command_updater_
.UpdateCommandEnabled(
1285 IDC_SHOW_SYNC_SETUP
, show_main_ui
&& pref_signin_allowed_
.GetValue());
1289 void BrowserCommandController::UpdateOpenFileState(
1290 CommandUpdater
* command_updater
) {
1291 bool enabled
= true;
1292 PrefService
* local_state
= g_browser_process
->local_state();
1294 enabled
= local_state
->GetBoolean(prefs::kAllowFileSelectionDialogs
);
1296 command_updater
->UpdateCommandEnabled(IDC_OPEN_FILE
, enabled
);
1299 void BrowserCommandController::UpdateReloadStopState(bool is_loading
,
1301 window()->UpdateReloadStopState(is_loading
, force
);
1302 command_updater_
.UpdateCommandEnabled(IDC_STOP
, is_loading
);
1305 void BrowserCommandController::UpdateTabRestoreCommandState() {
1306 TabRestoreService
* tab_restore_service
=
1307 TabRestoreServiceFactory::GetForProfile(profile());
1308 // The command is enabled if the service hasn't loaded yet to trigger loading.
1309 // The command is updated once the load completes.
1310 command_updater_
.UpdateCommandEnabled(
1312 tab_restore_service
&&
1313 (!tab_restore_service
->IsLoaded() ||
1314 GetRestoreTabType(browser_
) != TabStripModelDelegate::RESTORE_NONE
));
1317 void BrowserCommandController::UpdateCommandsForFind() {
1318 TabStripModel
* model
= browser_
->tab_strip_model();
1319 bool enabled
= !model
->IsTabBlocked(model
->active_index()) &&
1320 !browser_
->is_devtools();
1322 command_updater_
.UpdateCommandEnabled(IDC_FIND
, enabled
);
1323 command_updater_
.UpdateCommandEnabled(IDC_FIND_NEXT
, enabled
);
1324 command_updater_
.UpdateCommandEnabled(IDC_FIND_PREVIOUS
, enabled
);
1327 void BrowserCommandController::AddInterstitialObservers(WebContents
* contents
) {
1328 interstitial_observers_
.push_back(new InterstitialObserver(this, contents
));
1331 void BrowserCommandController::RemoveInterstitialObservers(
1332 WebContents
* contents
) {
1333 for (size_t i
= 0; i
< interstitial_observers_
.size(); i
++) {
1334 if (interstitial_observers_
[i
]->web_contents() != contents
)
1337 delete interstitial_observers_
[i
];
1338 interstitial_observers_
.erase(interstitial_observers_
.begin() + i
);
1343 BrowserWindow
* BrowserCommandController::window() {
1344 return browser_
->window();
1347 Profile
* BrowserCommandController::profile() {
1348 return browser_
->profile();
1351 } // namespace chrome