blob: 436272f1694c4c314372f1a5c8bfcfa14bddd1d8 [file] [log] [blame]
[email protected]5d98294912012-06-27 22:57:401// 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.
4
5#include "chrome/browser/ui/browser_command_controller.h"
6
7#include "chrome/app/chrome_command_ids.h"
8#include "chrome/browser/browser_process.h"
9#include "chrome/browser/defaults.h"
10#include "chrome/browser/extensions/extension_service.h"
11#include "chrome/browser/prefs/incognito_mode_prefs.h"
12#include "chrome/browser/prefs/pref_service.h"
13#include "chrome/browser/printing/print_preview_tab_controller.h"
14#include "chrome/browser/profiles/profile.h"
15#include "chrome/browser/profiles/profile_manager.h"
16#include "chrome/browser/sessions/tab_restore_service.h"
17#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]0b32f9b62012-09-17 19:08:0318#include "chrome/browser/shell_integration.h"
[email protected]5d98294912012-06-27 22:57:4019#include "chrome/browser/sync/profile_sync_service.h"
20#include "chrome/browser/sync/profile_sync_service_factory.h"
21#include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
22#include "chrome/browser/ui/browser.h"
23#include "chrome/browser/ui/browser_commands.h"
[email protected]52877dbc62012-06-29 22:22:0324#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]5d98294912012-06-27 22:57:4025#include "chrome/browser/ui/browser_window.h"
26#include "chrome/browser/ui/chrome_pages.h"
27#include "chrome/browser/ui/tab_contents/tab_contents.h"
28#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]00a1cc5b2012-11-07 13:44:5129#include "chrome/browser/ui/tabs/tab_strip_model_utils.h"
[email protected]5d98294912012-06-27 22:57:4030#include "chrome/browser/ui/webui/sync_promo/sync_promo_ui.h"
31#include "chrome/common/chrome_notification_types.h"
32#include "chrome/common/pref_names.h"
33#include "chrome/common/profiling.h"
34#include "content/public/browser/native_web_keyboard_event.h"
35#include "content/public/browser/navigation_controller.h"
36#include "content/public/browser/navigation_entry.h"
37#include "content/public/browser/notification_details.h"
38#include "content/public/browser/notification_source.h"
[email protected]6708dbc2012-11-04 00:17:2239#include "content/public/browser/user_metrics.h"
[email protected]5d98294912012-06-27 22:57:4040#include "content/public/browser/web_contents.h"
41#include "content/public/common/content_restriction.h"
42#include "content/public/common/url_constants.h"
43#include "ui/base/keycodes/keyboard_codes.h"
44
45#if defined(OS_WIN)
46#include "base/win/metro.h"
47#endif
48
[email protected]4b0bcef2012-09-27 00:20:5649#if defined(USE_ASH)
50#include "chrome/browser/ui/ash/ash_util.h"
51#endif
52
[email protected]5d98294912012-06-27 22:57:4053using content::WebContents;
54using content::NavigationEntry;
55using content::NavigationController;
56using content::WebContents;
57
58namespace {
59
60// Returns |true| if entry has an internal chrome:// URL, |false| otherwise.
61bool HasInternalURL(const NavigationEntry* entry) {
62 if (!entry)
63 return false;
64
65 // Check the |virtual_url()| first. This catches regular chrome:// URLs
66 // including URLs that were rewritten (such as chrome://bookmarks).
67 if (entry->GetVirtualURL().SchemeIs(chrome::kChromeUIScheme))
68 return true;
69
70 // If the |virtual_url()| isn't a chrome:// URL, check if it's actually
71 // view-source: of a chrome:// URL.
72 if (entry->GetVirtualURL().SchemeIs(chrome::kViewSourceScheme))
73 return entry->GetURL().SchemeIs(chrome::kChromeUIScheme);
74
75 return false;
76}
77
[email protected]0b32f9b62012-09-17 19:08:0378#if defined(OS_WIN)
79// Windows 8 specific helper class to manage DefaultBrowserWorker. It does the
80// following asynchronous actions in order:
81// 1- Check that chrome is the default browser
82// 2- If we are the default, restart chrome in metro and exit
83// 3- If not the default browser show the 'select default browser' system dialog
84// 4- When dialog dismisses check again who got made the default
85// 5- If we are the default then restart chrome in metro and exit
86// 6- If we are not the default exit.
87//
88// Note: this class deletes itself.
89class SwichToMetroUIHandler
90 : public ShellIntegration::DefaultWebClientObserver {
91 public:
92 SwichToMetroUIHandler()
93 : ALLOW_THIS_IN_INITIALIZER_LIST(default_browser_worker_(
94 new ShellIntegration::DefaultBrowserWorker(this))),
95 first_check_(true) {
96 default_browser_worker_->StartCheckIsDefault();
97 }
98
99 virtual ~SwichToMetroUIHandler() {
100 default_browser_worker_->ObserverDestroyed();
101 }
102
103 private:
104 virtual void SetDefaultWebClientUIState(
105 ShellIntegration::DefaultWebClientUIState state) OVERRIDE {
106 switch (state) {
107 case ShellIntegration::STATE_PROCESSING:
108 return;
109 case ShellIntegration::STATE_UNKNOWN :
110 break;
111 case ShellIntegration::STATE_IS_DEFAULT:
112 browser::AttemptRestartWithModeSwitch();
113 break;
114 case ShellIntegration::STATE_NOT_DEFAULT:
115 if (first_check_) {
116 default_browser_worker_->StartSetAsDefault();
117 return;
118 }
119 break;
120 default:
121 NOTREACHED();
122 }
123 delete this;
124 }
125
126 virtual void OnSetAsDefaultConcluded(bool success) OVERRIDE {
127 if (!success) {
128 delete this;
129 return;
130 }
131 first_check_ = false;
132 default_browser_worker_->StartCheckIsDefault();
133 }
134
135 virtual bool IsInteractiveSetDefaultPermitted() OVERRIDE {
136 return true;
137 }
138
139 scoped_refptr<ShellIntegration::DefaultBrowserWorker> default_browser_worker_;
140 bool first_check_;
141
142 DISALLOW_COPY_AND_ASSIGN(SwichToMetroUIHandler);
143};
144#endif // defined(OS_WIN)
145
[email protected]5d98294912012-06-27 22:57:40146} // namespace
147
148namespace chrome {
149
150///////////////////////////////////////////////////////////////////////////////
151// BrowserCommandController, public:
152
153BrowserCommandController::BrowserCommandController(Browser* browser)
154 : browser_(browser),
155 ALLOW_THIS_IN_INITIALIZER_LIST(command_updater_(this)),
156 block_command_execution_(false),
157 last_blocked_command_id_(-1),
158 last_blocked_command_disposition_(CURRENT_TAB) {
159 browser_->tab_strip_model()->AddObserver(this);
160 PrefService* local_state = g_browser_process->local_state();
161 if (local_state) {
162 local_pref_registrar_.Init(local_state);
[email protected]5d98294912012-06-27 22:57:40163 local_pref_registrar_.Add(prefs::kAllowFileSelectionDialogs, this);
164 local_pref_registrar_.Add(prefs::kInManagedMode, this);
165 }
166
167 profile_pref_registrar_.Init(profile()->GetPrefs());
168 profile_pref_registrar_.Add(prefs::kDevToolsDisabled, this);
169 profile_pref_registrar_.Add(prefs::kEditBookmarksEnabled, this);
170 profile_pref_registrar_.Add(prefs::kShowBookmarkBar, this);
171 profile_pref_registrar_.Add(prefs::kIncognitoModeAvailability, this);
[email protected]0045b0f42012-07-26 11:52:08172 profile_pref_registrar_.Add(prefs::kPrintingEnabled, this);
[email protected]5d98294912012-06-27 22:57:40173
174 InitCommandState();
175
176 TabRestoreService* tab_restore_service =
177 TabRestoreServiceFactory::GetForProfile(profile());
178 if (tab_restore_service) {
179 tab_restore_service->AddObserver(this);
180 TabRestoreServiceChanged(tab_restore_service);
181 }
182
183 ProfileSyncService* service =
184 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile());
185 if (service)
186 service->AddObserver(this);
187}
188
189BrowserCommandController::~BrowserCommandController() {
190 ProfileSyncService* service =
191 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile());
192 if (service)
193 service->RemoveObserver(this);
194
[email protected]95e39472012-10-05 23:37:36195 // TabRestoreService may have been shutdown by the time we get here. Don't
196 // trigger creating it.
[email protected]5d98294912012-06-27 22:57:40197 TabRestoreService* tab_restore_service =
[email protected]95e39472012-10-05 23:37:36198 TabRestoreServiceFactory::GetForProfileIfExisting(profile());
[email protected]5d98294912012-06-27 22:57:40199 if (tab_restore_service)
200 tab_restore_service->RemoveObserver(this);
201 profile_pref_registrar_.RemoveAll();
202 local_pref_registrar_.RemoveAll();
203 browser_->tab_strip_model()->RemoveObserver(this);
204}
205
206bool BrowserCommandController::IsReservedCommandOrKey(
207 int command_id,
208 const content::NativeWebKeyboardEvent& event) {
209 // In Apps mode, no keys are reserved.
210 if (browser_->is_app())
211 return false;
212
213#if defined(OS_CHROMEOS)
[email protected]3642e2d2012-10-29 21:31:14214 // On Chrome OS, the top row of keys are mapped to F1-F10. We don't want web
215 // pages to be able to change the behavior of these keys. Ash handles F4 and
216 // up; this leaves us needing to reserve F1-F3 here.
[email protected]5d98294912012-06-27 22:57:40217 ui::KeyboardCode key_code =
[email protected]3642e2d2012-10-29 21:31:14218 static_cast<ui::KeyboardCode>(event.windowsKeyCode);
219 if ((key_code == ui::VKEY_F1 && command_id == IDC_BACK) ||
220 (key_code == ui::VKEY_F2 && command_id == IDC_FORWARD) ||
221 (key_code == ui::VKEY_F3 && command_id == IDC_RELOAD)) {
[email protected]5d98294912012-06-27 22:57:40222 return true;
223 }
224#endif
225
226 if (window()->IsFullscreen() && command_id == IDC_FULLSCREEN)
227 return true;
228 return command_id == IDC_CLOSE_TAB ||
229 command_id == IDC_CLOSE_WINDOW ||
230 command_id == IDC_NEW_INCOGNITO_WINDOW ||
231 command_id == IDC_NEW_TAB ||
232 command_id == IDC_NEW_WINDOW ||
233 command_id == IDC_RESTORE_TAB ||
234 command_id == IDC_SELECT_NEXT_TAB ||
235 command_id == IDC_SELECT_PREVIOUS_TAB ||
236 command_id == IDC_TABPOSE ||
[email protected]3642e2d2012-10-29 21:31:14237 command_id == IDC_EXIT;
[email protected]5d98294912012-06-27 22:57:40238}
239
240void BrowserCommandController::SetBlockCommandExecution(bool block) {
241 block_command_execution_ = block;
242 if (block) {
243 last_blocked_command_id_ = -1;
244 last_blocked_command_disposition_ = CURRENT_TAB;
245 }
246}
247
248int BrowserCommandController::GetLastBlockedCommand(
249 WindowOpenDisposition* disposition) {
250 if (disposition)
251 *disposition = last_blocked_command_disposition_;
252 return last_blocked_command_id_;
253}
254
255void BrowserCommandController::TabStateChanged() {
256 UpdateCommandsForTabState();
257}
258
259void BrowserCommandController::ContentRestrictionsChanged() {
260 UpdateCommandsForContentRestrictionState();
261}
262
263void BrowserCommandController::FullscreenStateChanged() {
264 FullScreenMode fullscreen_mode = FULLSCREEN_DISABLED;
265 if (window()->IsFullscreen()) {
266#if defined(OS_WIN)
267 fullscreen_mode = window()->IsInMetroSnapMode() ? FULLSCREEN_METRO_SNAP :
268 FULLSCREEN_NORMAL;
269#else
270 fullscreen_mode = FULLSCREEN_NORMAL;
271#endif
272 }
273 UpdateCommandsForFullscreenMode(fullscreen_mode);
274}
275
276void BrowserCommandController::PrintingStateChanged() {
277 UpdatePrintingState();
278}
279
280void BrowserCommandController::LoadingStateChanged(bool is_loading,
281 bool force) {
282 UpdateReloadStopState(is_loading, force);
283}
284
[email protected]5d98294912012-06-27 22:57:40285////////////////////////////////////////////////////////////////////////////////
286// BrowserCommandController,
287// CommandUpdater::CommandUpdaterDelegate implementation:
288
289void BrowserCommandController::ExecuteCommandWithDisposition(
290 int id, WindowOpenDisposition disposition) {
291 // No commands are enabled if there is not yet any selected tab.
292 // TODO(pkasting): It seems like we should not need this, because either
293 // most/all commands should not have been enabled yet anyway or the ones that
294 // are enabled should be global, or safe themselves against having no selected
295 // tab. However, Ben says he tried removing this before and got lots of
296 // crashes, e.g. from Windows sending WM_COMMANDs at random times during
297 // window construction. This probably could use closer examination someday.
[email protected]52877dbc62012-06-29 22:22:03298 if (!chrome::GetActiveTabContents(browser_))
[email protected]5d98294912012-06-27 22:57:40299 return;
300
301 DCHECK(command_updater_.IsCommandEnabled(id)) << "Invalid/disabled command "
302 << id;
303
304 // If command execution is blocked then just record the command and return.
305 if (block_command_execution_) {
306 // We actually only allow no more than one blocked command, otherwise some
307 // commands maybe lost.
308 DCHECK_EQ(last_blocked_command_id_, -1);
309 last_blocked_command_id_ = id;
310 last_blocked_command_disposition_ = disposition;
311 return;
312 }
313
314 // The order of commands in this switch statement must match the function
315 // declaration order in browser.h!
316 switch (id) {
317 // Navigation commands
318 case IDC_BACK:
319 GoBack(browser_, disposition);
320 break;
321 case IDC_FORWARD:
322 GoForward(browser_, disposition);
323 break;
324 case IDC_RELOAD:
325 Reload(browser_, disposition);
326 break;
[email protected]58e29032012-08-06 20:19:57327 case IDC_RELOAD_CLEARING_CACHE:
328 ClearCache(browser_);
329 // FALL THROUGH
[email protected]5d98294912012-06-27 22:57:40330 case IDC_RELOAD_IGNORING_CACHE:
331 ReloadIgnoringCache(browser_, disposition);
332 break;
333 case IDC_HOME:
334 Home(browser_, disposition);
335 break;
336 case IDC_OPEN_CURRENT_URL:
337 OpenCurrentURL(browser_);
338 break;
339 case IDC_STOP:
340 Stop(browser_);
341 break;
342
343 // Window management commands
344 case IDC_NEW_WINDOW:
345 NewWindow(browser_);
346 break;
347 case IDC_NEW_INCOGNITO_WINDOW:
348 NewIncognitoWindow(browser_);
349 break;
350 case IDC_CLOSE_WINDOW:
[email protected]04b9e692012-08-24 14:49:09351 CloseWindow(browser_);
[email protected]5d98294912012-06-27 22:57:40352 break;
353 case IDC_NEW_TAB:
354 NewTab(browser_);
355 break;
356 case IDC_CLOSE_TAB:
[email protected]04b9e692012-08-24 14:49:09357 CloseTab(browser_);
[email protected]5d98294912012-06-27 22:57:40358 break;
359 case IDC_SELECT_NEXT_TAB:
360 SelectNextTab(browser_);
361 break;
362 case IDC_SELECT_PREVIOUS_TAB:
363 SelectPreviousTab(browser_);
364 break;
365 case IDC_TABPOSE:
366 OpenTabpose(browser_);
367 break;
368 case IDC_MOVE_TAB_NEXT:
369 MoveTabNext(browser_);
370 break;
371 case IDC_MOVE_TAB_PREVIOUS:
372 MoveTabPrevious(browser_);
373 break;
374 case IDC_SELECT_TAB_0:
375 case IDC_SELECT_TAB_1:
376 case IDC_SELECT_TAB_2:
377 case IDC_SELECT_TAB_3:
378 case IDC_SELECT_TAB_4:
379 case IDC_SELECT_TAB_5:
380 case IDC_SELECT_TAB_6:
381 case IDC_SELECT_TAB_7:
382 SelectNumberedTab(browser_, id - IDC_SELECT_TAB_0);
383 break;
384 case IDC_SELECT_LAST_TAB:
385 SelectLastTab(browser_);
386 break;
387 case IDC_DUPLICATE_TAB:
388 DuplicateTab(browser_);
389 break;
390 case IDC_RESTORE_TAB:
391 RestoreTab(browser_);
392 break;
[email protected]5d98294912012-06-27 22:57:40393 case IDC_SHOW_AS_TAB:
394 ConvertPopupToTabbedBrowser(browser_);
395 break;
396 case IDC_FULLSCREEN:
[email protected]3f32b9b2012-07-09 16:59:28397 chrome::ToggleFullscreenMode(browser_);
[email protected]5d98294912012-06-27 22:57:40398 break;
[email protected]770c6d82012-09-06 22:21:32399
[email protected]4b0bcef2012-09-27 00:20:56400#if defined(USE_ASH)
401 case IDC_TOGGLE_ASH_DESKTOP:
402 chrome::ToggleAshDesktop();
403 break;
404#endif
405
[email protected]5d98294912012-06-27 22:57:40406#if defined(OS_WIN)
[email protected]770c6d82012-09-06 22:21:32407 // Windows 8 specific commands.
[email protected]5d98294912012-06-27 22:57:40408 case IDC_METRO_SNAP_ENABLE:
409 browser_->SetMetroSnapMode(true);
410 break;
411 case IDC_METRO_SNAP_DISABLE:
412 browser_->SetMetroSnapMode(false);
413 break;
[email protected]770c6d82012-09-06 22:21:32414 case IDC_WIN8_DESKTOP_RESTART:
[email protected]81852552012-09-07 21:39:29415 browser::AttemptRestartWithModeSwitch();
[email protected]6708dbc2012-11-04 00:17:22416 content::RecordAction(content::UserMetricsAction("Win8DesktopRestart"));
[email protected]770c6d82012-09-06 22:21:32417 break;
[email protected]0b32f9b62012-09-17 19:08:03418 case IDC_WIN8_METRO_RESTART:
419 new SwichToMetroUIHandler;
[email protected]6708dbc2012-11-04 00:17:22420 content::RecordAction(content::UserMetricsAction("Win8MetroRestart"));
[email protected]0b32f9b62012-09-17 19:08:03421 break;
[email protected]5d98294912012-06-27 22:57:40422#endif
[email protected]770c6d82012-09-06 22:21:32423
[email protected]5d98294912012-06-27 22:57:40424#if defined(OS_MACOSX)
425 case IDC_PRESENTATION_MODE:
426 browser_->TogglePresentationMode();
427 break;
428#endif
429 case IDC_EXIT:
430 Exit();
431 break;
432
433 // Page-related commands
434 case IDC_SAVE_PAGE:
435 SavePage(browser_);
436 break;
437 case IDC_BOOKMARK_PAGE:
438 BookmarkCurrentPage(browser_);
439 break;
[email protected]172b67f2012-11-02 05:32:39440 case IDC_BOOKMARK_PAGE_FROM_STAR:
441 BookmarkCurrentPageFromStar(browser_);
442 break;
[email protected]e6ba5a12012-08-07 02:05:53443 case IDC_PIN_TO_START_SCREEN:
[email protected]ffe6de62012-07-19 00:02:35444 TogglePagePinnedToStartScreen(browser_);
[email protected]5d98294912012-06-27 22:57:40445 break;
446 case IDC_BOOKMARK_ALL_TABS:
447 BookmarkAllTabs(browser_);
448 break;
449 case IDC_VIEW_SOURCE:
450 ViewSelectedSource(browser_);
451 break;
452 case IDC_EMAIL_PAGE_LOCATION:
453 EmailPageLocation(browser_);
454 break;
455 case IDC_PRINT:
456 Print(browser_);
457 break;
458 case IDC_ADVANCED_PRINT:
459 AdvancedPrint(browser_);
460 break;
[email protected]d53e4032012-06-29 18:58:34461 case IDC_PRINT_TO_DESTINATION:
462 PrintToDestination(browser_);
463 break;
[email protected]5d98294912012-06-27 22:57:40464 case IDC_CHROME_TO_MOBILE_PAGE:
465 ShowChromeToMobileBubble(browser_);
466 break;
467 case IDC_ENCODING_AUTO_DETECT:
468 browser_->ToggleEncodingAutoDetect();
469 break;
470 case IDC_ENCODING_UTF8:
471 case IDC_ENCODING_UTF16LE:
472 case IDC_ENCODING_ISO88591:
473 case IDC_ENCODING_WINDOWS1252:
474 case IDC_ENCODING_GBK:
475 case IDC_ENCODING_GB18030:
476 case IDC_ENCODING_BIG5HKSCS:
477 case IDC_ENCODING_BIG5:
478 case IDC_ENCODING_KOREAN:
479 case IDC_ENCODING_SHIFTJIS:
480 case IDC_ENCODING_ISO2022JP:
481 case IDC_ENCODING_EUCJP:
482 case IDC_ENCODING_THAI:
483 case IDC_ENCODING_ISO885915:
484 case IDC_ENCODING_MACINTOSH:
485 case IDC_ENCODING_ISO88592:
486 case IDC_ENCODING_WINDOWS1250:
487 case IDC_ENCODING_ISO88595:
488 case IDC_ENCODING_WINDOWS1251:
489 case IDC_ENCODING_KOI8R:
490 case IDC_ENCODING_KOI8U:
491 case IDC_ENCODING_ISO88597:
492 case IDC_ENCODING_WINDOWS1253:
493 case IDC_ENCODING_ISO88594:
494 case IDC_ENCODING_ISO885913:
495 case IDC_ENCODING_WINDOWS1257:
496 case IDC_ENCODING_ISO88593:
497 case IDC_ENCODING_ISO885910:
498 case IDC_ENCODING_ISO885914:
499 case IDC_ENCODING_ISO885916:
500 case IDC_ENCODING_WINDOWS1254:
501 case IDC_ENCODING_ISO88596:
502 case IDC_ENCODING_WINDOWS1256:
503 case IDC_ENCODING_ISO88598:
504 case IDC_ENCODING_ISO88598I:
505 case IDC_ENCODING_WINDOWS1255:
506 case IDC_ENCODING_WINDOWS1258:
507 browser_->OverrideEncoding(id);
508 break;
509
510 // Clipboard commands
511 case IDC_CUT:
512 Cut(browser_);
513 break;
514 case IDC_COPY:
515 Copy(browser_);
516 break;
517 case IDC_PASTE:
518 Paste(browser_);
519 break;
520
521 // Find-in-page
522 case IDC_FIND:
523 Find(browser_);
524 break;
525 case IDC_FIND_NEXT:
526 FindNext(browser_);
527 break;
528 case IDC_FIND_PREVIOUS:
529 FindPrevious(browser_);
530 break;
531
532 // Zoom
533 case IDC_ZOOM_PLUS:
534 Zoom(browser_, content::PAGE_ZOOM_IN);
535 break;
536 case IDC_ZOOM_NORMAL:
537 Zoom(browser_, content::PAGE_ZOOM_RESET);
538 break;
539 case IDC_ZOOM_MINUS:
540 Zoom(browser_, content::PAGE_ZOOM_OUT);
541 break;
542
543 // Focus various bits of UI
544 case IDC_FOCUS_TOOLBAR:
545 FocusToolbar(browser_);
546 break;
547 case IDC_FOCUS_LOCATION:
548 FocusLocationBar(browser_);
549 break;
550 case IDC_FOCUS_SEARCH:
551 FocusSearch(browser_);
552 break;
553 case IDC_FOCUS_MENU_BAR:
554 FocusAppMenu(browser_);
555 break;
556 case IDC_FOCUS_BOOKMARKS:
557 FocusBookmarksToolbar(browser_);
558 break;
559 case IDC_FOCUS_NEXT_PANE:
560 FocusNextPane(browser_);
561 break;
562 case IDC_FOCUS_PREVIOUS_PANE:
563 FocusPreviousPane(browser_);
564 break;
565
566 // Show various bits of UI
567 case IDC_OPEN_FILE:
568 browser_->OpenFile();
569 break;
570 case IDC_CREATE_SHORTCUTS:
[email protected]619f86182012-07-03 21:30:18571 CreateApplicationShortcuts(browser_);
[email protected]5d98294912012-06-27 22:57:40572 break;
573 case IDC_DEV_TOOLS:
[email protected]d16657c2012-09-03 14:25:10574 ToggleDevToolsWindow(browser_, DEVTOOLS_TOGGLE_ACTION_SHOW);
[email protected]5d98294912012-06-27 22:57:40575 break;
576 case IDC_DEV_TOOLS_CONSOLE:
577 ToggleDevToolsWindow(browser_, DEVTOOLS_TOGGLE_ACTION_SHOW_CONSOLE);
578 break;
579 case IDC_DEV_TOOLS_INSPECT:
580 ToggleDevToolsWindow(browser_, DEVTOOLS_TOGGLE_ACTION_INSPECT);
581 break;
[email protected]d16657c2012-09-03 14:25:10582 case IDC_DEV_TOOLS_TOGGLE:
583 ToggleDevToolsWindow(browser_, DEVTOOLS_TOGGLE_ACTION_TOGGLE);
584 break;
[email protected]5d98294912012-06-27 22:57:40585 case IDC_TASK_MANAGER:
586 OpenTaskManager(browser_, false);
587 break;
588 case IDC_VIEW_BACKGROUND_PAGES:
589 OpenTaskManager(browser_, true);
590 break;
591 case IDC_FEEDBACK:
592 OpenFeedbackDialog(browser_);
593 break;
594
595 case IDC_SHOW_BOOKMARK_BAR:
596 ToggleBookmarkBar(browser_);
597 break;
598 case IDC_PROFILING_ENABLED:
599 Profiling::Toggle();
600 break;
601
602 case IDC_SHOW_BOOKMARK_MANAGER:
603 ShowBookmarkManager(browser_);
604 break;
605 case IDC_SHOW_APP_MENU:
606 ShowAppMenu(browser_);
607 break;
608 case IDC_SHOW_AVATAR_MENU:
609 ShowAvatarMenu(browser_);
610 break;
611 case IDC_SHOW_HISTORY:
612 ShowHistory(browser_);
613 break;
614 case IDC_SHOW_DOWNLOADS:
615 ShowDownloads(browser_);
616 break;
617 case IDC_MANAGE_EXTENSIONS:
618 ShowExtensions(browser_);
619 break;
620 case IDC_OPTIONS:
621 ShowSettings(browser_);
622 break;
623 case IDC_EDIT_SEARCH_ENGINES:
624 ShowSearchEngineSettings(browser_);
625 break;
626 case IDC_VIEW_PASSWORDS:
627 ShowPasswordManager(browser_);
628 break;
629 case IDC_CLEAR_BROWSING_DATA:
630 ShowClearBrowsingDataDialog(browser_);
631 break;
632 case IDC_IMPORT_SETTINGS:
633 ShowImportDialog(browser_);
634 break;
[email protected]9b7ab882012-09-10 23:46:36635 case IDC_TOGGLE_REQUEST_TABLET_SITE:
636 ToggleRequestTabletSite(browser_);
637 break;
[email protected]5d98294912012-06-27 22:57:40638 case IDC_ABOUT:
639 ShowAboutChrome(browser_);
640 break;
641 case IDC_UPGRADE_DIALOG:
642 OpenUpdateChromeDialog(browser_);
643 break;
644 case IDC_VIEW_INCOMPATIBILITIES:
645 ShowConflicts(browser_);
646 break;
647 case IDC_HELP_PAGE_VIA_KEYBOARD:
648 ShowHelp(browser_, HELP_SOURCE_KEYBOARD);
649 break;
650 case IDC_HELP_PAGE_VIA_MENU:
651 ShowHelp(browser_, HELP_SOURCE_MENU);
652 break;
653 case IDC_SHOW_SYNC_SETUP:
654 ShowSyncSetup(browser_, SyncPromoUI::SOURCE_MENU);
655 break;
656 case IDC_TOGGLE_SPEECH_INPUT:
657 ToggleSpeechInput(browser_);
658 break;
659
660 default:
661 LOG(WARNING) << "Received Unimplemented Command: " << id;
662 break;
663 }
664}
665
666////////////////////////////////////////////////////////////////////////////////
667// BrowserCommandController, content::NotificationObserver implementation:
668
669void BrowserCommandController::Observe(
670 int type,
671 const content::NotificationSource& source,
672 const content::NotificationDetails& details) {
673 switch (type) {
[email protected]5d98294912012-06-27 22:57:40674 case content::NOTIFICATION_INTERSTITIAL_ATTACHED:
675 UpdateCommandsForTabState();
676 break;
677
678 case content::NOTIFICATION_INTERSTITIAL_DETACHED:
679 UpdateCommandsForTabState();
680 break;
681
682 default:
683 NOTREACHED() << "Got a notification we didn't register for.";
684 }
685}
686
687////////////////////////////////////////////////////////////////////////////////
[email protected]a6a7ced2012-11-01 17:24:18688// PrefObserver implementation:
689
690void BrowserCommandController::OnPreferenceChanged(
691 PrefServiceBase* service,
692 const std::string& pref_name) {
693 if (pref_name == prefs::kPrintingEnabled) {
694 UpdatePrintingState();
695 } else if (pref_name == prefs::kIncognitoModeAvailability) {
696 UpdateCommandsForIncognitoAvailability();
697 } else if (pref_name == prefs::kDevToolsDisabled) {
698 UpdateCommandsForDevTools();
699 } else if (pref_name == prefs::kEditBookmarksEnabled) {
700 UpdateCommandsForBookmarkEditing();
701 } else if (pref_name == prefs::kShowBookmarkBar) {
702 UpdateCommandsForBookmarkBar();
703 } else if (pref_name == prefs::kAllowFileSelectionDialogs) {
704 UpdateSaveAsState();
705 UpdateOpenFileState();
706 } else if (pref_name == prefs::kInManagedMode) {
707 UpdateCommandsForMultipleProfiles();
708 } else {
709 NOTREACHED();
710 }
711}
712
713////////////////////////////////////////////////////////////////////////////////
[email protected]5d98294912012-06-27 22:57:40714// BrowserCommandController, TabStripModelObserver implementation:
715
716void BrowserCommandController::TabInsertedAt(TabContents* contents,
717 int index,
718 bool foreground) {
719 AddInterstitialObservers(contents);
720}
721
722void BrowserCommandController::TabDetachedAt(TabContents* contents, int index) {
723 RemoveInterstitialObservers(contents);
724}
725
726void BrowserCommandController::TabReplacedAt(TabStripModel* tab_strip_model,
727 TabContents* old_contents,
728 TabContents* new_contents,
729 int index) {
730 RemoveInterstitialObservers(old_contents);
731 AddInterstitialObservers(new_contents);
732}
733
734////////////////////////////////////////////////////////////////////////////////
735// BrowserCommandController, TabRestoreServiceObserver implementation:
736
737void BrowserCommandController::TabRestoreServiceChanged(
738 TabRestoreService* service) {
739 command_updater_.UpdateCommandEnabled(IDC_RESTORE_TAB,
740 CanRestoreTab(browser_));
741}
742
743void BrowserCommandController::TabRestoreServiceDestroyed(
744 TabRestoreService* service) {
745 service->RemoveObserver(this);
746}
747
748////////////////////////////////////////////////////////////////////////////////
749// BrowserCommandController, ProfileSyncServiceObserver implementation:
750
751void BrowserCommandController::OnStateChanged() {
752 DCHECK(ProfileSyncServiceFactory::GetInstance()->HasProfileSyncService(
753 profile()));
754 // For unit tests, we don't have a window.
755 if (!window())
756 return;
757 const bool show_main_ui = IsShowingMainUI(window()->IsFullscreen());
758 command_updater_.UpdateCommandEnabled(IDC_SHOW_SYNC_SETUP,
759 show_main_ui && profile()->GetOriginalProfile()->IsSyncAccessible());
760}
761
762////////////////////////////////////////////////////////////////////////////////
763// BrowserCommandController, private:
764
765bool BrowserCommandController::IsShowingMainUI(bool is_fullscreen) {
766#if !defined(OS_MACOSX)
767 return browser_->is_type_tabbed() && !is_fullscreen;
768#else
769 return browser_->is_type_tabbed();
770#endif
771}
772
773void BrowserCommandController::InitCommandState() {
774 // All browser commands whose state isn't set automagically some other way
775 // (like Back & Forward with initial page load) must have their state
776 // initialized here, otherwise they will be forever disabled.
777
778 // Navigation commands
779 command_updater_.UpdateCommandEnabled(IDC_RELOAD, true);
780 command_updater_.UpdateCommandEnabled(IDC_RELOAD_IGNORING_CACHE, true);
[email protected]58e29032012-08-06 20:19:57781 command_updater_.UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE, true);
[email protected]5d98294912012-06-27 22:57:40782
783 // Window management commands
784 command_updater_.UpdateCommandEnabled(IDC_CLOSE_WINDOW, true);
785 command_updater_.UpdateCommandEnabled(IDC_NEW_TAB, true);
786 command_updater_.UpdateCommandEnabled(IDC_CLOSE_TAB, true);
787 command_updater_.UpdateCommandEnabled(IDC_DUPLICATE_TAB, true);
788 command_updater_.UpdateCommandEnabled(IDC_RESTORE_TAB, false);
789 command_updater_.UpdateCommandEnabled(IDC_EXIT, true);
790 command_updater_.UpdateCommandEnabled(IDC_DEBUG_FRAME_TOGGLE, true);
[email protected]4b0bcef2012-09-27 00:20:56791#if defined(USE_ASH)
[email protected]0f9ccc82012-10-23 22:02:53792 if (chrome::HOST_DESKTOP_TYPE_NATIVE != chrome::HOST_DESKTOP_TYPE_ASH)
793 command_updater_.UpdateCommandEnabled(IDC_TOGGLE_ASH_DESKTOP, true);
[email protected]4b0bcef2012-09-27 00:20:56794#endif
[email protected]5d98294912012-06-27 22:57:40795
796 // Page-related commands
797 command_updater_.UpdateCommandEnabled(IDC_EMAIL_PAGE_LOCATION, true);
798 command_updater_.UpdateCommandEnabled(IDC_ENCODING_AUTO_DETECT, true);
799 command_updater_.UpdateCommandEnabled(IDC_ENCODING_UTF8, true);
800 command_updater_.UpdateCommandEnabled(IDC_ENCODING_UTF16LE, true);
801 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88591, true);
802 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1252, true);
803 command_updater_.UpdateCommandEnabled(IDC_ENCODING_GBK, true);
804 command_updater_.UpdateCommandEnabled(IDC_ENCODING_GB18030, true);
805 command_updater_.UpdateCommandEnabled(IDC_ENCODING_BIG5HKSCS, true);
806 command_updater_.UpdateCommandEnabled(IDC_ENCODING_BIG5, true);
807 command_updater_.UpdateCommandEnabled(IDC_ENCODING_THAI, true);
808 command_updater_.UpdateCommandEnabled(IDC_ENCODING_KOREAN, true);
809 command_updater_.UpdateCommandEnabled(IDC_ENCODING_SHIFTJIS, true);
810 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO2022JP, true);
811 command_updater_.UpdateCommandEnabled(IDC_ENCODING_EUCJP, true);
812 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO885915, true);
813 command_updater_.UpdateCommandEnabled(IDC_ENCODING_MACINTOSH, true);
814 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88592, true);
815 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1250, true);
816 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88595, true);
817 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1251, true);
818 command_updater_.UpdateCommandEnabled(IDC_ENCODING_KOI8R, true);
819 command_updater_.UpdateCommandEnabled(IDC_ENCODING_KOI8U, true);
820 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88597, true);
821 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1253, true);
822 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88594, true);
823 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO885913, true);
824 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1257, true);
825 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88593, true);
826 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO885910, true);
827 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO885914, true);
828 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO885916, true);
829 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1254, true);
830 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88596, true);
831 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1256, true);
832 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88598, true);
833 command_updater_.UpdateCommandEnabled(IDC_ENCODING_ISO88598I, true);
834 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1255, true);
835 command_updater_.UpdateCommandEnabled(IDC_ENCODING_WINDOWS1258, true);
[email protected]5d98294912012-06-27 22:57:40836
837 // Zoom
838 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MENU, true);
839 command_updater_.UpdateCommandEnabled(IDC_ZOOM_PLUS, true);
840 command_updater_.UpdateCommandEnabled(IDC_ZOOM_NORMAL, true);
841 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MINUS, true);
842
843 // Show various bits of UI
844 UpdateOpenFileState();
845 command_updater_.UpdateCommandEnabled(IDC_CREATE_SHORTCUTS, false);
846 UpdateCommandsForDevTools();
847 command_updater_.UpdateCommandEnabled(IDC_TASK_MANAGER, CanOpenTaskManager());
[email protected]0b083cd2012-09-26 16:21:23848 command_updater_.UpdateCommandEnabled(IDC_SHOW_HISTORY,
849 !Profile::IsGuestSession());
[email protected]5d98294912012-06-27 22:57:40850 command_updater_.UpdateCommandEnabled(IDC_SHOW_DOWNLOADS, true);
851 command_updater_.UpdateCommandEnabled(IDC_HELP_PAGE_VIA_KEYBOARD, true);
852 command_updater_.UpdateCommandEnabled(IDC_HELP_PAGE_VIA_MENU, true);
[email protected]fae8b9e2012-10-12 21:40:10853 command_updater_.UpdateCommandEnabled(IDC_BOOKMARKS_MENU,
854 !Profile::IsGuestSession());
[email protected]5d98294912012-06-27 22:57:40855
856 command_updater_.UpdateCommandEnabled(
857 IDC_SHOW_SYNC_SETUP, profile()->GetOriginalProfile()->IsSyncAccessible());
858
859 // Initialize other commands based on the window type.
860 bool normal_window = browser_->is_type_tabbed();
861
862 // Navigation commands
863 command_updater_.UpdateCommandEnabled(IDC_HOME, normal_window);
864
865 // Window management commands
[email protected]5d98294912012-06-27 22:57:40866 command_updater_.UpdateCommandEnabled(IDC_SELECT_NEXT_TAB, normal_window);
867 command_updater_.UpdateCommandEnabled(IDC_SELECT_PREVIOUS_TAB,
868 normal_window);
869 command_updater_.UpdateCommandEnabled(IDC_MOVE_TAB_NEXT, normal_window);
870 command_updater_.UpdateCommandEnabled(IDC_MOVE_TAB_PREVIOUS, normal_window);
871 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_0, normal_window);
872 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_1, normal_window);
873 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_2, normal_window);
874 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_3, normal_window);
875 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_4, normal_window);
876 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_5, normal_window);
877 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_6, normal_window);
878 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_7, normal_window);
879 command_updater_.UpdateCommandEnabled(IDC_SELECT_LAST_TAB, normal_window);
880#if defined(OS_WIN)
881 const bool metro_mode = base::win::IsMetroProcess();
882 command_updater_.UpdateCommandEnabled(IDC_METRO_SNAP_ENABLE, metro_mode);
883 command_updater_.UpdateCommandEnabled(IDC_METRO_SNAP_DISABLE, metro_mode);
[email protected]770c6d82012-09-06 22:21:32884 int restart_mode = metro_mode ?
885 IDC_WIN8_DESKTOP_RESTART : IDC_WIN8_METRO_RESTART;
886 command_updater_.UpdateCommandEnabled(restart_mode, normal_window);
[email protected]5d98294912012-06-27 22:57:40887#endif
[email protected]5d98294912012-06-27 22:57:40888 command_updater_.UpdateCommandEnabled(IDC_TABPOSE, normal_window);
[email protected]5d98294912012-06-27 22:57:40889
[email protected]5d98294912012-06-27 22:57:40890 // Find-in-page
891 command_updater_.UpdateCommandEnabled(IDC_FIND, !browser_->is_devtools());
892 command_updater_.UpdateCommandEnabled(IDC_FIND_NEXT,
893 !browser_->is_devtools());
894 command_updater_.UpdateCommandEnabled(IDC_FIND_PREVIOUS,
895 !browser_->is_devtools());
896
897 // Show various bits of UI
898 command_updater_.UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA, normal_window);
899
900 // The upgrade entry and the view incompatibility entry should always be
901 // enabled. Whether they are visible is a separate matter determined on menu
902 // show.
903 command_updater_.UpdateCommandEnabled(IDC_UPGRADE_DIALOG, true);
904 command_updater_.UpdateCommandEnabled(IDC_VIEW_INCOMPATIBILITIES, true);
905
906 // View Background Pages entry is always enabled, but is hidden if there are
907 // no background pages.
908 command_updater_.UpdateCommandEnabled(IDC_VIEW_BACKGROUND_PAGES, true);
909
910 // Toggle speech input
911 command_updater_.UpdateCommandEnabled(IDC_TOGGLE_SPEECH_INPUT, true);
912
913 // Initialize other commands whose state changes based on fullscreen mode.
914 UpdateCommandsForFullscreenMode(FULLSCREEN_DISABLED);
915
916 UpdateCommandsForContentRestrictionState();
917
918 UpdateCommandsForBookmarkEditing();
919
920 UpdateCommandsForIncognitoAvailability();
921}
922
923void BrowserCommandController::UpdateCommandsForIncognitoAvailability() {
924 IncognitoModePrefs::Availability incognito_availability =
925 IncognitoModePrefs::GetAvailability(profile()->GetPrefs());
926 command_updater_.UpdateCommandEnabled(
927 IDC_NEW_WINDOW,
928 incognito_availability != IncognitoModePrefs::FORCED);
929 command_updater_.UpdateCommandEnabled(
930 IDC_NEW_INCOGNITO_WINDOW,
931 incognito_availability != IncognitoModePrefs::DISABLED);
932
933 // Bookmark manager and settings page/subpages are forced to open in normal
934 // mode. For this reason we disable these commands when incognito is forced.
935 const bool command_enabled =
936 incognito_availability != IncognitoModePrefs::FORCED;
937 command_updater_.UpdateCommandEnabled(
938 IDC_SHOW_BOOKMARK_MANAGER,
939 browser_defaults::bookmarks_enabled && command_enabled);
940 ExtensionService* extension_service = profile()->GetExtensionService();
941 bool enable_extensions =
942 extension_service && extension_service->extensions_enabled();
943 command_updater_.UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS,
944 enable_extensions && command_enabled);
945
946 const bool show_main_ui =
947 IsShowingMainUI(window() && window()->IsFullscreen());
948 command_updater_.UpdateCommandEnabled(IDC_IMPORT_SETTINGS,
949 show_main_ui && command_enabled);
950 command_updater_.UpdateCommandEnabled(IDC_OPTIONS,
951 show_main_ui && command_enabled);
952}
953
954void BrowserCommandController::UpdateCommandsForTabState() {
[email protected]1c5119c2012-09-19 00:08:57955 WebContents* current_web_contents = chrome::GetActiveWebContents(browser_);
956 if (!current_web_contents) // May be NULL during tab restore.
[email protected]5d98294912012-06-27 22:57:40957 return;
[email protected]5d98294912012-06-27 22:57:40958
959 // Navigation commands
960 command_updater_.UpdateCommandEnabled(IDC_BACK, CanGoBack(browser_));
961 command_updater_.UpdateCommandEnabled(IDC_FORWARD, CanGoForward(browser_));
962 command_updater_.UpdateCommandEnabled(IDC_RELOAD, CanReload(browser_));
963 command_updater_.UpdateCommandEnabled(IDC_RELOAD_IGNORING_CACHE,
964 CanReload(browser_));
[email protected]58e29032012-08-06 20:19:57965 command_updater_.UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE,
966 CanReload(browser_));
[email protected]5d98294912012-06-27 22:57:40967
968 // Window management commands
969 command_updater_.UpdateCommandEnabled(IDC_DUPLICATE_TAB,
970 !browser_->is_app() && CanDuplicateTab(browser_));
971
972 // Page-related commands
973 window()->SetStarredState(
[email protected]1c5119c2012-09-19 00:08:57974 BookmarkTabHelper::FromWebContents(current_web_contents)->is_starred());
[email protected]5423c372012-08-22 05:50:16975 window()->ZoomChangedForActiveTab(false);
[email protected]5d98294912012-06-27 22:57:40976 command_updater_.UpdateCommandEnabled(IDC_VIEW_SOURCE,
977 CanViewSource(browser_));
978 command_updater_.UpdateCommandEnabled(IDC_EMAIL_PAGE_LOCATION,
979 CanEmailPageLocation(browser_));
980 if (browser_->is_devtools())
981 command_updater_.UpdateCommandEnabled(IDC_OPEN_FILE, false);
982
983 // Changing the encoding is not possible on Chrome-internal webpages.
984 NavigationController& nc = current_web_contents->GetController();
985 bool is_chrome_internal = HasInternalURL(nc.GetActiveEntry()) ||
986 current_web_contents->ShowingInterstitialPage();
987 command_updater_.UpdateCommandEnabled(IDC_ENCODING_MENU,
988 !is_chrome_internal && current_web_contents->IsSavable());
989
990 // Show various bits of UI
991 // TODO(pinkerton): Disable app-mode in the model until we implement it
992 // on the Mac. Be sure to remove both ifdefs. http://crbug.com/13148
993#if !defined(OS_MACOSX)
994 command_updater_.UpdateCommandEnabled(
995 IDC_CREATE_SHORTCUTS,
996 CanCreateApplicationShortcuts(browser_));
997#endif
998
[email protected]9b7ab882012-09-10 23:46:36999 command_updater_.UpdateCommandEnabled(
1000 IDC_TOGGLE_REQUEST_TABLET_SITE,
1001 CanRequestTabletSite(current_web_contents));
1002
[email protected]5d98294912012-06-27 22:57:401003 UpdateCommandsForContentRestrictionState();
1004 UpdateCommandsForBookmarkEditing();
1005}
1006
1007void BrowserCommandController::UpdateCommandsForContentRestrictionState() {
1008 int restrictions = GetContentRestrictions(browser_);
1009
1010 command_updater_.UpdateCommandEnabled(
1011 IDC_COPY, !(restrictions & content::CONTENT_RESTRICTION_COPY));
1012 command_updater_.UpdateCommandEnabled(
1013 IDC_CUT, !(restrictions & content::CONTENT_RESTRICTION_CUT));
1014 command_updater_.UpdateCommandEnabled(
1015 IDC_PASTE, !(restrictions & content::CONTENT_RESTRICTION_PASTE));
1016 UpdateSaveAsState();
1017 UpdatePrintingState();
1018}
1019
1020void BrowserCommandController::UpdateCommandsForDevTools() {
1021 bool dev_tools_enabled =
1022 !profile()->GetPrefs()->GetBoolean(prefs::kDevToolsDisabled);
1023 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS,
1024 dev_tools_enabled);
1025 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_CONSOLE,
1026 dev_tools_enabled);
1027 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_INSPECT,
1028 dev_tools_enabled);
[email protected]d16657c2012-09-03 14:25:101029 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_TOGGLE,
1030 dev_tools_enabled);
[email protected]5d98294912012-06-27 22:57:401031}
1032
1033void BrowserCommandController::UpdateCommandsForBookmarkEditing() {
1034 command_updater_.UpdateCommandEnabled(IDC_BOOKMARK_PAGE,
1035 CanBookmarkCurrentPage(browser_));
1036 command_updater_.UpdateCommandEnabled(IDC_BOOKMARK_ALL_TABS,
1037 CanBookmarkAllTabs(browser_));
[email protected]e6ba5a12012-08-07 02:05:531038 command_updater_.UpdateCommandEnabled(IDC_PIN_TO_START_SCREEN,
[email protected]5d98294912012-06-27 22:57:401039 true);
1040}
1041
1042void BrowserCommandController::UpdateCommandsForBookmarkBar() {
1043 const bool show_main_ui =
1044 IsShowingMainUI(window() && window()->IsFullscreen());
1045 command_updater_.UpdateCommandEnabled(IDC_SHOW_BOOKMARK_BAR,
1046 browser_defaults::bookmarks_enabled &&
1047 !profile()->GetPrefs()->IsManagedPreference(prefs::kShowBookmarkBar) &&
1048 show_main_ui);
1049}
1050
1051void BrowserCommandController::UpdateCommandsForFullscreenMode(
1052 FullScreenMode fullscreen_mode) {
1053 const bool show_main_ui =
1054 IsShowingMainUI(fullscreen_mode != FULLSCREEN_DISABLED);
1055 bool main_not_fullscreen = show_main_ui &&
1056 (fullscreen_mode == FULLSCREEN_DISABLED);
1057
1058 // Navigation commands
1059 command_updater_.UpdateCommandEnabled(IDC_OPEN_CURRENT_URL, show_main_ui);
1060
1061 // Window management commands
1062 command_updater_.UpdateCommandEnabled(
1063 IDC_SHOW_AS_TAB,
1064 !browser_->is_type_tabbed() && fullscreen_mode == FULLSCREEN_DISABLED);
1065
1066 // Focus various bits of UI
1067 command_updater_.UpdateCommandEnabled(IDC_FOCUS_TOOLBAR, show_main_ui);
1068 command_updater_.UpdateCommandEnabled(IDC_FOCUS_LOCATION, show_main_ui);
1069 command_updater_.UpdateCommandEnabled(IDC_FOCUS_SEARCH, show_main_ui);
1070 command_updater_.UpdateCommandEnabled(
1071 IDC_FOCUS_MENU_BAR, main_not_fullscreen);
1072 command_updater_.UpdateCommandEnabled(
1073 IDC_FOCUS_NEXT_PANE, main_not_fullscreen);
1074 command_updater_.UpdateCommandEnabled(
1075 IDC_FOCUS_PREVIOUS_PANE, main_not_fullscreen);
1076 command_updater_.UpdateCommandEnabled(
1077 IDC_FOCUS_BOOKMARKS, main_not_fullscreen);
1078
1079 // Show various bits of UI
1080 command_updater_.UpdateCommandEnabled(IDC_DEVELOPER_MENU, show_main_ui);
1081 command_updater_.UpdateCommandEnabled(IDC_FEEDBACK, show_main_ui);
1082 command_updater_.UpdateCommandEnabled(IDC_SHOW_SYNC_SETUP,
1083 show_main_ui && profile()->GetOriginalProfile()->IsSyncAccessible());
1084
1085 // Settings page/subpages are forced to open in normal mode. We disable these
1086 // commands when incognito is forced.
1087 const bool options_enabled = show_main_ui &&
1088 IncognitoModePrefs::GetAvailability(
1089 profile()->GetPrefs()) != IncognitoModePrefs::FORCED;
1090 command_updater_.UpdateCommandEnabled(IDC_OPTIONS, options_enabled);
1091 command_updater_.UpdateCommandEnabled(IDC_IMPORT_SETTINGS, options_enabled);
1092
1093 command_updater_.UpdateCommandEnabled(IDC_EDIT_SEARCH_ENGINES, show_main_ui);
1094 command_updater_.UpdateCommandEnabled(IDC_VIEW_PASSWORDS, show_main_ui);
1095 command_updater_.UpdateCommandEnabled(IDC_ABOUT, show_main_ui);
1096 command_updater_.UpdateCommandEnabled(IDC_SHOW_APP_MENU, show_main_ui);
1097#if defined (ENABLE_PROFILING) && !defined(NO_TCMALLOC)
1098 command_updater_.UpdateCommandEnabled(IDC_PROFILING_ENABLED, show_main_ui);
1099#endif
1100
1101 // Disable explicit fullscreen toggling when in metro snap mode.
[email protected]00a1cc5b2012-11-07 13:44:511102 bool fullscreen_enabled = !browser_->is_type_panel() &&
1103 !browser_->is_app() &&
1104 fullscreen_mode != FULLSCREEN_METRO_SNAP;
1105#if defined(OS_MACOSX)
1106 // The Mac implementation doesn't support switching to fullscreen while
1107 // a tab modal dialog is displayed.
1108 int tabIndex = chrome::IndexOfFirstBlockedTab(browser_->tab_strip_model());
1109 bool has_blocked_tab = tabIndex != browser_->tab_strip_model()->count();
1110 fullscreen_enabled &= !has_blocked_tab;
1111#endif
1112
1113 command_updater_.UpdateCommandEnabled(IDC_FULLSCREEN, fullscreen_enabled);
1114 command_updater_.UpdateCommandEnabled(IDC_PRESENTATION_MODE,
1115 fullscreen_enabled);
[email protected]5d98294912012-06-27 22:57:401116
1117 UpdateCommandsForBookmarkBar();
1118 UpdateCommandsForMultipleProfiles();
1119}
1120
1121void BrowserCommandController::UpdateCommandsForMultipleProfiles() {
1122 bool show_main_ui = IsShowingMainUI(window() && window()->IsFullscreen());
1123 command_updater_.UpdateCommandEnabled(IDC_SHOW_AVATAR_MENU,
1124 show_main_ui &&
1125 !profile()->IsOffTheRecord() &&
1126 ProfileManager::IsMultipleProfilesEnabled());
1127}
1128
1129void BrowserCommandController::UpdatePrintingState() {
[email protected]d53e4032012-06-29 18:58:341130 bool print_enabled = CanPrint(browser_);
1131 command_updater_.UpdateCommandEnabled(IDC_PRINT, print_enabled);
[email protected]5d98294912012-06-27 22:57:401132 command_updater_.UpdateCommandEnabled(IDC_ADVANCED_PRINT,
1133 CanAdvancedPrint(browser_));
[email protected]d53e4032012-06-29 18:58:341134 command_updater_.UpdateCommandEnabled(IDC_PRINT_TO_DESTINATION,
1135 print_enabled);
1136#if defined(OS_WIN)
1137 HMODULE metro_module = base::win::GetMetroModule();
1138 if (metro_module != NULL) {
1139 typedef void (*MetroEnablePrinting)(BOOL);
1140 MetroEnablePrinting metro_enable_printing =
1141 reinterpret_cast<MetroEnablePrinting>(
1142 ::GetProcAddress(metro_module, "MetroEnablePrinting"));
1143 if (metro_enable_printing)
1144 metro_enable_printing(print_enabled);
1145 }
1146#endif
[email protected]5d98294912012-06-27 22:57:401147}
1148
1149void BrowserCommandController::UpdateSaveAsState() {
1150 command_updater_.UpdateCommandEnabled(IDC_SAVE_PAGE, CanSavePage(browser_));
1151}
1152
1153void BrowserCommandController::UpdateOpenFileState() {
1154 bool enabled = true;
1155 PrefService* local_state = g_browser_process->local_state();
1156 if (local_state)
1157 enabled = local_state->GetBoolean(prefs::kAllowFileSelectionDialogs);
1158
1159 command_updater_.UpdateCommandEnabled(IDC_OPEN_FILE, enabled);
1160}
1161
1162void BrowserCommandController::UpdateReloadStopState(bool is_loading,
1163 bool force) {
1164 window()->UpdateReloadStopState(is_loading, force);
1165 command_updater_.UpdateCommandEnabled(IDC_STOP, is_loading);
1166}
1167
1168void BrowserCommandController::AddInterstitialObservers(TabContents* contents) {
1169 registrar_.Add(this, content::NOTIFICATION_INTERSTITIAL_ATTACHED,
1170 content::Source<WebContents>(contents->web_contents()));
1171 registrar_.Add(this, content::NOTIFICATION_INTERSTITIAL_DETACHED,
1172 content::Source<WebContents>(contents->web_contents()));
1173}
1174
1175void BrowserCommandController::RemoveInterstitialObservers(
1176 TabContents* contents) {
1177 registrar_.Remove(this, content::NOTIFICATION_INTERSTITIAL_ATTACHED,
1178 content::Source<WebContents>(contents->web_contents()));
1179 registrar_.Remove(this, content::NOTIFICATION_INTERSTITIAL_DETACHED,
1180 content::Source<WebContents>(contents->web_contents()));
1181}
1182
1183BrowserWindow* BrowserCommandController::window() {
1184 return browser_->window();
1185}
1186
1187Profile* BrowserCommandController::profile() {
1188 return browser_->profile();
1189}
1190
[email protected]5d98294912012-06-27 22:57:401191} // namespace chrome