Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: chrome/browser/background/background_mode_manager.h

Issue 8802013: Fixing background mode manager to correctly display the name of the profile in the status icon. P... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
64 ProfileInfoCache* profile_cache();
65
62 static void LaunchBackgroundApplication(Profile* profile, 66 static void LaunchBackgroundApplication(Profile* profile,
63 const Extension* extension); 67 const Extension* extension);
64 68
65 private: 69 private:
66 friend class TestBackgroundModeManager; 70 friend class TestBackgroundModeManager;
67 friend class BackgroundModeManagerTest; 71 friend class BackgroundModeManagerTest;
68 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, 72 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest,
69 BackgroundAppLoadUnload); 73 BackgroundAppLoadUnload);
70 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, 74 FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest,
71 BackgroundLaunchOnStartup); 75 BackgroundLaunchOnStartup);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 // Returns the number of background apps for this profile. 109 // Returns the number of background apps for this profile.
106 int GetBackgroundAppCount() const; 110 int GetBackgroundAppCount() const;
107 111
108 // Builds the profile specific parts of the menu. The menu passed in may 112 // 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 113 // 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 114 // of the single profile case. If containing_menu is valid, we will add
111 // menu as a submenu to it. 115 // menu as a submenu to it.
112 void BuildProfileMenu(ui::SimpleMenuModel* menu, 116 void BuildProfileMenu(ui::SimpleMenuModel* menu,
113 ui::SimpleMenuModel* containing_menu); 117 ui::SimpleMenuModel* containing_menu);
114 118
119 // Set the name associated with this background mode data for displaying in
120 // the status tray.
121 void SetName(const string16& new_profile_name);
122
123 // The name associated with this background mode data. This should match
124 // the name in the ProfileInfoCache for this profile.
125 string16 name();
126
115 // Used for sorting BackgroundModeData*s. 127 // Used for sorting BackgroundModeData*s.
116 static bool BackgroundModeDataCompare(const BackgroundModeData* bmd1, 128 static bool BackgroundModeDataCompare(const BackgroundModeData* bmd1,
117 const BackgroundModeData* bmd2); 129 const BackgroundModeData* bmd2);
118 130
119 private: 131 private:
120 // Command id for the sub menu for this BackgroundModeData. 132 // Command id for the sub menu for this BackgroundModeData.
121 int command_id_; 133 int command_id_;
122 134
123 // Name associated with this profile which is used to label its submenu. 135 // Name associated with this profile which is used to label its submenu.
124 string16 name_; 136 string16 name_;
125 137
126 // The profile associated with this background app data. 138 // The profile associated with this background app data.
127 Profile* profile_; 139 Profile* profile_;
128 140
129 // The background mode manager which owns this BackgroundModeData. 141 // The background mode manager which owns this BackgroundModeData.
130 BackgroundModeManager* background_mode_manager_; 142 BackgroundModeManager* background_mode_manager_;
131 }; 143 };
132 144
133 // Ideally we would want our BackgroundModeData to be scoped_ptrs, 145 // Ideally we would want our BackgroundModeData to be scoped_ptrs,
134 // but since maps copy their entries, we can't used scoped_ptrs. 146 // but since maps copy their entries, we can't used scoped_ptrs.
135 // Similarly, we can't just have a map of BackgroundModeData objects, 147 // Similarly, we can't just have a map of BackgroundModeData objects,
136 // since BackgroundModeData contains a scoped_ptr which once again 148 // since BackgroundModeData contains a scoped_ptr which once again
137 // can't be copied. So rather than using BackgroundModeData* which 149 // 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 150 // we'd have to remember to delete, we use the ref-counted linked_ptr
139 // which is similar to a shared_ptr. 151 // which is similar to a shared_ptr.
140 typedef linked_ptr<BackgroundModeData> BackgroundModeInfo; 152 typedef linked_ptr<BackgroundModeData> BackgroundModeInfo;
141 153
154 typedef std::map<Profile*, BackgroundModeInfo> BackgroundModeInfoMap;
155
142 // content::NotificationObserver implementation. 156 // content::NotificationObserver implementation.
143 virtual void Observe(int type, 157 virtual void Observe(int type,
144 const content::NotificationSource& source, 158 const content::NotificationSource& source,
145 const content::NotificationDetails& details) OVERRIDE; 159 const content::NotificationDetails& details) OVERRIDE;
146 160
147 // BackgroundApplicationListModel::Observer implementation. 161 // BackgroundApplicationListModel::Observer implementation.
148 virtual void OnApplicationDataChanged(const Extension* extension, 162 virtual void OnApplicationDataChanged(const Extension* extension,
149 Profile* profile) OVERRIDE; 163 Profile* profile) OVERRIDE;
150 virtual void OnApplicationListChanged(Profile* profile) OVERRIDE; 164 virtual void OnApplicationListChanged(Profile* profile) OVERRIDE;
151 165
166 // Overrides from ProfileInfoCacheObserver
167 virtual void OnProfileAdded(const string16& profile_name,
168 const string16& profile_base_dir) OVERRIDE;
169 virtual void OnProfileRemoved(const string16& profile_name) OVERRIDE;
170 virtual void OnProfileNameChanged(const string16& old_profile_name,
171 const string16& new_profile_name) OVERRIDE;
172
152 // Overrides from SimpleMenuModel::Delegate implementation. 173 // Overrides from SimpleMenuModel::Delegate implementation.
153 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; 174 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
154 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; 175 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
155 virtual bool GetAcceleratorForCommandId(int command_id, 176 virtual bool GetAcceleratorForCommandId(int command_id,
156 ui::Accelerator* accelerator) 177 ui::Accelerator* accelerator)
157 OVERRIDE; 178 OVERRIDE;
158 virtual void ExecuteCommand(int command_id) OVERRIDE; 179 virtual void ExecuteCommand(int command_id) OVERRIDE;
159 180
160 // Invoked when an extension is installed so we can ensure that 181 // Invoked when an extension is installed so we can ensure that
161 // launch-on-startup is enabled if appropriate. |extension| can be NULL when 182 // launch-on-startup is enabled if appropriate. |extension| can be NULL when
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 // status tray icon which, among other things, allows the user to shutdown 225 // status tray icon which, among other things, allows the user to shutdown
205 // Chrome when running in background mode. All profiles are listed under 226 // Chrome when running in background mode. All profiles are listed under
206 // the one context menu. 227 // the one context menu.
207 virtual void UpdateStatusTrayIconContextMenu(); 228 virtual void UpdateStatusTrayIconContextMenu();
208 229
209 // Returns the BackgroundModeData associated with this profile. If it does 230 // Returns the BackgroundModeData associated with this profile. If it does
210 // not exist, returns NULL. 231 // not exist, returns NULL.
211 BackgroundModeManager::BackgroundModeData* GetBackgroundModeData( 232 BackgroundModeManager::BackgroundModeData* GetBackgroundModeData(
212 Profile* const profile) const; 233 Profile* const profile) const;
213 234
235 // Returns the iterator associated with a particular profile name.
236 // If it does not exist, returns NULL.
Andrew T Wilson (Slow) 2011/12/06 00:46:03 Update this comment since this cannot return NULL.
rpetterson 2011/12/06 05:52:34 Done.
237 BackgroundModeInfoMap::const_iterator GetBackgroundModeIterator(
238 const string16& profile_name) const;
239
214 // Returns true if the "Let chrome run in the background" pref is checked. 240 // Returns true if the "Let chrome run in the background" pref is checked.
215 // (virtual to allow overriding in tests). 241 // (virtual to allow overriding in tests).
216 virtual bool IsBackgroundModePrefEnabled() const; 242 virtual bool IsBackgroundModePrefEnabled() const;
217 243
218 // Turns off background mode if it's currently enabled. 244 // Turns off background mode if it's currently enabled.
219 void DisableBackgroundMode(); 245 void DisableBackgroundMode();
220 246
221 // Turns on background mode if it's currently disabled. 247 // Turns on background mode if it's currently disabled.
222 void EnableBackgroundMode(); 248 void EnableBackgroundMode();
223 249
224 // Returns the number of background apps in the system (virtual to allow 250 // Returns the number of background apps in the system (virtual to allow
225 // overriding in unit tests). 251 // overriding in unit tests).
226 virtual int GetBackgroundAppCount() const; 252 virtual int GetBackgroundAppCount() const;
227 253
228 // Returns the number of background apps for a profile. 254 // Returns the number of background apps for a profile.
229 virtual int GetBackgroundAppCountForProfile(Profile* const profile) const; 255 virtual int GetBackgroundAppCountForProfile(Profile* const profile) const;
230 256
231 // Reference to the profile info cache. It is used to update the background 257 // Reference to the profile info cache. It is used to update the background
232 // app status of profiles when they open/close background apps. 258 // app status of profiles when they open/close background apps.
233 ProfileInfoCache* profile_cache_; 259 ProfileInfoCache* profile_cache_;
234 260
235 // Registrars for managing our change observers. 261 // Registrars for managing our change observers.
236 content::NotificationRegistrar registrar_; 262 content::NotificationRegistrar registrar_;
237 PrefChangeRegistrar pref_registrar_; 263 PrefChangeRegistrar pref_registrar_;
238 264
239 // The profile-keyed data for this background mode manager. Keyed on profile. 265 // The profile-keyed data for this background mode manager. Keyed on profile.
240 std::map<Profile*, BackgroundModeInfo> background_mode_data_; 266 BackgroundModeInfoMap background_mode_data_;
241 267
242 // Reference to our status tray. If null, the platform doesn't support status 268 // Reference to our status tray. If null, the platform doesn't support status
243 // icons. 269 // icons.
244 StatusTray* status_tray_; 270 StatusTray* status_tray_;
245 271
246 // Reference to our status icon (if any) - owned by the StatusTray. 272 // Reference to our status icon (if any) - owned by the StatusTray.
247 StatusIcon* status_icon_; 273 StatusIcon* status_icon_;
248 274
249 // Reference to our status icon's context menu (if any) - owned by the 275 // Reference to our status icon's context menu (if any) - owned by the
250 // status_icon_. 276 // status_icon_.
(...skipping 14 matching lines...) Expand all
265 // app). 291 // app).
266 bool keep_alive_for_test_; 292 bool keep_alive_for_test_;
267 293
268 // Provides a command id for each profile as they are created. 294 // Provides a command id for each profile as they are created.
269 int current_command_id_; 295 int current_command_id_;
270 296
271 DISALLOW_COPY_AND_ASSIGN(BackgroundModeManager); 297 DISALLOW_COPY_AND_ASSIGN(BackgroundModeManager);
272 }; 298 };
273 299
274 #endif // CHROME_BROWSER_BACKGROUND_BACKGROUND_MODE_MANAGER_H_ 300 #endif // CHROME_BROWSER_BACKGROUND_BACKGROUND_MODE_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698