OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <map> | 5 #include <map> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 weak_factory_(this), | 41 weak_factory_(this), |
42 message_loop_(base::MessageLoop::TYPE_IO), | 42 message_loop_(base::MessageLoop::TYPE_IO), |
43 db_thread_(BrowserThread::DB, &message_loop_), | 43 db_thread_(BrowserThread::DB, &message_loop_), |
44 webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &message_loop_), | 44 webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &message_loop_), |
45 file_thread_(BrowserThread::FILE, &message_loop_), | 45 file_thread_(BrowserThread::FILE, &message_loop_), |
46 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING, | 46 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING, |
47 &message_loop_), | 47 &message_loop_), |
48 io_thread_(BrowserThread::IO, &message_loop_) { | 48 io_thread_(BrowserThread::IO, &message_loop_) { |
49 browser_context_.reset(new TestBrowserContext()); | 49 browser_context_.reset(new TestBrowserContext()); |
50 idb_context_ = static_cast<IndexedDBContextImpl*>( | 50 idb_context_ = static_cast<IndexedDBContextImpl*>( |
51 BrowserContext::GetDefaultStoragePartition(browser_context_.get()) | 51 BrowserContext::GetDefaultStoragePartition(browser_context_.get())-> |
52 ->GetIndexedDBContext()); | 52 GetIndexedDBContext()); |
53 message_loop_.RunUntilIdle(); | 53 message_loop_.RunUntilIdle(); |
54 setup_temp_dir(); | 54 setup_temp_dir(); |
55 } | 55 } |
56 void setup_temp_dir() { | 56 void setup_temp_dir() { |
57 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 57 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
58 base::FilePath indexeddb_dir = | 58 base::FilePath indexeddb_dir = temp_dir_.path().Append( |
59 temp_dir_.path().Append(IndexedDBContextImpl::kIndexedDBDirectory); | 59 IndexedDBContextImpl::kIndexedDBDirectory); |
60 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir)); | 60 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir)); |
61 idb_context()->set_data_path_for_testing(indexeddb_dir); | 61 idb_context()->set_data_path_for_testing(indexeddb_dir); |
62 } | 62 } |
63 | 63 |
64 virtual ~IndexedDBQuotaClientTest() { | 64 virtual ~IndexedDBQuotaClientTest() { |
65 // IndexedDBContext needs to be destructed on | 65 // IndexedDBContext needs to be destructed on |
66 // BrowserThread::WEBKIT_DEPRECATED, which is also a member variable of this | 66 // BrowserThread::WEBKIT_DEPRECATED, which is also a member variable of this |
67 // class. Cause IndexedDBContext's destruction now to ensure that it | 67 // class. Cause IndexedDBContext's destruction now to ensure that it |
68 // doesn't outlive BrowserThread::WEBKIT_DEPRECATED. | 68 // doesn't outlive BrowserThread::WEBKIT_DEPRECATED. |
69 idb_context_ = NULL; | 69 idb_context_ = NULL; |
70 browser_context_.reset(); | 70 browser_context_.reset(); |
71 base::MessageLoop::current()->RunUntilIdle(); | 71 base::MessageLoop::current()->RunUntilIdle(); |
72 } | 72 } |
73 | 73 |
74 int64 GetOriginUsage(quota::QuotaClient* client, | 74 int64 GetOriginUsage( |
75 const GURL& origin, | 75 quota::QuotaClient* client, |
76 quota::StorageType type) { | 76 const GURL& origin, |
| 77 quota::StorageType type) { |
77 usage_ = -1; | 78 usage_ = -1; |
78 client->GetOriginUsage( | 79 client->GetOriginUsage( |
79 origin, | 80 origin, type, |
80 type, | |
81 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginUsageComplete, | 81 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginUsageComplete, |
82 weak_factory_.GetWeakPtr())); | 82 weak_factory_.GetWeakPtr())); |
83 base::MessageLoop::current()->RunUntilIdle(); | 83 base::MessageLoop::current()->RunUntilIdle(); |
84 EXPECT_GT(usage_, -1); | 84 EXPECT_GT(usage_, -1); |
85 return usage_; | 85 return usage_; |
86 } | 86 } |
87 | 87 |
88 const std::set<GURL>& GetOriginsForType(quota::QuotaClient* client, | 88 const std::set<GURL>& GetOriginsForType( |
89 quota::StorageType type) { | 89 quota::QuotaClient* client, |
| 90 quota::StorageType type) { |
90 origins_.clear(); | 91 origins_.clear(); |
91 type_ = quota::kStorageTypeTemporary; | 92 type_ = quota::kStorageTypeTemporary; |
92 client->GetOriginsForType( | 93 client->GetOriginsForType( |
93 type, | 94 type, |
94 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, | 95 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, |
95 weak_factory_.GetWeakPtr())); | 96 weak_factory_.GetWeakPtr())); |
96 base::MessageLoop::current()->RunUntilIdle(); | 97 base::MessageLoop::current()->RunUntilIdle(); |
97 return origins_; | 98 return origins_; |
98 } | 99 } |
99 | 100 |
100 const std::set<GURL>& GetOriginsForHost(quota::QuotaClient* client, | 101 const std::set<GURL>& GetOriginsForHost( |
101 quota::StorageType type, | 102 quota::QuotaClient* client, |
102 const std::string& host) { | 103 quota::StorageType type, |
| 104 const std::string& host) { |
103 origins_.clear(); | 105 origins_.clear(); |
104 type_ = quota::kStorageTypeTemporary; | 106 type_ = quota::kStorageTypeTemporary; |
105 client->GetOriginsForHost( | 107 client->GetOriginsForHost( |
106 type, | 108 type, host, |
107 host, | |
108 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, | 109 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, |
109 weak_factory_.GetWeakPtr())); | 110 weak_factory_.GetWeakPtr())); |
110 base::MessageLoop::current()->RunUntilIdle(); | 111 base::MessageLoop::current()->RunUntilIdle(); |
111 return origins_; | 112 return origins_; |
112 } | 113 } |
113 | 114 |
114 quota::QuotaStatusCode DeleteOrigin(quota::QuotaClient* client, | 115 quota::QuotaStatusCode DeleteOrigin(quota::QuotaClient* client, |
115 const GURL& origin_url) { | 116 const GURL& origin_url) { |
116 delete_status_ = quota::kQuotaStatusUnknown; | 117 delete_status_ = quota::kQuotaStatusUnknown; |
117 client->DeleteOriginData( | 118 client->DeleteOriginData( |
118 origin_url, | 119 origin_url, kTemp, |
119 kTemp, | |
120 base::Bind(&IndexedDBQuotaClientTest::OnDeleteOriginComplete, | 120 base::Bind(&IndexedDBQuotaClientTest::OnDeleteOriginComplete, |
121 weak_factory_.GetWeakPtr())); | 121 weak_factory_.GetWeakPtr())); |
122 base::MessageLoop::current()->RunUntilIdle(); | 122 base::MessageLoop::current()->RunUntilIdle(); |
123 return delete_status_; | 123 return delete_status_; |
124 } | 124 } |
125 | 125 |
126 IndexedDBContextImpl* idb_context() { return idb_context_.get(); } | 126 IndexedDBContextImpl* idb_context() { return idb_context_.get(); } |
127 | 127 |
128 void SetFileSizeTo(const base::FilePath& path, int size) { | 128 void SetFileSizeTo(const base::FilePath& path, int size) { |
129 std::string junk(size, 'a'); | 129 std::string junk(size, 'a'); |
130 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); | 130 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); |
131 } | 131 } |
132 | 132 |
133 void AddFakeIndexedDB(const GURL& origin, int size) { | 133 void AddFakeIndexedDB(const GURL& origin, int size) { |
134 base::FilePath file_path_origin = idb_context()->GetFilePathForTesting( | 134 base::FilePath file_path_origin = idb_context()->GetFilePathForTesting( |
135 webkit_base::GetOriginIdentifierFromURL(origin)); | 135 webkit_base::GetOriginIdentifierFromURL(origin)); |
136 if (!file_util::CreateDirectory(file_path_origin)) { | 136 if (!file_util::CreateDirectory(file_path_origin)) { |
137 LOG(ERROR) << "failed to file_util::CreateDirectory " | 137 LOG(ERROR) << "failed to file_util::CreateDirectory " |
138 << file_path_origin.value(); | 138 << file_path_origin.value(); |
139 } | 139 } |
140 file_path_origin = file_path_origin.Append(FILE_PATH_LITERAL("fake_file")); | 140 file_path_origin = file_path_origin.Append(FILE_PATH_LITERAL("fake_file")); |
141 SetFileSizeTo(file_path_origin, size); | 141 SetFileSizeTo(file_path_origin, size); |
142 idb_context()->ResetCaches(); | 142 idb_context()->ResetCaches(); |
143 } | 143 } |
144 | 144 |
145 private: | 145 private: |
146 void OnGetOriginUsageComplete(int64 usage) { usage_ = usage; } | 146 void OnGetOriginUsageComplete(int64 usage) { |
| 147 usage_ = usage; |
| 148 } |
147 | 149 |
148 void OnGetOriginsComplete(const std::set<GURL>& origins, | 150 void OnGetOriginsComplete(const std::set<GURL>& origins, |
149 quota::StorageType type) { | 151 quota::StorageType type) { |
150 origins_ = origins; | 152 origins_ = origins; |
151 type_ = type; | 153 type_ = type; |
152 } | 154 } |
153 | 155 |
154 void OnDeleteOriginComplete(quota::QuotaStatusCode code) { | 156 void OnDeleteOriginComplete(quota::QuotaStatusCode code) { |
155 delete_status_ = code; | 157 delete_status_ = code; |
156 } | 158 } |
157 | 159 |
158 base::ScopedTempDir temp_dir_; | 160 base::ScopedTempDir temp_dir_; |
159 int64 usage_; | 161 int64 usage_; |
160 std::set<GURL> origins_; | 162 std::set<GURL> origins_; |
161 quota::StorageType type_; | 163 quota::StorageType type_; |
162 scoped_refptr<IndexedDBContextImpl> idb_context_; | 164 scoped_refptr<IndexedDBContextImpl> idb_context_; |
163 base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_; | 165 base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_; |
164 base::MessageLoop message_loop_; | 166 base::MessageLoop message_loop_; |
165 BrowserThreadImpl db_thread_; | 167 BrowserThreadImpl db_thread_; |
166 BrowserThreadImpl webkit_thread_; | 168 BrowserThreadImpl webkit_thread_; |
167 BrowserThreadImpl file_thread_; | 169 BrowserThreadImpl file_thread_; |
168 BrowserThreadImpl file_user_blocking_thread_; | 170 BrowserThreadImpl file_user_blocking_thread_; |
169 BrowserThreadImpl io_thread_; | 171 BrowserThreadImpl io_thread_; |
170 scoped_ptr<TestBrowserContext> browser_context_; | 172 scoped_ptr<TestBrowserContext> browser_context_; |
171 quota::QuotaStatusCode delete_status_; | 173 quota::QuotaStatusCode delete_status_; |
172 }; | 174 }; |
173 | 175 |
174 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { | 176 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { |
175 IndexedDBQuotaClient client(base::MessageLoopProxy::current(), idb_context()); | 177 IndexedDBQuotaClient client( |
| 178 base::MessageLoopProxy::current(), |
| 179 idb_context()); |
176 | 180 |
177 AddFakeIndexedDB(kOriginA, 6); | 181 AddFakeIndexedDB(kOriginA, 6); |
178 AddFakeIndexedDB(kOriginB, 3); | 182 AddFakeIndexedDB(kOriginB, 3); |
179 EXPECT_EQ(6, GetOriginUsage(&client, kOriginA, kTemp)); | 183 EXPECT_EQ(6, GetOriginUsage(&client, kOriginA, kTemp)); |
180 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); | 184 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); |
181 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); | 185 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); |
182 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); | 186 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); |
183 | 187 |
184 AddFakeIndexedDB(kOriginA, 1000); | 188 AddFakeIndexedDB(kOriginA, 1000); |
185 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); | 189 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); |
186 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); | 190 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); |
187 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); | 191 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); |
188 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); | 192 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); |
189 } | 193 } |
190 | 194 |
191 TEST_F(IndexedDBQuotaClientTest, GetOriginsForHost) { | 195 TEST_F(IndexedDBQuotaClientTest, GetOriginsForHost) { |
192 IndexedDBQuotaClient client(base::MessageLoopProxy::current(), idb_context()); | 196 IndexedDBQuotaClient client( |
| 197 base::MessageLoopProxy::current(), |
| 198 idb_context()); |
193 | 199 |
194 EXPECT_EQ(kOriginA.host(), kOriginB.host()); | 200 EXPECT_EQ(kOriginA.host(), kOriginB.host()); |
195 EXPECT_NE(kOriginA.host(), kOriginOther.host()); | 201 EXPECT_NE(kOriginA.host(), kOriginOther.host()); |
196 | 202 |
197 std::set<GURL> origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); | 203 std::set<GURL> origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); |
198 EXPECT_TRUE(origins.empty()); | 204 EXPECT_TRUE(origins.empty()); |
199 | 205 |
200 AddFakeIndexedDB(kOriginA, 1000); | 206 AddFakeIndexedDB(kOriginA, 1000); |
201 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); | 207 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); |
202 EXPECT_EQ(origins.size(), 1ul); | 208 EXPECT_EQ(origins.size(), 1ul); |
203 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); | 209 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
204 | 210 |
205 AddFakeIndexedDB(kOriginB, 1000); | 211 AddFakeIndexedDB(kOriginB, 1000); |
206 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); | 212 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); |
207 EXPECT_EQ(origins.size(), 2ul); | 213 EXPECT_EQ(origins.size(), 2ul); |
208 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); | 214 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
209 EXPECT_TRUE(origins.find(kOriginB) != origins.end()); | 215 EXPECT_TRUE(origins.find(kOriginB) != origins.end()); |
210 | 216 |
211 EXPECT_TRUE(GetOriginsForHost(&client, kPerm, kOriginA.host()).empty()); | 217 EXPECT_TRUE(GetOriginsForHost(&client, kPerm, kOriginA.host()).empty()); |
212 EXPECT_TRUE(GetOriginsForHost(&client, kTemp, kOriginOther.host()).empty()); | 218 EXPECT_TRUE(GetOriginsForHost(&client, kTemp, kOriginOther.host()).empty()); |
213 } | 219 } |
214 | 220 |
215 TEST_F(IndexedDBQuotaClientTest, GetOriginsForType) { | 221 TEST_F(IndexedDBQuotaClientTest, GetOriginsForType) { |
216 IndexedDBQuotaClient client(base::MessageLoopProxy::current(), idb_context()); | 222 IndexedDBQuotaClient client( |
| 223 base::MessageLoopProxy::current(), |
| 224 idb_context()); |
217 | 225 |
218 EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty()); | 226 EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty()); |
219 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); | 227 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); |
220 | 228 |
221 AddFakeIndexedDB(kOriginA, 1000); | 229 AddFakeIndexedDB(kOriginA, 1000); |
222 std::set<GURL> origins = GetOriginsForType(&client, kTemp); | 230 std::set<GURL> origins = GetOriginsForType(&client, kTemp); |
223 EXPECT_EQ(origins.size(), 1ul); | 231 EXPECT_EQ(origins.size(), 1ul); |
224 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); | 232 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
225 | 233 |
226 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); | 234 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); |
227 } | 235 } |
228 | 236 |
229 TEST_F(IndexedDBQuotaClientTest, DeleteOrigin) { | 237 TEST_F(IndexedDBQuotaClientTest, DeleteOrigin) { |
230 IndexedDBQuotaClient client(base::MessageLoopProxy::current(), idb_context()); | 238 IndexedDBQuotaClient client( |
| 239 base::MessageLoopProxy::current(), |
| 240 idb_context()); |
231 | 241 |
232 AddFakeIndexedDB(kOriginA, 1000); | 242 AddFakeIndexedDB(kOriginA, 1000); |
233 AddFakeIndexedDB(kOriginB, 50); | 243 AddFakeIndexedDB(kOriginB, 50); |
234 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); | 244 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); |
235 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); | 245 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); |
236 | 246 |
237 quota::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA); | 247 quota::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA); |
238 EXPECT_EQ(quota::kQuotaStatusOk, delete_status); | 248 EXPECT_EQ(quota::kQuotaStatusOk, delete_status); |
239 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp)); | 249 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp)); |
240 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); | 250 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); |
241 } | 251 } |
242 | 252 |
243 } // namespace content | 253 } // namespace content |
OLD | NEW |