Componentize AccountReconcilor.
[chromium-blink-merge.git] / chrome / browser / ui / browser_command_controller.cc
blob735b49c5007197d09786a67f0fc3d243de4f77e1
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"
48 #endif
50 #if defined(OS_WIN)
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"
55 #endif
57 #if defined(USE_ASH)
58 #include "ash/accelerators/accelerator_commands.h"
59 #include "chrome/browser/ui/ash/ash_util.h"
60 #endif
62 #if defined(OS_CHROMEOS)
63 #include "ash/multi_profile_uma.h"
64 #include "ash/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"
69 #endif
71 #if defined(USE_X11) && !defined(OS_CHROMEOS)
72 #include "ui/events/x/text_edit_key_bindings_delegate_x11.h"
73 #endif
75 using content::NavigationEntry;
76 using content::NavigationController;
77 using content::WebContents;
79 namespace {
81 enum WindowState {
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
89 // the screen width.
90 WINDOW_STATE_METRO_SNAP,
93 // Returns |true| if entry has an internal chrome:// URL, |false| otherwise.
94 bool HasInternalURL(const NavigationEntry* entry) {
95 if (!entry)
96 return false;
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))
101 return true;
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);
108 return false;
111 #if defined(OS_WIN)
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 {
124 public:
125 SwitchToMetroUIHandler()
126 : default_browser_worker_(
127 new ShellIntegration::DefaultBrowserWorker(this)),
128 first_check_(true) {
129 default_browser_worker_->StartCheckIsDefault();
132 virtual ~SwitchToMetroUIHandler() {
133 default_browser_worker_->ObserverDestroyed();
136 private:
137 virtual void SetDefaultWebClientUIState(
138 ShellIntegration::DefaultWebClientUIState state) OVERRIDE {
139 switch (state) {
140 case ShellIntegration::STATE_PROCESSING:
141 return;
142 case ShellIntegration::STATE_UNKNOWN :
143 break;
144 case ShellIntegration::STATE_IS_DEFAULT:
145 chrome::AttemptRestartToMetroMode();
146 break;
147 case ShellIntegration::STATE_NOT_DEFAULT:
148 if (first_check_) {
149 default_browser_worker_->StartSetAsDefault();
150 return;
152 break;
153 default:
154 NOTREACHED();
156 delete this;
159 virtual void OnSetAsDefaultConcluded(bool success) OVERRIDE {
160 if (!success) {
161 delete this;
162 return;
164 first_check_ = false;
165 default_browser_worker_->StartCheckIsDefault();
168 virtual bool IsInteractiveSetDefaultPermitted() OVERRIDE {
169 return true;
172 scoped_refptr<ShellIntegration::DefaultBrowserWorker> default_browser_worker_;
173 bool first_check_;
175 DISALLOW_COPY_AND_ASSIGN(SwitchToMetroUIHandler);
177 #endif // defined(OS_WIN)
179 } // namespace
181 namespace chrome {
183 ///////////////////////////////////////////////////////////////////////////////
184 // BrowserCommandController, public:
186 BrowserCommandController::BrowserCommandController(
187 Browser* browser,
188 ProfileManager* profile_manager)
189 : browser_(browser),
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();
199 if (local_state) {
200 local_pref_registrar_.Init(local_state);
201 local_pref_registrar_.Add(
202 prefs::kAllowFileSelectionDialogs,
203 base::Bind(
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,
223 base::Bind(
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)));
235 #endif
236 pref_signin_allowed_.Init(
237 prefs::kSigninAllowed,
238 profile()->GetOriginalProfile()->GetPrefs(),
239 base::Bind(&BrowserCommandController::OnSigninAllowedPrefChange,
240 base::Unretained(this)));
242 InitCommandState();
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(
267 int command_id,
268 const content::NativeWebKeyboardEvent& event) {
269 // In Apps mode, no keys are reserved.
270 if (browser_->is_app())
271 return false;
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)) {
283 return true;
285 #endif
287 if (window()->IsFullscreen() && command_id == IDC_FULLSCREEN)
288 return true;
290 #if defined(USE_X11) && !defined(OS_CHROMEOS) && !defined(TOOLKIT_GTK)
291 // If this key was registered by the user as a content editing hotkey, then
292 // it is not reserved.
293 ui::TextEditKeyBindingsDelegateX11* delegate =
294 ui::GetTextEditKeyBindingsDelegate();
295 if (delegate && event.os_event && delegate->MatchEvent(*event.os_event, NULL))
296 return false;
297 #endif
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;
312 if (block) {
313 last_blocked_command_id_ = -1;
314 last_blocked_command_disposition_ = CURRENT_TAB;
318 int BrowserCommandController::GetLastBlockedCommand(
319 WindowOpenDisposition* disposition) {
320 if (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,
342 bool force) {
343 UpdateReloadStopState(is_loading, force);
346 ////////////////////////////////////////////////////////////////////////////////
347 // BrowserCommandController, CommandUpdaterDelegate implementation:
349 void BrowserCommandController::ExecuteCommandWithDisposition(
350 int id,
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)
360 return;
362 DCHECK(command_updater_.IsCommandEnabled(id)) << "Invalid/disabled command "
363 << id;
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;
372 return;
375 // The order of commands in this switch statement must match the function
376 // declaration order in browser.h!
377 switch (id) {
378 // Navigation commands
379 case IDC_BACK:
380 GoBack(browser_, disposition);
381 break;
382 case IDC_FORWARD:
383 GoForward(browser_, disposition);
384 break;
385 case IDC_RELOAD:
386 Reload(browser_, disposition);
387 break;
388 case IDC_RELOAD_CLEARING_CACHE:
389 ClearCache(browser_);
390 // FALL THROUGH
391 case IDC_RELOAD_IGNORING_CACHE:
392 ReloadIgnoringCache(browser_, disposition);
393 break;
394 case IDC_HOME:
395 Home(browser_, disposition);
396 break;
397 case IDC_OPEN_CURRENT_URL:
398 OpenCurrentURL(browser_);
399 break;
400 case IDC_STOP:
401 Stop(browser_);
402 break;
404 // Window management commands
405 case IDC_NEW_WINDOW:
406 NewWindow(browser_);
407 break;
408 case IDC_NEW_INCOGNITO_WINDOW:
409 NewIncognitoWindow(browser_);
410 break;
411 case IDC_CLOSE_WINDOW:
412 content::RecordAction(base::UserMetricsAction("CloseWindowByKey"));
413 CloseWindow(browser_);
414 break;
415 case IDC_NEW_TAB:
416 NewTab(browser_);
417 break;
418 case IDC_CLOSE_TAB:
419 content::RecordAction(base::UserMetricsAction("CloseTabByKey"));
420 CloseTab(browser_);
421 break;
422 case IDC_SELECT_NEXT_TAB:
423 content::RecordAction(base::UserMetricsAction("Accel_SelectNextTab"));
424 SelectNextTab(browser_);
425 break;
426 case IDC_SELECT_PREVIOUS_TAB:
427 content::RecordAction(
428 base::UserMetricsAction("Accel_SelectPreviousTab"));
429 SelectPreviousTab(browser_);
430 break;
431 case IDC_MOVE_TAB_NEXT:
432 MoveTabNext(browser_);
433 break;
434 case IDC_MOVE_TAB_PREVIOUS:
435 MoveTabPrevious(browser_);
436 break;
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);
446 break;
447 case IDC_SELECT_LAST_TAB:
448 SelectLastTab(browser_);
449 break;
450 case IDC_DUPLICATE_TAB:
451 DuplicateTab(browser_);
452 break;
453 case IDC_RESTORE_TAB:
454 RestoreTab(browser_);
455 break;
456 case IDC_SHOW_AS_TAB:
457 ConvertPopupToTabbedBrowser(browser_);
458 break;
459 case IDC_FULLSCREEN:
460 #if defined(OS_MACOSX)
461 chrome::ToggleFullscreenWithChromeOrFallback(browser_);
462 #else
463 chrome::ToggleFullscreenMode(browser_);
464 #endif
465 break;
467 #if defined(USE_ASH)
468 case IDC_TOGGLE_ASH_DESKTOP:
469 chrome::ToggleAshDesktop();
470 break;
471 case IDC_MINIMIZE_WINDOW:
472 content::RecordAction(
473 base::UserMetricsAction("Accel_Toggle_Minimized_M"));
474 ash::accelerators::ToggleMinimized();
475 break;
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
478 #endif
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());
484 break;
485 #endif
487 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && !defined(TOOLKIT_GTK)
488 case IDC_USE_SYSTEM_TITLE_BAR: {
489 PrefService* prefs = browser_->profile()->GetPrefs();
490 prefs->SetBoolean(prefs::kUseCustomChromeFrame,
491 !prefs->GetBoolean(prefs::kUseCustomChromeFrame));
492 break;
494 #endif
496 #if defined(OS_WIN)
497 // Windows 8 specific commands.
498 case IDC_METRO_SNAP_ENABLE:
499 browser_->SetMetroSnapMode(true);
500 break;
501 case IDC_METRO_SNAP_DISABLE:
502 browser_->SetMetroSnapMode(false);
503 break;
504 case IDC_WIN8_DESKTOP_RESTART:
505 if (!VerifyMetroSwitchForApps(window()->GetNativeWindow(), id))
506 break;
508 chrome::AttemptRestartToDesktopMode();
509 content::RecordAction(base::UserMetricsAction("Win8DesktopRestart"));
510 break;
511 case IDC_WIN8_METRO_RESTART:
512 if (!VerifyMetroSwitchForApps(window()->GetNativeWindow(), id))
513 break;
515 // SwitchToMetroUIHandler deletes itself.
516 new SwitchToMetroUIHandler;
517 content::RecordAction(base::UserMetricsAction("Win8MetroRestart"));
518 break;
519 #endif
521 #if defined(OS_MACOSX)
522 case IDC_PRESENTATION_MODE:
523 chrome::ToggleFullscreenMode(browser_);
524 break;
525 #endif
526 case IDC_EXIT:
527 Exit();
528 break;
530 // Page-related commands
531 case IDC_SAVE_PAGE:
532 SavePage(browser_);
533 break;
534 case IDC_BOOKMARK_PAGE:
535 BookmarkCurrentPage(browser_);
536 break;
537 case IDC_BOOKMARK_PAGE_FROM_STAR:
538 BookmarkCurrentPageFromStar(browser_);
539 break;
540 case IDC_PIN_TO_START_SCREEN:
541 TogglePagePinnedToStartScreen(browser_);
542 break;
543 case IDC_BOOKMARK_ALL_TABS:
544 BookmarkAllTabs(browser_);
545 break;
546 case IDC_VIEW_SOURCE:
547 ViewSelectedSource(browser_);
548 break;
549 case IDC_EMAIL_PAGE_LOCATION:
550 EmailPageLocation(browser_);
551 break;
552 case IDC_PRINT:
553 Print(browser_);
554 break;
555 case IDC_ADVANCED_PRINT:
556 content::RecordAction(base::UserMetricsAction("Accel_Advanced_Print"));
557 AdvancedPrint(browser_);
558 break;
559 case IDC_PRINT_TO_DESTINATION:
560 PrintToDestination(browser_);
561 break;
562 case IDC_TRANSLATE_PAGE:
563 Translate(browser_);
564 break;
565 case IDC_ENCODING_AUTO_DETECT:
566 browser_->ToggleEncodingAutoDetect();
567 break;
568 case IDC_ENCODING_UTF8:
569 case IDC_ENCODING_UTF16LE:
570 case IDC_ENCODING_ISO88591:
571 case IDC_ENCODING_WINDOWS1252:
572 case IDC_ENCODING_GBK:
573 case IDC_ENCODING_GB18030:
574 case IDC_ENCODING_BIG5HKSCS:
575 case IDC_ENCODING_BIG5:
576 case IDC_ENCODING_KOREAN:
577 case IDC_ENCODING_SHIFTJIS:
578 case IDC_ENCODING_ISO2022JP:
579 case IDC_ENCODING_EUCJP:
580 case IDC_ENCODING_THAI:
581 case IDC_ENCODING_ISO885915:
582 case IDC_ENCODING_MACINTOSH:
583 case IDC_ENCODING_ISO88592:
584 case IDC_ENCODING_WINDOWS1250:
585 case IDC_ENCODING_ISO88595:
586 case IDC_ENCODING_WINDOWS1251:
587 case IDC_ENCODING_KOI8R:
588 case IDC_ENCODING_KOI8U:
589 case IDC_ENCODING_ISO88597:
590 case IDC_ENCODING_WINDOWS1253:
591 case IDC_ENCODING_ISO88594:
592 case IDC_ENCODING_ISO885913:
593 case IDC_ENCODING_WINDOWS1257:
594 case IDC_ENCODING_ISO88593:
595 case IDC_ENCODING_ISO885910:
596 case IDC_ENCODING_ISO885914:
597 case IDC_ENCODING_ISO885916:
598 case IDC_ENCODING_WINDOWS1254:
599 case IDC_ENCODING_ISO88596:
600 case IDC_ENCODING_WINDOWS1256:
601 case IDC_ENCODING_ISO88598:
602 case IDC_ENCODING_ISO88598I:
603 case IDC_ENCODING_WINDOWS1255:
604 case IDC_ENCODING_WINDOWS1258:
605 browser_->OverrideEncoding(id);
606 break;
608 // Clipboard commands
609 case IDC_CUT:
610 Cut(browser_);
611 break;
612 case IDC_COPY:
613 Copy(browser_);
614 break;
615 case IDC_PASTE:
616 Paste(browser_);
617 break;
619 // Find-in-page
620 case IDC_FIND:
621 Find(browser_);
622 break;
623 case IDC_FIND_NEXT:
624 FindNext(browser_);
625 break;
626 case IDC_FIND_PREVIOUS:
627 FindPrevious(browser_);
628 break;
630 // Zoom
631 case IDC_ZOOM_PLUS:
632 Zoom(browser_, content::PAGE_ZOOM_IN);
633 break;
634 case IDC_ZOOM_NORMAL:
635 Zoom(browser_, content::PAGE_ZOOM_RESET);
636 break;
637 case IDC_ZOOM_MINUS:
638 Zoom(browser_, content::PAGE_ZOOM_OUT);
639 break;
641 // Focus various bits of UI
642 case IDC_FOCUS_TOOLBAR:
643 content::RecordAction(base::UserMetricsAction("Accel_Focus_Toolbar"));
644 FocusToolbar(browser_);
645 break;
646 case IDC_FOCUS_LOCATION:
647 content::RecordAction(base::UserMetricsAction("Accel_Focus_Location"));
648 FocusLocationBar(browser_);
649 break;
650 case IDC_FOCUS_SEARCH:
651 content::RecordAction(base::UserMetricsAction("Accel_Focus_Search"));
652 FocusSearch(browser_);
653 break;
654 case IDC_FOCUS_MENU_BAR:
655 FocusAppMenu(browser_);
656 break;
657 case IDC_FOCUS_BOOKMARKS:
658 content::RecordAction(
659 base::UserMetricsAction("Accel_Focus_Bookmarks"));
660 FocusBookmarksToolbar(browser_);
661 break;
662 case IDC_FOCUS_INFOBARS:
663 FocusInfobars(browser_);
664 break;
665 case IDC_FOCUS_NEXT_PANE:
666 FocusNextPane(browser_);
667 break;
668 case IDC_FOCUS_PREVIOUS_PANE:
669 FocusPreviousPane(browser_);
670 break;
672 // Show various bits of UI
673 case IDC_OPEN_FILE:
674 browser_->OpenFile();
675 break;
676 case IDC_CREATE_SHORTCUTS:
677 CreateApplicationShortcuts(browser_);
678 break;
679 case IDC_CREATE_HOSTED_APP:
680 CreateHostedAppFromCurrentWebContents(browser_);
681 break;
682 case IDC_DEV_TOOLS:
683 ToggleDevToolsWindow(browser_, DevToolsToggleAction::Show());
684 break;
685 case IDC_DEV_TOOLS_CONSOLE:
686 ToggleDevToolsWindow(browser_, DevToolsToggleAction::ShowConsole());
687 break;
688 case IDC_DEV_TOOLS_DEVICES:
689 InspectUI::InspectDevices(browser_);
690 break;
691 case IDC_DEV_TOOLS_INSPECT:
692 ToggleDevToolsWindow(browser_, DevToolsToggleAction::Inspect());
693 break;
694 case IDC_DEV_TOOLS_TOGGLE:
695 ToggleDevToolsWindow(browser_, DevToolsToggleAction::Toggle());
696 break;
697 case IDC_TASK_MANAGER:
698 OpenTaskManager(browser_);
699 break;
700 #if defined(OS_CHROMEOS)
701 case IDC_TAKE_SCREENSHOT:
702 TakeScreenshot();
703 break;
704 #endif
705 #if defined(GOOGLE_CHROME_BUILD)
706 case IDC_FEEDBACK:
707 OpenFeedbackDialog(browser_);
708 break;
709 #endif
710 case IDC_SHOW_BOOKMARK_BAR:
711 ToggleBookmarkBar(browser_);
712 break;
713 case IDC_PROFILING_ENABLED:
714 Profiling::Toggle();
715 break;
717 case IDC_SHOW_BOOKMARK_MANAGER:
718 ShowBookmarkManager(browser_);
719 break;
720 case IDC_SHOW_APP_MENU:
721 content::RecordAction(base::UserMetricsAction("Accel_Show_App_Menu"));
722 ShowAppMenu(browser_);
723 break;
724 case IDC_SHOW_AVATAR_MENU:
725 ShowAvatarMenu(browser_);
726 break;
727 case IDC_SHOW_HISTORY:
728 ShowHistory(browser_);
729 break;
730 case IDC_SHOW_DOWNLOADS:
731 ShowDownloads(browser_);
732 break;
733 case IDC_MANAGE_EXTENSIONS:
734 ShowExtensions(browser_, std::string());
735 break;
736 case IDC_OPTIONS:
737 ShowSettings(browser_);
738 break;
739 case IDC_EDIT_SEARCH_ENGINES:
740 ShowSearchEngineSettings(browser_);
741 break;
742 case IDC_VIEW_PASSWORDS:
743 ShowPasswordManager(browser_);
744 break;
745 case IDC_CLEAR_BROWSING_DATA:
746 ShowClearBrowsingDataDialog(browser_);
747 break;
748 case IDC_IMPORT_SETTINGS:
749 ShowImportDialog(browser_);
750 break;
751 case IDC_TOGGLE_REQUEST_TABLET_SITE:
752 ToggleRequestTabletSite(browser_);
753 break;
754 case IDC_ABOUT:
755 ShowAboutChrome(browser_);
756 break;
757 case IDC_UPGRADE_DIALOG:
758 OpenUpdateChromeDialog(browser_);
759 break;
760 case IDC_VIEW_INCOMPATIBILITIES:
761 ShowConflicts(browser_);
762 break;
763 case IDC_HELP_PAGE_VIA_KEYBOARD:
764 ShowHelp(browser_, HELP_SOURCE_KEYBOARD);
765 break;
766 case IDC_HELP_PAGE_VIA_MENU:
767 ShowHelp(browser_, HELP_SOURCE_MENU);
768 break;
769 case IDC_SHOW_SIGNIN:
770 ShowBrowserSignin(browser_, signin::SOURCE_MENU);
771 break;
772 case IDC_TOGGLE_SPEECH_INPUT:
773 ToggleSpeechInput(browser_);
774 break;
776 default:
777 LOG(WARNING) << "Received Unimplemented Command: " << id;
778 break;
782 ////////////////////////////////////////////////////////////////////////////////
783 // BrowserCommandController, ProfileInfoCacheObserver implementation:
785 void BrowserCommandController::OnProfileAdded(
786 const base::FilePath& profile_path) {
787 UpdateCommandsForMultipleProfiles();
790 void BrowserCommandController::OnProfileWasRemoved(
791 const base::FilePath& profile_path,
792 const base::string16& profile_name) {
793 UpdateCommandsForMultipleProfiles();
796 ////////////////////////////////////////////////////////////////////////////////
797 // BrowserCommandController, SigninPrefObserver implementation:
799 void BrowserCommandController::OnSigninAllowedPrefChange() {
800 // For unit tests, we don't have a window.
801 if (!window())
802 return;
803 UpdateShowSyncState(IsShowingMainUI());
806 // BrowserCommandController, TabStripModelObserver implementation:
808 void BrowserCommandController::TabInsertedAt(WebContents* contents,
809 int index,
810 bool foreground) {
811 AddInterstitialObservers(contents);
814 void BrowserCommandController::TabDetachedAt(WebContents* contents, int index) {
815 RemoveInterstitialObservers(contents);
818 void BrowserCommandController::TabReplacedAt(TabStripModel* tab_strip_model,
819 WebContents* old_contents,
820 WebContents* new_contents,
821 int index) {
822 RemoveInterstitialObservers(old_contents);
823 AddInterstitialObservers(new_contents);
826 void BrowserCommandController::TabBlockedStateChanged(
827 content::WebContents* contents,
828 int index) {
829 PrintingStateChanged();
830 FullscreenStateChanged();
831 UpdateCommandsForFind();
834 ////////////////////////////////////////////////////////////////////////////////
835 // BrowserCommandController, TabRestoreServiceObserver implementation:
837 void BrowserCommandController::TabRestoreServiceChanged(
838 TabRestoreService* service) {
839 command_updater_.UpdateCommandEnabled(
840 IDC_RESTORE_TAB,
841 GetRestoreTabType(browser_) != TabStripModelDelegate::RESTORE_NONE);
844 void BrowserCommandController::TabRestoreServiceDestroyed(
845 TabRestoreService* service) {
846 service->RemoveObserver(this);
849 ////////////////////////////////////////////////////////////////////////////////
850 // BrowserCommandController, private:
852 class BrowserCommandController::InterstitialObserver
853 : public content::WebContentsObserver {
854 public:
855 InterstitialObserver(BrowserCommandController* controller,
856 content::WebContents* web_contents)
857 : WebContentsObserver(web_contents),
858 controller_(controller) {
861 using content::WebContentsObserver::web_contents;
863 virtual void DidAttachInterstitialPage() OVERRIDE {
864 controller_->UpdateCommandsForTabState();
867 virtual void DidDetachInterstitialPage() OVERRIDE {
868 controller_->UpdateCommandsForTabState();
871 private:
872 BrowserCommandController* controller_;
874 DISALLOW_COPY_AND_ASSIGN(InterstitialObserver);
877 bool BrowserCommandController::IsShowingMainUI() {
878 bool should_hide_ui = window() && window()->ShouldHideUIForFullscreen();
879 return browser_->is_type_tabbed() && !should_hide_ui;
882 void BrowserCommandController::InitCommandState() {
883 // All browser commands whose state isn't set automagically some other way
884 // (like Back & Forward with initial page load) must have their state
885 // initialized here, otherwise they will be forever disabled.
887 // Navigation commands
888 command_updater_.UpdateCommandEnabled(IDC_RELOAD, true);
889 command_updater_.UpdateCommandEnabled(IDC_RELOAD_IGNORING_CACHE, true);
890 command_updater_.UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE, true);
892 // Window management commands
893 command_updater_.UpdateCommandEnabled(IDC_CLOSE_WINDOW, true);
894 command_updater_.UpdateCommandEnabled(IDC_NEW_TAB, true);
895 command_updater_.UpdateCommandEnabled(IDC_CLOSE_TAB, true);
896 command_updater_.UpdateCommandEnabled(IDC_DUPLICATE_TAB, true);
897 command_updater_.UpdateCommandEnabled(IDC_RESTORE_TAB, false);
898 #if defined(OS_WIN) && defined(USE_ASH)
899 if (browser_->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH)
900 command_updater_.UpdateCommandEnabled(IDC_EXIT, true);
901 #else
902 command_updater_.UpdateCommandEnabled(IDC_EXIT, true);
903 #endif
904 command_updater_.UpdateCommandEnabled(IDC_DEBUG_FRAME_TOGGLE, true);
905 #if defined(OS_WIN) && defined(USE_ASH) && !defined(NDEBUG)
906 if (base::win::GetVersion() < base::win::VERSION_WIN8 &&
907 chrome::HOST_DESKTOP_TYPE_NATIVE != chrome::HOST_DESKTOP_TYPE_ASH)
908 command_updater_.UpdateCommandEnabled(IDC_TOGGLE_ASH_DESKTOP, true);
909 #endif
910 #if defined(USE_ASH)
911 command_updater_.UpdateCommandEnabled(IDC_MINIMIZE_WINDOW, true);
912 #endif
913 #if defined(OS_CHROMEOS)
914 command_updater_.UpdateCommandEnabled(IDC_VISIT_DESKTOP_OF_LRU_USER_2, true);
915 command_updater_.UpdateCommandEnabled(IDC_VISIT_DESKTOP_OF_LRU_USER_3, true);
916 #endif
917 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && !defined(TOOLKIT_GTK)
918 command_updater_.UpdateCommandEnabled(IDC_USE_SYSTEM_TITLE_BAR, true);
919 #endif
921 // Page-related commands
922 command_updater_.UpdateCommandEnabled(IDC_EMAIL_PAGE_LOCATION, true);
923 command_updater_.UpdateCommandEnabled(IDC_BOOKMARK_PAGE_FROM_STAR, true);
924 command_updater_.UpdateCommandEnabled(IDC_ENCODING_AUTO_DETECT, true);
925 command_updater_.UpdateCommandEnabled(IDC_ENCODING_UTF8, true);
926 command_updater_.UpdateCommandEnabled(IDC_ENCODING_UTF16LE, true);
927 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88591, true);
928 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1252, true);
929 command_updater_.UpdateCommandEnabled(IDC_ENCODING_GBK, true);
930 command_updater_.UpdateCommandEnabled(IDC_ENCODING_GB18030, true);
931 command_updater_.UpdateCommandEnabled(IDC_ENCODING_BIG5HKSCS, true);
932 command_updater_.UpdateCommandEnabled(IDC_ENCODING_BIG5, true);
933 command_updater_.UpdateCommandEnabled(IDC_ENCODING_THAI, true);
934 command_updater_.UpdateCommandEnabled(IDC_ENCODING_KOREAN, true);
935 command_updater_.UpdateCommandEnabled(IDC_ENCODING_SHIFTJIS, true);
936 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO2022JP, true);
937 command_updater_.UpdateCommandEnabled(IDC_ENCODING_EUCJP, true);
938 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO885915, true);
939 command_updater_.UpdateCommandEnabled(IDC_ENCODING_MACINTOSH, true);
940 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88592, true);
941 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1250, true);
942 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88595, true);
943 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1251, true);
944 command_updater_.UpdateCommandEnabled(IDC_ENCODING_KOI8R, true);
945 command_updater_.UpdateCommandEnabled(IDC_ENCODING_KOI8U, true);
946 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88597, true);
947 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1253, true);
948 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88594, true);
949 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO885913, true);
950 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1257, true);
951 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88593, true);
952 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO885910, true);
953 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO885914, true);
954 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO885916, true);
955 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1254, true);
956 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88596, true);
957 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1256, true);
958 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88598, true);
959 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88598I, true);
960 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1255, true);
961 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1258, true);
963 // Zoom
964 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MENU, true);
965 command_updater_.UpdateCommandEnabled(IDC_ZOOM_PLUS, true);
966 command_updater_.UpdateCommandEnabled(IDC_ZOOM_NORMAL, true);
967 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MINUS, true);
969 // Show various bits of UI
970 UpdateOpenFileState(&command_updater_);
971 command_updater_.UpdateCommandEnabled(IDC_CREATE_SHORTCUTS, false);
972 UpdateCommandsForDevTools();
973 command_updater_.UpdateCommandEnabled(IDC_TASK_MANAGER, CanOpenTaskManager());
974 command_updater_.UpdateCommandEnabled(IDC_SHOW_HISTORY,
975 !profile()->IsGuestSession());
976 command_updater_.UpdateCommandEnabled(IDC_SHOW_DOWNLOADS, true);
977 command_updater_.UpdateCommandEnabled(IDC_HELP_PAGE_VIA_KEYBOARD, true);
978 command_updater_.UpdateCommandEnabled(IDC_HELP_PAGE_VIA_MENU, true);
979 command_updater_.UpdateCommandEnabled(IDC_BOOKMARKS_MENU,
980 !profile()->IsGuestSession());
981 command_updater_.UpdateCommandEnabled(IDC_RECENT_TABS_MENU,
982 !profile()->IsGuestSession() &&
983 !profile()->IsOffTheRecord());
984 #if defined(OS_CHROMEOS)
985 command_updater_.UpdateCommandEnabled(IDC_TAKE_SCREENSHOT, true);
986 #endif
988 UpdateShowSyncState(true);
990 // Initialize other commands based on the window type.
991 bool normal_window = browser_->is_type_tabbed();
993 // Navigation commands
994 command_updater_.UpdateCommandEnabled(
995 IDC_HOME,
996 normal_window || (CommandLine::ForCurrentProcess()->HasSwitch(
997 switches::kEnableStreamlinedHostedApps) &&
998 browser_->is_app()));
1000 // Window management commands
1001 command_updater_.UpdateCommandEnabled(IDC_SELECT_NEXT_TAB, normal_window);
1002 command_updater_.UpdateCommandEnabled(IDC_SELECT_PREVIOUS_TAB,
1003 normal_window);
1004 command_updater_.UpdateCommandEnabled(IDC_MOVE_TAB_NEXT, normal_window);
1005 command_updater_.UpdateCommandEnabled(IDC_MOVE_TAB_PREVIOUS, normal_window);
1006 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_0, normal_window);
1007 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_1, normal_window);
1008 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_2, normal_window);
1009 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_3, normal_window);
1010 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_4, normal_window);
1011 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_5, normal_window);
1012 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_6, normal_window);
1013 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_7, normal_window);
1014 command_updater_.UpdateCommandEnabled(IDC_SELECT_LAST_TAB, normal_window);
1015 #if defined(OS_WIN)
1016 #if !defined(USE_AURA)
1017 const bool metro_mode = base::win::IsMetroProcess();
1018 #else
1019 const bool metro_mode =
1020 browser_->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH ?
1021 true : false;
1022 #endif
1023 command_updater_.UpdateCommandEnabled(IDC_METRO_SNAP_ENABLE, metro_mode);
1024 command_updater_.UpdateCommandEnabled(IDC_METRO_SNAP_DISABLE, metro_mode);
1025 int restart_mode = metro_mode ?
1026 IDC_WIN8_DESKTOP_RESTART : IDC_WIN8_METRO_RESTART;
1027 command_updater_.UpdateCommandEnabled(restart_mode, normal_window);
1028 #endif
1030 // Show various bits of UI
1031 command_updater_.UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA, normal_window);
1033 // The upgrade entry and the view incompatibility entry should always be
1034 // enabled. Whether they are visible is a separate matter determined on menu
1035 // show.
1036 command_updater_.UpdateCommandEnabled(IDC_UPGRADE_DIALOG, true);
1037 command_updater_.UpdateCommandEnabled(IDC_VIEW_INCOMPATIBILITIES, true);
1039 // Toggle speech input
1040 command_updater_.UpdateCommandEnabled(IDC_TOGGLE_SPEECH_INPUT, true);
1042 // Initialize other commands whose state changes based on fullscreen mode.
1043 UpdateCommandsForFullscreenMode();
1045 UpdateCommandsForContentRestrictionState();
1047 UpdateCommandsForBookmarkEditing();
1049 UpdateCommandsForIncognitoAvailability();
1052 // static
1053 void BrowserCommandController::UpdateSharedCommandsForIncognitoAvailability(
1054 CommandUpdater* command_updater,
1055 Profile* profile) {
1056 IncognitoModePrefs::Availability incognito_availability =
1057 IncognitoModePrefs::GetAvailability(profile->GetPrefs());
1058 command_updater->UpdateCommandEnabled(
1059 IDC_NEW_WINDOW,
1060 incognito_availability != IncognitoModePrefs::FORCED);
1061 command_updater->UpdateCommandEnabled(
1062 IDC_NEW_INCOGNITO_WINDOW,
1063 incognito_availability != IncognitoModePrefs::DISABLED);
1065 // Bookmark manager and settings page/subpages are forced to open in normal
1066 // mode. For this reason we disable these commands when incognito is forced.
1067 const bool command_enabled =
1068 incognito_availability != IncognitoModePrefs::FORCED &&
1069 !profile->IsGuestSession();
1070 command_updater->UpdateCommandEnabled(
1071 IDC_SHOW_BOOKMARK_MANAGER,
1072 browser_defaults::bookmarks_enabled && command_enabled);
1073 ExtensionService* extension_service = profile->GetExtensionService();
1074 bool enable_extensions =
1075 extension_service && extension_service->extensions_enabled();
1076 command_updater->UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS,
1077 enable_extensions && command_enabled);
1079 command_updater->UpdateCommandEnabled(IDC_IMPORT_SETTINGS, command_enabled);
1080 command_updater->UpdateCommandEnabled(IDC_OPTIONS, command_enabled);
1081 command_updater->UpdateCommandEnabled(IDC_SHOW_SIGNIN, command_enabled);
1084 void BrowserCommandController::UpdateCommandsForIncognitoAvailability() {
1085 UpdateSharedCommandsForIncognitoAvailability(&command_updater_, profile());
1087 if (!IsShowingMainUI()) {
1088 command_updater_.UpdateCommandEnabled(IDC_IMPORT_SETTINGS, false);
1089 command_updater_.UpdateCommandEnabled(IDC_OPTIONS, false);
1093 void BrowserCommandController::UpdateCommandsForTabState() {
1094 WebContents* current_web_contents =
1095 browser_->tab_strip_model()->GetActiveWebContents();
1096 if (!current_web_contents) // May be NULL during tab restore.
1097 return;
1099 // Navigation commands
1100 command_updater_.UpdateCommandEnabled(IDC_BACK, CanGoBack(browser_));
1101 command_updater_.UpdateCommandEnabled(IDC_FORWARD, CanGoForward(browser_));
1102 command_updater_.UpdateCommandEnabled(IDC_RELOAD, CanReload(browser_));
1103 command_updater_.UpdateCommandEnabled(IDC_RELOAD_IGNORING_CACHE,
1104 CanReload(browser_));
1105 command_updater_.UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE,
1106 CanReload(browser_));
1108 // Window management commands
1109 command_updater_.UpdateCommandEnabled(IDC_DUPLICATE_TAB,
1110 !browser_->is_app() && CanDuplicateTab(browser_));
1112 // Page-related commands
1113 window()->SetStarredState(
1114 BookmarkTabHelper::FromWebContents(current_web_contents)->is_starred());
1115 window()->ZoomChangedForActiveTab(false);
1116 command_updater_.UpdateCommandEnabled(IDC_VIEW_SOURCE,
1117 CanViewSource(browser_));
1118 command_updater_.UpdateCommandEnabled(IDC_EMAIL_PAGE_LOCATION,
1119 CanEmailPageLocation(browser_));
1120 if (browser_->is_devtools())
1121 command_updater_.UpdateCommandEnabled(IDC_OPEN_FILE, false);
1123 // Changing the encoding is not possible on Chrome-internal webpages.
1124 NavigationController& nc = current_web_contents->GetController();
1125 bool is_chrome_internal = HasInternalURL(nc.GetActiveEntry()) ||
1126 current_web_contents->ShowingInterstitialPage();
1127 command_updater_.UpdateCommandEnabled(IDC_ENCODING_MENU,
1128 !is_chrome_internal && current_web_contents->IsSavable());
1130 // Show various bits of UI
1131 // TODO(pinkerton): Disable app-mode in the model until we implement it
1132 // on the Mac. Be sure to remove both ifdefs. http://crbug.com/13148
1133 #if !defined(OS_MACOSX)
1134 command_updater_.UpdateCommandEnabled(
1135 IDC_CREATE_SHORTCUTS,
1136 CanCreateApplicationShortcuts(browser_));
1137 command_updater_.UpdateCommandEnabled(
1138 IDC_CREATE_HOSTED_APP,
1139 CanCreateApplicationShortcuts(browser_));
1140 #endif
1142 command_updater_.UpdateCommandEnabled(
1143 IDC_TOGGLE_REQUEST_TABLET_SITE,
1144 CanRequestTabletSite(current_web_contents));
1146 UpdateCommandsForContentRestrictionState();
1147 UpdateCommandsForBookmarkEditing();
1148 UpdateCommandsForFind();
1151 void BrowserCommandController::UpdateCommandsForContentRestrictionState() {
1152 int restrictions = GetContentRestrictions(browser_);
1154 command_updater_.UpdateCommandEnabled(
1155 IDC_COPY, !(restrictions & CONTENT_RESTRICTION_COPY));
1156 command_updater_.UpdateCommandEnabled(
1157 IDC_CUT, !(restrictions & CONTENT_RESTRICTION_CUT));
1158 command_updater_.UpdateCommandEnabled(
1159 IDC_PASTE, !(restrictions & CONTENT_RESTRICTION_PASTE));
1160 UpdateSaveAsState();
1161 UpdatePrintingState();
1164 void BrowserCommandController::UpdateCommandsForDevTools() {
1165 bool dev_tools_enabled =
1166 !profile()->GetPrefs()->GetBoolean(prefs::kDevToolsDisabled);
1167 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS,
1168 dev_tools_enabled);
1169 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_CONSOLE,
1170 dev_tools_enabled);
1171 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_DEVICES,
1172 dev_tools_enabled);
1173 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_INSPECT,
1174 dev_tools_enabled);
1175 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_TOGGLE,
1176 dev_tools_enabled);
1179 void BrowserCommandController::UpdateCommandsForBookmarkEditing() {
1180 command_updater_.UpdateCommandEnabled(IDC_BOOKMARK_PAGE,
1181 CanBookmarkCurrentPage(browser_));
1182 command_updater_.UpdateCommandEnabled(IDC_BOOKMARK_ALL_TABS,
1183 CanBookmarkAllTabs(browser_));
1184 command_updater_.UpdateCommandEnabled(IDC_PIN_TO_START_SCREEN,
1185 true);
1188 void BrowserCommandController::UpdateCommandsForBookmarkBar() {
1189 command_updater_.UpdateCommandEnabled(IDC_SHOW_BOOKMARK_BAR,
1190 browser_defaults::bookmarks_enabled &&
1191 !profile()->GetPrefs()->IsManagedPreference(prefs::kShowBookmarkBar) &&
1192 IsShowingMainUI());
1195 void BrowserCommandController::UpdateCommandsForFileSelectionDialogs() {
1196 UpdateSaveAsState();
1197 UpdateOpenFileState(&command_updater_);
1200 void BrowserCommandController::UpdateCommandsForFullscreenMode() {
1201 WindowState window_state = WINDOW_STATE_NOT_FULLSCREEN;
1202 if (window() && window()->IsFullscreen()) {
1203 window_state = WINDOW_STATE_FULLSCREEN;
1204 #if defined(OS_WIN)
1205 if (window()->IsInMetroSnapMode())
1206 window_state = WINDOW_STATE_METRO_SNAP;
1207 #endif
1209 bool show_main_ui = IsShowingMainUI();
1210 bool main_not_fullscreen =
1211 show_main_ui && window_state == WINDOW_STATE_NOT_FULLSCREEN;
1213 // Navigation commands
1214 command_updater_.UpdateCommandEnabled(IDC_OPEN_CURRENT_URL, show_main_ui);
1216 // Window management commands
1217 command_updater_.UpdateCommandEnabled(
1218 IDC_SHOW_AS_TAB,
1219 !browser_->is_type_tabbed() &&
1220 window_state == WINDOW_STATE_NOT_FULLSCREEN);
1222 // Focus various bits of UI
1223 command_updater_.UpdateCommandEnabled(IDC_FOCUS_TOOLBAR, show_main_ui);
1224 command_updater_.UpdateCommandEnabled(IDC_FOCUS_LOCATION, show_main_ui);
1225 command_updater_.UpdateCommandEnabled(IDC_FOCUS_SEARCH, show_main_ui);
1226 command_updater_.UpdateCommandEnabled(
1227 IDC_FOCUS_MENU_BAR, main_not_fullscreen);
1228 command_updater_.UpdateCommandEnabled(
1229 IDC_FOCUS_NEXT_PANE, main_not_fullscreen);
1230 command_updater_.UpdateCommandEnabled(
1231 IDC_FOCUS_PREVIOUS_PANE, main_not_fullscreen);
1232 command_updater_.UpdateCommandEnabled(
1233 IDC_FOCUS_BOOKMARKS, main_not_fullscreen);
1234 command_updater_.UpdateCommandEnabled(
1235 IDC_FOCUS_INFOBARS, main_not_fullscreen);
1237 // Show various bits of UI
1238 command_updater_.UpdateCommandEnabled(IDC_DEVELOPER_MENU, show_main_ui);
1239 #if defined(GOOGLE_CHROME_BUILD)
1240 command_updater_.UpdateCommandEnabled(IDC_FEEDBACK, show_main_ui);
1241 #endif
1242 UpdateShowSyncState(show_main_ui);
1244 // Settings page/subpages are forced to open in normal mode. We disable these
1245 // commands when incognito is forced.
1246 const bool options_enabled = show_main_ui &&
1247 IncognitoModePrefs::GetAvailability(
1248 profile()->GetPrefs()) != IncognitoModePrefs::FORCED;
1249 command_updater_.UpdateCommandEnabled(IDC_OPTIONS, options_enabled);
1250 command_updater_.UpdateCommandEnabled(IDC_IMPORT_SETTINGS, options_enabled);
1252 command_updater_.UpdateCommandEnabled(IDC_EDIT_SEARCH_ENGINES, show_main_ui);
1253 command_updater_.UpdateCommandEnabled(IDC_VIEW_PASSWORDS, show_main_ui);
1254 command_updater_.UpdateCommandEnabled(IDC_ABOUT, show_main_ui);
1255 command_updater_.UpdateCommandEnabled(IDC_SHOW_APP_MENU, show_main_ui);
1256 #if defined (ENABLE_PROFILING) && !defined(NO_TCMALLOC)
1257 command_updater_.UpdateCommandEnabled(IDC_PROFILING_ENABLED, show_main_ui);
1258 #endif
1260 // Disable explicit fullscreen toggling when in metro snap mode.
1261 bool fullscreen_enabled = window_state != WINDOW_STATE_METRO_SNAP;
1262 #if defined(OS_MACOSX)
1263 // The Mac implementation doesn't support switching to fullscreen while
1264 // a tab modal dialog is displayed.
1265 int tab_index = chrome::IndexOfFirstBlockedTab(browser_->tab_strip_model());
1266 bool has_blocked_tab = tab_index != browser_->tab_strip_model()->count();
1267 fullscreen_enabled &= !has_blocked_tab;
1268 #else
1269 if (window_state == WINDOW_STATE_NOT_FULLSCREEN &&
1270 !profile()->GetPrefs()->GetBoolean(prefs::kFullscreenAllowed)) {
1271 // Disable toggling into fullscreen mode if disallowed by pref.
1272 fullscreen_enabled = false;
1274 #endif
1276 command_updater_.UpdateCommandEnabled(IDC_FULLSCREEN, fullscreen_enabled);
1277 command_updater_.UpdateCommandEnabled(IDC_PRESENTATION_MODE,
1278 fullscreen_enabled);
1280 UpdateCommandsForBookmarkBar();
1281 UpdateCommandsForMultipleProfiles();
1284 void BrowserCommandController::UpdateCommandsForMultipleProfiles() {
1285 bool is_regular_or_guest_session =
1286 profile()->IsGuestSession() || !profile()->IsOffTheRecord();
1287 bool enable = IsShowingMainUI() &&
1288 is_regular_or_guest_session &&
1289 profile_manager_ &&
1290 AvatarMenu::ShouldShowAvatarMenu();
1291 command_updater_.UpdateCommandEnabled(IDC_SHOW_AVATAR_MENU,
1292 enable);
1295 void BrowserCommandController::UpdatePrintingState() {
1296 bool print_enabled = CanPrint(browser_);
1297 command_updater_.UpdateCommandEnabled(IDC_PRINT, print_enabled);
1298 command_updater_.UpdateCommandEnabled(IDC_ADVANCED_PRINT,
1299 CanAdvancedPrint(browser_));
1300 command_updater_.UpdateCommandEnabled(IDC_PRINT_TO_DESTINATION,
1301 print_enabled);
1302 #if defined(OS_WIN)
1303 HMODULE metro_module = base::win::GetMetroModule();
1304 if (metro_module != NULL) {
1305 typedef void (*MetroEnablePrinting)(BOOL);
1306 MetroEnablePrinting metro_enable_printing =
1307 reinterpret_cast<MetroEnablePrinting>(
1308 ::GetProcAddress(metro_module, "MetroEnablePrinting"));
1309 if (metro_enable_printing)
1310 metro_enable_printing(print_enabled);
1312 #endif
1315 void BrowserCommandController::UpdateSaveAsState() {
1316 command_updater_.UpdateCommandEnabled(IDC_SAVE_PAGE, CanSavePage(browser_));
1319 void BrowserCommandController::UpdateShowSyncState(bool show_main_ui) {
1320 command_updater_.UpdateCommandEnabled(
1321 IDC_SHOW_SYNC_SETUP, show_main_ui && pref_signin_allowed_.GetValue());
1324 // static
1325 void BrowserCommandController::UpdateOpenFileState(
1326 CommandUpdater* command_updater) {
1327 bool enabled = true;
1328 PrefService* local_state = g_browser_process->local_state();
1329 if (local_state)
1330 enabled = local_state->GetBoolean(prefs::kAllowFileSelectionDialogs);
1332 command_updater->UpdateCommandEnabled(IDC_OPEN_FILE, enabled);
1335 void BrowserCommandController::UpdateReloadStopState(bool is_loading,
1336 bool force) {
1337 window()->UpdateReloadStopState(is_loading, force);
1338 command_updater_.UpdateCommandEnabled(IDC_STOP, is_loading);
1341 void BrowserCommandController::UpdateCommandsForFind() {
1342 TabStripModel* model = browser_->tab_strip_model();
1343 bool enabled = !model->IsTabBlocked(model->active_index()) &&
1344 !browser_->is_devtools();
1346 command_updater_.UpdateCommandEnabled(IDC_FIND, enabled);
1347 command_updater_.UpdateCommandEnabled(IDC_FIND_NEXT, enabled);
1348 command_updater_.UpdateCommandEnabled(IDC_FIND_PREVIOUS, enabled);
1351 void BrowserCommandController::AddInterstitialObservers(WebContents* contents) {
1352 interstitial_observers_.push_back(new InterstitialObserver(this, contents));
1355 void BrowserCommandController::RemoveInterstitialObservers(
1356 WebContents* contents) {
1357 for (size_t i = 0; i < interstitial_observers_.size(); i++) {
1358 if (interstitial_observers_[i]->web_contents() != contents)
1359 continue;
1361 delete interstitial_observers_[i];
1362 interstitial_observers_.erase(interstitial_observers_.begin() + i);
1363 return;
1367 BrowserWindow* BrowserCommandController::window() {
1368 return browser_->window();
1371 Profile* BrowserCommandController::profile() {
1372 return browser_->profile();
1375 } // namespace chrome