OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/profiles/profile_destroyer.h" |
| 6 |
| 7 #include "chrome/test/base/browser_with_test_window_test.h" |
| 8 #include "chrome/test/base/testing_profile.h" |
| 9 #include "content/public/browser/render_process_host.h" |
| 10 #include "content/public/browser/site_instance.h" |
| 11 |
| 12 class TestingOffTheRecordDestructionProfile : public TestingProfile { |
| 13 public: |
| 14 TestingOffTheRecordDestructionProfile() : destroyed_otr_profile_(false) { |
| 15 set_incognito(true); |
| 16 } |
| 17 virtual void DestroyOffTheRecordProfile() OVERRIDE { |
| 18 destroyed_otr_profile_ = true; |
| 19 } |
| 20 bool destroyed_otr_profile_; |
| 21 |
| 22 DISALLOW_COPY_AND_ASSIGN(TestingOffTheRecordDestructionProfile); |
| 23 }; |
| 24 |
| 25 class TestingOriginalDestructionProfile : public TestingProfile { |
| 26 public: |
| 27 TestingOriginalDestructionProfile() : destroyed_otr_profile_(false) { |
| 28 DCHECK_EQ(kNull, living_instance_); |
| 29 living_instance_ = this; |
| 30 } |
| 31 virtual ~TestingOriginalDestructionProfile() { |
| 32 DCHECK_EQ(this, living_instance_); |
| 33 living_instance_ = NULL; |
| 34 } |
| 35 virtual void DestroyOffTheRecordProfile() OVERRIDE { |
| 36 SetOffTheRecordProfile(NULL); |
| 37 destroyed_otr_profile_ = true; |
| 38 } |
| 39 bool destroyed_otr_profile_; |
| 40 static TestingOriginalDestructionProfile* living_instance_; |
| 41 |
| 42 // This is to avoid type casting in DCHECK_EQ & EXPECT_NE. |
| 43 static const TestingOriginalDestructionProfile* kNull; |
| 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(TestingOriginalDestructionProfile); |
| 46 }; |
| 47 const TestingOriginalDestructionProfile* |
| 48 TestingOriginalDestructionProfile::kNull = NULL; |
| 49 |
| 50 TestingOriginalDestructionProfile* |
| 51 TestingOriginalDestructionProfile::living_instance_ = NULL; |
| 52 |
| 53 class ProfileDestroyerTest : public BrowserWithTestWindowTest { |
| 54 public: |
| 55 ProfileDestroyerTest() : off_the_record_profile_(NULL) {} |
| 56 |
| 57 protected: |
| 58 virtual TestingProfile* CreateProfile() OVERRIDE { |
| 59 if (off_the_record_profile_ == NULL) |
| 60 off_the_record_profile_ = new TestingOffTheRecordDestructionProfile(); |
| 61 return off_the_record_profile_; |
| 62 } |
| 63 TestingOffTheRecordDestructionProfile* off_the_record_profile_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(ProfileDestroyerTest); |
| 66 }; |
| 67 |
| 68 TEST_F(ProfileDestroyerTest, DelayProfileDestruction) { |
| 69 scoped_refptr<content::SiteInstance> instance1( |
| 70 content::SiteInstance::Create(off_the_record_profile_)); |
| 71 scoped_ptr<content::RenderProcessHost> render_process_host1; |
| 72 render_process_host1.reset(instance1->GetProcess()); |
| 73 ASSERT_TRUE(render_process_host1.get() != NULL); |
| 74 |
| 75 scoped_refptr<content::SiteInstance> instance2( |
| 76 content::SiteInstance::Create(off_the_record_profile_)); |
| 77 scoped_ptr<content::RenderProcessHost> render_process_host2; |
| 78 render_process_host2.reset(instance2->GetProcess()); |
| 79 ASSERT_TRUE(render_process_host2.get() != NULL); |
| 80 |
| 81 // destroying the browser should not destroy the off the record profile... |
| 82 set_browser(NULL); |
| 83 EXPECT_FALSE(off_the_record_profile_->destroyed_otr_profile_); |
| 84 |
| 85 // until we destroy the render process host holding on to it... |
| 86 render_process_host1.release()->Cleanup(); |
| 87 |
| 88 // And asynchronicity kicked in properly. |
| 89 MessageLoop::current()->RunAllPending(); |
| 90 EXPECT_FALSE(off_the_record_profile_->destroyed_otr_profile_); |
| 91 |
| 92 // I meant, ALL the render process hosts... :-) |
| 93 render_process_host2.release()->Cleanup(); |
| 94 MessageLoop::current()->RunAllPending(); |
| 95 EXPECT_TRUE(off_the_record_profile_->destroyed_otr_profile_); |
| 96 } |
| 97 |
| 98 TEST_F(ProfileDestroyerTest, DelayOriginalProfileDestruction) { |
| 99 TestingOriginalDestructionProfile* original_profile = |
| 100 new TestingOriginalDestructionProfile; |
| 101 |
| 102 TestingOffTheRecordDestructionProfile* off_the_record_profile = |
| 103 new TestingOffTheRecordDestructionProfile; |
| 104 |
| 105 original_profile->SetOffTheRecordProfile(off_the_record_profile); |
| 106 |
| 107 scoped_refptr<content::SiteInstance> instance1( |
| 108 content::SiteInstance::Create(off_the_record_profile)); |
| 109 scoped_ptr<content::RenderProcessHost> render_process_host1; |
| 110 render_process_host1.reset(instance1->GetProcess()); |
| 111 ASSERT_TRUE(render_process_host1.get() != NULL); |
| 112 |
| 113 // Trying to destroy the original profile should be delayed until associated |
| 114 // off the record profile is released by all render process hosts. |
| 115 ProfileDestroyer::DestroyProfileWhenAppropriate(original_profile); |
| 116 EXPECT_NE(TestingOriginalDestructionProfile::kNull, |
| 117 TestingOriginalDestructionProfile::living_instance_); |
| 118 EXPECT_FALSE(original_profile->destroyed_otr_profile_); |
| 119 |
| 120 render_process_host1.release()->Cleanup(); |
| 121 MessageLoop::current()->RunAllPending(); |
| 122 EXPECT_EQ(NULL, TestingOriginalDestructionProfile::living_instance_); |
| 123 |
| 124 // And the same protection should apply to the main profile. |
| 125 TestingOriginalDestructionProfile* main_profile = |
| 126 new TestingOriginalDestructionProfile; |
| 127 scoped_refptr<content::SiteInstance> instance2( |
| 128 content::SiteInstance::Create(main_profile)); |
| 129 scoped_ptr<content::RenderProcessHost> render_process_host2; |
| 130 render_process_host2.reset(instance2->GetProcess()); |
| 131 ASSERT_TRUE(render_process_host2.get() != NULL); |
| 132 |
| 133 ProfileDestroyer::DestroyProfileWhenAppropriate(main_profile); |
| 134 EXPECT_EQ(main_profile, TestingOriginalDestructionProfile::living_instance_); |
| 135 render_process_host2.release()->Cleanup(); |
| 136 MessageLoop::current()->RunAllPending(); |
| 137 EXPECT_EQ(NULL, TestingOriginalDestructionProfile::living_instance_); |
| 138 } |
OLD | NEW |