blob: 066739ae965e0972216e31fa4653e2b2a8fffeb8 [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
avi655876a2015-12-25 07:18:157#include <stddef.h>
8
pmonette9c1457f2015-11-19 20:29:319#include <string>
10
Sebastien Marchandf1349f52019-01-25 03:16:4111#include "base/bind.h"
[email protected]488e3952013-11-18 05:29:1412#include "base/command_line.h"
Scott Violet44165792018-02-22 02:08:0813#include "base/debug/debugging_buildflags.h"
primianod3a81ab2016-01-25 22:21:1514#include "base/debug/profiler.h"
avi655876a2015-12-25 07:18:1515#include "base/macros.h"
bratell0a7406f2017-03-28 07:46:3716#include "base/metrics/user_metrics.h"
Haeun Kim3f6123502018-08-26 18:03:0417#include "base/stl_util.h"
Nico Weber0cc71122019-07-29 17:30:4018#include "build/branding_buildflags.h"
avi655876a2015-12-25 07:18:1519#include "build/build_config.h"
[email protected]5d98294912012-06-27 22:57:4020#include "chrome/app/chrome_command_ids.h"
21#include "chrome/browser/browser_process.h"
[email protected]dcc8fbc2013-07-12 00:54:0922#include "chrome/browser/chrome_notification_types.h"
[email protected]5d98294912012-06-27 22:57:4023#include "chrome/browser/defaults.h"
Pavol Marko56df0272018-07-04 17:55:0424#include "chrome/browser/devtools/devtools_window.h"
[email protected]5d98294912012-06-27 22:57:4025#include "chrome/browser/extensions/extension_service.h"
[email protected]2e9d79f2013-08-16 05:45:5626#include "chrome/browser/lifetime/application_lifetime.h"
[email protected]5d98294912012-06-27 22:57:4027#include "chrome/browser/prefs/incognito_mode_prefs.h"
[email protected]5d98294912012-06-27 22:57:4028#include "chrome/browser/profiles/profile.h"
mlermane01e6de2014-09-29 19:26:4729#include "chrome/browser/profiles/profile_manager.h"
[email protected]5d98294912012-06-27 22:57:4030#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]0b32f9b62012-09-17 19:08:0331#include "chrome/browser/shell_integration.h"
[email protected]3d27d272013-07-31 03:15:1632#include "chrome/browser/signin/signin_promo.h"
[email protected]5d98294912012-06-27 22:57:4033#include "chrome/browser/sync/profile_sync_service_factory.h"
Christopher Lam86b52712017-12-04 01:58:3334#include "chrome/browser/ui/apps/app_info_dialog.h"
[email protected]5d98294912012-06-27 22:57:4035#include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
36#include "chrome/browser/ui/browser.h"
37#include "chrome/browser/ui/browser_commands.h"
Dana Fried08b774a2019-05-10 18:21:4838#include "chrome/browser/ui/browser_finder.h"
[email protected]5d98294912012-06-27 22:57:4039#include "chrome/browser/ui/browser_window.h"
40#include "chrome/browser/ui/chrome_pages.h"
Alan Cutter0c3132302018-02-21 05:09:0241#include "chrome/browser/ui/page_info/page_info_dialog.h"
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:2742#include "chrome/browser/ui/singleton_tabs.h"
[email protected]5d98294912012-06-27 22:57:4043#include "chrome/browser/ui/tabs/tab_strip_model.h"
Eric Willigers4a5f7a92019-05-10 19:19:2644#include "chrome/browser/ui/web_applications/app_browser_controller.h"
Eric Willigerse2ca235a2019-08-17 01:01:0645#include "chrome/browser/ui/web_applications/web_app_launch_utils.h"
[email protected]2056c3192013-10-21 22:40:5146#include "chrome/browser/ui/webui/inspect_ui.h"
[email protected]3c71576ce2013-07-23 02:00:0147#include "chrome/common/content_restriction.h"
[email protected]5d98294912012-06-27 22:57:4048#include "chrome/common/pref_names.h"
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:2749#include "chrome/common/url_constants.h"
brettwe1f0af8b2015-10-09 21:30:4650#include "components/bookmarks/common/bookmark_pref_names.h"
Aran Gilman7b6ccf5d2019-09-05 19:16:0251#include "components/dom_distiller/core/dom_distiller_features.h"
Scott Violet9ae82892018-03-01 18:38:1252#include "components/feature_engagement/buildflags.h"
brettwb1fc1b82016-02-02 00:19:0853#include "components/prefs/pref_service.h"
blundella08c5dd2015-09-18 06:14:1654#include "components/sessions/core/tab_restore_service.h"
Colin Blundell3517170e2019-07-11 08:16:3455#include "components/signin/public/base/signin_pref_names.h"
[email protected]5d98294912012-06-27 22:57:4056#include "content/public/browser/native_web_keyboard_event.h"
57#include "content/public/browser/navigation_controller.h"
58#include "content/public/browser/navigation_entry.h"
Lukasz Anforowicze1b954d92017-10-30 21:28:0659#include "content/public/browser/render_frame_host.h"
[email protected]5d98294912012-06-27 22:57:4060#include "content/public/browser/web_contents.h"
[email protected]20ca0382013-02-28 19:50:0761#include "content/public/browser/web_contents_observer.h"
Bryce Thomas96b07772018-11-07 03:04:1562#include "content/public/common/profiling.h"
bend32292b2016-10-07 00:21:5863#include "content/public/common/service_manager_connection.h"
[email protected]5d98294912012-06-27 22:57:4064#include "content/public/common/url_constants.h"
[email protected]03d25812014-06-22 19:41:5565#include "extensions/browser/extension_system.h"
Scott Violet318a55f2018-03-30 19:08:1966#include "printing/buildflags/buildflags.h"
[email protected]7e9acd082013-09-17 23:31:1667#include "ui/events/keycodes/keyboard_codes.h"
[email protected]5d98294912012-06-27 22:57:4068
[email protected]9c4d68332013-01-30 13:34:4169#if defined(OS_MACOSX)
70#include "chrome/browser/ui/browser_commands_mac.h"
71#endif
72
[email protected]5d98294912012-06-27 22:57:4073#if defined(OS_WIN)
[email protected]a43ed002013-02-05 19:47:5474#include "base/win/windows_version.h"
[email protected]2d0f80f2013-11-01 22:47:1875#include "content/public/browser/gpu_data_manager.h"
[email protected]5d98294912012-06-27 22:57:4076#endif
77
[email protected]d12cc5e2013-10-19 18:25:0678#if defined(OS_CHROMEOS)
Ivan Sandrke0e92452019-03-21 09:32:4879#include "chrome/browser/platform_util.h"
[email protected]0c930812014-01-30 18:01:4780#include "chrome/browser/ui/ash/multi_user/multi_user_context_menu.h"
caelyn4e4e08a2015-02-04 21:27:4981#include "chrome/browser/ui/browser_commands_chromeos.h"
[email protected]d12cc5e2013-10-19 18:25:0682#endif
83
[email protected]d5bedb6d2014-04-08 10:49:3284#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
karandeepbe6200332016-06-21 12:26:1085#include "ui/base/ime/linux/text_edit_key_bindings_delegate_auralinux.h"
[email protected]1e2172f2014-04-01 17:32:3486#endif
87
Collin Baker4ab8ad82019-06-06 17:02:5488#if BUILDFLAG(ENABLE_LEGACY_DESKTOP_IN_PRODUCT_HELP)
Bettina Dea48125dc2017-09-06 03:23:1489#include "chrome/browser/feature_engagement/bookmark/bookmark_tracker.h"
90#include "chrome/browser/feature_engagement/bookmark/bookmark_tracker_factory.h"
Catherine Chungbeb884222017-08-01 15:47:5591#include "chrome/browser/feature_engagement/new_tab/new_tab_tracker.h"
92#include "chrome/browser/feature_engagement/new_tab/new_tab_tracker_factory.h"
93#endif
94
Maksim Sisovac767592018-08-10 08:00:1595#if defined(USE_OZONE)
96#include "ui/ozone/public/ozone_platform.h"
97#endif
98
[email protected]5d98294912012-06-27 22:57:4099using content::NavigationController;
Aran Gilmand217b5b2019-04-11 17:45:43100using content::NavigationEntry;
[email protected]5d98294912012-06-27 22:57:40101using content::WebContents;
102
[email protected]5d98294912012-06-27 22:57:40103namespace chrome {
104
Dana Fried08b774a2019-05-10 18:21:48105namespace {
106
107// Ensures that - if we have not popped up an infobar to prompt the user to e.g.
108// reload the current page - that the content pane of the browser is refocused.
109void AppInfoDialogClosedCallback(content::WebContents* web_contents,
Dana Fried98c0380e2019-05-14 16:58:32110 views::Widget::ClosedReason closed_reason,
Dana Fried08b774a2019-05-10 18:21:48111 bool reload_prompt) {
112 if (reload_prompt)
113 return;
114
Dana Fried98c0380e2019-05-14 16:58:32115 // If the user clicked on something specific or focus was changed, don't
116 // override the focus.
117 if (closed_reason != views::Widget::ClosedReason::kEscKeyPressed &&
118 closed_reason != views::Widget::ClosedReason::kCloseButtonClicked) {
119 return;
120 }
121
Dana Fried08b774a2019-05-10 18:21:48122 // Ensure that the web contents handle we have is still valid. It's possible
123 // (though unlikely) that either the browser or web contents has been pulled
124 // out from underneath us.
125 Browser* const browser = chrome::FindBrowserWithWebContents(web_contents);
126 if (!browser)
127 return;
128
129 // We want to focus the active web contents, which again, might not be the
130 // original web contents (though it should be the vast majority of the time).
131 content::WebContents* const active_contents =
132 browser->tab_strip_model()->GetActiveWebContents();
133 if (active_contents)
134 active_contents->Focus();
135}
136
137} // namespace
138
[email protected]5d98294912012-06-27 22:57:40139///////////////////////////////////////////////////////////////////////////////
140// BrowserCommandController, public:
141
[email protected]338416c02014-05-13 16:47:06142BrowserCommandController::BrowserCommandController(Browser* browser)
Aran Gilmand217b5b2019-04-11 17:45:43143 : browser_(browser), command_updater_(nullptr) {
[email protected]5d98294912012-06-27 22:57:40144 browser_->tab_strip_model()->AddObserver(this);
145 PrefService* local_state = g_browser_process->local_state();
146 if (local_state) {
147 local_pref_registrar_.Init(local_state);
[email protected]9ec3ea5b2012-12-03 18:14:30148 local_pref_registrar_.Add(
149 prefs::kAllowFileSelectionDialogs,
150 base::Bind(
151 &BrowserCommandController::UpdateCommandsForFileSelectionDialogs,
152 base::Unretained(this)));
[email protected]5d98294912012-06-27 22:57:40153 }
154
155 profile_pref_registrar_.Init(profile()->GetPrefs());
[email protected]9ec3ea5b2012-12-03 18:14:30156 profile_pref_registrar_.Add(
Pavol Markodebb0ff2018-05-07 18:35:41157 prefs::kDevToolsAvailability,
[email protected]9ec3ea5b2012-12-03 18:14:30158 base::Bind(&BrowserCommandController::UpdateCommandsForDevTools,
159 base::Unretained(this)));
160 profile_pref_registrar_.Add(
tfarina3bddbe112014-08-28 05:29:32161 bookmarks::prefs::kEditBookmarksEnabled,
[email protected]9ec3ea5b2012-12-03 18:14:30162 base::Bind(&BrowserCommandController::UpdateCommandsForBookmarkEditing,
163 base::Unretained(this)));
164 profile_pref_registrar_.Add(
tfarina3bddbe112014-08-28 05:29:32165 bookmarks::prefs::kShowBookmarkBar,
[email protected]9ec3ea5b2012-12-03 18:14:30166 base::Bind(&BrowserCommandController::UpdateCommandsForBookmarkBar,
167 base::Unretained(this)));
168 profile_pref_registrar_.Add(
169 prefs::kIncognitoModeAvailability,
170 base::Bind(
171 &BrowserCommandController::UpdateCommandsForIncognitoAvailability,
172 base::Unretained(this)));
173 profile_pref_registrar_.Add(
174 prefs::kPrintingEnabled,
175 base::Bind(&BrowserCommandController::UpdatePrintingState,
176 base::Unretained(this)));
[email protected]32dfede2013-08-25 15:48:25177#if !defined(OS_MACOSX)
178 profile_pref_registrar_.Add(
179 prefs::kFullscreenAllowed,
180 base::Bind(&BrowserCommandController::UpdateCommandsForFullscreenMode,
181 base::Unretained(this)));
182#endif
Mihai Sardarescuf87ccc12018-09-26 10:37:45183 pref_signin_allowed_.Init(
Aran Gilmand217b5b2019-04-11 17:45:43184 prefs::kSigninAllowed, profile()->GetOriginalProfile()->GetPrefs(),
Mihai Sardarescuf87ccc12018-09-26 10:37:45185 base::Bind(&BrowserCommandController::OnSigninAllowedPrefChange,
186 base::Unretained(this)));
[email protected]5d98294912012-06-27 22:57:40187
188 InitCommandState();
189
blundell74001adc2015-09-18 11:04:25190 sessions::TabRestoreService* tab_restore_service =
[email protected]5d98294912012-06-27 22:57:40191 TabRestoreServiceFactory::GetForProfile(profile());
192 if (tab_restore_service) {
193 tab_restore_service->AddObserver(this);
Cliff Smolinsky2c9586e2019-02-08 18:48:30194 if (!tab_restore_service->IsLoaded())
195 tab_restore_service->LoadTabsFromLastSession();
[email protected]5d98294912012-06-27 22:57:40196 }
[email protected]5d98294912012-06-27 22:57:40197}
198
199BrowserCommandController::~BrowserCommandController() {
[email protected]95e39472012-10-05 23:37:36200 // TabRestoreService may have been shutdown by the time we get here. Don't
201 // trigger creating it.
blundell74001adc2015-09-18 11:04:25202 sessions::TabRestoreService* tab_restore_service =
[email protected]95e39472012-10-05 23:37:36203 TabRestoreServiceFactory::GetForProfileIfExisting(profile());
[email protected]5d98294912012-06-27 22:57:40204 if (tab_restore_service)
205 tab_restore_service->RemoveObserver(this);
206 profile_pref_registrar_.RemoveAll();
207 local_pref_registrar_.RemoveAll();
208 browser_->tab_strip_model()->RemoveObserver(this);
209}
210
211bool BrowserCommandController::IsReservedCommandOrKey(
212 int command_id,
213 const content::NativeWebKeyboardEvent& event) {
214 // In Apps mode, no keys are reserved.
Joel Hockey2687ab52019-08-14 23:59:46215 if (browser_->deprecated_is_app())
[email protected]5d98294912012-06-27 22:57:40216 return false;
217
218#if defined(OS_CHROMEOS)
[email protected]397abd32013-08-21 05:44:19219 // On Chrome OS, the top row of keys are mapped to browser actions like
220 // back/forward or refresh. We don't want web pages to be able to change the
221 // behavior of these keys. Ash handles F4 and up; this leaves us needing to
222 // reserve browser back/forward and refresh here.
[email protected]5d98294912012-06-27 22:57:40223 ui::KeyboardCode key_code =
Blink Reformat1c4d759e2017-04-09 16:34:54224 static_cast<ui::KeyboardCode>(event.windows_key_code);
[email protected]397abd32013-08-21 05:44:19225 if ((key_code == ui::VKEY_BROWSER_BACK && command_id == IDC_BACK) ||
226 (key_code == ui::VKEY_BROWSER_FORWARD && command_id == IDC_FORWARD) ||
227 (key_code == ui::VKEY_BROWSER_REFRESH && command_id == IDC_RELOAD)) {
[email protected]5d98294912012-06-27 22:57:40228 return true;
229 }
230#endif
231
zijiehe68cd3dc22017-04-07 18:50:29232 if (window()->IsFullscreen()) {
233 // In fullscreen, all commands except for IDC_FULLSCREEN and IDC_EXIT should
234 // be delivered to the web page. The intent to implement and ship can be
235 // found in http://crbug.com/680809.
236 const bool is_exit_fullscreen =
237 (command_id == IDC_EXIT || command_id == IDC_FULLSCREEN);
238#if defined(OS_MACOSX)
239 // This behavior is different on Mac OS, which has a unique user-initiated
240 // full-screen mode. According to the discussion in http://crbug.com/702251,
241 // the commands should be reserved for browser-side handling if the browser
242 // window's toolbar is visible.
243 if (window()->IsToolbarShowing()) {
244 if (command_id == IDC_FULLSCREEN)
245 return true;
246 } else {
247 return is_exit_fullscreen;
248 }
249#else
250 return is_exit_fullscreen;
251#endif
252 }
[email protected]1e2172f2014-04-01 17:32:34253
[email protected]893124a22014-04-15 00:45:28254#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
[email protected]1e2172f2014-04-01 17:32:34255 // If this key was registered by the user as a content editing hotkey, then
256 // it is not reserved.
[email protected]d5bedb6d2014-04-08 10:49:32257 ui::TextEditKeyBindingsDelegateAuraLinux* delegate =
[email protected]1e2172f2014-04-01 17:32:34258 ui::GetTextEditKeyBindingsDelegate();
[email protected]47e8e7542014-04-03 07:52:42259 if (delegate && event.os_event && delegate->MatchEvent(*event.os_event, NULL))
[email protected]1e2172f2014-04-01 17:32:34260 return false;
261#endif
262
Aran Gilmand217b5b2019-04-11 17:45:43263 return command_id == IDC_CLOSE_TAB || command_id == IDC_CLOSE_WINDOW ||
264 command_id == IDC_NEW_INCOGNITO_WINDOW || command_id == IDC_NEW_TAB ||
265 command_id == IDC_NEW_WINDOW || command_id == IDC_RESTORE_TAB ||
zijiehe13207802017-02-16 08:06:10266 command_id == IDC_SELECT_NEXT_TAB ||
Aran Gilmand217b5b2019-04-11 17:45:43267 command_id == IDC_SELECT_PREVIOUS_TAB || command_id == IDC_EXIT;
[email protected]5d98294912012-06-27 22:57:40268}
269
[email protected]5d98294912012-06-27 22:57:40270void BrowserCommandController::TabStateChanged() {
271 UpdateCommandsForTabState();
272}
273
[email protected]d93dbd12014-08-04 23:42:53274void BrowserCommandController::ZoomStateChanged() {
275 UpdateCommandsForZoomState();
276}
277
[email protected]5d98294912012-06-27 22:57:40278void BrowserCommandController::ContentRestrictionsChanged() {
279 UpdateCommandsForContentRestrictionState();
280}
281
282void BrowserCommandController::FullscreenStateChanged() {
[email protected]32dfede2013-08-25 15:48:25283 UpdateCommandsForFullscreenMode();
[email protected]5d98294912012-06-27 22:57:40284}
285
Ivan Sandrk9669d0e2017-12-15 23:50:20286#if defined(OS_CHROMEOS)
287void BrowserCommandController::LockedFullscreenStateChanged() {
288 UpdateCommandsForLockedFullscreenMode();
289}
290#endif
291
[email protected]5d98294912012-06-27 22:57:40292void BrowserCommandController::PrintingStateChanged() {
293 UpdatePrintingState();
294}
295
296void BrowserCommandController::LoadingStateChanged(bool is_loading,
297 bool force) {
298 UpdateReloadStopState(is_loading, force);
299}
300
Jeff Fisher6cc1ce7b2019-06-28 23:02:38301void BrowserCommandController::FindBarVisibilityChanged() {
302 if (is_locked_fullscreen_)
303 return;
304 UpdateCloseFindOrStop();
305}
306
wittman76df71db32014-12-18 23:26:58307void BrowserCommandController::ExtensionStateChanged() {
308 // Extensions may disable the bookmark editing commands.
309 UpdateCommandsForBookmarkEditing();
310}
311
Elly Fong-Jones5e6baaf2019-10-10 23:17:14312void BrowserCommandController::TabKeyboardFocusChangedTo(
313 base::Optional<int> index) {
314 UpdateCommandsForTabKeyboardFocus(index);
315}
316
[email protected]5d98294912012-06-27 22:57:40317////////////////////////////////////////////////////////////////////////////////
Ivan Sandrk9669d0e2017-12-15 23:50:20318// BrowserCommandController, CommandUpdater implementation:
[email protected]5d98294912012-06-27 22:57:40319
Ivan Sandrk9669d0e2017-12-15 23:50:20320bool BrowserCommandController::SupportsCommand(int id) const {
321 return command_updater_.SupportsCommand(id);
322}
323
324bool BrowserCommandController::IsCommandEnabled(int id) const {
325 return command_updater_.IsCommandEnabled(id);
326}
327
Edwin Joe6f6fc1e2019-02-27 20:00:37328bool BrowserCommandController::ExecuteCommand(int id,
329 base::TimeTicks time_stamp) {
330 return ExecuteCommandWithDisposition(id, WindowOpenDisposition::CURRENT_TAB,
331 time_stamp);
Ivan Sandrk9669d0e2017-12-15 23:50:20332}
333
334bool BrowserCommandController::ExecuteCommandWithDisposition(
Edwin Joe6f6fc1e2019-02-27 20:00:37335 int id,
336 WindowOpenDisposition disposition,
337 base::TimeTicks time_stamp) {
Ivan Sandrk9669d0e2017-12-15 23:50:20338 // Doesn't go through the command_updater_ to avoid dealing with having a
339 // naming collision for ExecuteCommandWithDisposition (both
340 // CommandUpdaterDelegate and CommandUpdater declare this function so we
341 // choose to not implement CommandUpdaterDelegate inside this class and
342 // therefore command_updater_ doesn't have the delegate set).
343 if (!SupportsCommand(id) || !IsCommandEnabled(id))
344 return false;
345
[email protected]5d98294912012-06-27 22:57:40346 // No commands are enabled if there is not yet any selected tab.
347 // TODO(pkasting): It seems like we should not need this, because either
348 // most/all commands should not have been enabled yet anyway or the ones that
349 // are enabled should be global, or safe themselves against having no selected
350 // tab. However, Ben says he tried removing this before and got lots of
351 // crashes, e.g. from Windows sending WM_COMMANDs at random times during
352 // window construction. This probably could use closer examination someday.
[email protected]59253a652012-11-20 00:17:26353 if (browser_->tab_strip_model()->active_index() == TabStripModel::kNoTab)
Ivan Sandrk9669d0e2017-12-15 23:50:20354 return true;
[email protected]5d98294912012-06-27 22:57:40355
Aran Gilmand217b5b2019-04-11 17:45:43356 DCHECK(command_updater_.IsCommandEnabled(id))
357 << "Invalid/disabled command " << id;
[email protected]5d98294912012-06-27 22:57:40358
[email protected]5d98294912012-06-27 22:57:40359 // The order of commands in this switch statement must match the function
360 // declaration order in browser.h!
361 switch (id) {
362 // Navigation commands
363 case IDC_BACK:
364 GoBack(browser_, disposition);
365 break;
366 case IDC_FORWARD:
367 GoForward(browser_, disposition);
368 break;
369 case IDC_RELOAD:
370 Reload(browser_, disposition);
371 break;
[email protected]58e29032012-08-06 20:19:57372 case IDC_RELOAD_CLEARING_CACHE:
373 ClearCache(browser_);
Nico Webera745ef72018-01-29 23:45:57374 FALLTHROUGH;
toyoshim7dad4b1182016-04-01 14:28:05375 case IDC_RELOAD_BYPASSING_CACHE:
376 ReloadBypassingCache(browser_, disposition);
[email protected]5d98294912012-06-27 22:57:40377 break;
378 case IDC_HOME:
379 Home(browser_, disposition);
380 break;
381 case IDC_OPEN_CURRENT_URL:
382 OpenCurrentURL(browser_);
383 break;
384 case IDC_STOP:
385 Stop(browser_);
386 break;
387
Aran Gilmand217b5b2019-04-11 17:45:43388 // Window management commands
[email protected]5d98294912012-06-27 22:57:40389 case IDC_NEW_WINDOW:
390 NewWindow(browser_);
391 break;
392 case IDC_NEW_INCOGNITO_WINDOW:
Orin Jaworski5e4bcd0c2018-10-16 19:35:05393 NewIncognitoWindow(profile());
[email protected]5d98294912012-06-27 22:57:40394 break;
395 case IDC_CLOSE_WINDOW:
bratell0a7406f2017-03-28 07:46:37396 base::RecordAction(base::UserMetricsAction("CloseWindowByKey"));
[email protected]04b9e692012-08-24 14:49:09397 CloseWindow(browser_);
[email protected]5d98294912012-06-27 22:57:40398 break;
Bettina Dea0a4505922017-09-28 00:53:32399 case IDC_NEW_TAB: {
400 NewTab(browser_);
Collin Baker4ab8ad82019-06-06 17:02:54401#if BUILDFLAG(ENABLE_LEGACY_DESKTOP_IN_PRODUCT_HELP)
Catherine Chungbeb884222017-08-01 15:47:55402 // This is not in NewTab() to avoid tracking programmatic creation of new
403 // tabs by extensions.
Bettina Dea0a4505922017-09-28 00:53:32404 auto* new_tab_tracker =
405 feature_engagement::NewTabTrackerFactory::GetInstance()
406 ->GetForProfile(profile());
407
408 new_tab_tracker->OnNewTabOpened();
409 new_tab_tracker->CloseBubble();
Catherine Chungbeb884222017-08-01 15:47:55410#endif
[email protected]5d98294912012-06-27 22:57:40411 break;
Bettina Dea0a4505922017-09-28 00:53:32412 }
[email protected]5d98294912012-06-27 22:57:40413 case IDC_CLOSE_TAB:
bratell0a7406f2017-03-28 07:46:37414 base::RecordAction(base::UserMetricsAction("CloseTabByKey"));
[email protected]04b9e692012-08-24 14:49:09415 CloseTab(browser_);
[email protected]5d98294912012-06-27 22:57:40416 break;
417 case IDC_SELECT_NEXT_TAB:
bratell0a7406f2017-03-28 07:46:37418 base::RecordAction(base::UserMetricsAction("Accel_SelectNextTab"));
Edwin Joe6f6fc1e2019-02-27 20:00:37419 SelectNextTab(browser_,
420 {TabStripModel::GestureType::kKeyboard, time_stamp});
[email protected]5d98294912012-06-27 22:57:40421 break;
422 case IDC_SELECT_PREVIOUS_TAB:
bratell0a7406f2017-03-28 07:46:37423 base::RecordAction(base::UserMetricsAction("Accel_SelectPreviousTab"));
Edwin Joe6f6fc1e2019-02-27 20:00:37424 SelectPreviousTab(browser_,
425 {TabStripModel::GestureType::kKeyboard, time_stamp});
[email protected]5d98294912012-06-27 22:57:40426 break;
[email protected]5d98294912012-06-27 22:57:40427 case IDC_MOVE_TAB_NEXT:
428 MoveTabNext(browser_);
429 break;
430 case IDC_MOVE_TAB_PREVIOUS:
431 MoveTabPrevious(browser_);
432 break;
433 case IDC_SELECT_TAB_0:
434 case IDC_SELECT_TAB_1:
435 case IDC_SELECT_TAB_2:
436 case IDC_SELECT_TAB_3:
437 case IDC_SELECT_TAB_4:
438 case IDC_SELECT_TAB_5:
439 case IDC_SELECT_TAB_6:
440 case IDC_SELECT_TAB_7:
bratell0a7406f2017-03-28 07:46:37441 base::RecordAction(base::UserMetricsAction("Accel_SelectNumberedTab"));
Edwin Joe6f6fc1e2019-02-27 20:00:37442 SelectNumberedTab(browser_, id - IDC_SELECT_TAB_0,
443 {TabStripModel::GestureType::kKeyboard, time_stamp});
[email protected]5d98294912012-06-27 22:57:40444 break;
445 case IDC_SELECT_LAST_TAB:
bratell0a7406f2017-03-28 07:46:37446 base::RecordAction(base::UserMetricsAction("Accel_SelectNumberedTab"));
Edwin Joe6f6fc1e2019-02-27 20:00:37447 SelectLastTab(browser_,
448 {TabStripModel::GestureType::kKeyboard, time_stamp});
[email protected]5d98294912012-06-27 22:57:40449 break;
450 case IDC_DUPLICATE_TAB:
451 DuplicateTab(browser_);
452 break;
453 case IDC_RESTORE_TAB:
454 RestoreTab(browser_);
Collin Baker9bbe1d82019-03-08 01:16:27455 browser_->window()->OnTabRestored(IDC_RESTORE_TAB);
[email protected]5d98294912012-06-27 22:57:40456 break;
[email protected]5d98294912012-06-27 22:57:40457 case IDC_SHOW_AS_TAB:
458 ConvertPopupToTabbedBrowser(browser_);
459 break;
460 case IDC_FULLSCREEN:
[email protected]3f32b9b2012-07-09 16:59:28461 chrome::ToggleFullscreenMode(browser_);
[email protected]5d98294912012-06-27 22:57:40462 break;
Alan Cutter09965802018-03-27 07:25:29463 case IDC_OPEN_IN_PWA_WINDOW:
464 base::RecordAction(base::UserMetricsAction("OpenActiveTabInPwaWindow"));
Eric Willigerse2ca235a2019-08-17 01:01:06465 web_app::ReparentWebAppForSecureActiveTab(browser_);
Alan Cutter09965802018-03-27 07:25:29466 break;
[email protected]770c6d82012-09-06 22:21:32467
[email protected]d12cc5e2013-10-19 18:25:06468#if defined(OS_CHROMEOS)
469 case IDC_VISIT_DESKTOP_OF_LRU_USER_2:
[email protected]0c930812014-01-30 18:01:47470 case IDC_VISIT_DESKTOP_OF_LRU_USER_3:
zijiehe4dde8072017-02-13 20:38:35471 ExecuteVisitDesktopCommand(id, window()->GetNativeWindow());
[email protected]0c930812014-01-30 18:01:47472 break;
[email protected]d12cc5e2013-10-19 18:25:06473#endif
474
[email protected]893124a22014-04-15 00:45:28475#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
Tom Andersonf15ede502017-11-10 03:17:15476 case IDC_MINIMIZE_WINDOW:
477 browser_->window()->Minimize();
478 break;
479 case IDC_MAXIMIZE_WINDOW:
480 browser_->window()->Maximize();
481 break;
482 case IDC_RESTORE_WINDOW:
483 browser_->window()->Restore();
484 break;
[email protected]af97be4c62014-02-13 14:43:34485 case IDC_USE_SYSTEM_TITLE_BAR: {
zijiehe4dde8072017-02-13 20:38:35486 PrefService* prefs = profile()->GetPrefs();
[email protected]af97be4c62014-02-13 14:43:34487 prefs->SetBoolean(prefs::kUseCustomChromeFrame,
488 !prefs->GetBoolean(prefs::kUseCustomChromeFrame));
489 break;
490 }
491#endif
492
[email protected]5d98294912012-06-27 22:57:40493#if defined(OS_MACOSX)
spqchanb8ffc7d2015-11-17 01:17:21494 case IDC_TOGGLE_FULLSCREEN_TOOLBAR:
495 chrome::ToggleFullscreenToolbar(browser_);
496 break;
spqchan2c5d541e2017-10-25 07:07:11497 case IDC_TOGGLE_JAVASCRIPT_APPLE_EVENTS: {
Robert Sesek03282612018-10-31 15:48:06498 chrome::ToggleJavaScriptFromAppleEventsAllowed(browser_);
spqchan2c5d541e2017-10-25 07:07:11499 break;
500 }
[email protected]5d98294912012-06-27 22:57:40501#endif
502 case IDC_EXIT:
503 Exit();
504 break;
505
506 // Page-related commands
507 case IDC_SAVE_PAGE:
508 SavePage(browser_);
509 break;
Peter Kasting32bb2602019-08-22 19:01:43510 case IDC_BOOKMARK_THIS_TAB:
Collin Baker4ab8ad82019-06-06 17:02:54511#if BUILDFLAG(ENABLE_LEGACY_DESKTOP_IN_PRODUCT_HELP)
Bettina Dea48125dc2017-09-06 03:23:14512 feature_engagement::BookmarkTrackerFactory::GetInstance()
513 ->GetForProfile(profile())
514 ->OnBookmarkAdded();
515#endif
Peter Kasting32bb2602019-08-22 19:01:43516 BookmarkCurrentTabAllowingExtensionOverrides(browser_);
[email protected]5d98294912012-06-27 22:57:40517 break;
[email protected]5d98294912012-06-27 22:57:40518 case IDC_BOOKMARK_ALL_TABS:
Collin Baker4ab8ad82019-06-06 17:02:54519#if BUILDFLAG(ENABLE_LEGACY_DESKTOP_IN_PRODUCT_HELP)
Bettina Dea48125dc2017-09-06 03:23:14520 feature_engagement::BookmarkTrackerFactory::GetInstance()
521 ->GetForProfile(profile())
522 ->OnBookmarkAdded();
523#endif
[email protected]5d98294912012-06-27 22:57:40524 BookmarkAllTabs(browser_);
525 break;
526 case IDC_VIEW_SOURCE:
Lukasz Anforowicze1b954d92017-10-30 21:28:06527 browser_->tab_strip_model()
528 ->GetActiveWebContents()
529 ->GetMainFrame()
530 ->ViewSource();
[email protected]5d98294912012-06-27 22:57:40531 break;
532 case IDC_EMAIL_PAGE_LOCATION:
533 EmailPageLocation(browser_);
534 break;
535 case IDC_PRINT:
536 Print(browser_);
537 break;
bondd052b5f82015-10-28 22:39:32538
Lei Zhang48a4a5262018-04-17 20:18:44539#if BUILDFLAG(ENABLE_PRINTING)
vitalybukaf9433e42014-09-08 10:04:55540 case IDC_BASIC_PRINT:
bratell0a7406f2017-03-28 07:46:37541 base::RecordAction(base::UserMetricsAction("Accel_Advanced_Print"));
vitalybukaf9433e42014-09-08 10:04:55542 BasicPrint(browser_);
[email protected]5d98294912012-06-27 22:57:40543 break;
Lei Zhang48a4a5262018-04-17 20:18:44544#endif // ENABLE_PRINTING
bondd052b5f82015-10-28 22:39:32545
bondd052b5f82015-10-28 22:39:32546 case IDC_SAVE_CREDIT_CARD_FOR_PAGE:
547 SaveCreditCard(browser_);
548 break;
siyua3e599eaa2018-07-13 00:23:06549 case IDC_MIGRATE_LOCAL_CREDIT_CARD_FOR_PAGE:
550 MigrateLocalCards(browser_);
551 break;
siyuac6e018d2019-10-18 01:19:30552 case IDC_SHOW_SAVE_LOCAL_CARD_SIGN_IN_PROMO_IF_APPLICABLE:
553 MaybeShowSaveLocalCardSignInPromo(browser_);
554 break;
555 case IDC_CLOSE_SIGN_IN_PROMO:
556 CloseSaveLocalCardSignInPromo(browser_);
557 break;
[email protected]e625b7602013-10-28 09:24:56558 case IDC_TRANSLATE_PAGE:
559 Translate(browser_);
560 break;
[email protected]4bee4432014-05-05 13:11:41561 case IDC_MANAGE_PASSWORDS_FOR_PAGE:
562 ManagePasswordsForPage(browser_);
563 break;
Tina Wang4c324702019-04-25 18:35:15564 case IDC_SEND_TAB_TO_SELF:
Tina Wang0e7e6e92019-05-14 21:03:03565 SendTabToSelfFromPageAction(browser_);
Tina Wang4c324702019-04-25 18:35:15566 break;
[email protected]4bee4432014-05-05 13:11:41567
[email protected]5d98294912012-06-27 22:57:40568 // Clipboard commands
569 case IDC_CUT:
[email protected]5d98294912012-06-27 22:57:40570 case IDC_COPY:
[email protected]5d98294912012-06-27 22:57:40571 case IDC_PASTE:
pkastingcd3f08bce2015-04-18 13:37:12572 CutCopyPaste(browser_, id);
[email protected]5d98294912012-06-27 22:57:40573 break;
574
575 // Find-in-page
576 case IDC_FIND:
577 Find(browser_);
578 break;
579 case IDC_FIND_NEXT:
580 FindNext(browser_);
581 break;
582 case IDC_FIND_PREVIOUS:
583 FindPrevious(browser_);
584 break;
Jeff Fisher6cc1ce7b2019-06-28 23:02:38585 case IDC_CLOSE_FIND_OR_STOP:
586 if (CanCloseFind(browser_))
587 CloseFind(browser_);
588 else if (IsCommandEnabled(IDC_STOP))
589 ExecuteCommand(IDC_STOP);
590 break;
[email protected]5d98294912012-06-27 22:57:40591
592 // Zoom
593 case IDC_ZOOM_PLUS:
594 Zoom(browser_, content::PAGE_ZOOM_IN);
595 break;
596 case IDC_ZOOM_NORMAL:
597 Zoom(browser_, content::PAGE_ZOOM_RESET);
598 break;
599 case IDC_ZOOM_MINUS:
600 Zoom(browser_, content::PAGE_ZOOM_OUT);
601 break;
602
603 // Focus various bits of UI
604 case IDC_FOCUS_TOOLBAR:
bratell0a7406f2017-03-28 07:46:37605 base::RecordAction(base::UserMetricsAction("Accel_Focus_Toolbar"));
[email protected]5d98294912012-06-27 22:57:40606 FocusToolbar(browser_);
607 break;
608 case IDC_FOCUS_LOCATION:
bratell0a7406f2017-03-28 07:46:37609 base::RecordAction(base::UserMetricsAction("Accel_Focus_Location"));
[email protected]5d98294912012-06-27 22:57:40610 FocusLocationBar(browser_);
611 break;
612 case IDC_FOCUS_SEARCH:
bratell0a7406f2017-03-28 07:46:37613 base::RecordAction(base::UserMetricsAction("Accel_Focus_Search"));
[email protected]5d98294912012-06-27 22:57:40614 FocusSearch(browser_);
615 break;
616 case IDC_FOCUS_MENU_BAR:
617 FocusAppMenu(browser_);
618 break;
619 case IDC_FOCUS_BOOKMARKS:
bratell0a7406f2017-03-28 07:46:37620 base::RecordAction(base::UserMetricsAction("Accel_Focus_Bookmarks"));
[email protected]5d98294912012-06-27 22:57:40621 FocusBookmarksToolbar(browser_);
622 break;
David Tsengc0b1b642018-01-24 07:12:27623 case IDC_FOCUS_INACTIVE_POPUP_FOR_ACCESSIBILITY:
624 FocusInactivePopupForAccessibility(browser_);
[email protected]822ca8c62013-04-19 00:49:15625 break;
[email protected]5d98294912012-06-27 22:57:40626 case IDC_FOCUS_NEXT_PANE:
627 FocusNextPane(browser_);
628 break;
629 case IDC_FOCUS_PREVIOUS_PANE:
630 FocusPreviousPane(browser_);
631 break;
632
633 // Show various bits of UI
634 case IDC_OPEN_FILE:
635 browser_->OpenFile();
636 break;
Alan Cutter81ac726682018-09-25 00:18:36637 case IDC_CREATE_SHORTCUT:
Alan Cutter98844a9fa2019-06-19 21:11:40638 base::RecordAction(base::UserMetricsAction("CreateShortcut"));
Alan Cutter81ac726682018-09-25 00:18:36639 CreateBookmarkAppFromCurrentWebContents(browser_,
640 true /* force_shortcut_app */);
641 break;
642 case IDC_INSTALL_PWA:
Alan Cutter98844a9fa2019-06-19 21:11:40643 base::RecordAction(base::UserMetricsAction("InstallWebAppFromMenu"));
Alan Cutter81ac726682018-09-25 00:18:36644 CreateBookmarkAppFromCurrentWebContents(browser_,
645 false /* force_shortcut_app */);
[email protected]488e3952013-11-18 05:29:14646 break;
[email protected]5d98294912012-06-27 22:57:40647 case IDC_DEV_TOOLS:
[email protected]c934c382013-11-01 00:36:01648 ToggleDevToolsWindow(browser_, DevToolsToggleAction::Show());
[email protected]5d98294912012-06-27 22:57:40649 break;
650 case IDC_DEV_TOOLS_CONSOLE:
einbinderdfa567b2016-12-16 01:15:52651 ToggleDevToolsWindow(browser_, DevToolsToggleAction::ShowConsolePanel());
[email protected]5d98294912012-06-27 22:57:40652 break;
[email protected]2056c3192013-10-21 22:40:51653 case IDC_DEV_TOOLS_DEVICES:
654 InspectUI::InspectDevices(browser_);
655 break;
[email protected]5d98294912012-06-27 22:57:40656 case IDC_DEV_TOOLS_INSPECT:
[email protected]c934c382013-11-01 00:36:01657 ToggleDevToolsWindow(browser_, DevToolsToggleAction::Inspect());
[email protected]5d98294912012-06-27 22:57:40658 break;
[email protected]d16657c2012-09-03 14:25:10659 case IDC_DEV_TOOLS_TOGGLE:
[email protected]c934c382013-11-01 00:36:01660 ToggleDevToolsWindow(browser_, DevToolsToggleAction::Toggle());
[email protected]d16657c2012-09-03 14:25:10661 break;
[email protected]5d98294912012-06-27 22:57:40662 case IDC_TASK_MANAGER:
[email protected]29c262de2013-06-22 15:39:38663 OpenTaskManager(browser_);
[email protected]5d98294912012-06-27 22:57:40664 break;
caelyn4e4e08a2015-02-04 21:27:49665#if defined(OS_CHROMEOS)
666 case IDC_TAKE_SCREENSHOT:
667 TakeScreenshot();
668 break;
669#endif
Nico Weber0cc71122019-07-29 17:30:40670#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
[email protected]5d98294912012-06-27 22:57:40671 case IDC_FEEDBACK:
afakhryf4575bd2017-04-28 02:21:04672 OpenFeedbackDialog(browser_, kFeedbackSourceBrowserCommand);
[email protected]5d98294912012-06-27 22:57:40673 break;
[email protected]236ad3022013-09-04 03:27:43674#endif
[email protected]5d98294912012-06-27 22:57:40675 case IDC_SHOW_BOOKMARK_BAR:
676 ToggleBookmarkBar(browser_);
677 break;
678 case IDC_PROFILING_ENABLED:
Bryce Thomas96b07772018-11-07 03:04:15679 content::Profiling::Toggle();
[email protected]5d98294912012-06-27 22:57:40680 break;
681
682 case IDC_SHOW_BOOKMARK_MANAGER:
683 ShowBookmarkManager(browser_);
684 break;
685 case IDC_SHOW_APP_MENU:
bratell0a7406f2017-03-28 07:46:37686 base::RecordAction(base::UserMetricsAction("Accel_Show_App_Menu"));
[email protected]5d98294912012-06-27 22:57:40687 ShowAppMenu(browser_);
688 break;
689 case IDC_SHOW_AVATAR_MENU:
690 ShowAvatarMenu(browser_);
691 break;
692 case IDC_SHOW_HISTORY:
693 ShowHistory(browser_);
694 break;
695 case IDC_SHOW_DOWNLOADS:
696 ShowDownloads(browser_);
697 break;
698 case IDC_MANAGE_EXTENSIONS:
[email protected]bc9833c32013-02-28 04:05:08699 ShowExtensions(browser_, std::string());
[email protected]5d98294912012-06-27 22:57:40700 break;
701 case IDC_OPTIONS:
702 ShowSettings(browser_);
703 break;
704 case IDC_EDIT_SEARCH_ENGINES:
705 ShowSearchEngineSettings(browser_);
706 break;
707 case IDC_VIEW_PASSWORDS:
708 ShowPasswordManager(browser_);
709 break;
710 case IDC_CLEAR_BROWSING_DATA:
711 ShowClearBrowsingDataDialog(browser_);
712 break;
713 case IDC_IMPORT_SETTINGS:
714 ShowImportDialog(browser_);
715 break;
[email protected]9b7ab882012-09-10 23:46:36716 case IDC_TOGGLE_REQUEST_TABLET_SITE:
717 ToggleRequestTabletSite(browser_);
718 break;
[email protected]5d98294912012-06-27 22:57:40719 case IDC_ABOUT:
720 ShowAboutChrome(browser_);
721 break;
722 case IDC_UPGRADE_DIALOG:
723 OpenUpdateChromeDialog(browser_);
724 break;
[email protected]5d98294912012-06-27 22:57:40725 case IDC_HELP_PAGE_VIA_KEYBOARD:
726 ShowHelp(browser_, HELP_SOURCE_KEYBOARD);
727 break;
728 case IDC_HELP_PAGE_VIA_MENU:
729 ShowHelp(browser_, HELP_SOURCE_MENU);
730 break;
Bret Sepulveda2d018662017-05-18 21:31:48731 case IDC_SHOW_BETA_FORUM:
732 ShowBetaForum(browser_);
733 break;
David Roger7e25dff2019-02-07 09:03:17734#if !defined(OS_CHROMEOS)
estade8c0780f2015-08-21 23:36:41735 case IDC_SHOW_SIGNIN:
gogerald71bf6c902015-12-08 00:49:37736 ShowBrowserSigninOrSettings(
737 browser_, signin_metrics::AccessPoint::ACCESS_POINT_MENU);
estade8c0780f2015-08-21 23:36:41738 break;
David Roger7e25dff2019-02-07 09:03:17739#endif
[email protected]6bd370b2014-05-28 14:19:47740 case IDC_DISTILL_PAGE:
Aran Gilman6586e8f2019-06-03 18:50:56741 ToggleDistilledView(browser_);
[email protected]6bd370b2014-05-28 14:19:47742 break;
apacible45cbfc92015-09-28 22:45:41743 case IDC_ROUTE_MEDIA:
744 RouteMedia(browser_);
745 break;
Tommy Steimelc4477982017-11-29 18:07:18746 case IDC_WINDOW_MUTE_SITE:
747 MuteSite(browser_);
ellyjones0101ba02017-05-19 15:50:26748 break;
749 case IDC_WINDOW_PIN_TAB:
750 PinTab(browser_);
751 break;
Yann Dagocd13225a2019-03-07 18:39:05752 case IDC_SHOW_MANAGEMENT_PAGE: {
Yann Dagoc1d1c4af2019-04-17 13:40:51753 ShowSingletonTab(browser_, GURL(kChromeUIManagementURL));
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:27754 break;
Yann Dagocd13225a2019-03-07 18:39:05755 }
Elly Fong-Jones5e6baaf2019-10-10 23:17:14756 case IDC_MUTE_TARGET_SITE:
757 MuteSiteForKeyboardFocusedTab(browser_);
758 break;
759 case IDC_PIN_TARGET_TAB:
760 PinKeyboardFocusedTab(browser_);
761 break;
762 case IDC_DUPLICATE_TARGET_TAB:
763 DuplicateKeyboardFocusedTab(browser_);
764 break;
Christopher Lam0dbac2b2017-11-14 07:12:10765 // Hosted App commands
766 case IDC_COPY_URL:
767 CopyURL(browser_);
768 break;
769 case IDC_OPEN_IN_CHROME:
770 OpenInChrome(browser_);
771 break;
772 case IDC_SITE_SETTINGS:
773 ShowSiteSettings(
774 browser_,
775 browser_->tab_strip_model()->GetActiveWebContents()->GetVisibleURL());
776 break;
Alan Cuttera99d1522019-09-12 02:09:22777 case IDC_WEB_APP_MENU_APP_INFO: {
Dana Fried08b774a2019-05-10 18:21:48778 content::WebContents* const web_contents =
779 browser_->tab_strip_model()->GetActiveWebContents();
780 if (web_contents) {
781 ShowPageInfoDialog(web_contents,
782 base::BindOnce(&AppInfoDialogClosedCallback,
783 base::Unretained(web_contents)),
784 bubble_anchor_util::kAppMenuButton);
785 }
Christopher Lam86b52712017-12-04 01:58:33786 break;
Dana Fried08b774a2019-05-10 18:21:48787 }
[email protected]5d98294912012-06-27 22:57:40788 default:
789 LOG(WARNING) << "Received Unimplemented Command: " << id;
790 break;
791 }
Ivan Sandrk9669d0e2017-12-15 23:50:20792
793 return true;
794}
795
796void BrowserCommandController::AddCommandObserver(int id,
797 CommandObserver* observer) {
798 command_updater_.AddCommandObserver(id, observer);
799}
800
801void BrowserCommandController::RemoveCommandObserver(
Aran Gilmand217b5b2019-04-11 17:45:43802 int id,
803 CommandObserver* observer) {
Ivan Sandrk9669d0e2017-12-15 23:50:20804 command_updater_.RemoveCommandObserver(id, observer);
805}
806
807void BrowserCommandController::RemoveCommandObserver(
808 CommandObserver* observer) {
809 command_updater_.RemoveCommandObserver(observer);
810}
811
812bool BrowserCommandController::UpdateCommandEnabled(int id, bool state) {
813 if (is_locked_fullscreen_)
814 return false;
815
816 return command_updater_.UpdateCommandEnabled(id, state);
[email protected]5d98294912012-06-27 22:57:40817}
818
Mihai Sardarescuf87ccc12018-09-26 10:37:45819////////////////////////////////////////////////////////////////////////////////
820// BrowserCommandController, SigninPrefObserver implementation:
821
822void BrowserCommandController::OnSigninAllowedPrefChange() {
823 // For unit tests, we don't have a window.
824 if (!window())
825 return;
826 UpdateShowSyncState(IsShowingMainUI());
827}
828
[email protected]5d98294912012-06-27 22:57:40829// BrowserCommandController, TabStripModelObserver implementation:
830
sangwoo.ko1ae265f12018-10-18 08:30:28831void BrowserCommandController::OnTabStripModelChanged(
832 TabStripModel* tab_strip_model,
833 const TabStripModelChange& change,
834 const TabStripSelectionChange& selection) {
Dana Frieda391e6ce2019-05-08 00:00:20835 std::vector<content::WebContents*> new_contents;
836 std::vector<content::WebContents*> old_contents;
[email protected]5d98294912012-06-27 22:57:40837
Dana Frieda391e6ce2019-05-08 00:00:20838 switch (change.type()) {
839 case TabStripModelChange::kInserted:
840 for (const auto& contents : change.GetInsert()->contents)
841 new_contents.push_back(contents.contents);
842 break;
843 case TabStripModelChange::kReplaced: {
844 auto* replace = change.GetReplace();
845 new_contents.push_back(replace->new_contents);
846 old_contents.push_back(replace->old_contents);
847 break;
sangwoo.ko1ae265f12018-10-18 08:30:28848 }
Dana Frieda391e6ce2019-05-08 00:00:20849 case TabStripModelChange::kRemoved:
850 for (const auto& contents : change.GetRemove()->contents)
851 old_contents.push_back(contents.contents);
852 break;
853 default:
854 break;
sangwoo.ko1ae265f12018-10-18 08:30:28855 }
Dana Frieda391e6ce2019-05-08 00:00:20856
857 for (auto* contents : old_contents)
858 RemoveInterstitialObservers(contents);
859 for (auto* contents : new_contents)
860 AddInterstitialObservers(contents);
[email protected]5d98294912012-06-27 22:57:40861}
862
[email protected]3cac87232012-11-20 01:48:27863void BrowserCommandController::TabBlockedStateChanged(
864 content::WebContents* contents,
865 int index) {
866 PrintingStateChanged();
867 FullscreenStateChanged();
868 UpdateCommandsForFind();
apacible45cbfc92015-09-28 22:45:41869 UpdateCommandsForMediaRouter();
[email protected]3cac87232012-11-20 01:48:27870}
871
[email protected]5d98294912012-06-27 22:57:40872////////////////////////////////////////////////////////////////////////////////
873// BrowserCommandController, TabRestoreServiceObserver implementation:
874
875void BrowserCommandController::TabRestoreServiceChanged(
blundell74001adc2015-09-18 11:04:25876 sessions::TabRestoreService* service) {
[email protected]2e9369e2014-08-15 09:12:53877 UpdateTabRestoreCommandState();
[email protected]5d98294912012-06-27 22:57:40878}
879
880void BrowserCommandController::TabRestoreServiceDestroyed(
blundell74001adc2015-09-18 11:04:25881 sessions::TabRestoreService* service) {
[email protected]5d98294912012-06-27 22:57:40882 service->RemoveObserver(this);
883}
884
[email protected]2e9369e2014-08-15 09:12:53885void BrowserCommandController::TabRestoreServiceLoaded(
blundell74001adc2015-09-18 11:04:25886 sessions::TabRestoreService* service) {
[email protected]2e9369e2014-08-15 09:12:53887 UpdateTabRestoreCommandState();
888}
889
[email protected]5d98294912012-06-27 22:57:40890////////////////////////////////////////////////////////////////////////////////
[email protected]5d98294912012-06-27 22:57:40891// BrowserCommandController, private:
892
[email protected]20ca0382013-02-28 19:50:07893class BrowserCommandController::InterstitialObserver
894 : public content::WebContentsObserver {
895 public:
896 InterstitialObserver(BrowserCommandController* controller,
897 content::WebContents* web_contents)
Aran Gilmand217b5b2019-04-11 17:45:43898 : WebContentsObserver(web_contents), controller_(controller) {}
[email protected]20ca0382013-02-28 19:50:07899
dcheng5dd5ff62014-10-21 12:42:38900 void DidAttachInterstitialPage() override {
[email protected]20ca0382013-02-28 19:50:07901 controller_->UpdateCommandsForTabState();
902 }
903
dcheng5dd5ff62014-10-21 12:42:38904 void DidDetachInterstitialPage() override {
[email protected]20ca0382013-02-28 19:50:07905 controller_->UpdateCommandsForTabState();
906 }
907
908 private:
909 BrowserCommandController* controller_;
910
911 DISALLOW_COPY_AND_ASSIGN(InterstitialObserver);
912};
913
[email protected]6a414ff2013-02-27 08:22:54914bool BrowserCommandController::IsShowingMainUI() {
manukf1a116392018-10-25 16:28:24915 return browser_->SupportsWindowFeature(Browser::FEATURE_TABSTRIP);
916}
917
918bool BrowserCommandController::IsShowingLocationBar() {
919 return browser_->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR);
[email protected]5d98294912012-06-27 22:57:40920}
921
922void BrowserCommandController::InitCommandState() {
923 // All browser commands whose state isn't set automagically some other way
924 // (like Back & Forward with initial page load) must have their state
925 // initialized here, otherwise they will be forever disabled.
926
Ivan Sandrk9669d0e2017-12-15 23:50:20927 if (is_locked_fullscreen_)
928 return;
929
[email protected]5d98294912012-06-27 22:57:40930 // Navigation commands
931 command_updater_.UpdateCommandEnabled(IDC_RELOAD, true);
toyoshim7dad4b1182016-04-01 14:28:05932 command_updater_.UpdateCommandEnabled(IDC_RELOAD_BYPASSING_CACHE, true);
[email protected]58e29032012-08-06 20:19:57933 command_updater_.UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE, true);
[email protected]5d98294912012-06-27 22:57:40934
935 // Window management commands
936 command_updater_.UpdateCommandEnabled(IDC_CLOSE_WINDOW, true);
937 command_updater_.UpdateCommandEnabled(IDC_NEW_TAB, true);
938 command_updater_.UpdateCommandEnabled(IDC_CLOSE_TAB, true);
939 command_updater_.UpdateCommandEnabled(IDC_DUPLICATE_TAB, true);
[email protected]2e9369e2014-08-15 09:12:53940 UpdateTabRestoreCommandState();
[email protected]d28d3782013-02-26 16:31:55941 command_updater_.UpdateCommandEnabled(IDC_EXIT, true);
[email protected]5d98294912012-06-27 22:57:40942 command_updater_.UpdateCommandEnabled(IDC_DEBUG_FRAME_TOGGLE, true);
[email protected]d12cc5e2013-10-19 18:25:06943#if defined(OS_CHROMEOS)
James Cook934abaf2017-09-19 22:21:58944 command_updater_.UpdateCommandEnabled(IDC_MINIMIZE_WINDOW, true);
[email protected]d12cc5e2013-10-19 18:25:06945 command_updater_.UpdateCommandEnabled(IDC_VISIT_DESKTOP_OF_LRU_USER_2, true);
946 command_updater_.UpdateCommandEnabled(IDC_VISIT_DESKTOP_OF_LRU_USER_3, true);
947#endif
[email protected]893124a22014-04-15 00:45:28948#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
Tom Andersonf15ede502017-11-10 03:17:15949 command_updater_.UpdateCommandEnabled(IDC_MINIMIZE_WINDOW, true);
950 command_updater_.UpdateCommandEnabled(IDC_MAXIMIZE_WINDOW, true);
951 command_updater_.UpdateCommandEnabled(IDC_RESTORE_WINDOW, true);
Maksim Sisovac767592018-08-10 08:00:15952 bool use_system_title_bar = true;
953#if defined(USE_OZONE)
954 use_system_title_bar = ui::OzonePlatform::GetInstance()
955 ->GetPlatformProperties()
956 .use_system_title_bar;
957#endif
958 command_updater_.UpdateCommandEnabled(IDC_USE_SYSTEM_TITLE_BAR,
959 use_system_title_bar);
[email protected]af97be4c62014-02-13 14:43:34960#endif
Alan Cutter09965802018-03-27 07:25:29961 command_updater_.UpdateCommandEnabled(IDC_OPEN_IN_PWA_WINDOW, true);
[email protected]5d98294912012-06-27 22:57:40962
963 // Page-related commands
964 command_updater_.UpdateCommandEnabled(IDC_EMAIL_PAGE_LOCATION, true);
[email protected]4bee4432014-05-05 13:11:41965 command_updater_.UpdateCommandEnabled(IDC_MANAGE_PASSWORDS_FOR_PAGE, true);
[email protected]5d98294912012-06-27 22:57:40966
967 // Zoom
968 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MENU, true);
969 command_updater_.UpdateCommandEnabled(IDC_ZOOM_PLUS, true);
[email protected]d93dbd12014-08-04 23:42:53970 command_updater_.UpdateCommandEnabled(IDC_ZOOM_NORMAL, false);
[email protected]5d98294912012-06-27 22:57:40971 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MINUS, true);
972
973 // Show various bits of UI
Aran Gilmand217b5b2019-04-11 17:45:43974 const bool guest_session =
975 profile()->IsGuestSession() || profile()->IsSystemProfile();
mlerman7831f57d2015-05-25 11:40:15976 DCHECK(!profile()->IsSystemProfile())
977 << "Ought to never have browser for the system profile.";
Joel Hockey2687ab52019-08-14 23:59:46978 const bool normal_window = browser_->is_type_normal();
[email protected]05454532013-01-22 21:09:08979 UpdateOpenFileState(&command_updater_);
[email protected]5d98294912012-06-27 22:57:40980 UpdateCommandsForDevTools();
981 command_updater_.UpdateCommandEnabled(IDC_TASK_MANAGER, CanOpenTaskManager());
[email protected]338416c02014-05-13 16:47:06982 command_updater_.UpdateCommandEnabled(IDC_SHOW_HISTORY, !guest_session);
[email protected]5d98294912012-06-27 22:57:40983 command_updater_.UpdateCommandEnabled(IDC_SHOW_DOWNLOADS, true);
[email protected]674b65672014-06-02 23:21:56984 command_updater_.UpdateCommandEnabled(IDC_HELP_MENU, true);
[email protected]5d98294912012-06-27 22:57:40985 command_updater_.UpdateCommandEnabled(IDC_HELP_PAGE_VIA_KEYBOARD, true);
986 command_updater_.UpdateCommandEnabled(IDC_HELP_PAGE_VIA_MENU, true);
Bret Sepulveda2d018662017-05-18 21:31:48987 command_updater_.UpdateCommandEnabled(IDC_SHOW_BETA_FORUM, true);
[email protected]338416c02014-05-13 16:47:06988 command_updater_.UpdateCommandEnabled(IDC_BOOKMARKS_MENU, !guest_session);
Aran Gilmand217b5b2019-04-11 17:45:43989 command_updater_.UpdateCommandEnabled(
990 IDC_RECENT_TABS_MENU, !guest_session && !profile()->IsOffTheRecord());
Rohit Agarwal2a2230b2019-08-21 15:53:41991 command_updater_.UpdateCommandEnabled(
992 IDC_CLEAR_BROWSING_DATA,
993 !guest_session && !profile()->IsIncognitoProfile());
[email protected]39d47592014-01-10 21:42:45994#if defined(OS_CHROMEOS)
caelyn4e4e08a2015-02-04 21:27:49995 command_updater_.UpdateCommandEnabled(IDC_TAKE_SCREENSHOT, true);
Ramin Halavatif362b7a2019-04-17 05:24:50996 // Chrome OS uses the system tray menu to handle multi-profiles. Avatar menu
997 // is only required in incognito mode.
Ramin Halavati2394dc452019-05-21 16:21:58998 if (profile()->IsIncognitoProfile())
[email protected]338416c02014-05-13 16:47:06999 command_updater_.UpdateCommandEnabled(IDC_SHOW_AVATAR_MENU, true);
Ramin Halavatif362b7a2019-04-17 05:24:501000#else
1001 if (normal_window)
1002 command_updater_.UpdateCommandEnabled(IDC_SHOW_AVATAR_MENU, true);
[email protected]39d47592014-01-10 21:42:451003#endif
siyuac6e018d2019-10-18 01:19:301004 command_updater_.UpdateCommandEnabled(
1005 IDC_SHOW_SAVE_LOCAL_CARD_SIGN_IN_PROMO_IF_APPLICABLE, true);
1006 command_updater_.UpdateCommandEnabled(IDC_CLOSE_SIGN_IN_PROMO, true);
[email protected]5d98294912012-06-27 22:57:401007
Mihai Sardarescu2ec5205682018-09-26 09:20:051008 UpdateShowSyncState(true);
1009
[email protected]5d98294912012-06-27 22:57:401010 // Navigation commands
Joel Hockey2687ab52019-08-14 23:59:461011 command_updater_.UpdateCommandEnabled(
1012 IDC_HOME, normal_window || browser_->deprecated_is_app());
[email protected]5d98294912012-06-27 22:57:401013
Alan Cutterd0bd51902019-05-22 04:15:231014 const bool is_web_app =
1015 web_app::AppBrowserController::IsForWebAppBrowser(browser_);
Christopher Lam0dbac2b2017-11-14 07:12:101016 // Hosted app browser commands.
Alan Cutterd0bd51902019-05-22 04:15:231017 command_updater_.UpdateCommandEnabled(IDC_COPY_URL, is_web_app);
1018 command_updater_.UpdateCommandEnabled(IDC_OPEN_IN_CHROME, is_web_app);
1019 command_updater_.UpdateCommandEnabled(IDC_SITE_SETTINGS, is_web_app);
Alan Cuttera99d1522019-09-12 02:09:221020 command_updater_.UpdateCommandEnabled(IDC_WEB_APP_MENU_APP_INFO, is_web_app);
Christopher Lam0dbac2b2017-11-14 07:12:101021
[email protected]5d98294912012-06-27 22:57:401022 // Window management commands
[email protected]5d98294912012-06-27 22:57:401023 command_updater_.UpdateCommandEnabled(IDC_SELECT_NEXT_TAB, normal_window);
Aran Gilmand217b5b2019-04-11 17:45:431024 command_updater_.UpdateCommandEnabled(IDC_SELECT_PREVIOUS_TAB, normal_window);
[email protected]5d98294912012-06-27 22:57:401025 command_updater_.UpdateCommandEnabled(IDC_MOVE_TAB_NEXT, normal_window);
1026 command_updater_.UpdateCommandEnabled(IDC_MOVE_TAB_PREVIOUS, normal_window);
1027 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_0, normal_window);
1028 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_1, normal_window);
1029 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_2, normal_window);
1030 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_3, normal_window);
1031 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_4, normal_window);
1032 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_5, normal_window);
1033 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_6, normal_window);
1034 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_7, normal_window);
1035 command_updater_.UpdateCommandEnabled(IDC_SELECT_LAST_TAB, normal_window);
[email protected]5d98294912012-06-27 22:57:401036
[email protected]338416c02014-05-13 16:47:061037 // These are always enabled; the menu determines their menu item visibility.
[email protected]5d98294912012-06-27 22:57:401038 command_updater_.UpdateCommandEnabled(IDC_UPGRADE_DIALOG, true);
[email protected]5d98294912012-06-27 22:57:401039
[email protected]6bd370b2014-05-28 14:19:471040 // Distill current page.
Aran Gilman7b6ccf5d2019-09-05 19:16:021041 command_updater_.UpdateCommandEnabled(IDC_DISTILL_PAGE,
1042 dom_distiller::IsDomDistillerEnabled());
[email protected]6bd370b2014-05-28 14:19:471043
Tommy Steimelc4477982017-11-29 18:07:181044 command_updater_.UpdateCommandEnabled(IDC_WINDOW_MUTE_SITE, normal_window);
ellyjones0101ba02017-05-19 15:50:261045 command_updater_.UpdateCommandEnabled(IDC_WINDOW_PIN_TAB, normal_window);
1046
[email protected]338416c02014-05-13 16:47:061047 // Initialize other commands whose state changes based on various conditions.
[email protected]32dfede2013-08-25 15:48:251048 UpdateCommandsForFullscreenMode();
[email protected]5d98294912012-06-27 22:57:401049 UpdateCommandsForContentRestrictionState();
[email protected]5d98294912012-06-27 22:57:401050 UpdateCommandsForBookmarkEditing();
[email protected]5d98294912012-06-27 22:57:401051 UpdateCommandsForIncognitoAvailability();
Elly Fong-Jones5e6baaf2019-10-10 23:17:141052 UpdateCommandsForTabKeyboardFocus(GetKeyboardFocusedTabIndex(browser_));
[email protected]5d98294912012-06-27 22:57:401053}
1054
[email protected]05454532013-01-22 21:09:081055// static
1056void BrowserCommandController::UpdateSharedCommandsForIncognitoAvailability(
1057 CommandUpdater* command_updater,
1058 Profile* profile) {
mlermane01e6de2014-09-29 19:26:471059 const bool guest_session = profile->IsGuestSession();
1060 // TODO(mlerman): Make GetAvailability account for profile->IsGuestSession().
[email protected]5d98294912012-06-27 22:57:401061 IncognitoModePrefs::Availability incognito_availability =
[email protected]05454532013-01-22 21:09:081062 IncognitoModePrefs::GetAvailability(profile->GetPrefs());
1063 command_updater->UpdateCommandEnabled(
Aran Gilmand217b5b2019-04-11 17:45:431064 IDC_NEW_WINDOW, incognito_availability != IncognitoModePrefs::FORCED);
[email protected]05454532013-01-22 21:09:081065 command_updater->UpdateCommandEnabled(
[email protected]5d98294912012-06-27 22:57:401066 IDC_NEW_INCOGNITO_WINDOW,
mlermane01e6de2014-09-29 19:26:471067 incognito_availability != IncognitoModePrefs::DISABLED && !guest_session);
[email protected]5d98294912012-06-27 22:57:401068
[email protected]57b25292014-05-01 16:31:061069 const bool forced_incognito =
1070 incognito_availability == IncognitoModePrefs::FORCED ||
1071 guest_session; // Guest always runs in Incognito mode.
[email protected]05454532013-01-22 21:09:081072 command_updater->UpdateCommandEnabled(
[email protected]5d98294912012-06-27 22:57:401073 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]57b25292014-05-01 16:31:061074 browser_defaults::bookmarks_enabled && !forced_incognito);
Devlin Cronin21dba422018-05-30 15:45:161075 extensions::ExtensionService* extension_service =
[email protected]03d25812014-06-22 19:41:551076 extensions::ExtensionSystem::Get(profile)->extension_service();
[email protected]57b25292014-05-01 16:31:061077 const bool enable_extensions =
[email protected]5d98294912012-06-27 22:57:401078 extension_service && extension_service->extensions_enabled();
[email protected]5d98294912012-06-27 22:57:401079
[email protected]57b25292014-05-01 16:31:061080 // Bookmark manager and settings page/subpages are forced to open in normal
1081 // mode. For this reason we disable these commands when incognito is forced.
1082 command_updater->UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS,
1083 enable_extensions && !forced_incognito);
1084
1085 command_updater->UpdateCommandEnabled(IDC_IMPORT_SETTINGS, !forced_incognito);
1086 command_updater->UpdateCommandEnabled(IDC_OPTIONS,
1087 !forced_incognito || guest_session);
estade8c0780f2015-08-21 23:36:411088 command_updater->UpdateCommandEnabled(IDC_SHOW_SIGNIN, !forced_incognito);
[email protected]05454532013-01-22 21:09:081089}
1090
1091void BrowserCommandController::UpdateCommandsForIncognitoAvailability() {
Ivan Sandrk9669d0e2017-12-15 23:50:201092 if (is_locked_fullscreen_)
1093 return;
1094
[email protected]05454532013-01-22 21:09:081095 UpdateSharedCommandsForIncognitoAvailability(&command_updater_, profile());
1096
[email protected]6a414ff2013-02-27 08:22:541097 if (!IsShowingMainUI()) {
[email protected]05454532013-01-22 21:09:081098 command_updater_.UpdateCommandEnabled(IDC_IMPORT_SETTINGS, false);
1099 command_updater_.UpdateCommandEnabled(IDC_OPTIONS, false);
1100 }
[email protected]5d98294912012-06-27 22:57:401101}
1102
1103void BrowserCommandController::UpdateCommandsForTabState() {
Ivan Sandrk9669d0e2017-12-15 23:50:201104 if (is_locked_fullscreen_)
1105 return;
1106
[email protected]617ee962013-01-29 20:49:121107 WebContents* current_web_contents =
1108 browser_->tab_strip_model()->GetActiveWebContents();
[email protected]1c5119c2012-09-19 00:08:571109 if (!current_web_contents) // May be NULL during tab restore.
[email protected]5d98294912012-06-27 22:57:401110 return;
[email protected]5d98294912012-06-27 22:57:401111
1112 // Navigation commands
1113 command_updater_.UpdateCommandEnabled(IDC_BACK, CanGoBack(browser_));
1114 command_updater_.UpdateCommandEnabled(IDC_FORWARD, CanGoForward(browser_));
1115 command_updater_.UpdateCommandEnabled(IDC_RELOAD, CanReload(browser_));
toyoshim7dad4b1182016-04-01 14:28:051116 command_updater_.UpdateCommandEnabled(IDC_RELOAD_BYPASSING_CACHE,
[email protected]5d98294912012-06-27 22:57:401117 CanReload(browser_));
[email protected]58e29032012-08-06 20:19:571118 command_updater_.UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE,
1119 CanReload(browser_));
[email protected]5d98294912012-06-27 22:57:401120
1121 // Window management commands
Aran Gilmand217b5b2019-04-11 17:45:431122 command_updater_.UpdateCommandEnabled(
Joel Hockey2687ab52019-08-14 23:59:461123 IDC_DUPLICATE_TAB,
1124 !browser_->deprecated_is_app() && CanDuplicateTab(browser_));
Tommy Steimelc4477982017-11-29 18:07:181125 command_updater_.UpdateCommandEnabled(IDC_WINDOW_MUTE_SITE,
Joel Hockey2687ab52019-08-14 23:59:461126 !browser_->deprecated_is_app());
ellyjones0101ba02017-05-19 15:50:261127 command_updater_.UpdateCommandEnabled(IDC_WINDOW_PIN_TAB,
Joel Hockey2687ab52019-08-14 23:59:461128 !browser_->deprecated_is_app());
[email protected]5d98294912012-06-27 22:57:401129
1130 // Page-related commands
1131 window()->SetStarredState(
[email protected]1c5119c2012-09-19 00:08:571132 BookmarkTabHelper::FromWebContents(current_web_contents)->is_starred());
[email protected]5423c372012-08-22 05:50:161133 window()->ZoomChangedForActiveTab(false);
[email protected]5d98294912012-06-27 22:57:401134 command_updater_.UpdateCommandEnabled(IDC_VIEW_SOURCE,
1135 CanViewSource(browser_));
1136 command_updater_.UpdateCommandEnabled(IDC_EMAIL_PAGE_LOCATION,
1137 CanEmailPageLocation(browser_));
Joel Hockey2687ab52019-08-14 23:59:461138 if (browser_->is_type_devtools())
[email protected]5d98294912012-06-27 22:57:401139 command_updater_.UpdateCommandEnabled(IDC_OPEN_FILE, false);
1140
Alan Cutter81ac726682018-09-25 00:18:361141 bool can_create_bookmark_app = CanCreateBookmarkApp(browser_);
1142 command_updater_.UpdateCommandEnabled(IDC_INSTALL_PWA,
1143 can_create_bookmark_app);
1144 command_updater_.UpdateCommandEnabled(IDC_CREATE_SHORTCUT,
1145 can_create_bookmark_app);
Alan Cutter403cebb2018-05-17 05:22:451146 command_updater_.UpdateCommandEnabled(IDC_OPEN_IN_PWA_WINDOW,
Alan Cutter81ac726682018-09-25 00:18:361147 can_create_bookmark_app);
[email protected]5d98294912012-06-27 22:57:401148
[email protected]9b7ab882012-09-10 23:46:361149 command_updater_.UpdateCommandEnabled(
1150 IDC_TOGGLE_REQUEST_TABLET_SITE,
1151 CanRequestTabletSite(current_web_contents));
1152
[email protected]5d98294912012-06-27 22:57:401153 UpdateCommandsForContentRestrictionState();
1154 UpdateCommandsForBookmarkEditing();
[email protected]3cac87232012-11-20 01:48:271155 UpdateCommandsForFind();
apacible45cbfc92015-09-28 22:45:411156 UpdateCommandsForMediaRouter();
[email protected]d93dbd12014-08-04 23:42:531157 // Update the zoom commands when an active tab is selected.
1158 UpdateCommandsForZoomState();
Elly Fong-Jones5e6baaf2019-10-10 23:17:141159 UpdateCommandsForTabKeyboardFocus(GetKeyboardFocusedTabIndex(browser_));
[email protected]d93dbd12014-08-04 23:42:531160}
1161
1162void BrowserCommandController::UpdateCommandsForZoomState() {
Aran Gilmand217b5b2019-04-11 17:45:431163 WebContents* contents = browser_->tab_strip_model()->GetActiveWebContents();
[email protected]d93dbd12014-08-04 23:42:531164 if (!contents)
1165 return;
Aran Gilmand217b5b2019-04-11 17:45:431166 command_updater_.UpdateCommandEnabled(IDC_ZOOM_PLUS, CanZoomIn(contents));
a.sarkar.arundaadc712015-02-26 05:39:081167 command_updater_.UpdateCommandEnabled(IDC_ZOOM_NORMAL,
ccameronb7c1d6c2015-03-09 17:08:241168 CanResetZoom(contents));
Aran Gilmand217b5b2019-04-11 17:45:431169 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MINUS, CanZoomOut(contents));
[email protected]5d98294912012-06-27 22:57:401170}
1171
1172void BrowserCommandController::UpdateCommandsForContentRestrictionState() {
1173 int restrictions = GetContentRestrictions(browser_);
1174
1175 command_updater_.UpdateCommandEnabled(
[email protected]3c71576ce2013-07-23 02:00:011176 IDC_COPY, !(restrictions & CONTENT_RESTRICTION_COPY));
[email protected]5d98294912012-06-27 22:57:401177 command_updater_.UpdateCommandEnabled(
[email protected]3c71576ce2013-07-23 02:00:011178 IDC_CUT, !(restrictions & CONTENT_RESTRICTION_CUT));
[email protected]5d98294912012-06-27 22:57:401179 command_updater_.UpdateCommandEnabled(
[email protected]3c71576ce2013-07-23 02:00:011180 IDC_PASTE, !(restrictions & CONTENT_RESTRICTION_PASTE));
[email protected]5d98294912012-06-27 22:57:401181 UpdateSaveAsState();
1182 UpdatePrintingState();
1183}
1184
1185void BrowserCommandController::UpdateCommandsForDevTools() {
Ivan Sandrk9669d0e2017-12-15 23:50:201186 if (is_locked_fullscreen_)
1187 return;
1188
Pavol Marko56df0272018-07-04 17:55:041189 bool dev_tools_enabled = DevToolsWindow::AllowDevToolsFor(
1190 profile(), browser_->tab_strip_model()->GetActiveWebContents());
Aran Gilmand217b5b2019-04-11 17:45:431191 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS, dev_tools_enabled);
[email protected]5d98294912012-06-27 22:57:401192 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_CONSOLE,
1193 dev_tools_enabled);
[email protected]2056c3192013-10-21 22:40:511194 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_DEVICES,
1195 dev_tools_enabled);
[email protected]5d98294912012-06-27 22:57:401196 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_INSPECT,
1197 dev_tools_enabled);
[email protected]d16657c2012-09-03 14:25:101198 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_TOGGLE,
1199 dev_tools_enabled);
spqchan2c5d541e2017-10-25 07:07:111200#if defined(OS_MACOSX)
1201 command_updater_.UpdateCommandEnabled(IDC_TOGGLE_JAVASCRIPT_APPLE_EVENTS,
1202 dev_tools_enabled);
1203#endif
[email protected]5d98294912012-06-27 22:57:401204}
1205
1206void BrowserCommandController::UpdateCommandsForBookmarkEditing() {
Ivan Sandrk9669d0e2017-12-15 23:50:201207 if (is_locked_fullscreen_)
1208 return;
1209
Peter Kasting32bb2602019-08-22 19:01:431210 command_updater_.UpdateCommandEnabled(IDC_BOOKMARK_THIS_TAB,
1211 CanBookmarkCurrentTab(browser_));
[email protected]5d98294912012-06-27 22:57:401212 command_updater_.UpdateCommandEnabled(IDC_BOOKMARK_ALL_TABS,
1213 CanBookmarkAllTabs(browser_));
thestig5149d272014-10-30 07:25:291214#if defined(OS_WIN)
1215 command_updater_.UpdateCommandEnabled(IDC_PIN_TO_START_SCREEN, true);
1216#endif
[email protected]5d98294912012-06-27 22:57:401217}
1218
1219void BrowserCommandController::UpdateCommandsForBookmarkBar() {
Ivan Sandrk9669d0e2017-12-15 23:50:201220 if (is_locked_fullscreen_)
1221 return;
1222
tfarina3bddbe112014-08-28 05:29:321223 command_updater_.UpdateCommandEnabled(
Aran Gilmand217b5b2019-04-11 17:45:431224 IDC_SHOW_BOOKMARK_BAR, browser_defaults::bookmarks_enabled &&
1225 !profile()->IsGuestSession() &&
1226 !profile()->IsSystemProfile() &&
1227 !profile()->GetPrefs()->IsManagedPreference(
1228 bookmarks::prefs::kShowBookmarkBar) &&
1229 IsShowingMainUI());
[email protected]5d98294912012-06-27 22:57:401230}
1231
[email protected]9ec3ea5b2012-12-03 18:14:301232void BrowserCommandController::UpdateCommandsForFileSelectionDialogs() {
Ivan Sandrk9669d0e2017-12-15 23:50:201233 if (is_locked_fullscreen_)
1234 return;
1235
[email protected]9ec3ea5b2012-12-03 18:14:301236 UpdateSaveAsState();
[email protected]05454532013-01-22 21:09:081237 UpdateOpenFileState(&command_updater_);
[email protected]9ec3ea5b2012-12-03 18:14:301238}
1239
[email protected]32dfede2013-08-25 15:48:251240void BrowserCommandController::UpdateCommandsForFullscreenMode() {
Ivan Sandrk9669d0e2017-12-15 23:50:201241 if (is_locked_fullscreen_)
1242 return;
1243
zijiehe3c7af992017-02-12 20:59:401244 const bool is_fullscreen = window() && window()->IsFullscreen();
1245 const bool show_main_ui = IsShowingMainUI();
manukf1a116392018-10-25 16:28:241246 const bool show_location_bar = IsShowingLocationBar();
1247
zijiehe3c7af992017-02-12 20:59:401248 const bool main_not_fullscreen = show_main_ui && !is_fullscreen;
[email protected]5d98294912012-06-27 22:57:401249
1250 // Navigation commands
1251 command_updater_.UpdateCommandEnabled(IDC_OPEN_CURRENT_URL, show_main_ui);
1252
1253 // Window management commands
1254 command_updater_.UpdateCommandEnabled(
Joel Hockey2687ab52019-08-14 23:59:461255 IDC_SHOW_AS_TAB, !browser_->is_type_normal() && !is_fullscreen);
[email protected]5d98294912012-06-27 22:57:401256
1257 // Focus various bits of UI
1258 command_updater_.UpdateCommandEnabled(IDC_FOCUS_TOOLBAR, show_main_ui);
manukf1a116392018-10-25 16:28:241259 command_updater_.UpdateCommandEnabled(IDC_FOCUS_LOCATION, show_location_bar);
[email protected]5d98294912012-06-27 22:57:401260 command_updater_.UpdateCommandEnabled(IDC_FOCUS_SEARCH, show_main_ui);
Aran Gilmand217b5b2019-04-11 17:45:431261 command_updater_.UpdateCommandEnabled(IDC_FOCUS_MENU_BAR,
1262 main_not_fullscreen);
1263 command_updater_.UpdateCommandEnabled(IDC_FOCUS_NEXT_PANE,
1264 main_not_fullscreen);
1265 command_updater_.UpdateCommandEnabled(IDC_FOCUS_PREVIOUS_PANE,
1266 main_not_fullscreen);
1267 command_updater_.UpdateCommandEnabled(IDC_FOCUS_BOOKMARKS,
1268 main_not_fullscreen);
[email protected]822ca8c62013-04-19 00:49:151269 command_updater_.UpdateCommandEnabled(
David Tsengc0b1b642018-01-24 07:12:271270 IDC_FOCUS_INACTIVE_POPUP_FOR_ACCESSIBILITY, main_not_fullscreen);
[email protected]5d98294912012-06-27 22:57:401271
1272 // Show various bits of UI
1273 command_updater_.UpdateCommandEnabled(IDC_DEVELOPER_MENU, show_main_ui);
Nico Weber0cc71122019-07-29 17:30:401274#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
Fabio Rocha19bf4262019-04-04 01:05:181275 command_updater_.UpdateCommandEnabled(
Joel Hockey2687ab52019-08-14 23:59:461276 IDC_FEEDBACK, show_main_ui || browser_->is_type_devtools());
[email protected]236ad3022013-09-04 03:27:431277#endif
Mihai Sardarescu2ec5205682018-09-26 09:20:051278 UpdateShowSyncState(show_main_ui);
[email protected]5d98294912012-06-27 22:57:401279
[email protected]5d98294912012-06-27 22:57:401280 command_updater_.UpdateCommandEnabled(IDC_EDIT_SEARCH_ENGINES, show_main_ui);
1281 command_updater_.UpdateCommandEnabled(IDC_VIEW_PASSWORDS, show_main_ui);
1282 command_updater_.UpdateCommandEnabled(IDC_ABOUT, show_main_ui);
1283 command_updater_.UpdateCommandEnabled(IDC_SHOW_APP_MENU, show_main_ui);
Tina Wangf09681ac2019-03-27 20:52:101284 command_updater_.UpdateCommandEnabled(IDC_SEND_TAB_TO_SELF, show_main_ui);
Tina Wangfa016992019-05-31 00:01:151285 command_updater_.UpdateCommandEnabled(IDC_SEND_TAB_TO_SELF_SINGLE_TARGET,
1286 show_main_ui);
Yann Dago70ca3dd2019-02-27 01:12:351287 command_updater_.UpdateCommandEnabled(IDC_SHOW_MANAGEMENT_PAGE, true);
primianod3a81ab2016-01-25 22:21:151288
1289 if (base::debug::IsProfilingSupported())
1290 command_updater_.UpdateCommandEnabled(IDC_PROFILING_ENABLED, show_main_ui);
[email protected]5d98294912012-06-27 22:57:401291
[email protected]aeafc3852014-04-29 16:51:291292#if !defined(OS_MACOSX)
zijiehe3c7af992017-02-12 20:59:401293 // Disable toggling into fullscreen mode if disallowed by pref.
Aran Gilmand217b5b2019-04-11 17:45:431294 const bool fullscreen_enabled =
1295 is_fullscreen ||
zijiehe3c7af992017-02-12 20:59:401296 profile()->GetPrefs()->GetBoolean(prefs::kFullscreenAllowed);
1297#else
1298 const bool fullscreen_enabled = true;
[email protected]00a1cc5b2012-11-07 13:44:511299#endif
1300
1301 command_updater_.UpdateCommandEnabled(IDC_FULLSCREEN, fullscreen_enabled);
spqchanb8ffc7d2015-11-17 01:17:211302 command_updater_.UpdateCommandEnabled(IDC_TOGGLE_FULLSCREEN_TOOLBAR,
1303 fullscreen_enabled);
[email protected]5d98294912012-06-27 22:57:401304
1305 UpdateCommandsForBookmarkBar();
zijiehedb473d552017-02-24 01:13:411306 UpdateCommandsForIncognitoAvailability();
Alan Cutter167dfe82018-04-11 09:06:001307 UpdateCommandsForHostedAppAvailability();
1308}
1309
1310void BrowserCommandController::UpdateCommandsForHostedAppAvailability() {
1311 bool has_toolbar =
Joel Hockey2687ab52019-08-14 23:59:461312 browser_->is_type_normal() ||
Alan Cutterd0bd51902019-05-22 04:15:231313 web_app::AppBrowserController::IsForWebAppBrowser(browser_);
Alan Cutter167dfe82018-04-11 09:06:001314 if (window() && window()->ShouldHideUIForFullscreen())
1315 has_toolbar = false;
1316 command_updater_.UpdateCommandEnabled(IDC_FOCUS_TOOLBAR, has_toolbar);
1317 command_updater_.UpdateCommandEnabled(IDC_FOCUS_NEXT_PANE, has_toolbar);
1318 command_updater_.UpdateCommandEnabled(IDC_FOCUS_PREVIOUS_PANE, has_toolbar);
1319 command_updater_.UpdateCommandEnabled(IDC_SHOW_APP_MENU, has_toolbar);
[email protected]5d98294912012-06-27 22:57:401320}
1321
Ivan Sandrk9669d0e2017-12-15 23:50:201322#if defined(OS_CHROMEOS)
1323namespace {
1324
1325#if DCHECK_IS_ON()
1326// Makes sure that all commands that are not whitelisted are disabled. DCHECKs
1327// otherwise. Compiled only in debug mode.
1328void NonWhitelistedCommandsAreDisabled(CommandUpdaterImpl* command_updater) {
Ivan Sandrk6ac9c9a2018-12-14 14:15:071329 constexpr int kWhitelistedIds[] = {IDC_CUT, IDC_COPY, IDC_PASTE};
Ivan Sandrk9669d0e2017-12-15 23:50:201330
1331 // Go through all the command ids, skip the whitelisted ones.
1332 for (int id : command_updater->GetAllIds()) {
Jan Wilken Dörriea8cb56302019-06-06 18:59:361333 if (base::Contains(kWhitelistedIds, id)) {
Ivan Sandrk9669d0e2017-12-15 23:50:201334 continue;
1335 }
1336 DCHECK(!command_updater->IsCommandEnabled(id));
1337 }
1338}
1339#endif
1340
1341} // namespace
1342
1343void BrowserCommandController::UpdateCommandsForLockedFullscreenMode() {
Ivan Sandrke0e92452019-03-21 09:32:481344 bool is_locked_fullscreen =
1345 platform_util::IsBrowserLockedFullscreen(browser_);
Ivan Sandrk9669d0e2017-12-15 23:50:201346 // Sanity check to make sure this function is called only on state change.
1347 DCHECK_NE(is_locked_fullscreen, is_locked_fullscreen_);
1348 if (is_locked_fullscreen == is_locked_fullscreen_)
1349 return;
1350 is_locked_fullscreen_ = is_locked_fullscreen;
1351
1352 if (is_locked_fullscreen_) {
1353 command_updater_.DisableAllCommands();
1354 // Update the state of whitelisted commands:
1355 // IDC_CUT/IDC_COPY/IDC_PASTE,
1356 UpdateCommandsForContentRestrictionState();
Ivan Sandrk6ac9c9a2018-12-14 14:15:071357 // TODO(crbug.com/904637): Re-enable Find and Zoom in locked fullscreen.
Ivan Sandrk9669d0e2017-12-15 23:50:201358 // All other commands will be disabled (there is an early return in their
1359 // corresponding UpdateCommandsFor* functions).
1360#if DCHECK_IS_ON()
1361 NonWhitelistedCommandsAreDisabled(&command_updater_);
1362#endif
1363 } else {
1364 // Do an init call to re-initialize command state after the
1365 // DisableAllCommands.
1366 InitCommandState();
1367 }
1368}
1369#endif
1370
[email protected]5d98294912012-06-27 22:57:401371void BrowserCommandController::UpdatePrintingState() {
Ivan Sandrk9669d0e2017-12-15 23:50:201372 if (is_locked_fullscreen_)
1373 return;
1374
[email protected]d53e4032012-06-29 18:58:341375 bool print_enabled = CanPrint(browser_);
1376 command_updater_.UpdateCommandEnabled(IDC_PRINT, print_enabled);
Lei Zhang48a4a5262018-04-17 20:18:441377#if BUILDFLAG(ENABLE_PRINTING)
vitalybukaf9433e42014-09-08 10:04:551378 command_updater_.UpdateCommandEnabled(IDC_BASIC_PRINT,
1379 CanBasicPrint(browser_));
Lei Zhang48a4a5262018-04-17 20:18:441380#endif
[email protected]5d98294912012-06-27 22:57:401381}
1382
1383void BrowserCommandController::UpdateSaveAsState() {
Ivan Sandrk9669d0e2017-12-15 23:50:201384 if (is_locked_fullscreen_)
1385 return;
1386
[email protected]5d98294912012-06-27 22:57:401387 command_updater_.UpdateCommandEnabled(IDC_SAVE_PAGE, CanSavePage(browser_));
1388}
1389
Mihai Sardarescu2ec5205682018-09-26 09:20:051390void BrowserCommandController::UpdateShowSyncState(bool show_main_ui) {
1391 if (is_locked_fullscreen_)
1392 return;
1393
Mihai Sardarescuf87ccc12018-09-26 10:37:451394 command_updater_.UpdateCommandEnabled(
Mihai Sardarescu290bc882018-10-12 10:48:141395 IDC_SHOW_SIGNIN, show_main_ui && pref_signin_allowed_.GetValue());
Mihai Sardarescu2ec5205682018-09-26 09:20:051396}
1397
[email protected]05454532013-01-22 21:09:081398// static
1399void BrowserCommandController::UpdateOpenFileState(
1400 CommandUpdater* command_updater) {
[email protected]5d98294912012-06-27 22:57:401401 bool enabled = true;
1402 PrefService* local_state = g_browser_process->local_state();
1403 if (local_state)
1404 enabled = local_state->GetBoolean(prefs::kAllowFileSelectionDialogs);
1405
[email protected]05454532013-01-22 21:09:081406 command_updater->UpdateCommandEnabled(IDC_OPEN_FILE, enabled);
[email protected]5d98294912012-06-27 22:57:401407}
1408
1409void BrowserCommandController::UpdateReloadStopState(bool is_loading,
1410 bool force) {
Ivan Sandrk9669d0e2017-12-15 23:50:201411 if (is_locked_fullscreen_)
1412 return;
1413
[email protected]5d98294912012-06-27 22:57:401414 window()->UpdateReloadStopState(is_loading, force);
1415 command_updater_.UpdateCommandEnabled(IDC_STOP, is_loading);
Jeff Fisher6cc1ce7b2019-06-28 23:02:381416 UpdateCloseFindOrStop();
[email protected]5d98294912012-06-27 22:57:401417}
1418
[email protected]2e9369e2014-08-15 09:12:531419void BrowserCommandController::UpdateTabRestoreCommandState() {
Ivan Sandrk9669d0e2017-12-15 23:50:201420 if (is_locked_fullscreen_)
1421 return;
1422
blundell74001adc2015-09-18 11:04:251423 sessions::TabRestoreService* tab_restore_service =
[email protected]2e9369e2014-08-15 09:12:531424 TabRestoreServiceFactory::GetForProfile(profile());
1425 // The command is enabled if the service hasn't loaded yet to trigger loading.
1426 // The command is updated once the load completes.
1427 command_updater_.UpdateCommandEnabled(
1428 IDC_RESTORE_TAB,
Peter Kasting32bb2602019-08-22 19:01:431429 tab_restore_service && (!tab_restore_service->IsLoaded() ||
1430 !tab_restore_service->entries().empty()));
[email protected]2e9369e2014-08-15 09:12:531431}
1432
[email protected]3cac87232012-11-20 01:48:271433void BrowserCommandController::UpdateCommandsForFind() {
1434 TabStripModel* model = browser_->tab_strip_model();
Joel Hockey2687ab52019-08-14 23:59:461435 bool enabled = !model->IsTabBlocked(model->active_index()) &&
1436 !browser_->is_type_devtools();
[email protected]3cac87232012-11-20 01:48:271437
1438 command_updater_.UpdateCommandEnabled(IDC_FIND, enabled);
1439 command_updater_.UpdateCommandEnabled(IDC_FIND_NEXT, enabled);
1440 command_updater_.UpdateCommandEnabled(IDC_FIND_PREVIOUS, enabled);
1441}
1442
Jeff Fisher6cc1ce7b2019-06-28 23:02:381443void BrowserCommandController::UpdateCloseFindOrStop() {
1444 bool enabled = CanCloseFind(browser_) || IsCommandEnabled(IDC_STOP);
1445 command_updater_.UpdateCommandEnabled(IDC_CLOSE_FIND_OR_STOP, enabled);
1446}
1447
apacible45cbfc92015-09-28 22:45:411448void BrowserCommandController::UpdateCommandsForMediaRouter() {
Ivan Sandrk9669d0e2017-12-15 23:50:201449 if (is_locked_fullscreen_)
1450 return;
1451
apacible45cbfc92015-09-28 22:45:411452 command_updater_.UpdateCommandEnabled(IDC_ROUTE_MEDIA,
1453 CanRouteMedia(browser_));
1454}
1455
Elly Fong-Jones5e6baaf2019-10-10 23:17:141456void BrowserCommandController::UpdateCommandsForTabKeyboardFocus(
1457 base::Optional<int> target_index) {
1458 command_updater_.UpdateCommandEnabled(
1459 IDC_DUPLICATE_TARGET_TAB, !browser_->deprecated_is_app() &&
1460 target_index.has_value() &&
1461 CanDuplicateTabAt(browser_, *target_index));
1462 const bool normal_window = browser_->is_type_normal();
1463 command_updater_.UpdateCommandEnabled(
1464 IDC_MUTE_TARGET_SITE, normal_window && target_index.has_value());
1465 command_updater_.UpdateCommandEnabled(
1466 IDC_PIN_TARGET_TAB, normal_window && target_index.has_value());
1467}
1468
[email protected]409ea2972012-11-10 19:54:431469void BrowserCommandController::AddInterstitialObservers(WebContents* contents) {
[email protected]20ca0382013-02-28 19:50:071470 interstitial_observers_.push_back(new InterstitialObserver(this, contents));
[email protected]5d98294912012-06-27 22:57:401471}
1472
1473void BrowserCommandController::RemoveInterstitialObservers(
[email protected]e89cfcb2012-11-11 14:47:241474 WebContents* contents) {
[email protected]20ca0382013-02-28 19:50:071475 for (size_t i = 0; i < interstitial_observers_.size(); i++) {
1476 if (interstitial_observers_[i]->web_contents() != contents)
1477 continue;
1478
1479 delete interstitial_observers_[i];
1480 interstitial_observers_.erase(interstitial_observers_.begin() + i);
1481 return;
1482 }
[email protected]5d98294912012-06-27 22:57:401483}
1484
1485BrowserWindow* BrowserCommandController::window() {
1486 return browser_->window();
1487}
1488
1489Profile* BrowserCommandController::profile() {
1490 return browser_->profile();
1491}
1492
[email protected]5d98294912012-06-27 22:57:401493} // namespace chrome