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

Side by Side Diff: content/browser/appcache/appcache_database.cc

Issue 879393002: Expire appcaches that fail to update for "too long". (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 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 (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 "content/browser/appcache/appcache_database.h" 5 #include "content/browser/appcache/appcache_database.h"
6 6
7 #include "base/auto_reset.h" 7 #include "base/auto_reset.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "content/browser/appcache/appcache_entry.h" 13 #include "content/browser/appcache/appcache_entry.h"
14 #include "content/browser/appcache/appcache_histograms.h" 14 #include "content/browser/appcache/appcache_histograms.h"
15 #include "sql/connection.h" 15 #include "sql/connection.h"
16 #include "sql/error_delegate_util.h" 16 #include "sql/error_delegate_util.h"
17 #include "sql/meta_table.h" 17 #include "sql/meta_table.h"
18 #include "sql/statement.h" 18 #include "sql/statement.h"
19 #include "sql/transaction.h" 19 #include "sql/transaction.h"
20 20
21 namespace content { 21 namespace content {
22 22
23 // Schema ------------------------------------------------------------------- 23 // Schema -------------------------------------------------------------------
24 namespace { 24 namespace {
25 25
26 #if defined(APPCACHE_USE_SIMPLE_CACHE) 26 const int kCurrentVersion = 7;
27 const int kCurrentVersion = 6; 27 const int kCompatibleVersion = 7;
28 const int kCompatibleVersion = 6; 28 const bool kCreateIfNeeded = true;
29 #else 29 const bool kDontCreate = false;
30 const int kCurrentVersion = 5;
31 const int kCompatibleVersion = 5;
32 #endif
33 30
34 // A mechanism to run experiments that may affect in data being persisted 31 // A mechanism to run experiments that may affect in data being persisted
35 // in different ways such that when the experiment is toggled on/off via 32 // in different ways such that when the experiment is toggled on/off via
36 // cmd line flags, the database gets reset. The active flags are stored at 33 // cmd line flags, the database gets reset. The active flags are stored at
37 // the time of database creation and compared when reopening. If different 34 // the time of database creation and compared when reopening. If different
38 // the database is reset. 35 // the database is reset.
39 const char kExperimentFlagsKey[] = "ExperimentFlags"; 36 const char kExperimentFlagsKey[] = "ExperimentFlags";
40 37
41 const char kGroupsTable[] = "Groups"; 38 const char kGroupsTable[] = "Groups";
42 const char kCachesTable[] = "Caches"; 39 const char kCachesTable[] = "Caches";
(...skipping 13 matching lines...) Expand all
56 const char* columns; 53 const char* columns;
57 bool unique; 54 bool unique;
58 }; 55 };
59 56
60 const TableInfo kTables[] = { 57 const TableInfo kTables[] = {
61 { kGroupsTable, 58 { kGroupsTable,
62 "(group_id INTEGER PRIMARY KEY," 59 "(group_id INTEGER PRIMARY KEY,"
63 " origin TEXT," 60 " origin TEXT,"
64 " manifest_url TEXT," 61 " manifest_url TEXT,"
65 " creation_time INTEGER," 62 " creation_time INTEGER,"
66 " last_access_time INTEGER)" }, 63 " last_access_time INTEGER,"
64 " last_full_update_check_time INTEGER,"
65 " first_evictable_error_time INTEGER)" },
67 66
68 { kCachesTable, 67 { kCachesTable,
69 "(cache_id INTEGER PRIMARY KEY," 68 "(cache_id INTEGER PRIMARY KEY,"
70 " group_id INTEGER," 69 " group_id INTEGER,"
71 " online_wildcard INTEGER CHECK(online_wildcard IN (0, 1))," 70 " online_wildcard INTEGER CHECK(online_wildcard IN (0, 1)),"
72 " update_time INTEGER," 71 " update_time INTEGER,"
73 " cache_size INTEGER)" }, // intentionally not normalized 72 " cache_size INTEGER)" }, // intentionally not normalized
74 73
75 { kEntriesTable, 74 { kEntriesTable,
76 "(cache_id INTEGER," 75 "(cache_id INTEGER,"
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 return false; 238 return false;
240 for (std::set<GURL>::const_iterator origin = origins.begin(); 239 for (std::set<GURL>::const_iterator origin = origins.begin();
241 origin != origins.end(); ++origin) { 240 origin != origins.end(); ++origin) {
242 (*usage_map)[*origin] = GetOriginUsage(*origin); 241 (*usage_map)[*origin] = GetOriginUsage(*origin);
243 } 242 }
244 return true; 243 return true;
245 } 244 }
246 245
247 bool AppCacheDatabase::FindOriginsWithGroups(std::set<GURL>* origins) { 246 bool AppCacheDatabase::FindOriginsWithGroups(std::set<GURL>* origins) {
248 DCHECK(origins && origins->empty()); 247 DCHECK(origins && origins->empty());
249 if (!LazyOpen(false)) 248 if (!LazyOpen(kDontCreate))
250 return false; 249 return false;
251 250
252 const char* kSql = 251 const char kSql[] =
253 "SELECT DISTINCT(origin) FROM Groups"; 252 "SELECT DISTINCT(origin) FROM Groups";
254 253
255 sql::Statement statement(db_->GetUniqueStatement(kSql)); 254 sql::Statement statement(db_->GetUniqueStatement(kSql));
256 255
257 while (statement.Step()) 256 while (statement.Step())
258 origins->insert(GURL(statement.ColumnString(0))); 257 origins->insert(GURL(statement.ColumnString(0)));
259 258
260 return statement.Succeeded(); 259 return statement.Succeeded();
261 } 260 }
262 261
263 bool AppCacheDatabase::FindLastStorageIds( 262 bool AppCacheDatabase::FindLastStorageIds(
264 int64* last_group_id, int64* last_cache_id, int64* last_response_id, 263 int64* last_group_id, int64* last_cache_id, int64* last_response_id,
265 int64* last_deletable_response_rowid) { 264 int64* last_deletable_response_rowid) {
266 DCHECK(last_group_id && last_cache_id && last_response_id && 265 DCHECK(last_group_id && last_cache_id && last_response_id &&
267 last_deletable_response_rowid); 266 last_deletable_response_rowid);
268 267
269 *last_group_id = 0; 268 *last_group_id = 0;
270 *last_cache_id = 0; 269 *last_cache_id = 0;
271 *last_response_id = 0; 270 *last_response_id = 0;
272 *last_deletable_response_rowid = 0; 271 *last_deletable_response_rowid = 0;
273 272
274 if (!LazyOpen(false)) 273 if (!LazyOpen(kDontCreate))
275 return false; 274 return false;
276 275
277 const char* kMaxGroupIdSql = "SELECT MAX(group_id) FROM Groups"; 276 const char* kMaxGroupIdSql = "SELECT MAX(group_id) FROM Groups";
278 const char* kMaxCacheIdSql = "SELECT MAX(cache_id) FROM Caches"; 277 const char* kMaxCacheIdSql = "SELECT MAX(cache_id) FROM Caches";
279 const char* kMaxResponseIdFromEntriesSql = 278 const char* kMaxResponseIdFromEntriesSql =
280 "SELECT MAX(response_id) FROM Entries"; 279 "SELECT MAX(response_id) FROM Entries";
281 const char* kMaxResponseIdFromDeletablesSql = 280 const char* kMaxResponseIdFromDeletablesSql =
282 "SELECT MAX(response_id) FROM DeletableResponseIds"; 281 "SELECT MAX(response_id) FROM DeletableResponseIds";
283 const char* kMaxDeletableResponseRowIdSql = 282 const char* kMaxDeletableResponseRowIdSql =
284 "SELECT MAX(rowid) FROM DeletableResponseIds"; 283 "SELECT MAX(rowid) FROM DeletableResponseIds";
(...skipping 16 matching lines...) Expand all
301 *last_group_id = max_group_id; 300 *last_group_id = max_group_id;
302 *last_cache_id = max_cache_id; 301 *last_cache_id = max_cache_id;
303 *last_response_id = std::max(max_response_id_from_entries, 302 *last_response_id = std::max(max_response_id_from_entries,
304 max_response_id_from_deletables); 303 max_response_id_from_deletables);
305 *last_deletable_response_rowid = max_deletable_response_rowid; 304 *last_deletable_response_rowid = max_deletable_response_rowid;
306 return true; 305 return true;
307 } 306 }
308 307
309 bool AppCacheDatabase::FindGroup(int64 group_id, GroupRecord* record) { 308 bool AppCacheDatabase::FindGroup(int64 group_id, GroupRecord* record) {
310 DCHECK(record); 309 DCHECK(record);
311 if (!LazyOpen(false)) 310 if (!LazyOpen(kDontCreate))
312 return false; 311 return false;
313 312
314 const char* kSql = 313 const char kSql[] =
315 "SELECT group_id, origin, manifest_url," 314 "SELECT group_id, origin, manifest_url,"
316 " creation_time, last_access_time" 315 " creation_time, last_access_time,"
316 " last_full_update_check_time,"
317 " first_evictable_error_time"
317 " FROM Groups WHERE group_id = ?"; 318 " FROM Groups WHERE group_id = ?";
318 319
319 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 320 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
320 321
321 statement.BindInt64(0, group_id); 322 statement.BindInt64(0, group_id);
322 if (!statement.Step()) 323 if (!statement.Step())
323 return false; 324 return false;
324 325
325 ReadGroupRecord(statement, record); 326 ReadGroupRecord(statement, record);
326 DCHECK(record->group_id == group_id); 327 DCHECK(record->group_id == group_id);
327 return true; 328 return true;
328 } 329 }
329 330
330 bool AppCacheDatabase::FindGroupForManifestUrl( 331 bool AppCacheDatabase::FindGroupForManifestUrl(
331 const GURL& manifest_url, GroupRecord* record) { 332 const GURL& manifest_url, GroupRecord* record) {
332 DCHECK(record); 333 DCHECK(record);
333 if (!LazyOpen(false)) 334 if (!LazyOpen(kDontCreate))
334 return false; 335 return false;
335 336
336 const char* kSql = 337 const char kSql[] =
337 "SELECT group_id, origin, manifest_url," 338 "SELECT group_id, origin, manifest_url,"
338 " creation_time, last_access_time" 339 " creation_time, last_access_time,"
340 " last_full_update_check_time,"
341 " first_evictable_error_time"
339 " FROM Groups WHERE manifest_url = ?"; 342 " FROM Groups WHERE manifest_url = ?";
340 343
341 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 344 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
342 statement.BindString(0, manifest_url.spec()); 345 statement.BindString(0, manifest_url.spec());
343 346
344 if (!statement.Step()) 347 if (!statement.Step())
345 return false; 348 return false;
346 349
347 ReadGroupRecord(statement, record); 350 ReadGroupRecord(statement, record);
348 DCHECK(record->manifest_url == manifest_url); 351 DCHECK(record->manifest_url == manifest_url);
349 return true; 352 return true;
350 } 353 }
351 354
352 bool AppCacheDatabase::FindGroupsForOrigin( 355 bool AppCacheDatabase::FindGroupsForOrigin(
353 const GURL& origin, std::vector<GroupRecord>* records) { 356 const GURL& origin, std::vector<GroupRecord>* records) {
354 DCHECK(records && records->empty()); 357 DCHECK(records && records->empty());
355 if (!LazyOpen(false)) 358 if (!LazyOpen(kDontCreate))
356 return false; 359 return false;
357 360
358 const char* kSql = 361 const char kSql[] =
359 "SELECT group_id, origin, manifest_url," 362 "SELECT group_id, origin, manifest_url,"
360 " creation_time, last_access_time" 363 " creation_time, last_access_time,"
364 " last_full_update_check_time,"
365 " first_evictable_error_time"
361 " FROM Groups WHERE origin = ?"; 366 " FROM Groups WHERE origin = ?";
362 367
363 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 368 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
364 statement.BindString(0, origin.spec()); 369 statement.BindString(0, origin.spec());
365 370
366 while (statement.Step()) { 371 while (statement.Step()) {
367 records->push_back(GroupRecord()); 372 records->push_back(GroupRecord());
368 ReadGroupRecord(statement, &records->back()); 373 ReadGroupRecord(statement, &records->back());
369 DCHECK(records->back().origin == origin); 374 DCHECK(records->back().origin == origin);
370 } 375 }
371 376
372 return statement.Succeeded(); 377 return statement.Succeeded();
373 } 378 }
374 379
375 bool AppCacheDatabase::FindGroupForCache(int64 cache_id, GroupRecord* record) { 380 bool AppCacheDatabase::FindGroupForCache(int64 cache_id, GroupRecord* record) {
376 DCHECK(record); 381 DCHECK(record);
377 if (!LazyOpen(false)) 382 if (!LazyOpen(kDontCreate))
378 return false; 383 return false;
379 384
380 const char* kSql = 385 const char kSql[] =
381 "SELECT g.group_id, g.origin, g.manifest_url," 386 "SELECT g.group_id, g.origin, g.manifest_url,"
382 " g.creation_time, g.last_access_time" 387 " g.creation_time, g.last_access_time,"
388 " g.last_full_update_check_time,"
389 " g.first_evictable_error_time"
383 " FROM Groups g, Caches c" 390 " FROM Groups g, Caches c"
384 " WHERE c.cache_id = ? AND c.group_id = g.group_id"; 391 " WHERE c.cache_id = ? AND c.group_id = g.group_id";
385 392
386 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 393 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
387 statement.BindInt64(0, cache_id); 394 statement.BindInt64(0, cache_id);
388 395
389 if (!statement.Step()) 396 if (!statement.Step())
390 return false; 397 return false;
391 398
392 ReadGroupRecord(statement, record); 399 ReadGroupRecord(statement, record);
393 return true; 400 return true;
394 } 401 }
395 402
403 bool AppCacheDatabase::InsertGroup(const GroupRecord* record) {
404 if (!LazyOpen(kCreateIfNeeded))
405 return false;
406
407 const char kSql[] =
408 "INSERT INTO Groups"
409 " (group_id, origin, manifest_url, creation_time, last_access_time,"
410 " last_full_update_check_time, first_evictable_error_time)"
411 " VALUES(?, ?, ?, ?, ?, ?, ?)";
412 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
413 statement.BindInt64(0, record->group_id);
414 statement.BindString(1, record->origin.spec());
415 statement.BindString(2, record->manifest_url.spec());
416 statement.BindInt64(3, record->creation_time.ToInternalValue());
417 statement.BindInt64(4, record->last_access_time.ToInternalValue());
418 statement.BindInt64(5, record->last_full_update_check_time.ToInternalValue());
419 statement.BindInt64(6, record->first_evictable_error_time.ToInternalValue());
420 return statement.Run();
421 }
422
423 bool AppCacheDatabase::DeleteGroup(int64 group_id) {
424 if (!LazyOpen(kDontCreate))
425 return false;
426
427 const char kSql[] =
428 "DELETE FROM Groups WHERE group_id = ?";
429 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
430 statement.BindInt64(0, group_id);
431 return statement.Run();
432 }
433
396 bool AppCacheDatabase::UpdateLastAccessTime( 434 bool AppCacheDatabase::UpdateLastAccessTime(
397 int64 group_id, base::Time time) { 435 int64 group_id, base::Time time) {
398 if (!LazyOpen(true)) 436 if (!LazyUpdateLastAccessTime(group_id, time))
399 return false; 437 return false;
400 438 return CommitLazyLastAccessTimes();
401 const char* kSql =
402 "UPDATE Groups SET last_access_time = ? WHERE group_id = ?";
403
404 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
405 statement.BindInt64(0, time.ToInternalValue());
406 statement.BindInt64(1, group_id);
407
408 return statement.Run();
409 } 439 }
410 440
411 bool AppCacheDatabase::LazyUpdateLastAccessTime( 441 bool AppCacheDatabase::LazyUpdateLastAccessTime(
412 int64 group_id, base::Time time) { 442 int64 group_id, base::Time time) {
413 if (!LazyOpen(true)) 443 if (!LazyOpen(kCreateIfNeeded))
414 return false; 444 return false;
415 lazy_last_access_times_[group_id] = time; 445 lazy_last_access_times_[group_id] = time;
416 return true; 446 return true;
417 } 447 }
418 448
419 bool AppCacheDatabase::CommitLazyLastAccessTimes() { 449 bool AppCacheDatabase::CommitLazyLastAccessTimes() {
420 if (lazy_last_access_times_.empty()) 450 if (lazy_last_access_times_.empty())
421 return true; 451 return true;
422 if (!LazyOpen(false)) 452 if (!LazyOpen(kDontCreate))
423 return false; 453 return false;
424 454
425 sql::Transaction transaction(db_.get()); 455 sql::Transaction transaction(db_.get());
426 if (!transaction.Begin()) 456 if (!transaction.Begin())
427 return false; 457 return false;
428 for (const auto& pair : lazy_last_access_times_) 458 for (const auto& pair : lazy_last_access_times_) {
429 UpdateLastAccessTime(pair.first, pair.second); 459 const char kSql[] =
460 "UPDATE Groups SET last_access_time = ? WHERE group_id = ?";
461 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
462 statement.BindInt64(0, pair.second.ToInternalValue()); // time
463 statement.BindInt64(1, pair.first); // group_id
464 statement.Run();
465 }
430 lazy_last_access_times_.clear(); 466 lazy_last_access_times_.clear();
431 return transaction.Commit(); 467 return transaction.Commit();
432 } 468 }
433 469
434 bool AppCacheDatabase::InsertGroup(const GroupRecord* record) { 470 bool AppCacheDatabase::UpdateEvictionTimes(
435 if (!LazyOpen(true)) 471 int64 group_id,
472 base::Time last_full_update_check_time,
473 base::Time first_evictable_error_time) {
474 if (!LazyOpen(kCreateIfNeeded))
436 return false; 475 return false;
437 476
438 const char* kSql = 477 const char kSql[] =
439 "INSERT INTO Groups" 478 "UPDATE Groups"
440 " (group_id, origin, manifest_url, creation_time, last_access_time)" 479 " SET last_full_update_check_time = ?, first_evictable_error_time = ?"
441 " VALUES(?, ?, ?, ?, ?)"; 480 " WHERE group_id = ?";
442
443 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 481 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
444 statement.BindInt64(0, record->group_id); 482 statement.BindInt64(0, last_full_update_check_time.ToInternalValue());
445 statement.BindString(1, record->origin.spec()); 483 statement.BindInt64(1, first_evictable_error_time.ToInternalValue());
446 statement.BindString(2, record->manifest_url.spec()); 484 statement.BindInt64(2, group_id);
447 statement.BindInt64(3, record->creation_time.ToInternalValue()); 485 return statement.Run(); // Will succeed even if group_id is invalid.
448 statement.BindInt64(4, record->last_access_time.ToInternalValue());
449
450 return statement.Run();
451 }
452
453 bool AppCacheDatabase::DeleteGroup(int64 group_id) {
454 if (!LazyOpen(false))
455 return false;
456
457 const char* kSql =
458 "DELETE FROM Groups WHERE group_id = ?";
459
460 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
461 statement.BindInt64(0, group_id);
462
463 return statement.Run();
464 } 486 }
465 487
466 bool AppCacheDatabase::FindCache(int64 cache_id, CacheRecord* record) { 488 bool AppCacheDatabase::FindCache(int64 cache_id, CacheRecord* record) {
467 DCHECK(record); 489 DCHECK(record);
468 if (!LazyOpen(false)) 490 if (!LazyOpen(kDontCreate))
469 return false; 491 return false;
470 492
471 const char* kSql = 493 const char kSql[] =
472 "SELECT cache_id, group_id, online_wildcard, update_time, cache_size" 494 "SELECT cache_id, group_id, online_wildcard, update_time, cache_size"
473 " FROM Caches WHERE cache_id = ?"; 495 " FROM Caches WHERE cache_id = ?";
474 496
475 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 497 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
476 statement.BindInt64(0, cache_id); 498 statement.BindInt64(0, cache_id);
477 499
478 if (!statement.Step()) 500 if (!statement.Step())
479 return false; 501 return false;
480 502
481 ReadCacheRecord(statement, record); 503 ReadCacheRecord(statement, record);
482 return true; 504 return true;
483 } 505 }
484 506
485 bool AppCacheDatabase::FindCacheForGroup(int64 group_id, CacheRecord* record) { 507 bool AppCacheDatabase::FindCacheForGroup(int64 group_id, CacheRecord* record) {
486 DCHECK(record); 508 DCHECK(record);
487 if (!LazyOpen(false)) 509 if (!LazyOpen(kDontCreate))
488 return false; 510 return false;
489 511
490 const char* kSql = 512 const char kSql[] =
491 "SELECT cache_id, group_id, online_wildcard, update_time, cache_size" 513 "SELECT cache_id, group_id, online_wildcard, update_time, cache_size"
492 " FROM Caches WHERE group_id = ?"; 514 " FROM Caches WHERE group_id = ?";
493 515
494 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 516 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
495 statement.BindInt64(0, group_id); 517 statement.BindInt64(0, group_id);
496 518
497 if (!statement.Step()) 519 if (!statement.Step())
498 return false; 520 return false;
499 521
500 ReadCacheRecord(statement, record); 522 ReadCacheRecord(statement, record);
(...skipping 11 matching lines...) Expand all
512 std::vector<GroupRecord>::const_iterator iter = group_records.begin(); 534 std::vector<GroupRecord>::const_iterator iter = group_records.begin();
513 while (iter != group_records.end()) { 535 while (iter != group_records.end()) {
514 if (FindCacheForGroup(iter->group_id, &cache_record)) 536 if (FindCacheForGroup(iter->group_id, &cache_record))
515 records->push_back(cache_record); 537 records->push_back(cache_record);
516 ++iter; 538 ++iter;
517 } 539 }
518 return true; 540 return true;
519 } 541 }
520 542
521 bool AppCacheDatabase::InsertCache(const CacheRecord* record) { 543 bool AppCacheDatabase::InsertCache(const CacheRecord* record) {
522 if (!LazyOpen(true)) 544 if (!LazyOpen(kCreateIfNeeded))
523 return false; 545 return false;
524 546
525 const char* kSql = 547 const char kSql[] =
526 "INSERT INTO Caches (cache_id, group_id, online_wildcard," 548 "INSERT INTO Caches (cache_id, group_id, online_wildcard,"
527 " update_time, cache_size)" 549 " update_time, cache_size)"
528 " VALUES(?, ?, ?, ?, ?)"; 550 " VALUES(?, ?, ?, ?, ?)";
529 551
530 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 552 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
531 statement.BindInt64(0, record->cache_id); 553 statement.BindInt64(0, record->cache_id);
532 statement.BindInt64(1, record->group_id); 554 statement.BindInt64(1, record->group_id);
533 statement.BindBool(2, record->online_wildcard); 555 statement.BindBool(2, record->online_wildcard);
534 statement.BindInt64(3, record->update_time.ToInternalValue()); 556 statement.BindInt64(3, record->update_time.ToInternalValue());
535 statement.BindInt64(4, record->cache_size); 557 statement.BindInt64(4, record->cache_size);
536 558
537 return statement.Run(); 559 return statement.Run();
538 } 560 }
539 561
540 bool AppCacheDatabase::DeleteCache(int64 cache_id) { 562 bool AppCacheDatabase::DeleteCache(int64 cache_id) {
541 if (!LazyOpen(false)) 563 if (!LazyOpen(kDontCreate))
542 return false; 564 return false;
543 565
544 const char* kSql = 566 const char kSql[] =
545 "DELETE FROM Caches WHERE cache_id = ?"; 567 "DELETE FROM Caches WHERE cache_id = ?";
546 568
547 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 569 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
548 statement.BindInt64(0, cache_id); 570 statement.BindInt64(0, cache_id);
549 571
550 return statement.Run(); 572 return statement.Run();
551 } 573 }
552 574
553 bool AppCacheDatabase::FindEntriesForCache( 575 bool AppCacheDatabase::FindEntriesForCache(
554 int64 cache_id, std::vector<EntryRecord>* records) { 576 int64 cache_id, std::vector<EntryRecord>* records) {
555 DCHECK(records && records->empty()); 577 DCHECK(records && records->empty());
556 if (!LazyOpen(false)) 578 if (!LazyOpen(kDontCreate))
557 return false; 579 return false;
558 580
559 const char* kSql = 581 const char kSql[] =
560 "SELECT cache_id, url, flags, response_id, response_size FROM Entries" 582 "SELECT cache_id, url, flags, response_id, response_size FROM Entries"
561 " WHERE cache_id = ?"; 583 " WHERE cache_id = ?";
562 584
563 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 585 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
564 statement.BindInt64(0, cache_id); 586 statement.BindInt64(0, cache_id);
565 587
566 while (statement.Step()) { 588 while (statement.Step()) {
567 records->push_back(EntryRecord()); 589 records->push_back(EntryRecord());
568 ReadEntryRecord(statement, &records->back()); 590 ReadEntryRecord(statement, &records->back());
569 DCHECK(records->back().cache_id == cache_id); 591 DCHECK(records->back().cache_id == cache_id);
570 } 592 }
571 593
572 return statement.Succeeded(); 594 return statement.Succeeded();
573 } 595 }
574 596
575 bool AppCacheDatabase::FindEntriesForUrl( 597 bool AppCacheDatabase::FindEntriesForUrl(
576 const GURL& url, std::vector<EntryRecord>* records) { 598 const GURL& url, std::vector<EntryRecord>* records) {
577 DCHECK(records && records->empty()); 599 DCHECK(records && records->empty());
578 if (!LazyOpen(false)) 600 if (!LazyOpen(kDontCreate))
579 return false; 601 return false;
580 602
581 const char* kSql = 603 const char kSql[] =
582 "SELECT cache_id, url, flags, response_id, response_size FROM Entries" 604 "SELECT cache_id, url, flags, response_id, response_size FROM Entries"
583 " WHERE url = ?"; 605 " WHERE url = ?";
584 606
585 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 607 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
586 statement.BindString(0, url.spec()); 608 statement.BindString(0, url.spec());
587 609
588 while (statement.Step()) { 610 while (statement.Step()) {
589 records->push_back(EntryRecord()); 611 records->push_back(EntryRecord());
590 ReadEntryRecord(statement, &records->back()); 612 ReadEntryRecord(statement, &records->back());
591 DCHECK(records->back().url == url); 613 DCHECK(records->back().url == url);
592 } 614 }
593 615
594 return statement.Succeeded(); 616 return statement.Succeeded();
595 } 617 }
596 618
597 bool AppCacheDatabase::FindEntry( 619 bool AppCacheDatabase::FindEntry(
598 int64 cache_id, const GURL& url, EntryRecord* record) { 620 int64 cache_id, const GURL& url, EntryRecord* record) {
599 DCHECK(record); 621 DCHECK(record);
600 if (!LazyOpen(false)) 622 if (!LazyOpen(kDontCreate))
601 return false; 623 return false;
602 624
603 const char* kSql = 625 const char kSql[] =
604 "SELECT cache_id, url, flags, response_id, response_size FROM Entries" 626 "SELECT cache_id, url, flags, response_id, response_size FROM Entries"
605 " WHERE cache_id = ? AND url = ?"; 627 " WHERE cache_id = ? AND url = ?";
606 628
607 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 629 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
608 statement.BindInt64(0, cache_id); 630 statement.BindInt64(0, cache_id);
609 statement.BindString(1, url.spec()); 631 statement.BindString(1, url.spec());
610 632
611 if (!statement.Step()) 633 if (!statement.Step())
612 return false; 634 return false;
613 635
614 ReadEntryRecord(statement, record); 636 ReadEntryRecord(statement, record);
615 DCHECK(record->cache_id == cache_id); 637 DCHECK(record->cache_id == cache_id);
616 DCHECK(record->url == url); 638 DCHECK(record->url == url);
617 return true; 639 return true;
618 } 640 }
619 641
620 bool AppCacheDatabase::InsertEntry(const EntryRecord* record) { 642 bool AppCacheDatabase::InsertEntry(const EntryRecord* record) {
621 if (!LazyOpen(true)) 643 if (!LazyOpen(kCreateIfNeeded))
622 return false; 644 return false;
623 645
624 const char* kSql = 646 const char kSql[] =
625 "INSERT INTO Entries (cache_id, url, flags, response_id, response_size)" 647 "INSERT INTO Entries (cache_id, url, flags, response_id, response_size)"
626 " VALUES(?, ?, ?, ?, ?)"; 648 " VALUES(?, ?, ?, ?, ?)";
627 649
628 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 650 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
629 statement.BindInt64(0, record->cache_id); 651 statement.BindInt64(0, record->cache_id);
630 statement.BindString(1, record->url.spec()); 652 statement.BindString(1, record->url.spec());
631 statement.BindInt(2, record->flags); 653 statement.BindInt(2, record->flags);
632 statement.BindInt64(3, record->response_id); 654 statement.BindInt64(3, record->response_id);
633 statement.BindInt64(4, record->response_size); 655 statement.BindInt64(4, record->response_size);
634 656
(...skipping 10 matching lines...) Expand all
645 std::vector<EntryRecord>::const_iterator iter = records.begin(); 667 std::vector<EntryRecord>::const_iterator iter = records.begin();
646 while (iter != records.end()) { 668 while (iter != records.end()) {
647 if (!InsertEntry(&(*iter))) 669 if (!InsertEntry(&(*iter)))
648 return false; 670 return false;
649 ++iter; 671 ++iter;
650 } 672 }
651 return transaction.Commit(); 673 return transaction.Commit();
652 } 674 }
653 675
654 bool AppCacheDatabase::DeleteEntriesForCache(int64 cache_id) { 676 bool AppCacheDatabase::DeleteEntriesForCache(int64 cache_id) {
655 if (!LazyOpen(false)) 677 if (!LazyOpen(kDontCreate))
656 return false; 678 return false;
657 679
658 const char* kSql = 680 const char kSql[] =
659 "DELETE FROM Entries WHERE cache_id = ?"; 681 "DELETE FROM Entries WHERE cache_id = ?";
660 682
661 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 683 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
662 statement.BindInt64(0, cache_id); 684 statement.BindInt64(0, cache_id);
663 685
664 return statement.Run(); 686 return statement.Run();
665 } 687 }
666 688
667 bool AppCacheDatabase::AddEntryFlags( 689 bool AppCacheDatabase::AddEntryFlags(
668 const GURL& entry_url, int64 cache_id, int additional_flags) { 690 const GURL& entry_url, int64 cache_id, int additional_flags) {
669 if (!LazyOpen(false)) 691 if (!LazyOpen(kDontCreate))
670 return false; 692 return false;
671 693
672 const char* kSql = 694 const char kSql[] =
673 "UPDATE Entries SET flags = flags | ? WHERE cache_id = ? AND url = ?"; 695 "UPDATE Entries SET flags = flags | ? WHERE cache_id = ? AND url = ?";
674 696
675 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 697 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
676 statement.BindInt(0, additional_flags); 698 statement.BindInt(0, additional_flags);
677 statement.BindInt64(1, cache_id); 699 statement.BindInt64(1, cache_id);
678 statement.BindString(2, entry_url.spec()); 700 statement.BindString(2, entry_url.spec());
679 701
680 return statement.Run() && db_->GetLastChangeCount(); 702 return statement.Run() && db_->GetLastChangeCount();
681 } 703 }
682 704
683 bool AppCacheDatabase::FindNamespacesForOrigin( 705 bool AppCacheDatabase::FindNamespacesForOrigin(
684 const GURL& origin, 706 const GURL& origin,
685 std::vector<NamespaceRecord>* intercepts, 707 std::vector<NamespaceRecord>* intercepts,
686 std::vector<NamespaceRecord>* fallbacks) { 708 std::vector<NamespaceRecord>* fallbacks) {
687 DCHECK(intercepts && intercepts->empty()); 709 DCHECK(intercepts && intercepts->empty());
688 DCHECK(fallbacks && fallbacks->empty()); 710 DCHECK(fallbacks && fallbacks->empty());
689 if (!LazyOpen(false)) 711 if (!LazyOpen(kDontCreate))
690 return false; 712 return false;
691 713
692 const char* kSql = 714 const char kSql[] =
693 "SELECT cache_id, origin, type, namespace_url, target_url, is_pattern" 715 "SELECT cache_id, origin, type, namespace_url, target_url, is_pattern"
694 " FROM Namespaces WHERE origin = ?"; 716 " FROM Namespaces WHERE origin = ?";
695 717
696 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 718 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
697 statement.BindString(0, origin.spec()); 719 statement.BindString(0, origin.spec());
698 720
699 ReadNamespaceRecords(&statement, intercepts, fallbacks); 721 ReadNamespaceRecords(&statement, intercepts, fallbacks);
700 722
701 return statement.Succeeded(); 723 return statement.Succeeded();
702 } 724 }
703 725
704 bool AppCacheDatabase::FindNamespacesForCache( 726 bool AppCacheDatabase::FindNamespacesForCache(
705 int64 cache_id, 727 int64 cache_id,
706 std::vector<NamespaceRecord>* intercepts, 728 std::vector<NamespaceRecord>* intercepts,
707 std::vector<NamespaceRecord>* fallbacks) { 729 std::vector<NamespaceRecord>* fallbacks) {
708 DCHECK(intercepts && intercepts->empty()); 730 DCHECK(intercepts && intercepts->empty());
709 DCHECK(fallbacks && fallbacks->empty()); 731 DCHECK(fallbacks && fallbacks->empty());
710 if (!LazyOpen(false)) 732 if (!LazyOpen(kDontCreate))
711 return false; 733 return false;
712 734
713 const char* kSql = 735 const char kSql[] =
714 "SELECT cache_id, origin, type, namespace_url, target_url, is_pattern" 736 "SELECT cache_id, origin, type, namespace_url, target_url, is_pattern"
715 " FROM Namespaces WHERE cache_id = ?"; 737 " FROM Namespaces WHERE cache_id = ?";
716 738
717 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 739 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
718 statement.BindInt64(0, cache_id); 740 statement.BindInt64(0, cache_id);
719 741
720 ReadNamespaceRecords(&statement, intercepts, fallbacks); 742 ReadNamespaceRecords(&statement, intercepts, fallbacks);
721 743
722 return statement.Succeeded(); 744 return statement.Succeeded();
723 } 745 }
724 746
725 bool AppCacheDatabase::InsertNamespace( 747 bool AppCacheDatabase::InsertNamespace(
726 const NamespaceRecord* record) { 748 const NamespaceRecord* record) {
727 if (!LazyOpen(true)) 749 if (!LazyOpen(kCreateIfNeeded))
728 return false; 750 return false;
729 751
730 const char* kSql = 752 const char kSql[] =
731 "INSERT INTO Namespaces" 753 "INSERT INTO Namespaces"
732 " (cache_id, origin, type, namespace_url, target_url, is_pattern)" 754 " (cache_id, origin, type, namespace_url, target_url, is_pattern)"
733 " VALUES (?, ?, ?, ?, ?, ?)"; 755 " VALUES (?, ?, ?, ?, ?, ?)";
734 756
735 // Note: quick and dirty storage for the 'executable' bit w/o changing 757 // Note: quick and dirty storage for the 'executable' bit w/o changing
736 // schemas, we use the high bit of 'type' field. 758 // schemas, we use the high bit of 'type' field.
737 int type_with_executable_bit = record->namespace_.type; 759 int type_with_executable_bit = record->namespace_.type;
738 if (record->namespace_.is_executable) { 760 if (record->namespace_.is_executable) {
739 type_with_executable_bit |= 0x8000000; 761 type_with_executable_bit |= 0x8000000;
740 DCHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( 762 DCHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
(...skipping 20 matching lines...) Expand all
761 std::vector<NamespaceRecord>::const_iterator iter = records.begin(); 783 std::vector<NamespaceRecord>::const_iterator iter = records.begin();
762 while (iter != records.end()) { 784 while (iter != records.end()) {
763 if (!InsertNamespace(&(*iter))) 785 if (!InsertNamespace(&(*iter)))
764 return false; 786 return false;
765 ++iter; 787 ++iter;
766 } 788 }
767 return transaction.Commit(); 789 return transaction.Commit();
768 } 790 }
769 791
770 bool AppCacheDatabase::DeleteNamespacesForCache(int64 cache_id) { 792 bool AppCacheDatabase::DeleteNamespacesForCache(int64 cache_id) {
771 if (!LazyOpen(false)) 793 if (!LazyOpen(kDontCreate))
772 return false; 794 return false;
773 795
774 const char* kSql = 796 const char kSql[] =
775 "DELETE FROM Namespaces WHERE cache_id = ?"; 797 "DELETE FROM Namespaces WHERE cache_id = ?";
776 798
777 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 799 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
778 statement.BindInt64(0, cache_id); 800 statement.BindInt64(0, cache_id);
779 801
780 return statement.Run(); 802 return statement.Run();
781 } 803 }
782 804
783 bool AppCacheDatabase::FindOnlineWhiteListForCache( 805 bool AppCacheDatabase::FindOnlineWhiteListForCache(
784 int64 cache_id, std::vector<OnlineWhiteListRecord>* records) { 806 int64 cache_id, std::vector<OnlineWhiteListRecord>* records) {
785 DCHECK(records && records->empty()); 807 DCHECK(records && records->empty());
786 if (!LazyOpen(false)) 808 if (!LazyOpen(kDontCreate))
787 return false; 809 return false;
788 810
789 const char* kSql = 811 const char kSql[] =
790 "SELECT cache_id, namespace_url, is_pattern FROM OnlineWhiteLists" 812 "SELECT cache_id, namespace_url, is_pattern FROM OnlineWhiteLists"
791 " WHERE cache_id = ?"; 813 " WHERE cache_id = ?";
792 814
793 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 815 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
794 statement.BindInt64(0, cache_id); 816 statement.BindInt64(0, cache_id);
795 817
796 while (statement.Step()) { 818 while (statement.Step()) {
797 records->push_back(OnlineWhiteListRecord()); 819 records->push_back(OnlineWhiteListRecord());
798 this->ReadOnlineWhiteListRecord(statement, &records->back()); 820 this->ReadOnlineWhiteListRecord(statement, &records->back());
799 DCHECK(records->back().cache_id == cache_id); 821 DCHECK(records->back().cache_id == cache_id);
800 } 822 }
801 return statement.Succeeded(); 823 return statement.Succeeded();
802 } 824 }
803 825
804 bool AppCacheDatabase::InsertOnlineWhiteList( 826 bool AppCacheDatabase::InsertOnlineWhiteList(
805 const OnlineWhiteListRecord* record) { 827 const OnlineWhiteListRecord* record) {
806 if (!LazyOpen(true)) 828 if (!LazyOpen(kCreateIfNeeded))
807 return false; 829 return false;
808 830
809 const char* kSql = 831 const char kSql[] =
810 "INSERT INTO OnlineWhiteLists (cache_id, namespace_url, is_pattern)" 832 "INSERT INTO OnlineWhiteLists (cache_id, namespace_url, is_pattern)"
811 " VALUES (?, ?, ?)"; 833 " VALUES (?, ?, ?)";
812 834
813 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 835 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
814 statement.BindInt64(0, record->cache_id); 836 statement.BindInt64(0, record->cache_id);
815 statement.BindString(1, record->namespace_url.spec()); 837 statement.BindString(1, record->namespace_url.spec());
816 statement.BindBool(2, record->is_pattern); 838 statement.BindBool(2, record->is_pattern);
817 839
818 return statement.Run(); 840 return statement.Run();
819 } 841 }
820 842
821 bool AppCacheDatabase::InsertOnlineWhiteListRecords( 843 bool AppCacheDatabase::InsertOnlineWhiteListRecords(
822 const std::vector<OnlineWhiteListRecord>& records) { 844 const std::vector<OnlineWhiteListRecord>& records) {
823 if (records.empty()) 845 if (records.empty())
824 return true; 846 return true;
825 sql::Transaction transaction(db_.get()); 847 sql::Transaction transaction(db_.get());
826 if (!transaction.Begin()) 848 if (!transaction.Begin())
827 return false; 849 return false;
828 std::vector<OnlineWhiteListRecord>::const_iterator iter = records.begin(); 850 std::vector<OnlineWhiteListRecord>::const_iterator iter = records.begin();
829 while (iter != records.end()) { 851 while (iter != records.end()) {
830 if (!InsertOnlineWhiteList(&(*iter))) 852 if (!InsertOnlineWhiteList(&(*iter)))
831 return false; 853 return false;
832 ++iter; 854 ++iter;
833 } 855 }
834 return transaction.Commit(); 856 return transaction.Commit();
835 } 857 }
836 858
837 bool AppCacheDatabase::DeleteOnlineWhiteListForCache(int64 cache_id) { 859 bool AppCacheDatabase::DeleteOnlineWhiteListForCache(int64 cache_id) {
838 if (!LazyOpen(false)) 860 if (!LazyOpen(kDontCreate))
839 return false; 861 return false;
840 862
841 const char* kSql = 863 const char kSql[] =
842 "DELETE FROM OnlineWhiteLists WHERE cache_id = ?"; 864 "DELETE FROM OnlineWhiteLists WHERE cache_id = ?";
843 865
844 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 866 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
845 statement.BindInt64(0, cache_id); 867 statement.BindInt64(0, cache_id);
846 868
847 return statement.Run(); 869 return statement.Run();
848 } 870 }
849 871
850 bool AppCacheDatabase::GetDeletableResponseIds( 872 bool AppCacheDatabase::GetDeletableResponseIds(
851 std::vector<int64>* response_ids, int64 max_rowid, int limit) { 873 std::vector<int64>* response_ids, int64 max_rowid, int limit) {
852 if (!LazyOpen(false)) 874 if (!LazyOpen(kDontCreate))
853 return false; 875 return false;
854 876
855 const char* kSql = 877 const char kSql[] =
856 "SELECT response_id FROM DeletableResponseIds " 878 "SELECT response_id FROM DeletableResponseIds "
857 " WHERE rowid <= ?" 879 " WHERE rowid <= ?"
858 " LIMIT ?"; 880 " LIMIT ?";
859 881
860 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 882 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
861 statement.BindInt64(0, max_rowid); 883 statement.BindInt64(0, max_rowid);
862 statement.BindInt64(1, limit); 884 statement.BindInt64(1, limit);
863 885
864 while (statement.Step()) 886 while (statement.Step())
865 response_ids->push_back(statement.ColumnInt64(0)); 887 response_ids->push_back(statement.ColumnInt64(0));
866 return statement.Succeeded(); 888 return statement.Succeeded();
867 } 889 }
868 890
869 bool AppCacheDatabase::InsertDeletableResponseIds( 891 bool AppCacheDatabase::InsertDeletableResponseIds(
870 const std::vector<int64>& response_ids) { 892 const std::vector<int64>& response_ids) {
871 const char* kSql = 893 const char kSql[] =
872 "INSERT INTO DeletableResponseIds (response_id) VALUES (?)"; 894 "INSERT INTO DeletableResponseIds (response_id) VALUES (?)";
873 return RunCachedStatementWithIds(SQL_FROM_HERE, kSql, response_ids); 895 return RunCachedStatementWithIds(SQL_FROM_HERE, kSql, response_ids);
874 } 896 }
875 897
876 bool AppCacheDatabase::DeleteDeletableResponseIds( 898 bool AppCacheDatabase::DeleteDeletableResponseIds(
877 const std::vector<int64>& response_ids) { 899 const std::vector<int64>& response_ids) {
878 const char* kSql = 900 const char kSql[] =
879 "DELETE FROM DeletableResponseIds WHERE response_id = ?"; 901 "DELETE FROM DeletableResponseIds WHERE response_id = ?";
880 return RunCachedStatementWithIds(SQL_FROM_HERE, kSql, response_ids); 902 return RunCachedStatementWithIds(SQL_FROM_HERE, kSql, response_ids);
881 } 903 }
882 904
883 bool AppCacheDatabase::RunCachedStatementWithIds( 905 bool AppCacheDatabase::RunCachedStatementWithIds(
884 const sql::StatementID& statement_id, const char* sql, 906 const sql::StatementID& statement_id, const char* sql,
885 const std::vector<int64>& ids) { 907 const std::vector<int64>& ids) {
886 DCHECK(sql); 908 DCHECK(sql);
887 if (!LazyOpen(true)) 909 if (!LazyOpen(kCreateIfNeeded))
888 return false; 910 return false;
889 911
890 sql::Transaction transaction(db_.get()); 912 sql::Transaction transaction(db_.get());
891 if (!transaction.Begin()) 913 if (!transaction.Begin())
892 return false; 914 return false;
893 915
894 sql::Statement statement(db_->GetCachedStatement(statement_id, sql)); 916 sql::Statement statement(db_->GetCachedStatement(statement_id, sql));
895 917
896 std::vector<int64>::const_iterator iter = ids.begin(); 918 std::vector<int64>::const_iterator iter = ids.begin();
897 while (iter != ids.end()) { 919 while (iter != ids.end()) {
(...skipping 16 matching lines...) Expand all
914 } 936 }
915 *result = statement.ColumnInt64(0); 937 *result = statement.ColumnInt64(0);
916 return true; 938 return true;
917 } 939 }
918 940
919 bool AppCacheDatabase::FindResponseIdsForCacheHelper( 941 bool AppCacheDatabase::FindResponseIdsForCacheHelper(
920 int64 cache_id, std::vector<int64>* ids_vector, 942 int64 cache_id, std::vector<int64>* ids_vector,
921 std::set<int64>* ids_set) { 943 std::set<int64>* ids_set) {
922 DCHECK(ids_vector || ids_set); 944 DCHECK(ids_vector || ids_set);
923 DCHECK(!(ids_vector && ids_set)); 945 DCHECK(!(ids_vector && ids_set));
924 if (!LazyOpen(false)) 946 if (!LazyOpen(kDontCreate))
925 return false; 947 return false;
926 948
927 const char* kSql = 949 const char kSql[] =
928 "SELECT response_id FROM Entries WHERE cache_id = ?"; 950 "SELECT response_id FROM Entries WHERE cache_id = ?";
929 951
930 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); 952 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql));
931 953
932 statement.BindInt64(0, cache_id); 954 statement.BindInt64(0, cache_id);
933 while (statement.Step()) { 955 while (statement.Step()) {
934 int64 id = statement.ColumnInt64(0); 956 int64 id = statement.ColumnInt64(0);
935 if (ids_set) 957 if (ids_set)
936 ids_set->insert(id); 958 ids_set->insert(id);
937 else 959 else
(...skipping 11 matching lines...) Expand all
949 record->creation_time = 971 record->creation_time =
950 base::Time::FromInternalValue(statement.ColumnInt64(3)); 972 base::Time::FromInternalValue(statement.ColumnInt64(3));
951 973
952 const auto found = lazy_last_access_times_.find(record->group_id); 974 const auto found = lazy_last_access_times_.find(record->group_id);
953 if (found != lazy_last_access_times_.end()) { 975 if (found != lazy_last_access_times_.end()) {
954 record->last_access_time = found->second; 976 record->last_access_time = found->second;
955 } else { 977 } else {
956 record->last_access_time = 978 record->last_access_time =
957 base::Time::FromInternalValue(statement.ColumnInt64(4)); 979 base::Time::FromInternalValue(statement.ColumnInt64(4));
958 } 980 }
981
982 record->last_full_update_check_time =
983 base::Time::FromInternalValue(statement.ColumnInt64(5));
984 record->first_evictable_error_time =
985 base::Time::FromInternalValue(statement.ColumnInt64(6));
959 } 986 }
960 987
961 void AppCacheDatabase::ReadCacheRecord( 988 void AppCacheDatabase::ReadCacheRecord(
962 const sql::Statement& statement, CacheRecord* record) { 989 const sql::Statement& statement, CacheRecord* record) {
963 record->cache_id = statement.ColumnInt64(0); 990 record->cache_id = statement.ColumnInt64(0);
964 record->group_id = statement.ColumnInt64(1); 991 record->group_id = statement.ColumnInt64(1);
965 record->online_wildcard = statement.ColumnBool(2); 992 record->online_wildcard = statement.ColumnBool(2);
966 record->update_time = 993 record->update_time =
967 base::Time::FromInternalValue(statement.ColumnInt64(3)); 994 base::Time::FromInternalValue(statement.ColumnInt64(3));
968 record->cache_size = statement.ColumnInt64(4); 995 record->cache_size = statement.ColumnInt64(4);
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
1127 for (int i = 0; i < kIndexCount; ++i) { 1154 for (int i = 0; i < kIndexCount; ++i) {
1128 if (!CreateIndex(db_.get(), kIndexes[i])) 1155 if (!CreateIndex(db_.get(), kIndexes[i]))
1129 return false; 1156 return false;
1130 } 1157 }
1131 1158
1132 return transaction.Commit(); 1159 return transaction.Commit();
1133 } 1160 }
1134 1161
1135 bool AppCacheDatabase::UpgradeSchema() { 1162 bool AppCacheDatabase::UpgradeSchema() {
1136 #if defined(APPCACHE_USE_SIMPLE_CACHE) 1163 #if defined(APPCACHE_USE_SIMPLE_CACHE)
1137 return DeleteExistingAndCreateNewDatabase(); 1164 if (meta_table_->GetVersionNumber() < 6)
1138 #else 1165 return DeleteExistingAndCreateNewDatabase();
1166 #endif
1139 if (meta_table_->GetVersionNumber() == 3) { 1167 if (meta_table_->GetVersionNumber() == 3) {
1140 // version 3 was pre 12/17/2011 1168 // version 3 was pre 12/17/2011
1141 DCHECK_EQ(strcmp(kNamespacesTable, kTables[3].table_name), 0); 1169 DCHECK_EQ(strcmp(kNamespacesTable, kTables[3].table_name), 0);
1142 DCHECK_EQ(strcmp(kNamespacesTable, kIndexes[6].table_name), 0); 1170 DCHECK_EQ(strcmp(kNamespacesTable, kIndexes[6].table_name), 0);
1143 DCHECK_EQ(strcmp(kNamespacesTable, kIndexes[7].table_name), 0); 1171 DCHECK_EQ(strcmp(kNamespacesTable, kIndexes[7].table_name), 0);
1144 DCHECK_EQ(strcmp(kNamespacesTable, kIndexes[8].table_name), 0); 1172 DCHECK_EQ(strcmp(kNamespacesTable, kIndexes[8].table_name), 0);
1145 1173
1146 const TableInfo kNamespaceTable_v4 = { 1174 const TableInfo kNamespaceTable_v4 = {
1147 kNamespacesTable, 1175 kNamespacesTable,
1148 "(cache_id INTEGER," 1176 "(cache_id INTEGER,"
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
1200 " is_pattern INTEGER CHECK(is_pattern IN (0, 1))")) { 1228 " is_pattern INTEGER CHECK(is_pattern IN (0, 1))")) {
1201 return false; 1229 return false;
1202 } 1230 }
1203 if (!db_->Execute( 1231 if (!db_->Execute(
1204 "ALTER TABLE OnlineWhitelists ADD COLUMN" 1232 "ALTER TABLE OnlineWhitelists ADD COLUMN"
1205 " is_pattern INTEGER CHECK(is_pattern IN (0, 1))")) { 1233 " is_pattern INTEGER CHECK(is_pattern IN (0, 1))")) {
1206 return false; 1234 return false;
1207 } 1235 }
1208 meta_table_->SetVersionNumber(5); 1236 meta_table_->SetVersionNumber(5);
1209 meta_table_->SetCompatibleVersionNumber(5); 1237 meta_table_->SetCompatibleVersionNumber(5);
1238 if (!transaction.Commit())
1239 return false;
1240 }
1241
1242 #if defined(APPCACHE_USE_SIMPLE_CACHE)
1243 // The schema version number was increased to 6 when we switched to the
1244 // SimpleCache for Android, but the SQL part of the schema is identical
1245 // to v5 on desktop chrome.
1246 if (meta_table_->GetVersionNumber() == 6) {
1247 #else
1248 if (meta_table_->GetVersionNumber() == 5) {
1249 #endif
1250 // Versions 5 and 6 were pre-July 2015.
1251 // Version 7 adds support for expiring caches that are failing to update.
1252 sql::Transaction transaction(db_.get());
1253 if (!transaction.Begin() ||
1254 !db_->Execute(
1255 "ALTER TABLE Groups ADD COLUMN"
1256 " last_full_update_check_time INTEGER") ||
1257 !db_->Execute(
1258 "ALTER TABLE Groups ADD COLUMN"
1259 " first_evictable_error_time INTEGER") ||
1260 !db_->Execute(
1261 "UPDATE Groups"
1262 " SET last_full_update_check_time ="
1263 " (SELECT update_time FROM Caches"
1264 " WHERE Caches.group_id = Groups.group_id)")) {
1265 return false;
1266 }
1267 meta_table_->SetVersionNumber(7);
1268 meta_table_->SetCompatibleVersionNumber(7);
1210 return transaction.Commit(); 1269 return transaction.Commit();
1211 } 1270 }
1212 1271
1213 // If there is no upgrade path for the version on disk to the current 1272 // If there is no upgrade path for the version on disk to the current
1214 // version, nuke everything and start over. 1273 // version, nuke everything and start over.
1215 return DeleteExistingAndCreateNewDatabase(); 1274 return DeleteExistingAndCreateNewDatabase();
1216 #endif
1217 } 1275 }
1218 1276
1219 void AppCacheDatabase::ResetConnectionAndTables() { 1277 void AppCacheDatabase::ResetConnectionAndTables() {
1220 meta_table_.reset(); 1278 meta_table_.reset();
1221 db_.reset(); 1279 db_.reset();
1222 } 1280 }
1223 1281
1224 bool AppCacheDatabase::DeleteExistingAndCreateNewDatabase() { 1282 bool AppCacheDatabase::DeleteExistingAndCreateNewDatabase() {
1225 DCHECK(!db_file_path_.empty()); 1283 DCHECK(!db_file_path_.empty());
1226 DCHECK(base::PathExists(db_file_path_)); 1284 DCHECK(base::PathExists(db_file_path_));
(...skipping 11 matching lines...) Expand all
1238 return false; 1296 return false;
1239 1297
1240 if (!base::CreateDirectory(directory)) 1298 if (!base::CreateDirectory(directory))
1241 return false; 1299 return false;
1242 1300
1243 // So we can't go recursive. 1301 // So we can't go recursive.
1244 if (is_recreating_) 1302 if (is_recreating_)
1245 return false; 1303 return false;
1246 1304
1247 base::AutoReset<bool> auto_reset(&is_recreating_, true); 1305 base::AutoReset<bool> auto_reset(&is_recreating_, true);
1248 return LazyOpen(true); 1306 return LazyOpen(kCreateIfNeeded);
1249 } 1307 }
1250 1308
1251 void AppCacheDatabase::OnDatabaseError(int err, sql::Statement* stmt) { 1309 void AppCacheDatabase::OnDatabaseError(int err, sql::Statement* stmt) {
1252 was_corruption_detected_ |= sql::IsErrorCatastrophic(err); 1310 was_corruption_detected_ |= sql::IsErrorCatastrophic(err);
1253 if (!db_->ShouldIgnoreSqliteError(err)) 1311 if (!db_->ShouldIgnoreSqliteError(err))
1254 DLOG(ERROR) << db_->GetErrorMessage(); 1312 DLOG(ERROR) << db_->GetErrorMessage();
1255 // TODO: Maybe use non-catostrophic errors to trigger a full integrity check? 1313 // TODO: Maybe use non-catostrophic errors to trigger a full integrity check?
1256 } 1314 }
1257 1315
1258 } // namespace content 1316 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/appcache/appcache_database.h ('k') | content/browser/appcache/appcache_database_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698