blob: 55be14f018df268c3d2926a684168d5eb3b808dc [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
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.
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
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
[email protected]5d98294912012-06-27 22:57:40312////////////////////////////////////////////////////////////////////////////////
Ivan Sandrk9669d0e2017-12-15 23:50:20313// BrowserCommandController, CommandUpdater implementation:
[email protected]5d98294912012-06-27 22:57:40314
Ivan Sandrk9669d0e2017-12-15 23:50:20315bool BrowserCommandController::SupportsCommand(int id) const {
316 return command_updater_.SupportsCommand(id);
317}
318
319bool BrowserCommandController::IsCommandEnabled(int id) const {
320 return command_updater_.IsCommandEnabled(id);
321}
322
Edwin Joe6f6fc1e2019-02-27 20:00:37323bool BrowserCommandController::ExecuteCommand(int id,
324 base::TimeTicks time_stamp) {
325 return ExecuteCommandWithDisposition(id, WindowOpenDisposition::CURRENT_TAB,
326 time_stamp);
Ivan Sandrk9669d0e2017-12-15 23:50:20327}
328
329bool BrowserCommandController::ExecuteCommandWithDisposition(
Edwin Joe6f6fc1e2019-02-27 20:00:37330 int id,
331 WindowOpenDisposition disposition,
332 base::TimeTicks time_stamp) {
Ivan Sandrk9669d0e2017-12-15 23:50:20333 // Doesn't go through the command_updater_ to avoid dealing with having a
334 // naming collision for ExecuteCommandWithDisposition (both
335 // CommandUpdaterDelegate and CommandUpdater declare this function so we
336 // choose to not implement CommandUpdaterDelegate inside this class and
337 // therefore command_updater_ doesn't have the delegate set).
338 if (!SupportsCommand(id) || !IsCommandEnabled(id))
339 return false;
340
[email protected]5d98294912012-06-27 22:57:40341 // No commands are enabled if there is not yet any selected tab.
342 // TODO(pkasting): It seems like we should not need this, because either
343 // most/all commands should not have been enabled yet anyway or the ones that
344 // are enabled should be global, or safe themselves against having no selected
345 // tab. However, Ben says he tried removing this before and got lots of
346 // crashes, e.g. from Windows sending WM_COMMANDs at random times during
347 // window construction. This probably could use closer examination someday.
[email protected]59253a652012-11-20 00:17:26348 if (browser_->tab_strip_model()->active_index() == TabStripModel::kNoTab)
Ivan Sandrk9669d0e2017-12-15 23:50:20349 return true;
[email protected]5d98294912012-06-27 22:57:40350
Aran Gilmand217b5b2019-04-11 17:45:43351 DCHECK(command_updater_.IsCommandEnabled(id))
352 << "Invalid/disabled command " << id;
[email protected]5d98294912012-06-27 22:57:40353
[email protected]5d98294912012-06-27 22:57:40354 // The order of commands in this switch statement must match the function
355 // declaration order in browser.h!
356 switch (id) {
357 // Navigation commands
358 case IDC_BACK:
359 GoBack(browser_, disposition);
360 break;
361 case IDC_FORWARD:
362 GoForward(browser_, disposition);
363 break;
364 case IDC_RELOAD:
365 Reload(browser_, disposition);
366 break;
[email protected]58e29032012-08-06 20:19:57367 case IDC_RELOAD_CLEARING_CACHE:
368 ClearCache(browser_);
Nico Webera745ef72018-01-29 23:45:57369 FALLTHROUGH;
toyoshim7dad4b1182016-04-01 14:28:05370 case IDC_RELOAD_BYPASSING_CACHE:
371 ReloadBypassingCache(browser_, disposition);
[email protected]5d98294912012-06-27 22:57:40372 break;
373 case IDC_HOME:
374 Home(browser_, disposition);
375 break;
376 case IDC_OPEN_CURRENT_URL:
377 OpenCurrentURL(browser_);
378 break;
379 case IDC_STOP:
380 Stop(browser_);
381 break;
382
Aran Gilmand217b5b2019-04-11 17:45:43383 // Window management commands
[email protected]5d98294912012-06-27 22:57:40384 case IDC_NEW_WINDOW:
385 NewWindow(browser_);
386 break;
387 case IDC_NEW_INCOGNITO_WINDOW:
Orin Jaworski5e4bcd0c2018-10-16 19:35:05388 NewIncognitoWindow(profile());
[email protected]5d98294912012-06-27 22:57:40389 break;
390 case IDC_CLOSE_WINDOW:
bratell0a7406f2017-03-28 07:46:37391 base::RecordAction(base::UserMetricsAction("CloseWindowByKey"));
[email protected]04b9e692012-08-24 14:49:09392 CloseWindow(browser_);
[email protected]5d98294912012-06-27 22:57:40393 break;
Bettina Dea0a4505922017-09-28 00:53:32394 case IDC_NEW_TAB: {
395 NewTab(browser_);
Collin Baker4ab8ad82019-06-06 17:02:54396#if BUILDFLAG(ENABLE_LEGACY_DESKTOP_IN_PRODUCT_HELP)
Catherine Chungbeb884222017-08-01 15:47:55397 // This is not in NewTab() to avoid tracking programmatic creation of new
398 // tabs by extensions.
Bettina Dea0a4505922017-09-28 00:53:32399 auto* new_tab_tracker =
400 feature_engagement::NewTabTrackerFactory::GetInstance()
401 ->GetForProfile(profile());
402
403 new_tab_tracker->OnNewTabOpened();
404 new_tab_tracker->CloseBubble();
Catherine Chungbeb884222017-08-01 15:47:55405#endif
[email protected]5d98294912012-06-27 22:57:40406 break;
Bettina Dea0a4505922017-09-28 00:53:32407 }
[email protected]5d98294912012-06-27 22:57:40408 case IDC_CLOSE_TAB:
bratell0a7406f2017-03-28 07:46:37409 base::RecordAction(base::UserMetricsAction("CloseTabByKey"));
[email protected]04b9e692012-08-24 14:49:09410 CloseTab(browser_);
[email protected]5d98294912012-06-27 22:57:40411 break;
412 case IDC_SELECT_NEXT_TAB:
bratell0a7406f2017-03-28 07:46:37413 base::RecordAction(base::UserMetricsAction("Accel_SelectNextTab"));
Edwin Joe6f6fc1e2019-02-27 20:00:37414 SelectNextTab(browser_,
415 {TabStripModel::GestureType::kKeyboard, time_stamp});
[email protected]5d98294912012-06-27 22:57:40416 break;
417 case IDC_SELECT_PREVIOUS_TAB:
bratell0a7406f2017-03-28 07:46:37418 base::RecordAction(base::UserMetricsAction("Accel_SelectPreviousTab"));
Edwin Joe6f6fc1e2019-02-27 20:00:37419 SelectPreviousTab(browser_,
420 {TabStripModel::GestureType::kKeyboard, time_stamp});
[email protected]5d98294912012-06-27 22:57:40421 break;
[email protected]5d98294912012-06-27 22:57:40422 case IDC_MOVE_TAB_NEXT:
423 MoveTabNext(browser_);
424 break;
425 case IDC_MOVE_TAB_PREVIOUS:
426 MoveTabPrevious(browser_);
427 break;
428 case IDC_SELECT_TAB_0:
429 case IDC_SELECT_TAB_1:
430 case IDC_SELECT_TAB_2:
431 case IDC_SELECT_TAB_3:
432 case IDC_SELECT_TAB_4:
433 case IDC_SELECT_TAB_5:
434 case IDC_SELECT_TAB_6:
435 case IDC_SELECT_TAB_7:
bratell0a7406f2017-03-28 07:46:37436 base::RecordAction(base::UserMetricsAction("Accel_SelectNumberedTab"));
Edwin Joe6f6fc1e2019-02-27 20:00:37437 SelectNumberedTab(browser_, id - IDC_SELECT_TAB_0,
438 {TabStripModel::GestureType::kKeyboard, time_stamp});
[email protected]5d98294912012-06-27 22:57:40439 break;
440 case IDC_SELECT_LAST_TAB:
bratell0a7406f2017-03-28 07:46:37441 base::RecordAction(base::UserMetricsAction("Accel_SelectNumberedTab"));
Edwin Joe6f6fc1e2019-02-27 20:00:37442 SelectLastTab(browser_,
443 {TabStripModel::GestureType::kKeyboard, time_stamp});
[email protected]5d98294912012-06-27 22:57:40444 break;
445 case IDC_DUPLICATE_TAB:
446 DuplicateTab(browser_);
447 break;
448 case IDC_RESTORE_TAB:
449 RestoreTab(browser_);
Collin Baker9bbe1d82019-03-08 01:16:27450 browser_->window()->OnTabRestored(IDC_RESTORE_TAB);
[email protected]5d98294912012-06-27 22:57:40451 break;
[email protected]5d98294912012-06-27 22:57:40452 case IDC_SHOW_AS_TAB:
453 ConvertPopupToTabbedBrowser(browser_);
454 break;
455 case IDC_FULLSCREEN:
[email protected]3f32b9b2012-07-09 16:59:28456 chrome::ToggleFullscreenMode(browser_);
[email protected]5d98294912012-06-27 22:57:40457 break;
Alan Cutter09965802018-03-27 07:25:29458 case IDC_OPEN_IN_PWA_WINDOW:
459 base::RecordAction(base::UserMetricsAction("OpenActiveTabInPwaWindow"));
460 ReparentSecureActiveTabIntoPwaWindow(browser_);
461 break;
[email protected]770c6d82012-09-06 22:21:32462
[email protected]d12cc5e2013-10-19 18:25:06463#if defined(OS_CHROMEOS)
464 case IDC_VISIT_DESKTOP_OF_LRU_USER_2:
[email protected]0c930812014-01-30 18:01:47465 case IDC_VISIT_DESKTOP_OF_LRU_USER_3:
zijiehe4dde8072017-02-13 20:38:35466 ExecuteVisitDesktopCommand(id, window()->GetNativeWindow());
[email protected]0c930812014-01-30 18:01:47467 break;
[email protected]d12cc5e2013-10-19 18:25:06468#endif
469
[email protected]893124a22014-04-15 00:45:28470#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
Tom Andersonf15ede502017-11-10 03:17:15471 case IDC_MINIMIZE_WINDOW:
472 browser_->window()->Minimize();
473 break;
474 case IDC_MAXIMIZE_WINDOW:
475 browser_->window()->Maximize();
476 break;
477 case IDC_RESTORE_WINDOW:
478 browser_->window()->Restore();
479 break;
[email protected]af97be4c62014-02-13 14:43:34480 case IDC_USE_SYSTEM_TITLE_BAR: {
zijiehe4dde8072017-02-13 20:38:35481 PrefService* prefs = profile()->GetPrefs();
[email protected]af97be4c62014-02-13 14:43:34482 prefs->SetBoolean(prefs::kUseCustomChromeFrame,
483 !prefs->GetBoolean(prefs::kUseCustomChromeFrame));
484 break;
485 }
486#endif
487
[email protected]5d98294912012-06-27 22:57:40488#if defined(OS_MACOSX)
spqchanb8ffc7d2015-11-17 01:17:21489 case IDC_TOGGLE_FULLSCREEN_TOOLBAR:
490 chrome::ToggleFullscreenToolbar(browser_);
491 break;
spqchan2c5d541e2017-10-25 07:07:11492 case IDC_TOGGLE_JAVASCRIPT_APPLE_EVENTS: {
Robert Sesek03282612018-10-31 15:48:06493 chrome::ToggleJavaScriptFromAppleEventsAllowed(browser_);
spqchan2c5d541e2017-10-25 07:07:11494 break;
495 }
[email protected]5d98294912012-06-27 22:57:40496#endif
497 case IDC_EXIT:
498 Exit();
499 break;
500
501 // Page-related commands
502 case IDC_SAVE_PAGE:
503 SavePage(browser_);
504 break;
505 case IDC_BOOKMARK_PAGE:
Collin Baker4ab8ad82019-06-06 17:02:54506#if BUILDFLAG(ENABLE_LEGACY_DESKTOP_IN_PRODUCT_HELP)
Bettina Dea48125dc2017-09-06 03:23:14507 feature_engagement::BookmarkTrackerFactory::GetInstance()
508 ->GetForProfile(profile())
509 ->OnBookmarkAdded();
510#endif
deepak.m154a7f392014-12-15 04:41:43511 BookmarkCurrentPageAllowingExtensionOverrides(browser_);
[email protected]5d98294912012-06-27 22:57:40512 break;
[email protected]5d98294912012-06-27 22:57:40513 case IDC_BOOKMARK_ALL_TABS:
Collin Baker4ab8ad82019-06-06 17:02:54514#if BUILDFLAG(ENABLE_LEGACY_DESKTOP_IN_PRODUCT_HELP)
Bettina Dea48125dc2017-09-06 03:23:14515 feature_engagement::BookmarkTrackerFactory::GetInstance()
516 ->GetForProfile(profile())
517 ->OnBookmarkAdded();
518#endif
[email protected]5d98294912012-06-27 22:57:40519 BookmarkAllTabs(browser_);
520 break;
521 case IDC_VIEW_SOURCE:
Lukasz Anforowicze1b954d92017-10-30 21:28:06522 browser_->tab_strip_model()
523 ->GetActiveWebContents()
524 ->GetMainFrame()
525 ->ViewSource();
[email protected]5d98294912012-06-27 22:57:40526 break;
527 case IDC_EMAIL_PAGE_LOCATION:
528 EmailPageLocation(browser_);
529 break;
530 case IDC_PRINT:
531 Print(browser_);
532 break;
bondd052b5f82015-10-28 22:39:32533
Lei Zhang48a4a5262018-04-17 20:18:44534#if BUILDFLAG(ENABLE_PRINTING)
vitalybukaf9433e42014-09-08 10:04:55535 case IDC_BASIC_PRINT:
bratell0a7406f2017-03-28 07:46:37536 base::RecordAction(base::UserMetricsAction("Accel_Advanced_Print"));
vitalybukaf9433e42014-09-08 10:04:55537 BasicPrint(browser_);
[email protected]5d98294912012-06-27 22:57:40538 break;
Lei Zhang48a4a5262018-04-17 20:18:44539#endif // ENABLE_PRINTING
bondd052b5f82015-10-28 22:39:32540
bondd052b5f82015-10-28 22:39:32541 case IDC_SAVE_CREDIT_CARD_FOR_PAGE:
542 SaveCreditCard(browser_);
543 break;
siyua3e599eaa2018-07-13 00:23:06544 case IDC_MIGRATE_LOCAL_CREDIT_CARD_FOR_PAGE:
545 MigrateLocalCards(browser_);
546 break;
[email protected]e625b7602013-10-28 09:24:56547 case IDC_TRANSLATE_PAGE:
548 Translate(browser_);
549 break;
[email protected]4bee4432014-05-05 13:11:41550 case IDC_MANAGE_PASSWORDS_FOR_PAGE:
551 ManagePasswordsForPage(browser_);
552 break;
Tina Wang4c324702019-04-25 18:35:15553 case IDC_SEND_TAB_TO_SELF:
Tina Wang0e7e6e92019-05-14 21:03:03554 SendTabToSelfFromPageAction(browser_);
Tina Wang4c324702019-04-25 18:35:15555 break;
[email protected]4bee4432014-05-05 13:11:41556
[email protected]5d98294912012-06-27 22:57:40557 // Clipboard commands
558 case IDC_CUT:
[email protected]5d98294912012-06-27 22:57:40559 case IDC_COPY:
[email protected]5d98294912012-06-27 22:57:40560 case IDC_PASTE:
pkastingcd3f08bce2015-04-18 13:37:12561 CutCopyPaste(browser_, id);
[email protected]5d98294912012-06-27 22:57:40562 break;
563
564 // Find-in-page
565 case IDC_FIND:
566 Find(browser_);
567 break;
568 case IDC_FIND_NEXT:
569 FindNext(browser_);
570 break;
571 case IDC_FIND_PREVIOUS:
572 FindPrevious(browser_);
573 break;
Jeff Fisher6cc1ce7b2019-06-28 23:02:38574 case IDC_CLOSE_FIND_OR_STOP:
575 if (CanCloseFind(browser_))
576 CloseFind(browser_);
577 else if (IsCommandEnabled(IDC_STOP))
578 ExecuteCommand(IDC_STOP);
579 break;
[email protected]5d98294912012-06-27 22:57:40580
581 // Zoom
582 case IDC_ZOOM_PLUS:
583 Zoom(browser_, content::PAGE_ZOOM_IN);
584 break;
585 case IDC_ZOOM_NORMAL:
586 Zoom(browser_, content::PAGE_ZOOM_RESET);
587 break;
588 case IDC_ZOOM_MINUS:
589 Zoom(browser_, content::PAGE_ZOOM_OUT);
590 break;
591
592 // Focus various bits of UI
593 case IDC_FOCUS_TOOLBAR:
bratell0a7406f2017-03-28 07:46:37594 base::RecordAction(base::UserMetricsAction("Accel_Focus_Toolbar"));
[email protected]5d98294912012-06-27 22:57:40595 FocusToolbar(browser_);
596 break;
597 case IDC_FOCUS_LOCATION:
bratell0a7406f2017-03-28 07:46:37598 base::RecordAction(base::UserMetricsAction("Accel_Focus_Location"));
[email protected]5d98294912012-06-27 22:57:40599 FocusLocationBar(browser_);
600 break;
601 case IDC_FOCUS_SEARCH:
bratell0a7406f2017-03-28 07:46:37602 base::RecordAction(base::UserMetricsAction("Accel_Focus_Search"));
[email protected]5d98294912012-06-27 22:57:40603 FocusSearch(browser_);
604 break;
605 case IDC_FOCUS_MENU_BAR:
606 FocusAppMenu(browser_);
607 break;
608 case IDC_FOCUS_BOOKMARKS:
bratell0a7406f2017-03-28 07:46:37609 base::RecordAction(base::UserMetricsAction("Accel_Focus_Bookmarks"));
[email protected]5d98294912012-06-27 22:57:40610 FocusBookmarksToolbar(browser_);
611 break;
David Tsengc0b1b642018-01-24 07:12:27612 case IDC_FOCUS_INACTIVE_POPUP_FOR_ACCESSIBILITY:
613 FocusInactivePopupForAccessibility(browser_);
[email protected]822ca8c62013-04-19 00:49:15614 break;
[email protected]5d98294912012-06-27 22:57:40615 case IDC_FOCUS_NEXT_PANE:
616 FocusNextPane(browser_);
617 break;
618 case IDC_FOCUS_PREVIOUS_PANE:
619 FocusPreviousPane(browser_);
620 break;
621
622 // Show various bits of UI
623 case IDC_OPEN_FILE:
624 browser_->OpenFile();
625 break;
Alan Cutter81ac726682018-09-25 00:18:36626 case IDC_CREATE_SHORTCUT:
Alan Cutter98844a9fa2019-06-19 21:11:40627 base::RecordAction(base::UserMetricsAction("CreateShortcut"));
Alan Cutter81ac726682018-09-25 00:18:36628 CreateBookmarkAppFromCurrentWebContents(browser_,
629 true /* force_shortcut_app */);
630 break;
631 case IDC_INSTALL_PWA:
Alan Cutter98844a9fa2019-06-19 21:11:40632 base::RecordAction(base::UserMetricsAction("InstallWebAppFromMenu"));
Alan Cutter81ac726682018-09-25 00:18:36633 CreateBookmarkAppFromCurrentWebContents(browser_,
634 false /* force_shortcut_app */);
[email protected]488e3952013-11-18 05:29:14635 break;
[email protected]5d98294912012-06-27 22:57:40636 case IDC_DEV_TOOLS:
[email protected]c934c382013-11-01 00:36:01637 ToggleDevToolsWindow(browser_, DevToolsToggleAction::Show());
[email protected]5d98294912012-06-27 22:57:40638 break;
639 case IDC_DEV_TOOLS_CONSOLE:
einbinderdfa567b2016-12-16 01:15:52640 ToggleDevToolsWindow(browser_, DevToolsToggleAction::ShowConsolePanel());
[email protected]5d98294912012-06-27 22:57:40641 break;
[email protected]2056c3192013-10-21 22:40:51642 case IDC_DEV_TOOLS_DEVICES:
643 InspectUI::InspectDevices(browser_);
644 break;
[email protected]5d98294912012-06-27 22:57:40645 case IDC_DEV_TOOLS_INSPECT:
[email protected]c934c382013-11-01 00:36:01646 ToggleDevToolsWindow(browser_, DevToolsToggleAction::Inspect());
[email protected]5d98294912012-06-27 22:57:40647 break;
[email protected]d16657c2012-09-03 14:25:10648 case IDC_DEV_TOOLS_TOGGLE:
[email protected]c934c382013-11-01 00:36:01649 ToggleDevToolsWindow(browser_, DevToolsToggleAction::Toggle());
[email protected]d16657c2012-09-03 14:25:10650 break;
[email protected]5d98294912012-06-27 22:57:40651 case IDC_TASK_MANAGER:
[email protected]29c262de2013-06-22 15:39:38652 OpenTaskManager(browser_);
[email protected]5d98294912012-06-27 22:57:40653 break;
caelyn4e4e08a2015-02-04 21:27:49654#if defined(OS_CHROMEOS)
655 case IDC_TAKE_SCREENSHOT:
656 TakeScreenshot();
657 break;
658#endif
[email protected]236ad3022013-09-04 03:27:43659#if defined(GOOGLE_CHROME_BUILD)
[email protected]5d98294912012-06-27 22:57:40660 case IDC_FEEDBACK:
afakhryf4575bd2017-04-28 02:21:04661 OpenFeedbackDialog(browser_, kFeedbackSourceBrowserCommand);
[email protected]5d98294912012-06-27 22:57:40662 break;
[email protected]236ad3022013-09-04 03:27:43663#endif
[email protected]5d98294912012-06-27 22:57:40664 case IDC_SHOW_BOOKMARK_BAR:
665 ToggleBookmarkBar(browser_);
666 break;
667 case IDC_PROFILING_ENABLED:
Bryce Thomas96b07772018-11-07 03:04:15668 content::Profiling::Toggle();
[email protected]5d98294912012-06-27 22:57:40669 break;
670
671 case IDC_SHOW_BOOKMARK_MANAGER:
672 ShowBookmarkManager(browser_);
673 break;
674 case IDC_SHOW_APP_MENU:
bratell0a7406f2017-03-28 07:46:37675 base::RecordAction(base::UserMetricsAction("Accel_Show_App_Menu"));
[email protected]5d98294912012-06-27 22:57:40676 ShowAppMenu(browser_);
677 break;
678 case IDC_SHOW_AVATAR_MENU:
679 ShowAvatarMenu(browser_);
680 break;
681 case IDC_SHOW_HISTORY:
682 ShowHistory(browser_);
683 break;
684 case IDC_SHOW_DOWNLOADS:
685 ShowDownloads(browser_);
686 break;
687 case IDC_MANAGE_EXTENSIONS:
[email protected]bc9833c32013-02-28 04:05:08688 ShowExtensions(browser_, std::string());
[email protected]5d98294912012-06-27 22:57:40689 break;
690 case IDC_OPTIONS:
691 ShowSettings(browser_);
692 break;
693 case IDC_EDIT_SEARCH_ENGINES:
694 ShowSearchEngineSettings(browser_);
695 break;
696 case IDC_VIEW_PASSWORDS:
697 ShowPasswordManager(browser_);
698 break;
699 case IDC_CLEAR_BROWSING_DATA:
700 ShowClearBrowsingDataDialog(browser_);
701 break;
702 case IDC_IMPORT_SETTINGS:
703 ShowImportDialog(browser_);
704 break;
[email protected]9b7ab882012-09-10 23:46:36705 case IDC_TOGGLE_REQUEST_TABLET_SITE:
706 ToggleRequestTabletSite(browser_);
707 break;
[email protected]5d98294912012-06-27 22:57:40708 case IDC_ABOUT:
709 ShowAboutChrome(browser_);
710 break;
711 case IDC_UPGRADE_DIALOG:
712 OpenUpdateChromeDialog(browser_);
713 break;
[email protected]5d98294912012-06-27 22:57:40714 case IDC_HELP_PAGE_VIA_KEYBOARD:
715 ShowHelp(browser_, HELP_SOURCE_KEYBOARD);
716 break;
717 case IDC_HELP_PAGE_VIA_MENU:
718 ShowHelp(browser_, HELP_SOURCE_MENU);
719 break;
Bret Sepulveda2d018662017-05-18 21:31:48720 case IDC_SHOW_BETA_FORUM:
721 ShowBetaForum(browser_);
722 break;
David Roger7e25dff2019-02-07 09:03:17723#if !defined(OS_CHROMEOS)
estade8c0780f2015-08-21 23:36:41724 case IDC_SHOW_SIGNIN:
gogerald71bf6c902015-12-08 00:49:37725 ShowBrowserSigninOrSettings(
726 browser_, signin_metrics::AccessPoint::ACCESS_POINT_MENU);
estade8c0780f2015-08-21 23:36:41727 break;
David Roger7e25dff2019-02-07 09:03:17728#endif
[email protected]6bd370b2014-05-28 14:19:47729 case IDC_DISTILL_PAGE:
Aran Gilman6586e8f2019-06-03 18:50:56730 ToggleDistilledView(browser_);
[email protected]6bd370b2014-05-28 14:19:47731 break;
apacible45cbfc92015-09-28 22:45:41732 case IDC_ROUTE_MEDIA:
733 RouteMedia(browser_);
734 break;
Tommy Steimelc4477982017-11-29 18:07:18735 case IDC_WINDOW_MUTE_SITE:
736 MuteSite(browser_);
ellyjones0101ba02017-05-19 15:50:26737 break;
738 case IDC_WINDOW_PIN_TAB:
739 PinTab(browser_);
740 break;
Yann Dagocd13225a2019-03-07 18:39:05741 case IDC_SHOW_MANAGEMENT_PAGE: {
Yann Dagoc1d1c4af2019-04-17 13:40:51742 ShowSingletonTab(browser_, GURL(kChromeUIManagementURL));
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:27743 break;
Yann Dagocd13225a2019-03-07 18:39:05744 }
Christopher Lam0dbac2b2017-11-14 07:12:10745 // Hosted App commands
746 case IDC_COPY_URL:
747 CopyURL(browser_);
748 break;
749 case IDC_OPEN_IN_CHROME:
750 OpenInChrome(browser_);
751 break;
752 case IDC_SITE_SETTINGS:
753 ShowSiteSettings(
754 browser_,
755 browser_->tab_strip_model()->GetActiveWebContents()->GetVisibleURL());
756 break;
Dana Fried08b774a2019-05-10 18:21:48757 case IDC_HOSTED_APP_MENU_APP_INFO: {
758 content::WebContents* const web_contents =
759 browser_->tab_strip_model()->GetActiveWebContents();
760 if (web_contents) {
761 ShowPageInfoDialog(web_contents,
762 base::BindOnce(&AppInfoDialogClosedCallback,
763 base::Unretained(web_contents)),
764 bubble_anchor_util::kAppMenuButton);
765 }
Christopher Lam86b52712017-12-04 01:58:33766 break;
Dana Fried08b774a2019-05-10 18:21:48767 }
[email protected]5d98294912012-06-27 22:57:40768 default:
769 LOG(WARNING) << "Received Unimplemented Command: " << id;
770 break;
771 }
Ivan Sandrk9669d0e2017-12-15 23:50:20772
773 return true;
774}
775
776void BrowserCommandController::AddCommandObserver(int id,
777 CommandObserver* observer) {
778 command_updater_.AddCommandObserver(id, observer);
779}
780
781void BrowserCommandController::RemoveCommandObserver(
Aran Gilmand217b5b2019-04-11 17:45:43782 int id,
783 CommandObserver* observer) {
Ivan Sandrk9669d0e2017-12-15 23:50:20784 command_updater_.RemoveCommandObserver(id, observer);
785}
786
787void BrowserCommandController::RemoveCommandObserver(
788 CommandObserver* observer) {
789 command_updater_.RemoveCommandObserver(observer);
790}
791
792bool BrowserCommandController::UpdateCommandEnabled(int id, bool state) {
793 if (is_locked_fullscreen_)
794 return false;
795
796 return command_updater_.UpdateCommandEnabled(id, state);
[email protected]5d98294912012-06-27 22:57:40797}
798
Mihai Sardarescuf87ccc12018-09-26 10:37:45799////////////////////////////////////////////////////////////////////////////////
800// BrowserCommandController, SigninPrefObserver implementation:
801
802void BrowserCommandController::OnSigninAllowedPrefChange() {
803 // For unit tests, we don't have a window.
804 if (!window())
805 return;
806 UpdateShowSyncState(IsShowingMainUI());
807}
808
[email protected]5d98294912012-06-27 22:57:40809// BrowserCommandController, TabStripModelObserver implementation:
810
sangwoo.ko1ae265f12018-10-18 08:30:28811void BrowserCommandController::OnTabStripModelChanged(
812 TabStripModel* tab_strip_model,
813 const TabStripModelChange& change,
814 const TabStripSelectionChange& selection) {
Dana Frieda391e6ce2019-05-08 00:00:20815 std::vector<content::WebContents*> new_contents;
816 std::vector<content::WebContents*> old_contents;
[email protected]5d98294912012-06-27 22:57:40817
Dana Frieda391e6ce2019-05-08 00:00:20818 switch (change.type()) {
819 case TabStripModelChange::kInserted:
820 for (const auto& contents : change.GetInsert()->contents)
821 new_contents.push_back(contents.contents);
822 break;
823 case TabStripModelChange::kReplaced: {
824 auto* replace = change.GetReplace();
825 new_contents.push_back(replace->new_contents);
826 old_contents.push_back(replace->old_contents);
827 break;
sangwoo.ko1ae265f12018-10-18 08:30:28828 }
Dana Frieda391e6ce2019-05-08 00:00:20829 case TabStripModelChange::kRemoved:
830 for (const auto& contents : change.GetRemove()->contents)
831 old_contents.push_back(contents.contents);
832 break;
833 default:
834 break;
sangwoo.ko1ae265f12018-10-18 08:30:28835 }
Dana Frieda391e6ce2019-05-08 00:00:20836
837 for (auto* contents : old_contents)
838 RemoveInterstitialObservers(contents);
839 for (auto* contents : new_contents)
840 AddInterstitialObservers(contents);
[email protected]5d98294912012-06-27 22:57:40841}
842
[email protected]3cac87232012-11-20 01:48:27843void BrowserCommandController::TabBlockedStateChanged(
844 content::WebContents* contents,
845 int index) {
846 PrintingStateChanged();
847 FullscreenStateChanged();
848 UpdateCommandsForFind();
apacible45cbfc92015-09-28 22:45:41849 UpdateCommandsForMediaRouter();
[email protected]3cac87232012-11-20 01:48:27850}
851
[email protected]5d98294912012-06-27 22:57:40852////////////////////////////////////////////////////////////////////////////////
853// BrowserCommandController, TabRestoreServiceObserver implementation:
854
855void BrowserCommandController::TabRestoreServiceChanged(
blundell74001adc2015-09-18 11:04:25856 sessions::TabRestoreService* service) {
[email protected]2e9369e2014-08-15 09:12:53857 UpdateTabRestoreCommandState();
[email protected]5d98294912012-06-27 22:57:40858}
859
860void BrowserCommandController::TabRestoreServiceDestroyed(
blundell74001adc2015-09-18 11:04:25861 sessions::TabRestoreService* service) {
[email protected]5d98294912012-06-27 22:57:40862 service->RemoveObserver(this);
863}
864
[email protected]2e9369e2014-08-15 09:12:53865void BrowserCommandController::TabRestoreServiceLoaded(
blundell74001adc2015-09-18 11:04:25866 sessions::TabRestoreService* service) {
[email protected]2e9369e2014-08-15 09:12:53867 UpdateTabRestoreCommandState();
868}
869
[email protected]5d98294912012-06-27 22:57:40870////////////////////////////////////////////////////////////////////////////////
[email protected]5d98294912012-06-27 22:57:40871// BrowserCommandController, private:
872
[email protected]20ca0382013-02-28 19:50:07873class BrowserCommandController::InterstitialObserver
874 : public content::WebContentsObserver {
875 public:
876 InterstitialObserver(BrowserCommandController* controller,
877 content::WebContents* web_contents)
Aran Gilmand217b5b2019-04-11 17:45:43878 : WebContentsObserver(web_contents), controller_(controller) {}
[email protected]20ca0382013-02-28 19:50:07879
dcheng5dd5ff62014-10-21 12:42:38880 void DidAttachInterstitialPage() override {
[email protected]20ca0382013-02-28 19:50:07881 controller_->UpdateCommandsForTabState();
882 }
883
dcheng5dd5ff62014-10-21 12:42:38884 void DidDetachInterstitialPage() override {
[email protected]20ca0382013-02-28 19:50:07885 controller_->UpdateCommandsForTabState();
886 }
887
888 private:
889 BrowserCommandController* controller_;
890
891 DISALLOW_COPY_AND_ASSIGN(InterstitialObserver);
892};
893
[email protected]6a414ff2013-02-27 08:22:54894bool BrowserCommandController::IsShowingMainUI() {
manukf1a116392018-10-25 16:28:24895 return browser_->SupportsWindowFeature(Browser::FEATURE_TABSTRIP);
896}
897
898bool BrowserCommandController::IsShowingLocationBar() {
899 return browser_->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR);
[email protected]5d98294912012-06-27 22:57:40900}
901
902void BrowserCommandController::InitCommandState() {
903 // All browser commands whose state isn't set automagically some other way
904 // (like Back & Forward with initial page load) must have their state
905 // initialized here, otherwise they will be forever disabled.
906
Ivan Sandrk9669d0e2017-12-15 23:50:20907 if (is_locked_fullscreen_)
908 return;
909
[email protected]5d98294912012-06-27 22:57:40910 // Navigation commands
911 command_updater_.UpdateCommandEnabled(IDC_RELOAD, true);
toyoshim7dad4b1182016-04-01 14:28:05912 command_updater_.UpdateCommandEnabled(IDC_RELOAD_BYPASSING_CACHE, true);
[email protected]58e29032012-08-06 20:19:57913 command_updater_.UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE, true);
[email protected]5d98294912012-06-27 22:57:40914
915 // Window management commands
916 command_updater_.UpdateCommandEnabled(IDC_CLOSE_WINDOW, true);
917 command_updater_.UpdateCommandEnabled(IDC_NEW_TAB, true);
918 command_updater_.UpdateCommandEnabled(IDC_CLOSE_TAB, true);
919 command_updater_.UpdateCommandEnabled(IDC_DUPLICATE_TAB, true);
[email protected]2e9369e2014-08-15 09:12:53920 UpdateTabRestoreCommandState();
[email protected]d28d3782013-02-26 16:31:55921 command_updater_.UpdateCommandEnabled(IDC_EXIT, true);
[email protected]5d98294912012-06-27 22:57:40922 command_updater_.UpdateCommandEnabled(IDC_DEBUG_FRAME_TOGGLE, true);
[email protected]d12cc5e2013-10-19 18:25:06923#if defined(OS_CHROMEOS)
James Cook934abaf2017-09-19 22:21:58924 command_updater_.UpdateCommandEnabled(IDC_MINIMIZE_WINDOW, true);
[email protected]d12cc5e2013-10-19 18:25:06925 command_updater_.UpdateCommandEnabled(IDC_VISIT_DESKTOP_OF_LRU_USER_2, true);
926 command_updater_.UpdateCommandEnabled(IDC_VISIT_DESKTOP_OF_LRU_USER_3, true);
927#endif
[email protected]893124a22014-04-15 00:45:28928#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
Tom Andersonf15ede502017-11-10 03:17:15929 command_updater_.UpdateCommandEnabled(IDC_MINIMIZE_WINDOW, true);
930 command_updater_.UpdateCommandEnabled(IDC_MAXIMIZE_WINDOW, true);
931 command_updater_.UpdateCommandEnabled(IDC_RESTORE_WINDOW, true);
Maksim Sisovac767592018-08-10 08:00:15932 bool use_system_title_bar = true;
933#if defined(USE_OZONE)
934 use_system_title_bar = ui::OzonePlatform::GetInstance()
935 ->GetPlatformProperties()
936 .use_system_title_bar;
937#endif
938 command_updater_.UpdateCommandEnabled(IDC_USE_SYSTEM_TITLE_BAR,
939 use_system_title_bar);
[email protected]af97be4c62014-02-13 14:43:34940#endif
Alan Cutter09965802018-03-27 07:25:29941 command_updater_.UpdateCommandEnabled(IDC_OPEN_IN_PWA_WINDOW, true);
[email protected]5d98294912012-06-27 22:57:40942
943 // Page-related commands
944 command_updater_.UpdateCommandEnabled(IDC_EMAIL_PAGE_LOCATION, true);
[email protected]4bee4432014-05-05 13:11:41945 command_updater_.UpdateCommandEnabled(IDC_MANAGE_PASSWORDS_FOR_PAGE, true);
[email protected]5d98294912012-06-27 22:57:40946
947 // Zoom
948 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MENU, true);
949 command_updater_.UpdateCommandEnabled(IDC_ZOOM_PLUS, true);
[email protected]d93dbd12014-08-04 23:42:53950 command_updater_.UpdateCommandEnabled(IDC_ZOOM_NORMAL, false);
[email protected]5d98294912012-06-27 22:57:40951 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MINUS, true);
952
953 // Show various bits of UI
Aran Gilmand217b5b2019-04-11 17:45:43954 const bool guest_session =
955 profile()->IsGuestSession() || profile()->IsSystemProfile();
mlerman7831f57d2015-05-25 11:40:15956 DCHECK(!profile()->IsSystemProfile())
957 << "Ought to never have browser for the system profile.";
[email protected]338416c02014-05-13 16:47:06958 const bool normal_window = browser_->is_type_tabbed();
[email protected]05454532013-01-22 21:09:08959 UpdateOpenFileState(&command_updater_);
[email protected]5d98294912012-06-27 22:57:40960 UpdateCommandsForDevTools();
961 command_updater_.UpdateCommandEnabled(IDC_TASK_MANAGER, CanOpenTaskManager());
[email protected]338416c02014-05-13 16:47:06962 command_updater_.UpdateCommandEnabled(IDC_SHOW_HISTORY, !guest_session);
[email protected]5d98294912012-06-27 22:57:40963 command_updater_.UpdateCommandEnabled(IDC_SHOW_DOWNLOADS, true);
[email protected]674b65672014-06-02 23:21:56964 command_updater_.UpdateCommandEnabled(IDC_HELP_MENU, true);
[email protected]5d98294912012-06-27 22:57:40965 command_updater_.UpdateCommandEnabled(IDC_HELP_PAGE_VIA_KEYBOARD, true);
966 command_updater_.UpdateCommandEnabled(IDC_HELP_PAGE_VIA_MENU, true);
Bret Sepulveda2d018662017-05-18 21:31:48967 command_updater_.UpdateCommandEnabled(IDC_SHOW_BETA_FORUM, true);
[email protected]338416c02014-05-13 16:47:06968 command_updater_.UpdateCommandEnabled(IDC_BOOKMARKS_MENU, !guest_session);
Aran Gilmand217b5b2019-04-11 17:45:43969 command_updater_.UpdateCommandEnabled(
970 IDC_RECENT_TABS_MENU, !guest_session && !profile()->IsOffTheRecord());
tsergeant341a8272017-04-18 03:54:33971 command_updater_.UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA,
972 !guest_session);
[email protected]39d47592014-01-10 21:42:45973#if defined(OS_CHROMEOS)
caelyn4e4e08a2015-02-04 21:27:49974 command_updater_.UpdateCommandEnabled(IDC_TAKE_SCREENSHOT, true);
Ramin Halavatif362b7a2019-04-17 05:24:50975 // Chrome OS uses the system tray menu to handle multi-profiles. Avatar menu
976 // is only required in incognito mode.
Ramin Halavati2394dc452019-05-21 16:21:58977 if (profile()->IsIncognitoProfile())
[email protected]338416c02014-05-13 16:47:06978 command_updater_.UpdateCommandEnabled(IDC_SHOW_AVATAR_MENU, true);
Ramin Halavatif362b7a2019-04-17 05:24:50979#else
980 if (normal_window)
981 command_updater_.UpdateCommandEnabled(IDC_SHOW_AVATAR_MENU, true);
[email protected]39d47592014-01-10 21:42:45982#endif
[email protected]5d98294912012-06-27 22:57:40983
Mihai Sardarescu2ec5205682018-09-26 09:20:05984 UpdateShowSyncState(true);
985
[email protected]5d98294912012-06-27 22:57:40986 // Navigation commands
Christopher Camerone1385742019-04-05 20:37:25987 command_updater_.UpdateCommandEnabled(IDC_HOME,
988 normal_window || browser_->is_app());
[email protected]5d98294912012-06-27 22:57:40989
Alan Cutterd0bd51902019-05-22 04:15:23990 const bool is_web_app =
991 web_app::AppBrowserController::IsForWebAppBrowser(browser_);
Christopher Lam0dbac2b2017-11-14 07:12:10992 // Hosted app browser commands.
Alan Cutterd0bd51902019-05-22 04:15:23993 command_updater_.UpdateCommandEnabled(IDC_COPY_URL, is_web_app);
994 command_updater_.UpdateCommandEnabled(IDC_OPEN_IN_CHROME, is_web_app);
995 command_updater_.UpdateCommandEnabled(IDC_SITE_SETTINGS, is_web_app);
Alan Cutter0c3132302018-02-21 05:09:02996 command_updater_.UpdateCommandEnabled(IDC_HOSTED_APP_MENU_APP_INFO,
Alan Cutterd0bd51902019-05-22 04:15:23997 is_web_app);
Christopher Lam0dbac2b2017-11-14 07:12:10998
[email protected]5d98294912012-06-27 22:57:40999 // Window management commands
[email protected]5d98294912012-06-27 22:57:401000 command_updater_.UpdateCommandEnabled(IDC_SELECT_NEXT_TAB, normal_window);
Aran Gilmand217b5b2019-04-11 17:45:431001 command_updater_.UpdateCommandEnabled(IDC_SELECT_PREVIOUS_TAB, normal_window);
[email protected]5d98294912012-06-27 22:57:401002 command_updater_.UpdateCommandEnabled(IDC_MOVE_TAB_NEXT, normal_window);
1003 command_updater_.UpdateCommandEnabled(IDC_MOVE_TAB_PREVIOUS, normal_window);
1004 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_0, normal_window);
1005 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_1, normal_window);
1006 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_2, normal_window);
1007 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_3, normal_window);
1008 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_4, normal_window);
1009 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_5, normal_window);
1010 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_6, normal_window);
1011 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_7, normal_window);
1012 command_updater_.UpdateCommandEnabled(IDC_SELECT_LAST_TAB, normal_window);
[email protected]5d98294912012-06-27 22:57:401013
[email protected]338416c02014-05-13 16:47:061014 // These are always enabled; the menu determines their menu item visibility.
[email protected]5d98294912012-06-27 22:57:401015 command_updater_.UpdateCommandEnabled(IDC_UPGRADE_DIALOG, true);
[email protected]5d98294912012-06-27 22:57:401016
[email protected]6bd370b2014-05-28 14:19:471017 // Distill current page.
1018 command_updater_.UpdateCommandEnabled(
avi556c05022014-12-22 23:31:431019 IDC_DISTILL_PAGE, base::CommandLine::ForCurrentProcess()->HasSwitch(
1020 switches::kEnableDomDistiller));
[email protected]6bd370b2014-05-28 14:19:471021
Tommy Steimelc4477982017-11-29 18:07:181022 command_updater_.UpdateCommandEnabled(IDC_WINDOW_MUTE_SITE, normal_window);
ellyjones0101ba02017-05-19 15:50:261023 command_updater_.UpdateCommandEnabled(IDC_WINDOW_PIN_TAB, normal_window);
1024
[email protected]338416c02014-05-13 16:47:061025 // Initialize other commands whose state changes based on various conditions.
[email protected]32dfede2013-08-25 15:48:251026 UpdateCommandsForFullscreenMode();
[email protected]5d98294912012-06-27 22:57:401027 UpdateCommandsForContentRestrictionState();
[email protected]5d98294912012-06-27 22:57:401028 UpdateCommandsForBookmarkEditing();
[email protected]5d98294912012-06-27 22:57:401029 UpdateCommandsForIncognitoAvailability();
1030}
1031
[email protected]05454532013-01-22 21:09:081032// static
1033void BrowserCommandController::UpdateSharedCommandsForIncognitoAvailability(
1034 CommandUpdater* command_updater,
1035 Profile* profile) {
mlermane01e6de2014-09-29 19:26:471036 const bool guest_session = profile->IsGuestSession();
1037 // TODO(mlerman): Make GetAvailability account for profile->IsGuestSession().
[email protected]5d98294912012-06-27 22:57:401038 IncognitoModePrefs::Availability incognito_availability =
[email protected]05454532013-01-22 21:09:081039 IncognitoModePrefs::GetAvailability(profile->GetPrefs());
1040 command_updater->UpdateCommandEnabled(
Aran Gilmand217b5b2019-04-11 17:45:431041 IDC_NEW_WINDOW, incognito_availability != IncognitoModePrefs::FORCED);
[email protected]05454532013-01-22 21:09:081042 command_updater->UpdateCommandEnabled(
[email protected]5d98294912012-06-27 22:57:401043 IDC_NEW_INCOGNITO_WINDOW,
mlermane01e6de2014-09-29 19:26:471044 incognito_availability != IncognitoModePrefs::DISABLED && !guest_session);
[email protected]5d98294912012-06-27 22:57:401045
[email protected]57b25292014-05-01 16:31:061046 const bool forced_incognito =
1047 incognito_availability == IncognitoModePrefs::FORCED ||
1048 guest_session; // Guest always runs in Incognito mode.
[email protected]05454532013-01-22 21:09:081049 command_updater->UpdateCommandEnabled(
[email protected]5d98294912012-06-27 22:57:401050 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]57b25292014-05-01 16:31:061051 browser_defaults::bookmarks_enabled && !forced_incognito);
Devlin Cronin21dba422018-05-30 15:45:161052 extensions::ExtensionService* extension_service =
[email protected]03d25812014-06-22 19:41:551053 extensions::ExtensionSystem::Get(profile)->extension_service();
[email protected]57b25292014-05-01 16:31:061054 const bool enable_extensions =
[email protected]5d98294912012-06-27 22:57:401055 extension_service && extension_service->extensions_enabled();
[email protected]5d98294912012-06-27 22:57:401056
[email protected]57b25292014-05-01 16:31:061057 // Bookmark manager and settings page/subpages are forced to open in normal
1058 // mode. For this reason we disable these commands when incognito is forced.
1059 command_updater->UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS,
1060 enable_extensions && !forced_incognito);
1061
1062 command_updater->UpdateCommandEnabled(IDC_IMPORT_SETTINGS, !forced_incognito);
1063 command_updater->UpdateCommandEnabled(IDC_OPTIONS,
1064 !forced_incognito || guest_session);
estade8c0780f2015-08-21 23:36:411065 command_updater->UpdateCommandEnabled(IDC_SHOW_SIGNIN, !forced_incognito);
[email protected]05454532013-01-22 21:09:081066}
1067
1068void BrowserCommandController::UpdateCommandsForIncognitoAvailability() {
Ivan Sandrk9669d0e2017-12-15 23:50:201069 if (is_locked_fullscreen_)
1070 return;
1071
[email protected]05454532013-01-22 21:09:081072 UpdateSharedCommandsForIncognitoAvailability(&command_updater_, profile());
1073
[email protected]6a414ff2013-02-27 08:22:541074 if (!IsShowingMainUI()) {
[email protected]05454532013-01-22 21:09:081075 command_updater_.UpdateCommandEnabled(IDC_IMPORT_SETTINGS, false);
1076 command_updater_.UpdateCommandEnabled(IDC_OPTIONS, false);
1077 }
[email protected]5d98294912012-06-27 22:57:401078}
1079
1080void BrowserCommandController::UpdateCommandsForTabState() {
Ivan Sandrk9669d0e2017-12-15 23:50:201081 if (is_locked_fullscreen_)
1082 return;
1083
[email protected]617ee962013-01-29 20:49:121084 WebContents* current_web_contents =
1085 browser_->tab_strip_model()->GetActiveWebContents();
[email protected]1c5119c2012-09-19 00:08:571086 if (!current_web_contents) // May be NULL during tab restore.
[email protected]5d98294912012-06-27 22:57:401087 return;
[email protected]5d98294912012-06-27 22:57:401088
1089 // Navigation commands
1090 command_updater_.UpdateCommandEnabled(IDC_BACK, CanGoBack(browser_));
1091 command_updater_.UpdateCommandEnabled(IDC_FORWARD, CanGoForward(browser_));
1092 command_updater_.UpdateCommandEnabled(IDC_RELOAD, CanReload(browser_));
toyoshim7dad4b1182016-04-01 14:28:051093 command_updater_.UpdateCommandEnabled(IDC_RELOAD_BYPASSING_CACHE,
[email protected]5d98294912012-06-27 22:57:401094 CanReload(browser_));
[email protected]58e29032012-08-06 20:19:571095 command_updater_.UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE,
1096 CanReload(browser_));
[email protected]5d98294912012-06-27 22:57:401097
1098 // Window management commands
Aran Gilmand217b5b2019-04-11 17:45:431099 command_updater_.UpdateCommandEnabled(
1100 IDC_DUPLICATE_TAB, !browser_->is_app() && CanDuplicateTab(browser_));
Tommy Steimelc4477982017-11-29 18:07:181101 command_updater_.UpdateCommandEnabled(IDC_WINDOW_MUTE_SITE,
ellyjones0101ba02017-05-19 15:50:261102 !browser_->is_app());
1103 command_updater_.UpdateCommandEnabled(IDC_WINDOW_PIN_TAB,
1104 !browser_->is_app());
[email protected]5d98294912012-06-27 22:57:401105
1106 // Page-related commands
1107 window()->SetStarredState(
[email protected]1c5119c2012-09-19 00:08:571108 BookmarkTabHelper::FromWebContents(current_web_contents)->is_starred());
[email protected]5423c372012-08-22 05:50:161109 window()->ZoomChangedForActiveTab(false);
[email protected]5d98294912012-06-27 22:57:401110 command_updater_.UpdateCommandEnabled(IDC_VIEW_SOURCE,
1111 CanViewSource(browser_));
1112 command_updater_.UpdateCommandEnabled(IDC_EMAIL_PAGE_LOCATION,
1113 CanEmailPageLocation(browser_));
1114 if (browser_->is_devtools())
1115 command_updater_.UpdateCommandEnabled(IDC_OPEN_FILE, false);
1116
Alan Cutter81ac726682018-09-25 00:18:361117 bool can_create_bookmark_app = CanCreateBookmarkApp(browser_);
1118 command_updater_.UpdateCommandEnabled(IDC_INSTALL_PWA,
1119 can_create_bookmark_app);
1120 command_updater_.UpdateCommandEnabled(IDC_CREATE_SHORTCUT,
1121 can_create_bookmark_app);
Alan Cutter403cebb2018-05-17 05:22:451122 command_updater_.UpdateCommandEnabled(IDC_OPEN_IN_PWA_WINDOW,
Alan Cutter81ac726682018-09-25 00:18:361123 can_create_bookmark_app);
[email protected]5d98294912012-06-27 22:57:401124
[email protected]9b7ab882012-09-10 23:46:361125 command_updater_.UpdateCommandEnabled(
1126 IDC_TOGGLE_REQUEST_TABLET_SITE,
1127 CanRequestTabletSite(current_web_contents));
1128
[email protected]5d98294912012-06-27 22:57:401129 UpdateCommandsForContentRestrictionState();
1130 UpdateCommandsForBookmarkEditing();
[email protected]3cac87232012-11-20 01:48:271131 UpdateCommandsForFind();
apacible45cbfc92015-09-28 22:45:411132 UpdateCommandsForMediaRouter();
[email protected]d93dbd12014-08-04 23:42:531133 // Update the zoom commands when an active tab is selected.
1134 UpdateCommandsForZoomState();
1135}
1136
1137void BrowserCommandController::UpdateCommandsForZoomState() {
Aran Gilmand217b5b2019-04-11 17:45:431138 WebContents* contents = browser_->tab_strip_model()->GetActiveWebContents();
[email protected]d93dbd12014-08-04 23:42:531139 if (!contents)
1140 return;
Aran Gilmand217b5b2019-04-11 17:45:431141 command_updater_.UpdateCommandEnabled(IDC_ZOOM_PLUS, CanZoomIn(contents));
a.sarkar.arundaadc712015-02-26 05:39:081142 command_updater_.UpdateCommandEnabled(IDC_ZOOM_NORMAL,
ccameronb7c1d6c2015-03-09 17:08:241143 CanResetZoom(contents));
Aran Gilmand217b5b2019-04-11 17:45:431144 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MINUS, CanZoomOut(contents));
[email protected]5d98294912012-06-27 22:57:401145}
1146
1147void BrowserCommandController::UpdateCommandsForContentRestrictionState() {
1148 int restrictions = GetContentRestrictions(browser_);
1149
1150 command_updater_.UpdateCommandEnabled(
[email protected]3c71576ce2013-07-23 02:00:011151 IDC_COPY, !(restrictions & CONTENT_RESTRICTION_COPY));
[email protected]5d98294912012-06-27 22:57:401152 command_updater_.UpdateCommandEnabled(
[email protected]3c71576ce2013-07-23 02:00:011153 IDC_CUT, !(restrictions & CONTENT_RESTRICTION_CUT));
[email protected]5d98294912012-06-27 22:57:401154 command_updater_.UpdateCommandEnabled(
[email protected]3c71576ce2013-07-23 02:00:011155 IDC_PASTE, !(restrictions & CONTENT_RESTRICTION_PASTE));
[email protected]5d98294912012-06-27 22:57:401156 UpdateSaveAsState();
1157 UpdatePrintingState();
1158}
1159
1160void BrowserCommandController::UpdateCommandsForDevTools() {
Ivan Sandrk9669d0e2017-12-15 23:50:201161 if (is_locked_fullscreen_)
1162 return;
1163
Pavol Marko56df0272018-07-04 17:55:041164 bool dev_tools_enabled = DevToolsWindow::AllowDevToolsFor(
1165 profile(), browser_->tab_strip_model()->GetActiveWebContents());
Aran Gilmand217b5b2019-04-11 17:45:431166 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS, dev_tools_enabled);
[email protected]5d98294912012-06-27 22:57:401167 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_CONSOLE,
1168 dev_tools_enabled);
[email protected]2056c3192013-10-21 22:40:511169 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_DEVICES,
1170 dev_tools_enabled);
[email protected]5d98294912012-06-27 22:57:401171 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_INSPECT,
1172 dev_tools_enabled);
[email protected]d16657c2012-09-03 14:25:101173 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_TOGGLE,
1174 dev_tools_enabled);
spqchan2c5d541e2017-10-25 07:07:111175#if defined(OS_MACOSX)
1176 command_updater_.UpdateCommandEnabled(IDC_TOGGLE_JAVASCRIPT_APPLE_EVENTS,
1177 dev_tools_enabled);
1178#endif
[email protected]5d98294912012-06-27 22:57:401179}
1180
1181void BrowserCommandController::UpdateCommandsForBookmarkEditing() {
Ivan Sandrk9669d0e2017-12-15 23:50:201182 if (is_locked_fullscreen_)
1183 return;
1184
[email protected]5d98294912012-06-27 22:57:401185 command_updater_.UpdateCommandEnabled(IDC_BOOKMARK_PAGE,
1186 CanBookmarkCurrentPage(browser_));
1187 command_updater_.UpdateCommandEnabled(IDC_BOOKMARK_ALL_TABS,
1188 CanBookmarkAllTabs(browser_));
thestig5149d272014-10-30 07:25:291189#if defined(OS_WIN)
1190 command_updater_.UpdateCommandEnabled(IDC_PIN_TO_START_SCREEN, true);
1191#endif
[email protected]5d98294912012-06-27 22:57:401192}
1193
1194void BrowserCommandController::UpdateCommandsForBookmarkBar() {
Ivan Sandrk9669d0e2017-12-15 23:50:201195 if (is_locked_fullscreen_)
1196 return;
1197
tfarina3bddbe112014-08-28 05:29:321198 command_updater_.UpdateCommandEnabled(
Aran Gilmand217b5b2019-04-11 17:45:431199 IDC_SHOW_BOOKMARK_BAR, browser_defaults::bookmarks_enabled &&
1200 !profile()->IsGuestSession() &&
1201 !profile()->IsSystemProfile() &&
1202 !profile()->GetPrefs()->IsManagedPreference(
1203 bookmarks::prefs::kShowBookmarkBar) &&
1204 IsShowingMainUI());
[email protected]5d98294912012-06-27 22:57:401205}
1206
[email protected]9ec3ea5b2012-12-03 18:14:301207void BrowserCommandController::UpdateCommandsForFileSelectionDialogs() {
Ivan Sandrk9669d0e2017-12-15 23:50:201208 if (is_locked_fullscreen_)
1209 return;
1210
[email protected]9ec3ea5b2012-12-03 18:14:301211 UpdateSaveAsState();
[email protected]05454532013-01-22 21:09:081212 UpdateOpenFileState(&command_updater_);
[email protected]9ec3ea5b2012-12-03 18:14:301213}
1214
[email protected]32dfede2013-08-25 15:48:251215void BrowserCommandController::UpdateCommandsForFullscreenMode() {
Ivan Sandrk9669d0e2017-12-15 23:50:201216 if (is_locked_fullscreen_)
1217 return;
1218
zijiehe3c7af992017-02-12 20:59:401219 const bool is_fullscreen = window() && window()->IsFullscreen();
1220 const bool show_main_ui = IsShowingMainUI();
manukf1a116392018-10-25 16:28:241221 const bool show_location_bar = IsShowingLocationBar();
1222
zijiehe3c7af992017-02-12 20:59:401223 const bool main_not_fullscreen = show_main_ui && !is_fullscreen;
[email protected]5d98294912012-06-27 22:57:401224
1225 // Navigation commands
1226 command_updater_.UpdateCommandEnabled(IDC_OPEN_CURRENT_URL, show_main_ui);
1227
1228 // Window management commands
1229 command_updater_.UpdateCommandEnabled(
Aran Gilmand217b5b2019-04-11 17:45:431230 IDC_SHOW_AS_TAB, !browser_->is_type_tabbed() && !is_fullscreen);
[email protected]5d98294912012-06-27 22:57:401231
1232 // Focus various bits of UI
1233 command_updater_.UpdateCommandEnabled(IDC_FOCUS_TOOLBAR, show_main_ui);
manukf1a116392018-10-25 16:28:241234 command_updater_.UpdateCommandEnabled(IDC_FOCUS_LOCATION, show_location_bar);
[email protected]5d98294912012-06-27 22:57:401235 command_updater_.UpdateCommandEnabled(IDC_FOCUS_SEARCH, show_main_ui);
Aran Gilmand217b5b2019-04-11 17:45:431236 command_updater_.UpdateCommandEnabled(IDC_FOCUS_MENU_BAR,
1237 main_not_fullscreen);
1238 command_updater_.UpdateCommandEnabled(IDC_FOCUS_NEXT_PANE,
1239 main_not_fullscreen);
1240 command_updater_.UpdateCommandEnabled(IDC_FOCUS_PREVIOUS_PANE,
1241 main_not_fullscreen);
1242 command_updater_.UpdateCommandEnabled(IDC_FOCUS_BOOKMARKS,
1243 main_not_fullscreen);
[email protected]822ca8c62013-04-19 00:49:151244 command_updater_.UpdateCommandEnabled(
David Tsengc0b1b642018-01-24 07:12:271245 IDC_FOCUS_INACTIVE_POPUP_FOR_ACCESSIBILITY, main_not_fullscreen);
[email protected]5d98294912012-06-27 22:57:401246
1247 // Show various bits of UI
1248 command_updater_.UpdateCommandEnabled(IDC_DEVELOPER_MENU, show_main_ui);
[email protected]236ad3022013-09-04 03:27:431249#if defined(GOOGLE_CHROME_BUILD)
Fabio Rocha19bf4262019-04-04 01:05:181250 command_updater_.UpdateCommandEnabled(
1251 IDC_FEEDBACK, show_main_ui || browser_->is_devtools());
[email protected]236ad3022013-09-04 03:27:431252#endif
Mihai Sardarescu2ec5205682018-09-26 09:20:051253 UpdateShowSyncState(show_main_ui);
[email protected]5d98294912012-06-27 22:57:401254
[email protected]5d98294912012-06-27 22:57:401255 command_updater_.UpdateCommandEnabled(IDC_EDIT_SEARCH_ENGINES, show_main_ui);
1256 command_updater_.UpdateCommandEnabled(IDC_VIEW_PASSWORDS, show_main_ui);
1257 command_updater_.UpdateCommandEnabled(IDC_ABOUT, show_main_ui);
1258 command_updater_.UpdateCommandEnabled(IDC_SHOW_APP_MENU, show_main_ui);
Tina Wangf09681ac2019-03-27 20:52:101259 command_updater_.UpdateCommandEnabled(IDC_SEND_TAB_TO_SELF, show_main_ui);
Tina Wangfa016992019-05-31 00:01:151260 command_updater_.UpdateCommandEnabled(IDC_SEND_TAB_TO_SELF_SINGLE_TARGET,
1261 show_main_ui);
Yann Dago70ca3dd2019-02-27 01:12:351262 command_updater_.UpdateCommandEnabled(IDC_SHOW_MANAGEMENT_PAGE, true);
primianod3a81ab2016-01-25 22:21:151263
1264 if (base::debug::IsProfilingSupported())
1265 command_updater_.UpdateCommandEnabled(IDC_PROFILING_ENABLED, show_main_ui);
[email protected]5d98294912012-06-27 22:57:401266
[email protected]aeafc3852014-04-29 16:51:291267#if !defined(OS_MACOSX)
zijiehe3c7af992017-02-12 20:59:401268 // Disable toggling into fullscreen mode if disallowed by pref.
Aran Gilmand217b5b2019-04-11 17:45:431269 const bool fullscreen_enabled =
1270 is_fullscreen ||
zijiehe3c7af992017-02-12 20:59:401271 profile()->GetPrefs()->GetBoolean(prefs::kFullscreenAllowed);
1272#else
1273 const bool fullscreen_enabled = true;
[email protected]00a1cc5b2012-11-07 13:44:511274#endif
1275
1276 command_updater_.UpdateCommandEnabled(IDC_FULLSCREEN, fullscreen_enabled);
spqchanb8ffc7d2015-11-17 01:17:211277 command_updater_.UpdateCommandEnabled(IDC_TOGGLE_FULLSCREEN_TOOLBAR,
1278 fullscreen_enabled);
[email protected]5d98294912012-06-27 22:57:401279
1280 UpdateCommandsForBookmarkBar();
zijiehedb473d552017-02-24 01:13:411281 UpdateCommandsForIncognitoAvailability();
Alan Cutter167dfe82018-04-11 09:06:001282 UpdateCommandsForHostedAppAvailability();
1283}
1284
1285void BrowserCommandController::UpdateCommandsForHostedAppAvailability() {
1286 bool has_toolbar =
1287 browser_->is_type_tabbed() ||
Alan Cutterd0bd51902019-05-22 04:15:231288 web_app::AppBrowserController::IsForWebAppBrowser(browser_);
Alan Cutter167dfe82018-04-11 09:06:001289 if (window() && window()->ShouldHideUIForFullscreen())
1290 has_toolbar = false;
1291 command_updater_.UpdateCommandEnabled(IDC_FOCUS_TOOLBAR, has_toolbar);
1292 command_updater_.UpdateCommandEnabled(IDC_FOCUS_NEXT_PANE, has_toolbar);
1293 command_updater_.UpdateCommandEnabled(IDC_FOCUS_PREVIOUS_PANE, has_toolbar);
1294 command_updater_.UpdateCommandEnabled(IDC_SHOW_APP_MENU, has_toolbar);
[email protected]5d98294912012-06-27 22:57:401295}
1296
Ivan Sandrk9669d0e2017-12-15 23:50:201297#if defined(OS_CHROMEOS)
1298namespace {
1299
1300#if DCHECK_IS_ON()
1301// Makes sure that all commands that are not whitelisted are disabled. DCHECKs
1302// otherwise. Compiled only in debug mode.
1303void NonWhitelistedCommandsAreDisabled(CommandUpdaterImpl* command_updater) {
Ivan Sandrk6ac9c9a2018-12-14 14:15:071304 constexpr int kWhitelistedIds[] = {IDC_CUT, IDC_COPY, IDC_PASTE};
Ivan Sandrk9669d0e2017-12-15 23:50:201305
1306 // Go through all the command ids, skip the whitelisted ones.
1307 for (int id : command_updater->GetAllIds()) {
Jan Wilken Dörriea8cb56302019-06-06 18:59:361308 if (base::Contains(kWhitelistedIds, id)) {
Ivan Sandrk9669d0e2017-12-15 23:50:201309 continue;
1310 }
1311 DCHECK(!command_updater->IsCommandEnabled(id));
1312 }
1313}
1314#endif
1315
1316} // namespace
1317
1318void BrowserCommandController::UpdateCommandsForLockedFullscreenMode() {
Ivan Sandrke0e92452019-03-21 09:32:481319 bool is_locked_fullscreen =
1320 platform_util::IsBrowserLockedFullscreen(browser_);
Ivan Sandrk9669d0e2017-12-15 23:50:201321 // Sanity check to make sure this function is called only on state change.
1322 DCHECK_NE(is_locked_fullscreen, is_locked_fullscreen_);
1323 if (is_locked_fullscreen == is_locked_fullscreen_)
1324 return;
1325 is_locked_fullscreen_ = is_locked_fullscreen;
1326
1327 if (is_locked_fullscreen_) {
1328 command_updater_.DisableAllCommands();
1329 // Update the state of whitelisted commands:
1330 // IDC_CUT/IDC_COPY/IDC_PASTE,
1331 UpdateCommandsForContentRestrictionState();
Ivan Sandrk6ac9c9a2018-12-14 14:15:071332 // TODO(crbug.com/904637): Re-enable Find and Zoom in locked fullscreen.
Ivan Sandrk9669d0e2017-12-15 23:50:201333 // All other commands will be disabled (there is an early return in their
1334 // corresponding UpdateCommandsFor* functions).
1335#if DCHECK_IS_ON()
1336 NonWhitelistedCommandsAreDisabled(&command_updater_);
1337#endif
1338 } else {
1339 // Do an init call to re-initialize command state after the
1340 // DisableAllCommands.
1341 InitCommandState();
1342 }
1343}
1344#endif
1345
[email protected]5d98294912012-06-27 22:57:401346void BrowserCommandController::UpdatePrintingState() {
Ivan Sandrk9669d0e2017-12-15 23:50:201347 if (is_locked_fullscreen_)
1348 return;
1349
[email protected]d53e4032012-06-29 18:58:341350 bool print_enabled = CanPrint(browser_);
1351 command_updater_.UpdateCommandEnabled(IDC_PRINT, print_enabled);
Lei Zhang48a4a5262018-04-17 20:18:441352#if BUILDFLAG(ENABLE_PRINTING)
vitalybukaf9433e42014-09-08 10:04:551353 command_updater_.UpdateCommandEnabled(IDC_BASIC_PRINT,
1354 CanBasicPrint(browser_));
Lei Zhang48a4a5262018-04-17 20:18:441355#endif
[email protected]5d98294912012-06-27 22:57:401356}
1357
1358void BrowserCommandController::UpdateSaveAsState() {
Ivan Sandrk9669d0e2017-12-15 23:50:201359 if (is_locked_fullscreen_)
1360 return;
1361
[email protected]5d98294912012-06-27 22:57:401362 command_updater_.UpdateCommandEnabled(IDC_SAVE_PAGE, CanSavePage(browser_));
1363}
1364
Mihai Sardarescu2ec5205682018-09-26 09:20:051365void BrowserCommandController::UpdateShowSyncState(bool show_main_ui) {
1366 if (is_locked_fullscreen_)
1367 return;
1368
Mihai Sardarescuf87ccc12018-09-26 10:37:451369 command_updater_.UpdateCommandEnabled(
Mihai Sardarescu290bc882018-10-12 10:48:141370 IDC_SHOW_SIGNIN, show_main_ui && pref_signin_allowed_.GetValue());
Mihai Sardarescu2ec5205682018-09-26 09:20:051371}
1372
[email protected]05454532013-01-22 21:09:081373// static
1374void BrowserCommandController::UpdateOpenFileState(
1375 CommandUpdater* command_updater) {
[email protected]5d98294912012-06-27 22:57:401376 bool enabled = true;
1377 PrefService* local_state = g_browser_process->local_state();
1378 if (local_state)
1379 enabled = local_state->GetBoolean(prefs::kAllowFileSelectionDialogs);
1380
[email protected]05454532013-01-22 21:09:081381 command_updater->UpdateCommandEnabled(IDC_OPEN_FILE, enabled);
[email protected]5d98294912012-06-27 22:57:401382}
1383
1384void BrowserCommandController::UpdateReloadStopState(bool is_loading,
1385 bool force) {
Ivan Sandrk9669d0e2017-12-15 23:50:201386 if (is_locked_fullscreen_)
1387 return;
1388
[email protected]5d98294912012-06-27 22:57:401389 window()->UpdateReloadStopState(is_loading, force);
1390 command_updater_.UpdateCommandEnabled(IDC_STOP, is_loading);
Jeff Fisher6cc1ce7b2019-06-28 23:02:381391 UpdateCloseFindOrStop();
[email protected]5d98294912012-06-27 22:57:401392}
1393
[email protected]2e9369e2014-08-15 09:12:531394void BrowserCommandController::UpdateTabRestoreCommandState() {
Ivan Sandrk9669d0e2017-12-15 23:50:201395 if (is_locked_fullscreen_)
1396 return;
1397
blundell74001adc2015-09-18 11:04:251398 sessions::TabRestoreService* tab_restore_service =
[email protected]2e9369e2014-08-15 09:12:531399 TabRestoreServiceFactory::GetForProfile(profile());
1400 // The command is enabled if the service hasn't loaded yet to trigger loading.
1401 // The command is updated once the load completes.
1402 command_updater_.UpdateCommandEnabled(
1403 IDC_RESTORE_TAB,
1404 tab_restore_service &&
Aran Gilmand217b5b2019-04-11 17:45:431405 (!tab_restore_service->IsLoaded() ||
1406 GetRestoreTabType(browser_) != TabStripModelDelegate::RESTORE_NONE));
[email protected]2e9369e2014-08-15 09:12:531407}
1408
[email protected]3cac87232012-11-20 01:48:271409void BrowserCommandController::UpdateCommandsForFind() {
1410 TabStripModel* model = browser_->tab_strip_model();
Aran Gilmand217b5b2019-04-11 17:45:431411 bool enabled =
1412 !model->IsTabBlocked(model->active_index()) && !browser_->is_devtools();
[email protected]3cac87232012-11-20 01:48:271413
1414 command_updater_.UpdateCommandEnabled(IDC_FIND, enabled);
1415 command_updater_.UpdateCommandEnabled(IDC_FIND_NEXT, enabled);
1416 command_updater_.UpdateCommandEnabled(IDC_FIND_PREVIOUS, enabled);
1417}
1418
Jeff Fisher6cc1ce7b2019-06-28 23:02:381419void BrowserCommandController::UpdateCloseFindOrStop() {
1420 bool enabled = CanCloseFind(browser_) || IsCommandEnabled(IDC_STOP);
1421 command_updater_.UpdateCommandEnabled(IDC_CLOSE_FIND_OR_STOP, enabled);
1422}
1423
apacible45cbfc92015-09-28 22:45:411424void BrowserCommandController::UpdateCommandsForMediaRouter() {
Ivan Sandrk9669d0e2017-12-15 23:50:201425 if (is_locked_fullscreen_)
1426 return;
1427
apacible45cbfc92015-09-28 22:45:411428 command_updater_.UpdateCommandEnabled(IDC_ROUTE_MEDIA,
1429 CanRouteMedia(browser_));
1430}
1431
[email protected]409ea2972012-11-10 19:54:431432void BrowserCommandController::AddInterstitialObservers(WebContents* contents) {
[email protected]20ca0382013-02-28 19:50:071433 interstitial_observers_.push_back(new InterstitialObserver(this, contents));
[email protected]5d98294912012-06-27 22:57:401434}
1435
1436void BrowserCommandController::RemoveInterstitialObservers(
[email protected]e89cfcb2012-11-11 14:47:241437 WebContents* contents) {
[email protected]20ca0382013-02-28 19:50:071438 for (size_t i = 0; i < interstitial_observers_.size(); i++) {
1439 if (interstitial_observers_[i]->web_contents() != contents)
1440 continue;
1441
1442 delete interstitial_observers_[i];
1443 interstitial_observers_.erase(interstitial_observers_.begin() + i);
1444 return;
1445 }
[email protected]5d98294912012-06-27 22:57:401446}
1447
1448BrowserWindow* BrowserCommandController::window() {
1449 return browser_->window();
1450}
1451
1452Profile* BrowserCommandController::profile() {
1453 return browser_->profile();
1454}
1455
[email protected]5d98294912012-06-27 22:57:401456} // namespace chrome