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 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 12 matching lines...) Expand all Loading... | |
23 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
24 #include "content/browser/user_metrics.h" | 24 #include "content/browser/user_metrics.h" |
25 #include "content/common/notification_service.h" | 25 #include "content/common/notification_service.h" |
26 #include "content/common/notification_type.h" | 26 #include "content/common/notification_type.h" |
27 #include "grit/chromium_strings.h" | 27 #include "grit/chromium_strings.h" |
28 #include "grit/generated_resources.h" | 28 #include "grit/generated_resources.h" |
29 #include "grit/theme_resources.h" | 29 #include "grit/theme_resources.h" |
30 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
31 #include "ui/base/resource/resource_bundle.h" | 31 #include "ui/base/resource/resource_bundle.h" |
32 | 32 |
33 void BackgroundModeManager::OnApplicationDataChanged( | 33 BackgroundModeManager::BackgroundModeData::BackgroundModeData(Profile* profile) |
34 const Extension* extension) { | 34 : applications_(new BackgroundApplicationListModel(profile)), |
35 UpdateContextMenuEntryIcon(extension); | 35 status_icon_(NULL), |
36 context_menu_(NULL), | |
37 context_menu_application_offset_(0) { | |
36 } | 38 } |
37 | 39 |
38 void BackgroundModeManager::OnApplicationListChanged() { | 40 BackgroundModeManager::BackgroundModeData::~BackgroundModeData() { |
39 UpdateStatusTrayIconContextMenu(); | |
40 } | 41 } |
41 | 42 |
42 BackgroundModeManager::BackgroundModeManager(Profile* profile, | 43 void BackgroundModeManager::OnApplicationDataChanged( |
43 CommandLine* command_line) | 44 const Extension* extension, Profile* profile) { |
44 : profile_(profile), | 45 UpdateContextMenuEntryIcon(extension, profile); |
45 applications_(profile), | 46 } |
47 | |
48 void BackgroundModeManager::OnApplicationListChanged(Profile* profile) { | |
49 UpdateStatusTrayIconContextMenu(profile); | |
50 } | |
51 | |
52 BackgroundModeManager::BackgroundModeManager(CommandLine* command_line) | |
53 : status_tray_(NULL), | |
46 background_app_count_(0), | 54 background_app_count_(0), |
47 context_menu_(NULL), | |
48 context_menu_application_offset_(0), | |
49 in_background_mode_(false), | 55 in_background_mode_(false), |
50 keep_alive_for_startup_(false), | 56 keep_alive_for_startup_(false) { |
51 status_tray_(NULL), | |
52 status_icon_(NULL) { | |
53 // If background mode is currently disabled, just exit - don't listen for any | 57 // If background mode is currently disabled, just exit - don't listen for any |
54 // notifications. | 58 // notifications. |
55 if (IsBackgroundModePermanentlyDisabled(command_line)) | 59 if (IsBackgroundModePermanentlyDisabled(command_line)) |
56 return; | 60 return; |
57 | 61 |
58 // Listen for the background mode preference changing. | 62 // Listen for the background mode preference changing. |
59 if (g_browser_process->local_state()) { // Skip for unit tests | 63 if (g_browser_process->local_state()) { // Skip for unit tests |
60 pref_registrar_.Init(g_browser_process->local_state()); | 64 pref_registrar_.Init(g_browser_process->local_state()); |
61 pref_registrar_.Add(prefs::kBackgroundModeEnabled, this); | 65 pref_registrar_.Add(prefs::kBackgroundModeEnabled, this); |
62 } | 66 } |
63 | 67 |
64 // Keep the browser alive until extensions are done loading - this is needed | 68 // Keep the browser alive until extensions are done loading - this is needed |
65 // by the --no-startup-window flag. We want to stay alive until we load | 69 // by the --no-startup-window flag. We want to stay alive until we load |
66 // extensions, at which point we should either run in background mode (if | 70 // extensions, at which point we should either run in background mode (if |
67 // there are background apps) or exit if there are none. | 71 // there are background apps) or exit if there are none. |
68 if (command_line->HasSwitch(switches::kNoStartupWindow)) { | 72 if (command_line->HasSwitch(switches::kNoStartupWindow)) { |
69 keep_alive_for_startup_ = true; | 73 keep_alive_for_startup_ = true; |
70 BrowserList::StartKeepAlive(); | 74 BrowserList::StartKeepAlive(); |
71 } | 75 } |
72 | 76 |
73 // If the -keep-alive-for-test flag is passed, then always keep chrome running | 77 // If the -keep-alive-for-test flag is passed, then always keep chrome running |
74 // in the background until the user explicitly terminates it, by acting as if | 78 // in the background until the user explicitly terminates it, by acting as if |
75 // we loaded a background app. | 79 // we loaded a background app. |
76 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kKeepAliveForTest)) | 80 if (command_line->HasSwitch(switches::kKeepAliveForTest)) |
77 OnBackgroundAppLoaded(); | 81 OnBackgroundAppLoaded(); |
78 | 82 |
83 // Listen for the application shutting down so we can decrement our KeepAlive | |
84 // count. | |
85 registrar_.Add(this, NotificationType::APP_TERMINATING, | |
86 NotificationService::AllSources()); | |
87 } | |
88 | |
89 BackgroundModeManager::~BackgroundModeManager() { | |
90 for (std::map<Profile*, BackgroundModeInfo>::iterator it = | |
91 background_mode_data_.begin(); | |
92 it != background_mode_data_.end(); | |
93 ++it) { | |
94 it->second->applications_->RemoveObserver(this); | |
95 } | |
96 | |
97 // We're going away, so exit background mode (does nothing if we aren't in | |
98 // background mode currently). This is primarily needed for unit tests, | |
99 // because in an actual running system we'd get an APP_TERMINATING | |
100 // notification before being destroyed. | |
101 EndBackgroundMode(); | |
102 } | |
103 | |
104 void BackgroundModeManager::RegisterProfile(Profile* profile) { | |
105 // We don't want to register multiple times for one profile. | |
106 DCHECK(background_mode_data_.find(profile) == background_mode_data_.end()); | |
107 BackgroundModeInfo bmd(new BackgroundModeData(profile)); | |
108 background_mode_data_[profile] = bmd; | |
109 | |
79 // Listen for when extensions are loaded/unloaded so we can track the | 110 // Listen for when extensions are loaded/unloaded so we can track the |
80 // number of background apps and modify our keep-alive and launch-on-startup | 111 // number of background apps and modify our keep-alive and launch-on-startup |
81 // state appropriately. | 112 // state appropriately. |
82 registrar_.Add(this, NotificationType::EXTENSION_LOADED, | 113 registrar_.Add(this, NotificationType::EXTENSION_LOADED, |
83 Source<Profile>(profile)); | 114 Source<Profile>(profile)); |
84 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, | 115 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, |
85 Source<Profile>(profile)); | 116 Source<Profile>(profile)); |
86 | 117 |
87 // Check for the presence of background apps after all extensions have been | 118 // Check for the presence of background apps after all extensions have been |
88 // loaded, to handle the case where an extension has been manually removed | 119 // loaded, to handle the case where an extension has been manually removed |
89 // while Chrome was not running. | 120 // while Chrome was not running. |
90 registrar_.Add(this, NotificationType::EXTENSIONS_READY, | 121 registrar_.Add(this, NotificationType::EXTENSIONS_READY, |
91 Source<Profile>(profile)); | 122 Source<Profile>(profile)); |
92 | 123 |
93 // Listen for the application shutting down so we can decrement our KeepAlive | 124 background_mode_data_[profile]->applications_->AddObserver(this); |
94 // count. | |
95 registrar_.Add(this, NotificationType::APP_TERMINATING, | |
96 NotificationService::AllSources()); | |
97 | |
98 applications_.AddObserver(this); | |
99 } | |
100 | |
101 BackgroundModeManager::~BackgroundModeManager() { | |
102 applications_.RemoveObserver(this); | |
103 | |
104 // We're going away, so exit background mode (does nothing if we aren't in | |
105 // background mode currently). This is primarily needed for unit tests, | |
106 // because in an actual running system we'd get an APP_TERMINATING | |
107 // notification before being destroyed. | |
108 EndBackgroundMode(); | |
109 } | 125 } |
110 | 126 |
111 void BackgroundModeManager::Observe(NotificationType type, | 127 void BackgroundModeManager::Observe(NotificationType type, |
112 const NotificationSource& source, | 128 const NotificationSource& source, |
113 const NotificationDetails& details) { | 129 const NotificationDetails& details) { |
114 switch (type.value) { | 130 switch (type.value) { |
115 case NotificationType::PREF_CHANGED: | 131 case NotificationType::PREF_CHANGED: |
116 DCHECK(*Details<std::string>(details).ptr() == | 132 DCHECK(*Details<std::string>(details).ptr() == |
117 prefs::kBackgroundModeEnabled); | 133 prefs::kBackgroundModeEnabled); |
118 if (IsBackgroundModePrefEnabled()) | 134 if (IsBackgroundModePrefEnabled()) |
(...skipping 14 matching lines...) Expand all Loading... | |
133 // launch on startup even after the user removes the LoginItem manually. | 149 // launch on startup even after the user removes the LoginItem manually. |
134 #if !defined(OS_MACOSX) | 150 #if !defined(OS_MACOSX) |
135 EnableLaunchOnStartup(background_app_count_ > 0); | 151 EnableLaunchOnStartup(background_app_count_ > 0); |
136 #endif | 152 #endif |
137 break; | 153 break; |
138 case NotificationType::EXTENSION_LOADED: { | 154 case NotificationType::EXTENSION_LOADED: { |
139 Extension* extension = Details<Extension>(details).ptr(); | 155 Extension* extension = Details<Extension>(details).ptr(); |
140 if (BackgroundApplicationListModel::IsBackgroundApp(*extension)) { | 156 if (BackgroundApplicationListModel::IsBackgroundApp(*extension)) { |
141 // Extensions loaded after the ExtensionsService is ready should be | 157 // Extensions loaded after the ExtensionsService is ready should be |
142 // treated as new installs. | 158 // treated as new installs. |
143 if (profile_->GetExtensionService()->is_ready()) | 159 |
144 OnBackgroundAppInstalled(extension); | 160 Profile* profile = Source<Profile>(source).ptr(); |
161 if (profile->GetExtensionService()->is_ready()) | |
162 OnBackgroundAppInstalled(extension, profile); | |
145 OnBackgroundAppLoaded(); | 163 OnBackgroundAppLoaded(); |
146 } | 164 } |
147 } | 165 } |
148 break; | 166 break; |
149 case NotificationType::EXTENSION_UNLOADED: | 167 case NotificationType::EXTENSION_UNLOADED: |
150 if (BackgroundApplicationListModel::IsBackgroundApp( | 168 if (BackgroundApplicationListModel::IsBackgroundApp( |
151 *Details<UnloadedExtensionInfo>(details)->extension)) { | 169 *Details<UnloadedExtensionInfo>(details)->extension)) { |
152 Details<UnloadedExtensionInfo> info = | 170 Details<UnloadedExtensionInfo> info = |
153 Details<UnloadedExtensionInfo>(details); | 171 Details<UnloadedExtensionInfo>(details); |
154 // If we already got an unload notification when it was disabled, ignore | 172 // If we already got an unload notification when it was disabled, ignore |
(...skipping 30 matching lines...) Expand all Loading... | |
185 // keep-alive (which can shutdown Chrome) before the message loop has | 203 // keep-alive (which can shutdown Chrome) before the message loop has |
186 // started. | 204 // started. |
187 MessageLoop::current()->PostTask( | 205 MessageLoop::current()->PostTask( |
188 FROM_HERE, NewRunnableFunction(BrowserList::EndKeepAlive)); | 206 FROM_HERE, NewRunnableFunction(BrowserList::EndKeepAlive)); |
189 } | 207 } |
190 } | 208 } |
191 | 209 |
192 void BackgroundModeManager::OnBackgroundAppLoaded() { | 210 void BackgroundModeManager::OnBackgroundAppLoaded() { |
193 // When a background app loads, increment our count and also enable | 211 // When a background app loads, increment our count and also enable |
194 // KeepAlive mode if the preference is set. | 212 // KeepAlive mode if the preference is set. |
213 // The count here is across all profiles since we must have background | |
214 // mode if there is even one. | |
195 background_app_count_++; | 215 background_app_count_++; |
196 if (background_app_count_ == 1) | 216 if (background_app_count_ == 1) |
197 StartBackgroundMode(); | 217 StartBackgroundMode(); |
198 } | 218 } |
199 | 219 |
200 void BackgroundModeManager::StartBackgroundMode() { | 220 void BackgroundModeManager::StartBackgroundMode() { |
201 // Don't bother putting ourselves in background mode if we're already there | 221 // Don't bother putting ourselves in background mode if we're already there |
202 // or if background mode is disabled. | 222 // or if background mode is disabled. |
203 if (in_background_mode_ || !IsBackgroundModePrefEnabled()) | 223 if (in_background_mode_ || !IsBackgroundModePrefEnabled()) |
204 return; | 224 return; |
205 | 225 |
206 // Mark ourselves as running in background mode. | 226 // Mark ourselves as running in background mode. |
207 in_background_mode_ = true; | 227 in_background_mode_ = true; |
208 | 228 |
209 // Put ourselves in KeepAlive mode and create a status tray icon. | 229 // Put ourselves in KeepAlive mode and create a status tray icon. |
210 BrowserList::StartKeepAlive(); | 230 BrowserList::StartKeepAlive(); |
211 | 231 |
212 // Display a status icon to exit Chrome. | 232 // Display a status icon to exit Chrome. |
213 CreateStatusTrayIcon(); | 233 InitStatusTrayIcons(); |
234 } | |
235 | |
236 void BackgroundModeManager::InitStatusTrayIcons() { | |
237 // Only initiali status tray icons for those profiles which actually | |
Miranda Callahan
2011/05/20 15:16:53
nit: initiali...
rpetterson
2011/05/23 03:23:19
Done.
| |
238 // have a background app running. | |
239 for (std::map<Profile*, BackgroundModeInfo>::iterator it = | |
240 background_mode_data_.begin(); | |
241 it != background_mode_data_.end(); | |
242 ++it) { | |
243 if (it->second->applications_->HasBackgroundApp()) { | |
244 CreateStatusTrayIcon(it->first); | |
245 } | |
246 } | |
214 } | 247 } |
215 | 248 |
216 void BackgroundModeManager::OnBackgroundAppUnloaded() { | 249 void BackgroundModeManager::OnBackgroundAppUnloaded() { |
217 // When a background app unloads, decrement our count and also end | 250 // When a background app unloads, decrement our count and also end |
218 // KeepAlive mode if appropriate. | 251 // KeepAlive mode if appropriate. |
219 background_app_count_--; | 252 background_app_count_--; |
220 DCHECK_GE(background_app_count_, 0); | 253 DCHECK_GE(background_app_count_, 0); |
221 if (background_app_count_ == 0) | 254 if (background_app_count_ == 0) |
222 EndBackgroundMode(); | 255 EndBackgroundMode(); |
223 } | 256 } |
224 | 257 |
225 void BackgroundModeManager::EndBackgroundMode() { | 258 void BackgroundModeManager::EndBackgroundMode() { |
226 if (!in_background_mode_) | 259 if (!in_background_mode_) |
227 return; | 260 return; |
228 in_background_mode_ = false; | 261 in_background_mode_ = false; |
229 | 262 |
230 // End KeepAlive mode and blow away our status tray icon. | 263 // End KeepAlive mode and blow away our status tray icon. |
231 BrowserList::EndKeepAlive(); | 264 BrowserList::EndKeepAlive(); |
232 RemoveStatusTrayIcon(); | 265 // There is a status tray icon for each profile. Blow them all away. |
266 for (std::map<Profile*, BackgroundModeInfo>::iterator it = | |
267 background_mode_data_.begin(); | |
268 it != background_mode_data_.end(); | |
269 ++it) { | |
270 RemoveStatusTrayIcon(it->first); | |
271 } | |
233 } | 272 } |
234 | 273 |
235 void BackgroundModeManager::EnableBackgroundMode() { | 274 void BackgroundModeManager::EnableBackgroundMode() { |
236 DCHECK(IsBackgroundModePrefEnabled()); | 275 DCHECK(IsBackgroundModePrefEnabled()); |
237 // If background mode should be enabled, but isn't, turn it on. | 276 // If background mode should be enabled, but isn't, turn it on. |
238 if (background_app_count_ > 0 && !in_background_mode_) { | 277 if (background_app_count_ > 0 && !in_background_mode_) { |
239 StartBackgroundMode(); | 278 StartBackgroundMode(); |
240 EnableLaunchOnStartup(true); | 279 EnableLaunchOnStartup(true); |
241 } | 280 } |
242 } | 281 } |
243 | 282 |
244 void BackgroundModeManager::DisableBackgroundMode() { | 283 void BackgroundModeManager::DisableBackgroundMode() { |
245 DCHECK(!IsBackgroundModePrefEnabled()); | 284 DCHECK(!IsBackgroundModePrefEnabled()); |
246 // If background mode is currently enabled, turn it off. | 285 // If background mode is currently enabled, turn it off. |
247 if (in_background_mode_) { | 286 if (in_background_mode_) { |
248 EndBackgroundMode(); | 287 EndBackgroundMode(); |
249 EnableLaunchOnStartup(false); | 288 EnableLaunchOnStartup(false); |
250 } | 289 } |
251 } | 290 } |
252 | 291 |
253 void BackgroundModeManager::OnBackgroundAppInstalled( | 292 void BackgroundModeManager::OnBackgroundAppInstalled( |
254 const Extension* extension) { | 293 const Extension* extension, Profile* profile) { |
255 // Background mode is disabled - don't do anything. | 294 // Background mode is disabled - don't do anything. |
256 if (!IsBackgroundModePrefEnabled()) | 295 if (!IsBackgroundModePrefEnabled()) |
257 return; | 296 return; |
258 | 297 |
259 // We're installing a background app. If this is the first background app | 298 // We're installing a background app. If this is the first background app |
260 // being installed, make sure we are set to launch on startup. | 299 // being installed, make sure we are set to launch on startup. |
261 if (background_app_count_ == 0) | 300 if (background_app_count_ == 0) |
262 EnableLaunchOnStartup(true); | 301 EnableLaunchOnStartup(true); |
263 | 302 |
264 // Notify the user that a background app has been installed. | 303 // Notify the user that a background app has been installed. |
265 if (extension) // NULL when called by unit tests. | 304 if (extension) // NULL when called by unit tests. |
266 DisplayAppInstalledNotification(extension); | 305 DisplayAppInstalledNotification(extension, profile); |
267 } | 306 } |
268 | 307 |
269 void BackgroundModeManager::OnBackgroundAppUninstalled() { | 308 void BackgroundModeManager::OnBackgroundAppUninstalled() { |
270 // When uninstalling a background app, disable launch on startup if | 309 // When uninstalling a background app, disable launch on startup if |
271 // we have no more background apps. | 310 // we have no more background apps. |
272 if (background_app_count_ == 0) | 311 if (background_app_count_ == 0) |
273 EnableLaunchOnStartup(false); | 312 EnableLaunchOnStartup(false); |
274 } | 313 } |
275 | 314 |
276 void BackgroundModeManager::CreateStatusTrayIcon() { | 315 void BackgroundModeManager::CreateStatusTrayIcon(Profile* profile) { |
277 // Only need status icons on windows/linux. ChromeOS doesn't allow exiting | 316 // Only need status icons on windows/linux. ChromeOS doesn't allow exiting |
278 // Chrome and Mac can use the dock icon instead. | 317 // Chrome and Mac can use the dock icon instead. |
318 | |
319 // All profiles should point to the same status tray so we can just use the | |
320 // first profile to get the status tray. | |
Miranda Callahan
2011/05/20 15:16:53
looks like you changed this to use the g_browser_p
rpetterson
2011/05/23 03:23:19
Done.
| |
279 #if !defined(OS_MACOSX) && !defined(OS_CHROMEOS) | 321 #if !defined(OS_MACOSX) && !defined(OS_CHROMEOS) |
280 if (!status_tray_) | 322 if (!status_tray_) |
281 status_tray_ = profile_->GetStatusTray(); | 323 status_tray_ = g_browser_process->status_tray(); |
282 #endif | 324 #endif |
283 | 325 |
284 // If the platform doesn't support status icons, or we've already created | 326 // If the platform doesn't support status icons, or we've already created |
285 // our status icon, just return. | 327 // our status icon, just return. |
286 if (!status_tray_ || status_icon_) | 328 BackgroundModeInfo bmd = GetBackgroundModeInfo(profile); |
287 return; | 329 if (!status_tray_ || bmd->status_icon_) |
288 status_icon_ = status_tray_->CreateStatusIcon(); | |
289 if (!status_icon_) | |
290 return; | 330 return; |
291 | 331 |
292 // Set the image and add ourselves as a click observer on it | 332 bmd->status_icon_ = status_tray_->CreateStatusIcon(); |
333 if (!bmd->status_icon_) | |
334 return; | |
335 | |
336 // Set the image and add ourselves as a click observer on it. | |
337 // TODO(rlp): Status tray icon should have submenus for each profile. | |
293 SkBitmap* bitmap = ResourceBundle::GetSharedInstance().GetBitmapNamed( | 338 SkBitmap* bitmap = ResourceBundle::GetSharedInstance().GetBitmapNamed( |
294 IDR_STATUS_TRAY_ICON); | 339 IDR_STATUS_TRAY_ICON); |
295 status_icon_->SetImage(*bitmap); | 340 bmd->status_icon_->SetImage(*bitmap); |
296 status_icon_->SetToolTip(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)); | 341 bmd->status_icon_->SetToolTip(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)); |
297 UpdateStatusTrayIconContextMenu(); | 342 UpdateStatusTrayIconContextMenu(profile); |
298 } | 343 } |
299 | 344 |
300 void BackgroundModeManager::UpdateContextMenuEntryIcon( | 345 void BackgroundModeManager::UpdateContextMenuEntryIcon( |
301 const Extension* extension) { | 346 const Extension* extension, Profile* profile) { |
302 if (!context_menu_) | 347 BackgroundModeInfo bmd = GetBackgroundModeInfo(profile); |
348 | |
349 if (!bmd->context_menu_) | |
303 return; | 350 return; |
304 context_menu_->SetIcon( | 351 bmd->context_menu_->SetIcon( |
305 context_menu_application_offset_ + applications_.GetPosition(extension), | 352 bmd->context_menu_application_offset_ + |
306 *(applications_.GetIcon(extension))); | 353 bmd->applications_->GetPosition(extension), |
307 status_icon_->SetContextMenu(context_menu_); // for Update effect | 354 *(bmd->applications_->GetIcon(extension))); |
355 | |
356 bmd->status_icon_->SetContextMenu(bmd->context_menu_); // for Update effect | |
308 } | 357 } |
309 | 358 |
310 void BackgroundModeManager::UpdateStatusTrayIconContextMenu() { | 359 void BackgroundModeManager::UpdateStatusTrayIconContextMenu(Profile* profile) { |
311 if (!status_icon_) | 360 BackgroundModeInfo bmd = GetBackgroundModeInfo(profile); |
361 if (!bmd->status_icon_) | |
312 return; | 362 return; |
313 | 363 |
364 // TODO(rlp): Add current profile color. | |
314 // Create a context menu item for Chrome. | 365 // Create a context menu item for Chrome. |
315 ui::SimpleMenuModel* menu = new ui::SimpleMenuModel(this); | 366 ProfileStatusMenuModel* menu = new ProfileStatusMenuModel(this, profile); |
316 // Add About item | 367 // Add About item |
317 menu->AddItem(IDC_ABOUT, l10n_util::GetStringFUTF16(IDS_ABOUT, | 368 menu->AddItem(IDC_ABOUT, l10n_util::GetStringFUTF16(IDS_ABOUT, |
318 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME))); | 369 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME))); |
319 menu->AddItem(IDC_OPTIONS, GetPreferencesMenuLabel()); | 370 menu->AddItem(IDC_OPTIONS, GetPreferencesMenuLabel()); |
320 menu->AddItemWithStringId(IDC_TASK_MANAGER, IDS_TASK_MANAGER); | 371 menu->AddItemWithStringId(IDC_TASK_MANAGER, IDS_TASK_MANAGER); |
321 menu->AddSeparator(); | 372 menu->AddSeparator(); |
322 int position = 0; | 373 int position = 0; |
323 context_menu_application_offset_ = menu->GetItemCount(); | 374 bmd->context_menu_application_offset_ = menu->GetItemCount(); |
324 for (ExtensionList::const_iterator cursor = applications_.begin(); | 375 for (ExtensionList::const_iterator cursor = bmd->applications_->begin(); |
325 cursor != applications_.end(); | 376 cursor != bmd->applications_->end(); |
326 ++cursor, ++position) { | 377 ++cursor, ++position) { |
327 const SkBitmap* icon = applications_.GetIcon(*cursor); | 378 const SkBitmap* icon = bmd->applications_->GetIcon(*cursor); |
328 DCHECK(position == applications_.GetPosition(*cursor)); | 379 DCHECK(position == bmd->applications_->GetPosition(*cursor)); |
329 const std::string& name = (*cursor)->name(); | 380 const std::string& name = (*cursor)->name(); |
330 menu->AddItem(position, UTF8ToUTF16(name)); | 381 menu->AddItem(position, UTF8ToUTF16(name)); |
331 if (icon) | 382 if (icon) |
332 menu->SetIcon(menu->GetItemCount() - 1, *icon); | 383 menu->SetIcon(menu->GetItemCount() - 1, *icon); |
333 } | 384 } |
334 if (applications_.size() > 0) | 385 if (bmd->applications_->size() > 0) |
335 menu->AddSeparator(); | 386 menu->AddSeparator(); |
336 menu->AddCheckItemWithStringId( | 387 menu->AddCheckItemWithStringId( |
337 IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND, | 388 IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND, |
338 IDS_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND); | 389 IDS_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND); |
339 menu->AddItemWithStringId(IDC_EXIT, IDS_EXIT); | 390 menu->AddItemWithStringId(IDC_EXIT, IDS_EXIT); |
340 context_menu_ = menu; | 391 bmd->context_menu_ = menu; |
341 status_icon_->SetContextMenu(menu); | 392 bmd->status_icon_->SetContextMenu(menu); |
342 } | 393 } |
343 | 394 |
344 bool BackgroundModeManager::IsCommandIdChecked(int command_id) const { | 395 bool BackgroundModeManager::IsCommandIdChecked(int command_id) const { |
345 DCHECK(command_id == IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND); | 396 DCHECK(command_id == IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND); |
346 return true; | 397 return true; |
347 } | 398 } |
348 | 399 |
349 bool BackgroundModeManager::IsCommandIdEnabled(int command_id) const { | 400 bool BackgroundModeManager::IsCommandIdEnabled(int command_id) const { |
350 // For now, we do not support disabled items. | 401 // For now, we do not support disabled items. |
351 return true; | 402 return true; |
352 } | 403 } |
353 | 404 |
354 bool BackgroundModeManager::GetAcceleratorForCommandId( | 405 bool BackgroundModeManager::GetAcceleratorForCommandId( |
355 int command_id, | 406 int command_id, |
356 ui::Accelerator* accelerator) { | 407 ui::Accelerator* accelerator) { |
357 // No accelerators for status icon context menus. | 408 // No accelerators for status icon context menus. |
358 return false; | 409 return false; |
359 } | 410 } |
360 | 411 |
361 void BackgroundModeManager::RemoveStatusTrayIcon() { | 412 void BackgroundModeManager::RemoveStatusTrayIcon(Profile* profile) { |
362 if (status_icon_) | 413 BackgroundModeInfo bmd = GetBackgroundModeInfo(profile); |
363 status_tray_->RemoveStatusIcon(status_icon_); | 414 if (bmd->status_icon_) |
364 status_icon_ = NULL; | 415 status_tray_->RemoveStatusIcon(bmd->status_icon_); |
365 context_menu_ = NULL; // Do not delete, points within |status_icon_|. | 416 bmd->status_icon_ = NULL; |
417 bmd->context_menu_ = NULL; // Do not delete, points within |status_icon_|. | |
366 } | 418 } |
367 | 419 |
368 void BackgroundModeManager::ExecuteApplication(int item) { | 420 void BackgroundModeManager::ExecuteApplication(int item, Profile* profile) { |
369 DCHECK(item >= 0 && item < static_cast<int>(applications_.size())); | 421 BackgroundModeInfo bmd = GetBackgroundModeInfo(profile); |
370 Browser* browser = BrowserList::GetLastActive(); | 422 DCHECK(item >= 0 && item < static_cast<int>(bmd->applications_->size())); |
371 if (!browser) { | 423 |
372 Browser::OpenEmptyWindow(profile_); | 424 Browser* browser = GetBrowserWindow(profile); |
373 browser = BrowserList::GetLastActive(); | 425 const Extension* extension = bmd->applications_->GetExtension(item); |
374 } | 426 browser->OpenApplicationTab(profile, extension, NEW_FOREGROUND_TAB); |
375 const Extension* extension = applications_.GetExtension(item); | |
376 browser->OpenApplicationTab(profile_, extension, NEW_FOREGROUND_TAB); | |
377 } | 427 } |
378 | 428 |
379 void BackgroundModeManager::ExecuteCommand(int item) { | 429 void BackgroundModeManager::ExecuteCommand(int item) { |
430 // We only use the version with profile information. | |
431 NOTREACHED(); | |
432 } | |
433 | |
434 void BackgroundModeManager::ExecuteCommand(int item, Profile* profile) { | |
380 switch (item) { | 435 switch (item) { |
381 case IDC_ABOUT: | 436 case IDC_ABOUT: |
382 GetBrowserWindow()->OpenAboutChromeDialog(); | 437 GetBrowserWindow(profile)->OpenAboutChromeDialog(); |
383 break; | 438 break; |
384 case IDC_EXIT: | 439 case IDC_EXIT: |
385 UserMetrics::RecordAction(UserMetricsAction("Exit")); | 440 UserMetrics::RecordAction(UserMetricsAction("Exit")); |
386 BrowserList::CloseAllBrowsersAndExit(); | 441 BrowserList::CloseAllBrowsersAndExit(); |
387 break; | 442 break; |
388 case IDC_OPTIONS: | 443 case IDC_OPTIONS: |
389 GetBrowserWindow()->OpenOptionsDialog(); | 444 GetBrowserWindow(profile)->OpenOptionsDialog(); |
390 break; | 445 break; |
391 case IDC_TASK_MANAGER: | 446 case IDC_TASK_MANAGER: |
392 GetBrowserWindow()->OpenTaskManager(true); | 447 GetBrowserWindow(profile)->OpenTaskManager(true); |
393 break; | 448 break; |
394 case IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND: { | 449 case IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND: { |
395 // Background mode must already be enabled (as otherwise this menu would | 450 // Background mode must already be enabled (as otherwise this menu would |
396 // not be visible). | 451 // not be visible). |
397 DCHECK(IsBackgroundModePrefEnabled()); | 452 DCHECK(IsBackgroundModePrefEnabled()); |
398 DCHECK(BrowserList::WillKeepAlive()); | 453 DCHECK(BrowserList::WillKeepAlive()); |
399 if (BrowserList::size() == 0) { | 454 if (BrowserList::size() == 0) { |
400 // There are no windows open - unchecking this will exit Chrome. Warn | 455 // There are no windows open - unchecking this will exit Chrome. Warn |
401 // the user. | 456 // the user. |
402 string16 tab_title = l10n_util::GetStringFUTF16(IDS_OPTIONS_TAB_TITLE, | 457 string16 tab_title = l10n_util::GetStringFUTF16(IDS_OPTIONS_TAB_TITLE, |
(...skipping 11 matching lines...) Expand all Loading... | |
414 } | 469 } |
415 | 470 |
416 // Set the background mode pref to "disabled" - the resulting notification | 471 // Set the background mode pref to "disabled" - the resulting notification |
417 // will result in a call to DisableBackgroundMode(). | 472 // will result in a call to DisableBackgroundMode(). |
418 PrefService* service = g_browser_process->local_state(); | 473 PrefService* service = g_browser_process->local_state(); |
419 DCHECK(service); | 474 DCHECK(service); |
420 service->SetBoolean(prefs::kBackgroundModeEnabled, false); | 475 service->SetBoolean(prefs::kBackgroundModeEnabled, false); |
421 break; | 476 break; |
422 } | 477 } |
423 default: | 478 default: |
424 ExecuteApplication(item); | 479 ExecuteApplication(item, profile); |
425 break; | 480 break; |
426 } | 481 } |
427 } | 482 } |
428 | 483 |
429 Browser* BackgroundModeManager::GetBrowserWindow() { | 484 Browser* BackgroundModeManager::GetBrowserWindow(Profile* profile) { |
430 Browser* browser = BrowserList::GetLastActive(); | 485 Browser* browser = BrowserList::GetLastActive(); |
Miranda Callahan
2011/05/20 15:16:53
This should be changed to BrowserList::GetLastActi
rpetterson
2011/05/23 03:23:19
Done.
| |
431 if (!browser) { | 486 if (!browser) { |
432 Browser::OpenEmptyWindow(profile_); | 487 Browser::OpenEmptyWindow(profile); |
433 browser = BrowserList::GetLastActive(); | 488 browser = BrowserList::GetLastActive(); |
Miranda Callahan
2011/05/20 15:16:53
...should probably also be GetLastActiveForProfile
rpetterson
2011/05/23 03:23:19
Done.
| |
434 } | 489 } |
435 return browser; | 490 return browser; |
436 } | 491 } |
437 | 492 |
493 BackgroundModeManager::BackgroundModeInfo | |
494 BackgroundModeManager::GetBackgroundModeInfo( | |
Miranda Callahan
2011/05/20 15:16:53
Join this line and the one below.
rpetterson
2011/05/23 03:23:19
Done.
| |
495 Profile* profile) { | |
496 DCHECK(background_mode_data_.find(profile) != background_mode_data_.end()); | |
497 return background_mode_data_[profile]; | |
498 } | |
499 | |
438 // static | 500 // static |
439 bool BackgroundModeManager::IsBackgroundModePermanentlyDisabled( | 501 bool BackgroundModeManager::IsBackgroundModePermanentlyDisabled( |
440 const CommandLine* command_line) { | 502 const CommandLine* command_line) { |
441 | 503 |
442 // Background mode is disabled if the appropriate flag is passed, or if | 504 // Background mode is disabled if the appropriate flag is passed, or if |
443 // extensions are disabled, or if the associated preference is unset. It's | 505 // extensions are disabled, or if the associated preference is unset. It's |
444 // always disabled on chromeos since chrome is always running on that | 506 // always disabled on chromeos since chrome is always running on that |
445 // platform, making it superfluous. | 507 // platform, making it superfluous. |
446 #if defined(OS_CHROMEOS) | 508 #if defined(OS_CHROMEOS) |
447 return true; | 509 return true; |
448 #else | 510 #else |
449 bool background_mode_disabled = | 511 bool background_mode_disabled = |
450 command_line->HasSwitch(switches::kDisableBackgroundMode) || | 512 command_line->HasSwitch(switches::kDisableBackgroundMode) || |
451 command_line->HasSwitch(switches::kDisableExtensions); | 513 command_line->HasSwitch(switches::kDisableExtensions); |
452 return background_mode_disabled; | 514 return background_mode_disabled; |
453 #endif | 515 #endif |
454 } | 516 } |
455 | 517 |
456 bool BackgroundModeManager::IsBackgroundModePrefEnabled() { | 518 bool BackgroundModeManager::IsBackgroundModePrefEnabled() { |
457 PrefService* service = g_browser_process->local_state(); | 519 PrefService* service = g_browser_process->local_state(); |
458 DCHECK(service); | 520 DCHECK(service); |
459 return service->GetBoolean(prefs::kBackgroundModeEnabled); | 521 return service->GetBoolean(prefs::kBackgroundModeEnabled); |
460 } | 522 } |
461 | 523 |
462 // static | 524 // static |
463 void BackgroundModeManager::RegisterPrefs(PrefService* prefs) { | 525 void BackgroundModeManager::RegisterPrefs(PrefService* prefs) { |
464 prefs->RegisterBooleanPref(prefs::kUserCreatedLoginItem, false); | 526 prefs->RegisterBooleanPref(prefs::kUserCreatedLoginItem, false); |
465 prefs->RegisterBooleanPref(prefs::kBackgroundModeEnabled, true); | 527 prefs->RegisterBooleanPref(prefs::kBackgroundModeEnabled, true); |
466 } | 528 } |
OLD | NEW |