diff options
author | atwilson@chromium.org <atwilson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-14 18:12:47 +0000 |
---|---|---|
committer | atwilson@chromium.org <atwilson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-14 18:12:47 +0000 |
commit | 08392d8b0ddf22814b3e4c7aed44281ff3a9bee6 (patch) | |
tree | 8e1f49896180c65327e3e13debe8db6542f5eb21 /chrome/browser/task_manager | |
parent | b780ce945a519a9c4c77f19516a95f5e2254b8fc (diff) | |
download | chromium_src-08392d8b0ddf22814b3e4c7aed44281ff3a9bee6.zip chromium_src-08392d8b0ddf22814b3e4c7aed44281ff3a9bee6.tar.gz chromium_src-08392d8b0ddf22814b3e4c7aed44281ff3a9bee6.tar.bz2 |
Blow away BackgroundContents when RenderView goes away.
BUG=65189
TEST=TaskManagerBrowserTest.KillBGContents
Review URL: http://codereview.chromium.org/6226002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@71458 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/task_manager')
-rw-r--r-- | chrome/browser/task_manager/task_manager_browsertest.cc | 92 |
1 files changed, 87 insertions, 5 deletions
diff --git a/chrome/browser/task_manager/task_manager_browsertest.cc b/chrome/browser/task_manager/task_manager_browsertest.cc index b17132f..1e6c367 100644 --- a/chrome/browser/task_manager/task_manager_browsertest.cc +++ b/chrome/browser/task_manager/task_manager_browsertest.cc @@ -34,12 +34,15 @@ namespace { const FilePath::CharType* kTitle1File = FILE_PATH_LITERAL("title1.html"); -class ResourceChangeObserver : public TaskManagerModelObserver { +class ResourceChangeObserver + : public TaskManagerModelObserver, + public base::RefCountedThreadSafe<ResourceChangeObserver> { public: ResourceChangeObserver(const TaskManagerModel* model, int target_resource_count) : model_(model), - target_resource_count_(target_resource_count) { + target_resource_count_(target_resource_count), + check_pending_(false) { } virtual void OnModelChanged() { @@ -59,13 +62,46 @@ class ResourceChangeObserver : public TaskManagerModelObserver { } private: + friend class base::RefCountedThreadSafe<ResourceChangeObserver>; void OnResourceChange() { + // The task manager can churn resources (for example, when a + // BackgroundContents navigates, we remove and re-add the resource to + // allow the UI to update properly). So check the resource count via + // a task to make sure we aren't triggered by a transient change. + if (check_pending_) + return; + check_pending_ = true; + MessageLoopForUI::current()->PostTask( + FROM_HERE, + NewRunnableMethod(this, &ResourceChangeObserver::CheckResourceCount)); + } + + void CheckResourceCount() { if (model_->ResourceCount() == target_resource_count_) MessageLoopForUI::current()->Quit(); } const TaskManagerModel* model_; const int target_resource_count_; + bool check_pending_; +}; + +// Helper class used to wait for a BackgroundContents to finish loading. +class BackgroundContentsListener : public NotificationObserver { + public: + explicit BackgroundContentsListener(Profile* profile) { + registrar_.Add(this, NotificationType::BACKGROUND_CONTENTS_NAVIGATED, + Source<Profile>(profile)); + } + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + // Quit once the BackgroundContents has been loaded. + if (type.value == NotificationType::BACKGROUND_CONTENTS_NAVIGATED) + MessageLoopForUI::current()->Quit(); + } + private: + NotificationRegistrar registrar_; }; } // namespace @@ -79,10 +115,17 @@ class TaskManagerBrowserTest : public ExtensionBrowserTest { void WaitForResourceChange(int target_count) { if (model()->ResourceCount() == target_count) return; - ResourceChangeObserver observer(model(), target_count); - model()->AddObserver(&observer); + scoped_refptr<ResourceChangeObserver> observer( + new ResourceChangeObserver(model(), target_count)); + model()->AddObserver(observer.get()); + ui_test_utils::RunMessageLoop(); + model()->RemoveObserver(observer.get()); + } + + // Wait for any pending BackgroundContents to finish starting up. + void WaitForBackgroundContents() { + BackgroundContentsListener listener(browser()->profile()); ui_test_utils::RunMessageLoop(); - model()->RemoveObserver(&observer); } }; @@ -149,6 +192,45 @@ IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeBGContentsChanges) { WaitForResourceChange(2); } +IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, KillBGContents) { + EXPECT_EQ(0, model()->ResourceCount()); + + // Show the task manager. This populates the model, and helps with debugging + // (you see the task manager). + browser()->window()->ShowTaskManager(); + + // Browser and the New Tab Page. + WaitForResourceChange(2); + + // Open a new background contents and make sure we notice that. + GURL url(ui_test_utils::GetTestUrl(FilePath(FilePath::kCurrentDirectory), + FilePath(kTitle1File))); + + BackgroundContentsService* service = + browser()->profile()->GetBackgroundContentsService(); + string16 application_id(ASCIIToUTF16("test_app_id")); + service->LoadBackgroundContents(browser()->profile(), + url, + ASCIIToUTF16("background_page"), + application_id); + // Wait for the background contents process to finish loading. + WaitForBackgroundContents(); + EXPECT_EQ(3, model()->ResourceCount()); + + // Kill the background contents process and verify that it disappears from the + // model. + bool found = false; + for (int i = 0; i < model()->ResourceCount(); ++i) { + if (model()->IsBackgroundResource(i)) { + TaskManager::GetInstance()->KillProcess(i); + found = true; + break; + } + } + ASSERT_TRUE(found); + WaitForResourceChange(2); +} + IN_PROC_BROWSER_TEST_F(TaskManagerBrowserTest, NoticeExtensionChanges) { EXPECT_EQ(0, model()->ResourceCount()); |