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/lifetime/application_lifetime.h"
15 #include "chrome/browser/prefs/incognito_mode_prefs.h"
16 #include "chrome/browser/profiles/avatar_menu.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/sessions/tab_restore_service.h"
20 #include "chrome/browser/sessions/tab_restore_service_factory.h"
21 #include "chrome/browser/shell_integration.h"
22 #include "chrome/browser/signin/signin_promo.h"
23 #include "chrome/browser/sync/profile_sync_service.h"
24 #include "chrome/browser/sync/profile_sync_service_factory.h"
25 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
26 #include "chrome/browser/ui/browser.h"
27 #include "chrome/browser/ui/browser_commands.h"
28 #include "chrome/browser/ui/browser_window.h"
29 #include "chrome/browser/ui/chrome_pages.h"
30 #include "chrome/browser/ui/tabs/tab_strip_model.h"
31 #include "chrome/browser/ui/tabs/tab_strip_model_utils.h"
32 #include "chrome/browser/ui/webui/inspect_ui.h"
33 #include "chrome/common/chrome_switches.h"
34 #include "chrome/common/content_restriction.h"
35 #include "chrome/common/pref_names.h"
36 #include "chrome/common/profiling.h"
37 #include "content/public/browser/native_web_keyboard_event.h"
38 #include "content/public/browser/navigation_controller.h"
39 #include "content/public/browser/navigation_entry.h"
40 #include "content/public/browser/user_metrics.h"
41 #include "content/public/browser/web_contents.h"
42 #include "content/public/browser/web_contents_observer.h"
43 #include "content/public/common/url_constants.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(
188 ProfileManager
* profile_manager
)
190 profile_manager_(profile_manager
),
191 command_updater_(this),
192 block_command_execution_(false),
193 last_blocked_command_id_(-1),
194 last_blocked_command_disposition_(CURRENT_TAB
) {
195 if (profile_manager_
)
196 profile_manager_
->GetProfileInfoCache().AddObserver(this);
197 browser_
->tab_strip_model()->AddObserver(this);
198 PrefService
* local_state
= g_browser_process
->local_state();
200 local_pref_registrar_
.Init(local_state
);
201 local_pref_registrar_
.Add(
202 prefs::kAllowFileSelectionDialogs
,
204 &BrowserCommandController::UpdateCommandsForFileSelectionDialogs
,
205 base::Unretained(this)));
208 profile_pref_registrar_
.Init(profile()->GetPrefs());
209 profile_pref_registrar_
.Add(
210 prefs::kDevToolsDisabled
,
211 base::Bind(&BrowserCommandController::UpdateCommandsForDevTools
,
212 base::Unretained(this)));
213 profile_pref_registrar_
.Add(
214 prefs::kEditBookmarksEnabled
,
215 base::Bind(&BrowserCommandController::UpdateCommandsForBookmarkEditing
,
216 base::Unretained(this)));
217 profile_pref_registrar_
.Add(
218 prefs::kShowBookmarkBar
,
219 base::Bind(&BrowserCommandController::UpdateCommandsForBookmarkBar
,
220 base::Unretained(this)));
221 profile_pref_registrar_
.Add(
222 prefs::kIncognitoModeAvailability
,
224 &BrowserCommandController::UpdateCommandsForIncognitoAvailability
,
225 base::Unretained(this)));
226 profile_pref_registrar_
.Add(
227 prefs::kPrintingEnabled
,
228 base::Bind(&BrowserCommandController::UpdatePrintingState
,
229 base::Unretained(this)));
230 #if !defined(OS_MACOSX)
231 profile_pref_registrar_
.Add(
232 prefs::kFullscreenAllowed
,
233 base::Bind(&BrowserCommandController::UpdateCommandsForFullscreenMode
,
234 base::Unretained(this)));
236 pref_signin_allowed_
.Init(
237 prefs::kSigninAllowed
,
238 profile()->GetOriginalProfile()->GetPrefs(),
239 base::Bind(&BrowserCommandController::OnSigninAllowedPrefChange
,
240 base::Unretained(this)));
244 TabRestoreService
* tab_restore_service
=
245 TabRestoreServiceFactory::GetForProfile(profile());
246 if (tab_restore_service
) {
247 tab_restore_service
->AddObserver(this);
248 TabRestoreServiceChanged(tab_restore_service
);
252 BrowserCommandController::~BrowserCommandController() {
253 // TabRestoreService may have been shutdown by the time we get here. Don't
254 // trigger creating it.
255 TabRestoreService
* tab_restore_service
=
256 TabRestoreServiceFactory::GetForProfileIfExisting(profile());
257 if (tab_restore_service
)
258 tab_restore_service
->RemoveObserver(this);
259 profile_pref_registrar_
.RemoveAll();
260 local_pref_registrar_
.RemoveAll();
261 browser_
->tab_strip_model()->RemoveObserver(this);
262 if (profile_manager_
)
263 profile_manager_
->GetProfileInfoCache().RemoveObserver(this);
266 bool BrowserCommandController::IsReservedCommandOrKey(
268 const content::NativeWebKeyboardEvent
& event
) {
269 // In Apps mode, no keys are reserved.
270 if (browser_
->is_app())
273 #if defined(OS_CHROMEOS)
274 // On Chrome OS, the top row of keys are mapped to browser actions like
275 // back/forward or refresh. We don't want web pages to be able to change the
276 // behavior of these keys. Ash handles F4 and up; this leaves us needing to
277 // reserve browser back/forward and refresh here.
278 ui::KeyboardCode key_code
=
279 static_cast<ui::KeyboardCode
>(event
.windowsKeyCode
);
280 if ((key_code
== ui::VKEY_BROWSER_BACK
&& command_id
== IDC_BACK
) ||
281 (key_code
== ui::VKEY_BROWSER_FORWARD
&& command_id
== IDC_FORWARD
) ||
282 (key_code
== ui::VKEY_BROWSER_REFRESH
&& command_id
== IDC_RELOAD
)) {
287 if (window()->IsFullscreen() && command_id
== IDC_FULLSCREEN
)
290 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
291 // If this key was registered by the user as a content editing hotkey, then
292 // it is not reserved.
293 ui::TextEditKeyBindingsDelegateAuraLinux
* delegate
=
294 ui::GetTextEditKeyBindingsDelegate();
295 if (delegate
&& event
.os_event
&& delegate
->MatchEvent(*event
.os_event
, NULL
))
299 return command_id
== IDC_CLOSE_TAB
||
300 command_id
== IDC_CLOSE_WINDOW
||
301 command_id
== IDC_NEW_INCOGNITO_WINDOW
||
302 command_id
== IDC_NEW_TAB
||
303 command_id
== IDC_NEW_WINDOW
||
304 command_id
== IDC_RESTORE_TAB
||
305 command_id
== IDC_SELECT_NEXT_TAB
||
306 command_id
== IDC_SELECT_PREVIOUS_TAB
||
307 command_id
== IDC_EXIT
;
310 void BrowserCommandController::SetBlockCommandExecution(bool block
) {
311 block_command_execution_
= block
;
313 last_blocked_command_id_
= -1;
314 last_blocked_command_disposition_
= CURRENT_TAB
;
318 int BrowserCommandController::GetLastBlockedCommand(
319 WindowOpenDisposition
* disposition
) {
321 *disposition
= last_blocked_command_disposition_
;
322 return last_blocked_command_id_
;
325 void BrowserCommandController::TabStateChanged() {
326 UpdateCommandsForTabState();
329 void BrowserCommandController::ContentRestrictionsChanged() {
330 UpdateCommandsForContentRestrictionState();
333 void BrowserCommandController::FullscreenStateChanged() {
334 UpdateCommandsForFullscreenMode();
337 void BrowserCommandController::PrintingStateChanged() {
338 UpdatePrintingState();
341 void BrowserCommandController::LoadingStateChanged(bool is_loading
,
343 UpdateReloadStopState(is_loading
, force
);
346 ////////////////////////////////////////////////////////////////////////////////
347 // BrowserCommandController, CommandUpdaterDelegate implementation:
349 void BrowserCommandController::ExecuteCommandWithDisposition(
351 WindowOpenDisposition disposition
) {
352 // No commands are enabled if there is not yet any selected tab.
353 // TODO(pkasting): It seems like we should not need this, because either
354 // most/all commands should not have been enabled yet anyway or the ones that
355 // are enabled should be global, or safe themselves against having no selected
356 // tab. However, Ben says he tried removing this before and got lots of
357 // crashes, e.g. from Windows sending WM_COMMANDs at random times during
358 // window construction. This probably could use closer examination someday.
359 if (browser_
->tab_strip_model()->active_index() == TabStripModel::kNoTab
)
362 DCHECK(command_updater_
.IsCommandEnabled(id
)) << "Invalid/disabled command "
365 // If command execution is blocked then just record the command and return.
366 if (block_command_execution_
) {
367 // We actually only allow no more than one blocked command, otherwise some
368 // commands maybe lost.
369 DCHECK_EQ(last_blocked_command_id_
, -1);
370 last_blocked_command_id_
= id
;
371 last_blocked_command_disposition_
= disposition
;
375 // The order of commands in this switch statement must match the function
376 // declaration order in browser.h!
378 // Navigation commands
380 GoBack(browser_
, disposition
);
383 GoForward(browser_
, disposition
);
386 Reload(browser_
, disposition
);
388 case IDC_RELOAD_CLEARING_CACHE
:
389 ClearCache(browser_
);
391 case IDC_RELOAD_IGNORING_CACHE
:
392 ReloadIgnoringCache(browser_
, disposition
);
395 Home(browser_
, disposition
);
397 case IDC_OPEN_CURRENT_URL
:
398 OpenCurrentURL(browser_
);
404 // Window management commands
408 case IDC_NEW_INCOGNITO_WINDOW
:
409 NewIncognitoWindow(browser_
);
411 case IDC_CLOSE_WINDOW
:
412 content::RecordAction(base::UserMetricsAction("CloseWindowByKey"));
413 CloseWindow(browser_
);
419 content::RecordAction(base::UserMetricsAction("CloseTabByKey"));
422 case IDC_SELECT_NEXT_TAB
:
423 content::RecordAction(base::UserMetricsAction("Accel_SelectNextTab"));
424 SelectNextTab(browser_
);
426 case IDC_SELECT_PREVIOUS_TAB
:
427 content::RecordAction(
428 base::UserMetricsAction("Accel_SelectPreviousTab"));
429 SelectPreviousTab(browser_
);
431 case IDC_MOVE_TAB_NEXT
:
432 MoveTabNext(browser_
);
434 case IDC_MOVE_TAB_PREVIOUS
:
435 MoveTabPrevious(browser_
);
437 case IDC_SELECT_TAB_0
:
438 case IDC_SELECT_TAB_1
:
439 case IDC_SELECT_TAB_2
:
440 case IDC_SELECT_TAB_3
:
441 case IDC_SELECT_TAB_4
:
442 case IDC_SELECT_TAB_5
:
443 case IDC_SELECT_TAB_6
:
444 case IDC_SELECT_TAB_7
:
445 SelectNumberedTab(browser_
, id
- IDC_SELECT_TAB_0
);
447 case IDC_SELECT_LAST_TAB
:
448 SelectLastTab(browser_
);
450 case IDC_DUPLICATE_TAB
:
451 DuplicateTab(browser_
);
453 case IDC_RESTORE_TAB
:
454 RestoreTab(browser_
);
456 case IDC_SHOW_AS_TAB
:
457 ConvertPopupToTabbedBrowser(browser_
);
460 #if defined(OS_MACOSX)
461 chrome::ToggleFullscreenWithChromeOrFallback(browser_
);
463 chrome::ToggleFullscreenMode(browser_
);
468 case IDC_TOGGLE_ASH_DESKTOP
:
469 chrome::ToggleAshDesktop();
471 case IDC_MINIMIZE_WINDOW
:
472 content::RecordAction(
473 base::UserMetricsAction("Accel_Toggle_Minimized_M"));
474 ash::accelerators::ToggleMinimized();
476 // If Ash needs many more commands here we should implement a general
477 // mechanism to pass accelerators back into Ash. http://crbug.com/285308
480 #if defined(OS_CHROMEOS)
481 case IDC_VISIT_DESKTOP_OF_LRU_USER_2
:
482 case IDC_VISIT_DESKTOP_OF_LRU_USER_3
:
483 ExecuteVisitDesktopCommand(id
, browser_
->window()->GetNativeWindow());
487 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
488 case IDC_USE_SYSTEM_TITLE_BAR
: {
489 PrefService
* prefs
= browser_
->profile()->GetPrefs();
490 prefs
->SetBoolean(prefs::kUseCustomChromeFrame
,
491 !prefs
->GetBoolean(prefs::kUseCustomChromeFrame
));
497 // Windows 8 specific commands.
498 case IDC_METRO_SNAP_ENABLE
:
499 browser_
->SetMetroSnapMode(true);
501 case IDC_METRO_SNAP_DISABLE
:
502 browser_
->SetMetroSnapMode(false);
504 case IDC_WIN8_DESKTOP_RESTART
:
505 if (!VerifyMetroSwitchForApps(window()->GetNativeWindow(), id
))
508 chrome::AttemptRestartToDesktopMode();
509 content::RecordAction(base::UserMetricsAction("Win8DesktopRestart"));
511 case IDC_WIN8_METRO_RESTART
:
512 if (!VerifyMetroSwitchForApps(window()->GetNativeWindow(), id
))
515 // SwitchToMetroUIHandler deletes itself.
516 new SwitchToMetroUIHandler
;
517 content::RecordAction(base::UserMetricsAction("Win8MetroRestart"));
521 #if defined(OS_MACOSX)
522 case IDC_PRESENTATION_MODE
:
523 chrome::ToggleFullscreenMode(browser_
);
530 // Page-related commands
534 case IDC_BOOKMARK_PAGE
:
535 BookmarkCurrentPage(browser_
);
537 case IDC_PIN_TO_START_SCREEN
:
538 TogglePagePinnedToStartScreen(browser_
);
540 case IDC_BOOKMARK_ALL_TABS
:
541 BookmarkAllTabs(browser_
);
543 case IDC_VIEW_SOURCE
:
544 ViewSelectedSource(browser_
);
546 case IDC_EMAIL_PAGE_LOCATION
:
547 EmailPageLocation(browser_
);
552 case IDC_ADVANCED_PRINT
:
553 content::RecordAction(base::UserMetricsAction("Accel_Advanced_Print"));
554 AdvancedPrint(browser_
);
556 case IDC_PRINT_TO_DESTINATION
:
557 PrintToDestination(browser_
);
559 case IDC_TRANSLATE_PAGE
:
562 case IDC_ENCODING_AUTO_DETECT
:
563 browser_
->ToggleEncodingAutoDetect();
565 case IDC_ENCODING_UTF8
:
566 case IDC_ENCODING_UTF16LE
:
567 case IDC_ENCODING_ISO88591
:
568 case IDC_ENCODING_WINDOWS1252
:
569 case IDC_ENCODING_GBK
:
570 case IDC_ENCODING_GB18030
:
571 case IDC_ENCODING_BIG5HKSCS
:
572 case IDC_ENCODING_BIG5
:
573 case IDC_ENCODING_KOREAN
:
574 case IDC_ENCODING_SHIFTJIS
:
575 case IDC_ENCODING_ISO2022JP
:
576 case IDC_ENCODING_EUCJP
:
577 case IDC_ENCODING_THAI
:
578 case IDC_ENCODING_ISO885915
:
579 case IDC_ENCODING_MACINTOSH
:
580 case IDC_ENCODING_ISO88592
:
581 case IDC_ENCODING_WINDOWS1250
:
582 case IDC_ENCODING_ISO88595
:
583 case IDC_ENCODING_WINDOWS1251
:
584 case IDC_ENCODING_KOI8R
:
585 case IDC_ENCODING_KOI8U
:
586 case IDC_ENCODING_ISO88597
:
587 case IDC_ENCODING_WINDOWS1253
:
588 case IDC_ENCODING_ISO88594
:
589 case IDC_ENCODING_ISO885913
:
590 case IDC_ENCODING_WINDOWS1257
:
591 case IDC_ENCODING_ISO88593
:
592 case IDC_ENCODING_ISO885910
:
593 case IDC_ENCODING_ISO885914
:
594 case IDC_ENCODING_ISO885916
:
595 case IDC_ENCODING_WINDOWS1254
:
596 case IDC_ENCODING_ISO88596
:
597 case IDC_ENCODING_WINDOWS1256
:
598 case IDC_ENCODING_ISO88598
:
599 case IDC_ENCODING_ISO88598I
:
600 case IDC_ENCODING_WINDOWS1255
:
601 case IDC_ENCODING_WINDOWS1258
:
602 browser_
->OverrideEncoding(id
);
605 // Clipboard commands
623 case IDC_FIND_PREVIOUS
:
624 FindPrevious(browser_
);
629 Zoom(browser_
, content::PAGE_ZOOM_IN
);
631 case IDC_ZOOM_NORMAL
:
632 Zoom(browser_
, content::PAGE_ZOOM_RESET
);
635 Zoom(browser_
, content::PAGE_ZOOM_OUT
);
638 // Focus various bits of UI
639 case IDC_FOCUS_TOOLBAR
:
640 content::RecordAction(base::UserMetricsAction("Accel_Focus_Toolbar"));
641 FocusToolbar(browser_
);
643 case IDC_FOCUS_LOCATION
:
644 content::RecordAction(base::UserMetricsAction("Accel_Focus_Location"));
645 FocusLocationBar(browser_
);
647 case IDC_FOCUS_SEARCH
:
648 content::RecordAction(base::UserMetricsAction("Accel_Focus_Search"));
649 FocusSearch(browser_
);
651 case IDC_FOCUS_MENU_BAR
:
652 FocusAppMenu(browser_
);
654 case IDC_FOCUS_BOOKMARKS
:
655 content::RecordAction(
656 base::UserMetricsAction("Accel_Focus_Bookmarks"));
657 FocusBookmarksToolbar(browser_
);
659 case IDC_FOCUS_INFOBARS
:
660 FocusInfobars(browser_
);
662 case IDC_FOCUS_NEXT_PANE
:
663 FocusNextPane(browser_
);
665 case IDC_FOCUS_PREVIOUS_PANE
:
666 FocusPreviousPane(browser_
);
669 // Show various bits of UI
671 browser_
->OpenFile();
673 case IDC_CREATE_SHORTCUTS
:
674 CreateApplicationShortcuts(browser_
);
676 case IDC_CREATE_HOSTED_APP
:
677 CreateBookmarkAppFromCurrentWebContents(browser_
);
680 ToggleDevToolsWindow(browser_
, DevToolsToggleAction::Show());
682 case IDC_DEV_TOOLS_CONSOLE
:
683 ToggleDevToolsWindow(browser_
, DevToolsToggleAction::ShowConsole());
685 case IDC_DEV_TOOLS_DEVICES
:
686 InspectUI::InspectDevices(browser_
);
688 case IDC_DEV_TOOLS_INSPECT
:
689 ToggleDevToolsWindow(browser_
, DevToolsToggleAction::Inspect());
691 case IDC_DEV_TOOLS_TOGGLE
:
692 ToggleDevToolsWindow(browser_
, DevToolsToggleAction::Toggle());
694 case IDC_TASK_MANAGER
:
695 OpenTaskManager(browser_
);
697 #if defined(OS_CHROMEOS)
698 case IDC_TAKE_SCREENSHOT
:
702 #if defined(GOOGLE_CHROME_BUILD)
704 OpenFeedbackDialog(browser_
);
707 case IDC_SHOW_BOOKMARK_BAR
:
708 ToggleBookmarkBar(browser_
);
710 case IDC_PROFILING_ENABLED
:
714 case IDC_SHOW_BOOKMARK_MANAGER
:
715 ShowBookmarkManager(browser_
);
717 case IDC_SHOW_APP_MENU
:
718 content::RecordAction(base::UserMetricsAction("Accel_Show_App_Menu"));
719 ShowAppMenu(browser_
);
721 case IDC_SHOW_AVATAR_MENU
:
722 ShowAvatarMenu(browser_
);
724 case IDC_SHOW_HISTORY
:
725 ShowHistory(browser_
);
727 case IDC_SHOW_DOWNLOADS
:
728 ShowDownloads(browser_
);
730 case IDC_MANAGE_EXTENSIONS
:
731 ShowExtensions(browser_
, std::string());
734 ShowSettings(browser_
);
736 case IDC_EDIT_SEARCH_ENGINES
:
737 ShowSearchEngineSettings(browser_
);
739 case IDC_VIEW_PASSWORDS
:
740 ShowPasswordManager(browser_
);
742 case IDC_CLEAR_BROWSING_DATA
:
743 ShowClearBrowsingDataDialog(browser_
);
745 case IDC_IMPORT_SETTINGS
:
746 ShowImportDialog(browser_
);
748 case IDC_TOGGLE_REQUEST_TABLET_SITE
:
749 ToggleRequestTabletSite(browser_
);
752 ShowAboutChrome(browser_
);
754 case IDC_UPGRADE_DIALOG
:
755 OpenUpdateChromeDialog(browser_
);
757 case IDC_VIEW_INCOMPATIBILITIES
:
758 ShowConflicts(browser_
);
760 case IDC_HELP_PAGE_VIA_KEYBOARD
:
761 ShowHelp(browser_
, HELP_SOURCE_KEYBOARD
);
763 case IDC_HELP_PAGE_VIA_MENU
:
764 ShowHelp(browser_
, HELP_SOURCE_MENU
);
766 case IDC_SHOW_SIGNIN
:
767 ShowBrowserSignin(browser_
, signin::SOURCE_MENU
);
769 case IDC_TOGGLE_SPEECH_INPUT
:
770 ToggleSpeechInput(browser_
);
774 LOG(WARNING
) << "Received Unimplemented Command: " << id
;
779 ////////////////////////////////////////////////////////////////////////////////
780 // BrowserCommandController, ProfileInfoCacheObserver implementation:
782 void BrowserCommandController::OnProfileAdded(
783 const base::FilePath
& profile_path
) {
784 UpdateCommandsForMultipleProfiles();
787 void BrowserCommandController::OnProfileWasRemoved(
788 const base::FilePath
& profile_path
,
789 const base::string16
& profile_name
) {
790 UpdateCommandsForMultipleProfiles();
793 ////////////////////////////////////////////////////////////////////////////////
794 // BrowserCommandController, SigninPrefObserver implementation:
796 void BrowserCommandController::OnSigninAllowedPrefChange() {
797 // For unit tests, we don't have a window.
800 UpdateShowSyncState(IsShowingMainUI());
803 // BrowserCommandController, TabStripModelObserver implementation:
805 void BrowserCommandController::TabInsertedAt(WebContents
* contents
,
808 AddInterstitialObservers(contents
);
811 void BrowserCommandController::TabDetachedAt(WebContents
* contents
, int index
) {
812 RemoveInterstitialObservers(contents
);
815 void BrowserCommandController::TabReplacedAt(TabStripModel
* tab_strip_model
,
816 WebContents
* old_contents
,
817 WebContents
* new_contents
,
819 RemoveInterstitialObservers(old_contents
);
820 AddInterstitialObservers(new_contents
);
823 void BrowserCommandController::TabBlockedStateChanged(
824 content::WebContents
* contents
,
826 PrintingStateChanged();
827 FullscreenStateChanged();
828 UpdateCommandsForFind();
831 ////////////////////////////////////////////////////////////////////////////////
832 // BrowserCommandController, TabRestoreServiceObserver implementation:
834 void BrowserCommandController::TabRestoreServiceChanged(
835 TabRestoreService
* service
) {
836 command_updater_
.UpdateCommandEnabled(
838 GetRestoreTabType(browser_
) != TabStripModelDelegate::RESTORE_NONE
);
841 void BrowserCommandController::TabRestoreServiceDestroyed(
842 TabRestoreService
* service
) {
843 service
->RemoveObserver(this);
846 ////////////////////////////////////////////////////////////////////////////////
847 // BrowserCommandController, private:
849 class BrowserCommandController::InterstitialObserver
850 : public content::WebContentsObserver
{
852 InterstitialObserver(BrowserCommandController
* controller
,
853 content::WebContents
* web_contents
)
854 : WebContentsObserver(web_contents
),
855 controller_(controller
) {
858 using content::WebContentsObserver::web_contents
;
860 virtual void DidAttachInterstitialPage() OVERRIDE
{
861 controller_
->UpdateCommandsForTabState();
864 virtual void DidDetachInterstitialPage() OVERRIDE
{
865 controller_
->UpdateCommandsForTabState();
869 BrowserCommandController
* controller_
;
871 DISALLOW_COPY_AND_ASSIGN(InterstitialObserver
);
874 bool BrowserCommandController::IsShowingMainUI() {
875 bool should_hide_ui
= window() && window()->ShouldHideUIForFullscreen();
876 return browser_
->is_type_tabbed() && !should_hide_ui
;
879 void BrowserCommandController::InitCommandState() {
880 // All browser commands whose state isn't set automagically some other way
881 // (like Back & Forward with initial page load) must have their state
882 // initialized here, otherwise they will be forever disabled.
884 // Navigation commands
885 command_updater_
.UpdateCommandEnabled(IDC_RELOAD
, true);
886 command_updater_
.UpdateCommandEnabled(IDC_RELOAD_IGNORING_CACHE
, true);
887 command_updater_
.UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE
, true);
889 // Window management commands
890 command_updater_
.UpdateCommandEnabled(IDC_CLOSE_WINDOW
, true);
891 command_updater_
.UpdateCommandEnabled(IDC_NEW_TAB
, true);
892 command_updater_
.UpdateCommandEnabled(IDC_CLOSE_TAB
, true);
893 command_updater_
.UpdateCommandEnabled(IDC_DUPLICATE_TAB
, true);
894 command_updater_
.UpdateCommandEnabled(IDC_RESTORE_TAB
, false);
895 #if defined(OS_WIN) && defined(USE_ASH)
896 if (browser_
->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH
)
897 command_updater_
.UpdateCommandEnabled(IDC_EXIT
, true);
899 command_updater_
.UpdateCommandEnabled(IDC_EXIT
, true);
901 command_updater_
.UpdateCommandEnabled(IDC_DEBUG_FRAME_TOGGLE
, true);
902 #if defined(OS_WIN) && defined(USE_ASH) && !defined(NDEBUG)
903 if (base::win::GetVersion() < base::win::VERSION_WIN8
&&
904 chrome::HOST_DESKTOP_TYPE_NATIVE
!= chrome::HOST_DESKTOP_TYPE_ASH
)
905 command_updater_
.UpdateCommandEnabled(IDC_TOGGLE_ASH_DESKTOP
, true);
908 command_updater_
.UpdateCommandEnabled(IDC_MINIMIZE_WINDOW
, true);
910 #if defined(OS_CHROMEOS)
911 command_updater_
.UpdateCommandEnabled(IDC_VISIT_DESKTOP_OF_LRU_USER_2
, true);
912 command_updater_
.UpdateCommandEnabled(IDC_VISIT_DESKTOP_OF_LRU_USER_3
, true);
914 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
915 command_updater_
.UpdateCommandEnabled(IDC_USE_SYSTEM_TITLE_BAR
, true);
918 // Page-related commands
919 command_updater_
.UpdateCommandEnabled(IDC_EMAIL_PAGE_LOCATION
, true);
920 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_AUTO_DETECT
, true);
921 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_UTF8
, true);
922 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_UTF16LE
, true);
923 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88591
, true);
924 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1252
, true);
925 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_GBK
, true);
926 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_GB18030
, true);
927 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_BIG5HKSCS
, true);
928 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_BIG5
, true);
929 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_THAI
, true);
930 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_KOREAN
, true);
931 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_SHIFTJIS
, true);
932 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO2022JP
, true);
933 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_EUCJP
, true);
934 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO885915
, true);
935 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_MACINTOSH
, true);
936 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88592
, true);
937 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1250
, true);
938 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88595
, true);
939 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1251
, true);
940 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_KOI8R
, true);
941 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_KOI8U
, true);
942 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88597
, true);
943 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1253
, true);
944 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88594
, true);
945 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO885913
, true);
946 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1257
, true);
947 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88593
, true);
948 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO885910
, true);
949 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO885914
, true);
950 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO885916
, true);
951 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1254
, true);
952 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88596
, true);
953 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1256
, true);
954 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88598
, true);
955 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_ISO88598I
, true);
956 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1255
, true);
957 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1258
, true);
960 command_updater_
.UpdateCommandEnabled(IDC_ZOOM_MENU
, true);
961 command_updater_
.UpdateCommandEnabled(IDC_ZOOM_PLUS
, true);
962 command_updater_
.UpdateCommandEnabled(IDC_ZOOM_NORMAL
, true);
963 command_updater_
.UpdateCommandEnabled(IDC_ZOOM_MINUS
, true);
965 // Show various bits of UI
966 UpdateOpenFileState(&command_updater_
);
967 command_updater_
.UpdateCommandEnabled(IDC_CREATE_SHORTCUTS
, false);
968 UpdateCommandsForDevTools();
969 command_updater_
.UpdateCommandEnabled(IDC_TASK_MANAGER
, CanOpenTaskManager());
970 command_updater_
.UpdateCommandEnabled(IDC_SHOW_HISTORY
,
971 !profile()->IsGuestSession());
972 command_updater_
.UpdateCommandEnabled(IDC_SHOW_DOWNLOADS
, true);
973 command_updater_
.UpdateCommandEnabled(IDC_HELP_PAGE_VIA_KEYBOARD
, true);
974 command_updater_
.UpdateCommandEnabled(IDC_HELP_PAGE_VIA_MENU
, true);
975 command_updater_
.UpdateCommandEnabled(IDC_BOOKMARKS_MENU
,
976 !profile()->IsGuestSession());
977 command_updater_
.UpdateCommandEnabled(IDC_RECENT_TABS_MENU
,
978 !profile()->IsGuestSession() &&
979 !profile()->IsOffTheRecord());
980 #if defined(OS_CHROMEOS)
981 command_updater_
.UpdateCommandEnabled(IDC_TAKE_SCREENSHOT
, true);
984 UpdateShowSyncState(true);
986 // Initialize other commands based on the window type.
987 bool normal_window
= browser_
->is_type_tabbed();
989 // Navigation commands
990 command_updater_
.UpdateCommandEnabled(
992 normal_window
|| (CommandLine::ForCurrentProcess()->HasSwitch(
993 switches::kEnableStreamlinedHostedApps
) &&
994 browser_
->is_app()));
996 // Window management commands
997 command_updater_
.UpdateCommandEnabled(IDC_SELECT_NEXT_TAB
, normal_window
);
998 command_updater_
.UpdateCommandEnabled(IDC_SELECT_PREVIOUS_TAB
,
1000 command_updater_
.UpdateCommandEnabled(IDC_MOVE_TAB_NEXT
, normal_window
);
1001 command_updater_
.UpdateCommandEnabled(IDC_MOVE_TAB_PREVIOUS
, normal_window
);
1002 command_updater_
.UpdateCommandEnabled(IDC_SELECT_TAB_0
, normal_window
);
1003 command_updater_
.UpdateCommandEnabled(IDC_SELECT_TAB_1
, normal_window
);
1004 command_updater_
.UpdateCommandEnabled(IDC_SELECT_TAB_2
, normal_window
);
1005 command_updater_
.UpdateCommandEnabled(IDC_SELECT_TAB_3
, normal_window
);
1006 command_updater_
.UpdateCommandEnabled(IDC_SELECT_TAB_4
, normal_window
);
1007 command_updater_
.UpdateCommandEnabled(IDC_SELECT_TAB_5
, normal_window
);
1008 command_updater_
.UpdateCommandEnabled(IDC_SELECT_TAB_6
, normal_window
);
1009 command_updater_
.UpdateCommandEnabled(IDC_SELECT_TAB_7
, normal_window
);
1010 command_updater_
.UpdateCommandEnabled(IDC_SELECT_LAST_TAB
, normal_window
);
1012 #if !defined(USE_AURA)
1013 const bool metro_mode
= base::win::IsMetroProcess();
1015 const bool metro_mode
=
1016 browser_
->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH
?
1019 command_updater_
.UpdateCommandEnabled(IDC_METRO_SNAP_ENABLE
, metro_mode
);
1020 command_updater_
.UpdateCommandEnabled(IDC_METRO_SNAP_DISABLE
, metro_mode
);
1021 int restart_mode
= metro_mode
?
1022 IDC_WIN8_DESKTOP_RESTART
: IDC_WIN8_METRO_RESTART
;
1023 command_updater_
.UpdateCommandEnabled(restart_mode
, normal_window
);
1026 // Show various bits of UI
1027 command_updater_
.UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA
, normal_window
);
1029 // The upgrade entry and the view incompatibility entry should always be
1030 // enabled. Whether they are visible is a separate matter determined on menu
1032 command_updater_
.UpdateCommandEnabled(IDC_UPGRADE_DIALOG
, true);
1033 command_updater_
.UpdateCommandEnabled(IDC_VIEW_INCOMPATIBILITIES
, true);
1035 // Toggle speech input
1036 command_updater_
.UpdateCommandEnabled(IDC_TOGGLE_SPEECH_INPUT
, true);
1038 // Initialize other commands whose state changes based on fullscreen mode.
1039 UpdateCommandsForFullscreenMode();
1041 UpdateCommandsForContentRestrictionState();
1043 UpdateCommandsForBookmarkEditing();
1045 UpdateCommandsForIncognitoAvailability();
1049 void BrowserCommandController::UpdateSharedCommandsForIncognitoAvailability(
1050 CommandUpdater
* command_updater
,
1052 IncognitoModePrefs::Availability incognito_availability
=
1053 IncognitoModePrefs::GetAvailability(profile
->GetPrefs());
1054 command_updater
->UpdateCommandEnabled(
1056 incognito_availability
!= IncognitoModePrefs::FORCED
);
1057 command_updater
->UpdateCommandEnabled(
1058 IDC_NEW_INCOGNITO_WINDOW
,
1059 incognito_availability
!= IncognitoModePrefs::DISABLED
);
1061 const bool guest_session
= profile
->IsGuestSession();
1062 const bool forced_incognito
=
1063 incognito_availability
== IncognitoModePrefs::FORCED
||
1064 guest_session
; // Guest always runs in Incognito mode.
1065 command_updater
->UpdateCommandEnabled(
1066 IDC_SHOW_BOOKMARK_MANAGER
,
1067 browser_defaults::bookmarks_enabled
&& !forced_incognito
);
1068 ExtensionService
* extension_service
= profile
->GetExtensionService();
1069 const bool enable_extensions
=
1070 extension_service
&& extension_service
->extensions_enabled();
1072 // Bookmark manager and settings page/subpages are forced to open in normal
1073 // mode. For this reason we disable these commands when incognito is forced.
1074 command_updater
->UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS
,
1075 enable_extensions
&& !forced_incognito
);
1077 command_updater
->UpdateCommandEnabled(IDC_IMPORT_SETTINGS
, !forced_incognito
);
1078 command_updater
->UpdateCommandEnabled(IDC_OPTIONS
,
1079 !forced_incognito
|| guest_session
);
1080 command_updater
->UpdateCommandEnabled(IDC_SHOW_SIGNIN
, !forced_incognito
);
1083 void BrowserCommandController::UpdateCommandsForIncognitoAvailability() {
1084 UpdateSharedCommandsForIncognitoAvailability(&command_updater_
, profile());
1086 if (!IsShowingMainUI()) {
1087 command_updater_
.UpdateCommandEnabled(IDC_IMPORT_SETTINGS
, false);
1088 command_updater_
.UpdateCommandEnabled(IDC_OPTIONS
, false);
1092 void BrowserCommandController::UpdateCommandsForTabState() {
1093 WebContents
* current_web_contents
=
1094 browser_
->tab_strip_model()->GetActiveWebContents();
1095 if (!current_web_contents
) // May be NULL during tab restore.
1098 // Navigation commands
1099 command_updater_
.UpdateCommandEnabled(IDC_BACK
, CanGoBack(browser_
));
1100 command_updater_
.UpdateCommandEnabled(IDC_FORWARD
, CanGoForward(browser_
));
1101 command_updater_
.UpdateCommandEnabled(IDC_RELOAD
, CanReload(browser_
));
1102 command_updater_
.UpdateCommandEnabled(IDC_RELOAD_IGNORING_CACHE
,
1103 CanReload(browser_
));
1104 command_updater_
.UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE
,
1105 CanReload(browser_
));
1107 // Window management commands
1108 command_updater_
.UpdateCommandEnabled(IDC_DUPLICATE_TAB
,
1109 !browser_
->is_app() && CanDuplicateTab(browser_
));
1111 // Page-related commands
1112 window()->SetStarredState(
1113 BookmarkTabHelper::FromWebContents(current_web_contents
)->is_starred());
1114 window()->ZoomChangedForActiveTab(false);
1115 command_updater_
.UpdateCommandEnabled(IDC_VIEW_SOURCE
,
1116 CanViewSource(browser_
));
1117 command_updater_
.UpdateCommandEnabled(IDC_EMAIL_PAGE_LOCATION
,
1118 CanEmailPageLocation(browser_
));
1119 if (browser_
->is_devtools())
1120 command_updater_
.UpdateCommandEnabled(IDC_OPEN_FILE
, false);
1122 // Changing the encoding is not possible on Chrome-internal webpages.
1123 NavigationController
& nc
= current_web_contents
->GetController();
1124 bool is_chrome_internal
= HasInternalURL(nc
.GetActiveEntry()) ||
1125 current_web_contents
->ShowingInterstitialPage();
1126 command_updater_
.UpdateCommandEnabled(IDC_ENCODING_MENU
,
1127 !is_chrome_internal
&& current_web_contents
->IsSavable());
1129 // Show various bits of UI
1130 // TODO(pinkerton): Disable app-mode in the model until we implement it
1131 // on the Mac. Be sure to remove both ifdefs. http://crbug.com/13148
1132 #if !defined(OS_MACOSX)
1133 command_updater_
.UpdateCommandEnabled(
1134 IDC_CREATE_SHORTCUTS
,
1135 CanCreateApplicationShortcuts(browser_
));
1136 command_updater_
.UpdateCommandEnabled(IDC_CREATE_HOSTED_APP
,
1137 CanCreateBookmarkApp(browser_
));
1140 command_updater_
.UpdateCommandEnabled(
1141 IDC_TOGGLE_REQUEST_TABLET_SITE
,
1142 CanRequestTabletSite(current_web_contents
));
1144 UpdateCommandsForContentRestrictionState();
1145 UpdateCommandsForBookmarkEditing();
1146 UpdateCommandsForFind();
1149 void BrowserCommandController::UpdateCommandsForContentRestrictionState() {
1150 int restrictions
= GetContentRestrictions(browser_
);
1152 command_updater_
.UpdateCommandEnabled(
1153 IDC_COPY
, !(restrictions
& CONTENT_RESTRICTION_COPY
));
1154 command_updater_
.UpdateCommandEnabled(
1155 IDC_CUT
, !(restrictions
& CONTENT_RESTRICTION_CUT
));
1156 command_updater_
.UpdateCommandEnabled(
1157 IDC_PASTE
, !(restrictions
& CONTENT_RESTRICTION_PASTE
));
1158 UpdateSaveAsState();
1159 UpdatePrintingState();
1162 void BrowserCommandController::UpdateCommandsForDevTools() {
1163 bool dev_tools_enabled
=
1164 !profile()->GetPrefs()->GetBoolean(prefs::kDevToolsDisabled
);
1165 command_updater_
.UpdateCommandEnabled(IDC_DEV_TOOLS
,
1167 command_updater_
.UpdateCommandEnabled(IDC_DEV_TOOLS_CONSOLE
,
1169 command_updater_
.UpdateCommandEnabled(IDC_DEV_TOOLS_DEVICES
,
1171 command_updater_
.UpdateCommandEnabled(IDC_DEV_TOOLS_INSPECT
,
1173 command_updater_
.UpdateCommandEnabled(IDC_DEV_TOOLS_TOGGLE
,
1177 void BrowserCommandController::UpdateCommandsForBookmarkEditing() {
1178 command_updater_
.UpdateCommandEnabled(IDC_BOOKMARK_PAGE
,
1179 CanBookmarkCurrentPage(browser_
));
1180 command_updater_
.UpdateCommandEnabled(IDC_BOOKMARK_ALL_TABS
,
1181 CanBookmarkAllTabs(browser_
));
1182 command_updater_
.UpdateCommandEnabled(IDC_PIN_TO_START_SCREEN
,
1186 void BrowserCommandController::UpdateCommandsForBookmarkBar() {
1187 command_updater_
.UpdateCommandEnabled(IDC_SHOW_BOOKMARK_BAR
,
1188 browser_defaults::bookmarks_enabled
&&
1189 !profile()->IsGuestSession() &&
1190 !profile()->GetPrefs()->IsManagedPreference(prefs::kShowBookmarkBar
) &&
1194 void BrowserCommandController::UpdateCommandsForFileSelectionDialogs() {
1195 UpdateSaveAsState();
1196 UpdateOpenFileState(&command_updater_
);
1199 void BrowserCommandController::UpdateCommandsForFullscreenMode() {
1200 WindowState window_state
= WINDOW_STATE_NOT_FULLSCREEN
;
1201 if (window() && window()->IsFullscreen()) {
1202 window_state
= WINDOW_STATE_FULLSCREEN
;
1204 if (window()->IsInMetroSnapMode())
1205 window_state
= WINDOW_STATE_METRO_SNAP
;
1208 bool show_main_ui
= IsShowingMainUI();
1209 bool main_not_fullscreen
=
1210 show_main_ui
&& window_state
== WINDOW_STATE_NOT_FULLSCREEN
;
1212 // Navigation commands
1213 command_updater_
.UpdateCommandEnabled(IDC_OPEN_CURRENT_URL
, show_main_ui
);
1215 // Window management commands
1216 command_updater_
.UpdateCommandEnabled(
1218 !browser_
->is_type_tabbed() &&
1219 window_state
== WINDOW_STATE_NOT_FULLSCREEN
);
1221 // Focus various bits of UI
1222 command_updater_
.UpdateCommandEnabled(IDC_FOCUS_TOOLBAR
, show_main_ui
);
1223 command_updater_
.UpdateCommandEnabled(IDC_FOCUS_LOCATION
, show_main_ui
);
1224 command_updater_
.UpdateCommandEnabled(IDC_FOCUS_SEARCH
, show_main_ui
);
1225 command_updater_
.UpdateCommandEnabled(
1226 IDC_FOCUS_MENU_BAR
, main_not_fullscreen
);
1227 command_updater_
.UpdateCommandEnabled(
1228 IDC_FOCUS_NEXT_PANE
, main_not_fullscreen
);
1229 command_updater_
.UpdateCommandEnabled(
1230 IDC_FOCUS_PREVIOUS_PANE
, main_not_fullscreen
);
1231 command_updater_
.UpdateCommandEnabled(
1232 IDC_FOCUS_BOOKMARKS
, main_not_fullscreen
);
1233 command_updater_
.UpdateCommandEnabled(
1234 IDC_FOCUS_INFOBARS
, main_not_fullscreen
);
1236 // Show various bits of UI
1237 command_updater_
.UpdateCommandEnabled(IDC_DEVELOPER_MENU
, show_main_ui
);
1238 #if defined(GOOGLE_CHROME_BUILD)
1239 command_updater_
.UpdateCommandEnabled(IDC_FEEDBACK
, show_main_ui
);
1241 UpdateShowSyncState(show_main_ui
);
1243 // Settings page/subpages are forced to open in normal mode. We disable these
1244 // commands for guest sessions and when incognito is forced.
1245 const bool options_enabled
= show_main_ui
&&
1246 IncognitoModePrefs::GetAvailability(
1247 profile()->GetPrefs()) != IncognitoModePrefs::FORCED
;
1248 const bool guest_session
= profile()->IsGuestSession();
1249 command_updater_
.UpdateCommandEnabled(IDC_OPTIONS
, options_enabled
);
1250 command_updater_
.UpdateCommandEnabled(IDC_IMPORT_SETTINGS
,
1251 options_enabled
&& !guest_session
);
1253 command_updater_
.UpdateCommandEnabled(IDC_EDIT_SEARCH_ENGINES
, show_main_ui
);
1254 command_updater_
.UpdateCommandEnabled(IDC_VIEW_PASSWORDS
, show_main_ui
);
1255 command_updater_
.UpdateCommandEnabled(IDC_ABOUT
, show_main_ui
);
1256 command_updater_
.UpdateCommandEnabled(IDC_SHOW_APP_MENU
, show_main_ui
);
1257 #if defined (ENABLE_PROFILING) && !defined(NO_TCMALLOC)
1258 command_updater_
.UpdateCommandEnabled(IDC_PROFILING_ENABLED
, show_main_ui
);
1261 // Disable explicit fullscreen toggling when in metro snap mode.
1262 bool fullscreen_enabled
= window_state
!= WINDOW_STATE_METRO_SNAP
;
1263 #if !defined(OS_MACOSX)
1264 if (window_state
== WINDOW_STATE_NOT_FULLSCREEN
&&
1265 !profile()->GetPrefs()->GetBoolean(prefs::kFullscreenAllowed
)) {
1266 // Disable toggling into fullscreen mode if disallowed by pref.
1267 fullscreen_enabled
= false;
1271 command_updater_
.UpdateCommandEnabled(IDC_FULLSCREEN
, fullscreen_enabled
);
1272 command_updater_
.UpdateCommandEnabled(IDC_PRESENTATION_MODE
,
1273 fullscreen_enabled
);
1275 UpdateCommandsForBookmarkBar();
1276 UpdateCommandsForMultipleProfiles();
1279 void BrowserCommandController::UpdateCommandsForMultipleProfiles() {
1280 bool is_regular_or_guest_session
=
1281 profile()->IsGuestSession() || !profile()->IsOffTheRecord();
1282 bool enable
= IsShowingMainUI() &&
1283 is_regular_or_guest_session
&&
1285 AvatarMenu::ShouldShowAvatarMenu();
1286 command_updater_
.UpdateCommandEnabled(IDC_SHOW_AVATAR_MENU
,
1290 void BrowserCommandController::UpdatePrintingState() {
1291 bool print_enabled
= CanPrint(browser_
);
1292 command_updater_
.UpdateCommandEnabled(IDC_PRINT
, print_enabled
);
1293 command_updater_
.UpdateCommandEnabled(IDC_ADVANCED_PRINT
,
1294 CanAdvancedPrint(browser_
));
1295 command_updater_
.UpdateCommandEnabled(IDC_PRINT_TO_DESTINATION
,
1298 HMODULE metro_module
= base::win::GetMetroModule();
1299 if (metro_module
!= NULL
) {
1300 typedef void (*MetroEnablePrinting
)(BOOL
);
1301 MetroEnablePrinting metro_enable_printing
=
1302 reinterpret_cast<MetroEnablePrinting
>(
1303 ::GetProcAddress(metro_module
, "MetroEnablePrinting"));
1304 if (metro_enable_printing
)
1305 metro_enable_printing(print_enabled
);
1310 void BrowserCommandController::UpdateSaveAsState() {
1311 command_updater_
.UpdateCommandEnabled(IDC_SAVE_PAGE
, CanSavePage(browser_
));
1314 void BrowserCommandController::UpdateShowSyncState(bool show_main_ui
) {
1315 command_updater_
.UpdateCommandEnabled(
1316 IDC_SHOW_SYNC_SETUP
, show_main_ui
&& pref_signin_allowed_
.GetValue());
1320 void BrowserCommandController::UpdateOpenFileState(
1321 CommandUpdater
* command_updater
) {
1322 bool enabled
= true;
1323 PrefService
* local_state
= g_browser_process
->local_state();
1325 enabled
= local_state
->GetBoolean(prefs::kAllowFileSelectionDialogs
);
1327 command_updater
->UpdateCommandEnabled(IDC_OPEN_FILE
, enabled
);
1330 void BrowserCommandController::UpdateReloadStopState(bool is_loading
,
1332 window()->UpdateReloadStopState(is_loading
, force
);
1333 command_updater_
.UpdateCommandEnabled(IDC_STOP
, is_loading
);
1336 void BrowserCommandController::UpdateCommandsForFind() {
1337 TabStripModel
* model
= browser_
->tab_strip_model();
1338 bool enabled
= !model
->IsTabBlocked(model
->active_index()) &&
1339 !browser_
->is_devtools();
1341 command_updater_
.UpdateCommandEnabled(IDC_FIND
, enabled
);
1342 command_updater_
.UpdateCommandEnabled(IDC_FIND_NEXT
, enabled
);
1343 command_updater_
.UpdateCommandEnabled(IDC_FIND_PREVIOUS
, enabled
);
1346 void BrowserCommandController::AddInterstitialObservers(WebContents
* contents
) {
1347 interstitial_observers_
.push_back(new InterstitialObserver(this, contents
));
1350 void BrowserCommandController::RemoveInterstitialObservers(
1351 WebContents
* contents
) {
1352 for (size_t i
= 0; i
< interstitial_observers_
.size(); i
++) {
1353 if (interstitial_observers_
[i
]->web_contents() != contents
)
1356 delete interstitial_observers_
[i
];
1357 interstitial_observers_
.erase(interstitial_observers_
.begin() + i
);
1362 BrowserWindow
* BrowserCommandController::window() {
1363 return browser_
->window();
1366 Profile
* BrowserCommandController::profile() {
1367 return browser_
->profile();
1370 } // namespace chrome