ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / ui / browser_command_controller.h
blob05ec35a47e0daaa4934b7ac40648216e08c41a03
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_
8 #include <vector>
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"
18 class Browser;
19 class BrowserWindow;
20 class Profile;
22 namespace content {
23 struct NativeWebKeyboardEvent;
26 namespace chrome {
28 class BrowserCommandController : public CommandUpdaterDelegate,
29 public TabStripModelObserver,
30 public TabRestoreServiceObserver {
31 public:
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
53 // not NULL.
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);
64 void ExtensionStateChanged();
66 // Shared state updating: these functions are static and public to share with
67 // outside code.
69 // Updates the open-file state.
70 static void UpdateOpenFileState(CommandUpdater* command_updater);
72 // Update commands whose state depends on incognito mode availability and that
73 // only depend on the profile.
74 static void UpdateSharedCommandsForIncognitoAvailability(
75 CommandUpdater* command_updater,
76 Profile* profile);
78 private:
79 class InterstitialObserver;
81 // Overridden from CommandUpdaterDelegate:
82 void ExecuteCommandWithDisposition(int id, WindowOpenDisposition disposition)
83 override;
85 // Overridden from TabStripModelObserver:
86 void TabInsertedAt(content::WebContents* contents,
87 int index,
88 bool foreground) override;
89 void TabDetachedAt(content::WebContents* contents, int index) override;
90 void TabReplacedAt(TabStripModel* tab_strip_model,
91 content::WebContents* old_contents,
92 content::WebContents* new_contents,
93 int index) override;
94 void TabBlockedStateChanged(content::WebContents* contents,
95 int index) override;
97 // Overridden from TabRestoreServiceObserver:
98 void TabRestoreServiceChanged(TabRestoreService* service) override;
99 void TabRestoreServiceDestroyed(TabRestoreService* service) override;
100 void TabRestoreServiceLoaded(TabRestoreService* service) override;
102 // Returns true if the regular Chrome UI (not the fullscreen one and
103 // not the single-tab one) is shown. Used for updating window command states
104 // only. Consider using SupportsWindowFeature if you need the mentioned
105 // functionality anywhere else.
106 bool IsShowingMainUI();
108 // Initialize state for all browser commands.
109 void InitCommandState();
111 // Update commands whose state depends on incognito mode availability.
112 void UpdateCommandsForIncognitoAvailability();
114 // Update commands whose state depends on the tab's state.
115 void UpdateCommandsForTabState();
117 // Update Zoom commands based on zoom state.
118 void UpdateCommandsForZoomState();
120 // Updates commands when the content's restrictions change.
121 void UpdateCommandsForContentRestrictionState();
123 // Updates commands for enabling developer tools.
124 void UpdateCommandsForDevTools();
126 // Updates commands for bookmark editing.
127 void UpdateCommandsForBookmarkEditing();
129 // Updates commands that affect the bookmark bar.
130 void UpdateCommandsForBookmarkBar();
132 // Updates commands that affect file selection dialogs in aggregate,
133 // namely the save-page-as state and the open-file state.
134 void UpdateCommandsForFileSelectionDialogs();
136 // Update commands whose state depends on the type of fullscreen mode the
137 // window is in.
138 void UpdateCommandsForFullscreenMode();
140 // Updates the printing command state.
141 void UpdatePrintingState();
143 // Updates the SHOW_SYNC_SETUP menu entry.
144 void OnSigninAllowedPrefChange();
146 // Updates the save-page-as command state.
147 void UpdateSaveAsState();
149 // Updates the show-sync command state.
150 void UpdateShowSyncState(bool show_main_ui);
152 // Ask the Reload/Stop button to change its icon, and update the Stop command
153 // state. |is_loading| is true if the current WebContents is loading.
154 // |force| is true if the button should change its icon immediately.
155 void UpdateReloadStopState(bool is_loading, bool force);
157 // Updates commands for find.
158 void UpdateCommandsForFind();
160 void UpdateTabRestoreCommandState();
162 // Add/remove observers for interstitial attachment/detachment from
163 // |contents|.
164 void AddInterstitialObservers(content::WebContents* contents);
165 void RemoveInterstitialObservers(content::WebContents* contents);
167 inline BrowserWindow* window();
168 inline Profile* profile();
170 Browser* browser_;
172 // The CommandUpdater that manages the browser window commands.
173 CommandUpdater command_updater_;
175 // Indicates if command execution is blocked.
176 bool block_command_execution_;
178 // Stores the last blocked command id when |block_command_execution_| is true.
179 int last_blocked_command_id_;
181 // Stores the disposition type of the last blocked command.
182 WindowOpenDisposition last_blocked_command_disposition_;
184 std::vector<InterstitialObserver*> interstitial_observers_;
186 PrefChangeRegistrar profile_pref_registrar_;
187 PrefChangeRegistrar local_pref_registrar_;
188 BooleanPrefMember pref_signin_allowed_;
190 DISALLOW_COPY_AND_ASSIGN(BrowserCommandController);
193 } // namespace chrome
195 #endif // CHROME_BROWSER_UI_BROWSER_COMMAND_CONTROLLER_H_