1 // 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.
5 #ifndef CHROME_BROWSER_UI_BROWSER_COMMAND_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_BROWSER_COMMAND_CONTROLLER_H_
10 #include "base/prefs/pref_change_registrar.h"
11 #include "base/prefs/pref_member.h"
12 #include "chrome/browser/command_updater.h"
13 #include "chrome/browser/command_updater_delegate.h"
14 #include "chrome/browser/sessions/tab_restore_service_observer.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
16 #include "ui/base/window_open_disposition.h"
23 struct NativeWebKeyboardEvent;
28 class BrowserCommandController : public CommandUpdaterDelegate,
29 public TabStripModelObserver,
30 public TabRestoreServiceObserver {
32 explicit BrowserCommandController(Browser* browser);
33 ~BrowserCommandController() override;
35 CommandUpdater* command_updater() { return &command_updater_; }
36 bool block_command_execution() const { return block_command_execution_; }
38 // Returns true if |command_id| is a reserved command whose keyboard shortcuts
39 // should not be sent to the renderer or |event| was triggered by a key that
40 // we never want to send to the renderer.
41 bool IsReservedCommandOrKey(int command_id,
42 const content::NativeWebKeyboardEvent& event);
44 // Sets if command execution shall be blocked. If |block| is true then
45 // following calls to ExecuteCommand() or ExecuteCommandWithDisposition()
46 // method will not execute the command, and the last blocked command will be
47 // recorded for retrieval.
48 void SetBlockCommandExecution(bool block);
50 // Gets the last blocked command after calling SetBlockCommandExecution(true).
51 // Returns the command id or -1 if there is no command blocked. The
52 // disposition type of the command will be stored in |*disposition| if it's
54 int GetLastBlockedCommand(WindowOpenDisposition* disposition);
56 // Notifies the controller that state has changed in one of the following
57 // areas and it should update command states.
58 void TabStateChanged();
59 void ZoomStateChanged();
60 void ContentRestrictionsChanged();
61 void FullscreenStateChanged();
62 void PrintingStateChanged();
63 void LoadingStateChanged(bool is_loading, bool force);
65 // Shared state updating: these functions are static and public to share with
68 // Updates the open-file state.
69 static void UpdateOpenFileState(CommandUpdater* command_updater);
71 // Update commands whose state depends on incognito mode availability and that
72 // only depend on the profile.
73 static void UpdateSharedCommandsForIncognitoAvailability(
74 CommandUpdater* command_updater,
78 class InterstitialObserver;
80 // Overridden from CommandUpdaterDelegate:
81 void ExecuteCommandWithDisposition(int id, WindowOpenDisposition disposition)
84 // Overridden from TabStripModelObserver:
85 void TabInsertedAt(content::WebContents* contents,
87 bool foreground) override;
88 void TabDetachedAt(content::WebContents* contents, int index) override;
89 void TabReplacedAt(TabStripModel* tab_strip_model,
90 content::WebContents* old_contents,
91 content::WebContents* new_contents,
93 void TabBlockedStateChanged(content::WebContents* contents,
96 // Overridden from TabRestoreServiceObserver:
97 void TabRestoreServiceChanged(TabRestoreService* service) override;
98 void TabRestoreServiceDestroyed(TabRestoreService* service) override;
99 void TabRestoreServiceLoaded(TabRestoreService* service) override;
101 // Returns true if the regular Chrome UI (not the fullscreen one and
102 // not the single-tab one) is shown. Used for updating window command states
103 // only. Consider using SupportsWindowFeature if you need the mentioned
104 // functionality anywhere else.
105 bool IsShowingMainUI();
107 // Initialize state for all browser commands.
108 void InitCommandState();
110 // Update commands whose state depends on incognito mode availability.
111 void UpdateCommandsForIncognitoAvailability();
113 // Update commands whose state depends on the tab's state.
114 void UpdateCommandsForTabState();
116 // Update Zoom commands based on zoom state.
117 void UpdateCommandsForZoomState();
119 // Updates commands when the content's restrictions change.
120 void UpdateCommandsForContentRestrictionState();
122 // Updates commands for enabling developer tools.
123 void UpdateCommandsForDevTools();
125 // Updates commands for bookmark editing.
126 void UpdateCommandsForBookmarkEditing();
128 // Updates commands that affect the bookmark bar.
129 void UpdateCommandsForBookmarkBar();
131 // Updates commands that affect file selection dialogs in aggregate,
132 // namely the save-page-as state and the open-file state.
133 void UpdateCommandsForFileSelectionDialogs();
135 // Update commands whose state depends on the type of fullscreen mode the
137 void UpdateCommandsForFullscreenMode();
139 // Updates the printing command state.
140 void UpdatePrintingState();
142 // Updates the SHOW_SYNC_SETUP menu entry.
143 void OnSigninAllowedPrefChange();
145 // Updates the save-page-as command state.
146 void UpdateSaveAsState();
148 // Updates the show-sync command state.
149 void UpdateShowSyncState(bool show_main_ui);
151 // Ask the Reload/Stop button to change its icon, and update the Stop command
152 // state. |is_loading| is true if the current WebContents is loading.
153 // |force| is true if the button should change its icon immediately.
154 void UpdateReloadStopState(bool is_loading, bool force);
156 // Updates commands for find.
157 void UpdateCommandsForFind();
159 void UpdateTabRestoreCommandState();
161 // Add/remove observers for interstitial attachment/detachment from
163 void AddInterstitialObservers(content::WebContents* contents);
164 void RemoveInterstitialObservers(content::WebContents* contents);
166 inline BrowserWindow* window();
167 inline Profile* profile();
171 // The CommandUpdater that manages the browser window commands.
172 CommandUpdater command_updater_;
174 // Indicates if command execution is blocked.
175 bool block_command_execution_;
177 // Stores the last blocked command id when |block_command_execution_| is true.
178 int last_blocked_command_id_;
180 // Stores the disposition type of the last blocked command.
181 WindowOpenDisposition last_blocked_command_disposition_;
183 std::vector<InterstitialObserver*> interstitial_observers_;
185 PrefChangeRegistrar profile_pref_registrar_;
186 PrefChangeRegistrar local_pref_registrar_;
187 BooleanPrefMember pref_signin_allowed_;
189 DISALLOW_COPY_AND_ASSIGN(BrowserCommandController);
192 } // namespace chrome
194 #endif // CHROME_BROWSER_UI_BROWSER_COMMAND_CONTROLLER_H_