Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: content/browser/service_worker/service_worker_storage_unittest.cc

Issue 1693303002: ServiceWorker: Make ServiceWorkerStorage more self-defensive (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "content/browser/service_worker/service_worker_storage.h" 5 #include "content/browser/service_worker/service_worker_storage.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 75
76 ServiceWorkerStorage::FindRegistrationCallback MakeFindCallback( 76 ServiceWorkerStorage::FindRegistrationCallback MakeFindCallback(
77 bool* was_called, 77 bool* was_called,
78 ServiceWorkerStatusCode* result, 78 ServiceWorkerStatusCode* result,
79 scoped_refptr<ServiceWorkerRegistration>* found) { 79 scoped_refptr<ServiceWorkerRegistration>* found) {
80 return base::Bind(&FindCallback, was_called, result, found); 80 return base::Bind(&FindCallback, was_called, result, found);
81 } 81 }
82 82
83 void GetAllCallback( 83 void GetAllCallback(
84 bool* was_called, 84 bool* was_called,
85 ServiceWorkerStatusCode* result,
85 std::vector<scoped_refptr<ServiceWorkerRegistration>>* all_out, 86 std::vector<scoped_refptr<ServiceWorkerRegistration>>* all_out,
87 ServiceWorkerStatusCode status,
86 const std::vector<scoped_refptr<ServiceWorkerRegistration>>& all) { 88 const std::vector<scoped_refptr<ServiceWorkerRegistration>>& all) {
87 *was_called = true; 89 *was_called = true;
90 *result = status;
88 *all_out = all; 91 *all_out = all;
89 } 92 }
90 93
91 void GetAllInfosCallback( 94 void GetAllInfosCallback(
92 bool* was_called, 95 bool* was_called,
96 ServiceWorkerStatusCode* result,
93 std::vector<ServiceWorkerRegistrationInfo>* all_out, 97 std::vector<ServiceWorkerRegistrationInfo>* all_out,
98 ServiceWorkerStatusCode status,
94 const std::vector<ServiceWorkerRegistrationInfo>& all) { 99 const std::vector<ServiceWorkerRegistrationInfo>& all) {
95 *was_called = true; 100 *was_called = true;
101 *result = status;
96 *all_out = all; 102 *all_out = all;
97 } 103 }
98 104
99 ServiceWorkerStorage::GetRegistrationsCallback MakeGetRegistrationsCallback( 105 ServiceWorkerStorage::GetRegistrationsCallback MakeGetRegistrationsCallback(
100 bool* was_called, 106 bool* was_called,
107 ServiceWorkerStatusCode* status,
101 std::vector<scoped_refptr<ServiceWorkerRegistration>>* all) { 108 std::vector<scoped_refptr<ServiceWorkerRegistration>>* all) {
102 return base::Bind(&GetAllCallback, was_called, all); 109 return base::Bind(&GetAllCallback, was_called, status, all);
103 } 110 }
104 111
105 ServiceWorkerStorage::GetRegistrationsInfosCallback 112 ServiceWorkerStorage::GetRegistrationsInfosCallback
106 MakeGetRegistrationsInfosCallback( 113 MakeGetRegistrationsInfosCallback(
107 bool* was_called, 114 bool* was_called,
115 ServiceWorkerStatusCode* status,
108 std::vector<ServiceWorkerRegistrationInfo>* all) { 116 std::vector<ServiceWorkerRegistrationInfo>* all) {
109 return base::Bind(&GetAllInfosCallback, was_called, all); 117 return base::Bind(&GetAllInfosCallback, was_called, status, all);
110 } 118 }
111 119
112 void GetUserDataCallback( 120 void GetUserDataCallback(
113 bool* was_called, 121 bool* was_called,
114 std::string* data_out, 122 std::string* data_out,
115 ServiceWorkerStatusCode* status_out, 123 ServiceWorkerStatusCode* status_out,
116 const std::string& data, 124 const std::string& data,
117 ServiceWorkerStatusCode status) { 125 ServiceWorkerStatusCode status) {
118 *was_called = true; 126 *was_called = true;
119 *data_out = data; 127 *data_out = data;
120 *status_out = status; 128 *status_out = status;
121 } 129 }
122 130
123 void GetUserDataForAllRegistrationsCallback( 131 void GetUserDataForAllRegistrationsCallback(
124 bool* was_called, 132 bool* was_called,
125 std::vector<std::pair<int64_t, std::string>>* data_out, 133 std::vector<std::pair<int64_t, std::string>>* data_out,
126 ServiceWorkerStatusCode* status_out, 134 ServiceWorkerStatusCode* status_out,
127 const std::vector<std::pair<int64_t, std::string>>& data, 135 const std::vector<std::pair<int64_t, std::string>>& data,
128 ServiceWorkerStatusCode status) { 136 ServiceWorkerStatusCode status) {
129 *was_called = true; 137 *was_called = true;
130 *data_out = data; 138 *data_out = data;
131 *status_out = status; 139 *status_out = status;
132 } 140 }
133 141
134 void WriteResponse(ServiceWorkerStorage* storage, 142 int WriteResponse(ServiceWorkerStorage* storage,
135 int64_t id, 143 int64_t id,
136 const std::string& headers, 144 const std::string& headers,
137 IOBuffer* body, 145 IOBuffer* body,
138 int length) { 146 int length) {
139 scoped_ptr<ServiceWorkerResponseWriter> writer = 147 scoped_ptr<ServiceWorkerResponseWriter> writer =
140 storage->CreateResponseWriter(id); 148 storage->CreateResponseWriter(id);
141 149
142 scoped_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo); 150 scoped_ptr<net::HttpResponseInfo> info(new net::HttpResponseInfo);
143 info->request_time = base::Time::Now(); 151 info->request_time = base::Time::Now();
144 info->response_time = base::Time::Now(); 152 info->response_time = base::Time::Now();
145 info->was_cached = false; 153 info->was_cached = false;
146 info->headers = new net::HttpResponseHeaders(headers); 154 info->headers = new net::HttpResponseHeaders(headers);
147 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer = 155 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer =
148 new HttpResponseInfoIOBuffer(info.release()); 156 new HttpResponseInfoIOBuffer(info.release());
157 int rv = 0;
149 { 158 {
150 TestCompletionCallback cb; 159 TestCompletionCallback cb;
151 writer->WriteInfo(info_buffer.get(), cb.callback()); 160 writer->WriteInfo(info_buffer.get(), cb.callback());
152 int rv = cb.WaitForResult(); 161 rv = cb.WaitForResult();
153 EXPECT_LT(0, rv); 162 if (rv < 0)
163 return rv;
154 } 164 }
155 { 165 {
156 TestCompletionCallback cb; 166 TestCompletionCallback cb;
157 writer->WriteData(body, length, cb.callback()); 167 writer->WriteData(body, length, cb.callback());
158 int rv = cb.WaitForResult(); 168 rv = cb.WaitForResult();
159 EXPECT_EQ(length, rv);
160 } 169 }
170 return rv;
161 } 171 }
162 172
163 void WriteStringResponse(ServiceWorkerStorage* storage, 173 int WriteStringResponse(ServiceWorkerStorage* storage,
164 int64_t id, 174 int64_t id,
165 const std::string& headers, 175 const std::string& headers,
166 const std::string& body) { 176 const std::string& body) {
167 scoped_refptr<IOBuffer> body_buffer(new WrappedIOBuffer(body.data())); 177 scoped_refptr<IOBuffer> body_buffer(new WrappedIOBuffer(body.data()));
168 WriteResponse(storage, id, headers, body_buffer.get(), body.length()); 178 return WriteResponse(storage, id, headers, body_buffer.get(), body.length());
169 } 179 }
170 180
171 void WriteBasicResponse(ServiceWorkerStorage* storage, int64_t id) { 181 int WriteBasicResponse(ServiceWorkerStorage* storage, int64_t id) {
172 scoped_ptr<ServiceWorkerResponseWriter> writer =
173 storage->CreateResponseWriter(id);
174
175 const char kHttpHeaders[] = "HTTP/1.0 200 HONKYDORY\0Content-Length: 5\0\0"; 182 const char kHttpHeaders[] = "HTTP/1.0 200 HONKYDORY\0Content-Length: 5\0\0";
176 const char kHttpBody[] = "Hello"; 183 const char kHttpBody[] = "Hello";
177 std::string headers(kHttpHeaders, arraysize(kHttpHeaders)); 184 std::string headers(kHttpHeaders, arraysize(kHttpHeaders));
178 WriteStringResponse(storage, id, headers, std::string(kHttpBody)); 185 return WriteStringResponse(storage, id, headers, std::string(kHttpBody));
186 }
187
188 int ReadResponseInfo(ServiceWorkerStorage* storage,
189 int64_t id,
190 HttpResponseInfoIOBuffer* info_buffer) {
191 scoped_ptr<ServiceWorkerResponseReader> reader =
192 storage->CreateResponseReader(id);
193 TestCompletionCallback cb;
194 reader->ReadInfo(info_buffer, cb.callback());
195 return cb.WaitForResult();
179 } 196 }
180 197
181 bool VerifyBasicResponse(ServiceWorkerStorage* storage, 198 bool VerifyBasicResponse(ServiceWorkerStorage* storage,
182 int64_t id, 199 int64_t id,
183 bool expected_positive_result) { 200 bool expected_positive_result) {
184 const std::string kExpectedHttpBody("Hello"); 201 const std::string kExpectedHttpBody("Hello");
185 scoped_ptr<ServiceWorkerResponseReader> reader = 202 scoped_ptr<ServiceWorkerResponseReader> reader =
186 storage->CreateResponseReader(id); 203 storage->CreateResponseReader(id);
187 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer = 204 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer =
188 new HttpResponseInfoIOBuffer(); 205 new HttpResponseInfoIOBuffer();
189 { 206 int rv = ReadResponseInfo(storage, id, info_buffer.get());
190 TestCompletionCallback cb; 207 if (expected_positive_result)
191 reader->ReadInfo(info_buffer.get(), cb.callback()); 208 EXPECT_LT(0, rv);
192 int rv = cb.WaitForResult(); 209 if (rv <= 0)
193 if (expected_positive_result) 210 return false;
194 EXPECT_LT(0, rv);
195 if (rv <= 0)
196 return false;
197 }
198 211
199 std::string received_body; 212 std::string received_body;
200 { 213 const int kBigEnough = 512;
201 const int kBigEnough = 512; 214 scoped_refptr<net::IOBuffer> buffer = new IOBuffer(kBigEnough);
202 scoped_refptr<net::IOBuffer> buffer = new IOBuffer(kBigEnough); 215 TestCompletionCallback cb;
203 TestCompletionCallback cb; 216 reader->ReadData(buffer.get(), kBigEnough, cb.callback());
204 reader->ReadData(buffer.get(), kBigEnough, cb.callback()); 217 rv = cb.WaitForResult();
205 int rv = cb.WaitForResult(); 218 EXPECT_EQ(static_cast<int>(kExpectedHttpBody.size()), rv);
206 EXPECT_EQ(static_cast<int>(kExpectedHttpBody.size()), rv); 219 if (rv <= 0)
207 if (rv <= 0) 220 return false;
208 return false; 221 received_body.assign(buffer->data(), rv);
209 received_body.assign(buffer->data(), rv);
210 }
211 222
212 bool status_match = 223 bool status_match =
213 std::string("HONKYDORY") == 224 std::string("HONKYDORY") ==
214 info_buffer->http_info->headers->GetStatusText(); 225 info_buffer->http_info->headers->GetStatusText();
215 bool data_match = kExpectedHttpBody == received_body; 226 bool data_match = kExpectedHttpBody == received_body;
216 227
217 EXPECT_TRUE(status_match); 228 EXPECT_TRUE(status_match);
218 EXPECT_TRUE(data_match); 229 EXPECT_TRUE(data_match);
219 return status_match && data_match; 230 return status_match && data_match;
220 } 231 }
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 protected: 325 protected:
315 void LazyInitialize() { 326 void LazyInitialize() {
316 storage()->LazyInitialize(base::Bind(&base::DoNothing)); 327 storage()->LazyInitialize(base::Bind(&base::DoNothing));
317 base::RunLoop().RunUntilIdle(); 328 base::RunLoop().RunUntilIdle();
318 } 329 }
319 330
320 ServiceWorkerStatusCode StoreRegistration( 331 ServiceWorkerStatusCode StoreRegistration(
321 scoped_refptr<ServiceWorkerRegistration> registration, 332 scoped_refptr<ServiceWorkerRegistration> registration,
322 scoped_refptr<ServiceWorkerVersion> version) { 333 scoped_refptr<ServiceWorkerVersion> version) {
323 bool was_called = false; 334 bool was_called = false;
324 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 335 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_MAX_VALUE;
325 storage()->StoreRegistration(registration.get(), 336 storage()->StoreRegistration(registration.get(),
326 version.get(), 337 version.get(),
327 MakeStatusCallback(&was_called, &result)); 338 MakeStatusCallback(&was_called, &result));
328 EXPECT_FALSE(was_called); // always async 339 EXPECT_FALSE(was_called); // always async
329 base::RunLoop().RunUntilIdle(); 340 base::RunLoop().RunUntilIdle();
330 EXPECT_TRUE(was_called); 341 EXPECT_TRUE(was_called);
331 return result; 342 return result;
332 } 343 }
333 344
334 ServiceWorkerStatusCode DeleteRegistration(int64_t registration_id, 345 ServiceWorkerStatusCode DeleteRegistration(int64_t registration_id,
335 const GURL& origin) { 346 const GURL& origin) {
336 bool was_called = false; 347 bool was_called = false;
337 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 348 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_MAX_VALUE;
338 storage()->DeleteRegistration( 349 storage()->DeleteRegistration(
339 registration_id, origin, MakeStatusCallback(&was_called, &result)); 350 registration_id, origin, MakeStatusCallback(&was_called, &result));
340 EXPECT_FALSE(was_called); // always async 351 EXPECT_FALSE(was_called); // always async
341 base::RunLoop().RunUntilIdle(); 352 base::RunLoop().RunUntilIdle();
342 EXPECT_TRUE(was_called); 353 EXPECT_TRUE(was_called);
343 return result; 354 return result;
344 } 355 }
345 356
346 void GetAllRegistrationsInfos( 357 ServiceWorkerStatusCode GetAllRegistrationsInfos(
347 std::vector<ServiceWorkerRegistrationInfo>* registrations) { 358 std::vector<ServiceWorkerRegistrationInfo>* registrations) {
348 bool was_called = false; 359 bool was_called = false;
360 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_MAX_VALUE;
349 storage()->GetAllRegistrationsInfos( 361 storage()->GetAllRegistrationsInfos(
350 MakeGetRegistrationsInfosCallback(&was_called, registrations)); 362 MakeGetRegistrationsInfosCallback(&was_called, &result, registrations));
351 EXPECT_FALSE(was_called); // always async 363 EXPECT_FALSE(was_called); // always async
352 base::RunLoop().RunUntilIdle(); 364 base::RunLoop().RunUntilIdle();
353 EXPECT_TRUE(was_called); 365 EXPECT_TRUE(was_called);
366 return result;
354 } 367 }
355 368
356 void GetRegistrationsForOrigin( 369 ServiceWorkerStatusCode GetRegistrationsForOrigin(
357 const GURL& origin, 370 const GURL& origin,
358 std::vector<scoped_refptr<ServiceWorkerRegistration>>* registrations) { 371 std::vector<scoped_refptr<ServiceWorkerRegistration>>* registrations) {
359 bool was_called = false; 372 bool was_called = false;
373 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_MAX_VALUE;
360 storage()->GetRegistrationsForOrigin( 374 storage()->GetRegistrationsForOrigin(
361 origin, MakeGetRegistrationsCallback(&was_called, registrations)); 375 origin,
376 MakeGetRegistrationsCallback(&was_called, &result, registrations));
362 EXPECT_FALSE(was_called); // always async 377 EXPECT_FALSE(was_called); // always async
363 base::RunLoop().RunUntilIdle(); 378 base::RunLoop().RunUntilIdle();
364 EXPECT_TRUE(was_called); 379 EXPECT_TRUE(was_called);
380 return result;
365 } 381 }
366 382
367 ServiceWorkerStatusCode GetUserData(int64_t registration_id, 383 ServiceWorkerStatusCode GetUserData(int64_t registration_id,
368 const std::string& key, 384 const std::string& key,
369 std::string* data) { 385 std::string* data) {
370 bool was_called = false; 386 bool was_called = false;
371 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 387 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_MAX_VALUE;
372 storage()->GetUserData( 388 storage()->GetUserData(
373 registration_id, key, 389 registration_id, key,
374 base::Bind(&GetUserDataCallback, &was_called, data, &result)); 390 base::Bind(&GetUserDataCallback, &was_called, data, &result));
375 EXPECT_FALSE(was_called); // always async 391 EXPECT_FALSE(was_called); // always async
376 base::RunLoop().RunUntilIdle(); 392 base::RunLoop().RunUntilIdle();
377 EXPECT_TRUE(was_called); 393 EXPECT_TRUE(was_called);
378 return result; 394 return result;
379 } 395 }
380 396
381 ServiceWorkerStatusCode StoreUserData(int64_t registration_id, 397 ServiceWorkerStatusCode StoreUserData(int64_t registration_id,
382 const GURL& origin, 398 const GURL& origin,
383 const std::string& key, 399 const std::string& key,
384 const std::string& data) { 400 const std::string& data) {
385 bool was_called = false; 401 bool was_called = false;
386 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 402 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_MAX_VALUE;
387 storage()->StoreUserData( 403 storage()->StoreUserData(
388 registration_id, origin, key, data, 404 registration_id, origin, key, data,
389 MakeStatusCallback(&was_called, &result)); 405 MakeStatusCallback(&was_called, &result));
390 EXPECT_FALSE(was_called); // always async 406 EXPECT_FALSE(was_called); // always async
391 base::RunLoop().RunUntilIdle(); 407 base::RunLoop().RunUntilIdle();
392 EXPECT_TRUE(was_called); 408 EXPECT_TRUE(was_called);
393 return result; 409 return result;
394 } 410 }
395 411
396 ServiceWorkerStatusCode ClearUserData(int64_t registration_id, 412 ServiceWorkerStatusCode ClearUserData(int64_t registration_id,
397 const std::string& key) { 413 const std::string& key) {
398 bool was_called = false; 414 bool was_called = false;
399 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 415 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_MAX_VALUE;
400 storage()->ClearUserData( 416 storage()->ClearUserData(
401 registration_id, key, MakeStatusCallback(&was_called, &result)); 417 registration_id, key, MakeStatusCallback(&was_called, &result));
402 EXPECT_FALSE(was_called); // always async 418 EXPECT_FALSE(was_called); // always async
403 base::RunLoop().RunUntilIdle(); 419 base::RunLoop().RunUntilIdle();
404 EXPECT_TRUE(was_called); 420 EXPECT_TRUE(was_called);
405 return result; 421 return result;
406 } 422 }
407 423
408 ServiceWorkerStatusCode GetUserDataForAllRegistrations( 424 ServiceWorkerStatusCode GetUserDataForAllRegistrations(
409 const std::string& key, 425 const std::string& key,
410 std::vector<std::pair<int64_t, std::string>>* data) { 426 std::vector<std::pair<int64_t, std::string>>* data) {
411 bool was_called = false; 427 bool was_called = false;
412 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 428 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_MAX_VALUE;
413 storage()->GetUserDataForAllRegistrations( 429 storage()->GetUserDataForAllRegistrations(
414 key, base::Bind(&GetUserDataForAllRegistrationsCallback, &was_called, 430 key, base::Bind(&GetUserDataForAllRegistrationsCallback, &was_called,
415 data, &result)); 431 data, &result));
416 EXPECT_FALSE(was_called); // always async 432 EXPECT_FALSE(was_called); // always async
417 base::RunLoop().RunUntilIdle(); 433 base::RunLoop().RunUntilIdle();
418 EXPECT_TRUE(was_called); 434 EXPECT_TRUE(was_called);
419 return result; 435 return result;
420 } 436 }
421 437
422 ServiceWorkerStatusCode UpdateToActiveState( 438 ServiceWorkerStatusCode UpdateToActiveState(
423 scoped_refptr<ServiceWorkerRegistration> registration) { 439 const scoped_refptr<ServiceWorkerRegistration>& registration) {
424 bool was_called = false; 440 bool was_called = false;
425 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 441 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_MAX_VALUE;
426 storage()->UpdateToActiveState(registration.get(), 442 storage()->UpdateToActiveState(registration.get(),
427 MakeStatusCallback(&was_called, &result)); 443 MakeStatusCallback(&was_called, &result));
428 EXPECT_FALSE(was_called); // always async 444 EXPECT_FALSE(was_called); // always async
429 base::RunLoop().RunUntilIdle(); 445 base::RunLoop().RunUntilIdle();
430 EXPECT_TRUE(was_called); 446 EXPECT_TRUE(was_called);
431 return result; 447 return result;
432 } 448 }
433 449
434 void UpdateLastUpdateCheckTime(ServiceWorkerRegistration* registration) { 450 void UpdateLastUpdateCheckTime(
435 storage()->UpdateLastUpdateCheckTime(registration); 451 const scoped_refptr<ServiceWorkerRegistration>& registration) {
452 storage()->UpdateLastUpdateCheckTime(registration.get());
436 base::RunLoop().RunUntilIdle(); 453 base::RunLoop().RunUntilIdle();
437 } 454 }
438 455
439 ServiceWorkerStatusCode FindRegistrationForDocument( 456 ServiceWorkerStatusCode FindRegistrationForDocument(
440 const GURL& document_url, 457 const GURL& document_url,
441 scoped_refptr<ServiceWorkerRegistration>* registration) { 458 scoped_refptr<ServiceWorkerRegistration>* registration) {
442 bool was_called = false; 459 bool was_called = false;
443 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 460 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_MAX_VALUE;
444 storage()->FindRegistrationForDocument( 461 storage()->FindRegistrationForDocument(
445 document_url, MakeFindCallback(&was_called, &result, registration)); 462 document_url, MakeFindCallback(&was_called, &result, registration));
446 base::RunLoop().RunUntilIdle(); 463 base::RunLoop().RunUntilIdle();
447 EXPECT_TRUE(was_called); 464 EXPECT_TRUE(was_called);
448 return result; 465 return result;
449 } 466 }
450 467
451 ServiceWorkerStatusCode FindRegistrationForPattern( 468 ServiceWorkerStatusCode FindRegistrationForPattern(
452 const GURL& scope, 469 const GURL& scope,
453 scoped_refptr<ServiceWorkerRegistration>* registration) { 470 scoped_refptr<ServiceWorkerRegistration>* registration) {
454 bool was_called = false; 471 bool was_called = false;
455 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 472 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_MAX_VALUE;
456 storage()->FindRegistrationForPattern( 473 storage()->FindRegistrationForPattern(
457 scope, MakeFindCallback(&was_called, &result, registration)); 474 scope, MakeFindCallback(&was_called, &result, registration));
458 EXPECT_FALSE(was_called); // always async 475 EXPECT_FALSE(was_called); // always async
459 base::RunLoop().RunUntilIdle(); 476 base::RunLoop().RunUntilIdle();
460 EXPECT_TRUE(was_called); 477 EXPECT_TRUE(was_called);
461 return result; 478 return result;
462 } 479 }
463 480
464 ServiceWorkerStatusCode FindRegistrationForId( 481 ServiceWorkerStatusCode FindRegistrationForId(
465 int64_t registration_id, 482 int64_t registration_id,
466 const GURL& origin, 483 const GURL& origin,
467 scoped_refptr<ServiceWorkerRegistration>* registration) { 484 scoped_refptr<ServiceWorkerRegistration>* registration) {
468 bool was_called = false; 485 bool was_called = false;
469 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 486 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_MAX_VALUE;
470 storage()->FindRegistrationForId( 487 storage()->FindRegistrationForId(
471 registration_id, origin, 488 registration_id, origin,
472 MakeFindCallback(&was_called, &result, registration)); 489 MakeFindCallback(&was_called, &result, registration));
473 base::RunLoop().RunUntilIdle(); 490 base::RunLoop().RunUntilIdle();
474 EXPECT_TRUE(was_called); 491 EXPECT_TRUE(was_called);
475 return result; 492 return result;
476 } 493 }
477 494
478 ServiceWorkerStatusCode FindRegistrationForIdOnly( 495 ServiceWorkerStatusCode FindRegistrationForIdOnly(
479 int64_t registration_id, 496 int64_t registration_id,
480 scoped_refptr<ServiceWorkerRegistration>* registration) { 497 scoped_refptr<ServiceWorkerRegistration>* registration) {
481 bool was_called = false; 498 bool was_called = false;
482 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_FAILED; 499 ServiceWorkerStatusCode result = SERVICE_WORKER_ERROR_MAX_VALUE;
483 storage()->FindRegistrationForIdOnly( 500 storage()->FindRegistrationForIdOnly(
484 registration_id, MakeFindCallback(&was_called, &result, registration)); 501 registration_id, MakeFindCallback(&was_called, &result, registration));
485 base::RunLoop().RunUntilIdle(); 502 base::RunLoop().RunUntilIdle();
486 EXPECT_TRUE(was_called); 503 EXPECT_TRUE(was_called);
487 return result; 504 return result;
488 } 505 }
489 506
490 // user_data_directory_ must be declared first to preserve destructor order. 507 // user_data_directory_ must be declared first to preserve destructor order.
491 base::ScopedTempDir user_data_directory_; 508 base::ScopedTempDir user_data_directory_;
492 scoped_ptr<EmbeddedWorkerTestHelper> helper_; 509 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
493 TestBrowserThreadBundle browser_thread_bundle_; 510 TestBrowserThreadBundle browser_thread_bundle_;
494 }; 511 };
495 512
513 TEST_F(ServiceWorkerStorageTest, DisabledStorage) {
514 const GURL kScope("http://www.example.com/scope/");
515 const GURL kScript("http://www.example.com/script.js");
516 const GURL kDocumentUrl("http://www.example.com/scope/document.html");
517 const int64_t kRegistrationId = 0;
518 const int64_t kVersionId = 0;
519 const int64_t kResourceId = 0;
520
521 LazyInitialize();
522 storage()->Disable();
523
524 scoped_refptr<ServiceWorkerRegistration> found_registration;
525 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT,
526 FindRegistrationForDocument(kDocumentUrl, &found_registration));
527 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT,
528 FindRegistrationForPattern(kScope, &found_registration));
529 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT,
530 FindRegistrationForId(kRegistrationId, kScope.GetOrigin(),
531 &found_registration));
532 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT,
533 FindRegistrationForIdOnly(kRegistrationId, &found_registration));
534 EXPECT_FALSE(storage()->GetUninstallingRegistration(kScope.GetOrigin()));
535
536 std::vector<scoped_refptr<ServiceWorkerRegistration>> found_registrations;
537 EXPECT_EQ(
538 SERVICE_WORKER_ERROR_ABORT,
539 GetRegistrationsForOrigin(kScope.GetOrigin(), &found_registrations));
540
541 std::vector<ServiceWorkerRegistrationInfo> all_registrations;
542 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT,
543 GetAllRegistrationsInfos(&all_registrations));
544
545 scoped_refptr<ServiceWorkerRegistration> live_registration =
546 new ServiceWorkerRegistration(kScope, kRegistrationId,
547 context()->AsWeakPtr());
548 scoped_refptr<ServiceWorkerVersion> live_version = new ServiceWorkerVersion(
549 live_registration.get(), kScript, kVersionId, context()->AsWeakPtr());
550 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT,
551 StoreRegistration(live_registration, live_version));
552
553 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT, UpdateToActiveState(live_registration));
554
555 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT,
556 DeleteRegistration(kRegistrationId, kScope.GetOrigin()));
557
558 // Response reader and writer created by the disabled storage should fail to
559 // access the disk cache.
560 scoped_refptr<HttpResponseInfoIOBuffer> info_buffer =
561 new HttpResponseInfoIOBuffer();
562 EXPECT_EQ(net::ERR_CACHE_MISS,
563 ReadResponseInfo(storage(), kResourceId, info_buffer.get()));
564 EXPECT_EQ(net::ERR_FAILED, WriteBasicResponse(storage(), kResourceId));
565 EXPECT_EQ(net::ERR_FAILED,
566 WriteResponseMetadata(storage(), kResourceId, "foo"));
567
568 const std::string kUserDataKey = "key";
569 std::string user_data_out;
570 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT,
571 GetUserData(kRegistrationId, kUserDataKey, &user_data_out));
572 EXPECT_EQ(
573 SERVICE_WORKER_ERROR_ABORT,
574 StoreUserData(kRegistrationId, kScope.GetOrigin(), kUserDataKey, "foo"));
575 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT,
576 ClearUserData(kRegistrationId, kUserDataKey));
577 std::vector<std::pair<int64_t, std::string>> data_list_out;
578 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT,
579 GetUserDataForAllRegistrations(kUserDataKey, &data_list_out));
580
581 EXPECT_FALSE(
582 storage()->OriginHasForeignFetchRegistrations(kScope.GetOrigin()));
583
584 // Next available ids should be invalid.
585 EXPECT_EQ(kInvalidServiceWorkerRegistrationId,
586 storage()->NewRegistrationId());
587 EXPECT_EQ(kInvalidServiceWorkerVersionId, storage()->NewVersionId());
588 EXPECT_EQ(kInvalidServiceWorkerResourceId, storage()->NewRegistrationId());
589 }
590
496 TEST_F(ServiceWorkerStorageTest, StoreFindUpdateDeleteRegistration) { 591 TEST_F(ServiceWorkerStorageTest, StoreFindUpdateDeleteRegistration) {
497 const GURL kScope("http://www.test.not/scope/"); 592 const GURL kScope("http://www.test.not/scope/");
498 const GURL kScript("http://www.test.not/script.js"); 593 const GURL kScript("http://www.test.not/script.js");
499 const GURL kDocumentUrl("http://www.test.not/scope/document.html"); 594 const GURL kDocumentUrl("http://www.test.not/scope/document.html");
500 const GURL kResource1("http://www.test.not/scope/resource1.js"); 595 const GURL kResource1("http://www.test.not/scope/resource1.js");
501 const int64_t kResource1Size = 1591234; 596 const int64_t kResource1Size = 1591234;
502 const GURL kResource2("http://www.test.not/scope/resource2.js"); 597 const GURL kResource2("http://www.test.not/scope/resource2.js");
503 const int64_t kResource2Size = 51; 598 const int64_t kResource2Size = 51;
504 const int64_t kRegistrationId = 0; 599 const int64_t kRegistrationId = 0;
505 const int64_t kVersionId = 0; 600 const int64_t kVersionId = 0;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 680 FindRegistrationForDocument(kDocumentUrl, &found_registration));
586 ASSERT_TRUE(found_registration.get()); 681 ASSERT_TRUE(found_registration.get());
587 EXPECT_EQ(kRegistrationId, found_registration->id()); 682 EXPECT_EQ(kRegistrationId, found_registration->id());
588 EXPECT_TRUE(found_registration->HasOneRef()); 683 EXPECT_TRUE(found_registration->HasOneRef());
589 684
590 // Check that sizes are populated correctly 685 // Check that sizes are populated correctly
591 EXPECT_EQ(live_version.get(), found_registration->waiting_version()); 686 EXPECT_EQ(live_version.get(), found_registration->waiting_version());
592 EXPECT_EQ(kResource1Size + kResource2Size, 687 EXPECT_EQ(kResource1Size + kResource2Size,
593 found_registration->resources_total_size_bytes()); 688 found_registration->resources_total_size_bytes());
594 std::vector<ServiceWorkerRegistrationInfo> all_registrations; 689 std::vector<ServiceWorkerRegistrationInfo> all_registrations;
595 GetAllRegistrationsInfos(&all_registrations); 690 EXPECT_EQ(SERVICE_WORKER_OK, GetAllRegistrationsInfos(&all_registrations));
596 EXPECT_EQ(1u, all_registrations.size()); 691 EXPECT_EQ(1u, all_registrations.size());
597 ServiceWorkerRegistrationInfo info = all_registrations[0]; 692 ServiceWorkerRegistrationInfo info = all_registrations[0];
598 EXPECT_EQ(kResource1Size + kResource2Size, info.stored_version_size_bytes); 693 EXPECT_EQ(kResource1Size + kResource2Size, info.stored_version_size_bytes);
599 all_registrations.clear(); 694 all_registrations.clear();
600 695
601 // Finding by origin should provide the same result if origin is kScope. 696 // Finding by origin should provide the same result if origin is kScope.
602 std::vector<scoped_refptr<ServiceWorkerRegistration>> 697 std::vector<scoped_refptr<ServiceWorkerRegistration>>
603 registrations_for_origin; 698 registrations_for_origin;
604 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_for_origin); 699 EXPECT_EQ(
700 SERVICE_WORKER_OK,
701 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_for_origin));
605 EXPECT_EQ(1u, registrations_for_origin.size()); 702 EXPECT_EQ(1u, registrations_for_origin.size());
606 registrations_for_origin.clear(); 703 registrations_for_origin.clear();
607 704
608 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(), 705 EXPECT_EQ(SERVICE_WORKER_OK,
609 &registrations_for_origin); 706 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(),
707 &registrations_for_origin));
610 EXPECT_TRUE(registrations_for_origin.empty()); 708 EXPECT_TRUE(registrations_for_origin.empty());
611 709
612 found_registration = NULL; 710 found_registration = NULL;
613 711
614 // Drop the live version too. 712 // Drop the live version too.
615 live_version = NULL; 713 live_version = NULL;
616 714
617 // And FindRegistrationForPattern is always async. 715 // And FindRegistrationForPattern is always async.
618 EXPECT_EQ(SERVICE_WORKER_OK, 716 EXPECT_EQ(SERVICE_WORKER_OK,
619 FindRegistrationForPattern(kScope, &found_registration)); 717 FindRegistrationForPattern(kScope, &found_registration));
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 811
714 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 812 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
715 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 813 FindRegistrationForDocument(kDocumentUrl, &found_registration));
716 EXPECT_FALSE(found_registration.get()); 814 EXPECT_FALSE(found_registration.get());
717 815
718 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 816 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
719 FindRegistrationForPattern(kScope, &found_registration)); 817 FindRegistrationForPattern(kScope, &found_registration));
720 EXPECT_FALSE(found_registration.get()); 818 EXPECT_FALSE(found_registration.get());
721 819
722 std::vector<ServiceWorkerRegistrationInfo> all_registrations; 820 std::vector<ServiceWorkerRegistrationInfo> all_registrations;
723 GetAllRegistrationsInfos(&all_registrations); 821 EXPECT_EQ(SERVICE_WORKER_OK, GetAllRegistrationsInfos(&all_registrations));
724 EXPECT_TRUE(all_registrations.empty()); 822 EXPECT_TRUE(all_registrations.empty());
725 823
726 std::vector<scoped_refptr<ServiceWorkerRegistration>> 824 std::vector<scoped_refptr<ServiceWorkerRegistration>>
727 registrations_for_origin; 825 registrations_for_origin;
728 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_for_origin); 826 EXPECT_EQ(
827 SERVICE_WORKER_OK,
828 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_for_origin));
729 EXPECT_TRUE(registrations_for_origin.empty()); 829 EXPECT_TRUE(registrations_for_origin.empty());
730 830
731 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(), 831 EXPECT_EQ(SERVICE_WORKER_OK,
732 &registrations_for_origin); 832 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(),
833 &registrations_for_origin));
733 EXPECT_TRUE(registrations_for_origin.empty()); 834 EXPECT_TRUE(registrations_for_origin.empty());
734 835
735 // Notify storage of it being installed. 836 // Notify storage of it being installed.
736 storage()->NotifyInstallingRegistration(live_registration.get()); 837 storage()->NotifyInstallingRegistration(live_registration.get());
737 838
738 // Now should be findable. 839 // Now should be findable.
739 EXPECT_EQ(SERVICE_WORKER_OK, 840 EXPECT_EQ(SERVICE_WORKER_OK,
740 FindRegistrationForId( 841 FindRegistrationForId(
741 kRegistrationId, kScope.GetOrigin(), &found_registration)); 842 kRegistrationId, kScope.GetOrigin(), &found_registration));
742 EXPECT_EQ(live_registration, found_registration); 843 EXPECT_EQ(live_registration, found_registration);
743 found_registration = NULL; 844 found_registration = NULL;
744 845
745 EXPECT_EQ(SERVICE_WORKER_OK, 846 EXPECT_EQ(SERVICE_WORKER_OK,
746 FindRegistrationForIdOnly(kRegistrationId, &found_registration)); 847 FindRegistrationForIdOnly(kRegistrationId, &found_registration));
747 EXPECT_EQ(live_registration, found_registration); 848 EXPECT_EQ(live_registration, found_registration);
748 found_registration = NULL; 849 found_registration = NULL;
749 850
750 EXPECT_EQ(SERVICE_WORKER_OK, 851 EXPECT_EQ(SERVICE_WORKER_OK,
751 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 852 FindRegistrationForDocument(kDocumentUrl, &found_registration));
752 EXPECT_EQ(live_registration, found_registration); 853 EXPECT_EQ(live_registration, found_registration);
753 found_registration = NULL; 854 found_registration = NULL;
754 855
755 EXPECT_EQ(SERVICE_WORKER_OK, 856 EXPECT_EQ(SERVICE_WORKER_OK,
756 FindRegistrationForPattern(kScope, &found_registration)); 857 FindRegistrationForPattern(kScope, &found_registration));
757 EXPECT_EQ(live_registration, found_registration); 858 EXPECT_EQ(live_registration, found_registration);
758 found_registration = NULL; 859 found_registration = NULL;
759 860
760 GetAllRegistrationsInfos(&all_registrations); 861 EXPECT_EQ(SERVICE_WORKER_OK, GetAllRegistrationsInfos(&all_registrations));
761 EXPECT_EQ(1u, all_registrations.size()); 862 EXPECT_EQ(1u, all_registrations.size());
762 all_registrations.clear(); 863 all_registrations.clear();
763 864
764 // Finding by origin should provide the same result if origin is kScope. 865 // Finding by origin should provide the same result if origin is kScope.
765 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_for_origin); 866 EXPECT_EQ(
867 SERVICE_WORKER_OK,
868 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_for_origin));
766 EXPECT_EQ(1u, registrations_for_origin.size()); 869 EXPECT_EQ(1u, registrations_for_origin.size());
767 registrations_for_origin.clear(); 870 registrations_for_origin.clear();
768 871
769 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(), 872 EXPECT_EQ(SERVICE_WORKER_OK,
770 &registrations_for_origin); 873 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(),
874 &registrations_for_origin));
771 EXPECT_TRUE(registrations_for_origin.empty()); 875 EXPECT_TRUE(registrations_for_origin.empty());
772 876
773 // Notify storage of installation no longer happening. 877 // Notify storage of installation no longer happening.
774 storage()->NotifyDoneInstallingRegistration( 878 storage()->NotifyDoneInstallingRegistration(
775 live_registration.get(), NULL, SERVICE_WORKER_OK); 879 live_registration.get(), NULL, SERVICE_WORKER_OK);
776 880
777 // Once again, should not be findable. 881 // Once again, should not be findable.
778 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 882 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
779 FindRegistrationForId( 883 FindRegistrationForId(
780 kRegistrationId, kScope.GetOrigin(), &found_registration)); 884 kRegistrationId, kScope.GetOrigin(), &found_registration));
781 EXPECT_FALSE(found_registration.get()); 885 EXPECT_FALSE(found_registration.get());
782 886
783 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 887 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
784 FindRegistrationForIdOnly(kRegistrationId, &found_registration)); 888 FindRegistrationForIdOnly(kRegistrationId, &found_registration));
785 EXPECT_FALSE(found_registration.get()); 889 EXPECT_FALSE(found_registration.get());
786 890
787 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 891 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
788 FindRegistrationForDocument(kDocumentUrl, &found_registration)); 892 FindRegistrationForDocument(kDocumentUrl, &found_registration));
789 EXPECT_FALSE(found_registration.get()); 893 EXPECT_FALSE(found_registration.get());
790 894
791 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, 895 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
792 FindRegistrationForPattern(kScope, &found_registration)); 896 FindRegistrationForPattern(kScope, &found_registration));
793 EXPECT_FALSE(found_registration.get()); 897 EXPECT_FALSE(found_registration.get());
794 898
795 GetAllRegistrationsInfos(&all_registrations); 899 EXPECT_EQ(SERVICE_WORKER_OK, GetAllRegistrationsInfos(&all_registrations));
796 EXPECT_TRUE(all_registrations.empty()); 900 EXPECT_TRUE(all_registrations.empty());
797 901
798 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_for_origin); 902 EXPECT_EQ(
903 SERVICE_WORKER_OK,
904 GetRegistrationsForOrigin(kScope.GetOrigin(), &registrations_for_origin));
799 EXPECT_TRUE(registrations_for_origin.empty()); 905 EXPECT_TRUE(registrations_for_origin.empty());
800 906
801 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(), 907 EXPECT_EQ(SERVICE_WORKER_OK,
802 &registrations_for_origin); 908 GetRegistrationsForOrigin(GURL("http://example.com/").GetOrigin(),
909 &registrations_for_origin));
803 EXPECT_TRUE(registrations_for_origin.empty()); 910 EXPECT_TRUE(registrations_for_origin.empty());
804 } 911 }
805 912
806 TEST_F(ServiceWorkerStorageTest, StoreUserData) { 913 TEST_F(ServiceWorkerStorageTest, StoreUserData) {
807 const GURL kScope("http://www.test.not/scope/"); 914 const GURL kScope("http://www.test.not/scope/");
808 const GURL kScript("http://www.test.not/script.js"); 915 const GURL kScript("http://www.test.not/script.js");
809 const int64_t kRegistrationId = 0; 916 const int64_t kRegistrationId = 0;
810 const int64_t kVersionId = 0; 917 const int64_t kVersionId = 0;
811 918
812 LazyInitialize(); 919 LazyInitialize();
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after
1234 VerifyBasicResponse(storage(), kStaleUncommittedResourceId, false)); 1341 VerifyBasicResponse(storage(), kStaleUncommittedResourceId, false));
1235 EXPECT_TRUE(VerifyBasicResponse(storage(), kNewResourceId, true)); 1342 EXPECT_TRUE(VerifyBasicResponse(storage(), kNewResourceId, true));
1236 } 1343 }
1237 1344
1238 TEST_F(ServiceWorkerResourceStorageDiskTest, DeleteAndStartOver) { 1345 TEST_F(ServiceWorkerResourceStorageDiskTest, DeleteAndStartOver) {
1239 EXPECT_FALSE(storage()->IsDisabled()); 1346 EXPECT_FALSE(storage()->IsDisabled());
1240 ASSERT_TRUE(base::DirectoryExists(storage()->GetDiskCachePath())); 1347 ASSERT_TRUE(base::DirectoryExists(storage()->GetDiskCachePath()));
1241 ASSERT_TRUE(base::DirectoryExists(storage()->GetDatabasePath())); 1348 ASSERT_TRUE(base::DirectoryExists(storage()->GetDatabasePath()));
1242 1349
1243 base::RunLoop run_loop; 1350 base::RunLoop run_loop;
1244 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_ABORT; 1351 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE;
1245 storage()->DeleteAndStartOver( 1352 storage()->DeleteAndStartOver(
1246 base::Bind(&StatusAndQuitCallback, &status, run_loop.QuitClosure())); 1353 base::Bind(&StatusAndQuitCallback, &status, run_loop.QuitClosure()));
1247 run_loop.Run(); 1354 run_loop.Run();
1248 1355
1249 EXPECT_EQ(SERVICE_WORKER_OK, status); 1356 EXPECT_EQ(SERVICE_WORKER_OK, status);
1250 EXPECT_TRUE(storage()->IsDisabled()); 1357 EXPECT_TRUE(storage()->IsDisabled());
1251 EXPECT_FALSE(base::DirectoryExists(storage()->GetDiskCachePath())); 1358 EXPECT_FALSE(base::DirectoryExists(storage()->GetDiskCachePath()));
1252 EXPECT_FALSE(base::DirectoryExists(storage()->GetDatabasePath())); 1359 EXPECT_FALSE(base::DirectoryExists(storage()->GetDatabasePath()));
1253 } 1360 }
1254 1361
1255 TEST_F(ServiceWorkerResourceStorageDiskTest, 1362 TEST_F(ServiceWorkerResourceStorageDiskTest,
1256 DeleteAndStartOver_UnrelatedFileExists) { 1363 DeleteAndStartOver_UnrelatedFileExists) {
1257 EXPECT_FALSE(storage()->IsDisabled()); 1364 EXPECT_FALSE(storage()->IsDisabled());
1258 ASSERT_TRUE(base::DirectoryExists(storage()->GetDiskCachePath())); 1365 ASSERT_TRUE(base::DirectoryExists(storage()->GetDiskCachePath()));
1259 ASSERT_TRUE(base::DirectoryExists(storage()->GetDatabasePath())); 1366 ASSERT_TRUE(base::DirectoryExists(storage()->GetDatabasePath()));
1260 1367
1261 // Create an unrelated file in the database directory to make sure such a file 1368 // Create an unrelated file in the database directory to make sure such a file
1262 // does not prevent DeleteAndStartOver. 1369 // does not prevent DeleteAndStartOver.
1263 base::FilePath file_path; 1370 base::FilePath file_path;
1264 ASSERT_TRUE( 1371 ASSERT_TRUE(
1265 base::CreateTemporaryFileInDir(storage()->GetDatabasePath(), &file_path)); 1372 base::CreateTemporaryFileInDir(storage()->GetDatabasePath(), &file_path));
1266 ASSERT_TRUE(base::PathExists(file_path)); 1373 ASSERT_TRUE(base::PathExists(file_path));
1267 1374
1268 base::RunLoop run_loop; 1375 base::RunLoop run_loop;
1269 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_ABORT; 1376 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE;
1270 storage()->DeleteAndStartOver( 1377 storage()->DeleteAndStartOver(
1271 base::Bind(&StatusAndQuitCallback, &status, run_loop.QuitClosure())); 1378 base::Bind(&StatusAndQuitCallback, &status, run_loop.QuitClosure()));
1272 run_loop.Run(); 1379 run_loop.Run();
1273 1380
1274 EXPECT_EQ(SERVICE_WORKER_OK, status); 1381 EXPECT_EQ(SERVICE_WORKER_OK, status);
1275 EXPECT_TRUE(storage()->IsDisabled()); 1382 EXPECT_TRUE(storage()->IsDisabled());
1276 EXPECT_FALSE(base::DirectoryExists(storage()->GetDiskCachePath())); 1383 EXPECT_FALSE(base::DirectoryExists(storage()->GetDiskCachePath()));
1277 EXPECT_FALSE(base::DirectoryExists(storage()->GetDatabasePath())); 1384 EXPECT_FALSE(base::DirectoryExists(storage()->GetDatabasePath()));
1278 } 1385 }
1279 1386
1280 TEST_F(ServiceWorkerResourceStorageDiskTest, 1387 TEST_F(ServiceWorkerResourceStorageDiskTest,
1281 DeleteAndStartOver_OpenedFileExists) { 1388 DeleteAndStartOver_OpenedFileExists) {
1282 EXPECT_FALSE(storage()->IsDisabled()); 1389 EXPECT_FALSE(storage()->IsDisabled());
1283 ASSERT_TRUE(base::DirectoryExists(storage()->GetDiskCachePath())); 1390 ASSERT_TRUE(base::DirectoryExists(storage()->GetDiskCachePath()));
1284 ASSERT_TRUE(base::DirectoryExists(storage()->GetDatabasePath())); 1391 ASSERT_TRUE(base::DirectoryExists(storage()->GetDatabasePath()));
1285 1392
1286 // Create an unrelated opened file in the database directory to make sure such 1393 // Create an unrelated opened file in the database directory to make sure such
1287 // a file does not prevent DeleteAndStartOver on non-Windows platforms. 1394 // a file does not prevent DeleteAndStartOver on non-Windows platforms.
1288 base::FilePath file_path; 1395 base::FilePath file_path;
1289 base::ScopedFILE file(base::CreateAndOpenTemporaryFileInDir( 1396 base::ScopedFILE file(base::CreateAndOpenTemporaryFileInDir(
1290 storage()->GetDatabasePath(), &file_path)); 1397 storage()->GetDatabasePath(), &file_path));
1291 ASSERT_TRUE(file); 1398 ASSERT_TRUE(file);
1292 ASSERT_TRUE(base::PathExists(file_path)); 1399 ASSERT_TRUE(base::PathExists(file_path));
1293 1400
1294 base::RunLoop run_loop; 1401 base::RunLoop run_loop;
1295 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_ABORT; 1402 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_MAX_VALUE;
1296 storage()->DeleteAndStartOver( 1403 storage()->DeleteAndStartOver(
1297 base::Bind(&StatusAndQuitCallback, &status, run_loop.QuitClosure())); 1404 base::Bind(&StatusAndQuitCallback, &status, run_loop.QuitClosure()));
1298 run_loop.Run(); 1405 run_loop.Run();
1299 1406
1300 #if defined(OS_WIN) 1407 #if defined(OS_WIN)
1301 // On Windows, deleting the directory containing an opened file should fail. 1408 // On Windows, deleting the directory containing an opened file should fail.
1302 EXPECT_EQ(SERVICE_WORKER_ERROR_FAILED, status); 1409 EXPECT_EQ(SERVICE_WORKER_ERROR_FAILED, status);
1303 EXPECT_TRUE(storage()->IsDisabled()); 1410 EXPECT_TRUE(storage()->IsDisabled());
1304 EXPECT_TRUE(base::DirectoryExists(storage()->GetDiskCachePath())); 1411 EXPECT_TRUE(base::DirectoryExists(storage()->GetDiskCachePath()));
1305 EXPECT_TRUE(base::DirectoryExists(storage()->GetDatabasePath())); 1412 EXPECT_TRUE(base::DirectoryExists(storage()->GetDatabasePath()));
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
1568 // Remove other registration at first origin. 1675 // Remove other registration at first origin.
1569 EXPECT_EQ(SERVICE_WORKER_OK, 1676 EXPECT_EQ(SERVICE_WORKER_OK,
1570 DeleteRegistration(kRegistrationId2, kScope2.GetOrigin())); 1677 DeleteRegistration(kRegistrationId2, kScope2.GetOrigin()));
1571 1678
1572 // No foreign fetch registrations remain. 1679 // No foreign fetch registrations remain.
1573 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin1)); 1680 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin1));
1574 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin2)); 1681 EXPECT_FALSE(storage()->OriginHasForeignFetchRegistrations(kOrigin2));
1575 } 1682 }
1576 1683
1577 } // namespace content 1684 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698