OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_ |
6 #define CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_ | 6 #define CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | |
10 | |
11 #include "base/gtest_prod_util.h" | 9 #include "base/gtest_prod_util.h" |
12 #include "chrome/browser/background_application_list_model.h" | 10 #include "chrome/browser/background_application_list_model.h" |
13 #include "chrome/browser/prefs/pref_change_registrar.h" | 11 #include "chrome/browser/prefs/pref_change_registrar.h" |
14 #include "chrome/browser/profiles/profile_keyed_service.h" | 12 #include "chrome/browser/profiles/profile_keyed_service.h" |
15 #include "chrome/browser/status_icons/status_icon.h" | 13 #include "chrome/browser/status_icons/status_icon.h" |
16 #include "content/common/notification_observer.h" | 14 #include "content/common/notification_observer.h" |
17 #include "content/common/notification_registrar.h" | 15 #include "content/common/notification_registrar.h" |
18 #include "ui/base/models/simple_menu_model.h" | 16 #include "ui/base/models/simple_menu_model.h" |
19 | 17 |
20 class Browser; | 18 class Browser; |
(...skipping 13 matching lines...) Expand all Loading... |
34 // installed/loaded extensions to ensure that Chrome enters/exits background | 32 // installed/loaded extensions to ensure that Chrome enters/exits background |
35 // mode at the appropriate time. | 33 // mode at the appropriate time. |
36 // | 34 // |
37 // When Chrome is in background mode, it will continue running even after the | 35 // When Chrome is in background mode, it will continue running even after the |
38 // last browser window is closed, until the user explicitly exits the app. | 36 // last browser window is closed, until the user explicitly exits the app. |
39 // Additionally, when in background mode, Chrome will launch on OS login with | 37 // Additionally, when in background mode, Chrome will launch on OS login with |
40 // no open windows to allow apps with the "background" permission to run in the | 38 // no open windows to allow apps with the "background" permission to run in the |
41 // background. | 39 // background. |
42 class BackgroundModeManager | 40 class BackgroundModeManager |
43 : public NotificationObserver, | 41 : public NotificationObserver, |
| 42 public ui::SimpleMenuModel::Delegate, |
44 public BackgroundApplicationListModel::Observer, | 43 public BackgroundApplicationListModel::Observer, |
45 public ProfileKeyedService { | 44 public ProfileKeyedService { |
46 public: | 45 public: |
47 explicit BackgroundModeManager(CommandLine* command_line); | 46 BackgroundModeManager(Profile* profile, CommandLine* command_line); |
48 virtual ~BackgroundModeManager(); | 47 virtual ~BackgroundModeManager(); |
49 | 48 |
50 static void RegisterPrefs(PrefService* prefs); | 49 static void RegisterPrefs(PrefService* prefs); |
51 | 50 |
52 virtual void RegisterProfile(Profile* profile); | |
53 | |
54 private: | 51 private: |
55 friend class TestBackgroundModeManager; | 52 friend class TestBackgroundModeManager; |
56 friend class BackgroundModeManagerTest; | 53 friend class BackgroundModeManagerTest; |
57 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 54 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
58 BackgroundAppLoadUnload); | 55 BackgroundAppLoadUnload); |
59 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 56 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
60 BackgroundAppInstallUninstall); | 57 BackgroundAppInstallUninstall); |
61 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 58 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
62 BackgroundAppInstallUninstallWhileDisabled); | 59 BackgroundAppInstallUninstallWhileDisabled); |
63 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 60 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
64 EnableAfterBackgroundAppInstall); | 61 EnableAfterBackgroundAppInstall); |
65 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | |
66 MultiProfile); | |
67 | |
68 class BackgroundModeData : public ui::SimpleMenuModel::Delegate { | |
69 public: | |
70 explicit BackgroundModeData( | |
71 Profile* profile, | |
72 BackgroundModeManager* background_mode_manager); | |
73 virtual ~BackgroundModeData(); | |
74 | |
75 // The cached list of BackgroundApplications. | |
76 scoped_ptr<BackgroundApplicationListModel> applications_; | |
77 | |
78 // Reference to our status icon (if any) - owned by the StatusTray. | |
79 StatusIcon* status_icon_; | |
80 | |
81 // Reference to our status icon's context menu (if any) - owned by the | |
82 // status_icon_ | |
83 ui::SimpleMenuModel* context_menu_; | |
84 | |
85 // Set to the position of the first application entry in the status icon's | |
86 // context menu. | |
87 int context_menu_application_offset_; | |
88 | |
89 // The profile associated with this background app data. | |
90 Profile* profile_; | |
91 | |
92 // The background mode manager which owns this BackgroundModeData | |
93 BackgroundModeManager* background_mode_manager_; | |
94 | |
95 // Overrides from SimpleMenuModel::Delegate implementation. | |
96 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; | |
97 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; | |
98 virtual bool GetAcceleratorForCommandId(int command_id, | |
99 ui::Accelerator* accelerator) | |
100 OVERRIDE; | |
101 virtual void ExecuteCommand(int command_id) OVERRIDE; | |
102 | |
103 // Returns a browser window, or creates one if none are open. Used by | |
104 // operations (like displaying the preferences dialog) that require a | |
105 // Browser window. | |
106 Browser* GetBrowserWindow(); | |
107 | |
108 // Open an application in a new tab, opening a new window if needed. | |
109 virtual void ExecuteApplication(int application_id); | |
110 | |
111 // Updates the status icon's context menu entry corresponding to | |
112 // |extension| to use the icon associated with |extension| in the | |
113 // BackgroundApplicationListModel. | |
114 // TODO(rlp): Remove after creating one status icon. | |
115 void UpdateContextMenuEntryIcon(const Extension* extension); | |
116 | |
117 // Returns whether any of the extensions are background apps. | |
118 bool HasBackgroundApp(); | |
119 }; | |
120 | |
121 // Ideally we would want our BackgroundModeData to be scoped_ptrs, | |
122 // but since maps copy their entries, we can't used scoped_ptrs. | |
123 // Similarly, we can't just have a map of BackgroundModeData objects, | |
124 // since BackgroundModeData contains a scoped_ptr which once again | |
125 // can't be copied. So rather than using BackgroundModeData* which | |
126 // we'd have to remember to delete, we use the ref-counted linked_ptr | |
127 // which is similar to a shared_ptr. | |
128 typedef linked_ptr<BackgroundModeData> BackgroundModeInfo; | |
129 | 62 |
130 // NotificationObserver implementation. | 63 // NotificationObserver implementation. |
131 virtual void Observe(NotificationType type, | 64 virtual void Observe(NotificationType type, |
132 const NotificationSource& source, | 65 const NotificationSource& source, |
133 const NotificationDetails& details) OVERRIDE; | 66 const NotificationDetails& details); |
| 67 |
| 68 // SimpleMenuModel::Delegate implementation. |
| 69 virtual bool IsCommandIdChecked(int command_id) const; |
| 70 virtual bool IsCommandIdEnabled(int command_id) const; |
| 71 virtual bool GetAcceleratorForCommandId(int command_id, |
| 72 ui::Accelerator* accelerator); |
| 73 virtual void ExecuteCommand(int command_id); |
| 74 |
| 75 // Open an application in a new tab, opening a new window if needed. |
| 76 virtual void ExecuteApplication(int application_id); |
134 | 77 |
135 // BackgroundApplicationListModel::Observer implementation. | 78 // BackgroundApplicationListModel::Observer implementation. |
136 virtual void OnApplicationDataChanged(const Extension* extension, | 79 virtual void OnApplicationDataChanged(const Extension* extension); |
137 Profile* profile) OVERRIDE; | 80 virtual void OnApplicationListChanged(); |
138 virtual void OnApplicationListChanged(Profile* profile) OVERRIDE; | |
139 | 81 |
140 // Called when an extension is loaded to manage count of background apps. | 82 // Called when an extension is loaded to manage count of background apps. |
141 void OnBackgroundAppLoaded(); | 83 void OnBackgroundAppLoaded(); |
142 | 84 |
143 // Called when an extension is unloaded to manage count of background apps. | 85 // Called when an extension is unloaded to manage count of background apps. |
144 void OnBackgroundAppUnloaded(); | 86 void OnBackgroundAppUnloaded(); |
145 | 87 |
146 // Invoked when an extension is installed so we can ensure that | 88 // Invoked when an extension is installed so we can ensure that |
147 // launch-on-startup is enabled if appropriate. |extension| can be NULL when | 89 // launch-on-startup is enabled if appropriate. |extension| can be NULL when |
148 // called from unit tests. | 90 // called from unit tests. |
149 void OnBackgroundAppInstalled(const Extension* extension, Profile* profile); | 91 void OnBackgroundAppInstalled(const Extension* extension); |
150 | 92 |
151 // Invoked when an extension is uninstalled so we can ensure that | 93 // Invoked when an extension is uninstalled so we can ensure that |
152 // launch-on-startup is disabled if appropriate. | 94 // launch-on-startup is disabled if appropriate. |
153 void OnBackgroundAppUninstalled(Profile* profile); | 95 void OnBackgroundAppUninstalled(); |
154 | 96 |
155 // Called to make sure that our launch-on-startup mode is properly set. | 97 // Called to make sure that our launch-on-startup mode is properly set. |
156 // (virtual so we can override for tests). | 98 // (virtual so we can override for tests). |
157 virtual void EnableLaunchOnStartup(bool should_launch); | 99 virtual void EnableLaunchOnStartup(bool should_launch); |
158 | 100 |
159 // Invoked when a background app is installed so we can display a | 101 // Invoked when a background app is installed so we can display a |
160 // platform-specific notification to the user. | 102 // platform-specific notification to the user. |
161 void DisplayAppInstalledNotification(const Extension* extension, | 103 void DisplayAppInstalledNotification(const Extension* extension); |
162 Profile* profile); | |
163 | 104 |
164 // Invoked to put Chrome in KeepAlive mode - chrome runs in the background | 105 // Invoked to put Chrome in KeepAlive mode - chrome runs in the background |
165 // and has a status bar icon. | 106 // and has a status bar icon. |
166 void StartBackgroundMode(); | 107 void StartBackgroundMode(); |
167 | 108 |
168 // Invoked to create status icons for any profiles currently running | |
169 // background apps so that there is a way to exit Chrome. | |
170 void InitStatusTrayIcons(); | |
171 | |
172 // Invoked to take Chrome out of KeepAlive mode - chrome stops running in | 109 // Invoked to take Chrome out of KeepAlive mode - chrome stops running in |
173 // the background and removes its status bar icon. | 110 // the background and removes its status bar icon. |
174 void EndBackgroundMode(); | 111 void EndBackgroundMode(); |
175 | 112 |
176 // If --no-startup-window is passed, BackgroundModeManager will manually keep | 113 // If --no-startup-window is passed, BackgroundModeManager will manually keep |
177 // chrome running while waiting for apps to load. This is called when we no | 114 // chrome running while waiting for apps to load. This is called when we no |
178 // longer need to do this (either because the user has chosen to exit chrome | 115 // longer need to do this (either because the user has chosen to exit chrome |
179 // manually, or all apps have been loaded). | 116 // manually, or all apps have been loaded). |
180 void EndKeepAliveForStartup(); | 117 void EndKeepAliveForStartup(); |
181 | 118 |
182 // Return an appropriate name for a Preferences menu entry. Preferences is | 119 // Return an appropriate name for a Preferences menu entry. Preferences is |
183 // sometimes called Options or Settings. | 120 // sometimes called Options or Settings. |
184 string16 GetPreferencesMenuLabel(); | 121 string16 GetPreferencesMenuLabel(); |
185 | 122 |
186 // Create a status tray icon to allow the user to shutdown Chrome when running | 123 // Create a status tray icon to allow the user to shutdown Chrome when running |
187 // in background mode. Virtual to enable testing. | 124 // in background mode. Virtual to enable testing. |
188 virtual void CreateStatusTrayIcon(Profile* profile); | 125 virtual void CreateStatusTrayIcon(); |
189 | 126 |
190 // Removes the status tray icon because we are exiting background mode. | 127 // Removes the status tray icon because we are exiting background mode. |
191 // Virtual to enable testing. | 128 // Virtual to enable testing. |
192 virtual void RemoveStatusTrayIcon(Profile* profile); | 129 virtual void RemoveStatusTrayIcon(); |
193 | 130 |
194 // Updates the status icon's context menu entry corresponding to |extension| | 131 // Updates the status icon's context menu entry corresponding to |extension| |
195 // to use the icon associated with |extension| in the | 132 // to use the icon associated with |extension| in the |
196 // BackgroundApplicationListModel. | 133 // BackgroundApplicationListModel. |
197 void UpdateContextMenuEntryIcon(const Extension* extension, Profile* profile); | 134 void UpdateContextMenuEntryIcon(const Extension* extension); |
198 | 135 |
199 // Create a context menu, or replace/update an existing context menu, for the | 136 // Create a context menu, or replace/update an existing context menu, for the |
200 // status tray icon which, among other things, allows the user to shutdown | 137 // status tray icon which, among other things, allows the user to shutdown |
201 // Chrome when running in background mode. | 138 // Chrome when running in background mode. |
202 virtual void UpdateStatusTrayIconContextMenu(Profile* profile); | 139 virtual void UpdateStatusTrayIconContextMenu(); |
203 | 140 |
204 // Returns the BackgroundModeInfo associated with this profile. If it does | 141 // Returns a browser window, or creates one if none are open. Used by |
205 // not exist, returns an empty BackgroundModeInfo. | 142 // operations (like displaying the preferences dialog) that require a Browser |
206 BackgroundModeManager::BackgroundModeInfo GetBackgroundModeInfo( | 143 // window. |
207 Profile* profile); | 144 Browser* GetBrowserWindow(); |
208 | 145 |
209 // Returns true if the "Let chrome run in the background" pref is checked. | 146 // Returns true if the "Let chrome run in the background" pref is checked. |
210 // (virtual to allow overriding in tests). | 147 // (virtual to allow overriding in tests). |
211 virtual bool IsBackgroundModePrefEnabled(); | 148 virtual bool IsBackgroundModePrefEnabled(); |
212 | 149 |
213 // Turns off background mode if it's currently enabled. | 150 // Turns off background mode if it's currently enabled. |
214 void DisableBackgroundMode(); | 151 void DisableBackgroundMode(); |
215 | 152 |
216 // Turns on background mode if it's currently disabled. | 153 // Turns on background mode if it's currently disabled. |
217 void EnableBackgroundMode(); | 154 void EnableBackgroundMode(); |
218 | 155 |
219 // Returns true if background mode is permanently disabled for this chrome | 156 // Returns true if background mode is permanently disabled for this chrome |
220 // session. | 157 // session. |
221 static bool IsBackgroundModePermanentlyDisabled( | 158 static bool IsBackgroundModePermanentlyDisabled( |
222 const CommandLine* command_line); | 159 const CommandLine* command_line); |
223 | 160 |
224 // Registrars for managing our change observers. | 161 // Registrars for managing our change observers. |
225 NotificationRegistrar registrar_; | 162 NotificationRegistrar registrar_; |
226 PrefChangeRegistrar pref_registrar_; | 163 PrefChangeRegistrar pref_registrar_; |
227 | 164 |
228 // The profile-keyed data for this background mode manager. Keyed on profile. | 165 // The parent profile for this object. |
229 std::map<Profile*, BackgroundModeInfo> background_mode_data_; | 166 Profile* profile_; |
230 | 167 |
231 // Reference to our status tray. If null, the platform doesn't support status | 168 // The cached list of BackgroundApplications. |
232 // icons. | 169 BackgroundApplicationListModel applications_; |
233 StatusTray* status_tray_; | |
234 | 170 |
235 // The number of background apps currently loaded. This is the total over | 171 // The number of background apps currently loaded. |
236 // all profiles. | |
237 int background_app_count_; | 172 int background_app_count_; |
238 | 173 |
| 174 // Reference to our status icon's context menu (if any) - owned by the |
| 175 // status_icon_ |
| 176 ui::SimpleMenuModel* context_menu_; |
| 177 |
| 178 // Set to the position of the first application entry in the status icon's |
| 179 // context menu. |
| 180 int context_menu_application_offset_; |
| 181 |
239 // Set to true when we are running in background mode. Allows us to track our | 182 // Set to true when we are running in background mode. Allows us to track our |
240 // current background state so we can take the appropriate action when the | 183 // current background state so we can take the appropriate action when the |
241 // user disables/enables background mode via preferences. | 184 // user disables/enables background mode via preferences. |
242 bool in_background_mode_; | 185 bool in_background_mode_; |
243 | 186 |
244 // Set when we are keeping chrome running during the startup process - this | 187 // Set when we are keeping chrome running during the startup process - this |
245 // is required when running with the --no-startup-window flag, as otherwise | 188 // is required when running with the --no-startup-window flag, as otherwise |
246 // chrome would immediately exit due to having no open windows. | 189 // chrome would immediately exit due to having no open windows. |
247 bool keep_alive_for_startup_; | 190 bool keep_alive_for_startup_; |
248 | 191 |
| 192 // Reference to our status tray (owned by our parent profile). If null, the |
| 193 // platform doesn't support status icons. |
| 194 StatusTray* status_tray_; |
| 195 |
| 196 // Reference to our status icon (if any) - owned by the StatusTray. |
| 197 StatusIcon* status_icon_; |
| 198 |
249 DISALLOW_COPY_AND_ASSIGN(BackgroundModeManager); | 199 DISALLOW_COPY_AND_ASSIGN(BackgroundModeManager); |
250 }; | 200 }; |
251 | 201 |
252 #endif // CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_ | 202 #endif // CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_ |
OLD | NEW |