OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 TEST_F(DOMStorageContextImplTest, UsageInfo) { | 95 TEST_F(DOMStorageContextImplTest, UsageInfo) { |
96 // Should be empty initially | 96 // Should be empty initially |
97 std::vector<LocalStorageUsageInfo> infos; | 97 std::vector<LocalStorageUsageInfo> infos; |
98 context_->GetLocalStorageUsage(&infos, kDontIncludeFileInfo); | 98 context_->GetLocalStorageUsage(&infos, kDontIncludeFileInfo); |
99 EXPECT_TRUE(infos.empty()); | 99 EXPECT_TRUE(infos.empty()); |
100 context_->GetLocalStorageUsage(&infos, kDoIncludeFileInfo); | 100 context_->GetLocalStorageUsage(&infos, kDoIncludeFileInfo); |
101 EXPECT_TRUE(infos.empty()); | 101 EXPECT_TRUE(infos.empty()); |
102 | 102 |
103 // Put some data into local storage and shutdown the context | 103 // Put some data into local storage and shutdown the context |
104 // to ensure data is written to disk. | 104 // to ensure data is written to disk. |
| 105 auto* area = context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 106 OpenStorageArea(kOrigin); |
105 base::NullableString16 old_value; | 107 base::NullableString16 old_value; |
106 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> | 108 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
107 OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); | 109 area->ScheduleImmediateCommit(); |
108 context_->Shutdown(); | 110 context_->Shutdown(); |
109 context_ = NULL; | 111 context_ = NULL; |
110 base::MessageLoop::current()->RunUntilIdle(); | 112 base::MessageLoop::current()->RunUntilIdle(); |
111 | 113 |
112 // Create a new context that points to the same directory, see that | 114 // Create a new context that points to the same directory, see that |
113 // it knows about the origin that we stored data for. | 115 // it knows about the origin that we stored data for. |
114 context_ = new DOMStorageContextImpl(temp_dir_.path(), base::FilePath(), | 116 context_ = new DOMStorageContextImpl(temp_dir_.path(), base::FilePath(), |
115 NULL, NULL); | 117 NULL, NULL); |
116 context_->GetLocalStorageUsage(&infos, kDontIncludeFileInfo); | 118 context_->GetLocalStorageUsage(&infos, kDontIncludeFileInfo); |
117 EXPECT_EQ(1u, infos.size()); | 119 EXPECT_EQ(1u, infos.size()); |
118 EXPECT_EQ(kOrigin, infos[0].origin); | 120 EXPECT_EQ(kOrigin, infos[0].origin); |
119 EXPECT_EQ(0u, infos[0].data_size); | 121 EXPECT_EQ(0u, infos[0].data_size); |
120 EXPECT_EQ(base::Time(), infos[0].last_modified); | 122 EXPECT_EQ(base::Time(), infos[0].last_modified); |
121 infos.clear(); | 123 infos.clear(); |
122 context_->GetLocalStorageUsage(&infos, kDoIncludeFileInfo); | 124 context_->GetLocalStorageUsage(&infos, kDoIncludeFileInfo); |
123 EXPECT_EQ(1u, infos.size()); | 125 EXPECT_EQ(1u, infos.size()); |
124 EXPECT_EQ(kOrigin, infos[0].origin); | 126 EXPECT_EQ(kOrigin, infos[0].origin); |
125 EXPECT_NE(0u, infos[0].data_size); | 127 EXPECT_NE(0u, infos[0].data_size); |
126 EXPECT_NE(base::Time(), infos[0].last_modified); | 128 EXPECT_NE(base::Time(), infos[0].last_modified); |
127 } | 129 } |
128 | 130 |
129 TEST_F(DOMStorageContextImplTest, SessionOnly) { | 131 TEST_F(DOMStorageContextImplTest, SessionOnly) { |
130 const GURL kSessionOnlyOrigin("http://www.sessiononly.com/"); | 132 const GURL kSessionOnlyOrigin("http://www.sessiononly.com/"); |
131 storage_policy_->AddSessionOnly(kSessionOnlyOrigin); | 133 storage_policy_->AddSessionOnly(kSessionOnlyOrigin); |
132 | 134 |
133 // Store data for a normal and a session-only origin and then | 135 // Store data for a normal and a session-only origin and then |
134 // invoke Shutdown() which should delete data for session-only | 136 // invoke Shutdown() which should delete data for session-only |
135 // origins. | 137 // origins. |
| 138 auto* dom_namespace = context_->GetStorageNamespace(kLocalStorageNamespaceId); |
136 base::NullableString16 old_value; | 139 base::NullableString16 old_value; |
137 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> | 140 auto* area = dom_namespace->OpenStorageArea(kOrigin); |
138 OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); | 141 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
139 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> | 142 area->ScheduleImmediateCommit(); |
140 OpenStorageArea(kSessionOnlyOrigin)->SetItem(kKey, kValue, &old_value)); | 143 auto* session_only_area = dom_namespace->OpenStorageArea(kSessionOnlyOrigin); |
| 144 EXPECT_TRUE(session_only_area->SetItem(kKey, kValue, &old_value)); |
| 145 session_only_area->ScheduleImmediateCommit(); |
141 context_->Shutdown(); | 146 context_->Shutdown(); |
142 context_ = NULL; | 147 context_ = NULL; |
143 base::MessageLoop::current()->RunUntilIdle(); | 148 base::MessageLoop::current()->RunUntilIdle(); |
144 | 149 |
145 // Verify that the session-only origin data is gone. | 150 // Verify that the session-only origin data is gone. |
146 VerifySingleOriginRemains(kOrigin); | 151 VerifySingleOriginRemains(kOrigin); |
147 } | 152 } |
148 | 153 |
149 TEST_F(DOMStorageContextImplTest, SetForceKeepSessionState) { | 154 TEST_F(DOMStorageContextImplTest, SetForceKeepSessionState) { |
150 const GURL kSessionOnlyOrigin("http://www.sessiononly.com/"); | 155 const GURL kSessionOnlyOrigin("http://www.sessiononly.com/"); |
151 storage_policy_->AddSessionOnly(kSessionOnlyOrigin); | 156 storage_policy_->AddSessionOnly(kSessionOnlyOrigin); |
152 | 157 |
153 // Store data for a session-only origin, setup to save session data, then | 158 // Store data for a session-only origin, setup to save session data, then |
154 // shutdown. | 159 // shutdown. |
| 160 auto* area = context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 161 OpenStorageArea(kSessionOnlyOrigin); |
155 base::NullableString16 old_value; | 162 base::NullableString16 old_value; |
156 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> | 163 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
157 OpenStorageArea(kSessionOnlyOrigin)->SetItem(kKey, kValue, &old_value)); | 164 area->ScheduleImmediateCommit(); |
158 context_->SetForceKeepSessionState(); // Should override clear behavior. | 165 context_->SetForceKeepSessionState(); // Should override clear behavior. |
159 context_->Shutdown(); | 166 context_->Shutdown(); |
160 context_ = NULL; | 167 context_ = NULL; |
161 base::MessageLoop::current()->RunUntilIdle(); | 168 base::MessageLoop::current()->RunUntilIdle(); |
162 | 169 |
163 VerifySingleOriginRemains(kSessionOnlyOrigin); | 170 VerifySingleOriginRemains(kSessionOnlyOrigin); |
164 } | 171 } |
165 | 172 |
166 TEST_F(DOMStorageContextImplTest, PersistentIds) { | 173 TEST_F(DOMStorageContextImplTest, PersistentIds) { |
167 const int kFirstSessionStorageNamespaceId = 1 + session_id_offset(); | 174 const int kFirstSessionStorageNamespaceId = 1 + session_id_offset(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 const std::string kPersistentId = "persistent"; | 213 const std::string kPersistentId = "persistent"; |
207 context_->CreateSessionNamespace(kSessionStorageNamespaceId, | 214 context_->CreateSessionNamespace(kSessionStorageNamespaceId, |
208 kPersistentId); | 215 kPersistentId); |
209 DOMStorageNamespace* dom_namespace = | 216 DOMStorageNamespace* dom_namespace = |
210 context_->GetStorageNamespace(kSessionStorageNamespaceId); | 217 context_->GetStorageNamespace(kSessionStorageNamespaceId); |
211 DOMStorageArea* area = dom_namespace->OpenStorageArea(kOrigin); | 218 DOMStorageArea* area = dom_namespace->OpenStorageArea(kOrigin); |
212 const base::string16 kKey(ASCIIToUTF16("foo")); | 219 const base::string16 kKey(ASCIIToUTF16("foo")); |
213 const base::string16 kValue(ASCIIToUTF16("bar")); | 220 const base::string16 kValue(ASCIIToUTF16("bar")); |
214 base::NullableString16 old_nullable_value; | 221 base::NullableString16 old_nullable_value; |
215 area->SetItem(kKey, kValue, &old_nullable_value); | 222 area->SetItem(kKey, kValue, &old_nullable_value); |
| 223 area->ScheduleImmediateCommit(); |
216 dom_namespace->CloseStorageArea(area); | 224 dom_namespace->CloseStorageArea(area); |
217 | 225 |
218 // Destroy and recreate the DOMStorageContextImpl. | 226 // Destroy and recreate the DOMStorageContextImpl. |
219 context_->Shutdown(); | 227 context_->Shutdown(); |
220 context_ = NULL; | 228 context_ = NULL; |
221 base::MessageLoop::current()->RunUntilIdle(); | 229 base::MessageLoop::current()->RunUntilIdle(); |
222 context_ = new DOMStorageContextImpl( | 230 context_ = new DOMStorageContextImpl( |
223 temp_dir_.path(), temp_dir_.path(), | 231 temp_dir_.path(), temp_dir_.path(), |
224 storage_policy_.get(), task_runner_.get()); | 232 storage_policy_.get(), task_runner_.get()); |
225 context_->SetSaveSessionStorageOnDisk(); | 233 context_->SetSaveSessionStorageOnDisk(); |
226 | 234 |
227 // Read the data back. | 235 // Read the data back. |
228 context_->CreateSessionNamespace(kSessionStorageNamespaceId, | 236 context_->CreateSessionNamespace(kSessionStorageNamespaceId, |
229 kPersistentId); | 237 kPersistentId); |
230 dom_namespace = context_->GetStorageNamespace(kSessionStorageNamespaceId); | 238 dom_namespace = context_->GetStorageNamespace(kSessionStorageNamespaceId); |
231 area = dom_namespace->OpenStorageArea(kOrigin); | 239 area = dom_namespace->OpenStorageArea(kOrigin); |
232 base::NullableString16 read_value; | 240 base::NullableString16 read_value; |
233 read_value = area->GetItem(kKey); | 241 read_value = area->GetItem(kKey); |
234 EXPECT_EQ(kValue, read_value.string()); | 242 EXPECT_EQ(kValue, read_value.string()); |
235 dom_namespace->CloseStorageArea(area); | 243 dom_namespace->CloseStorageArea(area); |
236 | 244 |
237 SessionStorageUsageInfo info; | 245 SessionStorageUsageInfo info; |
238 info.origin = kOrigin; | 246 info.origin = kOrigin; |
239 info.persistent_namespace_id = kPersistentId; | 247 info.persistent_namespace_id = kPersistentId; |
240 context_->DeleteSessionStorage(info); | 248 context_->DeleteSessionStorage(info); |
| 249 area->ScheduleImmediateCommit(); |
241 | 250 |
242 // Destroy and recreate again. | 251 // Destroy and recreate again. |
243 context_->Shutdown(); | 252 context_->Shutdown(); |
244 context_ = NULL; | 253 context_ = NULL; |
245 base::MessageLoop::current()->RunUntilIdle(); | 254 base::MessageLoop::current()->RunUntilIdle(); |
246 context_ = new DOMStorageContextImpl( | 255 context_ = new DOMStorageContextImpl( |
247 temp_dir_.path(), temp_dir_.path(), | 256 temp_dir_.path(), temp_dir_.path(), |
248 storage_policy_.get(), task_runner_.get()); | 257 storage_policy_.get(), task_runner_.get()); |
249 context_->SetSaveSessionStorageOnDisk(); | 258 context_->SetSaveSessionStorageOnDisk(); |
250 | 259 |
251 // Now there should be no data. | 260 // Now there should be no data. |
252 context_->CreateSessionNamespace(kSessionStorageNamespaceId, | 261 context_->CreateSessionNamespace(kSessionStorageNamespaceId, |
253 kPersistentId); | 262 kPersistentId); |
254 dom_namespace = context_->GetStorageNamespace(kSessionStorageNamespaceId); | 263 dom_namespace = context_->GetStorageNamespace(kSessionStorageNamespaceId); |
255 area = dom_namespace->OpenStorageArea(kOrigin); | 264 area = dom_namespace->OpenStorageArea(kOrigin); |
256 read_value = area->GetItem(kKey); | 265 read_value = area->GetItem(kKey); |
257 EXPECT_TRUE(read_value.is_null()); | 266 EXPECT_TRUE(read_value.is_null()); |
258 dom_namespace->CloseStorageArea(area); | 267 dom_namespace->CloseStorageArea(area); |
259 context_->Shutdown(); | 268 context_->Shutdown(); |
260 context_ = NULL; | 269 context_ = NULL; |
261 base::MessageLoop::current()->RunUntilIdle(); | 270 base::MessageLoop::current()->RunUntilIdle(); |
262 } | 271 } |
263 | 272 |
264 } // namespace content | 273 } // namespace content |
OLD | NEW |