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 |
65 private: | 70 private: |
66 friend class TestBackgroundModeManager; | 71 friend class TestBackgroundModeManager; |
67 friend class BackgroundModeManagerTest; | 72 friend class BackgroundModeManagerTest; |
68 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 73 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
69 BackgroundAppLoadUnload); | 74 BackgroundAppLoadUnload); |
70 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 75 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
71 BackgroundLaunchOnStartup); | 76 BackgroundLaunchOnStartup); |
72 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 77 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
73 BackgroundAppInstallUninstallWhileDisabled); | 78 BackgroundAppInstallUninstallWhileDisabled); |
74 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 79 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
75 EnableAfterBackgroundAppInstall); | 80 EnableAfterBackgroundAppInstall); |
76 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 81 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
77 MultiProfile); | 82 MultiProfile); |
78 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 83 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
79 ProfileInfoCacheStorage); | 84 ProfileInfoCacheStorage); |
| 85 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, |
| 86 ProfileInfoCacheObserver); |
80 | 87 |
81 class BackgroundModeData : public ui::SimpleMenuModel::Delegate { | 88 class BackgroundModeData : public ui::SimpleMenuModel::Delegate { |
82 public: | 89 public: |
83 explicit BackgroundModeData( | 90 explicit BackgroundModeData( |
84 int command_id, | 91 int command_id, |
85 Profile* profile, | 92 Profile* profile); |
86 BackgroundModeManager* background_mode_manager); | |
87 virtual ~BackgroundModeData(); | 93 virtual ~BackgroundModeData(); |
88 | 94 |
89 // The cached list of BackgroundApplications. | 95 // The cached list of BackgroundApplications. |
90 scoped_ptr<BackgroundApplicationListModel> applications_; | 96 scoped_ptr<BackgroundApplicationListModel> applications_; |
91 | 97 |
92 // Overrides from SimpleMenuModel::Delegate implementation. | 98 // Overrides from SimpleMenuModel::Delegate implementation. |
93 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; | 99 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; |
94 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; | 100 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; |
95 virtual bool GetAcceleratorForCommandId(int command_id, | 101 virtual bool GetAcceleratorForCommandId(int command_id, |
96 ui::Accelerator* accelerator) | 102 ui::Accelerator* accelerator) |
97 OVERRIDE; | 103 OVERRIDE; |
98 virtual void ExecuteCommand(int command_id) OVERRIDE; | 104 virtual void ExecuteCommand(int command_id) OVERRIDE; |
99 | 105 |
100 // Returns a browser window, or creates one if none are open. Used by | 106 // Returns a browser window, or creates one if none are open. Used by |
101 // operations (like displaying the preferences dialog) that require a | 107 // operations (like displaying the preferences dialog) that require a |
102 // Browser window. | 108 // Browser window. |
103 Browser* GetBrowserWindow(); | 109 Browser* GetBrowserWindow(); |
104 | 110 |
105 // Returns the number of background apps for this profile. | 111 // Returns the number of background apps for this profile. |
106 int GetBackgroundAppCount() const; | 112 int GetBackgroundAppCount() const; |
107 | 113 |
108 // Builds the profile specific parts of the menu. The menu passed in may | 114 // 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 | 115 // 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 | 116 // of the single profile case. If containing_menu is valid, we will add |
111 // menu as a submenu to it. | 117 // menu as a submenu to it. |
112 void BuildProfileMenu(ui::SimpleMenuModel* menu, | 118 void BuildProfileMenu(ui::SimpleMenuModel* menu, |
113 ui::SimpleMenuModel* containing_menu); | 119 ui::SimpleMenuModel* containing_menu); |
114 | 120 |
| 121 // Set the name associated with this background mode data for displaying in |
| 122 // the status tray. |
| 123 void SetName(const string16& new_profile_name); |
| 124 |
| 125 // The name associated with this background mode data. This should match |
| 126 // the name in the ProfileInfoCache for this profile. |
| 127 string16 name(); |
| 128 |
115 // Used for sorting BackgroundModeData*s. | 129 // Used for sorting BackgroundModeData*s. |
116 static bool BackgroundModeDataCompare(const BackgroundModeData* bmd1, | 130 static bool BackgroundModeDataCompare(const BackgroundModeData* bmd1, |
117 const BackgroundModeData* bmd2); | 131 const BackgroundModeData* bmd2); |
118 | 132 |
119 private: | 133 private: |
120 // Command id for the sub menu for this BackgroundModeData. | 134 // Command id for the sub menu for this BackgroundModeData. |
121 int command_id_; | 135 int command_id_; |
122 | 136 |
123 // Name associated with this profile which is used to label its submenu. | 137 // Name associated with this profile which is used to label its submenu. |
124 string16 name_; | 138 string16 name_; |
125 | 139 |
126 // The profile associated with this background app data. | 140 // The profile associated with this background app data. |
127 Profile* profile_; | 141 Profile* profile_; |
128 | |
129 // The background mode manager which owns this BackgroundModeData. | |
130 BackgroundModeManager* background_mode_manager_; | |
131 }; | 142 }; |
132 | 143 |
133 // Ideally we would want our BackgroundModeData to be scoped_ptrs, | 144 // Ideally we would want our BackgroundModeData to be scoped_ptrs, |
134 // but since maps copy their entries, we can't used scoped_ptrs. | 145 // but since maps copy their entries, we can't used scoped_ptrs. |
135 // Similarly, we can't just have a map of BackgroundModeData objects, | 146 // Similarly, we can't just have a map of BackgroundModeData objects, |
136 // since BackgroundModeData contains a scoped_ptr which once again | 147 // since BackgroundModeData contains a scoped_ptr which once again |
137 // can't be copied. So rather than using BackgroundModeData* which | 148 // 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 | 149 // we'd have to remember to delete, we use the ref-counted linked_ptr |
139 // which is similar to a shared_ptr. | 150 // which is similar to a shared_ptr. |
140 typedef linked_ptr<BackgroundModeData> BackgroundModeInfo; | 151 typedef linked_ptr<BackgroundModeData> BackgroundModeInfo; |
141 | 152 |
| 153 typedef std::map<Profile*, BackgroundModeInfo> BackgroundModeInfoMap; |
| 154 |
142 // content::NotificationObserver implementation. | 155 // content::NotificationObserver implementation. |
143 virtual void Observe(int type, | 156 virtual void Observe(int type, |
144 const content::NotificationSource& source, | 157 const content::NotificationSource& source, |
145 const content::NotificationDetails& details) OVERRIDE; | 158 const content::NotificationDetails& details) OVERRIDE; |
146 | 159 |
147 // BackgroundApplicationListModel::Observer implementation. | 160 // BackgroundApplicationListModel::Observer implementation. |
148 virtual void OnApplicationDataChanged(const Extension* extension, | 161 virtual void OnApplicationDataChanged(const Extension* extension, |
149 Profile* profile) OVERRIDE; | 162 Profile* profile) OVERRIDE; |
150 virtual void OnApplicationListChanged(Profile* profile) OVERRIDE; | 163 virtual void OnApplicationListChanged(Profile* profile) OVERRIDE; |
151 | 164 |
| 165 // Overrides from ProfileInfoCacheObserver |
| 166 virtual void OnProfileAdded(const string16& profile_name, |
| 167 const string16& profile_base_dir, |
| 168 const FilePath& profile_path, |
| 169 const gfx::Image* avatar_image) OVERRIDE; |
| 170 virtual void OnProfileRemoved(const string16& profile_name) OVERRIDE; |
| 171 virtual void OnProfileNameChanged(const string16& old_profile_name, |
| 172 const string16& new_profile_name) OVERRIDE; |
| 173 virtual void OnProfileAvatarChanged(const string16& profile_name, |
| 174 const string16& profile_base_dir, |
| 175 const FilePath& profile_path, |
| 176 const gfx::Image* avatar_image) OVERRIDE; |
| 177 |
152 // Overrides from SimpleMenuModel::Delegate implementation. | 178 // Overrides from SimpleMenuModel::Delegate implementation. |
153 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; | 179 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; |
154 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; | 180 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; |
155 virtual bool GetAcceleratorForCommandId(int command_id, | 181 virtual bool GetAcceleratorForCommandId(int command_id, |
156 ui::Accelerator* accelerator) | 182 ui::Accelerator* accelerator) |
157 OVERRIDE; | 183 OVERRIDE; |
158 virtual void ExecuteCommand(int command_id) OVERRIDE; | 184 virtual void ExecuteCommand(int command_id) OVERRIDE; |
159 | 185 |
160 // Invoked when an extension is installed so we can ensure that | 186 // Invoked when an extension is installed so we can ensure that |
161 // launch-on-startup is enabled if appropriate. |extension| can be NULL when | 187 // 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 | 230 // status tray icon which, among other things, allows the user to shutdown |
205 // Chrome when running in background mode. All profiles are listed under | 231 // Chrome when running in background mode. All profiles are listed under |
206 // the one context menu. | 232 // the one context menu. |
207 virtual void UpdateStatusTrayIconContextMenu(); | 233 virtual void UpdateStatusTrayIconContextMenu(); |
208 | 234 |
209 // Returns the BackgroundModeData associated with this profile. If it does | 235 // Returns the BackgroundModeData associated with this profile. If it does |
210 // not exist, returns NULL. | 236 // not exist, returns NULL. |
211 BackgroundModeManager::BackgroundModeData* GetBackgroundModeData( | 237 BackgroundModeManager::BackgroundModeData* GetBackgroundModeData( |
212 Profile* const profile) const; | 238 Profile* const profile) const; |
213 | 239 |
| 240 // Returns the iterator associated with a particular profile name. |
| 241 // This should not be used to iterate over the background mode data. It is |
| 242 // used to efficiently delete an item from the background mode data map. |
| 243 BackgroundModeInfoMap::iterator GetBackgroundModeIterator( |
| 244 const string16& profile_name); |
| 245 |
214 // Returns true if the "Let chrome run in the background" pref is checked. | 246 // Returns true if the "Let chrome run in the background" pref is checked. |
215 // (virtual to allow overriding in tests). | 247 // (virtual to allow overriding in tests). |
216 virtual bool IsBackgroundModePrefEnabled() const; | 248 virtual bool IsBackgroundModePrefEnabled() const; |
217 | 249 |
218 // Turns off background mode if it's currently enabled. | 250 // Turns off background mode if it's currently enabled. |
219 void DisableBackgroundMode(); | 251 void DisableBackgroundMode(); |
220 | 252 |
221 // Turns on background mode if it's currently disabled. | 253 // Turns on background mode if it's currently disabled. |
222 void EnableBackgroundMode(); | 254 void EnableBackgroundMode(); |
223 | 255 |
224 // Returns the number of background apps in the system (virtual to allow | 256 // Returns the number of background apps in the system (virtual to allow |
225 // overriding in unit tests). | 257 // overriding in unit tests). |
226 virtual int GetBackgroundAppCount() const; | 258 virtual int GetBackgroundAppCount() const; |
227 | 259 |
228 // Returns the number of background apps for a profile. | 260 // Returns the number of background apps for a profile. |
229 virtual int GetBackgroundAppCountForProfile(Profile* const profile) const; | 261 virtual int GetBackgroundAppCountForProfile(Profile* const profile) const; |
230 | 262 |
231 // Reference to the profile info cache. It is used to update the background | 263 // Reference to the profile info cache. It is used to update the background |
232 // app status of profiles when they open/close background apps. | 264 // app status of profiles when they open/close background apps. |
233 ProfileInfoCache* profile_cache_; | 265 ProfileInfoCache* profile_cache_; |
234 | 266 |
235 // Registrars for managing our change observers. | 267 // Registrars for managing our change observers. |
236 content::NotificationRegistrar registrar_; | 268 content::NotificationRegistrar registrar_; |
237 PrefChangeRegistrar pref_registrar_; | 269 PrefChangeRegistrar pref_registrar_; |
238 | 270 |
239 // The profile-keyed data for this background mode manager. Keyed on profile. | 271 // The profile-keyed data for this background mode manager. Keyed on profile. |
240 std::map<Profile*, BackgroundModeInfo> background_mode_data_; | 272 BackgroundModeInfoMap background_mode_data_; |
241 | 273 |
242 // Reference to our status tray. If null, the platform doesn't support status | 274 // Reference to our status tray. If null, the platform doesn't support status |
243 // icons. | 275 // icons. |
244 StatusTray* status_tray_; | 276 StatusTray* status_tray_; |
245 | 277 |
246 // Reference to our status icon (if any) - owned by the StatusTray. | 278 // Reference to our status icon (if any) - owned by the StatusTray. |
247 StatusIcon* status_icon_; | 279 StatusIcon* status_icon_; |
248 | 280 |
249 // Reference to our status icon's context menu (if any) - owned by the | 281 // Reference to our status icon's context menu (if any) - owned by the |
250 // status_icon_. | 282 // status_icon_. |
(...skipping 14 matching lines...) Expand all Loading... |
265 // app). | 297 // app). |
266 bool keep_alive_for_test_; | 298 bool keep_alive_for_test_; |
267 | 299 |
268 // Provides a command id for each profile as they are created. | 300 // Provides a command id for each profile as they are created. |
269 int current_command_id_; | 301 int current_command_id_; |
270 | 302 |
271 DISALLOW_COPY_AND_ASSIGN(BackgroundModeManager); | 303 DISALLOW_COPY_AND_ASSIGN(BackgroundModeManager); |
272 }; | 304 }; |
273 | 305 |
274 #endif // CHROME_BROWSER_BACKGROUND_BACKGROUND_MODE_MANAGER_H_ | 306 #endif // CHROME_BROWSER_BACKGROUND_BACKGROUND_MODE_MANAGER_H_ |
OLD | NEW |