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