blob: e55e87b1976a19a3d236f0fcab3f13c6bcfed299 [file] [log] [blame]
[email protected]9d4ca082011-05-30 16:39:411// 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]cdba46992011-06-07 11:51:398#include "base/platform_file.h"
[email protected]9d4ca082011-05-30 16:39:419#include "chrome/browser/history/history.h"
10#include "chrome/test/testing_profile.h"
11#include "testing/gtest/include/gtest/gtest.h"
[email protected]cdba46992011-06-07 11:51:3912#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]9d4ca082011-05-30 16:39:4117
18namespace {
19
[email protected]cdba46992011-06-07 11:51:3920const char kTestkOrigin1[] = "http://host1:1/";
21const char kTestkOrigin2[] = "http://host2:1/";
22const char kTestkOrigin3[] = "http://host3:1/";
23
24const GURL kOrigin1(kTestkOrigin1);
25const GURL kOrigin2(kTestkOrigin2);
26const GURL kOrigin3(kTestkOrigin3);
27
[email protected]f5f665fd62011-06-01 11:39:1828class BrowsingDataRemoverTester : public BrowsingDataRemover::Observer {
[email protected]9d4ca082011-05-30 16:39:4129 public:
[email protected]f5f665fd62011-06-01 11:39:1830 BrowsingDataRemoverTester() {}
31 virtual ~BrowsingDataRemoverTester() {}
32
33 void BlockUntilNotified() {
34 MessageLoop::current()->Run();
[email protected]9d4ca082011-05-30 16:39:4135 }
[email protected]9d4ca082011-05-30 16:39:4136
37 protected:
[email protected]f5f665fd62011-06-01 11:39:1838 // 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
51class 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]9d4ca082011-05-30 16:39:4159 // Returns true, if the given URL exists in the history service.
[email protected]f5f665fd62011-06-01 11:39:1860 bool HistoryContainsURL(const GURL& url) {
61 history_service_->QueryURL(
[email protected]9d4ca082011-05-30 16:39:4162 url,
63 true,
64 &consumer_,
[email protected]f5f665fd62011-06-01 11:39:1865 NewCallback(this, &RemoveHistoryTester::SaveResultAndQuit));
66 BlockUntilNotified();
[email protected]9d4ca082011-05-30 16:39:4167 return query_url_success_;
68 }
69
[email protected]f5f665fd62011-06-01 11:39:1870 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]9d4ca082011-05-30 16:39:4173 }
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]f5f665fd62011-06-01 11:39:1882 Notify();
[email protected]9d4ca082011-05-30 16:39:4183 }
84
[email protected]9d4ca082011-05-30 16:39:4185
[email protected]f5f665fd62011-06-01 11:39:1886 // For History requests.
[email protected]9d4ca082011-05-30 16:39:4187 CancelableRequestConsumer consumer_;
88 bool query_url_success_;
[email protected]f5f665fd62011-06-01 11:39:1889
90 // TestingProfile owns the history service; we shouldn't delete it.
91 HistoryService* history_service_;
92
93 DISALLOW_COPY_AND_ASSIGN(RemoveHistoryTester);
[email protected]9d4ca082011-05-30 16:39:4194};
95
[email protected]cdba46992011-06-07 11:51:3996class 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]f5f665fd62011-06-01 11:39:18159class BrowsingDataRemoverTest : public testing::Test {
160 public:
[email protected]cdba46992011-06-07 11:51:39161 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]9d4ca082011-05-30 16:39:41182
[email protected]f5f665fd62011-06-01 11:39:18183 void BlockUntilBrowsingDataRemoved(BrowsingDataRemover::TimePeriod period,
184 base::Time delete_end,
185 int remove_mask,
186 BrowsingDataRemoverTester* tester) {
[email protected]cdba46992011-06-07 11:51:39187 BrowsingDataRemover* remover = new BrowsingDataRemover(profile_.get(),
[email protected]f5f665fd62011-06-01 11:39:18188 period, delete_end);
189 remover->AddObserver(tester);
[email protected]9d4ca082011-05-30 16:39:41190
[email protected]f5f665fd62011-06-01 11:39:18191 // BrowsingDataRemover deletes itself when it completes.
192 remover->Remove(remove_mask);
193 tester->BlockUntilNotified();
194 }
[email protected]9d4ca082011-05-30 16:39:41195
[email protected]f5f665fd62011-06-01 11:39:18196 TestingProfile* GetProfile() {
[email protected]cdba46992011-06-07 11:51:39197 return profile_.get();
[email protected]f5f665fd62011-06-01 11:39:18198 }
[email protected]9d4ca082011-05-30 16:39:41199
[email protected]f5f665fd62011-06-01 11:39:18200 private:
[email protected]cdba46992011-06-07 11:51:39201 // 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]f5f665fd62011-06-01 11:39:18203 MessageLoopForUI message_loop_;
[email protected]cdba46992011-06-07 11:51:39204 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]f5f665fd62011-06-01 11:39:18210
211 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTest);
212};
213
214TEST_F(BrowsingDataRemoverTest, RemoveHistoryForever) {
215 scoped_ptr<RemoveHistoryTester> tester(
216 new RemoveHistoryTester(GetProfile()));
217
[email protected]cdba46992011-06-07 11:51:39218 tester->AddHistory(kOrigin1, base::Time::Now());
219 ASSERT_TRUE(tester->HistoryContainsURL(kOrigin1));
[email protected]f5f665fd62011-06-01 11:39:18220
221 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::EVERYTHING,
222 base::Time::Now(), BrowsingDataRemover::REMOVE_HISTORY, tester.get());
223
[email protected]cdba46992011-06-07 11:51:39224 EXPECT_FALSE(tester->HistoryContainsURL(kOrigin1));
[email protected]f5f665fd62011-06-01 11:39:18225}
226
227TEST_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]cdba46992011-06-07 11:51:39233 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]f5f665fd62011-06-01 11:39:18237
238 BlockUntilBrowsingDataRemoved(BrowsingDataRemover::LAST_HOUR,
239 base::Time::Now(), BrowsingDataRemover::REMOVE_HISTORY, tester.get());
240
[email protected]cdba46992011-06-07 11:51:39241 EXPECT_FALSE(tester->HistoryContainsURL(kOrigin1));
242 EXPECT_TRUE(tester->HistoryContainsURL(kOrigin2));
243}
244
245TEST_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]9d4ca082011-05-30 16:39:41265}
266
267} // namespace