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. 4 5 #ifndef CHROME_BROWSER_UI_BROWSER_COMMAND_CONTROLLER_H_ 6 #define CHROME_BROWSER_UI_BROWSER_COMMAND_CONTROLLER_H_ 7 8 #include <vector> 9 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/profiles/profile_info_cache_observer.h" 15 #include "chrome/browser/sessions/tab_restore_service_observer.h" 16 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" 17 #include "ui/base/window_open_disposition.h" 18 19 class Browser; 20 class BrowserWindow; 21 class Profile; 22 class ProfileManager; 23 24 namespace content { 25 struct NativeWebKeyboardEvent; 26 } 27 28 namespace chrome { 29 30 class BrowserCommandController : public CommandUpdaterDelegate, 31 public ProfileInfoCacheObserver, 32 public TabStripModelObserver, 33 public TabRestoreServiceObserver { 34 public: 35 BrowserCommandController(Browser* browser, ProfileManager* profile_manager); 36 virtual ~BrowserCommandController(); 37 38 CommandUpdater* command_updater() { return &command_updater_; } 39 bool block_command_execution() const { return block_command_execution_; } 40 41 // Returns true if |command_id| is a reserved command whose keyboard shortcuts 42 // should not be sent to the renderer or |event| was triggered by a key that 43 // we never want to send to the renderer. 44 bool IsReservedCommandOrKey(int command_id, 45 const content::NativeWebKeyboardEvent& event); 46 47 // Sets if command execution shall be blocked. If |block| is true then 48 // following calls to ExecuteCommand() or ExecuteCommandWithDisposition() 49 // method will not execute the command, and the last blocked command will be 50 // recorded for retrieval. 51 void SetBlockCommandExecution(bool block); 52 53 // Gets the last blocked command after calling SetBlockCommandExecution(true). 54 // Returns the command id or -1 if there is no command blocked. The 55 // disposition type of the command will be stored in |*disposition| if it's 56 // not NULL. 57 int GetLastBlockedCommand(WindowOpenDisposition* disposition); 58 59 // Notifies the controller that state has changed in one of the following 60 // areas and it should update command states. 61 void TabStateChanged(); 62 void ContentRestrictionsChanged(); 63 void FullscreenStateChanged(); 64 void PrintingStateChanged(); 65 void LoadingStateChanged(bool is_loading, bool force); 66 67 // Shared state updating: these functions are static and public to share with 68 // outside code. 69 70 // Updates the open-file state. 71 static void UpdateOpenFileState(CommandUpdater* command_updater); 72 73 // Update commands whose state depends on incognito mode availability and that 74 // only depend on the profile. 75 static void UpdateSharedCommandsForIncognitoAvailability( 76 CommandUpdater* command_updater, 77 Profile* profile); 78 79 private: 80 class InterstitialObserver; 81 82 enum FullScreenMode { 83 // Not in fullscreen mode. 84 FULLSCREEN_DISABLED, 85 86 // Fullscreen mode, occupying the whole screen. 87 FULLSCREEN_NORMAL, 88 89 // Fullscreen mode for metro snap, occupying the full height and 20% of 90 // the screen width. 91 FULLSCREEN_METRO_SNAP, 92 }; 93 94 // Overridden from CommandUpdaterDelegate: 95 virtual void ExecuteCommandWithDisposition( 96 int id, 97 WindowOpenDisposition disposition) OVERRIDE; 98 99 // Overridden from ProfileInfoCacheObserver: 100 virtual void OnProfileAdded(const base::FilePath& profile_path) OVERRIDE; 101 virtual void OnProfileWasRemoved(const base::FilePath& profile_path, 102 const string16& profile_name) OVERRIDE; 103 104 // Overridden from TabStripModelObserver: 105 virtual void TabInsertedAt(content::WebContents* contents, 106 int index, 107 bool foreground) OVERRIDE; 108 virtual void TabDetachedAt(content::WebContents* contents, 109 int index) OVERRIDE; 110 virtual void TabReplacedAt(TabStripModel* tab_strip_model, 111 content::WebContents* old_contents, 112 content::WebContents* new_contents, 113 int index) OVERRIDE; 114 virtual void TabBlockedStateChanged(content::WebContents* contents, 115 int index) OVERRIDE; 116 117 // Overridden from TabRestoreServiceObserver: 118 virtual void TabRestoreServiceChanged(TabRestoreService* service) OVERRIDE; 119 virtual void TabRestoreServiceDestroyed(TabRestoreService* service) OVERRIDE; 120 121 // Returns true if the regular Chrome UI (not the fullscreen one and 122 // not the single-tab one) is shown. Used for updating window command states 123 // only. Consider using SupportsWindowFeature if you need the mentioned 124 // functionality anywhere else. 125 bool IsShowingMainUI(); 126 127 // Initialize state for all browser commands. 128 void InitCommandState(); 129 130 // Update commands whose state depends on incognito mode availability. 131 void UpdateCommandsForIncognitoAvailability(); 132 133 // Update commands whose state depends on the tab's state. 134 void UpdateCommandsForTabState(); 135 136 // Updates commands when the content's restrictions change. 137 void UpdateCommandsForContentRestrictionState(); 138 139 // Updates commands for enabling developer tools. 140 void UpdateCommandsForDevTools(); 141 142 // Updates commands for bookmark editing. 143 void UpdateCommandsForBookmarkEditing(); 144 145 // Updates commands that affect the bookmark bar. 146 void UpdateCommandsForBookmarkBar(); 147 148 // Updates commands that affect file selection dialogs in aggregate, 149 // namely the save-page-as state and the open-file state. 150 void UpdateCommandsForFileSelectionDialogs(); 151 152 // Update commands whose state depends on the type of fullscreen mode the 153 // window is in. 154 void UpdateCommandsForFullscreenMode(FullScreenMode fullscreen_mode); 155 156 // Update commands whose state depends on whether multiple profiles are 157 // allowed. 158 void UpdateCommandsForMultipleProfiles(); 159 160 // Updates the printing command state. 161 void UpdatePrintingState(); 162 163 // Updates the SHOW_SYNC_SETUP menu entry. 164 void OnSigninAllowedPrefChange(); 165 166 // Updates the save-page-as command state. 167 void UpdateSaveAsState(); 168 169 // Updates the show-sync command state. 170 void UpdateShowSyncState(bool show_main_ui); 171 172 // Ask the Reload/Stop button to change its icon, and update the Stop command 173 // state. |is_loading| is true if the current WebContents is loading. 174 // |force| is true if the button should change its icon immediately. 175 void UpdateReloadStopState(bool is_loading, bool force); 176 177 // Updates commands for find. 178 void UpdateCommandsForFind(); 179 180 // Add/remove observers for interstitial attachment/detachment from 181 // |contents|. 182 void AddInterstitialObservers(content::WebContents* contents); 183 void RemoveInterstitialObservers(content::WebContents* contents); 184 185 inline BrowserWindow* window(); 186 inline Profile* profile(); 187 188 Browser* browser_; 189 190 ProfileManager* profile_manager_; 191 192 // The CommandUpdater that manages the browser window commands. 193 CommandUpdater command_updater_; 194 195 // Indicates if command execution is blocked. 196 bool block_command_execution_; 197 198 // Stores the last blocked command id when |block_command_execution_| is true. 199 int last_blocked_command_id_; 200 201 // Stores the disposition type of the last blocked command. 202 WindowOpenDisposition last_blocked_command_disposition_; 203 204 std::vector<InterstitialObserver*> interstitial_observers_; 205 206 PrefChangeRegistrar profile_pref_registrar_; 207 PrefChangeRegistrar local_pref_registrar_; 208 BooleanPrefMember pref_signin_allowed_; 209 210 DISALLOW_COPY_AND_ASSIGN(BrowserCommandController); 211 }; 212 213 } // namespace chrome 214 215 #endif // CHROME_BROWSER_UI_BROWSER_COMMAND_CONTROLLER_H_ 216