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_BACKGROUND_MODE_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_BACKGROUND_BACKGROUND_MODE_MANAGER_H_ |
6 #define CHROME_BROWSER_BACKGROUND_BACKGROUND_MODE_MANAGER_H_ | 6 #define CHROME_BROWSER_BACKGROUND_BACKGROUND_MODE_MANAGER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "chrome/browser/background/background_application_list_model.h" | 12 #include "chrome/browser/background/background_application_list_model.h" |
13 #include "chrome/browser/prefs/pref_change_registrar.h" | 13 #include "chrome/browser/prefs/pref_change_registrar.h" |
14 #include "chrome/browser/profiles/profile_info_cache_observer.h" | |
14 #include "chrome/browser/profiles/profile_keyed_service.h" | 15 #include "chrome/browser/profiles/profile_keyed_service.h" |
15 #include "chrome/browser/status_icons/status_icon.h" | 16 #include "chrome/browser/status_icons/status_icon.h" |
16 #include "content/public/browser/notification_observer.h" | 17 #include "content/public/browser/notification_observer.h" |
17 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
18 #include "ui/base/models/simple_menu_model.h" | 19 #include "ui/base/models/simple_menu_model.h" |
19 | 20 |
20 class Browser; | 21 class Browser; |
21 class CommandLine; | 22 class CommandLine; |
22 class Extension; | 23 class Extension; |
23 class PrefService; | 24 class PrefService; |
(...skipping 12 matching lines...) Expand all Loading... | |
36 // mode at the appropriate time. | 37 // mode at the appropriate time. |
37 // | 38 // |
38 // 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 |
39 // 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. |
40 // 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 |
41 // 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 |
42 // background. | 43 // background. |
43 class BackgroundModeManager | 44 class BackgroundModeManager |
44 : public content::NotificationObserver, | 45 : public content::NotificationObserver, |
45 public BackgroundApplicationListModel::Observer, | 46 public BackgroundApplicationListModel::Observer, |
47 public ProfileInfoCacheObserver, | |
46 public ProfileKeyedService, | 48 public ProfileKeyedService, |
47 public ui::SimpleMenuModel::Delegate { | 49 public ui::SimpleMenuModel::Delegate { |
48 public: | 50 public: |
49 BackgroundModeManager(CommandLine* command_line, | 51 BackgroundModeManager(CommandLine* command_line, |
50 ProfileInfoCache* profile_cache); | 52 ProfileInfoCache* profile_cache); |
51 virtual ~BackgroundModeManager(); | 53 virtual ~BackgroundModeManager(); |
52 | 54 |
53 static void RegisterPrefs(PrefService* prefs); | 55 static void RegisterPrefs(PrefService* prefs); |
54 | 56 |
55 virtual void RegisterProfile(Profile* profile); | 57 virtual void RegisterProfile(Profile* profile); |
56 | 58 |
57 // Returns true if background mode is permanently disabled for this Chrome | 59 // Returns true if background mode is permanently disabled for this Chrome |
58 // session. | 60 // session. |
59 static bool IsBackgroundModePermanentlyDisabled( | 61 static bool IsBackgroundModePermanentlyDisabled( |
60 const CommandLine* command_line); | 62 const CommandLine* command_line); |
61 | 63 |
62 static void LaunchBackgroundApplication(Profile* profile, | 64 static void LaunchBackgroundApplication(Profile* profile, |
63 const Extension* extension); | 65 const Extension* extension); |
64 | 66 |
67 // For testing purposes. | |
68 int NumberOfBackgroundModeData(); | |
69 | |
70 // For testing purposes. | |
71 string16 BackgroundModeDataName(Profile* profile); | |
Andrew T Wilson (Slow)
2011/12/06 06:46:02
This is fine, although since we're already exposin
rpetterson
2011/12/06 20:09:05
Good point. I'd rather not have extraneous functio
| |
72 | |
65 private: | 73 private: |
66 friend class TestBackgroundModeManager; | 74 friend class TestBackgroundModeManager; |
67 friend class BackgroundModeManagerTest; | 75 friend class BackgroundModeManagerTest; |
68 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 76 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
69 BackgroundAppLoadUnload); | 77 BackgroundAppLoadUnload); |
70 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 78 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
71 BackgroundLaunchOnStartup); | 79 BackgroundLaunchOnStartup); |
72 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 80 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
73 BackgroundAppInstallUninstallWhileDisabled); | 81 BackgroundAppInstallUninstallWhileDisabled); |
74 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 82 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
75 EnableAfterBackgroundAppInstall); | 83 EnableAfterBackgroundAppInstall); |
76 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 84 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
77 MultiProfile); | 85 MultiProfile); |
78 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 86 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
79 ProfileInfoCacheStorage); | 87 ProfileInfoCacheStorage); |
88 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | |
89 ProfileInfoCacheObserver); | |
80 | 90 |
81 class BackgroundModeData : public ui::SimpleMenuModel::Delegate { | 91 class BackgroundModeData : public ui::SimpleMenuModel::Delegate { |
82 public: | 92 public: |
83 explicit BackgroundModeData( | 93 explicit BackgroundModeData( |
84 int command_id, | 94 int command_id, |
85 Profile* profile, | 95 Profile* profile); |
86 BackgroundModeManager* background_mode_manager); | |
87 virtual ~BackgroundModeData(); | 96 virtual ~BackgroundModeData(); |
88 | 97 |
89 // The cached list of BackgroundApplications. | 98 // The cached list of BackgroundApplications. |
90 scoped_ptr<BackgroundApplicationListModel> applications_; | 99 scoped_ptr<BackgroundApplicationListModel> applications_; |
91 | 100 |
92 // Overrides from SimpleMenuModel::Delegate implementation. | 101 // Overrides from SimpleMenuModel::Delegate implementation. |
93 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; | 102 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; |
94 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; | 103 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; |
95 virtual bool GetAcceleratorForCommandId(int command_id, | 104 virtual bool GetAcceleratorForCommandId(int command_id, |
96 ui::Accelerator* accelerator) | 105 ui::Accelerator* accelerator) |
97 OVERRIDE; | 106 OVERRIDE; |
98 virtual void ExecuteCommand(int command_id) OVERRIDE; | 107 virtual void ExecuteCommand(int command_id) OVERRIDE; |
99 | 108 |
100 // Returns a browser window, or creates one if none are open. Used by | 109 // Returns a browser window, or creates one if none are open. Used by |
101 // operations (like displaying the preferences dialog) that require a | 110 // operations (like displaying the preferences dialog) that require a |
102 // Browser window. | 111 // Browser window. |
103 Browser* GetBrowserWindow(); | 112 Browser* GetBrowserWindow(); |
104 | 113 |
105 // Returns the number of background apps for this profile. | 114 // Returns the number of background apps for this profile. |
106 int GetBackgroundAppCount() const; | 115 int GetBackgroundAppCount() const; |
107 | 116 |
108 // Builds the profile specific parts of the menu. The menu passed in may | 117 // Builds the profile specific parts of the menu. The menu passed in may |
109 // be a submenu in the case of multi-profiles or the main menu in the case | 118 // be a submenu in the case of multi-profiles or the main menu in the case |
110 // of the single profile case. If containing_menu is valid, we will add | 119 // of the single profile case. If containing_menu is valid, we will add |
111 // menu as a submenu to it. | 120 // menu as a submenu to it. |
112 void BuildProfileMenu(ui::SimpleMenuModel* menu, | 121 void BuildProfileMenu(ui::SimpleMenuModel* menu, |
113 ui::SimpleMenuModel* containing_menu); | 122 ui::SimpleMenuModel* containing_menu); |
114 | 123 |
124 // Set the name associated with this background mode data for displaying in | |
125 // the status tray. | |
126 void SetName(const string16& new_profile_name); | |
127 | |
128 // The name associated with this background mode data. This should match | |
129 // the name in the ProfileInfoCache for this profile. | |
130 string16 name(); | |
131 | |
115 // Used for sorting BackgroundModeData*s. | 132 // Used for sorting BackgroundModeData*s. |
116 static bool BackgroundModeDataCompare(const BackgroundModeData* bmd1, | 133 static bool BackgroundModeDataCompare(const BackgroundModeData* bmd1, |
117 const BackgroundModeData* bmd2); | 134 const BackgroundModeData* bmd2); |
118 | 135 |
119 private: | 136 private: |
120 // Command id for the sub menu for this BackgroundModeData. | 137 // Command id for the sub menu for this BackgroundModeData. |
121 int command_id_; | 138 int command_id_; |
122 | 139 |
123 // Name associated with this profile which is used to label its submenu. | 140 // Name associated with this profile which is used to label its submenu. |
124 string16 name_; | 141 string16 name_; |
125 | 142 |
126 // The profile associated with this background app data. | 143 // The profile associated with this background app data. |
127 Profile* profile_; | 144 Profile* profile_; |
128 | |
129 // The background mode manager which owns this BackgroundModeData. | |
130 BackgroundModeManager* background_mode_manager_; | |
131 }; | 145 }; |
132 | 146 |
133 // Ideally we would want our BackgroundModeData to be scoped_ptrs, | 147 // Ideally we would want our BackgroundModeData to be scoped_ptrs, |
134 // but since maps copy their entries, we can't used scoped_ptrs. | 148 // but since maps copy their entries, we can't used scoped_ptrs. |
135 // Similarly, we can't just have a map of BackgroundModeData objects, | 149 // Similarly, we can't just have a map of BackgroundModeData objects, |
136 // since BackgroundModeData contains a scoped_ptr which once again | 150 // since BackgroundModeData contains a scoped_ptr which once again |
137 // can't be copied. So rather than using BackgroundModeData* which | 151 // can't be copied. So rather than using BackgroundModeData* which |
138 // we'd have to remember to delete, we use the ref-counted linked_ptr | 152 // we'd have to remember to delete, we use the ref-counted linked_ptr |
139 // which is similar to a shared_ptr. | 153 // which is similar to a shared_ptr. |
140 typedef linked_ptr<BackgroundModeData> BackgroundModeInfo; | 154 typedef linked_ptr<BackgroundModeData> BackgroundModeInfo; |
141 | 155 |
156 typedef std::map<Profile*, BackgroundModeInfo> BackgroundModeInfoMap; | |
157 | |
142 // content::NotificationObserver implementation. | 158 // content::NotificationObserver implementation. |
143 virtual void Observe(int type, | 159 virtual void Observe(int type, |
144 const content::NotificationSource& source, | 160 const content::NotificationSource& source, |
145 const content::NotificationDetails& details) OVERRIDE; | 161 const content::NotificationDetails& details) OVERRIDE; |
146 | 162 |
147 // BackgroundApplicationListModel::Observer implementation. | 163 // BackgroundApplicationListModel::Observer implementation. |
148 virtual void OnApplicationDataChanged(const Extension* extension, | 164 virtual void OnApplicationDataChanged(const Extension* extension, |
149 Profile* profile) OVERRIDE; | 165 Profile* profile) OVERRIDE; |
150 virtual void OnApplicationListChanged(Profile* profile) OVERRIDE; | 166 virtual void OnApplicationListChanged(Profile* profile) OVERRIDE; |
151 | 167 |
168 // Overrides from ProfileInfoCacheObserver | |
169 virtual void OnProfileAdded(const string16& profile_name, | |
170 const string16& profile_base_dir) OVERRIDE; | |
171 virtual void OnProfileRemoved(const string16& profile_name) OVERRIDE; | |
172 virtual void OnProfileNameChanged(const string16& old_profile_name, | |
173 const string16& new_profile_name) OVERRIDE; | |
174 | |
152 // Overrides from SimpleMenuModel::Delegate implementation. | 175 // Overrides from SimpleMenuModel::Delegate implementation. |
153 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; | 176 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; |
154 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; | 177 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; |
155 virtual bool GetAcceleratorForCommandId(int command_id, | 178 virtual bool GetAcceleratorForCommandId(int command_id, |
156 ui::Accelerator* accelerator) | 179 ui::Accelerator* accelerator) |
157 OVERRIDE; | 180 OVERRIDE; |
158 virtual void ExecuteCommand(int command_id) OVERRIDE; | 181 virtual void ExecuteCommand(int command_id) OVERRIDE; |
159 | 182 |
160 // Invoked when an extension is installed so we can ensure that | 183 // Invoked when an extension is installed so we can ensure that |
161 // launch-on-startup is enabled if appropriate. |extension| can be NULL when | 184 // launch-on-startup is enabled if appropriate. |extension| can be NULL when |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
204 // status tray icon which, among other things, allows the user to shutdown | 227 // status tray icon which, among other things, allows the user to shutdown |
205 // Chrome when running in background mode. All profiles are listed under | 228 // Chrome when running in background mode. All profiles are listed under |
206 // the one context menu. | 229 // the one context menu. |
207 virtual void UpdateStatusTrayIconContextMenu(); | 230 virtual void UpdateStatusTrayIconContextMenu(); |
208 | 231 |
209 // Returns the BackgroundModeData associated with this profile. If it does | 232 // Returns the BackgroundModeData associated with this profile. If it does |
210 // not exist, returns NULL. | 233 // not exist, returns NULL. |
211 BackgroundModeManager::BackgroundModeData* GetBackgroundModeData( | 234 BackgroundModeManager::BackgroundModeData* GetBackgroundModeData( |
212 Profile* const profile) const; | 235 Profile* const profile) const; |
213 | 236 |
237 // Returns the iterator associated with a particular profile name. | |
238 // If it does not exist. | |
Andrew T Wilson (Slow)
2011/12/06 06:46:02
This comment seems truncated. Also, returning an i
rpetterson
2011/12/06 20:09:05
Done.
| |
239 BackgroundModeInfoMap::iterator GetBackgroundModeIterator( | |
240 const string16& profile_name); | |
241 | |
214 // Returns true if the "Let chrome run in the background" pref is checked. | 242 // Returns true if the "Let chrome run in the background" pref is checked. |
215 // (virtual to allow overriding in tests). | 243 // (virtual to allow overriding in tests). |
216 virtual bool IsBackgroundModePrefEnabled() const; | 244 virtual bool IsBackgroundModePrefEnabled() const; |
217 | 245 |
218 // Turns off background mode if it's currently enabled. | 246 // Turns off background mode if it's currently enabled. |
219 void DisableBackgroundMode(); | 247 void DisableBackgroundMode(); |
220 | 248 |
221 // Turns on background mode if it's currently disabled. | 249 // Turns on background mode if it's currently disabled. |
222 void EnableBackgroundMode(); | 250 void EnableBackgroundMode(); |
223 | 251 |
224 // Returns the number of background apps in the system (virtual to allow | 252 // Returns the number of background apps in the system (virtual to allow |
225 // overriding in unit tests). | 253 // overriding in unit tests). |
226 virtual int GetBackgroundAppCount() const; | 254 virtual int GetBackgroundAppCount() const; |
227 | 255 |
228 // Returns the number of background apps for a profile. | 256 // Returns the number of background apps for a profile. |
229 virtual int GetBackgroundAppCountForProfile(Profile* const profile) const; | 257 virtual int GetBackgroundAppCountForProfile(Profile* const profile) const; |
230 | 258 |
231 // Reference to the profile info cache. It is used to update the background | 259 // Reference to the profile info cache. It is used to update the background |
232 // app status of profiles when they open/close background apps. | 260 // app status of profiles when they open/close background apps. |
233 ProfileInfoCache* profile_cache_; | 261 ProfileInfoCache* profile_cache_; |
234 | 262 |
235 // Registrars for managing our change observers. | 263 // Registrars for managing our change observers. |
236 content::NotificationRegistrar registrar_; | 264 content::NotificationRegistrar registrar_; |
237 PrefChangeRegistrar pref_registrar_; | 265 PrefChangeRegistrar pref_registrar_; |
238 | 266 |
239 // The profile-keyed data for this background mode manager. Keyed on profile. | 267 // The profile-keyed data for this background mode manager. Keyed on profile. |
240 std::map<Profile*, BackgroundModeInfo> background_mode_data_; | 268 BackgroundModeInfoMap background_mode_data_; |
241 | 269 |
242 // Reference to our status tray. If null, the platform doesn't support status | 270 // Reference to our status tray. If null, the platform doesn't support status |
243 // icons. | 271 // icons. |
244 StatusTray* status_tray_; | 272 StatusTray* status_tray_; |
245 | 273 |
246 // Reference to our status icon (if any) - owned by the StatusTray. | 274 // Reference to our status icon (if any) - owned by the StatusTray. |
247 StatusIcon* status_icon_; | 275 StatusIcon* status_icon_; |
248 | 276 |
249 // Reference to our status icon's context menu (if any) - owned by the | 277 // Reference to our status icon's context menu (if any) - owned by the |
250 // status_icon_. | 278 // status_icon_. |
(...skipping 14 matching lines...) Expand all Loading... | |
265 // app). | 293 // app). |
266 bool keep_alive_for_test_; | 294 bool keep_alive_for_test_; |
267 | 295 |
268 // Provides a command id for each profile as they are created. | 296 // Provides a command id for each profile as they are created. |
269 int current_command_id_; | 297 int current_command_id_; |
270 | 298 |
271 DISALLOW_COPY_AND_ASSIGN(BackgroundModeManager); | 299 DISALLOW_COPY_AND_ASSIGN(BackgroundModeManager); |
272 }; | 300 }; |
273 | 301 |
274 #endif // CHROME_BROWSER_BACKGROUND_BACKGROUND_MODE_MANAGER_H_ | 302 #endif // CHROME_BROWSER_BACKGROUND_BACKGROUND_MODE_MANAGER_H_ |
OLD | NEW |