blob: cc19ed917cb9ac4ca1cf9b6fb5d95421f4f48017 [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"
avi655876a2015-12-25 07:18:1518#include "build/build_config.h"
[email protected]5d98294912012-06-27 22:57:4019#include "chrome/app/chrome_command_ids.h"
20#include "chrome/browser/browser_process.h"
[email protected]dcc8fbc2013-07-12 00:54:0921#include "chrome/browser/chrome_notification_types.h"
[email protected]5d98294912012-06-27 22:57:4022#include "chrome/browser/defaults.h"
Pavol Marko56df0272018-07-04 17:55:0423#include "chrome/browser/devtools/devtools_window.h"
[email protected]5d98294912012-06-27 22:57:4024#include "chrome/browser/extensions/extension_service.h"
[email protected]2e9d79f2013-08-16 05:45:5625#include "chrome/browser/lifetime/application_lifetime.h"
[email protected]5d98294912012-06-27 22:57:4026#include "chrome/browser/prefs/incognito_mode_prefs.h"
[email protected]5d98294912012-06-27 22:57:4027#include "chrome/browser/profiles/profile.h"
mlermane01e6de2014-09-29 19:26:4728#include "chrome/browser/profiles/profile_manager.h"
[email protected]5d98294912012-06-27 22:57:4029#include "chrome/browser/sessions/tab_restore_service_factory.h"
[email protected]0b32f9b62012-09-17 19:08:0330#include "chrome/browser/shell_integration.h"
[email protected]3d27d272013-07-31 03:15:1631#include "chrome/browser/signin/signin_promo.h"
[email protected]5d98294912012-06-27 22:57:4032#include "chrome/browser/sync/profile_sync_service_factory.h"
Christopher Lam86b52712017-12-04 01:58:3333#include "chrome/browser/ui/apps/app_info_dialog.h"
[email protected]5d98294912012-06-27 22:57:4034#include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
35#include "chrome/browser/ui/browser.h"
36#include "chrome/browser/ui/browser_commands.h"
Dana Fried08b774a2019-05-10 18:21:4837#include "chrome/browser/ui/browser_finder.h"
[email protected]5d98294912012-06-27 22:57:4038#include "chrome/browser/ui/browser_window.h"
39#include "chrome/browser/ui/chrome_pages.h"
Alan Cutter09965802018-03-27 07:25:2940#include "chrome/browser/ui/extensions/application_launch.h"
Christopher Lam0dbac2b2017-11-14 07:12:1041#include "chrome/browser/ui/extensions/hosted_app_browser_controller.h"
Alan Cutter0c3132302018-02-21 05:09:0242#include "chrome/browser/ui/page_info/page_info_dialog.h"
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:2743#include "chrome/browser/ui/singleton_tabs.h"
[email protected]5d98294912012-06-27 22:57:4044#include "chrome/browser/ui/tabs/tab_strip_model.h"
Eric Willigers4a5f7a92019-05-10 19:19:2645#include "chrome/browser/ui/web_applications/app_browser_controller.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"
sdefresne0f2ef352015-07-27 19:18:0051#include "components/dom_distiller/core/dom_distiller_switches.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"
David Rogera6c88122017-10-25 13:02:4655#include "components/signin/core/browser/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
Bettina Deaf3dac202017-09-16 20:53:2288#if BUILDFLAG(ENABLE_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.
215 if (browser_->is_app())
216 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
wittman76df71db32014-12-18 23:26:58301void BrowserCommandController::ExtensionStateChanged() {
302 // Extensions may disable the bookmark editing commands.
303 UpdateCommandsForBookmarkEditing();
304}
305
[email protected]5d98294912012-06-27 22:57:40306////////////////////////////////////////////////////////////////////////////////
Ivan Sandrk9669d0e2017-12-15 23:50:20307// BrowserCommandController, CommandUpdater implementation:
[email protected]5d98294912012-06-27 22:57:40308
Ivan Sandrk9669d0e2017-12-15 23:50:20309bool BrowserCommandController::SupportsCommand(int id) const {
310 return command_updater_.SupportsCommand(id);
311}
312
313bool BrowserCommandController::IsCommandEnabled(int id) const {
314 return command_updater_.IsCommandEnabled(id);
315}
316
Edwin Joe6f6fc1e2019-02-27 20:00:37317bool BrowserCommandController::ExecuteCommand(int id,
318 base::TimeTicks time_stamp) {
319 return ExecuteCommandWithDisposition(id, WindowOpenDisposition::CURRENT_TAB,
320 time_stamp);
Ivan Sandrk9669d0e2017-12-15 23:50:20321}
322
323bool BrowserCommandController::ExecuteCommandWithDisposition(
Edwin Joe6f6fc1e2019-02-27 20:00:37324 int id,
325 WindowOpenDisposition disposition,
326 base::TimeTicks time_stamp) {
Ivan Sandrk9669d0e2017-12-15 23:50:20327 // Doesn't go through the command_updater_ to avoid dealing with having a
328 // naming collision for ExecuteCommandWithDisposition (both
329 // CommandUpdaterDelegate and CommandUpdater declare this function so we
330 // choose to not implement CommandUpdaterDelegate inside this class and
331 // therefore command_updater_ doesn't have the delegate set).
332 if (!SupportsCommand(id) || !IsCommandEnabled(id))
333 return false;
334
[email protected]5d98294912012-06-27 22:57:40335 // No commands are enabled if there is not yet any selected tab.
336 // TODO(pkasting): It seems like we should not need this, because either
337 // most/all commands should not have been enabled yet anyway or the ones that
338 // are enabled should be global, or safe themselves against having no selected
339 // tab. However, Ben says he tried removing this before and got lots of
340 // crashes, e.g. from Windows sending WM_COMMANDs at random times during
341 // window construction. This probably could use closer examination someday.
[email protected]59253a652012-11-20 00:17:26342 if (browser_->tab_strip_model()->active_index() == TabStripModel::kNoTab)
Ivan Sandrk9669d0e2017-12-15 23:50:20343 return true;
[email protected]5d98294912012-06-27 22:57:40344
Aran Gilmand217b5b2019-04-11 17:45:43345 DCHECK(command_updater_.IsCommandEnabled(id))
346 << "Invalid/disabled command " << id;
[email protected]5d98294912012-06-27 22:57:40347
[email protected]5d98294912012-06-27 22:57:40348 // The order of commands in this switch statement must match the function
349 // declaration order in browser.h!
350 switch (id) {
351 // Navigation commands
352 case IDC_BACK:
353 GoBack(browser_, disposition);
354 break;
355 case IDC_FORWARD:
356 GoForward(browser_, disposition);
357 break;
358 case IDC_RELOAD:
359 Reload(browser_, disposition);
360 break;
[email protected]58e29032012-08-06 20:19:57361 case IDC_RELOAD_CLEARING_CACHE:
362 ClearCache(browser_);
Nico Webera745ef72018-01-29 23:45:57363 FALLTHROUGH;
toyoshim7dad4b1182016-04-01 14:28:05364 case IDC_RELOAD_BYPASSING_CACHE:
365 ReloadBypassingCache(browser_, disposition);
[email protected]5d98294912012-06-27 22:57:40366 break;
367 case IDC_HOME:
368 Home(browser_, disposition);
369 break;
370 case IDC_OPEN_CURRENT_URL:
371 OpenCurrentURL(browser_);
372 break;
373 case IDC_STOP:
374 Stop(browser_);
375 break;
376
Aran Gilmand217b5b2019-04-11 17:45:43377 // Window management commands
[email protected]5d98294912012-06-27 22:57:40378 case IDC_NEW_WINDOW:
379 NewWindow(browser_);
380 break;
381 case IDC_NEW_INCOGNITO_WINDOW:
Orin Jaworski5e4bcd0c2018-10-16 19:35:05382 NewIncognitoWindow(profile());
[email protected]5d98294912012-06-27 22:57:40383 break;
384 case IDC_CLOSE_WINDOW:
bratell0a7406f2017-03-28 07:46:37385 base::RecordAction(base::UserMetricsAction("CloseWindowByKey"));
[email protected]04b9e692012-08-24 14:49:09386 CloseWindow(browser_);
[email protected]5d98294912012-06-27 22:57:40387 break;
Bettina Dea0a4505922017-09-28 00:53:32388 case IDC_NEW_TAB: {
389 NewTab(browser_);
390#if BUILDFLAG(ENABLE_DESKTOP_IN_PRODUCT_HELP)
Catherine Chungbeb884222017-08-01 15:47:55391 // This is not in NewTab() to avoid tracking programmatic creation of new
392 // tabs by extensions.
Bettina Dea0a4505922017-09-28 00:53:32393 auto* new_tab_tracker =
394 feature_engagement::NewTabTrackerFactory::GetInstance()
395 ->GetForProfile(profile());
396
397 new_tab_tracker->OnNewTabOpened();
398 new_tab_tracker->CloseBubble();
Catherine Chungbeb884222017-08-01 15:47:55399#endif
[email protected]5d98294912012-06-27 22:57:40400 break;
Bettina Dea0a4505922017-09-28 00:53:32401 }
[email protected]5d98294912012-06-27 22:57:40402 case IDC_CLOSE_TAB:
bratell0a7406f2017-03-28 07:46:37403 base::RecordAction(base::UserMetricsAction("CloseTabByKey"));
[email protected]04b9e692012-08-24 14:49:09404 CloseTab(browser_);
[email protected]5d98294912012-06-27 22:57:40405 break;
406 case IDC_SELECT_NEXT_TAB:
bratell0a7406f2017-03-28 07:46:37407 base::RecordAction(base::UserMetricsAction("Accel_SelectNextTab"));
Edwin Joe6f6fc1e2019-02-27 20:00:37408 SelectNextTab(browser_,
409 {TabStripModel::GestureType::kKeyboard, time_stamp});
[email protected]5d98294912012-06-27 22:57:40410 break;
411 case IDC_SELECT_PREVIOUS_TAB:
bratell0a7406f2017-03-28 07:46:37412 base::RecordAction(base::UserMetricsAction("Accel_SelectPreviousTab"));
Edwin Joe6f6fc1e2019-02-27 20:00:37413 SelectPreviousTab(browser_,
414 {TabStripModel::GestureType::kKeyboard, time_stamp});
[email protected]5d98294912012-06-27 22:57:40415 break;
[email protected]5d98294912012-06-27 22:57:40416 case IDC_MOVE_TAB_NEXT:
417 MoveTabNext(browser_);
418 break;
419 case IDC_MOVE_TAB_PREVIOUS:
420 MoveTabPrevious(browser_);
421 break;
422 case IDC_SELECT_TAB_0:
423 case IDC_SELECT_TAB_1:
424 case IDC_SELECT_TAB_2:
425 case IDC_SELECT_TAB_3:
426 case IDC_SELECT_TAB_4:
427 case IDC_SELECT_TAB_5:
428 case IDC_SELECT_TAB_6:
429 case IDC_SELECT_TAB_7:
bratell0a7406f2017-03-28 07:46:37430 base::RecordAction(base::UserMetricsAction("Accel_SelectNumberedTab"));
Edwin Joe6f6fc1e2019-02-27 20:00:37431 SelectNumberedTab(browser_, id - IDC_SELECT_TAB_0,
432 {TabStripModel::GestureType::kKeyboard, time_stamp});
[email protected]5d98294912012-06-27 22:57:40433 break;
434 case IDC_SELECT_LAST_TAB:
bratell0a7406f2017-03-28 07:46:37435 base::RecordAction(base::UserMetricsAction("Accel_SelectNumberedTab"));
Edwin Joe6f6fc1e2019-02-27 20:00:37436 SelectLastTab(browser_,
437 {TabStripModel::GestureType::kKeyboard, time_stamp});
[email protected]5d98294912012-06-27 22:57:40438 break;
439 case IDC_DUPLICATE_TAB:
440 DuplicateTab(browser_);
441 break;
442 case IDC_RESTORE_TAB:
443 RestoreTab(browser_);
Collin Baker9bbe1d82019-03-08 01:16:27444 browser_->window()->OnTabRestored(IDC_RESTORE_TAB);
[email protected]5d98294912012-06-27 22:57:40445 break;
[email protected]5d98294912012-06-27 22:57:40446 case IDC_SHOW_AS_TAB:
447 ConvertPopupToTabbedBrowser(browser_);
448 break;
449 case IDC_FULLSCREEN:
[email protected]3f32b9b2012-07-09 16:59:28450 chrome::ToggleFullscreenMode(browser_);
[email protected]5d98294912012-06-27 22:57:40451 break;
Alan Cutter09965802018-03-27 07:25:29452 case IDC_OPEN_IN_PWA_WINDOW:
453 base::RecordAction(base::UserMetricsAction("OpenActiveTabInPwaWindow"));
454 ReparentSecureActiveTabIntoPwaWindow(browser_);
455 break;
[email protected]770c6d82012-09-06 22:21:32456
[email protected]d12cc5e2013-10-19 18:25:06457#if defined(OS_CHROMEOS)
458 case IDC_VISIT_DESKTOP_OF_LRU_USER_2:
[email protected]0c930812014-01-30 18:01:47459 case IDC_VISIT_DESKTOP_OF_LRU_USER_3:
zijiehe4dde8072017-02-13 20:38:35460 ExecuteVisitDesktopCommand(id, window()->GetNativeWindow());
[email protected]0c930812014-01-30 18:01:47461 break;
[email protected]d12cc5e2013-10-19 18:25:06462#endif
463
[email protected]893124a22014-04-15 00:45:28464#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
Tom Andersonf15ede502017-11-10 03:17:15465 case IDC_MINIMIZE_WINDOW:
466 browser_->window()->Minimize();
467 break;
468 case IDC_MAXIMIZE_WINDOW:
469 browser_->window()->Maximize();
470 break;
471 case IDC_RESTORE_WINDOW:
472 browser_->window()->Restore();
473 break;
[email protected]af97be4c62014-02-13 14:43:34474 case IDC_USE_SYSTEM_TITLE_BAR: {
zijiehe4dde8072017-02-13 20:38:35475 PrefService* prefs = profile()->GetPrefs();
[email protected]af97be4c62014-02-13 14:43:34476 prefs->SetBoolean(prefs::kUseCustomChromeFrame,
477 !prefs->GetBoolean(prefs::kUseCustomChromeFrame));
478 break;
479 }
480#endif
481
[email protected]5d98294912012-06-27 22:57:40482#if defined(OS_MACOSX)
spqchanb8ffc7d2015-11-17 01:17:21483 case IDC_TOGGLE_FULLSCREEN_TOOLBAR:
484 chrome::ToggleFullscreenToolbar(browser_);
485 break;
spqchan2c5d541e2017-10-25 07:07:11486 case IDC_TOGGLE_JAVASCRIPT_APPLE_EVENTS: {
Robert Sesek03282612018-10-31 15:48:06487 chrome::ToggleJavaScriptFromAppleEventsAllowed(browser_);
spqchan2c5d541e2017-10-25 07:07:11488 break;
489 }
[email protected]5d98294912012-06-27 22:57:40490#endif
491 case IDC_EXIT:
492 Exit();
493 break;
494
495 // Page-related commands
496 case IDC_SAVE_PAGE:
497 SavePage(browser_);
498 break;
499 case IDC_BOOKMARK_PAGE:
Anatoly Pilikovee720922017-11-01 09:43:17500#if BUILDFLAG(ENABLE_DESKTOP_IN_PRODUCT_HELP)
Bettina Dea48125dc2017-09-06 03:23:14501 feature_engagement::BookmarkTrackerFactory::GetInstance()
502 ->GetForProfile(profile())
503 ->OnBookmarkAdded();
504#endif
deepak.m154a7f392014-12-15 04:41:43505 BookmarkCurrentPageAllowingExtensionOverrides(browser_);
[email protected]5d98294912012-06-27 22:57:40506 break;
[email protected]5d98294912012-06-27 22:57:40507 case IDC_BOOKMARK_ALL_TABS:
Anatoly Pilikovee720922017-11-01 09:43:17508#if BUILDFLAG(ENABLE_DESKTOP_IN_PRODUCT_HELP)
Bettina Dea48125dc2017-09-06 03:23:14509 feature_engagement::BookmarkTrackerFactory::GetInstance()
510 ->GetForProfile(profile())
511 ->OnBookmarkAdded();
512#endif
[email protected]5d98294912012-06-27 22:57:40513 BookmarkAllTabs(browser_);
514 break;
515 case IDC_VIEW_SOURCE:
Lukasz Anforowicze1b954d92017-10-30 21:28:06516 browser_->tab_strip_model()
517 ->GetActiveWebContents()
518 ->GetMainFrame()
519 ->ViewSource();
[email protected]5d98294912012-06-27 22:57:40520 break;
521 case IDC_EMAIL_PAGE_LOCATION:
522 EmailPageLocation(browser_);
523 break;
524 case IDC_PRINT:
525 Print(browser_);
526 break;
bondd052b5f82015-10-28 22:39:32527
Lei Zhang48a4a5262018-04-17 20:18:44528#if BUILDFLAG(ENABLE_PRINTING)
vitalybukaf9433e42014-09-08 10:04:55529 case IDC_BASIC_PRINT:
bratell0a7406f2017-03-28 07:46:37530 base::RecordAction(base::UserMetricsAction("Accel_Advanced_Print"));
vitalybukaf9433e42014-09-08 10:04:55531 BasicPrint(browser_);
[email protected]5d98294912012-06-27 22:57:40532 break;
Lei Zhang48a4a5262018-04-17 20:18:44533#endif // ENABLE_PRINTING
bondd052b5f82015-10-28 22:39:32534
bondd052b5f82015-10-28 22:39:32535 case IDC_SAVE_CREDIT_CARD_FOR_PAGE:
536 SaveCreditCard(browser_);
537 break;
siyua3e599eaa2018-07-13 00:23:06538 case IDC_MIGRATE_LOCAL_CREDIT_CARD_FOR_PAGE:
539 MigrateLocalCards(browser_);
540 break;
[email protected]e625b7602013-10-28 09:24:56541 case IDC_TRANSLATE_PAGE:
542 Translate(browser_);
543 break;
[email protected]4bee4432014-05-05 13:11:41544 case IDC_MANAGE_PASSWORDS_FOR_PAGE:
545 ManagePasswordsForPage(browser_);
546 break;
Tina Wang4c324702019-04-25 18:35:15547 case IDC_SEND_TAB_TO_SELF:
Tina Wang0e7e6e92019-05-14 21:03:03548 SendTabToSelfFromPageAction(browser_);
Tina Wang4c324702019-04-25 18:35:15549 break;
[email protected]4bee4432014-05-05 13:11:41550
[email protected]5d98294912012-06-27 22:57:40551 // Clipboard commands
552 case IDC_CUT:
[email protected]5d98294912012-06-27 22:57:40553 case IDC_COPY:
[email protected]5d98294912012-06-27 22:57:40554 case IDC_PASTE:
pkastingcd3f08bce2015-04-18 13:37:12555 CutCopyPaste(browser_, id);
[email protected]5d98294912012-06-27 22:57:40556 break;
557
558 // Find-in-page
559 case IDC_FIND:
560 Find(browser_);
561 break;
562 case IDC_FIND_NEXT:
563 FindNext(browser_);
564 break;
565 case IDC_FIND_PREVIOUS:
566 FindPrevious(browser_);
567 break;
568
569 // Zoom
570 case IDC_ZOOM_PLUS:
571 Zoom(browser_, content::PAGE_ZOOM_IN);
572 break;
573 case IDC_ZOOM_NORMAL:
574 Zoom(browser_, content::PAGE_ZOOM_RESET);
575 break;
576 case IDC_ZOOM_MINUS:
577 Zoom(browser_, content::PAGE_ZOOM_OUT);
578 break;
579
580 // Focus various bits of UI
581 case IDC_FOCUS_TOOLBAR:
bratell0a7406f2017-03-28 07:46:37582 base::RecordAction(base::UserMetricsAction("Accel_Focus_Toolbar"));
[email protected]5d98294912012-06-27 22:57:40583 FocusToolbar(browser_);
584 break;
585 case IDC_FOCUS_LOCATION:
bratell0a7406f2017-03-28 07:46:37586 base::RecordAction(base::UserMetricsAction("Accel_Focus_Location"));
[email protected]5d98294912012-06-27 22:57:40587 FocusLocationBar(browser_);
588 break;
589 case IDC_FOCUS_SEARCH:
bratell0a7406f2017-03-28 07:46:37590 base::RecordAction(base::UserMetricsAction("Accel_Focus_Search"));
[email protected]5d98294912012-06-27 22:57:40591 FocusSearch(browser_);
592 break;
593 case IDC_FOCUS_MENU_BAR:
594 FocusAppMenu(browser_);
595 break;
596 case IDC_FOCUS_BOOKMARKS:
bratell0a7406f2017-03-28 07:46:37597 base::RecordAction(base::UserMetricsAction("Accel_Focus_Bookmarks"));
[email protected]5d98294912012-06-27 22:57:40598 FocusBookmarksToolbar(browser_);
599 break;
David Tsengc0b1b642018-01-24 07:12:27600 case IDC_FOCUS_INACTIVE_POPUP_FOR_ACCESSIBILITY:
601 FocusInactivePopupForAccessibility(browser_);
[email protected]822ca8c62013-04-19 00:49:15602 break;
[email protected]5d98294912012-06-27 22:57:40603 case IDC_FOCUS_NEXT_PANE:
604 FocusNextPane(browser_);
605 break;
606 case IDC_FOCUS_PREVIOUS_PANE:
607 FocusPreviousPane(browser_);
608 break;
609
610 // Show various bits of UI
611 case IDC_OPEN_FILE:
612 browser_->OpenFile();
613 break;
Alan Cutter81ac726682018-09-25 00:18:36614 case IDC_CREATE_SHORTCUT:
615 CreateBookmarkAppFromCurrentWebContents(browser_,
616 true /* force_shortcut_app */);
617 break;
618 case IDC_INSTALL_PWA:
619 CreateBookmarkAppFromCurrentWebContents(browser_,
620 false /* force_shortcut_app */);
[email protected]488e3952013-11-18 05:29:14621 break;
[email protected]5d98294912012-06-27 22:57:40622 case IDC_DEV_TOOLS:
[email protected]c934c382013-11-01 00:36:01623 ToggleDevToolsWindow(browser_, DevToolsToggleAction::Show());
[email protected]5d98294912012-06-27 22:57:40624 break;
625 case IDC_DEV_TOOLS_CONSOLE:
einbinderdfa567b2016-12-16 01:15:52626 ToggleDevToolsWindow(browser_, DevToolsToggleAction::ShowConsolePanel());
[email protected]5d98294912012-06-27 22:57:40627 break;
[email protected]2056c3192013-10-21 22:40:51628 case IDC_DEV_TOOLS_DEVICES:
629 InspectUI::InspectDevices(browser_);
630 break;
[email protected]5d98294912012-06-27 22:57:40631 case IDC_DEV_TOOLS_INSPECT:
[email protected]c934c382013-11-01 00:36:01632 ToggleDevToolsWindow(browser_, DevToolsToggleAction::Inspect());
[email protected]5d98294912012-06-27 22:57:40633 break;
[email protected]d16657c2012-09-03 14:25:10634 case IDC_DEV_TOOLS_TOGGLE:
[email protected]c934c382013-11-01 00:36:01635 ToggleDevToolsWindow(browser_, DevToolsToggleAction::Toggle());
[email protected]d16657c2012-09-03 14:25:10636 break;
[email protected]5d98294912012-06-27 22:57:40637 case IDC_TASK_MANAGER:
[email protected]29c262de2013-06-22 15:39:38638 OpenTaskManager(browser_);
[email protected]5d98294912012-06-27 22:57:40639 break;
caelyn4e4e08a2015-02-04 21:27:49640#if defined(OS_CHROMEOS)
641 case IDC_TAKE_SCREENSHOT:
642 TakeScreenshot();
643 break;
644#endif
[email protected]236ad3022013-09-04 03:27:43645#if defined(GOOGLE_CHROME_BUILD)
[email protected]5d98294912012-06-27 22:57:40646 case IDC_FEEDBACK:
afakhryf4575bd2017-04-28 02:21:04647 OpenFeedbackDialog(browser_, kFeedbackSourceBrowserCommand);
[email protected]5d98294912012-06-27 22:57:40648 break;
[email protected]236ad3022013-09-04 03:27:43649#endif
[email protected]5d98294912012-06-27 22:57:40650 case IDC_SHOW_BOOKMARK_BAR:
651 ToggleBookmarkBar(browser_);
652 break;
653 case IDC_PROFILING_ENABLED:
Bryce Thomas96b07772018-11-07 03:04:15654 content::Profiling::Toggle();
[email protected]5d98294912012-06-27 22:57:40655 break;
656
657 case IDC_SHOW_BOOKMARK_MANAGER:
658 ShowBookmarkManager(browser_);
659 break;
660 case IDC_SHOW_APP_MENU:
bratell0a7406f2017-03-28 07:46:37661 base::RecordAction(base::UserMetricsAction("Accel_Show_App_Menu"));
[email protected]5d98294912012-06-27 22:57:40662 ShowAppMenu(browser_);
663 break;
664 case IDC_SHOW_AVATAR_MENU:
665 ShowAvatarMenu(browser_);
666 break;
667 case IDC_SHOW_HISTORY:
668 ShowHistory(browser_);
669 break;
670 case IDC_SHOW_DOWNLOADS:
671 ShowDownloads(browser_);
672 break;
673 case IDC_MANAGE_EXTENSIONS:
[email protected]bc9833c32013-02-28 04:05:08674 ShowExtensions(browser_, std::string());
[email protected]5d98294912012-06-27 22:57:40675 break;
676 case IDC_OPTIONS:
677 ShowSettings(browser_);
678 break;
679 case IDC_EDIT_SEARCH_ENGINES:
680 ShowSearchEngineSettings(browser_);
681 break;
682 case IDC_VIEW_PASSWORDS:
683 ShowPasswordManager(browser_);
684 break;
685 case IDC_CLEAR_BROWSING_DATA:
686 ShowClearBrowsingDataDialog(browser_);
687 break;
688 case IDC_IMPORT_SETTINGS:
689 ShowImportDialog(browser_);
690 break;
[email protected]9b7ab882012-09-10 23:46:36691 case IDC_TOGGLE_REQUEST_TABLET_SITE:
692 ToggleRequestTabletSite(browser_);
693 break;
[email protected]5d98294912012-06-27 22:57:40694 case IDC_ABOUT:
695 ShowAboutChrome(browser_);
696 break;
697 case IDC_UPGRADE_DIALOG:
698 OpenUpdateChromeDialog(browser_);
699 break;
[email protected]5d98294912012-06-27 22:57:40700 case IDC_HELP_PAGE_VIA_KEYBOARD:
701 ShowHelp(browser_, HELP_SOURCE_KEYBOARD);
702 break;
703 case IDC_HELP_PAGE_VIA_MENU:
704 ShowHelp(browser_, HELP_SOURCE_MENU);
705 break;
Bret Sepulveda2d018662017-05-18 21:31:48706 case IDC_SHOW_BETA_FORUM:
707 ShowBetaForum(browser_);
708 break;
David Roger7e25dff2019-02-07 09:03:17709#if !defined(OS_CHROMEOS)
estade8c0780f2015-08-21 23:36:41710 case IDC_SHOW_SIGNIN:
gogerald71bf6c902015-12-08 00:49:37711 ShowBrowserSigninOrSettings(
712 browser_, signin_metrics::AccessPoint::ACCESS_POINT_MENU);
estade8c0780f2015-08-21 23:36:41713 break;
David Roger7e25dff2019-02-07 09:03:17714#endif
[email protected]6bd370b2014-05-28 14:19:47715 case IDC_DISTILL_PAGE:
716 DistillCurrentPage(browser_);
717 break;
apacible45cbfc92015-09-28 22:45:41718 case IDC_ROUTE_MEDIA:
719 RouteMedia(browser_);
720 break;
Tommy Steimelc4477982017-11-29 18:07:18721 case IDC_WINDOW_MUTE_SITE:
722 MuteSite(browser_);
ellyjones0101ba02017-05-19 15:50:26723 break;
724 case IDC_WINDOW_PIN_TAB:
725 PinTab(browser_);
726 break;
Yann Dagocd13225a2019-03-07 18:39:05727 case IDC_SHOW_MANAGEMENT_PAGE: {
Yann Dagoc1d1c4af2019-04-17 13:40:51728 ShowSingletonTab(browser_, GURL(kChromeUIManagementURL));
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:27729 break;
Yann Dagocd13225a2019-03-07 18:39:05730 }
Christopher Lam0dbac2b2017-11-14 07:12:10731 // Hosted App commands
732 case IDC_COPY_URL:
733 CopyURL(browser_);
734 break;
735 case IDC_OPEN_IN_CHROME:
736 OpenInChrome(browser_);
737 break;
738 case IDC_SITE_SETTINGS:
739 ShowSiteSettings(
740 browser_,
741 browser_->tab_strip_model()->GetActiveWebContents()->GetVisibleURL());
742 break;
Dana Fried08b774a2019-05-10 18:21:48743 case IDC_HOSTED_APP_MENU_APP_INFO: {
744 content::WebContents* const web_contents =
745 browser_->tab_strip_model()->GetActiveWebContents();
746 if (web_contents) {
747 ShowPageInfoDialog(web_contents,
748 base::BindOnce(&AppInfoDialogClosedCallback,
749 base::Unretained(web_contents)),
750 bubble_anchor_util::kAppMenuButton);
751 }
Christopher Lam86b52712017-12-04 01:58:33752 break;
Dana Fried08b774a2019-05-10 18:21:48753 }
[email protected]5d98294912012-06-27 22:57:40754 default:
755 LOG(WARNING) << "Received Unimplemented Command: " << id;
756 break;
757 }
Ivan Sandrk9669d0e2017-12-15 23:50:20758
759 return true;
760}
761
762void BrowserCommandController::AddCommandObserver(int id,
763 CommandObserver* observer) {
764 command_updater_.AddCommandObserver(id, observer);
765}
766
767void BrowserCommandController::RemoveCommandObserver(
Aran Gilmand217b5b2019-04-11 17:45:43768 int id,
769 CommandObserver* observer) {
Ivan Sandrk9669d0e2017-12-15 23:50:20770 command_updater_.RemoveCommandObserver(id, observer);
771}
772
773void BrowserCommandController::RemoveCommandObserver(
774 CommandObserver* observer) {
775 command_updater_.RemoveCommandObserver(observer);
776}
777
778bool BrowserCommandController::UpdateCommandEnabled(int id, bool state) {
779 if (is_locked_fullscreen_)
780 return false;
781
782 return command_updater_.UpdateCommandEnabled(id, state);
[email protected]5d98294912012-06-27 22:57:40783}
784
Mihai Sardarescuf87ccc12018-09-26 10:37:45785////////////////////////////////////////////////////////////////////////////////
786// BrowserCommandController, SigninPrefObserver implementation:
787
788void BrowserCommandController::OnSigninAllowedPrefChange() {
789 // For unit tests, we don't have a window.
790 if (!window())
791 return;
792 UpdateShowSyncState(IsShowingMainUI());
793}
794
[email protected]5d98294912012-06-27 22:57:40795// BrowserCommandController, TabStripModelObserver implementation:
796
sangwoo.ko1ae265f12018-10-18 08:30:28797void BrowserCommandController::OnTabStripModelChanged(
798 TabStripModel* tab_strip_model,
799 const TabStripModelChange& change,
800 const TabStripSelectionChange& selection) {
Dana Frieda391e6ce2019-05-08 00:00:20801 std::vector<content::WebContents*> new_contents;
802 std::vector<content::WebContents*> old_contents;
[email protected]5d98294912012-06-27 22:57:40803
Dana Frieda391e6ce2019-05-08 00:00:20804 switch (change.type()) {
805 case TabStripModelChange::kInserted:
806 for (const auto& contents : change.GetInsert()->contents)
807 new_contents.push_back(contents.contents);
808 break;
809 case TabStripModelChange::kReplaced: {
810 auto* replace = change.GetReplace();
811 new_contents.push_back(replace->new_contents);
812 old_contents.push_back(replace->old_contents);
813 break;
sangwoo.ko1ae265f12018-10-18 08:30:28814 }
Dana Frieda391e6ce2019-05-08 00:00:20815 case TabStripModelChange::kRemoved:
816 for (const auto& contents : change.GetRemove()->contents)
817 old_contents.push_back(contents.contents);
818 break;
819 default:
820 break;
sangwoo.ko1ae265f12018-10-18 08:30:28821 }
Dana Frieda391e6ce2019-05-08 00:00:20822
823 for (auto* contents : old_contents)
824 RemoveInterstitialObservers(contents);
825 for (auto* contents : new_contents)
826 AddInterstitialObservers(contents);
[email protected]5d98294912012-06-27 22:57:40827}
828
[email protected]3cac87232012-11-20 01:48:27829void BrowserCommandController::TabBlockedStateChanged(
830 content::WebContents* contents,
831 int index) {
832 PrintingStateChanged();
833 FullscreenStateChanged();
834 UpdateCommandsForFind();
apacible45cbfc92015-09-28 22:45:41835 UpdateCommandsForMediaRouter();
[email protected]3cac87232012-11-20 01:48:27836}
837
[email protected]5d98294912012-06-27 22:57:40838////////////////////////////////////////////////////////////////////////////////
839// BrowserCommandController, TabRestoreServiceObserver implementation:
840
841void BrowserCommandController::TabRestoreServiceChanged(
blundell74001adc2015-09-18 11:04:25842 sessions::TabRestoreService* service) {
[email protected]2e9369e2014-08-15 09:12:53843 UpdateTabRestoreCommandState();
[email protected]5d98294912012-06-27 22:57:40844}
845
846void BrowserCommandController::TabRestoreServiceDestroyed(
blundell74001adc2015-09-18 11:04:25847 sessions::TabRestoreService* service) {
[email protected]5d98294912012-06-27 22:57:40848 service->RemoveObserver(this);
849}
850
[email protected]2e9369e2014-08-15 09:12:53851void BrowserCommandController::TabRestoreServiceLoaded(
blundell74001adc2015-09-18 11:04:25852 sessions::TabRestoreService* service) {
[email protected]2e9369e2014-08-15 09:12:53853 UpdateTabRestoreCommandState();
854}
855
[email protected]5d98294912012-06-27 22:57:40856////////////////////////////////////////////////////////////////////////////////
[email protected]5d98294912012-06-27 22:57:40857// BrowserCommandController, private:
858
[email protected]20ca0382013-02-28 19:50:07859class BrowserCommandController::InterstitialObserver
860 : public content::WebContentsObserver {
861 public:
862 InterstitialObserver(BrowserCommandController* controller,
863 content::WebContents* web_contents)
Aran Gilmand217b5b2019-04-11 17:45:43864 : WebContentsObserver(web_contents), controller_(controller) {}
[email protected]20ca0382013-02-28 19:50:07865
dcheng5dd5ff62014-10-21 12:42:38866 void DidAttachInterstitialPage() override {
[email protected]20ca0382013-02-28 19:50:07867 controller_->UpdateCommandsForTabState();
868 }
869
dcheng5dd5ff62014-10-21 12:42:38870 void DidDetachInterstitialPage() override {
[email protected]20ca0382013-02-28 19:50:07871 controller_->UpdateCommandsForTabState();
872 }
873
874 private:
875 BrowserCommandController* controller_;
876
877 DISALLOW_COPY_AND_ASSIGN(InterstitialObserver);
878};
879
[email protected]6a414ff2013-02-27 08:22:54880bool BrowserCommandController::IsShowingMainUI() {
manukf1a116392018-10-25 16:28:24881 return browser_->SupportsWindowFeature(Browser::FEATURE_TABSTRIP);
882}
883
884bool BrowserCommandController::IsShowingLocationBar() {
885 return browser_->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR);
[email protected]5d98294912012-06-27 22:57:40886}
887
888void BrowserCommandController::InitCommandState() {
889 // All browser commands whose state isn't set automagically some other way
890 // (like Back & Forward with initial page load) must have their state
891 // initialized here, otherwise they will be forever disabled.
892
Ivan Sandrk9669d0e2017-12-15 23:50:20893 if (is_locked_fullscreen_)
894 return;
895
[email protected]5d98294912012-06-27 22:57:40896 // Navigation commands
897 command_updater_.UpdateCommandEnabled(IDC_RELOAD, true);
toyoshim7dad4b1182016-04-01 14:28:05898 command_updater_.UpdateCommandEnabled(IDC_RELOAD_BYPASSING_CACHE, true);
[email protected]58e29032012-08-06 20:19:57899 command_updater_.UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE, true);
[email protected]5d98294912012-06-27 22:57:40900
901 // Window management commands
902 command_updater_.UpdateCommandEnabled(IDC_CLOSE_WINDOW, true);
903 command_updater_.UpdateCommandEnabled(IDC_NEW_TAB, true);
904 command_updater_.UpdateCommandEnabled(IDC_CLOSE_TAB, true);
905 command_updater_.UpdateCommandEnabled(IDC_DUPLICATE_TAB, true);
[email protected]2e9369e2014-08-15 09:12:53906 UpdateTabRestoreCommandState();
[email protected]d28d3782013-02-26 16:31:55907 command_updater_.UpdateCommandEnabled(IDC_EXIT, true);
[email protected]5d98294912012-06-27 22:57:40908 command_updater_.UpdateCommandEnabled(IDC_DEBUG_FRAME_TOGGLE, true);
[email protected]d12cc5e2013-10-19 18:25:06909#if defined(OS_CHROMEOS)
James Cook934abaf2017-09-19 22:21:58910 command_updater_.UpdateCommandEnabled(IDC_MINIMIZE_WINDOW, true);
[email protected]d12cc5e2013-10-19 18:25:06911 command_updater_.UpdateCommandEnabled(IDC_VISIT_DESKTOP_OF_LRU_USER_2, true);
912 command_updater_.UpdateCommandEnabled(IDC_VISIT_DESKTOP_OF_LRU_USER_3, true);
913#endif
[email protected]893124a22014-04-15 00:45:28914#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
Tom Andersonf15ede502017-11-10 03:17:15915 command_updater_.UpdateCommandEnabled(IDC_MINIMIZE_WINDOW, true);
916 command_updater_.UpdateCommandEnabled(IDC_MAXIMIZE_WINDOW, true);
917 command_updater_.UpdateCommandEnabled(IDC_RESTORE_WINDOW, true);
Maksim Sisovac767592018-08-10 08:00:15918 bool use_system_title_bar = true;
919#if defined(USE_OZONE)
920 use_system_title_bar = ui::OzonePlatform::GetInstance()
921 ->GetPlatformProperties()
922 .use_system_title_bar;
923#endif
924 command_updater_.UpdateCommandEnabled(IDC_USE_SYSTEM_TITLE_BAR,
925 use_system_title_bar);
[email protected]af97be4c62014-02-13 14:43:34926#endif
Alan Cutter09965802018-03-27 07:25:29927 command_updater_.UpdateCommandEnabled(IDC_OPEN_IN_PWA_WINDOW, true);
[email protected]5d98294912012-06-27 22:57:40928
929 // Page-related commands
930 command_updater_.UpdateCommandEnabled(IDC_EMAIL_PAGE_LOCATION, true);
[email protected]4bee4432014-05-05 13:11:41931 command_updater_.UpdateCommandEnabled(IDC_MANAGE_PASSWORDS_FOR_PAGE, true);
[email protected]5d98294912012-06-27 22:57:40932
933 // Zoom
934 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MENU, true);
935 command_updater_.UpdateCommandEnabled(IDC_ZOOM_PLUS, true);
[email protected]d93dbd12014-08-04 23:42:53936 command_updater_.UpdateCommandEnabled(IDC_ZOOM_NORMAL, false);
[email protected]5d98294912012-06-27 22:57:40937 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MINUS, true);
938
939 // Show various bits of UI
Aran Gilmand217b5b2019-04-11 17:45:43940 const bool guest_session =
941 profile()->IsGuestSession() || profile()->IsSystemProfile();
mlerman7831f57d2015-05-25 11:40:15942 DCHECK(!profile()->IsSystemProfile())
943 << "Ought to never have browser for the system profile.";
[email protected]338416c02014-05-13 16:47:06944 const bool normal_window = browser_->is_type_tabbed();
[email protected]05454532013-01-22 21:09:08945 UpdateOpenFileState(&command_updater_);
[email protected]5d98294912012-06-27 22:57:40946 UpdateCommandsForDevTools();
947 command_updater_.UpdateCommandEnabled(IDC_TASK_MANAGER, CanOpenTaskManager());
[email protected]338416c02014-05-13 16:47:06948 command_updater_.UpdateCommandEnabled(IDC_SHOW_HISTORY, !guest_session);
[email protected]5d98294912012-06-27 22:57:40949 command_updater_.UpdateCommandEnabled(IDC_SHOW_DOWNLOADS, true);
[email protected]674b65672014-06-02 23:21:56950 command_updater_.UpdateCommandEnabled(IDC_HELP_MENU, true);
[email protected]5d98294912012-06-27 22:57:40951 command_updater_.UpdateCommandEnabled(IDC_HELP_PAGE_VIA_KEYBOARD, true);
952 command_updater_.UpdateCommandEnabled(IDC_HELP_PAGE_VIA_MENU, true);
Bret Sepulveda2d018662017-05-18 21:31:48953 command_updater_.UpdateCommandEnabled(IDC_SHOW_BETA_FORUM, true);
[email protected]338416c02014-05-13 16:47:06954 command_updater_.UpdateCommandEnabled(IDC_BOOKMARKS_MENU, !guest_session);
Aran Gilmand217b5b2019-04-11 17:45:43955 command_updater_.UpdateCommandEnabled(
956 IDC_RECENT_TABS_MENU, !guest_session && !profile()->IsOffTheRecord());
tsergeant341a8272017-04-18 03:54:33957 command_updater_.UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA,
958 !guest_session);
[email protected]39d47592014-01-10 21:42:45959#if defined(OS_CHROMEOS)
caelyn4e4e08a2015-02-04 21:27:49960 command_updater_.UpdateCommandEnabled(IDC_TAKE_SCREENSHOT, true);
Ramin Halavatif362b7a2019-04-17 05:24:50961 // Chrome OS uses the system tray menu to handle multi-profiles. Avatar menu
962 // is only required in incognito mode.
Ramin Halavati2394dc452019-05-21 16:21:58963 if (profile()->IsIncognitoProfile())
[email protected]338416c02014-05-13 16:47:06964 command_updater_.UpdateCommandEnabled(IDC_SHOW_AVATAR_MENU, true);
Ramin Halavatif362b7a2019-04-17 05:24:50965#else
966 if (normal_window)
967 command_updater_.UpdateCommandEnabled(IDC_SHOW_AVATAR_MENU, true);
[email protected]39d47592014-01-10 21:42:45968#endif
[email protected]5d98294912012-06-27 22:57:40969
Mihai Sardarescu2ec5205682018-09-26 09:20:05970 UpdateShowSyncState(true);
971
[email protected]5d98294912012-06-27 22:57:40972 // Navigation commands
Christopher Camerone1385742019-04-05 20:37:25973 command_updater_.UpdateCommandEnabled(IDC_HOME,
974 normal_window || browser_->is_app());
[email protected]5d98294912012-06-27 22:57:40975
Christopher Lam0dbac2b2017-11-14 07:12:10976 const bool is_experimental_hosted_app =
Eric Willigers4a5f7a92019-05-10 19:19:26977 web_app::AppBrowserController::IsForExperimentalWebAppBrowser(browser_);
Christopher Lam0dbac2b2017-11-14 07:12:10978 // Hosted app browser commands.
979 command_updater_.UpdateCommandEnabled(IDC_COPY_URL,
980 is_experimental_hosted_app);
981 command_updater_.UpdateCommandEnabled(IDC_OPEN_IN_CHROME,
982 is_experimental_hosted_app);
983 command_updater_.UpdateCommandEnabled(IDC_SITE_SETTINGS,
984 is_experimental_hosted_app);
Alan Cutter0c3132302018-02-21 05:09:02985 command_updater_.UpdateCommandEnabled(IDC_HOSTED_APP_MENU_APP_INFO,
Christopher Lam86b52712017-12-04 01:58:33986 is_experimental_hosted_app);
Christopher Lam0dbac2b2017-11-14 07:12:10987
[email protected]5d98294912012-06-27 22:57:40988 // Window management commands
[email protected]5d98294912012-06-27 22:57:40989 command_updater_.UpdateCommandEnabled(IDC_SELECT_NEXT_TAB, normal_window);
Aran Gilmand217b5b2019-04-11 17:45:43990 command_updater_.UpdateCommandEnabled(IDC_SELECT_PREVIOUS_TAB, normal_window);
[email protected]5d98294912012-06-27 22:57:40991 command_updater_.UpdateCommandEnabled(IDC_MOVE_TAB_NEXT, normal_window);
992 command_updater_.UpdateCommandEnabled(IDC_MOVE_TAB_PREVIOUS, normal_window);
993 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_0, normal_window);
994 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_1, normal_window);
995 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_2, normal_window);
996 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_3, normal_window);
997 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_4, normal_window);
998 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_5, normal_window);
999 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_6, normal_window);
1000 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_7, normal_window);
1001 command_updater_.UpdateCommandEnabled(IDC_SELECT_LAST_TAB, normal_window);
[email protected]5d98294912012-06-27 22:57:401002
[email protected]338416c02014-05-13 16:47:061003 // These are always enabled; the menu determines their menu item visibility.
[email protected]5d98294912012-06-27 22:57:401004 command_updater_.UpdateCommandEnabled(IDC_UPGRADE_DIALOG, true);
[email protected]5d98294912012-06-27 22:57:401005
[email protected]6bd370b2014-05-28 14:19:471006 // Distill current page.
1007 command_updater_.UpdateCommandEnabled(
avi556c05022014-12-22 23:31:431008 IDC_DISTILL_PAGE, base::CommandLine::ForCurrentProcess()->HasSwitch(
1009 switches::kEnableDomDistiller));
[email protected]6bd370b2014-05-28 14:19:471010
Tommy Steimelc4477982017-11-29 18:07:181011 command_updater_.UpdateCommandEnabled(IDC_WINDOW_MUTE_SITE, normal_window);
ellyjones0101ba02017-05-19 15:50:261012 command_updater_.UpdateCommandEnabled(IDC_WINDOW_PIN_TAB, normal_window);
1013
[email protected]338416c02014-05-13 16:47:061014 // Initialize other commands whose state changes based on various conditions.
[email protected]32dfede2013-08-25 15:48:251015 UpdateCommandsForFullscreenMode();
[email protected]5d98294912012-06-27 22:57:401016 UpdateCommandsForContentRestrictionState();
[email protected]5d98294912012-06-27 22:57:401017 UpdateCommandsForBookmarkEditing();
[email protected]5d98294912012-06-27 22:57:401018 UpdateCommandsForIncognitoAvailability();
1019}
1020
[email protected]05454532013-01-22 21:09:081021// static
1022void BrowserCommandController::UpdateSharedCommandsForIncognitoAvailability(
1023 CommandUpdater* command_updater,
1024 Profile* profile) {
mlermane01e6de2014-09-29 19:26:471025 const bool guest_session = profile->IsGuestSession();
1026 // TODO(mlerman): Make GetAvailability account for profile->IsGuestSession().
[email protected]5d98294912012-06-27 22:57:401027 IncognitoModePrefs::Availability incognito_availability =
[email protected]05454532013-01-22 21:09:081028 IncognitoModePrefs::GetAvailability(profile->GetPrefs());
1029 command_updater->UpdateCommandEnabled(
Aran Gilmand217b5b2019-04-11 17:45:431030 IDC_NEW_WINDOW, incognito_availability != IncognitoModePrefs::FORCED);
[email protected]05454532013-01-22 21:09:081031 command_updater->UpdateCommandEnabled(
[email protected]5d98294912012-06-27 22:57:401032 IDC_NEW_INCOGNITO_WINDOW,
mlermane01e6de2014-09-29 19:26:471033 incognito_availability != IncognitoModePrefs::DISABLED && !guest_session);
[email protected]5d98294912012-06-27 22:57:401034
[email protected]57b25292014-05-01 16:31:061035 const bool forced_incognito =
1036 incognito_availability == IncognitoModePrefs::FORCED ||
1037 guest_session; // Guest always runs in Incognito mode.
[email protected]05454532013-01-22 21:09:081038 command_updater->UpdateCommandEnabled(
[email protected]5d98294912012-06-27 22:57:401039 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]57b25292014-05-01 16:31:061040 browser_defaults::bookmarks_enabled && !forced_incognito);
Devlin Cronin21dba422018-05-30 15:45:161041 extensions::ExtensionService* extension_service =
[email protected]03d25812014-06-22 19:41:551042 extensions::ExtensionSystem::Get(profile)->extension_service();
[email protected]57b25292014-05-01 16:31:061043 const bool enable_extensions =
[email protected]5d98294912012-06-27 22:57:401044 extension_service && extension_service->extensions_enabled();
[email protected]5d98294912012-06-27 22:57:401045
[email protected]57b25292014-05-01 16:31:061046 // Bookmark manager and settings page/subpages are forced to open in normal
1047 // mode. For this reason we disable these commands when incognito is forced.
1048 command_updater->UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS,
1049 enable_extensions && !forced_incognito);
1050
1051 command_updater->UpdateCommandEnabled(IDC_IMPORT_SETTINGS, !forced_incognito);
1052 command_updater->UpdateCommandEnabled(IDC_OPTIONS,
1053 !forced_incognito || guest_session);
estade8c0780f2015-08-21 23:36:411054 command_updater->UpdateCommandEnabled(IDC_SHOW_SIGNIN, !forced_incognito);
[email protected]05454532013-01-22 21:09:081055}
1056
1057void BrowserCommandController::UpdateCommandsForIncognitoAvailability() {
Ivan Sandrk9669d0e2017-12-15 23:50:201058 if (is_locked_fullscreen_)
1059 return;
1060
[email protected]05454532013-01-22 21:09:081061 UpdateSharedCommandsForIncognitoAvailability(&command_updater_, profile());
1062
[email protected]6a414ff2013-02-27 08:22:541063 if (!IsShowingMainUI()) {
[email protected]05454532013-01-22 21:09:081064 command_updater_.UpdateCommandEnabled(IDC_IMPORT_SETTINGS, false);
1065 command_updater_.UpdateCommandEnabled(IDC_OPTIONS, false);
1066 }
[email protected]5d98294912012-06-27 22:57:401067}
1068
1069void BrowserCommandController::UpdateCommandsForTabState() {
Ivan Sandrk9669d0e2017-12-15 23:50:201070 if (is_locked_fullscreen_)
1071 return;
1072
[email protected]617ee962013-01-29 20:49:121073 WebContents* current_web_contents =
1074 browser_->tab_strip_model()->GetActiveWebContents();
[email protected]1c5119c2012-09-19 00:08:571075 if (!current_web_contents) // May be NULL during tab restore.
[email protected]5d98294912012-06-27 22:57:401076 return;
[email protected]5d98294912012-06-27 22:57:401077
1078 // Navigation commands
1079 command_updater_.UpdateCommandEnabled(IDC_BACK, CanGoBack(browser_));
1080 command_updater_.UpdateCommandEnabled(IDC_FORWARD, CanGoForward(browser_));
1081 command_updater_.UpdateCommandEnabled(IDC_RELOAD, CanReload(browser_));
toyoshim7dad4b1182016-04-01 14:28:051082 command_updater_.UpdateCommandEnabled(IDC_RELOAD_BYPASSING_CACHE,
[email protected]5d98294912012-06-27 22:57:401083 CanReload(browser_));
[email protected]58e29032012-08-06 20:19:571084 command_updater_.UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE,
1085 CanReload(browser_));
[email protected]5d98294912012-06-27 22:57:401086
1087 // Window management commands
Aran Gilmand217b5b2019-04-11 17:45:431088 command_updater_.UpdateCommandEnabled(
1089 IDC_DUPLICATE_TAB, !browser_->is_app() && CanDuplicateTab(browser_));
Tommy Steimelc4477982017-11-29 18:07:181090 command_updater_.UpdateCommandEnabled(IDC_WINDOW_MUTE_SITE,
ellyjones0101ba02017-05-19 15:50:261091 !browser_->is_app());
1092 command_updater_.UpdateCommandEnabled(IDC_WINDOW_PIN_TAB,
1093 !browser_->is_app());
[email protected]5d98294912012-06-27 22:57:401094
1095 // Page-related commands
1096 window()->SetStarredState(
[email protected]1c5119c2012-09-19 00:08:571097 BookmarkTabHelper::FromWebContents(current_web_contents)->is_starred());
[email protected]5423c372012-08-22 05:50:161098 window()->ZoomChangedForActiveTab(false);
[email protected]5d98294912012-06-27 22:57:401099 command_updater_.UpdateCommandEnabled(IDC_VIEW_SOURCE,
1100 CanViewSource(browser_));
1101 command_updater_.UpdateCommandEnabled(IDC_EMAIL_PAGE_LOCATION,
1102 CanEmailPageLocation(browser_));
1103 if (browser_->is_devtools())
1104 command_updater_.UpdateCommandEnabled(IDC_OPEN_FILE, false);
1105
Alan Cutter81ac726682018-09-25 00:18:361106 bool can_create_bookmark_app = CanCreateBookmarkApp(browser_);
1107 command_updater_.UpdateCommandEnabled(IDC_INSTALL_PWA,
1108 can_create_bookmark_app);
1109 command_updater_.UpdateCommandEnabled(IDC_CREATE_SHORTCUT,
1110 can_create_bookmark_app);
Alan Cutter403cebb2018-05-17 05:22:451111 command_updater_.UpdateCommandEnabled(IDC_OPEN_IN_PWA_WINDOW,
Alan Cutter81ac726682018-09-25 00:18:361112 can_create_bookmark_app);
[email protected]5d98294912012-06-27 22:57:401113
[email protected]9b7ab882012-09-10 23:46:361114 command_updater_.UpdateCommandEnabled(
1115 IDC_TOGGLE_REQUEST_TABLET_SITE,
1116 CanRequestTabletSite(current_web_contents));
1117
[email protected]5d98294912012-06-27 22:57:401118 UpdateCommandsForContentRestrictionState();
1119 UpdateCommandsForBookmarkEditing();
[email protected]3cac87232012-11-20 01:48:271120 UpdateCommandsForFind();
apacible45cbfc92015-09-28 22:45:411121 UpdateCommandsForMediaRouter();
[email protected]d93dbd12014-08-04 23:42:531122 // Update the zoom commands when an active tab is selected.
1123 UpdateCommandsForZoomState();
1124}
1125
1126void BrowserCommandController::UpdateCommandsForZoomState() {
Aran Gilmand217b5b2019-04-11 17:45:431127 WebContents* contents = browser_->tab_strip_model()->GetActiveWebContents();
[email protected]d93dbd12014-08-04 23:42:531128 if (!contents)
1129 return;
Aran Gilmand217b5b2019-04-11 17:45:431130 command_updater_.UpdateCommandEnabled(IDC_ZOOM_PLUS, CanZoomIn(contents));
a.sarkar.arundaadc712015-02-26 05:39:081131 command_updater_.UpdateCommandEnabled(IDC_ZOOM_NORMAL,
ccameronb7c1d6c2015-03-09 17:08:241132 CanResetZoom(contents));
Aran Gilmand217b5b2019-04-11 17:45:431133 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MINUS, CanZoomOut(contents));
[email protected]5d98294912012-06-27 22:57:401134}
1135
1136void BrowserCommandController::UpdateCommandsForContentRestrictionState() {
1137 int restrictions = GetContentRestrictions(browser_);
1138
1139 command_updater_.UpdateCommandEnabled(
[email protected]3c71576ce2013-07-23 02:00:011140 IDC_COPY, !(restrictions & CONTENT_RESTRICTION_COPY));
[email protected]5d98294912012-06-27 22:57:401141 command_updater_.UpdateCommandEnabled(
[email protected]3c71576ce2013-07-23 02:00:011142 IDC_CUT, !(restrictions & CONTENT_RESTRICTION_CUT));
[email protected]5d98294912012-06-27 22:57:401143 command_updater_.UpdateCommandEnabled(
[email protected]3c71576ce2013-07-23 02:00:011144 IDC_PASTE, !(restrictions & CONTENT_RESTRICTION_PASTE));
[email protected]5d98294912012-06-27 22:57:401145 UpdateSaveAsState();
1146 UpdatePrintingState();
1147}
1148
1149void BrowserCommandController::UpdateCommandsForDevTools() {
Ivan Sandrk9669d0e2017-12-15 23:50:201150 if (is_locked_fullscreen_)
1151 return;
1152
Pavol Marko56df0272018-07-04 17:55:041153 bool dev_tools_enabled = DevToolsWindow::AllowDevToolsFor(
1154 profile(), browser_->tab_strip_model()->GetActiveWebContents());
Aran Gilmand217b5b2019-04-11 17:45:431155 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS, dev_tools_enabled);
[email protected]5d98294912012-06-27 22:57:401156 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_CONSOLE,
1157 dev_tools_enabled);
[email protected]2056c3192013-10-21 22:40:511158 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_DEVICES,
1159 dev_tools_enabled);
[email protected]5d98294912012-06-27 22:57:401160 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_INSPECT,
1161 dev_tools_enabled);
[email protected]d16657c2012-09-03 14:25:101162 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_TOGGLE,
1163 dev_tools_enabled);
spqchan2c5d541e2017-10-25 07:07:111164#if defined(OS_MACOSX)
1165 command_updater_.UpdateCommandEnabled(IDC_TOGGLE_JAVASCRIPT_APPLE_EVENTS,
1166 dev_tools_enabled);
1167#endif
[email protected]5d98294912012-06-27 22:57:401168}
1169
1170void BrowserCommandController::UpdateCommandsForBookmarkEditing() {
Ivan Sandrk9669d0e2017-12-15 23:50:201171 if (is_locked_fullscreen_)
1172 return;
1173
[email protected]5d98294912012-06-27 22:57:401174 command_updater_.UpdateCommandEnabled(IDC_BOOKMARK_PAGE,
1175 CanBookmarkCurrentPage(browser_));
1176 command_updater_.UpdateCommandEnabled(IDC_BOOKMARK_ALL_TABS,
1177 CanBookmarkAllTabs(browser_));
thestig5149d272014-10-30 07:25:291178#if defined(OS_WIN)
1179 command_updater_.UpdateCommandEnabled(IDC_PIN_TO_START_SCREEN, true);
1180#endif
[email protected]5d98294912012-06-27 22:57:401181}
1182
1183void BrowserCommandController::UpdateCommandsForBookmarkBar() {
Ivan Sandrk9669d0e2017-12-15 23:50:201184 if (is_locked_fullscreen_)
1185 return;
1186
tfarina3bddbe112014-08-28 05:29:321187 command_updater_.UpdateCommandEnabled(
Aran Gilmand217b5b2019-04-11 17:45:431188 IDC_SHOW_BOOKMARK_BAR, browser_defaults::bookmarks_enabled &&
1189 !profile()->IsGuestSession() &&
1190 !profile()->IsSystemProfile() &&
1191 !profile()->GetPrefs()->IsManagedPreference(
1192 bookmarks::prefs::kShowBookmarkBar) &&
1193 IsShowingMainUI());
[email protected]5d98294912012-06-27 22:57:401194}
1195
[email protected]9ec3ea5b2012-12-03 18:14:301196void BrowserCommandController::UpdateCommandsForFileSelectionDialogs() {
Ivan Sandrk9669d0e2017-12-15 23:50:201197 if (is_locked_fullscreen_)
1198 return;
1199
[email protected]9ec3ea5b2012-12-03 18:14:301200 UpdateSaveAsState();
[email protected]05454532013-01-22 21:09:081201 UpdateOpenFileState(&command_updater_);
[email protected]9ec3ea5b2012-12-03 18:14:301202}
1203
[email protected]32dfede2013-08-25 15:48:251204void BrowserCommandController::UpdateCommandsForFullscreenMode() {
Ivan Sandrk9669d0e2017-12-15 23:50:201205 if (is_locked_fullscreen_)
1206 return;
1207
zijiehe3c7af992017-02-12 20:59:401208 const bool is_fullscreen = window() && window()->IsFullscreen();
1209 const bool show_main_ui = IsShowingMainUI();
manukf1a116392018-10-25 16:28:241210 const bool show_location_bar = IsShowingLocationBar();
1211
zijiehe3c7af992017-02-12 20:59:401212 const bool main_not_fullscreen = show_main_ui && !is_fullscreen;
[email protected]5d98294912012-06-27 22:57:401213
1214 // Navigation commands
1215 command_updater_.UpdateCommandEnabled(IDC_OPEN_CURRENT_URL, show_main_ui);
1216
1217 // Window management commands
1218 command_updater_.UpdateCommandEnabled(
Aran Gilmand217b5b2019-04-11 17:45:431219 IDC_SHOW_AS_TAB, !browser_->is_type_tabbed() && !is_fullscreen);
[email protected]5d98294912012-06-27 22:57:401220
1221 // Focus various bits of UI
1222 command_updater_.UpdateCommandEnabled(IDC_FOCUS_TOOLBAR, show_main_ui);
manukf1a116392018-10-25 16:28:241223 command_updater_.UpdateCommandEnabled(IDC_FOCUS_LOCATION, show_location_bar);
[email protected]5d98294912012-06-27 22:57:401224 command_updater_.UpdateCommandEnabled(IDC_FOCUS_SEARCH, show_main_ui);
Aran Gilmand217b5b2019-04-11 17:45:431225 command_updater_.UpdateCommandEnabled(IDC_FOCUS_MENU_BAR,
1226 main_not_fullscreen);
1227 command_updater_.UpdateCommandEnabled(IDC_FOCUS_NEXT_PANE,
1228 main_not_fullscreen);
1229 command_updater_.UpdateCommandEnabled(IDC_FOCUS_PREVIOUS_PANE,
1230 main_not_fullscreen);
1231 command_updater_.UpdateCommandEnabled(IDC_FOCUS_BOOKMARKS,
1232 main_not_fullscreen);
[email protected]822ca8c62013-04-19 00:49:151233 command_updater_.UpdateCommandEnabled(
David Tsengc0b1b642018-01-24 07:12:271234 IDC_FOCUS_INACTIVE_POPUP_FOR_ACCESSIBILITY, main_not_fullscreen);
[email protected]5d98294912012-06-27 22:57:401235
1236 // Show various bits of UI
1237 command_updater_.UpdateCommandEnabled(IDC_DEVELOPER_MENU, show_main_ui);
[email protected]236ad3022013-09-04 03:27:431238#if defined(GOOGLE_CHROME_BUILD)
Fabio Rocha19bf4262019-04-04 01:05:181239 command_updater_.UpdateCommandEnabled(
1240 IDC_FEEDBACK, show_main_ui || browser_->is_devtools());
[email protected]236ad3022013-09-04 03:27:431241#endif
Mihai Sardarescu2ec5205682018-09-26 09:20:051242 UpdateShowSyncState(show_main_ui);
[email protected]5d98294912012-06-27 22:57:401243
[email protected]5d98294912012-06-27 22:57:401244 command_updater_.UpdateCommandEnabled(IDC_EDIT_SEARCH_ENGINES, show_main_ui);
1245 command_updater_.UpdateCommandEnabled(IDC_VIEW_PASSWORDS, show_main_ui);
1246 command_updater_.UpdateCommandEnabled(IDC_ABOUT, show_main_ui);
1247 command_updater_.UpdateCommandEnabled(IDC_SHOW_APP_MENU, show_main_ui);
Tina Wangf09681ac2019-03-27 20:52:101248 command_updater_.UpdateCommandEnabled(IDC_SEND_TAB_TO_SELF, show_main_ui);
Yann Dago70ca3dd2019-02-27 01:12:351249 command_updater_.UpdateCommandEnabled(IDC_SHOW_MANAGEMENT_PAGE, true);
primianod3a81ab2016-01-25 22:21:151250
1251 if (base::debug::IsProfilingSupported())
1252 command_updater_.UpdateCommandEnabled(IDC_PROFILING_ENABLED, show_main_ui);
[email protected]5d98294912012-06-27 22:57:401253
[email protected]aeafc3852014-04-29 16:51:291254#if !defined(OS_MACOSX)
zijiehe3c7af992017-02-12 20:59:401255 // Disable toggling into fullscreen mode if disallowed by pref.
Aran Gilmand217b5b2019-04-11 17:45:431256 const bool fullscreen_enabled =
1257 is_fullscreen ||
zijiehe3c7af992017-02-12 20:59:401258 profile()->GetPrefs()->GetBoolean(prefs::kFullscreenAllowed);
1259#else
1260 const bool fullscreen_enabled = true;
[email protected]00a1cc5b2012-11-07 13:44:511261#endif
1262
1263 command_updater_.UpdateCommandEnabled(IDC_FULLSCREEN, fullscreen_enabled);
spqchanb8ffc7d2015-11-17 01:17:211264 command_updater_.UpdateCommandEnabled(IDC_TOGGLE_FULLSCREEN_TOOLBAR,
1265 fullscreen_enabled);
[email protected]5d98294912012-06-27 22:57:401266
1267 UpdateCommandsForBookmarkBar();
zijiehedb473d552017-02-24 01:13:411268 UpdateCommandsForIncognitoAvailability();
Alan Cutter167dfe82018-04-11 09:06:001269 UpdateCommandsForHostedAppAvailability();
1270}
1271
1272void BrowserCommandController::UpdateCommandsForHostedAppAvailability() {
1273 bool has_toolbar =
1274 browser_->is_type_tabbed() ||
Eric Willigers4a5f7a92019-05-10 19:19:261275 web_app::AppBrowserController::IsForExperimentalWebAppBrowser(browser_);
Alan Cutter167dfe82018-04-11 09:06:001276 if (window() && window()->ShouldHideUIForFullscreen())
1277 has_toolbar = false;
1278 command_updater_.UpdateCommandEnabled(IDC_FOCUS_TOOLBAR, has_toolbar);
1279 command_updater_.UpdateCommandEnabled(IDC_FOCUS_NEXT_PANE, has_toolbar);
1280 command_updater_.UpdateCommandEnabled(IDC_FOCUS_PREVIOUS_PANE, has_toolbar);
1281 command_updater_.UpdateCommandEnabled(IDC_SHOW_APP_MENU, has_toolbar);
[email protected]5d98294912012-06-27 22:57:401282}
1283
Ivan Sandrk9669d0e2017-12-15 23:50:201284#if defined(OS_CHROMEOS)
1285namespace {
1286
1287#if DCHECK_IS_ON()
1288// Makes sure that all commands that are not whitelisted are disabled. DCHECKs
1289// otherwise. Compiled only in debug mode.
1290void NonWhitelistedCommandsAreDisabled(CommandUpdaterImpl* command_updater) {
Ivan Sandrk6ac9c9a2018-12-14 14:15:071291 constexpr int kWhitelistedIds[] = {IDC_CUT, IDC_COPY, IDC_PASTE};
Ivan Sandrk9669d0e2017-12-15 23:50:201292
1293 // Go through all the command ids, skip the whitelisted ones.
1294 for (int id : command_updater->GetAllIds()) {
Haeun Kim3f6123502018-08-26 18:03:041295 if (base::ContainsValue(kWhitelistedIds, id)) {
Ivan Sandrk9669d0e2017-12-15 23:50:201296 continue;
1297 }
1298 DCHECK(!command_updater->IsCommandEnabled(id));
1299 }
1300}
1301#endif
1302
1303} // namespace
1304
1305void BrowserCommandController::UpdateCommandsForLockedFullscreenMode() {
Ivan Sandrke0e92452019-03-21 09:32:481306 bool is_locked_fullscreen =
1307 platform_util::IsBrowserLockedFullscreen(browser_);
Ivan Sandrk9669d0e2017-12-15 23:50:201308 // Sanity check to make sure this function is called only on state change.
1309 DCHECK_NE(is_locked_fullscreen, is_locked_fullscreen_);
1310 if (is_locked_fullscreen == is_locked_fullscreen_)
1311 return;
1312 is_locked_fullscreen_ = is_locked_fullscreen;
1313
1314 if (is_locked_fullscreen_) {
1315 command_updater_.DisableAllCommands();
1316 // Update the state of whitelisted commands:
1317 // IDC_CUT/IDC_COPY/IDC_PASTE,
1318 UpdateCommandsForContentRestrictionState();
Ivan Sandrk6ac9c9a2018-12-14 14:15:071319 // TODO(crbug.com/904637): Re-enable Find and Zoom in locked fullscreen.
Ivan Sandrk9669d0e2017-12-15 23:50:201320 // All other commands will be disabled (there is an early return in their
1321 // corresponding UpdateCommandsFor* functions).
1322#if DCHECK_IS_ON()
1323 NonWhitelistedCommandsAreDisabled(&command_updater_);
1324#endif
1325 } else {
1326 // Do an init call to re-initialize command state after the
1327 // DisableAllCommands.
1328 InitCommandState();
1329 }
1330}
1331#endif
1332
[email protected]5d98294912012-06-27 22:57:401333void BrowserCommandController::UpdatePrintingState() {
Ivan Sandrk9669d0e2017-12-15 23:50:201334 if (is_locked_fullscreen_)
1335 return;
1336
[email protected]d53e4032012-06-29 18:58:341337 bool print_enabled = CanPrint(browser_);
1338 command_updater_.UpdateCommandEnabled(IDC_PRINT, print_enabled);
Lei Zhang48a4a5262018-04-17 20:18:441339#if BUILDFLAG(ENABLE_PRINTING)
vitalybukaf9433e42014-09-08 10:04:551340 command_updater_.UpdateCommandEnabled(IDC_BASIC_PRINT,
1341 CanBasicPrint(browser_));
Lei Zhang48a4a5262018-04-17 20:18:441342#endif
[email protected]5d98294912012-06-27 22:57:401343}
1344
1345void BrowserCommandController::UpdateSaveAsState() {
Ivan Sandrk9669d0e2017-12-15 23:50:201346 if (is_locked_fullscreen_)
1347 return;
1348
[email protected]5d98294912012-06-27 22:57:401349 command_updater_.UpdateCommandEnabled(IDC_SAVE_PAGE, CanSavePage(browser_));
1350}
1351
Mihai Sardarescu2ec5205682018-09-26 09:20:051352void BrowserCommandController::UpdateShowSyncState(bool show_main_ui) {
1353 if (is_locked_fullscreen_)
1354 return;
1355
Mihai Sardarescuf87ccc12018-09-26 10:37:451356 command_updater_.UpdateCommandEnabled(
Mihai Sardarescu290bc882018-10-12 10:48:141357 IDC_SHOW_SIGNIN, show_main_ui && pref_signin_allowed_.GetValue());
Mihai Sardarescu2ec5205682018-09-26 09:20:051358}
1359
[email protected]05454532013-01-22 21:09:081360// static
1361void BrowserCommandController::UpdateOpenFileState(
1362 CommandUpdater* command_updater) {
[email protected]5d98294912012-06-27 22:57:401363 bool enabled = true;
1364 PrefService* local_state = g_browser_process->local_state();
1365 if (local_state)
1366 enabled = local_state->GetBoolean(prefs::kAllowFileSelectionDialogs);
1367
[email protected]05454532013-01-22 21:09:081368 command_updater->UpdateCommandEnabled(IDC_OPEN_FILE, enabled);
[email protected]5d98294912012-06-27 22:57:401369}
1370
1371void BrowserCommandController::UpdateReloadStopState(bool is_loading,
1372 bool force) {
Ivan Sandrk9669d0e2017-12-15 23:50:201373 if (is_locked_fullscreen_)
1374 return;
1375
[email protected]5d98294912012-06-27 22:57:401376 window()->UpdateReloadStopState(is_loading, force);
1377 command_updater_.UpdateCommandEnabled(IDC_STOP, is_loading);
1378}
1379
[email protected]2e9369e2014-08-15 09:12:531380void BrowserCommandController::UpdateTabRestoreCommandState() {
Ivan Sandrk9669d0e2017-12-15 23:50:201381 if (is_locked_fullscreen_)
1382 return;
1383
blundell74001adc2015-09-18 11:04:251384 sessions::TabRestoreService* tab_restore_service =
[email protected]2e9369e2014-08-15 09:12:531385 TabRestoreServiceFactory::GetForProfile(profile());
1386 // The command is enabled if the service hasn't loaded yet to trigger loading.
1387 // The command is updated once the load completes.
1388 command_updater_.UpdateCommandEnabled(
1389 IDC_RESTORE_TAB,
1390 tab_restore_service &&
Aran Gilmand217b5b2019-04-11 17:45:431391 (!tab_restore_service->IsLoaded() ||
1392 GetRestoreTabType(browser_) != TabStripModelDelegate::RESTORE_NONE));
[email protected]2e9369e2014-08-15 09:12:531393}
1394
[email protected]3cac87232012-11-20 01:48:271395void BrowserCommandController::UpdateCommandsForFind() {
1396 TabStripModel* model = browser_->tab_strip_model();
Aran Gilmand217b5b2019-04-11 17:45:431397 bool enabled =
1398 !model->IsTabBlocked(model->active_index()) && !browser_->is_devtools();
[email protected]3cac87232012-11-20 01:48:271399
1400 command_updater_.UpdateCommandEnabled(IDC_FIND, enabled);
1401 command_updater_.UpdateCommandEnabled(IDC_FIND_NEXT, enabled);
1402 command_updater_.UpdateCommandEnabled(IDC_FIND_PREVIOUS, enabled);
1403}
1404
apacible45cbfc92015-09-28 22:45:411405void BrowserCommandController::UpdateCommandsForMediaRouter() {
Ivan Sandrk9669d0e2017-12-15 23:50:201406 if (is_locked_fullscreen_)
1407 return;
1408
apacible45cbfc92015-09-28 22:45:411409 command_updater_.UpdateCommandEnabled(IDC_ROUTE_MEDIA,
1410 CanRouteMedia(browser_));
1411}
1412
[email protected]409ea2972012-11-10 19:54:431413void BrowserCommandController::AddInterstitialObservers(WebContents* contents) {
[email protected]20ca0382013-02-28 19:50:071414 interstitial_observers_.push_back(new InterstitialObserver(this, contents));
[email protected]5d98294912012-06-27 22:57:401415}
1416
1417void BrowserCommandController::RemoveInterstitialObservers(
[email protected]e89cfcb2012-11-11 14:47:241418 WebContents* contents) {
[email protected]20ca0382013-02-28 19:50:071419 for (size_t i = 0; i < interstitial_observers_.size(); i++) {
1420 if (interstitial_observers_[i]->web_contents() != contents)
1421 continue;
1422
1423 delete interstitial_observers_[i];
1424 interstitial_observers_.erase(interstitial_observers_.begin() + i);
1425 return;
1426 }
[email protected]5d98294912012-06-27 22:57:401427}
1428
1429BrowserWindow* BrowserCommandController::window() {
1430 return browser_->window();
1431}
1432
1433Profile* BrowserCommandController::profile() {
1434 return browser_->profile();
1435}
1436
[email protected]5d98294912012-06-27 22:57:401437} // namespace chrome