This source file includes following definitions.
- command_id_extension_vector_
- ExecuteCommand
- GetBrowserWindow
- GetBackgroundAppCount
- BuildProfileMenu
- SetName
- name
- BackgroundModeDataCompare
- keeping_alive_
- RegisterPrefs
- RegisterProfile
- LaunchBackgroundApplication
- IsBackgroundModeActive
- NumberOfBackgroundModeData
- Observe
- OnBackgroundModeEnabledPrefChanged
- OnApplicationDataChanged
- OnApplicationListChanged
- OnProfileAdded
- OnProfileWillBeRemoved
- OnProfileNameChanged
- ExecuteCommand
- DecrementKeepAliveCountForStartup
- StartBackgroundMode
- EndBackgroundMode
- EnableBackgroundMode
- DisableBackgroundMode
- SuspendBackgroundMode
- ResumeBackgroundMode
- UpdateKeepAliveAndTrayIcon
- OnBrowserAdded
- GetBackgroundAppCount
- GetBackgroundAppCountForProfile
- ShouldBeInBackgroundMode
- OnBackgroundAppInstalled
- CheckReloadStatus
- CreateStatusTrayIcon
- UpdateStatusTrayIconContextMenu
- RemoveStatusTrayIcon
- GetBackgroundModeData
- GetBackgroundModeIterator
- IsBackgroundModePrefEnabled
#include <algorithm>
#include <string>
#include <vector>
#include "base/base_paths.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/background/background_application_list_model.h"
#include "chrome/browser/background/background_mode_manager.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_shutdown.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_info_cache.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/status_icons/status_icon.h"
#include "chrome/browser/status_icons/status_tray.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/extensions/application_launch.h"
#include "chrome/browser/ui/host_desktop.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/manifest_url_handler.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/user_metrics.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/extension.h"
#include "extensions/common/permissions/permission_set.h"
#include "grit/chrome_unscaled_resources.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
using base::UserMetricsAction;
using extensions::Extension;
using extensions::UpdatedExtensionPermissionsInfo;
namespace {
const int kInvalidExtensionIndex = -1;
}
BackgroundModeManager::BackgroundModeData::BackgroundModeData(
    Profile* profile,
    CommandIdExtensionVector* command_id_extension_vector)
    : applications_(new BackgroundApplicationListModel(profile)),
      profile_(profile),
      command_id_extension_vector_(command_id_extension_vector) {
}
BackgroundModeManager::BackgroundModeData::~BackgroundModeData() {
}
void BackgroundModeManager::BackgroundModeData::ExecuteCommand(
    int command_id,
    int event_flags) {
  switch (command_id) {
    case IDC_MinimumLabelValue:
      
      break;
    default:
      
      int extension_index = command_id_extension_vector_->at(command_id);
      if (extension_index != kInvalidExtensionIndex) {
        const Extension* extension =
            applications_->GetExtension(extension_index);
        BackgroundModeManager::LaunchBackgroundApplication(profile_, extension);
      }
      break;
  }
}
Browser* BackgroundModeManager::BackgroundModeData::GetBrowserWindow() {
  chrome::HostDesktopType host_desktop_type = chrome::GetActiveDesktop();
  Browser* browser = chrome::FindLastActiveWithProfile(profile_,
                                                       host_desktop_type);
  return browser ? browser : chrome::OpenEmptyWindow(profile_,
                                                     host_desktop_type);
}
int BackgroundModeManager::BackgroundModeData::GetBackgroundAppCount() const {
  return applications_->size();
}
void BackgroundModeManager::BackgroundModeData::BuildProfileMenu(
    StatusIconMenuModel* menu,
    StatusIconMenuModel* containing_menu) {
  int position = 0;
  
  
  if (applications_->size() < 1) {
    menu->AddItemWithStringId(IDC_MinimumLabelValue,
                              IDS_BACKGROUND_APP_NOT_INSTALLED);
    menu->SetCommandIdEnabled(IDC_MinimumLabelValue, false);
  } else {
    for (extensions::ExtensionList::const_iterator cursor =
             applications_->begin();
         cursor != applications_->end();
         ++cursor, ++position) {
      const gfx::ImageSkia* icon = applications_->GetIcon(cursor->get());
      DCHECK(position == applications_->GetPosition(cursor->get()));
      const std::string& name = (*cursor)->name();
      int command_id = command_id_extension_vector_->size();
      
      DCHECK(command_id < IDC_MinimumLabelValue);
      command_id_extension_vector_->push_back(position);
      menu->AddItem(command_id, base::UTF8ToUTF16(name));
      if (icon)
        menu->SetIcon(menu->GetItemCount() - 1, gfx::Image(*icon));
      
      
      
      
      
      
      
      
      
      if ((*cursor)->location() == extensions::Manifest::COMPONENT) {
        GURL options_page = extensions::ManifestURL::GetOptionsPage(*cursor);
        if (!options_page.is_valid())
          menu->SetCommandIdEnabled(command_id, false);
      }
    }
  }
  if (containing_menu) {
    int menu_command_id = command_id_extension_vector_->size();
    
    DCHECK(menu_command_id < IDC_MinimumLabelValue);
    command_id_extension_vector_->push_back(kInvalidExtensionIndex);
    containing_menu->AddSubMenu(menu_command_id, name_, menu);
  }
}
void BackgroundModeManager::BackgroundModeData::SetName(
    const base::string16& new_profile_name) {
  name_ = new_profile_name;
}
base::string16 BackgroundModeManager::BackgroundModeData::name() {
  return name_;
}
bool BackgroundModeManager::BackgroundModeData::BackgroundModeDataCompare(
    const BackgroundModeData* bmd1,
    const BackgroundModeData* bmd2) {
  return bmd1->name_ < bmd2->name_;
}
BackgroundModeManager::BackgroundModeManager(
    CommandLine* command_line,
    ProfileInfoCache* profile_cache)
    : profile_cache_(profile_cache),
      status_tray_(NULL),
      status_icon_(NULL),
      context_menu_(NULL),
      in_background_mode_(false),
      keep_alive_for_startup_(false),
      keep_alive_for_test_(false),
      background_mode_suspended_(false),
      keeping_alive_(false) {
  
  
  CHECK(g_browser_process != NULL);
  CHECK(!browser_shutdown::IsTryingToQuit());
  
  
  profile_cache_->AddObserver(this);
  
  if (g_browser_process->local_state()) {  
    pref_registrar_.Init(g_browser_process->local_state());
    pref_registrar_.Add(
        prefs::kBackgroundModeEnabled,
        base::Bind(&BackgroundModeManager::OnBackgroundModeEnabledPrefChanged,
                   base::Unretained(this)));
  }
  
  
  
  
  if (command_line->HasSwitch(switches::kNoStartupWindow)) {
    keep_alive_for_startup_ = true;
    chrome::IncrementKeepAliveCount();
  } else {
    
    
    
    SuspendBackgroundMode();
  }
  
  
  if (command_line->HasSwitch(switches::kKeepAliveForTest))
    keep_alive_for_test_ = true;
  if (ShouldBeInBackgroundMode())
    StartBackgroundMode();
  
  
  registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
                 content::NotificationService::AllSources());
  BrowserList::AddObserver(this);
}
BackgroundModeManager::~BackgroundModeManager() {
  
  
  for (BackgroundModeInfoMap::iterator it =
       background_mode_data_.begin();
       it != background_mode_data_.end();
       ++it) {
    it->second->applications_->RemoveObserver(this);
  }
  BrowserList::RemoveObserver(this);
  
  
  
  
  EndBackgroundMode();
}
void BackgroundModeManager::RegisterPrefs(PrefRegistrySimple* registry) {
#if defined(OS_MACOSX)
  registry->RegisterBooleanPref(prefs::kUserRemovedLoginItem, false);
  registry->RegisterBooleanPref(prefs::kChromeCreatedLoginItem, false);
  registry->RegisterBooleanPref(prefs::kMigratedLoginItemPref, false);
#endif
  registry->RegisterBooleanPref(prefs::kBackgroundModeEnabled, true);
}
void BackgroundModeManager::RegisterProfile(Profile* profile) {
  
  DCHECK(background_mode_data_.find(profile) == background_mode_data_.end());
  BackgroundModeInfo bmd(new BackgroundModeData(profile,
                                                &command_id_extension_vector_));
  background_mode_data_[profile] = bmd;
  
  size_t index = profile_cache_->GetIndexOfProfileWithPath(profile->GetPath());
  base::string16 name = l10n_util::GetStringUTF16(IDS_PROFILES_DEFAULT_NAME);
  if (index != std::string::npos)
    name = profile_cache_->GetNameOfProfileAtIndex(index);
  bmd->SetName(name);
  
  
  
  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
                 content::Source<Profile>(profile));
  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED,
                 content::Source<Profile>(profile));
  
  
  
  registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
                 content::Source<Profile>(profile));
  bmd->applications_->AddObserver(this);
  
  
  if (in_background_mode_ && status_icon_)
    UpdateStatusTrayIconContextMenu();
}
void BackgroundModeManager::LaunchBackgroundApplication(
    Profile* profile,
    const Extension* extension) {
  OpenApplication(AppLaunchParams(profile, extension, NEW_FOREGROUND_TAB));
}
bool BackgroundModeManager::IsBackgroundModeActive() {
  return in_background_mode_;
}
int BackgroundModeManager::NumberOfBackgroundModeData() {
  return background_mode_data_.size();
}
void BackgroundModeManager::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  switch (type) {
    case chrome::NOTIFICATION_EXTENSIONS_READY:
      
      
      DecrementKeepAliveCountForStartup();
      break;
    case chrome::NOTIFICATION_EXTENSION_LOADED: {
        Extension* extension = content::Details<Extension>(details).ptr();
        Profile* profile = content::Source<Profile>(source).ptr();
        if (BackgroundApplicationListModel::IsBackgroundApp(
                *extension, profile)) {
          
          
          if (extensions::ExtensionSystem::Get(profile)->extension_service()->
                  is_ready()) {
            bool is_being_reloaded = false;
            CheckReloadStatus(extension, &is_being_reloaded);
            
            
            if (!is_being_reloaded)
              OnBackgroundAppInstalled(extension);
          }
        }
      }
      break;
    case chrome::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED: {
        UpdatedExtensionPermissionsInfo* info =
            content::Details<UpdatedExtensionPermissionsInfo>(details).ptr();
        if (info->permissions->HasAPIPermission(
                extensions::APIPermission::kBackground) &&
            info->reason == UpdatedExtensionPermissionsInfo::ADDED) {
          
          OnBackgroundAppInstalled(info->extension);
        }
      }
      break;
    case chrome::NOTIFICATION_APP_TERMINATING:
      
      
      DecrementKeepAliveCountForStartup();
      
      
      EndBackgroundMode();
      
      
      registrar_.RemoveAll();
      for (BackgroundModeInfoMap::iterator it =
               background_mode_data_.begin();
           it != background_mode_data_.end();
           ++it) {
        it->second->applications_->RemoveObserver(this);
      }
      break;
    default:
      NOTREACHED();
      break;
  }
}
void BackgroundModeManager::OnBackgroundModeEnabledPrefChanged() {
  if (IsBackgroundModePrefEnabled())
    EnableBackgroundMode();
  else
    DisableBackgroundMode();
}
void BackgroundModeManager::OnApplicationDataChanged(
    const Extension* extension, Profile* profile) {
  UpdateStatusTrayIconContextMenu();
}
void BackgroundModeManager::OnApplicationListChanged(Profile* profile) {
  if (!IsBackgroundModePrefEnabled())
    return;
  
  
  size_t profile_index = profile_cache_->GetIndexOfProfileWithPath(
      profile->GetPath());
  if (profile_index != std::string::npos) {
    profile_cache_->SetBackgroundStatusOfProfileAtIndex(
        profile_index, GetBackgroundAppCountForProfile(profile) != 0);
  }
  if (!ShouldBeInBackgroundMode()) {
    
    
    EnableLaunchOnStartup(false);
    EndBackgroundMode();
  } else {
    
    
    if (!in_background_mode_) {
      
      
      
      
      EnableLaunchOnStartup(true);
      StartBackgroundMode();
    }
    
    UpdateStatusTrayIconContextMenu();
  }
}
void BackgroundModeManager::OnProfileAdded(const base::FilePath& profile_path) {
  ProfileInfoCache& cache =
      g_browser_process->profile_manager()->GetProfileInfoCache();
  base::string16 profile_name = cache.GetNameOfProfileAtIndex(
      cache.GetIndexOfProfileWithPath(profile_path));
  
  
  
  for (BackgroundModeInfoMap::const_iterator it =
       background_mode_data_.begin();
       it != background_mode_data_.end();
       ++it) {
    if (it->first->GetPath() == profile_path) {
      it->second->SetName(profile_name);
      UpdateStatusTrayIconContextMenu();
      return;
    }
  }
}
void BackgroundModeManager::OnProfileWillBeRemoved(
    const base::FilePath& profile_path) {
  ProfileInfoCache& cache =
      g_browser_process->profile_manager()->GetProfileInfoCache();
  base::string16 profile_name = cache.GetNameOfProfileAtIndex(
      cache.GetIndexOfProfileWithPath(profile_path));
  
  BackgroundModeInfoMap::iterator it =
      GetBackgroundModeIterator(profile_name);
  
  if (it != background_mode_data_.end()) {
    it->second->applications_->RemoveObserver(this);
    background_mode_data_.erase(it);
    
    
    if (!ShouldBeInBackgroundMode()) {
      EnableLaunchOnStartup(false);
      EndBackgroundMode();
    }
    UpdateStatusTrayIconContextMenu();
  }
}
void BackgroundModeManager::OnProfileNameChanged(
    const base::FilePath& profile_path,
    const base::string16& old_profile_name) {
  ProfileInfoCache& cache =
      g_browser_process->profile_manager()->GetProfileInfoCache();
  base::string16 new_profile_name = cache.GetNameOfProfileAtIndex(
      cache.GetIndexOfProfileWithPath(profile_path));
  BackgroundModeInfoMap::const_iterator it =
      GetBackgroundModeIterator(old_profile_name);
  
  
  
  if (it != background_mode_data_.end()) {
    it->second->SetName(new_profile_name);
    UpdateStatusTrayIconContextMenu();
  }
}
void BackgroundModeManager::ExecuteCommand(int command_id, int event_flags) {
  
  
  
  BackgroundModeData* bmd = background_mode_data_.begin()->second.get();
  switch (command_id) {
    case IDC_ABOUT:
      chrome::ShowAboutChrome(bmd->GetBrowserWindow());
      break;
    case IDC_TASK_MANAGER:
      chrome::OpenTaskManager(bmd->GetBrowserWindow());
      break;
    case IDC_EXIT:
      content::RecordAction(UserMetricsAction("Exit"));
      chrome::CloseAllBrowsers();
      break;
    case IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND: {
      
      
      DCHECK(IsBackgroundModePrefEnabled());
      DCHECK(chrome::WillKeepAlive());
      
      
      PrefService* service = g_browser_process->local_state();
      DCHECK(service);
      service->SetBoolean(prefs::kBackgroundModeEnabled, false);
      break;
    }
    default:
      bmd->ExecuteCommand(command_id, event_flags);
      break;
  }
}
void BackgroundModeManager::DecrementKeepAliveCountForStartup() {
  if (keep_alive_for_startup_) {
    keep_alive_for_startup_ = false;
    
    
    
    base::MessageLoop::current()->PostTask(
        FROM_HERE, base::Bind(&chrome::DecrementKeepAliveCount));
  }
}
void BackgroundModeManager::StartBackgroundMode() {
  DCHECK(ShouldBeInBackgroundMode());
  
  
  if (in_background_mode_)
    return;
  
  in_background_mode_ = true;
  UpdateKeepAliveAndTrayIcon();
  content::NotificationService::current()->Notify(
      chrome::NOTIFICATION_BACKGROUND_MODE_CHANGED,
      content::Source<BackgroundModeManager>(this),
      content::Details<bool>(&in_background_mode_));
}
void BackgroundModeManager::EndBackgroundMode() {
  if (!in_background_mode_)
    return;
  in_background_mode_ = false;
  UpdateKeepAliveAndTrayIcon();
  content::NotificationService::current()->Notify(
      chrome::NOTIFICATION_BACKGROUND_MODE_CHANGED,
      content::Source<BackgroundModeManager>(this),
      content::Details<bool>(&in_background_mode_));
}
void BackgroundModeManager::EnableBackgroundMode() {
  DCHECK(IsBackgroundModePrefEnabled());
  
  if (!in_background_mode_ && ShouldBeInBackgroundMode()) {
    StartBackgroundMode();
    EnableLaunchOnStartup(true);
  }
}
void BackgroundModeManager::DisableBackgroundMode() {
  DCHECK(!IsBackgroundModePrefEnabled());
  
  if (in_background_mode_) {
    EndBackgroundMode();
    EnableLaunchOnStartup(false);
  }
}
void BackgroundModeManager::SuspendBackgroundMode() {
  background_mode_suspended_ = true;
  UpdateKeepAliveAndTrayIcon();
}
void BackgroundModeManager::ResumeBackgroundMode() {
  background_mode_suspended_ = false;
  UpdateKeepAliveAndTrayIcon();
}
void BackgroundModeManager::UpdateKeepAliveAndTrayIcon() {
  if (in_background_mode_ && !background_mode_suspended_) {
    if (!keeping_alive_) {
      keeping_alive_ = true;
      chrome::IncrementKeepAliveCount();
    }
    CreateStatusTrayIcon();
    return;
  }
  RemoveStatusTrayIcon();
  if (keeping_alive_) {
    keeping_alive_ = false;
    chrome::DecrementKeepAliveCount();
  }
}
void BackgroundModeManager::OnBrowserAdded(Browser* browser) {
  ResumeBackgroundMode();
}
int BackgroundModeManager::GetBackgroundAppCount() const {
  int count = 0;
  
  for (BackgroundModeInfoMap::const_iterator it =
       background_mode_data_.begin();
       it != background_mode_data_.end();
       ++it) {
    count += it->second->GetBackgroundAppCount();
  }
  DCHECK(count >= 0);
  return count;
}
int BackgroundModeManager::GetBackgroundAppCountForProfile(
    Profile* const profile) const {
  BackgroundModeData* bmd = GetBackgroundModeData(profile);
  return bmd->GetBackgroundAppCount();
}
bool BackgroundModeManager::ShouldBeInBackgroundMode() const {
  return IsBackgroundModePrefEnabled() &&
      (GetBackgroundAppCount() > 0 || keep_alive_for_test_);
}
void BackgroundModeManager::OnBackgroundAppInstalled(
    const Extension* extension) {
  
  if (!IsBackgroundModePrefEnabled())
    return;
  
  
  EnableBackgroundMode();
  ResumeBackgroundMode();
  
  if (extension) {  
    DisplayAppInstalledNotification(extension);
  }
}
void BackgroundModeManager::CheckReloadStatus(
    const Extension* extension,
    bool* is_being_reloaded) {
    
    
    for (BackgroundModeInfoMap::const_iterator it =
             background_mode_data_.begin();
         it != background_mode_data_.end();
         ++it) {
      Profile* profile = it->first;
      
      if (profile->GetExtensionService()->IsBeingReloaded(extension->id()))
        *is_being_reloaded = true;
    }
}
void BackgroundModeManager::CreateStatusTrayIcon() {
  
  
  
  
#if !defined(OS_MACOSX) && !defined(OS_CHROMEOS)
  if (!status_tray_)
    status_tray_ = g_browser_process->status_tray();
#endif
  
  
  if (!status_tray_ || status_icon_)
    return;
  
  gfx::ImageSkia* image_skia = ui::ResourceBundle::GetSharedInstance().
      GetImageSkiaNamed(IDR_STATUS_TRAY_ICON);
  status_icon_ = status_tray_->CreateStatusIcon(
      StatusTray::BACKGROUND_MODE_ICON,
      *image_skia,
      l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
  if (!status_icon_)
    return;
  UpdateStatusTrayIconContextMenu();
}
void BackgroundModeManager::UpdateStatusTrayIconContextMenu() {
  
  UpdateKeepAliveAndTrayIcon();
  
  
  if (!status_icon_)
    return;
  
  
  if (background_mode_data_.empty()) {
    DCHECK(keep_alive_for_test_);
    return;
  }
  
  command_id_extension_vector_.clear();
  
  submenus.clear();
  
  
  scoped_ptr<StatusIconMenuModel> menu(new StatusIconMenuModel(this));
  
  menu->AddItem(IDC_ABOUT, l10n_util::GetStringUTF16(IDS_ABOUT));
  menu->AddItemWithStringId(IDC_TASK_MANAGER, IDS_TASK_MANAGER);
  menu->AddSeparator(ui::NORMAL_SEPARATOR);
  if (profile_cache_->GetNumberOfProfiles() > 1) {
    std::vector<BackgroundModeData*> bmd_vector;
    for (BackgroundModeInfoMap::iterator it =
         background_mode_data_.begin();
         it != background_mode_data_.end();
         ++it) {
       bmd_vector.push_back(it->second.get());
    }
    std::sort(bmd_vector.begin(), bmd_vector.end(),
              &BackgroundModeData::BackgroundModeDataCompare);
    int profiles_with_apps = 0;
    for (std::vector<BackgroundModeData*>::const_iterator bmd_it =
         bmd_vector.begin();
         bmd_it != bmd_vector.end();
         ++bmd_it) {
      BackgroundModeData* bmd = *bmd_it;
      
      
      if (bmd->GetBackgroundAppCount() > 0) {
        StatusIconMenuModel* submenu = new StatusIconMenuModel(bmd);
        
        
        submenus.push_back(submenu);
        bmd->BuildProfileMenu(submenu, menu.get());
        profiles_with_apps++;
      }
    }
    
    
    DCHECK_GT(profiles_with_apps, 0);
  } else {
    
    
    
    DCHECK(profile_cache_->GetNumberOfProfiles() == size_t(1) ||
           keep_alive_for_test_);
    background_mode_data_.begin()->second->BuildProfileMenu(menu.get(), NULL);
  }
  menu->AddSeparator(ui::NORMAL_SEPARATOR);
  menu->AddCheckItemWithStringId(
      IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND,
      IDS_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND);
  menu->SetCommandIdChecked(IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND,
                            true);
  PrefService* service = g_browser_process->local_state();
  DCHECK(service);
  bool enabled =
      service->IsUserModifiablePreference(prefs::kBackgroundModeEnabled);
  menu->SetCommandIdEnabled(IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND,
                            enabled);
  menu->AddItemWithStringId(IDC_EXIT, IDS_EXIT);
  context_menu_ = menu.get();
  status_icon_->SetContextMenu(menu.Pass());
}
void BackgroundModeManager::RemoveStatusTrayIcon() {
  if (status_icon_)
    status_tray_->RemoveStatusIcon(status_icon_);
  status_icon_ = NULL;
  context_menu_ = NULL;
}
BackgroundModeManager::BackgroundModeData*
BackgroundModeManager::GetBackgroundModeData(Profile* const profile) const {
  DCHECK(background_mode_data_.find(profile) != background_mode_data_.end());
  return background_mode_data_.find(profile)->second.get();
}
BackgroundModeManager::BackgroundModeInfoMap::iterator
BackgroundModeManager::GetBackgroundModeIterator(
    const base::string16& profile_name) {
  BackgroundModeInfoMap::iterator profile_it =
      background_mode_data_.end();
  for (BackgroundModeInfoMap::iterator it =
       background_mode_data_.begin();
       it != background_mode_data_.end();
       ++it) {
    if (it->second->name() == profile_name) {
      profile_it = it;
    }
  }
  return profile_it;
}
bool BackgroundModeManager::IsBackgroundModePrefEnabled() const {
  PrefService* service = g_browser_process->local_state();
  DCHECK(service);
  return service->GetBoolean(prefs::kBackgroundModeEnabled);
}