• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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"
17 
18 class Browser;
19 class BrowserWindow;
20 class Profile;
21 
22 namespace content {
23 struct NativeWebKeyboardEvent;
24 }
25 
26 namespace chrome {
27 
28 class BrowserCommandController : public CommandUpdaterDelegate,
29                                  public TabStripModelObserver,
30                                  public TabRestoreServiceObserver {
31  public:
32   explicit BrowserCommandController(Browser* browser);
33   virtual ~BrowserCommandController();
34 
command_updater()35   CommandUpdater* command_updater() { return &command_updater_; }
block_command_execution()36   bool block_command_execution() const { return block_command_execution_; }
37 
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);
43 
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);
49 
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);
55 
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 
65   // Shared state updating: these functions are static and public to share with
66   // outside code.
67 
68   // Updates the open-file state.
69   static void UpdateOpenFileState(CommandUpdater* command_updater);
70 
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,
75       Profile* profile);
76 
77  private:
78   class InterstitialObserver;
79 
80   // Overridden from CommandUpdaterDelegate:
81   virtual void ExecuteCommandWithDisposition(
82       int id,
83       WindowOpenDisposition disposition) OVERRIDE;
84 
85   // Overridden from TabStripModelObserver:
86   virtual void TabInsertedAt(content::WebContents* contents,
87                              int index,
88                              bool foreground) OVERRIDE;
89   virtual void TabDetachedAt(content::WebContents* contents,
90                              int index) OVERRIDE;
91   virtual void TabReplacedAt(TabStripModel* tab_strip_model,
92                              content::WebContents* old_contents,
93                              content::WebContents* new_contents,
94                              int index) OVERRIDE;
95   virtual void TabBlockedStateChanged(content::WebContents* contents,
96                                       int index) OVERRIDE;
97 
98   // Overridden from TabRestoreServiceObserver:
99   virtual void TabRestoreServiceChanged(TabRestoreService* service) OVERRIDE;
100   virtual void TabRestoreServiceDestroyed(TabRestoreService* service) OVERRIDE;
101   virtual void TabRestoreServiceLoaded(TabRestoreService* service) OVERRIDE;
102 
103   // Returns true if the regular Chrome UI (not the fullscreen one and
104   // not the single-tab one) is shown. Used for updating window command states
105   // only. Consider using SupportsWindowFeature if you need the mentioned
106   // functionality anywhere else.
107   bool IsShowingMainUI();
108 
109   // Initialize state for all browser commands.
110   void InitCommandState();
111 
112   // Update commands whose state depends on incognito mode availability.
113   void UpdateCommandsForIncognitoAvailability();
114 
115   // Update commands whose state depends on the tab's state.
116   void UpdateCommandsForTabState();
117 
118   // Update Zoom commands based on zoom state.
119   void UpdateCommandsForZoomState();
120 
121   // Updates commands when the content's restrictions change.
122   void UpdateCommandsForContentRestrictionState();
123 
124   // Updates commands for enabling developer tools.
125   void UpdateCommandsForDevTools();
126 
127   // Updates commands for bookmark editing.
128   void UpdateCommandsForBookmarkEditing();
129 
130   // Updates commands that affect the bookmark bar.
131   void UpdateCommandsForBookmarkBar();
132 
133   // Updates commands that affect file selection dialogs in aggregate,
134   // namely the save-page-as state and the open-file state.
135   void UpdateCommandsForFileSelectionDialogs();
136 
137   // Update commands whose state depends on the type of fullscreen mode the
138   // window is in.
139   void UpdateCommandsForFullscreenMode();
140 
141   // Updates the printing command state.
142   void UpdatePrintingState();
143 
144   // Updates the SHOW_SYNC_SETUP menu entry.
145   void OnSigninAllowedPrefChange();
146 
147   // Updates the save-page-as command state.
148   void UpdateSaveAsState();
149 
150   // Updates the show-sync command state.
151   void UpdateShowSyncState(bool show_main_ui);
152 
153   // Ask the Reload/Stop button to change its icon, and update the Stop command
154   // state.  |is_loading| is true if the current WebContents is loading.
155   // |force| is true if the button should change its icon immediately.
156   void UpdateReloadStopState(bool is_loading, bool force);
157 
158   // Updates commands for find.
159   void UpdateCommandsForFind();
160 
161   void UpdateTabRestoreCommandState();
162 
163   // Add/remove observers for interstitial attachment/detachment from
164   // |contents|.
165   void AddInterstitialObservers(content::WebContents* contents);
166   void RemoveInterstitialObservers(content::WebContents* contents);
167 
168   inline BrowserWindow* window();
169   inline Profile* profile();
170 
171   Browser* browser_;
172 
173   // The CommandUpdater that manages the browser window commands.
174   CommandUpdater command_updater_;
175 
176   // Indicates if command execution is blocked.
177   bool block_command_execution_;
178 
179   // Stores the last blocked command id when |block_command_execution_| is true.
180   int last_blocked_command_id_;
181 
182   // Stores the disposition type of the last blocked command.
183   WindowOpenDisposition last_blocked_command_disposition_;
184 
185   std::vector<InterstitialObserver*> interstitial_observers_;
186 
187   PrefChangeRegistrar profile_pref_registrar_;
188   PrefChangeRegistrar local_pref_registrar_;
189   BooleanPrefMember pref_signin_allowed_;
190 
191   DISALLOW_COPY_AND_ASSIGN(BrowserCommandController);
192 };
193 
194 }  // namespace chrome
195 
196 #endif  // CHROME_BROWSER_UI_BROWSER_COMMAND_CONTROLLER_H_
197