blob: bbfb1ce1ab3f55f24a1d77795a7cc0353e2d578b [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,
110 views::Widget::ClosedReason /* unused */,
111 bool reload_prompt) {
112 if (reload_prompt)
113 return;
114
115 // Ensure that the web contents handle we have is still valid. It's possible
116 // (though unlikely) that either the browser or web contents has been pulled
117 // out from underneath us.
118 Browser* const browser = chrome::FindBrowserWithWebContents(web_contents);
119 if (!browser)
120 return;
121
122 // We want to focus the active web contents, which again, might not be the
123 // original web contents (though it should be the vast majority of the time).
124 content::WebContents* const active_contents =
125 browser->tab_strip_model()->GetActiveWebContents();
126 if (active_contents)
127 active_contents->Focus();
128}
129
130} // namespace
131
[email protected]5d98294912012-06-27 22:57:40132///////////////////////////////////////////////////////////////////////////////
133// BrowserCommandController, public:
134
[email protected]338416c02014-05-13 16:47:06135BrowserCommandController::BrowserCommandController(Browser* browser)
Aran Gilmand217b5b2019-04-11 17:45:43136 : browser_(browser), command_updater_(nullptr) {
[email protected]5d98294912012-06-27 22:57:40137 browser_->tab_strip_model()->AddObserver(this);
138 PrefService* local_state = g_browser_process->local_state();
139 if (local_state) {
140 local_pref_registrar_.Init(local_state);
[email protected]9ec3ea5b2012-12-03 18:14:30141 local_pref_registrar_.Add(
142 prefs::kAllowFileSelectionDialogs,
143 base::Bind(
144 &BrowserCommandController::UpdateCommandsForFileSelectionDialogs,
145 base::Unretained(this)));
[email protected]5d98294912012-06-27 22:57:40146 }
147
148 profile_pref_registrar_.Init(profile()->GetPrefs());
[email protected]9ec3ea5b2012-12-03 18:14:30149 profile_pref_registrar_.Add(
Pavol Markodebb0ff2018-05-07 18:35:41150 prefs::kDevToolsAvailability,
[email protected]9ec3ea5b2012-12-03 18:14:30151 base::Bind(&BrowserCommandController::UpdateCommandsForDevTools,
152 base::Unretained(this)));
153 profile_pref_registrar_.Add(
tfarina3bddbe112014-08-28 05:29:32154 bookmarks::prefs::kEditBookmarksEnabled,
[email protected]9ec3ea5b2012-12-03 18:14:30155 base::Bind(&BrowserCommandController::UpdateCommandsForBookmarkEditing,
156 base::Unretained(this)));
157 profile_pref_registrar_.Add(
tfarina3bddbe112014-08-28 05:29:32158 bookmarks::prefs::kShowBookmarkBar,
[email protected]9ec3ea5b2012-12-03 18:14:30159 base::Bind(&BrowserCommandController::UpdateCommandsForBookmarkBar,
160 base::Unretained(this)));
161 profile_pref_registrar_.Add(
162 prefs::kIncognitoModeAvailability,
163 base::Bind(
164 &BrowserCommandController::UpdateCommandsForIncognitoAvailability,
165 base::Unretained(this)));
166 profile_pref_registrar_.Add(
167 prefs::kPrintingEnabled,
168 base::Bind(&BrowserCommandController::UpdatePrintingState,
169 base::Unretained(this)));
[email protected]32dfede2013-08-25 15:48:25170#if !defined(OS_MACOSX)
171 profile_pref_registrar_.Add(
172 prefs::kFullscreenAllowed,
173 base::Bind(&BrowserCommandController::UpdateCommandsForFullscreenMode,
174 base::Unretained(this)));
175#endif
Mihai Sardarescuf87ccc12018-09-26 10:37:45176 pref_signin_allowed_.Init(
Aran Gilmand217b5b2019-04-11 17:45:43177 prefs::kSigninAllowed, profile()->GetOriginalProfile()->GetPrefs(),
Mihai Sardarescuf87ccc12018-09-26 10:37:45178 base::Bind(&BrowserCommandController::OnSigninAllowedPrefChange,
179 base::Unretained(this)));
[email protected]5d98294912012-06-27 22:57:40180
181 InitCommandState();
182
blundell74001adc2015-09-18 11:04:25183 sessions::TabRestoreService* tab_restore_service =
[email protected]5d98294912012-06-27 22:57:40184 TabRestoreServiceFactory::GetForProfile(profile());
185 if (tab_restore_service) {
186 tab_restore_service->AddObserver(this);
Cliff Smolinsky2c9586e2019-02-08 18:48:30187 if (!tab_restore_service->IsLoaded())
188 tab_restore_service->LoadTabsFromLastSession();
[email protected]5d98294912012-06-27 22:57:40189 }
[email protected]5d98294912012-06-27 22:57:40190}
191
192BrowserCommandController::~BrowserCommandController() {
[email protected]95e39472012-10-05 23:37:36193 // TabRestoreService may have been shutdown by the time we get here. Don't
194 // trigger creating it.
blundell74001adc2015-09-18 11:04:25195 sessions::TabRestoreService* tab_restore_service =
[email protected]95e39472012-10-05 23:37:36196 TabRestoreServiceFactory::GetForProfileIfExisting(profile());
[email protected]5d98294912012-06-27 22:57:40197 if (tab_restore_service)
198 tab_restore_service->RemoveObserver(this);
199 profile_pref_registrar_.RemoveAll();
200 local_pref_registrar_.RemoveAll();
201 browser_->tab_strip_model()->RemoveObserver(this);
202}
203
204bool BrowserCommandController::IsReservedCommandOrKey(
205 int command_id,
206 const content::NativeWebKeyboardEvent& event) {
207 // In Apps mode, no keys are reserved.
208 if (browser_->is_app())
209 return false;
210
211#if defined(OS_CHROMEOS)
[email protected]397abd32013-08-21 05:44:19212 // On Chrome OS, the top row of keys are mapped to browser actions like
213 // back/forward or refresh. We don't want web pages to be able to change the
214 // behavior of these keys. Ash handles F4 and up; this leaves us needing to
215 // reserve browser back/forward and refresh here.
[email protected]5d98294912012-06-27 22:57:40216 ui::KeyboardCode key_code =
Blink Reformat1c4d759e2017-04-09 16:34:54217 static_cast<ui::KeyboardCode>(event.windows_key_code);
[email protected]397abd32013-08-21 05:44:19218 if ((key_code == ui::VKEY_BROWSER_BACK && command_id == IDC_BACK) ||
219 (key_code == ui::VKEY_BROWSER_FORWARD && command_id == IDC_FORWARD) ||
220 (key_code == ui::VKEY_BROWSER_REFRESH && command_id == IDC_RELOAD)) {
[email protected]5d98294912012-06-27 22:57:40221 return true;
222 }
223#endif
224
zijiehe68cd3dc22017-04-07 18:50:29225 if (window()->IsFullscreen()) {
226 // In fullscreen, all commands except for IDC_FULLSCREEN and IDC_EXIT should
227 // be delivered to the web page. The intent to implement and ship can be
228 // found in http://crbug.com/680809.
229 const bool is_exit_fullscreen =
230 (command_id == IDC_EXIT || command_id == IDC_FULLSCREEN);
231#if defined(OS_MACOSX)
232 // This behavior is different on Mac OS, which has a unique user-initiated
233 // full-screen mode. According to the discussion in http://crbug.com/702251,
234 // the commands should be reserved for browser-side handling if the browser
235 // window's toolbar is visible.
236 if (window()->IsToolbarShowing()) {
237 if (command_id == IDC_FULLSCREEN)
238 return true;
239 } else {
240 return is_exit_fullscreen;
241 }
242#else
243 return is_exit_fullscreen;
244#endif
245 }
[email protected]1e2172f2014-04-01 17:32:34246
[email protected]893124a22014-04-15 00:45:28247#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
[email protected]1e2172f2014-04-01 17:32:34248 // If this key was registered by the user as a content editing hotkey, then
249 // it is not reserved.
[email protected]d5bedb6d2014-04-08 10:49:32250 ui::TextEditKeyBindingsDelegateAuraLinux* delegate =
[email protected]1e2172f2014-04-01 17:32:34251 ui::GetTextEditKeyBindingsDelegate();
[email protected]47e8e7542014-04-03 07:52:42252 if (delegate && event.os_event && delegate->MatchEvent(*event.os_event, NULL))
[email protected]1e2172f2014-04-01 17:32:34253 return false;
254#endif
255
Aran Gilmand217b5b2019-04-11 17:45:43256 return command_id == IDC_CLOSE_TAB || command_id == IDC_CLOSE_WINDOW ||
257 command_id == IDC_NEW_INCOGNITO_WINDOW || command_id == IDC_NEW_TAB ||
258 command_id == IDC_NEW_WINDOW || command_id == IDC_RESTORE_TAB ||
zijiehe13207802017-02-16 08:06:10259 command_id == IDC_SELECT_NEXT_TAB ||
Aran Gilmand217b5b2019-04-11 17:45:43260 command_id == IDC_SELECT_PREVIOUS_TAB || command_id == IDC_EXIT;
[email protected]5d98294912012-06-27 22:57:40261}
262
[email protected]5d98294912012-06-27 22:57:40263void BrowserCommandController::TabStateChanged() {
264 UpdateCommandsForTabState();
265}
266
[email protected]d93dbd12014-08-04 23:42:53267void BrowserCommandController::ZoomStateChanged() {
268 UpdateCommandsForZoomState();
269}
270
[email protected]5d98294912012-06-27 22:57:40271void BrowserCommandController::ContentRestrictionsChanged() {
272 UpdateCommandsForContentRestrictionState();
273}
274
275void BrowserCommandController::FullscreenStateChanged() {
[email protected]32dfede2013-08-25 15:48:25276 UpdateCommandsForFullscreenMode();
[email protected]5d98294912012-06-27 22:57:40277}
278
Ivan Sandrk9669d0e2017-12-15 23:50:20279#if defined(OS_CHROMEOS)
280void BrowserCommandController::LockedFullscreenStateChanged() {
281 UpdateCommandsForLockedFullscreenMode();
282}
283#endif
284
[email protected]5d98294912012-06-27 22:57:40285void BrowserCommandController::PrintingStateChanged() {
286 UpdatePrintingState();
287}
288
289void BrowserCommandController::LoadingStateChanged(bool is_loading,
290 bool force) {
291 UpdateReloadStopState(is_loading, force);
292}
293
wittman76df71db32014-12-18 23:26:58294void BrowserCommandController::ExtensionStateChanged() {
295 // Extensions may disable the bookmark editing commands.
296 UpdateCommandsForBookmarkEditing();
297}
298
[email protected]5d98294912012-06-27 22:57:40299////////////////////////////////////////////////////////////////////////////////
Ivan Sandrk9669d0e2017-12-15 23:50:20300// BrowserCommandController, CommandUpdater implementation:
[email protected]5d98294912012-06-27 22:57:40301
Ivan Sandrk9669d0e2017-12-15 23:50:20302bool BrowserCommandController::SupportsCommand(int id) const {
303 return command_updater_.SupportsCommand(id);
304}
305
306bool BrowserCommandController::IsCommandEnabled(int id) const {
307 return command_updater_.IsCommandEnabled(id);
308}
309
Edwin Joe6f6fc1e2019-02-27 20:00:37310bool BrowserCommandController::ExecuteCommand(int id,
311 base::TimeTicks time_stamp) {
312 return ExecuteCommandWithDisposition(id, WindowOpenDisposition::CURRENT_TAB,
313 time_stamp);
Ivan Sandrk9669d0e2017-12-15 23:50:20314}
315
316bool BrowserCommandController::ExecuteCommandWithDisposition(
Edwin Joe6f6fc1e2019-02-27 20:00:37317 int id,
318 WindowOpenDisposition disposition,
319 base::TimeTicks time_stamp) {
Ivan Sandrk9669d0e2017-12-15 23:50:20320 // Doesn't go through the command_updater_ to avoid dealing with having a
321 // naming collision for ExecuteCommandWithDisposition (both
322 // CommandUpdaterDelegate and CommandUpdater declare this function so we
323 // choose to not implement CommandUpdaterDelegate inside this class and
324 // therefore command_updater_ doesn't have the delegate set).
325 if (!SupportsCommand(id) || !IsCommandEnabled(id))
326 return false;
327
[email protected]5d98294912012-06-27 22:57:40328 // No commands are enabled if there is not yet any selected tab.
329 // TODO(pkasting): It seems like we should not need this, because either
330 // most/all commands should not have been enabled yet anyway or the ones that
331 // are enabled should be global, or safe themselves against having no selected
332 // tab. However, Ben says he tried removing this before and got lots of
333 // crashes, e.g. from Windows sending WM_COMMANDs at random times during
334 // window construction. This probably could use closer examination someday.
[email protected]59253a652012-11-20 00:17:26335 if (browser_->tab_strip_model()->active_index() == TabStripModel::kNoTab)
Ivan Sandrk9669d0e2017-12-15 23:50:20336 return true;
[email protected]5d98294912012-06-27 22:57:40337
Aran Gilmand217b5b2019-04-11 17:45:43338 DCHECK(command_updater_.IsCommandEnabled(id))
339 << "Invalid/disabled command " << id;
[email protected]5d98294912012-06-27 22:57:40340
[email protected]5d98294912012-06-27 22:57:40341 // The order of commands in this switch statement must match the function
342 // declaration order in browser.h!
343 switch (id) {
344 // Navigation commands
345 case IDC_BACK:
346 GoBack(browser_, disposition);
347 break;
348 case IDC_FORWARD:
349 GoForward(browser_, disposition);
350 break;
351 case IDC_RELOAD:
352 Reload(browser_, disposition);
353 break;
[email protected]58e29032012-08-06 20:19:57354 case IDC_RELOAD_CLEARING_CACHE:
355 ClearCache(browser_);
Nico Webera745ef72018-01-29 23:45:57356 FALLTHROUGH;
toyoshim7dad4b1182016-04-01 14:28:05357 case IDC_RELOAD_BYPASSING_CACHE:
358 ReloadBypassingCache(browser_, disposition);
[email protected]5d98294912012-06-27 22:57:40359 break;
360 case IDC_HOME:
361 Home(browser_, disposition);
362 break;
363 case IDC_OPEN_CURRENT_URL:
364 OpenCurrentURL(browser_);
365 break;
366 case IDC_STOP:
367 Stop(browser_);
368 break;
369
Aran Gilmand217b5b2019-04-11 17:45:43370 // Window management commands
[email protected]5d98294912012-06-27 22:57:40371 case IDC_NEW_WINDOW:
372 NewWindow(browser_);
373 break;
374 case IDC_NEW_INCOGNITO_WINDOW:
Orin Jaworski5e4bcd0c2018-10-16 19:35:05375 NewIncognitoWindow(profile());
[email protected]5d98294912012-06-27 22:57:40376 break;
377 case IDC_CLOSE_WINDOW:
bratell0a7406f2017-03-28 07:46:37378 base::RecordAction(base::UserMetricsAction("CloseWindowByKey"));
[email protected]04b9e692012-08-24 14:49:09379 CloseWindow(browser_);
[email protected]5d98294912012-06-27 22:57:40380 break;
Bettina Dea0a4505922017-09-28 00:53:32381 case IDC_NEW_TAB: {
382 NewTab(browser_);
383#if BUILDFLAG(ENABLE_DESKTOP_IN_PRODUCT_HELP)
Catherine Chungbeb884222017-08-01 15:47:55384 // This is not in NewTab() to avoid tracking programmatic creation of new
385 // tabs by extensions.
Bettina Dea0a4505922017-09-28 00:53:32386 auto* new_tab_tracker =
387 feature_engagement::NewTabTrackerFactory::GetInstance()
388 ->GetForProfile(profile());
389
390 new_tab_tracker->OnNewTabOpened();
391 new_tab_tracker->CloseBubble();
Catherine Chungbeb884222017-08-01 15:47:55392#endif
[email protected]5d98294912012-06-27 22:57:40393 break;
Bettina Dea0a4505922017-09-28 00:53:32394 }
[email protected]5d98294912012-06-27 22:57:40395 case IDC_CLOSE_TAB:
bratell0a7406f2017-03-28 07:46:37396 base::RecordAction(base::UserMetricsAction("CloseTabByKey"));
[email protected]04b9e692012-08-24 14:49:09397 CloseTab(browser_);
[email protected]5d98294912012-06-27 22:57:40398 break;
399 case IDC_SELECT_NEXT_TAB:
bratell0a7406f2017-03-28 07:46:37400 base::RecordAction(base::UserMetricsAction("Accel_SelectNextTab"));
Edwin Joe6f6fc1e2019-02-27 20:00:37401 SelectNextTab(browser_,
402 {TabStripModel::GestureType::kKeyboard, time_stamp});
[email protected]5d98294912012-06-27 22:57:40403 break;
404 case IDC_SELECT_PREVIOUS_TAB:
bratell0a7406f2017-03-28 07:46:37405 base::RecordAction(base::UserMetricsAction("Accel_SelectPreviousTab"));
Edwin Joe6f6fc1e2019-02-27 20:00:37406 SelectPreviousTab(browser_,
407 {TabStripModel::GestureType::kKeyboard, time_stamp});
[email protected]5d98294912012-06-27 22:57:40408 break;
[email protected]5d98294912012-06-27 22:57:40409 case IDC_MOVE_TAB_NEXT:
410 MoveTabNext(browser_);
411 break;
412 case IDC_MOVE_TAB_PREVIOUS:
413 MoveTabPrevious(browser_);
414 break;
415 case IDC_SELECT_TAB_0:
416 case IDC_SELECT_TAB_1:
417 case IDC_SELECT_TAB_2:
418 case IDC_SELECT_TAB_3:
419 case IDC_SELECT_TAB_4:
420 case IDC_SELECT_TAB_5:
421 case IDC_SELECT_TAB_6:
422 case IDC_SELECT_TAB_7:
bratell0a7406f2017-03-28 07:46:37423 base::RecordAction(base::UserMetricsAction("Accel_SelectNumberedTab"));
Edwin Joe6f6fc1e2019-02-27 20:00:37424 SelectNumberedTab(browser_, id - IDC_SELECT_TAB_0,
425 {TabStripModel::GestureType::kKeyboard, time_stamp});
[email protected]5d98294912012-06-27 22:57:40426 break;
427 case IDC_SELECT_LAST_TAB:
bratell0a7406f2017-03-28 07:46:37428 base::RecordAction(base::UserMetricsAction("Accel_SelectNumberedTab"));
Edwin Joe6f6fc1e2019-02-27 20:00:37429 SelectLastTab(browser_,
430 {TabStripModel::GestureType::kKeyboard, time_stamp});
[email protected]5d98294912012-06-27 22:57:40431 break;
432 case IDC_DUPLICATE_TAB:
433 DuplicateTab(browser_);
434 break;
435 case IDC_RESTORE_TAB:
436 RestoreTab(browser_);
Collin Baker9bbe1d82019-03-08 01:16:27437 browser_->window()->OnTabRestored(IDC_RESTORE_TAB);
[email protected]5d98294912012-06-27 22:57:40438 break;
[email protected]5d98294912012-06-27 22:57:40439 case IDC_SHOW_AS_TAB:
440 ConvertPopupToTabbedBrowser(browser_);
441 break;
442 case IDC_FULLSCREEN:
[email protected]3f32b9b2012-07-09 16:59:28443 chrome::ToggleFullscreenMode(browser_);
[email protected]5d98294912012-06-27 22:57:40444 break;
Alan Cutter09965802018-03-27 07:25:29445 case IDC_OPEN_IN_PWA_WINDOW:
446 base::RecordAction(base::UserMetricsAction("OpenActiveTabInPwaWindow"));
447 ReparentSecureActiveTabIntoPwaWindow(browser_);
448 break;
[email protected]770c6d82012-09-06 22:21:32449
[email protected]d12cc5e2013-10-19 18:25:06450#if defined(OS_CHROMEOS)
451 case IDC_VISIT_DESKTOP_OF_LRU_USER_2:
[email protected]0c930812014-01-30 18:01:47452 case IDC_VISIT_DESKTOP_OF_LRU_USER_3:
zijiehe4dde8072017-02-13 20:38:35453 ExecuteVisitDesktopCommand(id, window()->GetNativeWindow());
[email protected]0c930812014-01-30 18:01:47454 break;
[email protected]d12cc5e2013-10-19 18:25:06455#endif
456
[email protected]893124a22014-04-15 00:45:28457#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
Tom Andersonf15ede502017-11-10 03:17:15458 case IDC_MINIMIZE_WINDOW:
459 browser_->window()->Minimize();
460 break;
461 case IDC_MAXIMIZE_WINDOW:
462 browser_->window()->Maximize();
463 break;
464 case IDC_RESTORE_WINDOW:
465 browser_->window()->Restore();
466 break;
[email protected]af97be4c62014-02-13 14:43:34467 case IDC_USE_SYSTEM_TITLE_BAR: {
zijiehe4dde8072017-02-13 20:38:35468 PrefService* prefs = profile()->GetPrefs();
[email protected]af97be4c62014-02-13 14:43:34469 prefs->SetBoolean(prefs::kUseCustomChromeFrame,
470 !prefs->GetBoolean(prefs::kUseCustomChromeFrame));
471 break;
472 }
473#endif
474
[email protected]5d98294912012-06-27 22:57:40475#if defined(OS_MACOSX)
spqchanb8ffc7d2015-11-17 01:17:21476 case IDC_TOGGLE_FULLSCREEN_TOOLBAR:
477 chrome::ToggleFullscreenToolbar(browser_);
478 break;
spqchan2c5d541e2017-10-25 07:07:11479 case IDC_TOGGLE_JAVASCRIPT_APPLE_EVENTS: {
Robert Sesek03282612018-10-31 15:48:06480 chrome::ToggleJavaScriptFromAppleEventsAllowed(browser_);
spqchan2c5d541e2017-10-25 07:07:11481 break;
482 }
[email protected]5d98294912012-06-27 22:57:40483#endif
484 case IDC_EXIT:
485 Exit();
486 break;
487
488 // Page-related commands
489 case IDC_SAVE_PAGE:
490 SavePage(browser_);
491 break;
492 case IDC_BOOKMARK_PAGE:
Anatoly Pilikovee720922017-11-01 09:43:17493#if BUILDFLAG(ENABLE_DESKTOP_IN_PRODUCT_HELP)
Bettina Dea48125dc2017-09-06 03:23:14494 feature_engagement::BookmarkTrackerFactory::GetInstance()
495 ->GetForProfile(profile())
496 ->OnBookmarkAdded();
497#endif
deepak.m154a7f392014-12-15 04:41:43498 BookmarkCurrentPageAllowingExtensionOverrides(browser_);
[email protected]5d98294912012-06-27 22:57:40499 break;
[email protected]5d98294912012-06-27 22:57:40500 case IDC_BOOKMARK_ALL_TABS:
Anatoly Pilikovee720922017-11-01 09:43:17501#if BUILDFLAG(ENABLE_DESKTOP_IN_PRODUCT_HELP)
Bettina Dea48125dc2017-09-06 03:23:14502 feature_engagement::BookmarkTrackerFactory::GetInstance()
503 ->GetForProfile(profile())
504 ->OnBookmarkAdded();
505#endif
[email protected]5d98294912012-06-27 22:57:40506 BookmarkAllTabs(browser_);
507 break;
508 case IDC_VIEW_SOURCE:
Lukasz Anforowicze1b954d92017-10-30 21:28:06509 browser_->tab_strip_model()
510 ->GetActiveWebContents()
511 ->GetMainFrame()
512 ->ViewSource();
[email protected]5d98294912012-06-27 22:57:40513 break;
514 case IDC_EMAIL_PAGE_LOCATION:
515 EmailPageLocation(browser_);
516 break;
517 case IDC_PRINT:
518 Print(browser_);
519 break;
bondd052b5f82015-10-28 22:39:32520
Lei Zhang48a4a5262018-04-17 20:18:44521#if BUILDFLAG(ENABLE_PRINTING)
vitalybukaf9433e42014-09-08 10:04:55522 case IDC_BASIC_PRINT:
bratell0a7406f2017-03-28 07:46:37523 base::RecordAction(base::UserMetricsAction("Accel_Advanced_Print"));
vitalybukaf9433e42014-09-08 10:04:55524 BasicPrint(browser_);
[email protected]5d98294912012-06-27 22:57:40525 break;
Lei Zhang48a4a5262018-04-17 20:18:44526#endif // ENABLE_PRINTING
bondd052b5f82015-10-28 22:39:32527
bondd052b5f82015-10-28 22:39:32528 case IDC_SAVE_CREDIT_CARD_FOR_PAGE:
529 SaveCreditCard(browser_);
530 break;
siyua3e599eaa2018-07-13 00:23:06531 case IDC_MIGRATE_LOCAL_CREDIT_CARD_FOR_PAGE:
532 MigrateLocalCards(browser_);
533 break;
[email protected]e625b7602013-10-28 09:24:56534 case IDC_TRANSLATE_PAGE:
535 Translate(browser_);
536 break;
[email protected]4bee4432014-05-05 13:11:41537 case IDC_MANAGE_PASSWORDS_FOR_PAGE:
538 ManagePasswordsForPage(browser_);
539 break;
Tina Wang4c324702019-04-25 18:35:15540 case IDC_SEND_TAB_TO_SELF:
541 // TODO(crbug/950388): add implementation.
542 NOTIMPLEMENTED();
543 break;
[email protected]4bee4432014-05-05 13:11:41544
[email protected]5d98294912012-06-27 22:57:40545 // Clipboard commands
546 case IDC_CUT:
[email protected]5d98294912012-06-27 22:57:40547 case IDC_COPY:
[email protected]5d98294912012-06-27 22:57:40548 case IDC_PASTE:
pkastingcd3f08bce2015-04-18 13:37:12549 CutCopyPaste(browser_, id);
[email protected]5d98294912012-06-27 22:57:40550 break;
551
552 // Find-in-page
553 case IDC_FIND:
554 Find(browser_);
555 break;
556 case IDC_FIND_NEXT:
557 FindNext(browser_);
558 break;
559 case IDC_FIND_PREVIOUS:
560 FindPrevious(browser_);
561 break;
562
563 // Zoom
564 case IDC_ZOOM_PLUS:
565 Zoom(browser_, content::PAGE_ZOOM_IN);
566 break;
567 case IDC_ZOOM_NORMAL:
568 Zoom(browser_, content::PAGE_ZOOM_RESET);
569 break;
570 case IDC_ZOOM_MINUS:
571 Zoom(browser_, content::PAGE_ZOOM_OUT);
572 break;
573
574 // Focus various bits of UI
575 case IDC_FOCUS_TOOLBAR:
bratell0a7406f2017-03-28 07:46:37576 base::RecordAction(base::UserMetricsAction("Accel_Focus_Toolbar"));
[email protected]5d98294912012-06-27 22:57:40577 FocusToolbar(browser_);
578 break;
579 case IDC_FOCUS_LOCATION:
bratell0a7406f2017-03-28 07:46:37580 base::RecordAction(base::UserMetricsAction("Accel_Focus_Location"));
[email protected]5d98294912012-06-27 22:57:40581 FocusLocationBar(browser_);
582 break;
583 case IDC_FOCUS_SEARCH:
bratell0a7406f2017-03-28 07:46:37584 base::RecordAction(base::UserMetricsAction("Accel_Focus_Search"));
[email protected]5d98294912012-06-27 22:57:40585 FocusSearch(browser_);
586 break;
587 case IDC_FOCUS_MENU_BAR:
588 FocusAppMenu(browser_);
589 break;
590 case IDC_FOCUS_BOOKMARKS:
bratell0a7406f2017-03-28 07:46:37591 base::RecordAction(base::UserMetricsAction("Accel_Focus_Bookmarks"));
[email protected]5d98294912012-06-27 22:57:40592 FocusBookmarksToolbar(browser_);
593 break;
David Tsengc0b1b642018-01-24 07:12:27594 case IDC_FOCUS_INACTIVE_POPUP_FOR_ACCESSIBILITY:
595 FocusInactivePopupForAccessibility(browser_);
[email protected]822ca8c62013-04-19 00:49:15596 break;
[email protected]5d98294912012-06-27 22:57:40597 case IDC_FOCUS_NEXT_PANE:
598 FocusNextPane(browser_);
599 break;
600 case IDC_FOCUS_PREVIOUS_PANE:
601 FocusPreviousPane(browser_);
602 break;
603
604 // Show various bits of UI
605 case IDC_OPEN_FILE:
606 browser_->OpenFile();
607 break;
Alan Cutter81ac726682018-09-25 00:18:36608 case IDC_CREATE_SHORTCUT:
609 CreateBookmarkAppFromCurrentWebContents(browser_,
610 true /* force_shortcut_app */);
611 break;
612 case IDC_INSTALL_PWA:
613 CreateBookmarkAppFromCurrentWebContents(browser_,
614 false /* force_shortcut_app */);
[email protected]488e3952013-11-18 05:29:14615 break;
[email protected]5d98294912012-06-27 22:57:40616 case IDC_DEV_TOOLS:
[email protected]c934c382013-11-01 00:36:01617 ToggleDevToolsWindow(browser_, DevToolsToggleAction::Show());
[email protected]5d98294912012-06-27 22:57:40618 break;
619 case IDC_DEV_TOOLS_CONSOLE:
einbinderdfa567b2016-12-16 01:15:52620 ToggleDevToolsWindow(browser_, DevToolsToggleAction::ShowConsolePanel());
[email protected]5d98294912012-06-27 22:57:40621 break;
[email protected]2056c3192013-10-21 22:40:51622 case IDC_DEV_TOOLS_DEVICES:
623 InspectUI::InspectDevices(browser_);
624 break;
[email protected]5d98294912012-06-27 22:57:40625 case IDC_DEV_TOOLS_INSPECT:
[email protected]c934c382013-11-01 00:36:01626 ToggleDevToolsWindow(browser_, DevToolsToggleAction::Inspect());
[email protected]5d98294912012-06-27 22:57:40627 break;
[email protected]d16657c2012-09-03 14:25:10628 case IDC_DEV_TOOLS_TOGGLE:
[email protected]c934c382013-11-01 00:36:01629 ToggleDevToolsWindow(browser_, DevToolsToggleAction::Toggle());
[email protected]d16657c2012-09-03 14:25:10630 break;
[email protected]5d98294912012-06-27 22:57:40631 case IDC_TASK_MANAGER:
[email protected]29c262de2013-06-22 15:39:38632 OpenTaskManager(browser_);
[email protected]5d98294912012-06-27 22:57:40633 break;
caelyn4e4e08a2015-02-04 21:27:49634#if defined(OS_CHROMEOS)
635 case IDC_TAKE_SCREENSHOT:
636 TakeScreenshot();
637 break;
638#endif
[email protected]236ad3022013-09-04 03:27:43639#if defined(GOOGLE_CHROME_BUILD)
[email protected]5d98294912012-06-27 22:57:40640 case IDC_FEEDBACK:
afakhryf4575bd2017-04-28 02:21:04641 OpenFeedbackDialog(browser_, kFeedbackSourceBrowserCommand);
[email protected]5d98294912012-06-27 22:57:40642 break;
[email protected]236ad3022013-09-04 03:27:43643#endif
[email protected]5d98294912012-06-27 22:57:40644 case IDC_SHOW_BOOKMARK_BAR:
645 ToggleBookmarkBar(browser_);
646 break;
647 case IDC_PROFILING_ENABLED:
Bryce Thomas96b07772018-11-07 03:04:15648 content::Profiling::Toggle();
[email protected]5d98294912012-06-27 22:57:40649 break;
650
651 case IDC_SHOW_BOOKMARK_MANAGER:
652 ShowBookmarkManager(browser_);
653 break;
654 case IDC_SHOW_APP_MENU:
bratell0a7406f2017-03-28 07:46:37655 base::RecordAction(base::UserMetricsAction("Accel_Show_App_Menu"));
[email protected]5d98294912012-06-27 22:57:40656 ShowAppMenu(browser_);
657 break;
658 case IDC_SHOW_AVATAR_MENU:
659 ShowAvatarMenu(browser_);
660 break;
661 case IDC_SHOW_HISTORY:
662 ShowHistory(browser_);
663 break;
664 case IDC_SHOW_DOWNLOADS:
665 ShowDownloads(browser_);
666 break;
667 case IDC_MANAGE_EXTENSIONS:
[email protected]bc9833c32013-02-28 04:05:08668 ShowExtensions(browser_, std::string());
[email protected]5d98294912012-06-27 22:57:40669 break;
670 case IDC_OPTIONS:
671 ShowSettings(browser_);
672 break;
673 case IDC_EDIT_SEARCH_ENGINES:
674 ShowSearchEngineSettings(browser_);
675 break;
676 case IDC_VIEW_PASSWORDS:
677 ShowPasswordManager(browser_);
678 break;
679 case IDC_CLEAR_BROWSING_DATA:
680 ShowClearBrowsingDataDialog(browser_);
681 break;
682 case IDC_IMPORT_SETTINGS:
683 ShowImportDialog(browser_);
684 break;
[email protected]9b7ab882012-09-10 23:46:36685 case IDC_TOGGLE_REQUEST_TABLET_SITE:
686 ToggleRequestTabletSite(browser_);
687 break;
[email protected]5d98294912012-06-27 22:57:40688 case IDC_ABOUT:
689 ShowAboutChrome(browser_);
690 break;
691 case IDC_UPGRADE_DIALOG:
692 OpenUpdateChromeDialog(browser_);
693 break;
[email protected]5d98294912012-06-27 22:57:40694 case IDC_HELP_PAGE_VIA_KEYBOARD:
695 ShowHelp(browser_, HELP_SOURCE_KEYBOARD);
696 break;
697 case IDC_HELP_PAGE_VIA_MENU:
698 ShowHelp(browser_, HELP_SOURCE_MENU);
699 break;
Bret Sepulveda2d018662017-05-18 21:31:48700 case IDC_SHOW_BETA_FORUM:
701 ShowBetaForum(browser_);
702 break;
David Roger7e25dff2019-02-07 09:03:17703#if !defined(OS_CHROMEOS)
estade8c0780f2015-08-21 23:36:41704 case IDC_SHOW_SIGNIN:
gogerald71bf6c902015-12-08 00:49:37705 ShowBrowserSigninOrSettings(
706 browser_, signin_metrics::AccessPoint::ACCESS_POINT_MENU);
estade8c0780f2015-08-21 23:36:41707 break;
David Roger7e25dff2019-02-07 09:03:17708#endif
[email protected]6bd370b2014-05-28 14:19:47709 case IDC_DISTILL_PAGE:
710 DistillCurrentPage(browser_);
711 break;
apacible45cbfc92015-09-28 22:45:41712 case IDC_ROUTE_MEDIA:
713 RouteMedia(browser_);
714 break;
Tommy Steimelc4477982017-11-29 18:07:18715 case IDC_WINDOW_MUTE_SITE:
716 MuteSite(browser_);
ellyjones0101ba02017-05-19 15:50:26717 break;
718 case IDC_WINDOW_PIN_TAB:
719 PinTab(browser_);
720 break;
Yann Dagocd13225a2019-03-07 18:39:05721 case IDC_SHOW_MANAGEMENT_PAGE: {
Yann Dagoc1d1c4af2019-04-17 13:40:51722 ShowSingletonTab(browser_, GURL(kChromeUIManagementURL));
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:27723 break;
Yann Dagocd13225a2019-03-07 18:39:05724 }
Christopher Lam0dbac2b2017-11-14 07:12:10725 // Hosted App commands
726 case IDC_COPY_URL:
727 CopyURL(browser_);
728 break;
729 case IDC_OPEN_IN_CHROME:
730 OpenInChrome(browser_);
731 break;
732 case IDC_SITE_SETTINGS:
733 ShowSiteSettings(
734 browser_,
735 browser_->tab_strip_model()->GetActiveWebContents()->GetVisibleURL());
736 break;
Dana Fried08b774a2019-05-10 18:21:48737 case IDC_HOSTED_APP_MENU_APP_INFO: {
738 content::WebContents* const web_contents =
739 browser_->tab_strip_model()->GetActiveWebContents();
740 if (web_contents) {
741 ShowPageInfoDialog(web_contents,
742 base::BindOnce(&AppInfoDialogClosedCallback,
743 base::Unretained(web_contents)),
744 bubble_anchor_util::kAppMenuButton);
745 }
Christopher Lam86b52712017-12-04 01:58:33746 break;
Dana Fried08b774a2019-05-10 18:21:48747 }
[email protected]5d98294912012-06-27 22:57:40748 default:
749 LOG(WARNING) << "Received Unimplemented Command: " << id;
750 break;
751 }
Ivan Sandrk9669d0e2017-12-15 23:50:20752
753 return true;
754}
755
756void BrowserCommandController::AddCommandObserver(int id,
757 CommandObserver* observer) {
758 command_updater_.AddCommandObserver(id, observer);
759}
760
761void BrowserCommandController::RemoveCommandObserver(
Aran Gilmand217b5b2019-04-11 17:45:43762 int id,
763 CommandObserver* observer) {
Ivan Sandrk9669d0e2017-12-15 23:50:20764 command_updater_.RemoveCommandObserver(id, observer);
765}
766
767void BrowserCommandController::RemoveCommandObserver(
768 CommandObserver* observer) {
769 command_updater_.RemoveCommandObserver(observer);
770}
771
772bool BrowserCommandController::UpdateCommandEnabled(int id, bool state) {
773 if (is_locked_fullscreen_)
774 return false;
775
776 return command_updater_.UpdateCommandEnabled(id, state);
[email protected]5d98294912012-06-27 22:57:40777}
778
Mihai Sardarescuf87ccc12018-09-26 10:37:45779////////////////////////////////////////////////////////////////////////////////
780// BrowserCommandController, SigninPrefObserver implementation:
781
782void BrowserCommandController::OnSigninAllowedPrefChange() {
783 // For unit tests, we don't have a window.
784 if (!window())
785 return;
786 UpdateShowSyncState(IsShowingMainUI());
787}
788
[email protected]5d98294912012-06-27 22:57:40789// BrowserCommandController, TabStripModelObserver implementation:
790
sangwoo.ko1ae265f12018-10-18 08:30:28791void BrowserCommandController::OnTabStripModelChanged(
792 TabStripModel* tab_strip_model,
793 const TabStripModelChange& change,
794 const TabStripSelectionChange& selection) {
Dana Frieda391e6ce2019-05-08 00:00:20795 std::vector<content::WebContents*> new_contents;
796 std::vector<content::WebContents*> old_contents;
[email protected]5d98294912012-06-27 22:57:40797
Dana Frieda391e6ce2019-05-08 00:00:20798 switch (change.type()) {
799 case TabStripModelChange::kInserted:
800 for (const auto& contents : change.GetInsert()->contents)
801 new_contents.push_back(contents.contents);
802 break;
803 case TabStripModelChange::kReplaced: {
804 auto* replace = change.GetReplace();
805 new_contents.push_back(replace->new_contents);
806 old_contents.push_back(replace->old_contents);
807 break;
sangwoo.ko1ae265f12018-10-18 08:30:28808 }
Dana Frieda391e6ce2019-05-08 00:00:20809 case TabStripModelChange::kRemoved:
810 for (const auto& contents : change.GetRemove()->contents)
811 old_contents.push_back(contents.contents);
812 break;
813 default:
814 break;
sangwoo.ko1ae265f12018-10-18 08:30:28815 }
Dana Frieda391e6ce2019-05-08 00:00:20816
817 for (auto* contents : old_contents)
818 RemoveInterstitialObservers(contents);
819 for (auto* contents : new_contents)
820 AddInterstitialObservers(contents);
[email protected]5d98294912012-06-27 22:57:40821}
822
[email protected]3cac87232012-11-20 01:48:27823void BrowserCommandController::TabBlockedStateChanged(
824 content::WebContents* contents,
825 int index) {
826 PrintingStateChanged();
827 FullscreenStateChanged();
828 UpdateCommandsForFind();
apacible45cbfc92015-09-28 22:45:41829 UpdateCommandsForMediaRouter();
[email protected]3cac87232012-11-20 01:48:27830}
831
[email protected]5d98294912012-06-27 22:57:40832////////////////////////////////////////////////////////////////////////////////
833// BrowserCommandController, TabRestoreServiceObserver implementation:
834
835void BrowserCommandController::TabRestoreServiceChanged(
blundell74001adc2015-09-18 11:04:25836 sessions::TabRestoreService* service) {
[email protected]2e9369e2014-08-15 09:12:53837 UpdateTabRestoreCommandState();
[email protected]5d98294912012-06-27 22:57:40838}
839
840void BrowserCommandController::TabRestoreServiceDestroyed(
blundell74001adc2015-09-18 11:04:25841 sessions::TabRestoreService* service) {
[email protected]5d98294912012-06-27 22:57:40842 service->RemoveObserver(this);
843}
844
[email protected]2e9369e2014-08-15 09:12:53845void BrowserCommandController::TabRestoreServiceLoaded(
blundell74001adc2015-09-18 11:04:25846 sessions::TabRestoreService* service) {
[email protected]2e9369e2014-08-15 09:12:53847 UpdateTabRestoreCommandState();
848}
849
[email protected]5d98294912012-06-27 22:57:40850////////////////////////////////////////////////////////////////////////////////
[email protected]5d98294912012-06-27 22:57:40851// BrowserCommandController, private:
852
[email protected]20ca0382013-02-28 19:50:07853class BrowserCommandController::InterstitialObserver
854 : public content::WebContentsObserver {
855 public:
856 InterstitialObserver(BrowserCommandController* controller,
857 content::WebContents* web_contents)
Aran Gilmand217b5b2019-04-11 17:45:43858 : WebContentsObserver(web_contents), controller_(controller) {}
[email protected]20ca0382013-02-28 19:50:07859
dcheng5dd5ff62014-10-21 12:42:38860 void DidAttachInterstitialPage() override {
[email protected]20ca0382013-02-28 19:50:07861 controller_->UpdateCommandsForTabState();
862 }
863
dcheng5dd5ff62014-10-21 12:42:38864 void DidDetachInterstitialPage() override {
[email protected]20ca0382013-02-28 19:50:07865 controller_->UpdateCommandsForTabState();
866 }
867
868 private:
869 BrowserCommandController* controller_;
870
871 DISALLOW_COPY_AND_ASSIGN(InterstitialObserver);
872};
873
[email protected]6a414ff2013-02-27 08:22:54874bool BrowserCommandController::IsShowingMainUI() {
manukf1a116392018-10-25 16:28:24875 return browser_->SupportsWindowFeature(Browser::FEATURE_TABSTRIP);
876}
877
878bool BrowserCommandController::IsShowingLocationBar() {
879 return browser_->SupportsWindowFeature(Browser::FEATURE_LOCATIONBAR);
[email protected]5d98294912012-06-27 22:57:40880}
881
882void BrowserCommandController::InitCommandState() {
883 // All browser commands whose state isn't set automagically some other way
884 // (like Back & Forward with initial page load) must have their state
885 // initialized here, otherwise they will be forever disabled.
886
Ivan Sandrk9669d0e2017-12-15 23:50:20887 if (is_locked_fullscreen_)
888 return;
889
[email protected]5d98294912012-06-27 22:57:40890 // Navigation commands
891 command_updater_.UpdateCommandEnabled(IDC_RELOAD, true);
toyoshim7dad4b1182016-04-01 14:28:05892 command_updater_.UpdateCommandEnabled(IDC_RELOAD_BYPASSING_CACHE, true);
[email protected]58e29032012-08-06 20:19:57893 command_updater_.UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE, true);
[email protected]5d98294912012-06-27 22:57:40894
895 // Window management commands
896 command_updater_.UpdateCommandEnabled(IDC_CLOSE_WINDOW, true);
897 command_updater_.UpdateCommandEnabled(IDC_NEW_TAB, true);
898 command_updater_.UpdateCommandEnabled(IDC_CLOSE_TAB, true);
899 command_updater_.UpdateCommandEnabled(IDC_DUPLICATE_TAB, true);
[email protected]2e9369e2014-08-15 09:12:53900 UpdateTabRestoreCommandState();
[email protected]d28d3782013-02-26 16:31:55901 command_updater_.UpdateCommandEnabled(IDC_EXIT, true);
[email protected]5d98294912012-06-27 22:57:40902 command_updater_.UpdateCommandEnabled(IDC_DEBUG_FRAME_TOGGLE, true);
[email protected]d12cc5e2013-10-19 18:25:06903#if defined(OS_CHROMEOS)
James Cook934abaf2017-09-19 22:21:58904 command_updater_.UpdateCommandEnabled(IDC_MINIMIZE_WINDOW, true);
[email protected]d12cc5e2013-10-19 18:25:06905 command_updater_.UpdateCommandEnabled(IDC_VISIT_DESKTOP_OF_LRU_USER_2, true);
906 command_updater_.UpdateCommandEnabled(IDC_VISIT_DESKTOP_OF_LRU_USER_3, true);
907#endif
[email protected]893124a22014-04-15 00:45:28908#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
Tom Andersonf15ede502017-11-10 03:17:15909 command_updater_.UpdateCommandEnabled(IDC_MINIMIZE_WINDOW, true);
910 command_updater_.UpdateCommandEnabled(IDC_MAXIMIZE_WINDOW, true);
911 command_updater_.UpdateCommandEnabled(IDC_RESTORE_WINDOW, true);
Maksim Sisovac767592018-08-10 08:00:15912 bool use_system_title_bar = true;
913#if defined(USE_OZONE)
914 use_system_title_bar = ui::OzonePlatform::GetInstance()
915 ->GetPlatformProperties()
916 .use_system_title_bar;
917#endif
918 command_updater_.UpdateCommandEnabled(IDC_USE_SYSTEM_TITLE_BAR,
919 use_system_title_bar);
[email protected]af97be4c62014-02-13 14:43:34920#endif
Alan Cutter09965802018-03-27 07:25:29921 command_updater_.UpdateCommandEnabled(IDC_OPEN_IN_PWA_WINDOW, true);
[email protected]5d98294912012-06-27 22:57:40922
923 // Page-related commands
924 command_updater_.UpdateCommandEnabled(IDC_EMAIL_PAGE_LOCATION, true);
[email protected]4bee4432014-05-05 13:11:41925 command_updater_.UpdateCommandEnabled(IDC_MANAGE_PASSWORDS_FOR_PAGE, true);
[email protected]5d98294912012-06-27 22:57:40926
927 // Zoom
928 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MENU, true);
929 command_updater_.UpdateCommandEnabled(IDC_ZOOM_PLUS, true);
[email protected]d93dbd12014-08-04 23:42:53930 command_updater_.UpdateCommandEnabled(IDC_ZOOM_NORMAL, false);
[email protected]5d98294912012-06-27 22:57:40931 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MINUS, true);
932
933 // Show various bits of UI
Aran Gilmand217b5b2019-04-11 17:45:43934 const bool guest_session =
935 profile()->IsGuestSession() || profile()->IsSystemProfile();
mlerman7831f57d2015-05-25 11:40:15936 DCHECK(!profile()->IsSystemProfile())
937 << "Ought to never have browser for the system profile.";
[email protected]338416c02014-05-13 16:47:06938 const bool normal_window = browser_->is_type_tabbed();
[email protected]05454532013-01-22 21:09:08939 UpdateOpenFileState(&command_updater_);
[email protected]5d98294912012-06-27 22:57:40940 UpdateCommandsForDevTools();
941 command_updater_.UpdateCommandEnabled(IDC_TASK_MANAGER, CanOpenTaskManager());
[email protected]338416c02014-05-13 16:47:06942 command_updater_.UpdateCommandEnabled(IDC_SHOW_HISTORY, !guest_session);
[email protected]5d98294912012-06-27 22:57:40943 command_updater_.UpdateCommandEnabled(IDC_SHOW_DOWNLOADS, true);
[email protected]674b65672014-06-02 23:21:56944 command_updater_.UpdateCommandEnabled(IDC_HELP_MENU, true);
[email protected]5d98294912012-06-27 22:57:40945 command_updater_.UpdateCommandEnabled(IDC_HELP_PAGE_VIA_KEYBOARD, true);
946 command_updater_.UpdateCommandEnabled(IDC_HELP_PAGE_VIA_MENU, true);
Bret Sepulveda2d018662017-05-18 21:31:48947 command_updater_.UpdateCommandEnabled(IDC_SHOW_BETA_FORUM, true);
[email protected]338416c02014-05-13 16:47:06948 command_updater_.UpdateCommandEnabled(IDC_BOOKMARKS_MENU, !guest_session);
Aran Gilmand217b5b2019-04-11 17:45:43949 command_updater_.UpdateCommandEnabled(
950 IDC_RECENT_TABS_MENU, !guest_session && !profile()->IsOffTheRecord());
tsergeant341a8272017-04-18 03:54:33951 command_updater_.UpdateCommandEnabled(IDC_CLEAR_BROWSING_DATA,
952 !guest_session);
[email protected]39d47592014-01-10 21:42:45953#if defined(OS_CHROMEOS)
caelyn4e4e08a2015-02-04 21:27:49954 command_updater_.UpdateCommandEnabled(IDC_TAKE_SCREENSHOT, true);
Ramin Halavatif362b7a2019-04-17 05:24:50955 // Chrome OS uses the system tray menu to handle multi-profiles. Avatar menu
956 // is only required in incognito mode.
957 if (profile()->IsIncognito())
[email protected]338416c02014-05-13 16:47:06958 command_updater_.UpdateCommandEnabled(IDC_SHOW_AVATAR_MENU, true);
Ramin Halavatif362b7a2019-04-17 05:24:50959#else
960 if (normal_window)
961 command_updater_.UpdateCommandEnabled(IDC_SHOW_AVATAR_MENU, true);
[email protected]39d47592014-01-10 21:42:45962#endif
[email protected]5d98294912012-06-27 22:57:40963
Mihai Sardarescu2ec5205682018-09-26 09:20:05964 UpdateShowSyncState(true);
965
[email protected]5d98294912012-06-27 22:57:40966 // Navigation commands
Christopher Camerone1385742019-04-05 20:37:25967 command_updater_.UpdateCommandEnabled(IDC_HOME,
968 normal_window || browser_->is_app());
[email protected]5d98294912012-06-27 22:57:40969
Christopher Lam0dbac2b2017-11-14 07:12:10970 const bool is_experimental_hosted_app =
Eric Willigers4a5f7a92019-05-10 19:19:26971 web_app::AppBrowserController::IsForExperimentalWebAppBrowser(browser_);
Christopher Lam0dbac2b2017-11-14 07:12:10972 // Hosted app browser commands.
973 command_updater_.UpdateCommandEnabled(IDC_COPY_URL,
974 is_experimental_hosted_app);
975 command_updater_.UpdateCommandEnabled(IDC_OPEN_IN_CHROME,
976 is_experimental_hosted_app);
977 command_updater_.UpdateCommandEnabled(IDC_SITE_SETTINGS,
978 is_experimental_hosted_app);
Alan Cutter0c3132302018-02-21 05:09:02979 command_updater_.UpdateCommandEnabled(IDC_HOSTED_APP_MENU_APP_INFO,
Christopher Lam86b52712017-12-04 01:58:33980 is_experimental_hosted_app);
Christopher Lam0dbac2b2017-11-14 07:12:10981
[email protected]5d98294912012-06-27 22:57:40982 // Window management commands
[email protected]5d98294912012-06-27 22:57:40983 command_updater_.UpdateCommandEnabled(IDC_SELECT_NEXT_TAB, normal_window);
Aran Gilmand217b5b2019-04-11 17:45:43984 command_updater_.UpdateCommandEnabled(IDC_SELECT_PREVIOUS_TAB, normal_window);
[email protected]5d98294912012-06-27 22:57:40985 command_updater_.UpdateCommandEnabled(IDC_MOVE_TAB_NEXT, normal_window);
986 command_updater_.UpdateCommandEnabled(IDC_MOVE_TAB_PREVIOUS, normal_window);
987 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_0, normal_window);
988 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_1, normal_window);
989 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_2, normal_window);
990 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_3, normal_window);
991 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_4, normal_window);
992 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_5, normal_window);
993 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_6, normal_window);
994 command_updater_.UpdateCommandEnabled(IDC_SELECT_TAB_7, normal_window);
995 command_updater_.UpdateCommandEnabled(IDC_SELECT_LAST_TAB, normal_window);
[email protected]5d98294912012-06-27 22:57:40996
[email protected]338416c02014-05-13 16:47:06997 // These are always enabled; the menu determines their menu item visibility.
[email protected]5d98294912012-06-27 22:57:40998 command_updater_.UpdateCommandEnabled(IDC_UPGRADE_DIALOG, true);
[email protected]5d98294912012-06-27 22:57:40999
[email protected]6bd370b2014-05-28 14:19:471000 // Distill current page.
1001 command_updater_.UpdateCommandEnabled(
avi556c05022014-12-22 23:31:431002 IDC_DISTILL_PAGE, base::CommandLine::ForCurrentProcess()->HasSwitch(
1003 switches::kEnableDomDistiller));
[email protected]6bd370b2014-05-28 14:19:471004
Tommy Steimelc4477982017-11-29 18:07:181005 command_updater_.UpdateCommandEnabled(IDC_WINDOW_MUTE_SITE, normal_window);
ellyjones0101ba02017-05-19 15:50:261006 command_updater_.UpdateCommandEnabled(IDC_WINDOW_PIN_TAB, normal_window);
1007
[email protected]338416c02014-05-13 16:47:061008 // Initialize other commands whose state changes based on various conditions.
[email protected]32dfede2013-08-25 15:48:251009 UpdateCommandsForFullscreenMode();
[email protected]5d98294912012-06-27 22:57:401010 UpdateCommandsForContentRestrictionState();
[email protected]5d98294912012-06-27 22:57:401011 UpdateCommandsForBookmarkEditing();
[email protected]5d98294912012-06-27 22:57:401012 UpdateCommandsForIncognitoAvailability();
1013}
1014
[email protected]05454532013-01-22 21:09:081015// static
1016void BrowserCommandController::UpdateSharedCommandsForIncognitoAvailability(
1017 CommandUpdater* command_updater,
1018 Profile* profile) {
mlermane01e6de2014-09-29 19:26:471019 const bool guest_session = profile->IsGuestSession();
1020 // TODO(mlerman): Make GetAvailability account for profile->IsGuestSession().
[email protected]5d98294912012-06-27 22:57:401021 IncognitoModePrefs::Availability incognito_availability =
[email protected]05454532013-01-22 21:09:081022 IncognitoModePrefs::GetAvailability(profile->GetPrefs());
1023 command_updater->UpdateCommandEnabled(
Aran Gilmand217b5b2019-04-11 17:45:431024 IDC_NEW_WINDOW, incognito_availability != IncognitoModePrefs::FORCED);
[email protected]05454532013-01-22 21:09:081025 command_updater->UpdateCommandEnabled(
[email protected]5d98294912012-06-27 22:57:401026 IDC_NEW_INCOGNITO_WINDOW,
mlermane01e6de2014-09-29 19:26:471027 incognito_availability != IncognitoModePrefs::DISABLED && !guest_session);
[email protected]5d98294912012-06-27 22:57:401028
[email protected]57b25292014-05-01 16:31:061029 const bool forced_incognito =
1030 incognito_availability == IncognitoModePrefs::FORCED ||
1031 guest_session; // Guest always runs in Incognito mode.
[email protected]05454532013-01-22 21:09:081032 command_updater->UpdateCommandEnabled(
[email protected]5d98294912012-06-27 22:57:401033 IDC_SHOW_BOOKMARK_MANAGER,
[email protected]57b25292014-05-01 16:31:061034 browser_defaults::bookmarks_enabled && !forced_incognito);
Devlin Cronin21dba422018-05-30 15:45:161035 extensions::ExtensionService* extension_service =
[email protected]03d25812014-06-22 19:41:551036 extensions::ExtensionSystem::Get(profile)->extension_service();
[email protected]57b25292014-05-01 16:31:061037 const bool enable_extensions =
[email protected]5d98294912012-06-27 22:57:401038 extension_service && extension_service->extensions_enabled();
[email protected]5d98294912012-06-27 22:57:401039
[email protected]57b25292014-05-01 16:31:061040 // Bookmark manager and settings page/subpages are forced to open in normal
1041 // mode. For this reason we disable these commands when incognito is forced.
1042 command_updater->UpdateCommandEnabled(IDC_MANAGE_EXTENSIONS,
1043 enable_extensions && !forced_incognito);
1044
1045 command_updater->UpdateCommandEnabled(IDC_IMPORT_SETTINGS, !forced_incognito);
1046 command_updater->UpdateCommandEnabled(IDC_OPTIONS,
1047 !forced_incognito || guest_session);
estade8c0780f2015-08-21 23:36:411048 command_updater->UpdateCommandEnabled(IDC_SHOW_SIGNIN, !forced_incognito);
[email protected]05454532013-01-22 21:09:081049}
1050
1051void BrowserCommandController::UpdateCommandsForIncognitoAvailability() {
Ivan Sandrk9669d0e2017-12-15 23:50:201052 if (is_locked_fullscreen_)
1053 return;
1054
[email protected]05454532013-01-22 21:09:081055 UpdateSharedCommandsForIncognitoAvailability(&command_updater_, profile());
1056
[email protected]6a414ff2013-02-27 08:22:541057 if (!IsShowingMainUI()) {
[email protected]05454532013-01-22 21:09:081058 command_updater_.UpdateCommandEnabled(IDC_IMPORT_SETTINGS, false);
1059 command_updater_.UpdateCommandEnabled(IDC_OPTIONS, false);
1060 }
[email protected]5d98294912012-06-27 22:57:401061}
1062
1063void BrowserCommandController::UpdateCommandsForTabState() {
Ivan Sandrk9669d0e2017-12-15 23:50:201064 if (is_locked_fullscreen_)
1065 return;
1066
[email protected]617ee962013-01-29 20:49:121067 WebContents* current_web_contents =
1068 browser_->tab_strip_model()->GetActiveWebContents();
[email protected]1c5119c2012-09-19 00:08:571069 if (!current_web_contents) // May be NULL during tab restore.
[email protected]5d98294912012-06-27 22:57:401070 return;
[email protected]5d98294912012-06-27 22:57:401071
1072 // Navigation commands
1073 command_updater_.UpdateCommandEnabled(IDC_BACK, CanGoBack(browser_));
1074 command_updater_.UpdateCommandEnabled(IDC_FORWARD, CanGoForward(browser_));
1075 command_updater_.UpdateCommandEnabled(IDC_RELOAD, CanReload(browser_));
toyoshim7dad4b1182016-04-01 14:28:051076 command_updater_.UpdateCommandEnabled(IDC_RELOAD_BYPASSING_CACHE,
[email protected]5d98294912012-06-27 22:57:401077 CanReload(browser_));
[email protected]58e29032012-08-06 20:19:571078 command_updater_.UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE,
1079 CanReload(browser_));
[email protected]5d98294912012-06-27 22:57:401080
1081 // Window management commands
Aran Gilmand217b5b2019-04-11 17:45:431082 command_updater_.UpdateCommandEnabled(
1083 IDC_DUPLICATE_TAB, !browser_->is_app() && CanDuplicateTab(browser_));
Tommy Steimelc4477982017-11-29 18:07:181084 command_updater_.UpdateCommandEnabled(IDC_WINDOW_MUTE_SITE,
ellyjones0101ba02017-05-19 15:50:261085 !browser_->is_app());
1086 command_updater_.UpdateCommandEnabled(IDC_WINDOW_PIN_TAB,
1087 !browser_->is_app());
[email protected]5d98294912012-06-27 22:57:401088
1089 // Page-related commands
1090 window()->SetStarredState(
[email protected]1c5119c2012-09-19 00:08:571091 BookmarkTabHelper::FromWebContents(current_web_contents)->is_starred());
[email protected]5423c372012-08-22 05:50:161092 window()->ZoomChangedForActiveTab(false);
[email protected]5d98294912012-06-27 22:57:401093 command_updater_.UpdateCommandEnabled(IDC_VIEW_SOURCE,
1094 CanViewSource(browser_));
1095 command_updater_.UpdateCommandEnabled(IDC_EMAIL_PAGE_LOCATION,
1096 CanEmailPageLocation(browser_));
1097 if (browser_->is_devtools())
1098 command_updater_.UpdateCommandEnabled(IDC_OPEN_FILE, false);
1099
Alan Cutter81ac726682018-09-25 00:18:361100 bool can_create_bookmark_app = CanCreateBookmarkApp(browser_);
1101 command_updater_.UpdateCommandEnabled(IDC_INSTALL_PWA,
1102 can_create_bookmark_app);
1103 command_updater_.UpdateCommandEnabled(IDC_CREATE_SHORTCUT,
1104 can_create_bookmark_app);
Alan Cutter403cebb2018-05-17 05:22:451105 command_updater_.UpdateCommandEnabled(IDC_OPEN_IN_PWA_WINDOW,
Alan Cutter81ac726682018-09-25 00:18:361106 can_create_bookmark_app);
[email protected]5d98294912012-06-27 22:57:401107
[email protected]9b7ab882012-09-10 23:46:361108 command_updater_.UpdateCommandEnabled(
1109 IDC_TOGGLE_REQUEST_TABLET_SITE,
1110 CanRequestTabletSite(current_web_contents));
1111
[email protected]5d98294912012-06-27 22:57:401112 UpdateCommandsForContentRestrictionState();
1113 UpdateCommandsForBookmarkEditing();
[email protected]3cac87232012-11-20 01:48:271114 UpdateCommandsForFind();
apacible45cbfc92015-09-28 22:45:411115 UpdateCommandsForMediaRouter();
[email protected]d93dbd12014-08-04 23:42:531116 // Update the zoom commands when an active tab is selected.
1117 UpdateCommandsForZoomState();
1118}
1119
1120void BrowserCommandController::UpdateCommandsForZoomState() {
Aran Gilmand217b5b2019-04-11 17:45:431121 WebContents* contents = browser_->tab_strip_model()->GetActiveWebContents();
[email protected]d93dbd12014-08-04 23:42:531122 if (!contents)
1123 return;
Aran Gilmand217b5b2019-04-11 17:45:431124 command_updater_.UpdateCommandEnabled(IDC_ZOOM_PLUS, CanZoomIn(contents));
a.sarkar.arundaadc712015-02-26 05:39:081125 command_updater_.UpdateCommandEnabled(IDC_ZOOM_NORMAL,
ccameronb7c1d6c2015-03-09 17:08:241126 CanResetZoom(contents));
Aran Gilmand217b5b2019-04-11 17:45:431127 command_updater_.UpdateCommandEnabled(IDC_ZOOM_MINUS, CanZoomOut(contents));
[email protected]5d98294912012-06-27 22:57:401128}
1129
1130void BrowserCommandController::UpdateCommandsForContentRestrictionState() {
1131 int restrictions = GetContentRestrictions(browser_);
1132
1133 command_updater_.UpdateCommandEnabled(
[email protected]3c71576ce2013-07-23 02:00:011134 IDC_COPY, !(restrictions & CONTENT_RESTRICTION_COPY));
[email protected]5d98294912012-06-27 22:57:401135 command_updater_.UpdateCommandEnabled(
[email protected]3c71576ce2013-07-23 02:00:011136 IDC_CUT, !(restrictions & CONTENT_RESTRICTION_CUT));
[email protected]5d98294912012-06-27 22:57:401137 command_updater_.UpdateCommandEnabled(
[email protected]3c71576ce2013-07-23 02:00:011138 IDC_PASTE, !(restrictions & CONTENT_RESTRICTION_PASTE));
[email protected]5d98294912012-06-27 22:57:401139 UpdateSaveAsState();
1140 UpdatePrintingState();
1141}
1142
1143void BrowserCommandController::UpdateCommandsForDevTools() {
Ivan Sandrk9669d0e2017-12-15 23:50:201144 if (is_locked_fullscreen_)
1145 return;
1146
Pavol Marko56df0272018-07-04 17:55:041147 bool dev_tools_enabled = DevToolsWindow::AllowDevToolsFor(
1148 profile(), browser_->tab_strip_model()->GetActiveWebContents());
Aran Gilmand217b5b2019-04-11 17:45:431149 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS, dev_tools_enabled);
[email protected]5d98294912012-06-27 22:57:401150 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_CONSOLE,
1151 dev_tools_enabled);
[email protected]2056c3192013-10-21 22:40:511152 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_DEVICES,
1153 dev_tools_enabled);
[email protected]5d98294912012-06-27 22:57:401154 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_INSPECT,
1155 dev_tools_enabled);
[email protected]d16657c2012-09-03 14:25:101156 command_updater_.UpdateCommandEnabled(IDC_DEV_TOOLS_TOGGLE,
1157 dev_tools_enabled);
spqchan2c5d541e2017-10-25 07:07:111158#if defined(OS_MACOSX)
1159 command_updater_.UpdateCommandEnabled(IDC_TOGGLE_JAVASCRIPT_APPLE_EVENTS,
1160 dev_tools_enabled);
1161#endif
[email protected]5d98294912012-06-27 22:57:401162}
1163
1164void BrowserCommandController::UpdateCommandsForBookmarkEditing() {
Ivan Sandrk9669d0e2017-12-15 23:50:201165 if (is_locked_fullscreen_)
1166 return;
1167
[email protected]5d98294912012-06-27 22:57:401168 command_updater_.UpdateCommandEnabled(IDC_BOOKMARK_PAGE,
1169 CanBookmarkCurrentPage(browser_));
1170 command_updater_.UpdateCommandEnabled(IDC_BOOKMARK_ALL_TABS,
1171 CanBookmarkAllTabs(browser_));
thestig5149d272014-10-30 07:25:291172#if defined(OS_WIN)
1173 command_updater_.UpdateCommandEnabled(IDC_PIN_TO_START_SCREEN, true);
1174#endif
[email protected]5d98294912012-06-27 22:57:401175}
1176
1177void BrowserCommandController::UpdateCommandsForBookmarkBar() {
Ivan Sandrk9669d0e2017-12-15 23:50:201178 if (is_locked_fullscreen_)
1179 return;
1180
tfarina3bddbe112014-08-28 05:29:321181 command_updater_.UpdateCommandEnabled(
Aran Gilmand217b5b2019-04-11 17:45:431182 IDC_SHOW_BOOKMARK_BAR, browser_defaults::bookmarks_enabled &&
1183 !profile()->IsGuestSession() &&
1184 !profile()->IsSystemProfile() &&
1185 !profile()->GetPrefs()->IsManagedPreference(
1186 bookmarks::prefs::kShowBookmarkBar) &&
1187 IsShowingMainUI());
[email protected]5d98294912012-06-27 22:57:401188}
1189
[email protected]9ec3ea5b2012-12-03 18:14:301190void BrowserCommandController::UpdateCommandsForFileSelectionDialogs() {
Ivan Sandrk9669d0e2017-12-15 23:50:201191 if (is_locked_fullscreen_)
1192 return;
1193
[email protected]9ec3ea5b2012-12-03 18:14:301194 UpdateSaveAsState();
[email protected]05454532013-01-22 21:09:081195 UpdateOpenFileState(&command_updater_);
[email protected]9ec3ea5b2012-12-03 18:14:301196}
1197
[email protected]32dfede2013-08-25 15:48:251198void BrowserCommandController::UpdateCommandsForFullscreenMode() {
Ivan Sandrk9669d0e2017-12-15 23:50:201199 if (is_locked_fullscreen_)
1200 return;
1201
zijiehe3c7af992017-02-12 20:59:401202 const bool is_fullscreen = window() && window()->IsFullscreen();
1203 const bool show_main_ui = IsShowingMainUI();
manukf1a116392018-10-25 16:28:241204 const bool show_location_bar = IsShowingLocationBar();
1205
zijiehe3c7af992017-02-12 20:59:401206 const bool main_not_fullscreen = show_main_ui && !is_fullscreen;
[email protected]5d98294912012-06-27 22:57:401207
1208 // Navigation commands
1209 command_updater_.UpdateCommandEnabled(IDC_OPEN_CURRENT_URL, show_main_ui);
1210
1211 // Window management commands
1212 command_updater_.UpdateCommandEnabled(
Aran Gilmand217b5b2019-04-11 17:45:431213 IDC_SHOW_AS_TAB, !browser_->is_type_tabbed() && !is_fullscreen);
[email protected]5d98294912012-06-27 22:57:401214
1215 // Focus various bits of UI
1216 command_updater_.UpdateCommandEnabled(IDC_FOCUS_TOOLBAR, show_main_ui);
manukf1a116392018-10-25 16:28:241217 command_updater_.UpdateCommandEnabled(IDC_FOCUS_LOCATION, show_location_bar);
[email protected]5d98294912012-06-27 22:57:401218 command_updater_.UpdateCommandEnabled(IDC_FOCUS_SEARCH, show_main_ui);
Aran Gilmand217b5b2019-04-11 17:45:431219 command_updater_.UpdateCommandEnabled(IDC_FOCUS_MENU_BAR,
1220 main_not_fullscreen);
1221 command_updater_.UpdateCommandEnabled(IDC_FOCUS_NEXT_PANE,
1222 main_not_fullscreen);
1223 command_updater_.UpdateCommandEnabled(IDC_FOCUS_PREVIOUS_PANE,
1224 main_not_fullscreen);
1225 command_updater_.UpdateCommandEnabled(IDC_FOCUS_BOOKMARKS,
1226 main_not_fullscreen);
[email protected]822ca8c62013-04-19 00:49:151227 command_updater_.UpdateCommandEnabled(
David Tsengc0b1b642018-01-24 07:12:271228 IDC_FOCUS_INACTIVE_POPUP_FOR_ACCESSIBILITY, main_not_fullscreen);
[email protected]5d98294912012-06-27 22:57:401229
1230 // Show various bits of UI
1231 command_updater_.UpdateCommandEnabled(IDC_DEVELOPER_MENU, show_main_ui);
[email protected]236ad3022013-09-04 03:27:431232#if defined(GOOGLE_CHROME_BUILD)
Fabio Rocha19bf4262019-04-04 01:05:181233 command_updater_.UpdateCommandEnabled(
1234 IDC_FEEDBACK, show_main_ui || browser_->is_devtools());
[email protected]236ad3022013-09-04 03:27:431235#endif
Mihai Sardarescu2ec5205682018-09-26 09:20:051236 UpdateShowSyncState(show_main_ui);
[email protected]5d98294912012-06-27 22:57:401237
[email protected]5d98294912012-06-27 22:57:401238 command_updater_.UpdateCommandEnabled(IDC_EDIT_SEARCH_ENGINES, show_main_ui);
1239 command_updater_.UpdateCommandEnabled(IDC_VIEW_PASSWORDS, show_main_ui);
1240 command_updater_.UpdateCommandEnabled(IDC_ABOUT, show_main_ui);
1241 command_updater_.UpdateCommandEnabled(IDC_SHOW_APP_MENU, show_main_ui);
Tina Wangf09681ac2019-03-27 20:52:101242 command_updater_.UpdateCommandEnabled(IDC_SEND_TAB_TO_SELF, show_main_ui);
Yann Dago70ca3dd2019-02-27 01:12:351243 command_updater_.UpdateCommandEnabled(IDC_SHOW_MANAGEMENT_PAGE, true);
primianod3a81ab2016-01-25 22:21:151244
1245 if (base::debug::IsProfilingSupported())
1246 command_updater_.UpdateCommandEnabled(IDC_PROFILING_ENABLED, show_main_ui);
[email protected]5d98294912012-06-27 22:57:401247
[email protected]aeafc3852014-04-29 16:51:291248#if !defined(OS_MACOSX)
zijiehe3c7af992017-02-12 20:59:401249 // Disable toggling into fullscreen mode if disallowed by pref.
Aran Gilmand217b5b2019-04-11 17:45:431250 const bool fullscreen_enabled =
1251 is_fullscreen ||
zijiehe3c7af992017-02-12 20:59:401252 profile()->GetPrefs()->GetBoolean(prefs::kFullscreenAllowed);
1253#else
1254 const bool fullscreen_enabled = true;
[email protected]00a1cc5b2012-11-07 13:44:511255#endif
1256
1257 command_updater_.UpdateCommandEnabled(IDC_FULLSCREEN, fullscreen_enabled);
spqchanb8ffc7d2015-11-17 01:17:211258 command_updater_.UpdateCommandEnabled(IDC_TOGGLE_FULLSCREEN_TOOLBAR,
1259 fullscreen_enabled);
[email protected]5d98294912012-06-27 22:57:401260
1261 UpdateCommandsForBookmarkBar();
zijiehedb473d552017-02-24 01:13:411262 UpdateCommandsForIncognitoAvailability();
Alan Cutter167dfe82018-04-11 09:06:001263 UpdateCommandsForHostedAppAvailability();
1264}
1265
1266void BrowserCommandController::UpdateCommandsForHostedAppAvailability() {
1267 bool has_toolbar =
1268 browser_->is_type_tabbed() ||
Eric Willigers4a5f7a92019-05-10 19:19:261269 web_app::AppBrowserController::IsForExperimentalWebAppBrowser(browser_);
Alan Cutter167dfe82018-04-11 09:06:001270 if (window() && window()->ShouldHideUIForFullscreen())
1271 has_toolbar = false;
1272 command_updater_.UpdateCommandEnabled(IDC_FOCUS_TOOLBAR, has_toolbar);
1273 command_updater_.UpdateCommandEnabled(IDC_FOCUS_NEXT_PANE, has_toolbar);
1274 command_updater_.UpdateCommandEnabled(IDC_FOCUS_PREVIOUS_PANE, has_toolbar);
1275 command_updater_.UpdateCommandEnabled(IDC_SHOW_APP_MENU, has_toolbar);
[email protected]5d98294912012-06-27 22:57:401276}
1277
Ivan Sandrk9669d0e2017-12-15 23:50:201278#if defined(OS_CHROMEOS)
1279namespace {
1280
1281#if DCHECK_IS_ON()
1282// Makes sure that all commands that are not whitelisted are disabled. DCHECKs
1283// otherwise. Compiled only in debug mode.
1284void NonWhitelistedCommandsAreDisabled(CommandUpdaterImpl* command_updater) {
Ivan Sandrk6ac9c9a2018-12-14 14:15:071285 constexpr int kWhitelistedIds[] = {IDC_CUT, IDC_COPY, IDC_PASTE};
Ivan Sandrk9669d0e2017-12-15 23:50:201286
1287 // Go through all the command ids, skip the whitelisted ones.
1288 for (int id : command_updater->GetAllIds()) {
Haeun Kim3f6123502018-08-26 18:03:041289 if (base::ContainsValue(kWhitelistedIds, id)) {
Ivan Sandrk9669d0e2017-12-15 23:50:201290 continue;
1291 }
1292 DCHECK(!command_updater->IsCommandEnabled(id));
1293 }
1294}
1295#endif
1296
1297} // namespace
1298
1299void BrowserCommandController::UpdateCommandsForLockedFullscreenMode() {
Ivan Sandrke0e92452019-03-21 09:32:481300 bool is_locked_fullscreen =
1301 platform_util::IsBrowserLockedFullscreen(browser_);
Ivan Sandrk9669d0e2017-12-15 23:50:201302 // Sanity check to make sure this function is called only on state change.
1303 DCHECK_NE(is_locked_fullscreen, is_locked_fullscreen_);
1304 if (is_locked_fullscreen == is_locked_fullscreen_)
1305 return;
1306 is_locked_fullscreen_ = is_locked_fullscreen;
1307
1308 if (is_locked_fullscreen_) {
1309 command_updater_.DisableAllCommands();
1310 // Update the state of whitelisted commands:
1311 // IDC_CUT/IDC_COPY/IDC_PASTE,
1312 UpdateCommandsForContentRestrictionState();
Ivan Sandrk6ac9c9a2018-12-14 14:15:071313 // TODO(crbug.com/904637): Re-enable Find and Zoom in locked fullscreen.
Ivan Sandrk9669d0e2017-12-15 23:50:201314 // All other commands will be disabled (there is an early return in their
1315 // corresponding UpdateCommandsFor* functions).
1316#if DCHECK_IS_ON()
1317 NonWhitelistedCommandsAreDisabled(&command_updater_);
1318#endif
1319 } else {
1320 // Do an init call to re-initialize command state after the
1321 // DisableAllCommands.
1322 InitCommandState();
1323 }
1324}
1325#endif
1326
[email protected]5d98294912012-06-27 22:57:401327void BrowserCommandController::UpdatePrintingState() {
Ivan Sandrk9669d0e2017-12-15 23:50:201328 if (is_locked_fullscreen_)
1329 return;
1330
[email protected]d53e4032012-06-29 18:58:341331 bool print_enabled = CanPrint(browser_);
1332 command_updater_.UpdateCommandEnabled(IDC_PRINT, print_enabled);
Lei Zhang48a4a5262018-04-17 20:18:441333#if BUILDFLAG(ENABLE_PRINTING)
vitalybukaf9433e42014-09-08 10:04:551334 command_updater_.UpdateCommandEnabled(IDC_BASIC_PRINT,
1335 CanBasicPrint(browser_));
Lei Zhang48a4a5262018-04-17 20:18:441336#endif
[email protected]5d98294912012-06-27 22:57:401337}
1338
1339void BrowserCommandController::UpdateSaveAsState() {
Ivan Sandrk9669d0e2017-12-15 23:50:201340 if (is_locked_fullscreen_)
1341 return;
1342
[email protected]5d98294912012-06-27 22:57:401343 command_updater_.UpdateCommandEnabled(IDC_SAVE_PAGE, CanSavePage(browser_));
1344}
1345
Mihai Sardarescu2ec5205682018-09-26 09:20:051346void BrowserCommandController::UpdateShowSyncState(bool show_main_ui) {
1347 if (is_locked_fullscreen_)
1348 return;
1349
Mihai Sardarescuf87ccc12018-09-26 10:37:451350 command_updater_.UpdateCommandEnabled(
Mihai Sardarescu290bc882018-10-12 10:48:141351 IDC_SHOW_SIGNIN, show_main_ui && pref_signin_allowed_.GetValue());
Mihai Sardarescu2ec5205682018-09-26 09:20:051352}
1353
[email protected]05454532013-01-22 21:09:081354// static
1355void BrowserCommandController::UpdateOpenFileState(
1356 CommandUpdater* command_updater) {
[email protected]5d98294912012-06-27 22:57:401357 bool enabled = true;
1358 PrefService* local_state = g_browser_process->local_state();
1359 if (local_state)
1360 enabled = local_state->GetBoolean(prefs::kAllowFileSelectionDialogs);
1361
[email protected]05454532013-01-22 21:09:081362 command_updater->UpdateCommandEnabled(IDC_OPEN_FILE, enabled);
[email protected]5d98294912012-06-27 22:57:401363}
1364
1365void BrowserCommandController::UpdateReloadStopState(bool is_loading,
1366 bool force) {
Ivan Sandrk9669d0e2017-12-15 23:50:201367 if (is_locked_fullscreen_)
1368 return;
1369
[email protected]5d98294912012-06-27 22:57:401370 window()->UpdateReloadStopState(is_loading, force);
1371 command_updater_.UpdateCommandEnabled(IDC_STOP, is_loading);
1372}
1373
[email protected]2e9369e2014-08-15 09:12:531374void BrowserCommandController::UpdateTabRestoreCommandState() {
Ivan Sandrk9669d0e2017-12-15 23:50:201375 if (is_locked_fullscreen_)
1376 return;
1377
blundell74001adc2015-09-18 11:04:251378 sessions::TabRestoreService* tab_restore_service =
[email protected]2e9369e2014-08-15 09:12:531379 TabRestoreServiceFactory::GetForProfile(profile());
1380 // The command is enabled if the service hasn't loaded yet to trigger loading.
1381 // The command is updated once the load completes.
1382 command_updater_.UpdateCommandEnabled(
1383 IDC_RESTORE_TAB,
1384 tab_restore_service &&
Aran Gilmand217b5b2019-04-11 17:45:431385 (!tab_restore_service->IsLoaded() ||
1386 GetRestoreTabType(browser_) != TabStripModelDelegate::RESTORE_NONE));
[email protected]2e9369e2014-08-15 09:12:531387}
1388
[email protected]3cac87232012-11-20 01:48:271389void BrowserCommandController::UpdateCommandsForFind() {
1390 TabStripModel* model = browser_->tab_strip_model();
Aran Gilmand217b5b2019-04-11 17:45:431391 bool enabled =
1392 !model->IsTabBlocked(model->active_index()) && !browser_->is_devtools();
[email protected]3cac87232012-11-20 01:48:271393
1394 command_updater_.UpdateCommandEnabled(IDC_FIND, enabled);
1395 command_updater_.UpdateCommandEnabled(IDC_FIND_NEXT, enabled);
1396 command_updater_.UpdateCommandEnabled(IDC_FIND_PREVIOUS, enabled);
1397}
1398
apacible45cbfc92015-09-28 22:45:411399void BrowserCommandController::UpdateCommandsForMediaRouter() {
Ivan Sandrk9669d0e2017-12-15 23:50:201400 if (is_locked_fullscreen_)
1401 return;
1402
apacible45cbfc92015-09-28 22:45:411403 command_updater_.UpdateCommandEnabled(IDC_ROUTE_MEDIA,
1404 CanRouteMedia(browser_));
1405}
1406
[email protected]409ea2972012-11-10 19:54:431407void BrowserCommandController::AddInterstitialObservers(WebContents* contents) {
[email protected]20ca0382013-02-28 19:50:071408 interstitial_observers_.push_back(new InterstitialObserver(this, contents));
[email protected]5d98294912012-06-27 22:57:401409}
1410
1411void BrowserCommandController::RemoveInterstitialObservers(
[email protected]e89cfcb2012-11-11 14:47:241412 WebContents* contents) {
[email protected]20ca0382013-02-28 19:50:071413 for (size_t i = 0; i < interstitial_observers_.size(); i++) {
1414 if (interstitial_observers_[i]->web_contents() != contents)
1415 continue;
1416
1417 delete interstitial_observers_[i];
1418 interstitial_observers_.erase(interstitial_observers_.begin() + i);
1419 return;
1420 }
[email protected]5d98294912012-06-27 22:57:401421}
1422
1423BrowserWindow* BrowserCommandController::window() {
1424 return browser_->window();
1425}
1426
1427Profile* BrowserCommandController::profile() {
1428 return browser_->profile();
1429}
1430
[email protected]5d98294912012-06-27 22:57:401431} // namespace chrome