| 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" |
| 11 #include "chrome/app/chrome_command_ids.h" | 11 #include "chrome/app/chrome_command_ids.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/background/background_mode_manager.h" | 13 #include "chrome/browser/background/background_mode_manager.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/status_icons/status_icon.h" | 17 #include "chrome/browser/status_icons/status_icon.h" |
| 18 #include "chrome/browser/status_icons/status_tray.h" | 18 #include "chrome/browser/status_icons/status_tray.h" |
| 19 #include "chrome/browser/ui/browser_list.h" | 19 #include "chrome/browser/ui/browser_list.h" |
| 20 #include "chrome/common/chrome_constants.h" | 20 #include "chrome/common/chrome_constants.h" |
| 21 #include "chrome/common/chrome_notification_types.h" |
| 21 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
| 22 #include "chrome/common/extensions/extension.h" | 23 #include "chrome/common/extensions/extension.h" |
| 23 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
| 24 #include "content/browser/user_metrics.h" | 25 #include "content/browser/user_metrics.h" |
| 26 #include "content/common/content_notification_types.h" |
| 25 #include "content/common/notification_service.h" | 27 #include "content/common/notification_service.h" |
| 26 #include "content/common/notification_type.h" | |
| 27 #include "grit/chromium_strings.h" | 28 #include "grit/chromium_strings.h" |
| 28 #include "grit/generated_resources.h" | 29 #include "grit/generated_resources.h" |
| 29 #include "grit/theme_resources.h" | 30 #include "grit/theme_resources.h" |
| 30 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
| 31 #include "ui/base/resource/resource_bundle.h" | 32 #include "ui/base/resource/resource_bundle.h" |
| 32 | 33 |
| 33 BackgroundModeManager::BackgroundModeData::BackgroundModeData( | 34 BackgroundModeManager::BackgroundModeData::BackgroundModeData( |
| 34 int command_id, | 35 int command_id, |
| 35 Profile* profile, | 36 Profile* profile, |
| 36 BackgroundModeManager* background_mode_manager) | 37 BackgroundModeManager* background_mode_manager) |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 } | 156 } |
| 156 | 157 |
| 157 // If the -keep-alive-for-test flag is passed, then always keep chrome running | 158 // If the -keep-alive-for-test flag is passed, then always keep chrome running |
| 158 // in the background until the user explicitly terminates it, by acting as if | 159 // in the background until the user explicitly terminates it, by acting as if |
| 159 // we loaded a background app. | 160 // we loaded a background app. |
| 160 if (command_line->HasSwitch(switches::kKeepAliveForTest)) | 161 if (command_line->HasSwitch(switches::kKeepAliveForTest)) |
| 161 OnBackgroundAppLoaded(); | 162 OnBackgroundAppLoaded(); |
| 162 | 163 |
| 163 // Listen for the application shutting down so we can decrement our KeepAlive | 164 // Listen for the application shutting down so we can decrement our KeepAlive |
| 164 // count. | 165 // count. |
| 165 registrar_.Add(this, NotificationType::APP_TERMINATING, | 166 registrar_.Add(this, content::NOTIFICATION_APP_TERMINATING, |
| 166 NotificationService::AllSources()); | 167 NotificationService::AllSources()); |
| 167 } | 168 } |
| 168 | 169 |
| 169 BackgroundModeManager::~BackgroundModeManager() { | 170 BackgroundModeManager::~BackgroundModeManager() { |
| 170 for (std::map<Profile*, BackgroundModeInfo>::iterator it = | 171 for (std::map<Profile*, BackgroundModeInfo>::iterator it = |
| 171 background_mode_data_.begin(); | 172 background_mode_data_.begin(); |
| 172 it != background_mode_data_.end(); | 173 it != background_mode_data_.end(); |
| 173 ++it) { | 174 ++it) { |
| 174 it->second->applications_->RemoveObserver(this); | 175 it->second->applications_->RemoveObserver(this); |
| 175 } | 176 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 191 void BackgroundModeManager::RegisterProfile(Profile* profile) { | 192 void BackgroundModeManager::RegisterProfile(Profile* profile) { |
| 192 // We don't want to register multiple times for one profile. | 193 // We don't want to register multiple times for one profile. |
| 193 DCHECK(background_mode_data_.find(profile) == background_mode_data_.end()); | 194 DCHECK(background_mode_data_.find(profile) == background_mode_data_.end()); |
| 194 BackgroundModeInfo bmd(new BackgroundModeData(current_command_id_++, | 195 BackgroundModeInfo bmd(new BackgroundModeData(current_command_id_++, |
| 195 profile, this)); | 196 profile, this)); |
| 196 background_mode_data_[profile] = bmd; | 197 background_mode_data_[profile] = bmd; |
| 197 | 198 |
| 198 // Listen for when extensions are loaded/unloaded so we can track the | 199 // Listen for when extensions are loaded/unloaded so we can track the |
| 199 // number of background apps and modify our keep-alive and launch-on-startup | 200 // number of background apps and modify our keep-alive and launch-on-startup |
| 200 // state appropriately. | 201 // state appropriately. |
| 201 registrar_.Add(this, NotificationType::EXTENSION_LOADED, | 202 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 202 Source<Profile>(profile)); | 203 Source<Profile>(profile)); |
| 203 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, | 204 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 204 Source<Profile>(profile)); | 205 Source<Profile>(profile)); |
| 205 | 206 |
| 206 // Check for the presence of background apps after all extensions have been | 207 // Check for the presence of background apps after all extensions have been |
| 207 // loaded, to handle the case where an extension has been manually removed | 208 // loaded, to handle the case where an extension has been manually removed |
| 208 // while Chrome was not running. | 209 // while Chrome was not running. |
| 209 registrar_.Add(this, NotificationType::EXTENSIONS_READY, | 210 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, |
| 210 Source<Profile>(profile)); | 211 Source<Profile>(profile)); |
| 211 | 212 |
| 212 bmd->applications_->AddObserver(this); | 213 bmd->applications_->AddObserver(this); |
| 213 | 214 |
| 214 // If we're adding a new profile and running in multi-profile mode, this new | 215 // If we're adding a new profile and running in multi-profile mode, this new |
| 215 // profile should be added to the status icon if one currently exists. | 216 // profile should be added to the status icon if one currently exists. |
| 216 if (in_background_mode_ && status_icon_) | 217 if (in_background_mode_ && status_icon_) |
| 217 UpdateStatusTrayIconContextMenu(); | 218 UpdateStatusTrayIconContextMenu(); |
| 218 } | 219 } |
| 219 | 220 |
| 220 /////////////////////////////////////////////////////////////////////////////// | 221 /////////////////////////////////////////////////////////////////////////////// |
| 221 // BackgroundModeManager, NotificationObserver overrides | 222 // BackgroundModeManager, NotificationObserver overrides |
| 222 void BackgroundModeManager::Observe(NotificationType type, | 223 void BackgroundModeManager::Observe(int type, |
| 223 const NotificationSource& source, | 224 const NotificationSource& source, |
| 224 const NotificationDetails& details) { | 225 const NotificationDetails& details) { |
| 225 switch (type.value) { | 226 switch (type) { |
| 226 case NotificationType::PREF_CHANGED: | 227 case chrome::NOTIFICATION_PREF_CHANGED: |
| 227 DCHECK(*Details<std::string>(details).ptr() == | 228 DCHECK(*Details<std::string>(details).ptr() == |
| 228 prefs::kBackgroundModeEnabled); | 229 prefs::kBackgroundModeEnabled); |
| 229 if (IsBackgroundModePrefEnabled()) | 230 if (IsBackgroundModePrefEnabled()) |
| 230 EnableBackgroundMode(); | 231 EnableBackgroundMode(); |
| 231 else | 232 else |
| 232 DisableBackgroundMode(); | 233 DisableBackgroundMode(); |
| 233 break; | 234 break; |
| 234 case NotificationType::EXTENSIONS_READY: | 235 case chrome::NOTIFICATION_EXTENSIONS_READY: |
| 235 // Extensions are loaded, so we don't need to manually keep the browser | 236 // Extensions are loaded, so we don't need to manually keep the browser |
| 236 // process alive any more when running in no-startup-window mode. | 237 // process alive any more when running in no-startup-window mode. |
| 237 EndKeepAliveForStartup(); | 238 EndKeepAliveForStartup(); |
| 238 | 239 |
| 239 // On a Mac, we use 'login items' mechanism which has user-facing UI so we | 240 // On a Mac, we use 'login items' mechanism which has user-facing UI so we |
| 240 // don't want to stomp on user choice every time we start and load | 241 // don't want to stomp on user choice every time we start and load |
| 241 // registered extensions. This means that if a background app is removed | 242 // registered extensions. This means that if a background app is removed |
| 242 // or added while Chrome is not running, we could leave Chrome in the | 243 // or added while Chrome is not running, we could leave Chrome in the |
| 243 // wrong state, but this is better than constantly forcing Chrome to | 244 // wrong state, but this is better than constantly forcing Chrome to |
| 244 // launch on startup even after the user removes the LoginItem manually. | 245 // launch on startup even after the user removes the LoginItem manually. |
| 245 #if !defined(OS_MACOSX) | 246 #if !defined(OS_MACOSX) |
| 246 EnableLaunchOnStartup(background_app_count_ > 0); | 247 EnableLaunchOnStartup(background_app_count_ > 0); |
| 247 #endif | 248 #endif |
| 248 break; | 249 break; |
| 249 case NotificationType::EXTENSION_LOADED: { | 250 case chrome::NOTIFICATION_EXTENSION_LOADED: { |
| 250 Extension* extension = Details<Extension>(details).ptr(); | 251 Extension* extension = Details<Extension>(details).ptr(); |
| 251 if (BackgroundApplicationListModel::IsBackgroundApp(*extension)) { | 252 if (BackgroundApplicationListModel::IsBackgroundApp(*extension)) { |
| 252 // Extensions loaded after the ExtensionsService is ready should be | 253 // Extensions loaded after the ExtensionsService is ready should be |
| 253 // treated as new installs. | 254 // treated as new installs. |
| 254 Profile* profile = Source<Profile>(source).ptr(); | 255 Profile* profile = Source<Profile>(source).ptr(); |
| 255 if (profile->GetExtensionService()->is_ready()) | 256 if (profile->GetExtensionService()->is_ready()) |
| 256 OnBackgroundAppInstalled(extension); | 257 OnBackgroundAppInstalled(extension); |
| 257 OnBackgroundAppLoaded(); | 258 OnBackgroundAppLoaded(); |
| 258 } | 259 } |
| 259 } | 260 } |
| 260 break; | 261 break; |
| 261 case NotificationType::EXTENSION_UNLOADED: | 262 case chrome::NOTIFICATION_EXTENSION_UNLOADED: |
| 262 if (BackgroundApplicationListModel::IsBackgroundApp( | 263 if (BackgroundApplicationListModel::IsBackgroundApp( |
| 263 *Details<UnloadedExtensionInfo>(details)->extension)) { | 264 *Details<UnloadedExtensionInfo>(details)->extension)) { |
| 264 Details<UnloadedExtensionInfo> info = | 265 Details<UnloadedExtensionInfo> info = |
| 265 Details<UnloadedExtensionInfo>(details); | 266 Details<UnloadedExtensionInfo>(details); |
| 266 // If we already got an unload notification when it was disabled, ignore | 267 // If we already got an unload notification when it was disabled, ignore |
| 267 // this one. | 268 // this one. |
| 268 // TODO(atwilson): Change BackgroundModeManager to use | 269 // TODO(atwilson): Change BackgroundModeManager to use |
| 269 // BackgroundApplicationListModel instead of tracking the count here. | 270 // BackgroundApplicationListModel instead of tracking the count here. |
| 270 if (info->already_disabled) | 271 if (info->already_disabled) |
| 271 return; | 272 return; |
| 272 OnBackgroundAppUnloaded(); | 273 OnBackgroundAppUnloaded(); |
| 273 OnBackgroundAppUninstalled(); | 274 OnBackgroundAppUninstalled(); |
| 274 } | 275 } |
| 275 break; | 276 break; |
| 276 case NotificationType::APP_TERMINATING: | 277 case content::NOTIFICATION_APP_TERMINATING: |
| 277 // Make sure we aren't still keeping the app alive (only happens if we | 278 // Make sure we aren't still keeping the app alive (only happens if we |
| 278 // don't receive an EXTENSIONS_READY notification for some reason). | 279 // don't receive an EXTENSIONS_READY notification for some reason). |
| 279 EndKeepAliveForStartup(); | 280 EndKeepAliveForStartup(); |
| 280 // Performing an explicit shutdown, so exit background mode (does nothing | 281 // Performing an explicit shutdown, so exit background mode (does nothing |
| 281 // if we aren't in background mode currently). | 282 // if we aren't in background mode currently). |
| 282 EndBackgroundMode(); | 283 EndBackgroundMode(); |
| 283 // Shutting down, so don't listen for any more notifications so we don't | 284 // Shutting down, so don't listen for any more notifications so we don't |
| 284 // try to re-enter/exit background mode again. | 285 // try to re-enter/exit background mode again. |
| 285 registrar_.RemoveAll(); | 286 registrar_.RemoveAll(); |
| 286 break; | 287 break; |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 589 command_line->HasSwitch(switches::kDisableExtensions); | 590 command_line->HasSwitch(switches::kDisableExtensions); |
| 590 return background_mode_disabled; | 591 return background_mode_disabled; |
| 591 #endif | 592 #endif |
| 592 } | 593 } |
| 593 | 594 |
| 594 bool BackgroundModeManager::IsBackgroundModePrefEnabled() { | 595 bool BackgroundModeManager::IsBackgroundModePrefEnabled() { |
| 595 PrefService* service = g_browser_process->local_state(); | 596 PrefService* service = g_browser_process->local_state(); |
| 596 DCHECK(service); | 597 DCHECK(service); |
| 597 return service->GetBoolean(prefs::kBackgroundModeEnabled); | 598 return service->GetBoolean(prefs::kBackgroundModeEnabled); |
| 598 } | 599 } |
| OLD | NEW |