[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 1 | // Copyright (c) 2011 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/browsing_data_remover.h" |
| 6 | |
| 7 | #include "base/message_loop.h" |
[email protected] | cdba4699 | 2011-06-07 11:51:39 | [diff] [blame^] | 8 | #include "base/platform_file.h" |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 9 | #include "chrome/browser/history/history.h" |
| 10 | #include "chrome/test/testing_profile.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | cdba4699 | 2011-06-07 11:51:39 | [diff] [blame^] | 12 | #include "webkit/fileapi/file_system_context.h" |
| 13 | #include "webkit/fileapi/file_system_operation_context.h" |
| 14 | #include "webkit/fileapi/file_system_file_util.h" |
| 15 | #include "webkit/fileapi/file_system_path_manager.h" |
| 16 | #include "webkit/fileapi/sandbox_mount_point_provider.h" |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 17 | |
| 18 | namespace { |
| 19 | |
[email protected] | cdba4699 | 2011-06-07 11:51:39 | [diff] [blame^] | 20 | const char kTestkOrigin1[] = "http://host1:1/"; |
| 21 | const char kTestkOrigin2[] = "http://host2:1/"; |
| 22 | const char kTestkOrigin3[] = "http://host3:1/"; |
| 23 | |
| 24 | const GURL kOrigin1(kTestkOrigin1); |
| 25 | const GURL kOrigin2(kTestkOrigin2); |
| 26 | const GURL kOrigin3(kTestkOrigin3); |
| 27 | |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 28 | class BrowsingDataRemoverTester : public BrowsingDataRemover::Observer { |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 29 | public: |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 30 | BrowsingDataRemoverTester() {} |
| 31 | virtual ~BrowsingDataRemoverTester() {} |
| 32 | |
| 33 | void BlockUntilNotified() { |
| 34 | MessageLoop::current()->Run(); |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 35 | } |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 36 | |
| 37 | protected: |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 38 | // BrowsingDataRemover::Observer implementation. |
| 39 | virtual void OnBrowsingDataRemoverDone() { |
| 40 | Notify(); |
| 41 | } |
| 42 | |
| 43 | void Notify() { |
| 44 | MessageLoop::current()->Quit(); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTester); |
| 49 | }; |
| 50 | |
| 51 | class RemoveHistoryTester : public BrowsingDataRemoverTester { |
| 52 | public: |
| 53 | explicit RemoveHistoryTester(TestingProfile* profile) |
| 54 | : query_url_success_(false) { |
| 55 | profile->CreateHistoryService(true, false); |
| 56 | history_service_ = profile->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 57 | } |
| 58 | |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 59 | // Returns true, if the given URL exists in the history service. |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 60 | bool HistoryContainsURL(const GURL& url) { |
| 61 | history_service_->QueryURL( |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 62 | url, |
| 63 | true, |
| 64 | &consumer_, |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 65 | NewCallback(this, &RemoveHistoryTester::SaveResultAndQuit)); |
| 66 | BlockUntilNotified(); |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 67 | return query_url_success_; |
| 68 | } |
| 69 | |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 70 | void AddHistory(const GURL& url, base::Time time) { |
| 71 | history_service_->AddPage(url, time, NULL, 0, GURL(), PageTransition::LINK, |
| 72 | history::RedirectList(), history::SOURCE_BROWSED, false); |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | private: |
| 76 | // Callback for HistoryService::QueryURL. |
| 77 | void SaveResultAndQuit(HistoryService::Handle, |
| 78 | bool success, |
| 79 | const history::URLRow*, |
| 80 | history::VisitVector*) { |
| 81 | query_url_success_ = success; |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 82 | Notify(); |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 83 | } |
| 84 | |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 85 | |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 86 | // For History requests. |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 87 | CancelableRequestConsumer consumer_; |
| 88 | bool query_url_success_; |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 89 | |
| 90 | // TestingProfile owns the history service; we shouldn't delete it. |
| 91 | HistoryService* history_service_; |
| 92 | |
| 93 | DISALLOW_COPY_AND_ASSIGN(RemoveHistoryTester); |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 94 | }; |
| 95 | |
[email protected] | cdba4699 | 2011-06-07 11:51:39 | [diff] [blame^] | 96 | class RemoveFileSystemTester : public BrowsingDataRemoverTester { |
| 97 | public: |
| 98 | explicit RemoveFileSystemTester() {} |
| 99 | |
| 100 | void FindFileSystemPathCallback(bool success, |
| 101 | const FilePath& path, |
| 102 | const std::string& name) { |
| 103 | found_file_system_ = success; |
| 104 | Notify(); |
| 105 | } |
| 106 | |
| 107 | bool FileSystemContainsOriginAndType(const GURL& origin, |
| 108 | fileapi::FileSystemType type) { |
| 109 | sandbox_->ValidateFileSystemRootAndGetURL( |
| 110 | origin, type, false, NewCallback(this, |
| 111 | &RemoveFileSystemTester::FindFileSystemPathCallback)); |
| 112 | BlockUntilNotified(); |
| 113 | return found_file_system_; |
| 114 | } |
| 115 | |
| 116 | virtual void PopulateTestFileSystemData(TestingProfile* profile) { |
| 117 | // Set up kOrigin1 with a temporary file system, kOrigin2 with a persistent |
| 118 | // file system, and kOrigin3 with both. |
| 119 | sandbox_ = profile->GetFileSystemContext()->path_manager()-> |
| 120 | sandbox_provider(); |
| 121 | |
| 122 | CreateDirectoryForOriginAndType(kOrigin1, |
| 123 | fileapi::kFileSystemTypeTemporary); |
| 124 | CreateDirectoryForOriginAndType(kOrigin2, |
| 125 | fileapi::kFileSystemTypePersistent); |
| 126 | CreateDirectoryForOriginAndType(kOrigin3, |
| 127 | fileapi::kFileSystemTypeTemporary); |
| 128 | CreateDirectoryForOriginAndType(kOrigin3, |
| 129 | fileapi::kFileSystemTypePersistent); |
| 130 | |
| 131 | EXPECT_FALSE(FileSystemContainsOriginAndType(kOrigin1, |
| 132 | fileapi::kFileSystemTypePersistent)); |
| 133 | EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin1, |
| 134 | fileapi::kFileSystemTypeTemporary)); |
| 135 | EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin2, |
| 136 | fileapi::kFileSystemTypePersistent)); |
| 137 | EXPECT_FALSE(FileSystemContainsOriginAndType(kOrigin2, |
| 138 | fileapi::kFileSystemTypeTemporary)); |
| 139 | EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin3, |
| 140 | fileapi::kFileSystemTypePersistent)); |
| 141 | EXPECT_TRUE(FileSystemContainsOriginAndType(kOrigin3, |
| 142 | fileapi::kFileSystemTypeTemporary)); |
| 143 | } |
| 144 | |
| 145 | void CreateDirectoryForOriginAndType(const GURL& origin, |
| 146 | fileapi::FileSystemType type) { |
| 147 | FilePath target = sandbox_->ValidateFileSystemRootAndGetPathOnFileThread( |
| 148 | origin, type, FilePath(), true); |
| 149 | EXPECT_TRUE(file_util::DirectoryExists(target)); |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | fileapi::SandboxMountPointProvider* sandbox_; |
| 154 | bool found_file_system_; |
| 155 | |
| 156 | DISALLOW_COPY_AND_ASSIGN(RemoveFileSystemTester); |
| 157 | }; |
| 158 | |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 159 | class BrowsingDataRemoverTest : public testing::Test { |
| 160 | public: |
[email protected] | cdba4699 | 2011-06-07 11:51:39 | [diff] [blame^] | 161 | BrowsingDataRemoverTest() |
| 162 | : ui_thread_(BrowserThread::UI, &message_loop_), |
| 163 | db_thread_(BrowserThread::DB, &message_loop_), |
| 164 | webkit_thread_(BrowserThread::WEBKIT, &message_loop_), |
| 165 | file_thread_(BrowserThread::FILE, &message_loop_), |
| 166 | io_thread_(BrowserThread::IO, &message_loop_), |
| 167 | profile_(new TestingProfile()) { |
| 168 | } |
| 169 | |
| 170 | virtual ~BrowsingDataRemoverTest() { |
| 171 | } |
| 172 | |
| 173 | void TearDown() { |
| 174 | // TestingProfile contains a WebKitContext. WebKitContext's destructor |
| 175 | // posts a message to the WEBKIT thread to delete some of its member |
| 176 | // variables. We need to ensure that the profile is destroyed, and that |
| 177 | // the message loop is cleared out, before destroying the threads and loop. |
| 178 | // Otherwise we leak memory. |
| 179 | profile_.reset(); |
| 180 | message_loop_.RunAllPending(); |
| 181 | } |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 182 | |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 183 | void BlockUntilBrowsingDataRemoved(BrowsingDataRemover::TimePeriod period, |
| 184 | base::Time delete_end, |
| 185 | int remove_mask, |
| 186 | BrowsingDataRemoverTester* tester) { |
[email protected] | cdba4699 | 2011-06-07 11:51:39 | [diff] [blame^] | 187 | BrowsingDataRemover* remover = new BrowsingDataRemover(profile_.get(), |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 188 | period, delete_end); |
| 189 | remover->AddObserver(tester); |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 190 | |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 191 | // BrowsingDataRemover deletes itself when it completes. |
| 192 | remover->Remove(remove_mask); |
| 193 | tester->BlockUntilNotified(); |
| 194 | } |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 195 | |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 196 | TestingProfile* GetProfile() { |
[email protected] | cdba4699 | 2011-06-07 11:51:39 | [diff] [blame^] | 197 | return profile_.get(); |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 198 | } |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 199 | |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 200 | private: |
[email protected] | cdba4699 | 2011-06-07 11:51:39 | [diff] [blame^] | 201 | // message_loop_, as well as all the threads associated with it must be |
| 202 | // defined before profile_ to prevent explosions. Oh how I love C++. |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 203 | MessageLoopForUI message_loop_; |
[email protected] | cdba4699 | 2011-06-07 11:51:39 | [diff] [blame^] | 204 | BrowserThread ui_thread_; |
| 205 | BrowserThread db_thread_; |
| 206 | BrowserThread webkit_thread_; |
| 207 | BrowserThread file_thread_; |
| 208 | BrowserThread io_thread_; |
| 209 | scoped_ptr<TestingProfile> profile_; |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 210 | |
| 211 | DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTest); |
| 212 | }; |
| 213 | |
| 214 | TEST_F(BrowsingDataRemoverTest, RemoveHistoryForever) { |
| 215 | scoped_ptr<RemoveHistoryTester> tester( |
| 216 | new RemoveHistoryTester(GetProfile())); |
| 217 | |
[email protected] | cdba4699 | 2011-06-07 11:51:39 | [diff] [blame^] | 218 | tester->AddHistory(kOrigin1, base::Time::Now()); |
| 219 | ASSERT_TRUE(tester->HistoryContainsURL(kOrigin1)); |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 220 | |
| 221 | BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING, |
| 222 | base::Time::Now(), BrowsingDataRemover::REMOVE_HISTORY, tester.get()); |
| 223 | |
[email protected] | cdba4699 | 2011-06-07 11:51:39 | [diff] [blame^] | 224 | EXPECT_FALSE(tester->HistoryContainsURL(kOrigin1)); |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | TEST_F(BrowsingDataRemoverTest, RemoveHistoryForLastHour) { |
| 228 | scoped_ptr<RemoveHistoryTester> tester( |
| 229 | new RemoveHistoryTester(GetProfile())); |
| 230 | |
| 231 | base::Time two_hours_ago = base::Time::Now() - base::TimeDelta::FromHours(2); |
| 232 | |
[email protected] | cdba4699 | 2011-06-07 11:51:39 | [diff] [blame^] | 233 | tester->AddHistory(kOrigin1, base::Time::Now()); |
| 234 | tester->AddHistory(kOrigin2, two_hours_ago); |
| 235 | ASSERT_TRUE(tester->HistoryContainsURL(kOrigin1)); |
| 236 | ASSERT_TRUE(tester->HistoryContainsURL(kOrigin2)); |
[email protected] | f5f665fd6 | 2011-06-01 11:39:18 | [diff] [blame] | 237 | |
| 238 | BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR, |
| 239 | base::Time::Now(), BrowsingDataRemover::REMOVE_HISTORY, tester.get()); |
| 240 | |
[email protected] | cdba4699 | 2011-06-07 11:51:39 | [diff] [blame^] | 241 | EXPECT_FALSE(tester->HistoryContainsURL(kOrigin1)); |
| 242 | EXPECT_TRUE(tester->HistoryContainsURL(kOrigin2)); |
| 243 | } |
| 244 | |
| 245 | TEST_F(BrowsingDataRemoverTest, RemoveFileSystemsForever) { |
| 246 | scoped_ptr<RemoveFileSystemTester> tester(new RemoveFileSystemTester()); |
| 247 | |
| 248 | tester->PopulateTestFileSystemData(GetProfile()); |
| 249 | |
| 250 | BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING, |
| 251 | base::Time::Now(), BrowsingDataRemover::REMOVE_COOKIES, tester.get()); |
| 252 | |
| 253 | EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin1, |
| 254 | fileapi::kFileSystemTypePersistent)); |
| 255 | EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin1, |
| 256 | fileapi::kFileSystemTypeTemporary)); |
| 257 | EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin2, |
| 258 | fileapi::kFileSystemTypePersistent)); |
| 259 | EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin2, |
| 260 | fileapi::kFileSystemTypeTemporary)); |
| 261 | EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin3, |
| 262 | fileapi::kFileSystemTypePersistent)); |
| 263 | EXPECT_FALSE(tester->FileSystemContainsOriginAndType(kOrigin3, |
| 264 | fileapi::kFileSystemTypeTemporary)); |
[email protected] | 9d4ca08 | 2011-05-30 16:39:41 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | } // namespace |