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 | |
9 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/memory/singleton.h" | |
10 #include "chrome/browser/background_application_list_model.h" | 13 #include "chrome/browser/background_application_list_model.h" |
11 #include "chrome/browser/prefs/pref_change_registrar.h" | 14 #include "chrome/browser/prefs/pref_change_registrar.h" |
12 #include "chrome/browser/profiles/profile_keyed_service.h" | 15 #include "chrome/browser/profiles/profile_keyed_service.h" |
16 #include "chrome/browser/profiles/profile_status_menu_model.h" | |
13 #include "chrome/browser/status_icons/status_icon.h" | 17 #include "chrome/browser/status_icons/status_icon.h" |
14 #include "content/common/notification_observer.h" | 18 #include "content/common/notification_observer.h" |
15 #include "content/common/notification_registrar.h" | 19 #include "content/common/notification_registrar.h" |
16 #include "ui/base/models/simple_menu_model.h" | |
17 | 20 |
18 class Browser; | 21 class Browser; |
19 class CommandLine; | 22 class CommandLine; |
20 class Extension; | 23 class Extension; |
21 class PrefService; | 24 class PrefService; |
22 class Profile; | 25 class Profile; |
23 class StatusIcon; | 26 class StatusIcon; |
24 class StatusTray; | 27 class StatusTray; |
25 | 28 |
29 | |
26 // BackgroundModeManager is responsible for switching Chrome into and out of | 30 // BackgroundModeManager is responsible for switching Chrome into and out of |
27 // "background mode" and for providing UI for the user to exit Chrome when there | 31 // "background mode" and for providing UI for the user to exit Chrome when there |
28 // are no open browser windows. | 32 // are no open browser windows. |
29 // | 33 // |
30 // Chrome enters background mode whenever there is an application with the | 34 // Chrome enters background mode whenever there is an application with the |
31 // "background" permission installed. This class monitors the set of | 35 // "background" permission installed. This class monitors the set of |
32 // installed/loaded extensions to ensure that Chrome enters/exits background | 36 // installed/loaded extensions to ensure that Chrome enters/exits background |
33 // mode at the appropriate time. | 37 // mode at the appropriate time. |
34 // | 38 // |
35 // When Chrome is in background mode, it will continue running even after the | 39 // When Chrome is in background mode, it will continue running even after the |
36 // last browser window is closed, until the user explicitly exits the app. | 40 // last browser window is closed, until the user explicitly exits the app. |
37 // Additionally, when in background mode, Chrome will launch on OS login with | 41 // Additionally, when in background mode, Chrome will launch on OS login with |
38 // no open windows to allow apps with the "background" permission to run in the | 42 // no open windows to allow apps with the "background" permission to run in the |
39 // background. | 43 // background. |
40 class BackgroundModeManager | 44 class BackgroundModeManager |
41 : public NotificationObserver, | 45 : public NotificationObserver, |
42 public ui::SimpleMenuModel::Delegate, | 46 public ProfileStatusMenuModel::Delegate, |
43 public BackgroundApplicationListModel::Observer, | 47 public BackgroundApplicationListModel::Observer, |
44 public ProfileKeyedService { | 48 public ProfileKeyedService { |
45 public: | 49 public: |
46 BackgroundModeManager(Profile* profile, CommandLine* command_line); | 50 BackgroundModeManager(); |
47 virtual ~BackgroundModeManager(); | 51 virtual ~BackgroundModeManager(); |
48 | 52 |
53 static BackgroundModeManager* GetInstance(); | |
54 | |
49 static void RegisterPrefs(PrefService* prefs); | 55 static void RegisterPrefs(PrefService* prefs); |
50 | 56 |
57 virtual void RegisterProfile(Profile* profile); | |
58 | |
51 private: | 59 private: |
60 friend struct DefaultSingletonTraits<BackgroundModeManager>; | |
52 friend class TestBackgroundModeManager; | 61 friend class TestBackgroundModeManager; |
53 friend class BackgroundModeManagerTest; | 62 friend class BackgroundModeManagerTest; |
54 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 63 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
55 BackgroundAppLoadUnload); | 64 BackgroundAppLoadUnload); |
56 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 65 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
57 BackgroundAppInstallUninstall); | 66 BackgroundAppInstallUninstall); |
58 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 67 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
59 BackgroundAppInstallUninstallWhileDisabled); | 68 BackgroundAppInstallUninstallWhileDisabled); |
60 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 69 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
61 EnableAfterBackgroundAppInstall); | 70 EnableAfterBackgroundAppInstall); |
62 | 71 |
72 struct BackgroundModeData { | |
73 BackgroundModeData(); | |
74 ~BackgroundModeData(); | |
75 | |
76 // The cached list of BackgroundApplications. | |
77 BackgroundApplicationListModel* applications_; | |
Andrew T Wilson (Slow)
2011/05/20 00:22:09
nit: struct member variables should not have trail
Andrew T Wilson (Slow)
2011/05/20 00:22:09
Should this be a scoped_ptr<> so you don't have to
rpetterson
2011/05/20 05:53:17
Not an issue any more.
rpetterson
2011/05/20 05:53:17
Done.
| |
78 | |
79 // Reference to our status icon (if any) - owned by the StatusTray. | |
80 StatusIcon* status_icon_; | |
81 | |
82 // Reference to our status icon's context menu (if any) - owned by the | |
83 // status_icon_ | |
84 ui::SimpleMenuModel* context_menu_; | |
85 | |
86 // Set to the position of the first application entry in the status icon's | |
87 // context menu. | |
88 int context_menu_application_offset_; | |
89 }; | |
90 | |
63 // NotificationObserver implementation. | 91 // NotificationObserver implementation. |
64 virtual void Observe(NotificationType type, | 92 virtual void Observe(NotificationType type, |
65 const NotificationSource& source, | 93 const NotificationSource& source, |
66 const NotificationDetails& details); | 94 const NotificationDetails& details) OVERRIDE; |
67 | 95 |
68 // SimpleMenuModel::Delegate implementation. | 96 // Overrides from SimpleMenuModel::Delegate implementation. |
69 virtual bool IsCommandIdChecked(int command_id) const; | 97 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; |
70 virtual bool IsCommandIdEnabled(int command_id) const; | 98 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; |
71 virtual bool GetAcceleratorForCommandId(int command_id, | 99 virtual bool GetAcceleratorForCommandId(int command_id, |
72 ui::Accelerator* accelerator); | 100 ui::Accelerator* accelerator) OVERRIDE ; |
73 virtual void ExecuteCommand(int command_id); | 101 virtual void ExecuteCommand(int command_id) OVERRIDE; |
102 | |
103 // Overrides from ProfileSatusMenuModel::Delegate implementation. | |
Andrew T Wilson (Slow)
2011/05/20 00:22:09
nit: ProfileStatusMenuModel (not ProfileSatus)
rpetterson
2011/05/20 05:53:17
Done.
| |
104 virtual void ExecuteCommand(int command_id, Profile* profile) OVERRIDE; | |
74 | 105 |
75 // Open an application in a new tab, opening a new window if needed. | 106 // Open an application in a new tab, opening a new window if needed. |
76 virtual void ExecuteApplication(int application_id); | 107 virtual void ExecuteApplication(int application_id, Profile* profile); |
77 | 108 |
78 // BackgroundApplicationListModel::Observer implementation. | 109 // BackgroundApplicationListModel::Observer implementation. |
79 virtual void OnApplicationDataChanged(const Extension* extension); | 110 virtual void OnApplicationDataChanged(const Extension* extension, |
80 virtual void OnApplicationListChanged(); | 111 Profile* profile) OVERRIDE; |
112 virtual void OnApplicationListChanged(Profile* profile) OVERRIDE; | |
81 | 113 |
82 // Called when an extension is loaded to manage count of background apps. | 114 // Called when an extension is loaded to manage count of background apps. |
83 void OnBackgroundAppLoaded(); | 115 void OnBackgroundAppLoaded(); |
84 | 116 |
85 // Called when an extension is unloaded to manage count of background apps. | 117 // Called when an extension is unloaded to manage count of background apps. |
86 void OnBackgroundAppUnloaded(); | 118 void OnBackgroundAppUnloaded(); |
87 | 119 |
88 // Invoked when an extension is installed so we can ensure that | 120 // Invoked when an extension is installed so we can ensure that |
89 // launch-on-startup is enabled if appropriate. |extension| can be NULL when | 121 // launch-on-startup is enabled if appropriate. |extension| can be NULL when |
90 // called from unit tests. | 122 // called from unit tests. |
91 void OnBackgroundAppInstalled(const Extension* extension); | 123 void OnBackgroundAppInstalled(const Extension* extension, Profile* profile); |
92 | 124 |
93 // Invoked when an extension is uninstalled so we can ensure that | 125 // Invoked when an extension is uninstalled so we can ensure that |
94 // launch-on-startup is disabled if appropriate. | 126 // launch-on-startup is disabled if appropriate. |
95 void OnBackgroundAppUninstalled(); | 127 void OnBackgroundAppUninstalled(); |
96 | 128 |
97 // Called to make sure that our launch-on-startup mode is properly set. | 129 // Called to make sure that our launch-on-startup mode is properly set. |
98 // (virtual so we can override for tests). | 130 // (virtual so we can override for tests). |
99 virtual void EnableLaunchOnStartup(bool should_launch); | 131 virtual void EnableLaunchOnStartup(bool should_launch); |
100 | 132 |
101 // Invoked when a background app is installed so we can display a | 133 // Invoked when a background app is installed so we can display a |
102 // platform-specific notification to the user. | 134 // platform-specific notification to the user. |
103 void DisplayAppInstalledNotification(const Extension* extension); | 135 void DisplayAppInstalledNotification(const Extension* extension, |
136 Profile* profile); | |
104 | 137 |
105 // Invoked to put Chrome in KeepAlive mode - chrome runs in the background | 138 // Invoked to put Chrome in KeepAlive mode - chrome runs in the background |
106 // and has a status bar icon. | 139 // and has a status bar icon. |
107 void StartBackgroundMode(); | 140 void StartBackgroundMode(); |
108 | 141 |
109 // Invoked to take Chrome out of KeepAlive mode - chrome stops running in | 142 // Invoked to take Chrome out of KeepAlive mode - chrome stops running in |
110 // the background and removes its status bar icon. | 143 // the background and removes its status bar icon. |
111 void EndBackgroundMode(); | 144 void EndBackgroundMode(); |
112 | 145 |
113 // If --no-startup-window is passed, BackgroundModeManager will manually keep | 146 // If --no-startup-window is passed, BackgroundModeManager will manually keep |
114 // chrome running while waiting for apps to load. This is called when we no | 147 // chrome running while waiting for apps to load. This is called when we no |
115 // longer need to do this (either because the user has chosen to exit chrome | 148 // longer need to do this (either because the user has chosen to exit chrome |
116 // manually, or all apps have been loaded). | 149 // manually, or all apps have been loaded). |
117 void EndKeepAliveForStartup(); | 150 void EndKeepAliveForStartup(); |
118 | 151 |
119 // Return an appropriate name for a Preferences menu entry. Preferences is | 152 // Return an appropriate name for a Preferences menu entry. Preferences is |
120 // sometimes called Options or Settings. | 153 // sometimes called Options or Settings. |
121 string16 GetPreferencesMenuLabel(); | 154 string16 GetPreferencesMenuLabel(); |
122 | 155 |
123 // Create a status tray icon to allow the user to shutdown Chrome when running | 156 // Create a status tray icon to allow the user to shutdown Chrome when running |
124 // in background mode. Virtual to enable testing. | 157 // in background mode. Virtual to enable testing. |
125 virtual void CreateStatusTrayIcon(); | 158 virtual void CreateStatusTrayIcon(Profile* profile); |
126 | 159 |
127 // Removes the status tray icon because we are exiting background mode. | 160 // Removes the status tray icon because we are exiting background mode. |
128 // Virtual to enable testing. | 161 // Virtual to enable testing. |
129 virtual void RemoveStatusTrayIcon(); | 162 virtual void RemoveStatusTrayIcon(Profile* profile); |
130 | 163 |
131 // Updates the status icon's context menu entry corresponding to |extension| | 164 // Updates the status icon's context menu entry corresponding to |extension| |
132 // to use the icon associated with |extension| in the | 165 // to use the icon associated with |extension| in the |
133 // BackgroundApplicationListModel. | 166 // BackgroundApplicationListModel. |
134 void UpdateContextMenuEntryIcon(const Extension* extension); | 167 void UpdateContextMenuEntryIcon(const Extension* extension, Profile* profile); |
135 | 168 |
136 // Create a context menu, or replace/update an existing context menu, for the | 169 // Create a context menu, or replace/update an existing context menu, for the |
137 // status tray icon which, among other things, allows the user to shutdown | 170 // status tray icon which, among other things, allows the user to shutdown |
138 // Chrome when running in background mode. | 171 // Chrome when running in background mode. |
139 virtual void UpdateStatusTrayIconContextMenu(); | 172 virtual void UpdateStatusTrayIconContextMenu(Profile* profile); |
140 | 173 |
141 // Returns a browser window, or creates one if none are open. Used by | 174 // Returns a browser window, or creates one if none are open. Used by |
142 // operations (like displaying the preferences dialog) that require a Browser | 175 // operations (like displaying the preferences dialog) that require a Browser |
143 // window. | 176 // window. |
144 Browser* GetBrowserWindow(); | 177 Browser* GetBrowserWindow(Profile* profile); |
145 | 178 |
146 // Returns true if the "Let chrome run in the background" pref is checked. | 179 // Returns true if the "Let chrome run in the background" pref is checked. |
147 // (virtual to allow overriding in tests). | 180 // (virtual to allow overriding in tests). |
148 virtual bool IsBackgroundModePrefEnabled(); | 181 virtual bool IsBackgroundModePrefEnabled(); |
149 | 182 |
150 // Turns off background mode if it's currently enabled. | 183 // Turns off background mode if it's currently enabled. |
151 void DisableBackgroundMode(); | 184 void DisableBackgroundMode(); |
152 | 185 |
153 // Turns on background mode if it's currently disabled. | 186 // Turns on background mode if it's currently disabled. |
154 void EnableBackgroundMode(); | 187 void EnableBackgroundMode(); |
155 | 188 |
156 // Returns true if background mode is permanently disabled for this chrome | 189 // Returns true if background mode is permanently disabled for this chrome |
157 // session. | 190 // session. |
158 static bool IsBackgroundModePermanentlyDisabled( | 191 static bool IsBackgroundModePermanentlyDisabled( |
159 const CommandLine* command_line); | 192 const CommandLine* command_line); |
160 | 193 |
161 // Registrars for managing our change observers. | 194 // Registrars for managing our change observers. |
162 NotificationRegistrar registrar_; | 195 NotificationRegistrar registrar_; |
163 PrefChangeRegistrar pref_registrar_; | 196 PrefChangeRegistrar pref_registrar_; |
164 | 197 |
165 // The parent profile for this object. | 198 // The profile-keyed data for this background mode manager. Keyed on profile. |
166 Profile* profile_; | 199 std::map<Profile*, BackgroundModeData> background_mode_data_; |
167 | 200 |
168 // The cached list of BackgroundApplications. | 201 // Reference to our status tray (owned by our parent profile). If null, the |
Andrew T Wilson (Slow)
2011/05/20 00:22:09
Since we don't have a parent profile any more, thi
rpetterson
2011/05/20 05:53:17
Done.
| |
169 BackgroundApplicationListModel applications_; | 202 // platform doesn't support status icons. |
203 StatusTray* status_tray_; | |
170 | 204 |
171 // The number of background apps currently loaded. | 205 // The number of background apps currently loaded. This is the total over |
206 // all profiles. | |
172 int background_app_count_; | 207 int background_app_count_; |
173 | 208 |
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 | |
182 // Set to true when we are running in background mode. Allows us to track our | 209 // Set to true when we are running in background mode. Allows us to track our |
183 // current background state so we can take the appropriate action when the | 210 // current background state so we can take the appropriate action when the |
184 // user disables/enables background mode via preferences. | 211 // user disables/enables background mode via preferences. |
185 bool in_background_mode_; | 212 bool in_background_mode_; |
186 | 213 |
187 // Set when we are keeping chrome running during the startup process - this | 214 // Set when we are keeping chrome running during the startup process - this |
188 // is required when running with the --no-startup-window flag, as otherwise | 215 // is required when running with the --no-startup-window flag, as otherwise |
189 // chrome would immediately exit due to having no open windows. | 216 // chrome would immediately exit due to having no open windows. |
190 bool keep_alive_for_startup_; | 217 bool keep_alive_for_startup_; |
191 | 218 |
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 | |
199 DISALLOW_COPY_AND_ASSIGN(BackgroundModeManager); | 219 DISALLOW_COPY_AND_ASSIGN(BackgroundModeManager); |
200 }; | 220 }; |
201 | 221 |
202 #endif // CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_ | 222 #endif // CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_ |
OLD | NEW |