| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 | 229 |
| 230 | 230 |
| 231 void BackgroundModeManager::RegisterProfile(Profile* profile) { | 231 void BackgroundModeManager::RegisterProfile(Profile* profile) { |
| 232 // We don't want to register multiple times for one profile. | 232 // We don't want to register multiple times for one profile. |
| 233 DCHECK(background_mode_data_.find(profile) == background_mode_data_.end()); | 233 DCHECK(background_mode_data_.find(profile) == background_mode_data_.end()); |
| 234 BackgroundModeInfo bmd(new BackgroundModeData(current_command_id_++, | 234 BackgroundModeInfo bmd(new BackgroundModeData(current_command_id_++, |
| 235 profile)); | 235 profile)); |
| 236 background_mode_data_[profile] = bmd; | 236 background_mode_data_[profile] = bmd; |
| 237 | 237 |
| 238 // Initially set the name for this background mode data. | 238 // Initially set the name for this background mode data. |
| 239 size_t index = profile_cache_->GetIndexOfProfileWithPath(profile->GetPath()); | |
| 240 string16 name = l10n_util::GetStringUTF16(IDS_PROFILES_DEFAULT_NAME); | 239 string16 name = l10n_util::GetStringUTF16(IDS_PROFILES_DEFAULT_NAME); |
| 241 if (index != std::string::npos) | 240 ProfileInfoEntry entry; |
| 242 name = profile_cache_->GetNameOfProfileAtIndex(index); | 241 if (profile_cache_->GetInfoForProfile(profile->GetPath(), &entry)) |
| 242 name = entry.GetDisplayName(); |
| 243 bmd->SetName(name); | 243 bmd->SetName(name); |
| 244 | 244 |
| 245 // Listen for when extensions are loaded or add the background permission so | 245 // Listen for when extensions are loaded or add the background permission so |
| 246 // we can display a "background app installed" notification and enter | 246 // we can display a "background app installed" notification and enter |
| 247 // "launch on login" mode on the Mac. | 247 // "launch on login" mode on the Mac. |
| 248 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 248 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 249 content::Source<Profile>(profile)); | 249 content::Source<Profile>(profile)); |
| 250 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED, | 250 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED, |
| 251 content::Source<Profile>(profile)); | 251 content::Source<Profile>(profile)); |
| 252 | 252 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 const Extension* extension, Profile* profile) { | 359 const Extension* extension, Profile* profile) { |
| 360 UpdateStatusTrayIconContextMenu(); | 360 UpdateStatusTrayIconContextMenu(); |
| 361 } | 361 } |
| 362 | 362 |
| 363 void BackgroundModeManager::OnApplicationListChanged(Profile* profile) { | 363 void BackgroundModeManager::OnApplicationListChanged(Profile* profile) { |
| 364 if (!IsBackgroundModePrefEnabled()) | 364 if (!IsBackgroundModePrefEnabled()) |
| 365 return; | 365 return; |
| 366 | 366 |
| 367 // Update the profile cache with the fact whether background apps are running | 367 // Update the profile cache with the fact whether background apps are running |
| 368 // for this profile. | 368 // for this profile. |
| 369 size_t profile_index = profile_cache_->GetIndexOfProfileWithPath( | 369 ProfileInfoEntry entry; |
| 370 profile->GetPath()); | 370 if (profile_cache_->GetInfoForProfile(profile->GetPath(), &entry)) { |
| 371 if (profile_index != std::string::npos) { | 371 entry.set_is_running_background_apps( |
| 372 profile_cache_->SetBackgroundStatusOfProfileAtIndex( | 372 GetBackgroundAppCountForProfile(profile) != 0); |
| 373 profile_index, GetBackgroundAppCountForProfile(profile) != 0); | 373 profile_cache_->SetInfoForProfile(entry); |
| 374 } | 374 } |
| 375 | 375 |
| 376 if (!ShouldBeInBackgroundMode()) { | 376 if (!ShouldBeInBackgroundMode()) { |
| 377 // We've uninstalled our last background app, make sure we exit background | 377 // We've uninstalled our last background app, make sure we exit background |
| 378 // mode and no longer launch on startup. | 378 // mode and no longer launch on startup. |
| 379 EnableLaunchOnStartup(false); | 379 EnableLaunchOnStartup(false); |
| 380 EndBackgroundMode(); | 380 EndBackgroundMode(); |
| 381 } else { | 381 } else { |
| 382 // We have at least one background app running - make sure we're in | 382 // We have at least one background app running - make sure we're in |
| 383 // background mode. | 383 // background mode. |
| 384 if (!in_background_mode_) { | 384 if (!in_background_mode_) { |
| 385 // We're entering background mode - make sure we have launch-on-startup | 385 // We're entering background mode - make sure we have launch-on-startup |
| 386 // enabled. On Mac, the platform-specific code tracks whether the user | 386 // enabled. On Mac, the platform-specific code tracks whether the user |
| 387 // has deleted a login item in the past, and if so, no login item will | 387 // has deleted a login item in the past, and if so, no login item will |
| 388 // be created (to avoid overriding the specific user action). | 388 // be created (to avoid overriding the specific user action). |
| 389 EnableLaunchOnStartup(true); | 389 EnableLaunchOnStartup(true); |
| 390 | 390 |
| 391 StartBackgroundMode(); | 391 StartBackgroundMode(); |
| 392 } | 392 } |
| 393 // List of applications changed so update the UI. | 393 // List of applications changed so update the UI. |
| 394 UpdateStatusTrayIconContextMenu(); | 394 UpdateStatusTrayIconContextMenu(); |
| 395 } | 395 } |
| 396 } | 396 } |
| 397 | 397 |
| 398 /////////////////////////////////////////////////////////////////////////////// | 398 /////////////////////////////////////////////////////////////////////////////// |
| 399 // BackgroundModeManager, ProfileInfoCacheObserver overrides | 399 // BackgroundModeManager, ProfileInfoCacheObserver overrides |
| 400 void BackgroundModeManager::OnProfileAdded(const base::FilePath& profile_path) { | 400 void BackgroundModeManager::OnProfileAdded(const base::FilePath& profile_path) { |
| 401 ProfileInfoCache& cache = | 401 ProfileInfoCache& cache = |
| 402 g_browser_process->profile_manager()->GetProfileInfoCache(); | 402 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 403 string16 profile_name = cache.GetNameOfProfileAtIndex( | 403 |
| 404 cache.GetIndexOfProfileWithPath(profile_path)); | 404 ProfileInfoEntry entry; |
| 405 if (!cache.GetInfoForProfile(profile_path, &entry)) |
| 406 return; |
| 407 |
| 408 string16 profile_name = entry.GetDisplayName(); |
| 405 // At this point, the profile should be registered with the background mode | 409 // At this point, the profile should be registered with the background mode |
| 406 // manager, but when it's actually added to the cache is when its name is | 410 // manager, but when it's actually added to the cache is when its name is |
| 407 // set so we need up to update that with the background_mode_data. | 411 // set so we need up to update that with the background_mode_data. |
| 408 for (BackgroundModeInfoMap::const_iterator it = | 412 for (BackgroundModeInfoMap::const_iterator it = |
| 409 background_mode_data_.begin(); | 413 background_mode_data_.begin(); |
| 410 it != background_mode_data_.end(); | 414 it != background_mode_data_.end(); |
| 411 ++it) { | 415 ++it) { |
| 412 if (it->first->GetPath() == profile_path) { | 416 if (it->first->GetPath() == profile_path) { |
| 413 it->second->SetName(profile_name); | 417 it->second->SetName(profile_name); |
| 414 UpdateStatusTrayIconContextMenu(); | 418 UpdateStatusTrayIconContextMenu(); |
| 415 return; | 419 return; |
| 416 } | 420 } |
| 417 } | 421 } |
| 418 } | 422 } |
| 419 | 423 |
| 420 void BackgroundModeManager::OnProfileWillBeRemoved( | 424 void BackgroundModeManager::OnProfileWillBeRemoved( |
| 421 const base::FilePath& profile_path) { | 425 const base::FilePath& profile_path) { |
| 422 ProfileInfoCache& cache = | 426 ProfileInfoCache& cache = |
| 423 g_browser_process->profile_manager()->GetProfileInfoCache(); | 427 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 424 string16 profile_name = cache.GetNameOfProfileAtIndex( | 428 |
| 425 cache.GetIndexOfProfileWithPath(profile_path)); | 429 ProfileInfoEntry entry; |
| 430 if (!cache.GetInfoForProfile(profile_path, &entry)) |
| 431 return; |
| 432 |
| 433 string16 profile_name = entry.GetDisplayName(); |
| 426 // Remove the profile from our map of profiles. | 434 // Remove the profile from our map of profiles. |
| 427 BackgroundModeInfoMap::iterator it = | 435 BackgroundModeInfoMap::iterator it = |
| 428 GetBackgroundModeIterator(profile_name); | 436 GetBackgroundModeIterator(profile_name); |
| 429 // If a profile isn't running a background app, it may not be in the map. | 437 // If a profile isn't running a background app, it may not be in the map. |
| 430 if (it != background_mode_data_.end()) { | 438 if (it != background_mode_data_.end()) { |
| 431 background_mode_data_.erase(it); | 439 background_mode_data_.erase(it); |
| 432 UpdateStatusTrayIconContextMenu(); | 440 UpdateStatusTrayIconContextMenu(); |
| 433 } | 441 } |
| 434 } | 442 } |
| 435 | 443 |
| 436 void BackgroundModeManager::OnProfileNameChanged( | 444 void BackgroundModeManager::OnProfileNameChanged( |
| 437 const base::FilePath& profile_path, | 445 const base::FilePath& profile_path, |
| 438 const string16& old_profile_name) { | 446 const string16& old_profile_name) { |
| 439 ProfileInfoCache& cache = | 447 ProfileInfoCache& cache = |
| 440 g_browser_process->profile_manager()->GetProfileInfoCache(); | 448 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 441 string16 new_profile_name = cache.GetNameOfProfileAtIndex( | 449 ProfileInfoEntry entry; |
| 442 cache.GetIndexOfProfileWithPath(profile_path)); | 450 if (!cache.GetInfoForProfile(profile_path, &entry)) |
| 451 return; |
| 452 |
| 453 string16 new_profile_name = entry.GetDisplayName(); |
| 443 BackgroundModeInfoMap::const_iterator it = | 454 BackgroundModeInfoMap::const_iterator it = |
| 444 GetBackgroundModeIterator(old_profile_name); | 455 GetBackgroundModeIterator(old_profile_name); |
| 445 // We check that the returned iterator is valid due to unittests, but really | 456 // We check that the returned iterator is valid due to unittests, but really |
| 446 // this should only be called on profiles already known by the background | 457 // this should only be called on profiles already known by the background |
| 447 // mode manager. | 458 // mode manager. |
| 448 if (it != background_mode_data_.end()) { | 459 if (it != background_mode_data_.end()) { |
| 449 it->second->SetName(new_profile_name); | 460 it->second->SetName(new_profile_name); |
| 450 UpdateStatusTrayIconContextMenu(); | 461 UpdateStatusTrayIconContextMenu(); |
| 451 } | 462 } |
| 452 } | 463 } |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 775 } | 786 } |
| 776 } | 787 } |
| 777 return profile_it; | 788 return profile_it; |
| 778 } | 789 } |
| 779 | 790 |
| 780 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { | 791 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { |
| 781 PrefService* service = g_browser_process->local_state(); | 792 PrefService* service = g_browser_process->local_state(); |
| 782 DCHECK(service); | 793 DCHECK(service); |
| 783 return service->GetBoolean(prefs::kBackgroundModeEnabled); | 794 return service->GetBoolean(prefs::kBackgroundModeEnabled); |
| 784 } | 795 } |
| OLD | NEW |