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/gtest_prod_util.h"
11 #include "base/macros.h"
12 #include "chrome/browser/command_updater.h"
13 #include "chrome/browser/command_updater_delegate.h"
14 #include "chrome/browser/command_updater_impl.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
16 #include "components/prefs/pref_change_registrar.h"
17 #include "components/prefs/pref_member.h"
18 #include "components/sessions/core/tab_restore_service_observer.h"
19 #include "ui/base/window_open_disposition.h"
20 
21 class Browser;
22 class BrowserWindow;
23 class Profile;
24 
25 namespace content {
26 struct NativeWebKeyboardEvent;
27 }
28 
29 namespace chrome {
30 
31 // This class needs to expose the internal command_updater_ in some way, hence
32 // it implements CommandUpdater as the public API for it (so it's not directly
33 // exposed).
34 class BrowserCommandController : public CommandUpdater,
35                                  public TabStripModelObserver,
36                                  public sessions::TabRestoreServiceObserver {
37  public:
38   explicit BrowserCommandController(Browser* browser);
39   ~BrowserCommandController() override;
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   // Notifies the controller that state has changed in one of the following
48   // areas and it should update command states.
49   void TabStateChanged();
50   void ZoomStateChanged();
51   void ContentRestrictionsChanged();
52   void FullscreenStateChanged();
53 #if defined(OS_CHROMEOS)
54   // Called when the browser goes in or out of the special locked fullscreen
55   // mode. In this mode the user is basically locked into the current browser
56   // window and tab hence we disable most keyboard shortcuts and we also
57   // prevent changing the state of enabled shortcuts while in this mode (so the
58   // other *Changed() functions will be a NO-OP in this state).
59   void LockedFullscreenStateChanged();
60 #endif
61   void PrintingStateChanged();
62   void LoadingStateChanged(bool is_loading, bool force);
63   void FindBarVisibilityChanged();
64   void ExtensionStateChanged();
65   void TabKeyboardFocusChangedTo(base::Optional<int> index);
66   void WebContentsFocusChanged();
67 
68   // Overriden from CommandUpdater:
69   bool SupportsCommand(int id) const override;
70   bool IsCommandEnabled(int id) const override;
71   bool ExecuteCommand(
72       int id,
73       base::TimeTicks time_stamp = base::TimeTicks::Now()) override;
74   bool ExecuteCommandWithDisposition(
75       int id,
76       WindowOpenDisposition disposition,
77       base::TimeTicks time_stamp = base::TimeTicks::Now()) override;
78   void AddCommandObserver(int id, CommandObserver* observer) override;
79   void RemoveCommandObserver(int id, CommandObserver* observer) override;
80   void RemoveCommandObserver(CommandObserver* observer) override;
81   bool UpdateCommandEnabled(int id, bool state) override;
82 
83   // Shared state updating: these functions are static and public to share with
84   // outside code.
85 
86   // Updates the open-file state.
87   static void UpdateOpenFileState(CommandUpdater* command_updater);
88 
89   // Update commands whose state depends on incognito mode availability and that
90   // only depend on the profile.
91   static void UpdateSharedCommandsForIncognitoAvailability(
92       CommandUpdater* command_updater,
93       Profile* profile);
94 
95  private:
96   FRIEND_TEST_ALL_PREFIXES(BrowserCommandControllerBrowserTest,
97                            LockedFullscreen);
98 
99   // Overridden from TabStripModelObserver:
100   void OnTabStripModelChanged(
101       TabStripModel* tab_strip_model,
102       const TabStripModelChange& change,
103       const TabStripSelectionChange& selection) override;
104   void TabBlockedStateChanged(content::WebContents* contents,
105                               int index) override;
106 
107   // Overridden from TabRestoreServiceObserver:
108   void TabRestoreServiceChanged(sessions::TabRestoreService* service) override;
109   void TabRestoreServiceDestroyed(
110       sessions::TabRestoreService* service) override;
111   void TabRestoreServiceLoaded(sessions::TabRestoreService* service) override;
112 
113   // Returns true if the regular Chrome UI (not the fullscreen one and
114   // not the single-tab one) is shown. Used for updating window command states
115   // only. Consider using SupportsWindowFeature if you need the mentioned
116   // functionality anywhere else.
117   bool IsShowingMainUI();
118 
119   // Returns true if the location bar is shown or is currently hidden, but can
120   // be shown. Used for updating window command states only.
121   bool IsShowingLocationBar();
122 
123   // Initialize state for all browser commands.
124   void InitCommandState();
125 
126   // Update commands whose state depends on incognito mode availability.
127   void UpdateCommandsForIncognitoAvailability();
128 
129   // Update commands whose state depends on the tab's state.
130   void UpdateCommandsForTabState();
131 
132   // Update Zoom commands based on zoom state.
133   void UpdateCommandsForZoomState();
134 
135   // Updates commands when the content's restrictions change.
136   void UpdateCommandsForContentRestrictionState();
137 
138   // Updates commands for enabling developer tools.
139   void UpdateCommandsForDevTools();
140 
141   // Updates commands for bookmark editing.
142   void UpdateCommandsForBookmarkEditing();
143 
144   // Updates commands that affect the bookmark bar.
145   void UpdateCommandsForBookmarkBar();
146 
147   // Updates commands that affect file selection dialogs in aggregate,
148   // namely the save-page-as state and the open-file state.
149   void UpdateCommandsForFileSelectionDialogs();
150 
151   // Update commands whose state depends on the type of fullscreen mode the
152   // window is in.
153   void UpdateCommandsForFullscreenMode();
154 
155   // Update commands whose state depends on whether they're available to hosted
156   // app windows.
157   void UpdateCommandsForHostedAppAvailability();
158 
159 #if defined(OS_CHROMEOS)
160   // Update commands whose state depends on whether the window is in locked
161   // fullscreen mode or not.
162   void UpdateCommandsForLockedFullscreenMode();
163 #endif
164 
165   // Updates the printing command state.
166   void UpdatePrintingState();
167 
168   // Updates the SHOW_SYNC_SETUP menu entry.
169   void OnSigninAllowedPrefChange();
170 
171   // Updates the save-page-as command state.
172   void UpdateSaveAsState();
173 
174   // Updates the show-sync command state.
175   void UpdateShowSyncState(bool show_main_ui);
176 
177   // Ask the Reload/Stop button to change its icon, and update the Stop command
178   // state.  |is_loading| is true if the current WebContents is loading.
179   // |force| is true if the button should change its icon immediately.
180   void UpdateReloadStopState(bool is_loading, bool force);
181 
182   void UpdateTabRestoreCommandState();
183 
184   // Updates commands for find.
185   void UpdateCommandsForFind();
186 
187   // Updates the command to close find or stop loading.
188   void UpdateCloseFindOrStop();
189 
190   // Updates commands for Media Router.
191   void UpdateCommandsForMediaRouter();
192 
193   // Updates commands for tab keyboard focus state. If |target_index| is
194   // populated, it is the index of the tab with focus; if it is not populated,
195   // no tab has keyboard focus.
196   void UpdateCommandsForTabKeyboardFocus(base::Optional<int> target_index);
197 
198   // Updates commands that depend on whether web contents is focused or not.
199   void UpdateCommandsForWebContentsFocus();
200 
201   inline BrowserWindow* window();
202   inline Profile* profile();
203 
204   Browser* const browser_;
205 
206   // The CommandUpdaterImpl that manages the browser window commands.
207   CommandUpdaterImpl command_updater_;
208 
209   PrefChangeRegistrar profile_pref_registrar_;
210   PrefChangeRegistrar local_pref_registrar_;
211   BooleanPrefMember pref_signin_allowed_;
212 
213   // In locked fullscreen mode disallow enabling/disabling commands.
214   bool is_locked_fullscreen_ = false;
215 
216   DISALLOW_COPY_AND_ASSIGN(BrowserCommandController);
217 };
218 
219 }  // namespace chrome
220 
221 #endif  // CHROME_BROWSER_UI_BROWSER_COMMAND_CONTROLLER_H_
222