diff options
author | atwilson@chromium.org <atwilson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-04 23:23:23 +0000 |
---|---|---|
committer | atwilson@chromium.org <atwilson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-04 23:23:23 +0000 |
commit | abcc5a1c6f935d34793cf4e2afb3e61a4af4b1b3 (patch) | |
tree | 57e8d98365555b96980710de3f631e9d2cc6de30 | |
parent | c5f9e6620100db945269985fb48668700a9374bc (diff) | |
download | chromium_src-abcc5a1c6f935d34793cf4e2afb3e61a4af4b1b3.zip chromium_src-abcc5a1c6f935d34793cf4e2afb3e61a4af4b1b3.tar.gz chromium_src-abcc5a1c6f935d34793cf4e2afb3e61a4af4b1b3.tar.bz2 |
Removed BackgroundPageTracker - less code = more happiness
BUG=80755
TEST=Install extensions with background pages, make sure View Background Pages wrench menu item has the correct count.
Review URL: http://codereview.chromium.org/6929001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@84152 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/background_page_tracker.cc | 301 | ||||
-rw-r--r-- | chrome/browser/background_page_tracker.h | 95 | ||||
-rw-r--r-- | chrome/browser/background_page_tracker_unittest.cc | 125 | ||||
-rw-r--r-- | chrome/browser/prefs/browser_prefs.cc | 2 | ||||
-rw-r--r-- | chrome/browser/task_manager/task_manager.cc | 53 | ||||
-rw-r--r-- | chrome/browser/task_manager/task_manager.h | 5 | ||||
-rw-r--r-- | chrome/browser/task_manager/task_manager_browsertest.cc | 12 | ||||
-rw-r--r-- | chrome/browser/ui/toolbar/wrench_menu_model.cc | 9 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 2 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 1 |
10 files changed, 74 insertions, 531 deletions
diff --git a/chrome/browser/background_page_tracker.cc b/chrome/browser/background_page_tracker.cc deleted file mode 100644 index de6c04d..0000000 --- a/chrome/browser/background_page_tracker.cc +++ /dev/null @@ -1,301 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/background_page_tracker.h" - -#include <set> -#include <string> -#include <vector> - -#include "base/command_line.h" -#include "base/utf_string_conversions.h" -#include "base/values.h" -#include "chrome/browser/background_application_list_model.h" -#include "chrome/browser/background_contents_service.h" -#include "chrome/browser/background_contents_service_factory.h" -#include "chrome/browser/background_mode_manager.h" -#include "chrome/browser/browser_process.h" -#include "chrome/browser/extensions/extension_service.h" -#include "chrome/browser/prefs/pref_service.h" -#include "chrome/browser/prefs/scoped_user_pref_update.h" -#include "chrome/browser/profiles/profile.h" -#include "chrome/browser/profiles/profile_manager.h" -#include "chrome/common/extensions/extension.h" -#include "chrome/common/pref_names.h" -#include "content/common/notification_service.h" -#include "content/common/notification_type.h" - -/////////////////////////////////////////////////////////////////////////////// -// BackgroundPageTracker keeps a single DictionaryValue (stored at -// prefs::kKnownBackgroundPages). We keep only two pieces of information for -// each background page: the parent application/extension ID, and a boolean -// flag that is true if the user has acknowledged this page. -// -// kKnownBackgroundPages: -// DictionaryValue { -// <appid_1>: false, -// <appid_2>: true, -// ... etc ... -// } - -// static -void BackgroundPageTracker::RegisterPrefs(PrefService* prefs) { - prefs->RegisterDictionaryPref(prefs::kKnownBackgroundPages); -} - -// static -BackgroundPageTracker* BackgroundPageTracker::GetInstance() { - return Singleton<BackgroundPageTracker>::get(); -} - -int BackgroundPageTracker::GetBackgroundPageCount() { - if (!IsEnabled()) - return 0; - - PrefService* prefs = GetPrefService(); - const DictionaryValue* contents = - prefs->GetDictionary(prefs::kKnownBackgroundPages); - return contents ? contents->size() : 0; -} - -int BackgroundPageTracker::GetUnacknowledgedBackgroundPageCount() { - if (!IsEnabled()) - return 0; - PrefService* prefs = GetPrefService(); - const DictionaryValue* contents = - prefs->GetDictionary(prefs::kKnownBackgroundPages); - if (!contents) - return 0; - int count = 0; - for (DictionaryValue::key_iterator it = contents->begin_keys(); - it != contents->end_keys(); ++it) { - Value* value; - bool found = contents->GetWithoutPathExpansion(*it, &value); - DCHECK(found); - bool acknowledged = true; - bool valid = value->GetAsBoolean(&acknowledged); - DCHECK(valid); - if (!acknowledged) - count++; - } - return count; -} - -void BackgroundPageTracker::AcknowledgeBackgroundPages() { - if (!IsEnabled()) - return; - PrefService* prefs = GetPrefService(); - DictionaryPrefUpdate update(prefs, prefs::kKnownBackgroundPages); - DictionaryValue* contents = update.Get(); - bool prefs_modified = false; - for (DictionaryValue::key_iterator it = contents->begin_keys(); - it != contents->end_keys(); ++it) { - contents->SetWithoutPathExpansion(*it, Value::CreateBooleanValue(true)); - prefs_modified = true; - } - if (prefs_modified) { - prefs->ScheduleSavePersistentPrefs(); - SendChangeNotification(); - } -} - -BackgroundPageTracker::BackgroundPageTracker() { - // If background mode is disabled, just exit - don't load information from - // prefs or listen for any notifications so we will act as if there are no - // background pages, effectively disabling any associated badging. - if (!IsEnabled()) - return; - - // Check to make sure all of the extensions are loaded - once they are loaded - // we can update the list. - Profile* profile = g_browser_process->profile_manager()->GetDefaultProfile(); - if (profile->GetExtensionService() && - profile->GetExtensionService()->is_ready()) { - UpdateExtensionList(); - // We do not send any change notifications here, because the object was - // just created (it doesn't seem appropriate to send a change notification - // at initialization time). Also, since this is a singleton object, sending - // a notification in the constructor can lead to deadlock if one of the - // observers tries to get the singleton. - } else { - // Extensions aren't loaded yet - register to be notified when they are - // ready. - registrar_.Add(this, NotificationType::EXTENSIONS_READY, - NotificationService::AllSources()); - } -} - -BackgroundPageTracker::~BackgroundPageTracker() { -} - -PrefService* BackgroundPageTracker::GetPrefService() { - PrefService* service = g_browser_process->local_state(); - DCHECK(service); - return service; -} - -bool BackgroundPageTracker::IsEnabled() { - // Disable the background page tracker for unittests. - if (!g_browser_process->local_state()) - return false; - - // BackgroundPageTracker is enabled if background mode is enabled. - CommandLine* command_line = CommandLine::ForCurrentProcess(); - return BackgroundModeManager::IsBackgroundModeEnabled(command_line); -} - -void BackgroundPageTracker::Observe(NotificationType type, - const NotificationSource& source, - const NotificationDetails& details) { - switch (type.value) { - case NotificationType::EXTENSIONS_READY: - if (UpdateExtensionList()) - SendChangeNotification(); - break; - case NotificationType::BACKGROUND_CONTENTS_OPENED: { - std::string id = UTF16ToUTF8( - Details<BackgroundContentsOpenedDetails>(details)->application_id); - OnBackgroundPageLoaded(id); - break; - } - case NotificationType::EXTENSION_LOADED: { - const Extension* extension = Details<const Extension>(details).ptr(); - if (!extension->is_hosted_app() && - extension->background_url().is_valid()) - OnBackgroundPageLoaded(extension->id()); - break; - } - case NotificationType::EXTENSION_UNLOADED: { - std::string id = Details<UnloadedExtensionInfo>(details)->extension->id(); - OnExtensionUnloaded(id); - break; - } - default: - NOTREACHED(); - } -} - -bool BackgroundPageTracker::UpdateExtensionList() { - // Extensions are loaded - update our list. - Profile* profile = g_browser_process->profile_manager()->GetDefaultProfile(); - ExtensionService* extensions_service = profile->GetExtensionService(); - DCHECK(extensions_service); - - // We will make two passes to update the list: - // 1) Walk our list, and make sure that there's a corresponding extension for - // each item in the list. If not, delete it (extension was uninstalled). - // 2) Walk the set of currently loaded extensions and background contents, and - // make sure there's an entry in our list for each one. If not, create one. - - PrefService* prefs = GetPrefService(); - std::set<std::string> keys_to_delete; - bool pref_modified = false; - // If we've never set any prefs, then this is the first launch ever, so we - // want to automatically mark all existing extensions as acknowledged. - bool first_launch = - prefs->GetDictionary(prefs::kKnownBackgroundPages) == NULL; - DictionaryPrefUpdate update(prefs, prefs::kKnownBackgroundPages); - DictionaryValue* contents = update.Get(); - for (DictionaryValue::key_iterator it = contents->begin_keys(); - it != contents->end_keys(); ++it) { - // Check to make sure that the parent extension is still enabled. - const Extension* extension = extensions_service->GetExtensionById( - *it, false); - // If the extension is not loaded, add the id to our list of keys to delete - // later (can't delete now since we're still iterating). - if (!extension) { - keys_to_delete.insert(*it); - pref_modified = true; - } - } - - for (std::set<std::string>::const_iterator iter = keys_to_delete.begin(); - iter != keys_to_delete.end(); - ++iter) { - contents->RemoveWithoutPathExpansion(*iter, NULL); - } - - // Look for new extensions/background contents. - const ExtensionList* list = extensions_service->extensions(); - for (ExtensionList::const_iterator iter = list->begin(); - iter != list->begin(); - ++iter) { - // Any extension with a background page should be in our list. - if ((*iter)->background_url().is_valid()) { - // If we have not seen this extension ID before, add it to our list. - if (!contents->HasKey((*iter)->id())) { - contents->SetWithoutPathExpansion( - (*iter)->id(), Value::CreateBooleanValue(first_launch)); - pref_modified = true; - } - } - } - - // Add all apps with background contents also. - BackgroundContentsService* background_contents_service = - BackgroundContentsServiceFactory::GetForProfile(profile); - std::vector<BackgroundContents*> background_contents = - background_contents_service->GetBackgroundContents(); - for (std::vector<BackgroundContents*>::const_iterator iter = - background_contents.begin(); - iter != background_contents.end(); - ++iter) { - std::string application_id = UTF16ToUTF8( - background_contents_service->GetParentApplicationId(*iter)); - if (!contents->HasKey(application_id)) { - contents->SetWithoutPathExpansion( - application_id, Value::CreateBooleanValue(first_launch)); - pref_modified = true; - } - } - - // Register for when new pages are loaded/unloaded so we can update our list. - registrar_.Add(this, NotificationType::EXTENSION_LOADED, - NotificationService::AllSources()); - registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, - NotificationService::AllSources()); - registrar_.Add(this, NotificationType::BACKGROUND_CONTENTS_OPENED, - NotificationService::AllSources()); - - // If we modified the list, save it to prefs and let our caller know. - if (pref_modified) - prefs->ScheduleSavePersistentPrefs(); - return pref_modified; -} - -void BackgroundPageTracker::OnBackgroundPageLoaded(const std::string& id) { - DCHECK(IsEnabled()); - PrefService* prefs = GetPrefService(); - DictionaryPrefUpdate update(prefs, prefs::kKnownBackgroundPages); - DictionaryValue* contents = update.Get(); - // No need to update our list if this extension was already known. - if (contents->HasKey(id)) - return; - - // Update our list with this new as-yet-unacknowledged page. - contents->SetWithoutPathExpansion(id, Value::CreateBooleanValue(false)); - prefs->ScheduleSavePersistentPrefs(); - SendChangeNotification(); -} - -void BackgroundPageTracker::OnExtensionUnloaded(const std::string& id) { - DCHECK(IsEnabled()); - PrefService* prefs = GetPrefService(); - DictionaryPrefUpdate update(prefs, prefs::kKnownBackgroundPages); - DictionaryValue* contents = update.Get(); - - if (!contents->HasKey(id)) - return; - - contents->RemoveWithoutPathExpansion(id, NULL); - prefs->ScheduleSavePersistentPrefs(); - SendChangeNotification(); -} - -void BackgroundPageTracker::SendChangeNotification() { - NotificationService::current()->Notify( - NotificationType::BACKGROUND_PAGE_TRACKER_CHANGED, - Source<BackgroundPageTracker>(this), - NotificationService::NoDetails()); -} diff --git a/chrome/browser/background_page_tracker.h b/chrome/browser/background_page_tracker.h deleted file mode 100644 index e00f5eb..0000000 --- a/chrome/browser/background_page_tracker.h +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_BACKGROUND_PAGE_TRACKER_H_ -#define CHROME_BROWSER_BACKGROUND_PAGE_TRACKER_H_ -#pragma once - -#include <map> -#include <string> - -#include "base/gtest_prod_util.h" -#include "base/memory/singleton.h" -#include "content/common/notification_observer.h" -#include "content/common/notification_registrar.h" - -class PrefService; - -/////////////////////////////////////////////////////////////////////////////// -// BackgroundPageTracker -// -// This class is a singleton class that monitors when new background pages are -// loaded, and sends out a notification. -// -class BackgroundPageTracker : public NotificationObserver { - public: - static void RegisterPrefs(PrefService* prefs); - - // Convenience routine which gets the singleton object. - static BackgroundPageTracker* GetInstance(); - - // Returns the number of background apps/extensions currently loaded. - int GetBackgroundPageCount(); - - // Returns the number of unacknowledged background pages (for use in badges, - // etc). - int GetUnacknowledgedBackgroundPageCount(); - - // Called when the user has acknowledged that new background apps have been - // loaded. Future calls to GetUnacknowledgedBackgroundPageCount() will return - // 0 until another background page is installed. - void AcknowledgeBackgroundPages(); - - protected: - // Constructor marked as protected so we can create mock subclasses. - BackgroundPageTracker(); - ~BackgroundPageTracker(); - friend struct DefaultSingletonTraits<BackgroundPageTracker>; - friend class BackgroundPageTrackerTest; - FRIEND_TEST_ALL_PREFIXES(BackgroundPageTrackerTest, OnBackgroundPageLoaded); - FRIEND_TEST_ALL_PREFIXES(BackgroundPageTrackerTest, - AcknowledgeBackgroundPages); - FRIEND_TEST_ALL_PREFIXES(BackgroundPageTrackerTest, - TestTrackerChangedNotifications); - - // Mockable method to get the PrefService where we store information about - // background pages. - virtual PrefService* GetPrefService(); - - // Helper routine which checks to see if this object should be enabled or not. - // If false, the BackgroundPageTracker always acts as if there are no - // background pages. Virtual to allow enabling in mocks. - virtual bool IsEnabled(); - - private: - // NotificationObserver implementation. - virtual void Observe(NotificationType type, - const NotificationSource& source, - const NotificationDetails& details); - - // Invoked once all extensions have been loaded, so we can update our list of - // tracked pages and send out the appropriate notifications. Returns true if - // the extension list was changed. - bool UpdateExtensionList(); - - // Called when a background page is loaded (either because a hosted app - // opened a BackgroundContents or a packaged extension was loaded with an - // extension background page). Updates our internal list of unacknowledged - // background pages. - void OnBackgroundPageLoaded(const std::string& parent_application_id); - - // When an extension is unloaded, removes it from the list of background pages - // so we no longer nag the user about it (if it is still unacknowledged) and - // we inform the user if it is ever re-installed/enabled. - void OnExtensionUnloaded(const std::string& parent_application_id); - - // Sends out a notification that the list of background pages has changed. - void SendChangeNotification(); - - NotificationRegistrar registrar_; - - DISALLOW_COPY_AND_ASSIGN(BackgroundPageTracker); -}; - -#endif // CHROME_BROWSER_BACKGROUND_PAGE_TRACKER_H_ diff --git a/chrome/browser/background_page_tracker_unittest.cc b/chrome/browser/background_page_tracker_unittest.cc deleted file mode 100644 index 40f33f5..0000000 --- a/chrome/browser/background_page_tracker_unittest.cc +++ /dev/null @@ -1,125 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/basictypes.h" -#include "base/command_line.h" -#include "base/utf_string_conversions.h" -#include "chrome/browser/background_page_tracker.h" -#include "chrome/test/testing_browser_process.h" -#include "chrome/test/testing_browser_process_test.h" -#include "chrome/test/testing_pref_service.h" -#include "content/common/notification_service.h" -#include "content/common/notification_type.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "testing/platform_test.h" - -class MockBackgroundPageTracker : public BackgroundPageTracker { - public: - MockBackgroundPageTracker() { - BackgroundPageTracker::RegisterPrefs(&prefs_); - } - ~MockBackgroundPageTracker() {} - // Overridden from BackgroundPageTracker to mock out functionality. - virtual bool IsEnabled() { return true; } - virtual PrefService* GetPrefService() { return &prefs_; } - private: - TestingPrefService prefs_; -}; - -class BackgroundPageTrackerTest : public TestingBrowserProcessTest { -}; - -TEST_F(BackgroundPageTrackerTest, Create) { - MockBackgroundPageTracker tracker; - EXPECT_EQ(0, tracker.GetBackgroundPageCount()); - EXPECT_EQ(0, tracker.GetUnacknowledgedBackgroundPageCount()); -} - -TEST_F(BackgroundPageTrackerTest, OnBackgroundPageLoaded) { - MockBackgroundPageTracker tracker; - EXPECT_EQ(0, tracker.GetBackgroundPageCount()); - EXPECT_EQ(0, tracker.GetUnacknowledgedBackgroundPageCount()); - std::string app1 = "app_id_1"; - std::string app2 = "app_id_2"; - tracker.OnBackgroundPageLoaded(app1); - EXPECT_EQ(1, tracker.GetBackgroundPageCount()); - EXPECT_EQ(1, tracker.GetUnacknowledgedBackgroundPageCount()); - tracker.OnBackgroundPageLoaded(app1); - EXPECT_EQ(1, tracker.GetBackgroundPageCount()); - EXPECT_EQ(1, tracker.GetUnacknowledgedBackgroundPageCount()); - tracker.OnBackgroundPageLoaded(app2); - EXPECT_EQ(2, tracker.GetBackgroundPageCount()); - EXPECT_EQ(2, tracker.GetUnacknowledgedBackgroundPageCount()); - - tracker.OnExtensionUnloaded(app1); - EXPECT_EQ(1, tracker.GetBackgroundPageCount()); - EXPECT_EQ(1, tracker.GetUnacknowledgedBackgroundPageCount()); - - tracker.OnExtensionUnloaded(app2); - EXPECT_EQ(0, tracker.GetBackgroundPageCount()); - EXPECT_EQ(0, tracker.GetUnacknowledgedBackgroundPageCount()); -} - -TEST_F(BackgroundPageTrackerTest, AcknowledgeBackgroundPages) { - MockBackgroundPageTracker tracker; - EXPECT_EQ(0, tracker.GetBackgroundPageCount()); - EXPECT_EQ(0, tracker.GetUnacknowledgedBackgroundPageCount()); - std::string app1 = "app_id_1"; - tracker.OnBackgroundPageLoaded(app1); - EXPECT_EQ(1, tracker.GetBackgroundPageCount()); - EXPECT_EQ(1, tracker.GetUnacknowledgedBackgroundPageCount()); - tracker.AcknowledgeBackgroundPages(); - EXPECT_EQ(1, tracker.GetBackgroundPageCount()); - EXPECT_EQ(0, tracker.GetUnacknowledgedBackgroundPageCount()); - tracker.OnBackgroundPageLoaded(app1); - EXPECT_EQ(1, tracker.GetBackgroundPageCount()); - EXPECT_EQ(0, tracker.GetUnacknowledgedBackgroundPageCount()); -} - -class BadgeChangedNotificationCounter : public NotificationObserver { - public: - BadgeChangedNotificationCounter() - : count_(0) { - registrar_.Add(this, - NotificationType::BACKGROUND_PAGE_TRACKER_CHANGED, - NotificationService::AllSources()); - } - // # notifications received. - int count() { return count_; } - // NotificationObserver implementation. - virtual void Observe(NotificationType type, - const NotificationSource& source, - const NotificationDetails& details) { - EXPECT_EQ(type.value, - NotificationType::BACKGROUND_PAGE_TRACKER_CHANGED); - count_++; - } - private: - int count_; - NotificationRegistrar registrar_; -}; - -TEST_F(BackgroundPageTrackerTest, TestTrackerChangedNotifications) { - MockBackgroundPageTracker tracker; - BadgeChangedNotificationCounter counter; - std::string app1 = "app_id_1"; - std::string app2 = "app_id_2"; - std::string app3 = "app_id_3"; - // New extension should generate notification - tracker.OnBackgroundPageLoaded(app1); - EXPECT_EQ(1, counter.count()); - // Same extension should not generate notification - tracker.OnBackgroundPageLoaded(app1); - EXPECT_EQ(1, counter.count()); - // New extension should generate notification - tracker.OnBackgroundPageLoaded(app2); - EXPECT_EQ(2, counter.count()); - // Acknowledging pages should generate notification. - tracker.AcknowledgeBackgroundPages(); - EXPECT_EQ(3, counter.count()); - tracker.OnBackgroundPageLoaded(app1); - EXPECT_EQ(3, counter.count()); - tracker.OnBackgroundPageLoaded(app3); - EXPECT_EQ(4, counter.count()); -} diff --git a/chrome/browser/prefs/browser_prefs.cc b/chrome/browser/prefs/browser_prefs.cc index 08879d1..43d6fa7 100644 --- a/chrome/browser/prefs/browser_prefs.cc +++ b/chrome/browser/prefs/browser_prefs.cc @@ -8,7 +8,6 @@ #include "chrome/browser/autofill/autofill_manager.h" #include "chrome/browser/background_contents_service.h" #include "chrome/browser/background_mode_manager.h" -#include "chrome/browser/background_page_tracker.h" #include "chrome/browser/bookmarks/bookmark_utils.h" #include "chrome/browser/browser_shutdown.h" #include "chrome/browser/content_settings/host_content_settings_map.h" @@ -107,7 +106,6 @@ void RegisterLocalState(PrefService* local_state) { geolocation::RegisterPrefs(local_state); AutofillManager::RegisterBrowserPrefs(local_state); BackgroundModeManager::RegisterPrefs(local_state); - BackgroundPageTracker::RegisterPrefs(local_state); NotificationUIManager::RegisterPrefs(local_state); PrefProxyConfigService::RegisterPrefs(local_state); policy::CloudPolicySubsystem::RegisterPrefs(local_state); diff --git a/chrome/browser/task_manager/task_manager.cc b/chrome/browser/task_manager/task_manager.cc index 6bc6543..2acca11 100644 --- a/chrome/browser/task_manager/task_manager.cc +++ b/chrome/browser/task_manager/task_manager.cc @@ -12,7 +12,11 @@ #include "base/string_util.h" #include "base/threading/thread.h" #include "base/utf_string_conversions.h" +#include "chrome/browser/background_contents_service.h" +#include "chrome/browser/background_contents_service_factory.h" #include "chrome/browser/browser_process.h" +#include "chrome/browser/extensions/extension_host.h" +#include "chrome/browser/extensions/extension_process_manager.h" #include "chrome/browser/net/url_request_tracking.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/profiles/profile_manager.h" @@ -1002,3 +1006,52 @@ bool TaskManagerModel::GetAndCacheMemoryMetrics( memory_usage_map_.insert(std::make_pair(handle, *usage)); return true; } + +namespace { + +// Counts the number of extension background pages associated with this profile. +int CountExtensionBackgroundPagesForProfile(Profile* profile) { + int count = 0; + ExtensionProcessManager* manager = profile->GetExtensionProcessManager(); + if (!manager) + return count; + for (ExtensionProcessManager::const_iterator iter = manager->begin(); + iter != manager->end(); + ++iter) { + if ((*iter)->GetRenderViewType() == ViewType::EXTENSION_BACKGROUND_PAGE) + count++; + } + return count; +} + +} // namespace + +// static +int TaskManager::GetBackgroundPageCount() { + int count = 0; + ProfileManager* profile_manager = g_browser_process->profile_manager(); + if (!profile_manager) // Null when running unit tests. + return count; + std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles()); + for (std::vector<Profile*>::const_iterator iter = profiles.begin(); + iter != profiles.end(); + ++iter) { + Profile* profile = *iter; + // Count the number of Background Contents (background pages for hosted + // apps). Incognito windows do not support hosted apps, so just check the + // main profile. + BackgroundContentsService* background_contents_service = + BackgroundContentsServiceFactory::GetForProfile(profile); + if (background_contents_service) + count += background_contents_service->GetBackgroundContents().size(); + + // Count the number of extensions with background pages (including + // incognito). + count += CountExtensionBackgroundPagesForProfile(profile); + if (profile->HasOffTheRecordProfile()) { + count += CountExtensionBackgroundPagesForProfile( + profile->GetOffTheRecordProfile()); + } + } + return count; +} diff --git a/chrome/browser/task_manager/task_manager.h b/chrome/browser/task_manager/task_manager.h index e11f595..2b8d0a2 100644 --- a/chrome/browser/task_manager/task_manager.h +++ b/chrome/browser/task_manager/task_manager.h @@ -169,6 +169,11 @@ class TaskManager { void OpenAboutMemory(); + // Returns the number of background pages that will be displayed in the + // TaskManager. Used by the wrench menu code to display a count of background + // pages in the "View Background Pages" menu item. + static int GetBackgroundPageCount(); + private: FRIEND_TEST_ALL_PREFIXES(TaskManagerTest, Basic); FRIEND_TEST_ALL_PREFIXES(TaskManagerTest, Resources); diff --git a/chrome/browser/task_manager/task_manager_browsertest.cc b/chrome/browser/task_manager/task_manager_browsertest.cc index 6d68ac7..5f9455e 100644 --- a/chrome/browser/task_manager/task_manager_browsertest.cc +++ b/chrome/browser/task_manager/task_manager_browsertest.cc @@ -147,6 +147,7 @@ IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeTabContentsChanges) { IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeBGContentsChanges) { EXPECT_EQ(0, model()->ResourceCount()); + EXPECT_EQ(0, TaskManager::GetBackgroundPageCount()); // Show the task manager. This populates the model, and helps with debugging // (you see the task manager). @@ -167,14 +168,17 @@ IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeBGContentsChanges) { ASCIIToUTF16("background_page"), application_id); WaitForResourceChange(3); + EXPECT_EQ(1, TaskManager::GetBackgroundPageCount()); // Close the background contents and verify that we notice. service->ShutdownAssociatedBackgroundContents(application_id); WaitForResourceChange(2); + EXPECT_EQ(0, TaskManager::GetBackgroundPageCount()); } IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, KillBGContents) { EXPECT_EQ(0, model()->ResourceCount()); + EXPECT_EQ(0, TaskManager::GetBackgroundPageCount()); // Show the task manager. This populates the model, and helps with debugging // (you see the task manager). @@ -197,6 +201,7 @@ IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, KillBGContents) { // Wait for the background contents process to finish loading. WaitForBackgroundContents(); EXPECT_EQ(3, model()->ResourceCount()); + EXPECT_EQ(1, TaskManager::GetBackgroundPageCount()); // Kill the background contents process and verify that it disappears from the // model. @@ -210,10 +215,12 @@ IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, KillBGContents) { } ASSERT_TRUE(found); WaitForResourceChange(2); + EXPECT_EQ(0, TaskManager::GetBackgroundPageCount()); } IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeExtensionChanges) { EXPECT_EQ(0, model()->ResourceCount()); + EXPECT_EQ(0, TaskManager::GetBackgroundPageCount()); // Show the task manager. This populates the model, and helps with debugging // (you see the task manager). @@ -227,10 +234,12 @@ IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeExtensionChanges) { ASSERT_TRUE(LoadExtension( test_data_dir_.AppendASCII("common").AppendASCII("background_page"))); WaitForResourceChange(3); + EXPECT_EQ(1, TaskManager::GetBackgroundPageCount()); // Unload extension to avoid crash on Windows (see http://crbug.com/31663). UnloadExtension(last_loaded_extension_id_); WaitForResourceChange(2); + EXPECT_EQ(0, TaskManager::GetBackgroundPageCount()); } IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeExtensionTabs) { @@ -339,6 +348,7 @@ IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeNotificationChanges) { } IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, KillExtension) { + EXPECT_EQ(0, TaskManager::GetBackgroundPageCount()); // Show the task manager. This populates the model, and helps with debugging // (you see the task manager). browser()->window()->ShowTaskManager(); @@ -349,6 +359,7 @@ IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, KillExtension) { // Wait until we see the loaded extension in the task manager (the three // resources are: the browser process, New Tab Page, and the extension). WaitForResourceChange(3); + EXPECT_EQ(1, TaskManager::GetBackgroundPageCount()); EXPECT_TRUE(model()->GetResourceExtension(0) == NULL); EXPECT_TRUE(model()->GetResourceExtension(1) == NULL); @@ -357,6 +368,7 @@ IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, KillExtension) { // Kill the extension process and make sure we notice it. TaskManager::GetInstance()->KillProcess(2); WaitForResourceChange(2); + EXPECT_EQ(0, TaskManager::GetBackgroundPageCount()); } // Disabled, http://crbug.com/66957. diff --git a/chrome/browser/ui/toolbar/wrench_menu_model.cc b/chrome/browser/ui/toolbar/wrench_menu_model.cc index ea832ff..2007004 100644 --- a/chrome/browser/ui/toolbar/wrench_menu_model.cc +++ b/chrome/browser/ui/toolbar/wrench_menu_model.cc @@ -13,7 +13,6 @@ #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "chrome/app/chrome_command_ids.h" -#include "chrome/browser/background_page_tracker.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/defaults.h" #include "chrome/browser/prefs/pref_service.h" @@ -21,6 +20,7 @@ #include "chrome/browser/sync/profile_sync_service.h" #include "chrome/browser/sync/sync_ui_util.h" #include "chrome/browser/tabs/tab_strip_model.h" +#include "chrome/browser/task_manager/task_manager.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/toolbar/encoding_menu_controller.h" #include "chrome/browser/upgrade_detector.h" @@ -249,7 +249,7 @@ string16 WrenchMenuModel::GetLabelForCommandId(int command_id) const { #endif case IDC_VIEW_BACKGROUND_PAGES: { string16 num_background_pages = base::FormatNumber( - BackgroundPageTracker::GetInstance()->GetBackgroundPageCount()); + TaskManager::GetBackgroundPageCount()); return l10n_util::GetStringFUTF16(IDS_VIEW_BACKGROUND_PAGES, num_background_pages); } @@ -329,8 +329,7 @@ bool WrenchMenuModel::IsCommandIdVisible(int command_id) const { return false; #endif } else if (command_id == IDC_VIEW_BACKGROUND_PAGES) { - BackgroundPageTracker* tracker = BackgroundPageTracker::GetInstance(); - return tracker->GetBackgroundPageCount() > 0; + return TaskManager::GetBackgroundPageCount() > 0; } return true; } @@ -466,7 +465,7 @@ void WrenchMenuModel::Build() { #endif AddItem(IDC_ABOUT, l10n_util::GetStringFUTF16(IDS_ABOUT, product_name)); string16 num_background_pages = base::FormatNumber( - BackgroundPageTracker::GetInstance()->GetBackgroundPageCount()); + TaskManager::GetBackgroundPageCount()); AddItem(IDC_VIEW_BACKGROUND_PAGES, l10n_util::GetStringFUTF16( IDS_VIEW_BACKGROUND_PAGES, num_background_pages)); AddItem(IDC_UPGRADE_DIALOG, l10n_util::GetStringFUTF16( diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 8a13e1c..be11431 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -253,8 +253,6 @@ 'browser/background_mode_manager_linux.cc', 'browser/background_mode_manager_mac.mm', 'browser/background_mode_manager_win.cc', - 'browser/background_page_tracker.cc', - 'browser/background_page_tracker.h', 'browser/bookmarks/base_bookmark_model_observer.cc', 'browser/bookmarks/base_bookmark_model_observer.h', 'browser/bookmarks/bookmark_codec.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 1841bc0..ed473fc 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1237,7 +1237,6 @@ 'browser/background_application_list_model_unittest.cc', 'browser/background_contents_service_unittest.cc', 'browser/background_mode_manager_unittest.cc', - 'browser/background_page_tracker_unittest.cc', 'browser/bookmarks/bookmark_codec_unittest.cc', 'browser/bookmarks/bookmark_context_menu_controller_unittest.cc', 'browser/bookmarks/bookmark_html_writer_unittest.cc', |