1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_BROWSER_COMMAND_CONTROLLER_H_
#define CHROME_BROWSER_UI_BROWSER_COMMAND_CONTROLLER_H_
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "chrome/browser/command_updater.h"
#include "chrome/browser/command_updater_delegate.h"
#include "chrome/browser/command_updater_impl.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "chrome/browser/ui/webui/side_panel/customize_chrome/customize_chrome_section.h"
#include "chrome/common/buildflags.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_member.h"
#include "components/sessions/core/tab_restore_service_observer.h"
#include "content/public/browser/web_contents_observer.h"
#include "ui/actions/actions.h"
#include "ui/base/window_open_disposition.h"
class Browser;
class BrowserWindow;
class Profile;
namespace input {
struct NativeWebKeyboardEvent;
}
namespace chrome {
// This class needs to expose the internal command_updater_ in some way, hence
// it implements CommandUpdater as the public API for it (so it's not directly
// exposed).
class BrowserCommandController : public CommandUpdater,
public TabStripModelObserver,
public sessions::TabRestoreServiceObserver {
public:
explicit BrowserCommandController(Browser* browser);
BrowserCommandController(const BrowserCommandController&) = delete;
BrowserCommandController& operator=(const BrowserCommandController&) = delete;
~BrowserCommandController() override;
// Returns true if |command_id| is a reserved command whose keyboard shortcuts
// should not be sent to the renderer or |event| was triggered by a key that
// we never want to send to the renderer.
bool IsReservedCommandOrKey(int command_id,
const input::NativeWebKeyboardEvent& event);
// Notifies the controller that state has changed in one of the following
// areas and it should update command states.
void TabStateChanged();
void ZoomStateChanged();
void ContentRestrictionsChanged();
void FullscreenStateChanged();
#if BUILDFLAG(IS_CHROMEOS)
// Called when the browser goes in or out of the special locked fullscreen
// mode. In this mode the user is basically locked into the current browser
// window and tab hence we disable most keyboard shortcuts and we also
// prevent changing the state of enabled shortcuts while in this mode (so the
// other *Changed() functions will be a NO-OP in this state).
void LockedFullscreenStateChanged();
#endif
void PrintingStateChanged();
#if BUILDFLAG(ENABLE_GLIC)
void GlicWindowActivationChanged(bool active);
#endif
void LoadingStateChanged(bool is_loading, bool force);
void FindBarVisibilityChanged();
void ExtensionStateChanged();
void TabKeyboardFocusChangedTo(std::optional<int> index);
void WebContentsFocusChanged();
// Overriden from CommandUpdater:
bool SupportsCommand(int id) const override;
bool IsCommandEnabled(int id) const override;
using CommandUpdater::ExecuteCommand;
bool ExecuteCommand(int id, base::TimeTicks time_stamp) override;
using CommandUpdater::ExecuteCommandWithDisposition;
bool ExecuteCommandWithDisposition(int id,
WindowOpenDisposition disposition,
base::TimeTicks time_stamp) override;
void AddCommandObserver(int id, CommandObserver* observer) override;
void RemoveCommandObserver(int id, CommandObserver* observer) override;
void RemoveCommandObserver(CommandObserver* observer) override;
bool UpdateCommandEnabled(int id, bool state) override;
// Shared state updating: these functions are static and public to share with
// outside code.
// Update commands whose state depends on incognito mode availability and that
// only depend on the profile.
static void UpdateSharedCommandsForIncognitoAvailability(
CommandUpdater* command_updater,
Profile* profile);
private:
#if BUILDFLAG(IS_CHROMEOS)
friend class BrowserCommandControllerBrowserTestLockedFullscreen;
#endif
// Overridden from TabStripModelObserver:
void OnTabStripModelChanged(
TabStripModel* tab_strip_model,
const TabStripModelChange& change,
const TabStripSelectionChange& selection) override;
void TabBlockedStateChanged(content::WebContents* contents,
int index) override;
// Overridden from TabRestoreServiceObserver:
void TabRestoreServiceChanged(sessions::TabRestoreService* service) override;
void TabRestoreServiceDestroyed(
sessions::TabRestoreService* service) override;
void TabRestoreServiceLoaded(sessions::TabRestoreService* service) override;
// Returns true if the regular Chrome UI (not the fullscreen one and
// not the single-tab one) is shown. Used for updating window command states
// only. Consider using SupportsWindowFeature if you need the mentioned
// functionality anywhere else.
bool IsShowingMainUI();
// Returns true if the location bar is shown or is currently hidden, but can
// be shown. Used for updating window command states only.
bool IsShowingLocationBar();
// Initialize state for all browser commands.
void InitCommandState();
// Update commands whose state depends on incognito mode availability.
void UpdateCommandsForIncognitoAvailability();
// Update commands whose state depends on the tab's state.
void UpdateCommandsForTabState();
// Update Zoom commands based on zoom state.
void UpdateCommandsForZoomState();
// Updates commands when the content's restrictions change.
void UpdateCommandsForContentRestrictionState();
// Updates commands for enabling developer tools.
void UpdateCommandsForDevTools();
// Updates commands for bookmark editing.
void UpdateCommandsForBookmarkEditing();
// Updates commands that affect the bookmark bar.
void UpdateCommandsForBookmarkBar();
// Updates commands that affect file selection dialogs in aggregate,
// namely the save-page-as state and the open-file state.
void UpdateCommandsForFileSelectionDialogs();
// Update commands whose state depends on the type of fullscreen mode the
// window is in.
void UpdateCommandsForFullscreenMode();
// Update commands whose state depends on whether they're available to hosted
// app windows.
void UpdateCommandsForHostedAppAvailability();
// Update commands that are used in the Extensions menu in the app menu.
void UpdateCommandsForExtensionsMenu();
#if BUILDFLAG(IS_CHROMEOS)
// Update commands whose state depends on whether the window is in locked
// fullscreen mode or not.
void UpdateCommandsForLockedFullscreenMode();
#endif
// Updates the printing command state.
void UpdatePrintingState();
#if BUILDFLAG(ENABLE_GLIC)
// Updates the Glic command state.
void UpdateGlicState();
#endif
// Updates the SHOW_SYNC_SETUP menu entry.
void OnSigninAllowedPrefChange();
// Updates the save-page-as command state.
void UpdateSaveAsState();
// Ask the Reload/Stop button to change its icon, and update the Stop command
// state. |is_loading| is true if the current WebContents is loading.
// |force| is true if the button should change its icon immediately.
void UpdateReloadStopState(bool is_loading, bool force);
void UpdateTabRestoreCommandState();
// Updates commands for find.
void UpdateCommandsForFind();
// Updates the command to close find or stop loading.
void UpdateCloseFindOrStop();
// Updates commands for Media Router.
void UpdateCommandsForMediaRouter();
// Updates commands for tab keyboard focus state. If |target_index| is
// populated, it is the index of the tab with focus; if it is not populated,
// no tab has keyboard focus.
void UpdateCommandsForTabKeyboardFocus(std::optional<int> target_index);
// Updates commands that depend on whether web contents is focused or not.
void UpdateCommandsForWebContentsFocus();
// Updates commands that depend on the state of the tab strip model.
void UpdateCommandsForTabStripStateChanged();
// Updates commands that depend on the enabled state of glic.
void UpdateCommandsForEnableGlicChanged();
// Returns the relevant action for the current browser for a given
// `action_id`.
actions::ActionItem* FindAction(actions::ActionId action_id);
// Updates the enabled status for both `command_id` and `action_id`, given
// that it exists.
void UpdateCommandAndActionEnabled(int command_id,
actions::ActionId action_id,
bool enabled);
// Helper method to show the customize chrome sidepanel and optionally scroll
// to a specific section.
void ShowCustomizeChromeSidePanel(
std::optional<CustomizeChromeSection> section = std::nullopt);
inline BrowserWindow* window();
inline Profile* profile();
const raw_ptr<Browser> browser_;
// The CommandUpdaterImpl that manages the browser window commands.
CommandUpdaterImpl command_updater_;
PrefChangeRegistrar profile_pref_registrar_;
PrefChangeRegistrar local_pref_registrar_;
std::unique_ptr<base::CallbackListSubscription> glic_enabling_subscription_;
// In locked fullscreen mode disallow enabling/disabling commands.
bool is_locked_fullscreen_ = false;
// If the Customize Chrome side panel is shown, determines which section to
// display.
CustomizeChromeSection customize_chrome_section_ =
CustomizeChromeSection::kUnspecified;
// Callback subscription for listening to changes to the Glic window
// activation changes.
base::CallbackListSubscription glic_window_activation_subscription_;
};
} // namespace chrome
#endif // CHROME_BROWSER_UI_BROWSER_COMMAND_CONTROLLER_H_
|