OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 storage_policy_ = new quota::MockSpecialStoragePolicy; | 41 storage_policy_ = new quota::MockSpecialStoragePolicy; |
42 task_runner_ = new MockDomStorageTaskRunner( | 42 task_runner_ = new MockDomStorageTaskRunner( |
43 base::MessageLoopProxy::current()); | 43 base::MessageLoopProxy::current()); |
44 context_ = new DomStorageContext(temp_dir_.path(), | 44 context_ = new DomStorageContext(temp_dir_.path(), |
45 FilePath(), | 45 FilePath(), |
46 storage_policy_, | 46 storage_policy_, |
47 task_runner_); | 47 task_runner_); |
48 } | 48 } |
49 | 49 |
50 virtual void TearDown() { | 50 virtual void TearDown() { |
51 MessageLoop::current()->RunAllPending(); | 51 MessageLoop::current()->RunUntilIdle(); |
52 } | 52 } |
53 | 53 |
54 void VerifySingleOriginRemains(const GURL& origin) { | 54 void VerifySingleOriginRemains(const GURL& origin) { |
55 // Use a new instance to examine the contexts of temp_dir_. | 55 // Use a new instance to examine the contexts of temp_dir_. |
56 scoped_refptr<DomStorageContext> context = | 56 scoped_refptr<DomStorageContext> context = |
57 new DomStorageContext(temp_dir_.path(), FilePath(), NULL, NULL); | 57 new DomStorageContext(temp_dir_.path(), FilePath(), NULL, NULL); |
58 std::vector<LocalStorageUsageInfo> infos; | 58 std::vector<LocalStorageUsageInfo> infos; |
59 context->GetLocalStorageUsage(&infos, kDontIncludeFileInfo); | 59 context->GetLocalStorageUsage(&infos, kDontIncludeFileInfo); |
60 ASSERT_EQ(1u, infos.size()); | 60 ASSERT_EQ(1u, infos.size()); |
61 EXPECT_EQ(origin, infos[0].origin); | 61 EXPECT_EQ(origin, infos[0].origin); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 context_->GetLocalStorageUsage(&infos, kDoIncludeFileInfo); | 97 context_->GetLocalStorageUsage(&infos, kDoIncludeFileInfo); |
98 EXPECT_TRUE(infos.empty()); | 98 EXPECT_TRUE(infos.empty()); |
99 | 99 |
100 // Put some data into local storage and shutdown the context | 100 // Put some data into local storage and shutdown the context |
101 // to ensure data is written to disk. | 101 // to ensure data is written to disk. |
102 NullableString16 old_value; | 102 NullableString16 old_value; |
103 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> | 103 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
104 OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); | 104 OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); |
105 context_->Shutdown(); | 105 context_->Shutdown(); |
106 context_ = NULL; | 106 context_ = NULL; |
107 MessageLoop::current()->RunAllPending(); | 107 MessageLoop::current()->RunUntilIdle(); |
108 | 108 |
109 // Create a new context that points to the same directory, see that | 109 // Create a new context that points to the same directory, see that |
110 // it knows about the origin that we stored data for. | 110 // it knows about the origin that we stored data for. |
111 context_ = new DomStorageContext(temp_dir_.path(), FilePath(), NULL, NULL); | 111 context_ = new DomStorageContext(temp_dir_.path(), FilePath(), NULL, NULL); |
112 context_->GetLocalStorageUsage(&infos, kDontIncludeFileInfo); | 112 context_->GetLocalStorageUsage(&infos, kDontIncludeFileInfo); |
113 EXPECT_EQ(1u, infos.size()); | 113 EXPECT_EQ(1u, infos.size()); |
114 EXPECT_EQ(kOrigin, infos[0].origin); | 114 EXPECT_EQ(kOrigin, infos[0].origin); |
115 EXPECT_EQ(0u, infos[0].data_size); | 115 EXPECT_EQ(0u, infos[0].data_size); |
116 EXPECT_EQ(base::Time(), infos[0].last_modified); | 116 EXPECT_EQ(base::Time(), infos[0].last_modified); |
117 infos.clear(); | 117 infos.clear(); |
(...skipping 11 matching lines...) Expand all Loading... |
129 // Store data for a normal and a session-only origin and then | 129 // Store data for a normal and a session-only origin and then |
130 // invoke Shutdown() which should delete data for session-only | 130 // invoke Shutdown() which should delete data for session-only |
131 // origins. | 131 // origins. |
132 NullableString16 old_value; | 132 NullableString16 old_value; |
133 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> | 133 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
134 OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); | 134 OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); |
135 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> | 135 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
136 OpenStorageArea(kSessionOnlyOrigin)->SetItem(kKey, kValue, &old_value)); | 136 OpenStorageArea(kSessionOnlyOrigin)->SetItem(kKey, kValue, &old_value)); |
137 context_->Shutdown(); | 137 context_->Shutdown(); |
138 context_ = NULL; | 138 context_ = NULL; |
139 MessageLoop::current()->RunAllPending(); | 139 MessageLoop::current()->RunUntilIdle(); |
140 | 140 |
141 // Verify that the session-only origin data is gone. | 141 // Verify that the session-only origin data is gone. |
142 VerifySingleOriginRemains(kOrigin); | 142 VerifySingleOriginRemains(kOrigin); |
143 } | 143 } |
144 | 144 |
145 TEST_F(DomStorageContextTest, SetForceKeepSessionState) { | 145 TEST_F(DomStorageContextTest, SetForceKeepSessionState) { |
146 const GURL kSessionOnlyOrigin("http://www.sessiononly.com/"); | 146 const GURL kSessionOnlyOrigin("http://www.sessiononly.com/"); |
147 storage_policy_->AddSessionOnly(kSessionOnlyOrigin); | 147 storage_policy_->AddSessionOnly(kSessionOnlyOrigin); |
148 | 148 |
149 // Store data for a session-only origin, setup to save session data, then | 149 // Store data for a session-only origin, setup to save session data, then |
150 // shutdown. | 150 // shutdown. |
151 NullableString16 old_value; | 151 NullableString16 old_value; |
152 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> | 152 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
153 OpenStorageArea(kSessionOnlyOrigin)->SetItem(kKey, kValue, &old_value)); | 153 OpenStorageArea(kSessionOnlyOrigin)->SetItem(kKey, kValue, &old_value)); |
154 context_->SetForceKeepSessionState(); // Should override clear behavior. | 154 context_->SetForceKeepSessionState(); // Should override clear behavior. |
155 context_->Shutdown(); | 155 context_->Shutdown(); |
156 context_ = NULL; | 156 context_ = NULL; |
157 MessageLoop::current()->RunAllPending(); | 157 MessageLoop::current()->RunUntilIdle(); |
158 | 158 |
159 VerifySingleOriginRemains(kSessionOnlyOrigin); | 159 VerifySingleOriginRemains(kSessionOnlyOrigin); |
160 } | 160 } |
161 | 161 |
162 TEST_F(DomStorageContextTest, PersistentIds) { | 162 TEST_F(DomStorageContextTest, PersistentIds) { |
163 const int kFirstSessionStorageNamespaceId = 1; | 163 const int kFirstSessionStorageNamespaceId = 1; |
164 const std::string kPersistentId = "persistent"; | 164 const std::string kPersistentId = "persistent"; |
165 context_->CreateSessionNamespace(kFirstSessionStorageNamespaceId, | 165 context_->CreateSessionNamespace(kFirstSessionStorageNamespaceId, |
166 kPersistentId); | 166 kPersistentId); |
167 DomStorageNamespace* dom_namespace = | 167 DomStorageNamespace* dom_namespace = |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 DomStorageArea* area = dom_namespace->OpenStorageArea(kOrigin); | 207 DomStorageArea* area = dom_namespace->OpenStorageArea(kOrigin); |
208 const string16 kKey(ASCIIToUTF16("foo")); | 208 const string16 kKey(ASCIIToUTF16("foo")); |
209 const string16 kValue(ASCIIToUTF16("bar")); | 209 const string16 kValue(ASCIIToUTF16("bar")); |
210 NullableString16 old_nullable_value; | 210 NullableString16 old_nullable_value; |
211 area->SetItem(kKey, kValue, &old_nullable_value); | 211 area->SetItem(kKey, kValue, &old_nullable_value); |
212 dom_namespace->CloseStorageArea(area); | 212 dom_namespace->CloseStorageArea(area); |
213 | 213 |
214 // Destroy and recreate the DomStorageContext. | 214 // Destroy and recreate the DomStorageContext. |
215 context_->Shutdown(); | 215 context_->Shutdown(); |
216 context_ = NULL; | 216 context_ = NULL; |
217 MessageLoop::current()->RunAllPending(); | 217 MessageLoop::current()->RunUntilIdle(); |
218 context_ = new DomStorageContext(temp_dir_.path(), | 218 context_ = new DomStorageContext(temp_dir_.path(), |
219 temp_dir_.path(), | 219 temp_dir_.path(), |
220 storage_policy_, | 220 storage_policy_, |
221 task_runner_); | 221 task_runner_); |
222 context_->SetSaveSessionStorageOnDisk(); | 222 context_->SetSaveSessionStorageOnDisk(); |
223 | 223 |
224 // Read the data back. | 224 // Read the data back. |
225 context_->CreateSessionNamespace(kSessionStorageNamespaceId, | 225 context_->CreateSessionNamespace(kSessionStorageNamespaceId, |
226 kPersistentId); | 226 kPersistentId); |
227 dom_namespace = context_->GetStorageNamespace(kSessionStorageNamespaceId); | 227 dom_namespace = context_->GetStorageNamespace(kSessionStorageNamespaceId); |
228 area = dom_namespace->OpenStorageArea(kOrigin); | 228 area = dom_namespace->OpenStorageArea(kOrigin); |
229 NullableString16 read_value; | 229 NullableString16 read_value; |
230 read_value = area->GetItem(kKey); | 230 read_value = area->GetItem(kKey); |
231 EXPECT_EQ(kValue, read_value.string()); | 231 EXPECT_EQ(kValue, read_value.string()); |
232 dom_namespace->CloseStorageArea(area); | 232 dom_namespace->CloseStorageArea(area); |
233 | 233 |
234 SessionStorageUsageInfo info; | 234 SessionStorageUsageInfo info; |
235 info.origin = kOrigin; | 235 info.origin = kOrigin; |
236 info.persistent_namespace_id = kPersistentId; | 236 info.persistent_namespace_id = kPersistentId; |
237 context_->DeleteSessionStorage(info); | 237 context_->DeleteSessionStorage(info); |
238 | 238 |
239 // Destroy and recreate again. | 239 // Destroy and recreate again. |
240 context_->Shutdown(); | 240 context_->Shutdown(); |
241 context_ = NULL; | 241 context_ = NULL; |
242 MessageLoop::current()->RunAllPending(); | 242 MessageLoop::current()->RunUntilIdle(); |
243 context_ = new DomStorageContext(temp_dir_.path(), | 243 context_ = new DomStorageContext(temp_dir_.path(), |
244 temp_dir_.path(), | 244 temp_dir_.path(), |
245 storage_policy_, | 245 storage_policy_, |
246 task_runner_); | 246 task_runner_); |
247 context_->SetSaveSessionStorageOnDisk(); | 247 context_->SetSaveSessionStorageOnDisk(); |
248 | 248 |
249 // Now there should be no data. | 249 // Now there should be no data. |
250 context_->CreateSessionNamespace(kSessionStorageNamespaceId, | 250 context_->CreateSessionNamespace(kSessionStorageNamespaceId, |
251 kPersistentId); | 251 kPersistentId); |
252 dom_namespace = context_->GetStorageNamespace(kSessionStorageNamespaceId); | 252 dom_namespace = context_->GetStorageNamespace(kSessionStorageNamespaceId); |
253 area = dom_namespace->OpenStorageArea(kOrigin); | 253 area = dom_namespace->OpenStorageArea(kOrigin); |
254 read_value = area->GetItem(kKey); | 254 read_value = area->GetItem(kKey); |
255 EXPECT_TRUE(read_value.is_null()); | 255 EXPECT_TRUE(read_value.is_null()); |
256 dom_namespace->CloseStorageArea(area); | 256 dom_namespace->CloseStorageArea(area); |
257 context_->Shutdown(); | 257 context_->Shutdown(); |
258 context_ = NULL; | 258 context_ = NULL; |
259 MessageLoop::current()->RunAllPending(); | 259 MessageLoop::current()->RunUntilIdle(); |
260 } | 260 } |
261 | 261 |
262 } // namespace dom_storage | 262 } // namespace dom_storage |
OLD | NEW |