1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
|
/*
* Copyright (C) 2008, 2009 Apple Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "ApplicationCacheStorage.h"
#if ENABLE(OFFLINE_WEB_APPLICATIONS)
#include "ApplicationCache.h"
#include "ApplicationCacheHost.h"
#include "ApplicationCacheGroup.h"
#include "ApplicationCacheResource.h"
#include "CString.h"
#include "FileSystem.h"
#include "KURL.h"
#include "SQLiteStatement.h"
#include "SQLiteTransaction.h"
#include <wtf/StdLibExtras.h>
#include <wtf/StringExtras.h>
using namespace std;
namespace WebCore {
template <class T>
class StorageIDJournal {
public:
~StorageIDJournal()
{
size_t size = m_records.size();
for (size_t i = 0; i < size; ++i)
m_records[i].restore();
}
void add(T* resource, unsigned storageID)
{
m_records.append(Record(resource, storageID));
}
void commit()
{
m_records.clear();
}
private:
class Record {
public:
Record() : m_resource(0), m_storageID(0) { }
Record(T* resource, unsigned storageID) : m_resource(resource), m_storageID(storageID) { }
void restore()
{
m_resource->setStorageID(m_storageID);
}
private:
T* m_resource;
unsigned m_storageID;
};
Vector<Record> m_records;
};
static unsigned urlHostHash(const KURL& url)
{
unsigned hostStart = url.hostStart();
unsigned hostEnd = url.hostEnd();
return AlreadyHashed::avoidDeletedValue(StringImpl::computeHash(url.string().characters() + hostStart, hostEnd - hostStart));
}
ApplicationCacheGroup* ApplicationCacheStorage::loadCacheGroup(const KURL& manifestURL)
{
openDatabase(false);
if (!m_database.isOpen())
return 0;
SQLiteStatement statement(m_database, "SELECT id, manifestURL, newestCache FROM CacheGroups WHERE newestCache IS NOT NULL AND manifestURL=?");
if (statement.prepare() != SQLResultOk)
return 0;
statement.bindText(1, manifestURL);
int result = statement.step();
if (result == SQLResultDone)
return 0;
if (result != SQLResultRow) {
LOG_ERROR("Could not load cache group, error \"%s\"", m_database.lastErrorMsg());
return 0;
}
unsigned newestCacheStorageID = static_cast<unsigned>(statement.getColumnInt64(2));
RefPtr<ApplicationCache> cache = loadCache(newestCacheStorageID);
if (!cache)
return 0;
ApplicationCacheGroup* group = new ApplicationCacheGroup(manifestURL);
group->setStorageID(static_cast<unsigned>(statement.getColumnInt64(0)));
group->setNewestCache(cache.release());
return group;
}
ApplicationCacheGroup* ApplicationCacheStorage::findOrCreateCacheGroup(const KURL& manifestURL)
{
ASSERT(!manifestURL.hasFragmentIdentifier());
std::pair<CacheGroupMap::iterator, bool> result = m_cachesInMemory.add(manifestURL, 0);
if (!result.second) {
ASSERT(result.first->second);
return result.first->second;
}
// Look up the group in the database
ApplicationCacheGroup* group = loadCacheGroup(manifestURL);
// If the group was not found we need to create it
if (!group) {
group = new ApplicationCacheGroup(manifestURL);
m_cacheHostSet.add(urlHostHash(manifestURL));
}
result.first->second = group;
return group;
}
void ApplicationCacheStorage::loadManifestHostHashes()
{
static bool hasLoadedHashes = false;
if (hasLoadedHashes)
return;
// We set this flag to true before the database has been opened
// to avoid trying to open the database over and over if it doesn't exist.
hasLoadedHashes = true;
openDatabase(false);
if (!m_database.isOpen())
return;
// Fetch the host hashes.
SQLiteStatement statement(m_database, "SELECT manifestHostHash FROM CacheGroups");
if (statement.prepare() != SQLResultOk)
return;
int result;
while ((result = statement.step()) == SQLResultRow)
m_cacheHostSet.add(static_cast<unsigned>(statement.getColumnInt64(0)));
}
ApplicationCacheGroup* ApplicationCacheStorage::cacheGroupForURL(const KURL& url)
{
ASSERT(!url.hasFragmentIdentifier());
loadManifestHostHashes();
// Hash the host name and see if there's a manifest with the same host.
if (!m_cacheHostSet.contains(urlHostHash(url)))
return 0;
// Check if a cache already exists in memory.
CacheGroupMap::const_iterator end = m_cachesInMemory.end();
for (CacheGroupMap::const_iterator it = m_cachesInMemory.begin(); it != end; ++it) {
ApplicationCacheGroup* group = it->second;
ASSERT(!group->isObsolete());
if (!protocolHostAndPortAreEqual(url, group->manifestURL()))
continue;
if (ApplicationCache* cache = group->newestCache()) {
ApplicationCacheResource* resource = cache->resourceForURL(url);
if (!resource)
continue;
if (resource->type() & ApplicationCacheResource::Foreign)
continue;
return group;
}
}
if (!m_database.isOpen())
return 0;
// Check the database. Look for all cache groups with a newest cache.
SQLiteStatement statement(m_database, "SELECT id, manifestURL, newestCache FROM CacheGroups WHERE newestCache IS NOT NULL");
if (statement.prepare() != SQLResultOk)
return 0;
int result;
while ((result = statement.step()) == SQLResultRow) {
KURL manifestURL = KURL(ParsedURLString, statement.getColumnText(1));
if (m_cachesInMemory.contains(manifestURL))
continue;
if (!protocolHostAndPortAreEqual(url, manifestURL))
continue;
// We found a cache group that matches. Now check if the newest cache has a resource with
// a matching URL.
unsigned newestCacheID = static_cast<unsigned>(statement.getColumnInt64(2));
RefPtr<ApplicationCache> cache = loadCache(newestCacheID);
if (!cache)
continue;
ApplicationCacheResource* resource = cache->resourceForURL(url);
if (!resource)
continue;
if (resource->type() & ApplicationCacheResource::Foreign)
continue;
ApplicationCacheGroup* group = new ApplicationCacheGroup(manifestURL);
group->setStorageID(static_cast<unsigned>(statement.getColumnInt64(0)));
group->setNewestCache(cache.release());
m_cachesInMemory.set(group->manifestURL(), group);
return group;
}
if (result != SQLResultDone)
LOG_ERROR("Could not load cache group, error \"%s\"", m_database.lastErrorMsg());
return 0;
}
ApplicationCacheGroup* ApplicationCacheStorage::fallbackCacheGroupForURL(const KURL& url)
{
ASSERT(!url.hasFragmentIdentifier());
// Check if an appropriate cache already exists in memory.
CacheGroupMap::const_iterator end = m_cachesInMemory.end();
for (CacheGroupMap::const_iterator it = m_cachesInMemory.begin(); it != end; ++it) {
ApplicationCacheGroup* group = it->second;
ASSERT(!group->isObsolete());
if (ApplicationCache* cache = group->newestCache()) {
KURL fallbackURL;
if (!cache->urlMatchesFallbackNamespace(url, &fallbackURL))
continue;
if (cache->resourceForURL(fallbackURL)->type() & ApplicationCacheResource::Foreign)
continue;
return group;
}
}
if (!m_database.isOpen())
return 0;
// Check the database. Look for all cache groups with a newest cache.
SQLiteStatement statement(m_database, "SELECT id, manifestURL, newestCache FROM CacheGroups WHERE newestCache IS NOT NULL");
if (statement.prepare() != SQLResultOk)
return 0;
int result;
while ((result = statement.step()) == SQLResultRow) {
KURL manifestURL = KURL(ParsedURLString, statement.getColumnText(1));
if (m_cachesInMemory.contains(manifestURL))
continue;
// Fallback namespaces always have the same origin as manifest URL, so we can avoid loading caches that cannot match.
if (!protocolHostAndPortAreEqual(url, manifestURL))
continue;
// We found a cache group that matches. Now check if the newest cache has a resource with
// a matching fallback namespace.
unsigned newestCacheID = static_cast<unsigned>(statement.getColumnInt64(2));
RefPtr<ApplicationCache> cache = loadCache(newestCacheID);
KURL fallbackURL;
if (!cache->urlMatchesFallbackNamespace(url, &fallbackURL))
continue;
if (cache->resourceForURL(fallbackURL)->type() & ApplicationCacheResource::Foreign)
continue;
ApplicationCacheGroup* group = new ApplicationCacheGroup(manifestURL);
group->setStorageID(static_cast<unsigned>(statement.getColumnInt64(0)));
group->setNewestCache(cache.release());
m_cachesInMemory.set(group->manifestURL(), group);
return group;
}
if (result != SQLResultDone)
LOG_ERROR("Could not load cache group, error \"%s\"", m_database.lastErrorMsg());
return 0;
}
void ApplicationCacheStorage::cacheGroupDestroyed(ApplicationCacheGroup* group)
{
if (group->isObsolete()) {
ASSERT(!group->storageID());
ASSERT(m_cachesInMemory.get(group->manifestURL()) != group);
return;
}
ASSERT(m_cachesInMemory.get(group->manifestURL()) == group);
m_cachesInMemory.remove(group->manifestURL());
// If the cache group is half-created, we don't want it in the saved set (as it is not stored in database).
if (!group->storageID())
m_cacheHostSet.remove(urlHostHash(group->manifestURL()));
}
void ApplicationCacheStorage::cacheGroupMadeObsolete(ApplicationCacheGroup* group)
{
ASSERT(m_cachesInMemory.get(group->manifestURL()) == group);
ASSERT(m_cacheHostSet.contains(urlHostHash(group->manifestURL())));
if (ApplicationCache* newestCache = group->newestCache())
remove(newestCache);
m_cachesInMemory.remove(group->manifestURL());
m_cacheHostSet.remove(urlHostHash(group->manifestURL()));
}
void ApplicationCacheStorage::setCacheDirectory(const String& cacheDirectory)
{
ASSERT(m_cacheDirectory.isNull());
ASSERT(!cacheDirectory.isNull());
m_cacheDirectory = cacheDirectory;
}
const String& ApplicationCacheStorage::cacheDirectory() const
{
return m_cacheDirectory;
}
void ApplicationCacheStorage::setMaximumSize(int64_t size)
{
m_maximumSize = size;
}
int64_t ApplicationCacheStorage::maximumSize() const
{
return m_maximumSize;
}
bool ApplicationCacheStorage::isMaximumSizeReached() const
{
return m_isMaximumSizeReached;
}
int64_t ApplicationCacheStorage::spaceNeeded(int64_t cacheToSave)
{
int64_t spaceNeeded = 0;
long long fileSize = 0;
if (!getFileSize(m_cacheFile, fileSize))
return 0;
int64_t currentSize = fileSize;
// Determine the amount of free space we have available.
int64_t totalAvailableSize = 0;
if (m_maximumSize < currentSize) {
// The max size is smaller than the actual size of the app cache file.
// This can happen if the client previously imposed a larger max size
// value and the app cache file has already grown beyond the current
// max size value.
// The amount of free space is just the amount of free space inside
// the database file. Note that this is always 0 if SQLite is compiled
// with AUTO_VACUUM = 1.
totalAvailableSize = m_database.freeSpaceSize();
} else {
// The max size is the same or larger than the current size.
// The amount of free space available is the amount of free space
// inside the database file plus the amount we can grow until we hit
// the max size.
totalAvailableSize = (m_maximumSize - currentSize) + m_database.freeSpaceSize();
}
// The space needed to be freed in order to accommodate the failed cache is
// the size of the failed cache minus any already available free space.
spaceNeeded = cacheToSave - totalAvailableSize;
// The space needed value must be positive (or else the total already
// available free space would be larger than the size of the failed cache and
// saving of the cache should have never failed).
ASSERT(spaceNeeded);
return spaceNeeded;
}
bool ApplicationCacheStorage::executeSQLCommand(const String& sql)
{
ASSERT(m_database.isOpen());
bool result = m_database.executeCommand(sql);
if (!result)
LOG_ERROR("Application Cache Storage: failed to execute statement \"%s\" error \"%s\"",
sql.utf8().data(), m_database.lastErrorMsg());
return result;
}
static const int schemaVersion = 5;
void ApplicationCacheStorage::verifySchemaVersion()
{
int version = SQLiteStatement(m_database, "PRAGMA user_version").getColumnInt(0);
if (version == schemaVersion)
return;
m_database.clearAllTables();
// Update user version.
SQLiteTransaction setDatabaseVersion(m_database);
setDatabaseVersion.begin();
char userVersionSQL[32];
int unusedNumBytes = snprintf(userVersionSQL, sizeof(userVersionSQL), "PRAGMA user_version=%d", schemaVersion);
ASSERT_UNUSED(unusedNumBytes, static_cast<int>(sizeof(userVersionSQL)) >= unusedNumBytes);
SQLiteStatement statement(m_database, userVersionSQL);
if (statement.prepare() != SQLResultOk)
return;
executeStatement(statement);
setDatabaseVersion.commit();
}
void ApplicationCacheStorage::openDatabase(bool createIfDoesNotExist)
{
if (m_database.isOpen())
return;
// The cache directory should never be null, but if it for some weird reason is we bail out.
if (m_cacheDirectory.isNull())
return;
m_cacheFile = pathByAppendingComponent(m_cacheDirectory, "ApplicationCache.db");
if (!createIfDoesNotExist && !fileExists(m_cacheFile))
return;
makeAllDirectories(m_cacheDirectory);
m_database.open(m_cacheFile);
if (!m_database.isOpen())
return;
verifySchemaVersion();
// Create tables
executeSQLCommand("CREATE TABLE IF NOT EXISTS CacheGroups (id INTEGER PRIMARY KEY AUTOINCREMENT, "
"manifestHostHash INTEGER NOT NULL ON CONFLICT FAIL, manifestURL TEXT UNIQUE ON CONFLICT FAIL, newestCache INTEGER)");
executeSQLCommand("CREATE TABLE IF NOT EXISTS Caches (id INTEGER PRIMARY KEY AUTOINCREMENT, cacheGroup INTEGER, size INTEGER)");
executeSQLCommand("CREATE TABLE IF NOT EXISTS CacheWhitelistURLs (url TEXT NOT NULL ON CONFLICT FAIL, cache INTEGER NOT NULL ON CONFLICT FAIL)");
executeSQLCommand("CREATE TABLE IF NOT EXISTS CacheAllowsAllNetworkRequests (wildcard INTEGER NOT NULL ON CONFLICT FAIL, cache INTEGER NOT NULL ON CONFLICT FAIL)");
executeSQLCommand("CREATE TABLE IF NOT EXISTS FallbackURLs (namespace TEXT NOT NULL ON CONFLICT FAIL, fallbackURL TEXT NOT NULL ON CONFLICT FAIL, "
"cache INTEGER NOT NULL ON CONFLICT FAIL)");
executeSQLCommand("CREATE TABLE IF NOT EXISTS CacheEntries (cache INTEGER NOT NULL ON CONFLICT FAIL, type INTEGER, resource INTEGER NOT NULL)");
executeSQLCommand("CREATE TABLE IF NOT EXISTS CacheResources (id INTEGER PRIMARY KEY AUTOINCREMENT, url TEXT NOT NULL ON CONFLICT FAIL, "
"statusCode INTEGER NOT NULL, responseURL TEXT NOT NULL, mimeType TEXT, textEncodingName TEXT, headers TEXT, data INTEGER NOT NULL ON CONFLICT FAIL)");
executeSQLCommand("CREATE TABLE IF NOT EXISTS CacheResourceData (id INTEGER PRIMARY KEY AUTOINCREMENT, data BLOB)");
// When a cache is deleted, all its entries and its whitelist should be deleted.
executeSQLCommand("CREATE TRIGGER IF NOT EXISTS CacheDeleted AFTER DELETE ON Caches"
" FOR EACH ROW BEGIN"
" DELETE FROM CacheEntries WHERE cache = OLD.id;"
" DELETE FROM CacheWhitelistURLs WHERE cache = OLD.id;"
" DELETE FROM CacheAllowsAllNetworkRequests WHERE cache = OLD.id;"
" DELETE FROM FallbackURLs WHERE cache = OLD.id;"
" END");
// When a cache entry is deleted, its resource should also be deleted.
executeSQLCommand("CREATE TRIGGER IF NOT EXISTS CacheEntryDeleted AFTER DELETE ON CacheEntries"
" FOR EACH ROW BEGIN"
" DELETE FROM CacheResources WHERE id = OLD.resource;"
" END");
// When a cache resource is deleted, its data blob should also be deleted.
executeSQLCommand("CREATE TRIGGER IF NOT EXISTS CacheResourceDeleted AFTER DELETE ON CacheResources"
" FOR EACH ROW BEGIN"
" DELETE FROM CacheResourceData WHERE id = OLD.data;"
" END");
}
bool ApplicationCacheStorage::executeStatement(SQLiteStatement& statement)
{
bool result = statement.executeCommand();
if (!result)
LOG_ERROR("Application Cache Storage: failed to execute statement \"%s\" error \"%s\"",
statement.query().utf8().data(), m_database.lastErrorMsg());
return result;
}
bool ApplicationCacheStorage::store(ApplicationCacheGroup* group, GroupStorageIDJournal* journal)
{
ASSERT(group->storageID() == 0);
ASSERT(journal);
SQLiteStatement statement(m_database, "INSERT INTO CacheGroups (manifestHostHash, manifestURL) VALUES (?, ?)");
if (statement.prepare() != SQLResultOk)
return false;
statement.bindInt64(1, urlHostHash(group->manifestURL()));
statement.bindText(2, group->manifestURL());
if (!executeStatement(statement))
return false;
group->setStorageID(static_cast<unsigned>(m_database.lastInsertRowID()));
journal->add(group, 0);
return true;
}
bool ApplicationCacheStorage::store(ApplicationCache* cache, ResourceStorageIDJournal* storageIDJournal)
{
ASSERT(cache->storageID() == 0);
ASSERT(cache->group()->storageID() != 0);
ASSERT(storageIDJournal);
SQLiteStatement statement(m_database, "INSERT INTO Caches (cacheGroup, size) VALUES (?, ?)");
if (statement.prepare() != SQLResultOk)
return false;
statement.bindInt64(1, cache->group()->storageID());
statement.bindInt64(2, cache->estimatedSizeInStorage());
if (!executeStatement(statement))
return false;
unsigned cacheStorageID = static_cast<unsigned>(m_database.lastInsertRowID());
// Store all resources
{
ApplicationCache::ResourceMap::const_iterator end = cache->end();
for (ApplicationCache::ResourceMap::const_iterator it = cache->begin(); it != end; ++it) {
unsigned oldStorageID = it->second->storageID();
if (!store(it->second.get(), cacheStorageID))
return false;
// Storing the resource succeeded. Log its old storageID in case
// it needs to be restored later.
storageIDJournal->add(it->second.get(), oldStorageID);
}
}
// Store the online whitelist
const Vector<KURL>& onlineWhitelist = cache->onlineWhitelist();
{
size_t whitelistSize = onlineWhitelist.size();
for (size_t i = 0; i < whitelistSize; ++i) {
SQLiteStatement statement(m_database, "INSERT INTO CacheWhitelistURLs (url, cache) VALUES (?, ?)");
statement.prepare();
statement.bindText(1, onlineWhitelist[i]);
statement.bindInt64(2, cacheStorageID);
if (!executeStatement(statement))
return false;
}
}
// Store online whitelist wildcard flag.
{
SQLiteStatement statement(m_database, "INSERT INTO CacheAllowsAllNetworkRequests (wildcard, cache) VALUES (?, ?)");
statement.prepare();
statement.bindInt64(1, cache->allowsAllNetworkRequests());
statement.bindInt64(2, cacheStorageID);
if (!executeStatement(statement))
return false;
}
// Store fallback URLs.
const FallbackURLVector& fallbackURLs = cache->fallbackURLs();
{
size_t fallbackCount = fallbackURLs.size();
for (size_t i = 0; i < fallbackCount; ++i) {
SQLiteStatement statement(m_database, "INSERT INTO FallbackURLs (namespace, fallbackURL, cache) VALUES (?, ?, ?)");
statement.prepare();
statement.bindText(1, fallbackURLs[i].first);
statement.bindText(2, fallbackURLs[i].second);
statement.bindInt64(3, cacheStorageID);
if (!executeStatement(statement))
return false;
}
}
cache->setStorageID(cacheStorageID);
return true;
}
bool ApplicationCacheStorage::store(ApplicationCacheResource* resource, unsigned cacheStorageID)
{
ASSERT(cacheStorageID);
ASSERT(!resource->storageID());
openDatabase(true);
// First, insert the data
SQLiteStatement dataStatement(m_database, "INSERT INTO CacheResourceData (data) VALUES (?)");
if (dataStatement.prepare() != SQLResultOk)
return false;
if (resource->data()->size())
dataStatement.bindBlob(1, resource->data()->data(), resource->data()->size());
if (!dataStatement.executeCommand())
return false;
unsigned dataId = static_cast<unsigned>(m_database.lastInsertRowID());
// Then, insert the resource
// Serialize the headers
Vector<UChar> stringBuilder;
HTTPHeaderMap::const_iterator end = resource->response().httpHeaderFields().end();
for (HTTPHeaderMap::const_iterator it = resource->response().httpHeaderFields().begin(); it!= end; ++it) {
stringBuilder.append(it->first.characters(), it->first.length());
stringBuilder.append((UChar)':');
stringBuilder.append(it->second.characters(), it->second.length());
stringBuilder.append((UChar)'\n');
}
String headers = String::adopt(stringBuilder);
SQLiteStatement resourceStatement(m_database, "INSERT INTO CacheResources (url, statusCode, responseURL, headers, data, mimeType, textEncodingName) VALUES (?, ?, ?, ?, ?, ?, ?)");
if (resourceStatement.prepare() != SQLResultOk)
return false;
// The same ApplicationCacheResource are used in ApplicationCacheResource::size()
// to calculate the approximate size of an ApplicationCacheResource object. If
// you change the code below, please also change ApplicationCacheResource::size().
resourceStatement.bindText(1, resource->url());
resourceStatement.bindInt64(2, resource->response().httpStatusCode());
resourceStatement.bindText(3, resource->response().url());
resourceStatement.bindText(4, headers);
resourceStatement.bindInt64(5, dataId);
resourceStatement.bindText(6, resource->response().mimeType());
resourceStatement.bindText(7, resource->response().textEncodingName());
if (!executeStatement(resourceStatement))
return false;
unsigned resourceId = static_cast<unsigned>(m_database.lastInsertRowID());
// Finally, insert the cache entry
SQLiteStatement entryStatement(m_database, "INSERT INTO CacheEntries (cache, type, resource) VALUES (?, ?, ?)");
if (entryStatement.prepare() != SQLResultOk)
return false;
entryStatement.bindInt64(1, cacheStorageID);
entryStatement.bindInt64(2, resource->type());
entryStatement.bindInt64(3, resourceId);
if (!executeStatement(entryStatement))
return false;
resource->setStorageID(resourceId);
return true;
}
bool ApplicationCacheStorage::storeUpdatedType(ApplicationCacheResource* resource, ApplicationCache* cache)
{
ASSERT_UNUSED(cache, cache->storageID());
ASSERT(resource->storageID());
// First, insert the data
SQLiteStatement entryStatement(m_database, "UPDATE CacheEntries SET type=? WHERE resource=?");
if (entryStatement.prepare() != SQLResultOk)
return false;
entryStatement.bindInt64(1, resource->type());
entryStatement.bindInt64(2, resource->storageID());
return executeStatement(entryStatement);
}
bool ApplicationCacheStorage::store(ApplicationCacheResource* resource, ApplicationCache* cache)
{
ASSERT(cache->storageID());
openDatabase(true);
m_isMaximumSizeReached = false;
m_database.setMaximumSize(m_maximumSize);
SQLiteTransaction storeResourceTransaction(m_database);
storeResourceTransaction.begin();
if (!store(resource, cache->storageID())) {
checkForMaxSizeReached();
return false;
}
// A resource was added to the cache. Update the total data size for the cache.
SQLiteStatement sizeUpdateStatement(m_database, "UPDATE Caches SET size=size+? WHERE id=?");
if (sizeUpdateStatement.prepare() != SQLResultOk)
return false;
sizeUpdateStatement.bindInt64(1, resource->estimatedSizeInStorage());
sizeUpdateStatement.bindInt64(2, cache->storageID());
if (!executeStatement(sizeUpdateStatement))
return false;
storeResourceTransaction.commit();
return true;
}
bool ApplicationCacheStorage::storeNewestCache(ApplicationCacheGroup* group)
{
openDatabase(true);
m_isMaximumSizeReached = false;
m_database.setMaximumSize(m_maximumSize);
SQLiteTransaction storeCacheTransaction(m_database);
storeCacheTransaction.begin();
GroupStorageIDJournal groupStorageIDJournal;
if (!group->storageID()) {
// Store the group
if (!store(group, &groupStorageIDJournal)) {
checkForMaxSizeReached();
return false;
}
}
ASSERT(group->newestCache());
ASSERT(!group->isObsolete());
ASSERT(!group->newestCache()->storageID());
// Log the storageID changes to the in-memory resource objects. The journal
// object will roll them back automatically in case a database operation
// fails and this method returns early.
ResourceStorageIDJournal resourceStorageIDJournal;
// Store the newest cache
if (!store(group->newestCache(), &resourceStorageIDJournal)) {
checkForMaxSizeReached();
return false;
}
// Update the newest cache in the group.
SQLiteStatement statement(m_database, "UPDATE CacheGroups SET newestCache=? WHERE id=?");
if (statement.prepare() != SQLResultOk)
return false;
statement.bindInt64(1, group->newestCache()->storageID());
statement.bindInt64(2, group->storageID());
if (!executeStatement(statement))
return false;
groupStorageIDJournal.commit();
resourceStorageIDJournal.commit();
storeCacheTransaction.commit();
return true;
}
static inline void parseHeader(const UChar* header, size_t headerLength, ResourceResponse& response)
{
int pos = find(header, headerLength, ':');
ASSERT(pos != -1);
AtomicString headerName = AtomicString(header, pos);
String headerValue = String(header + pos + 1, headerLength - pos - 1);
response.setHTTPHeaderField(headerName, headerValue);
}
static inline void parseHeaders(const String& headers, ResourceResponse& response)
{
int startPos = 0;
int endPos;
while ((endPos = headers.find('\n', startPos)) != -1) {
ASSERT(startPos != endPos);
parseHeader(headers.characters() + startPos, endPos - startPos, response);
startPos = endPos + 1;
}
if (startPos != static_cast<int>(headers.length()))
parseHeader(headers.characters(), headers.length(), response);
}
PassRefPtr<ApplicationCache> ApplicationCacheStorage::loadCache(unsigned storageID)
{
SQLiteStatement cacheStatement(m_database,
"SELECT url, type, mimeType, textEncodingName, headers, CacheResourceData.data FROM CacheEntries INNER JOIN CacheResources ON CacheEntries.resource=CacheResources.id "
"INNER JOIN CacheResourceData ON CacheResourceData.id=CacheResources.data WHERE CacheEntries.cache=?");
if (cacheStatement.prepare() != SQLResultOk) {
LOG_ERROR("Could not prepare cache statement, error \"%s\"", m_database.lastErrorMsg());
return 0;
}
cacheStatement.bindInt64(1, storageID);
RefPtr<ApplicationCache> cache = ApplicationCache::create();
int result;
while ((result = cacheStatement.step()) == SQLResultRow) {
KURL url(ParsedURLString, cacheStatement.getColumnText(0));
unsigned type = static_cast<unsigned>(cacheStatement.getColumnInt64(1));
Vector<char> blob;
cacheStatement.getColumnBlobAsVector(5, blob);
RefPtr<SharedBuffer> data = SharedBuffer::adoptVector(blob);
String mimeType = cacheStatement.getColumnText(2);
String textEncodingName = cacheStatement.getColumnText(3);
ResourceResponse response(url, mimeType, data->size(), textEncodingName, "");
String headers = cacheStatement.getColumnText(4);
parseHeaders(headers, response);
RefPtr<ApplicationCacheResource> resource = ApplicationCacheResource::create(url, response, type, data.release());
if (type & ApplicationCacheResource::Manifest)
cache->setManifestResource(resource.release());
else
cache->addResource(resource.release());
}
if (result != SQLResultDone)
LOG_ERROR("Could not load cache resources, error \"%s\"", m_database.lastErrorMsg());
// Load the online whitelist
SQLiteStatement whitelistStatement(m_database, "SELECT url FROM CacheWhitelistURLs WHERE cache=?");
if (whitelistStatement.prepare() != SQLResultOk)
return 0;
whitelistStatement.bindInt64(1, storageID);
Vector<KURL> whitelist;
while ((result = whitelistStatement.step()) == SQLResultRow)
whitelist.append(KURL(ParsedURLString, whitelistStatement.getColumnText(0)));
if (result != SQLResultDone)
LOG_ERROR("Could not load cache online whitelist, error \"%s\"", m_database.lastErrorMsg());
cache->setOnlineWhitelist(whitelist);
// Load online whitelist wildcard flag.
SQLiteStatement whitelistWildcardStatement(m_database, "SELECT wildcard FROM CacheAllowsAllNetworkRequests WHERE cache=?");
if (whitelistWildcardStatement.prepare() != SQLResultOk)
return 0;
whitelistWildcardStatement.bindInt64(1, storageID);
result = whitelistWildcardStatement.step();
if (result != SQLResultRow)
LOG_ERROR("Could not load cache online whitelist wildcard flag, error \"%s\"", m_database.lastErrorMsg());
cache->setAllowsAllNetworkRequests(whitelistWildcardStatement.getColumnInt64(0));
if (whitelistWildcardStatement.step() != SQLResultDone)
LOG_ERROR("Too many rows for online whitelist wildcard flag");
// Load fallback URLs.
SQLiteStatement fallbackStatement(m_database, "SELECT namespace, fallbackURL FROM FallbackURLs WHERE cache=?");
if (fallbackStatement.prepare() != SQLResultOk)
return 0;
fallbackStatement.bindInt64(1, storageID);
FallbackURLVector fallbackURLs;
while ((result = fallbackStatement.step()) == SQLResultRow)
fallbackURLs.append(make_pair(KURL(ParsedURLString, fallbackStatement.getColumnText(0)), KURL(ParsedURLString, fallbackStatement.getColumnText(1))));
if (result != SQLResultDone)
LOG_ERROR("Could not load fallback URLs, error \"%s\"", m_database.lastErrorMsg());
cache->setFallbackURLs(fallbackURLs);
cache->setStorageID(storageID);
return cache.release();
}
void ApplicationCacheStorage::remove(ApplicationCache* cache)
{
if (!cache->storageID())
return;
openDatabase(false);
if (!m_database.isOpen())
return;
ASSERT(cache->group());
ASSERT(cache->group()->storageID());
// All associated data will be deleted by database triggers.
SQLiteStatement statement(m_database, "DELETE FROM Caches WHERE id=?");
if (statement.prepare() != SQLResultOk)
return;
statement.bindInt64(1, cache->storageID());
executeStatement(statement);
cache->clearStorageID();
if (cache->group()->newestCache() == cache) {
// Currently, there are no triggers on the cache group, which is why the cache had to be removed separately above.
SQLiteStatement groupStatement(m_database, "DELETE FROM CacheGroups WHERE id=?");
if (groupStatement.prepare() != SQLResultOk)
return;
groupStatement.bindInt64(1, cache->group()->storageID());
executeStatement(groupStatement);
cache->group()->clearStorageID();
}
}
void ApplicationCacheStorage::empty()
{
openDatabase(false);
if (!m_database.isOpen())
return;
// Clear cache groups, caches and cache resources.
executeSQLCommand("DELETE FROM CacheGroups");
executeSQLCommand("DELETE FROM Caches");
// Clear the storage IDs for the caches in memory.
// The caches will still work, but cached resources will not be saved to disk
// until a cache update process has been initiated.
CacheGroupMap::const_iterator end = m_cachesInMemory.end();
for (CacheGroupMap::const_iterator it = m_cachesInMemory.begin(); it != end; ++it)
it->second->clearStorageID();
}
bool ApplicationCacheStorage::storeCopyOfCache(const String& cacheDirectory, ApplicationCacheHost* cacheHost)
{
ApplicationCache* cache = cacheHost->applicationCache();
if (!cache)
return true;
// Create a new cache.
RefPtr<ApplicationCache> cacheCopy = ApplicationCache::create();
cacheCopy->setOnlineWhitelist(cache->onlineWhitelist());
cacheCopy->setFallbackURLs(cache->fallbackURLs());
// Traverse the cache and add copies of all resources.
ApplicationCache::ResourceMap::const_iterator end = cache->end();
for (ApplicationCache::ResourceMap::const_iterator it = cache->begin(); it != end; ++it) {
ApplicationCacheResource* resource = it->second.get();
RefPtr<ApplicationCacheResource> resourceCopy = ApplicationCacheResource::create(resource->url(), resource->response(), resource->type(), resource->data());
cacheCopy->addResource(resourceCopy.release());
}
// Now create a new cache group.
OwnPtr<ApplicationCacheGroup> groupCopy(new ApplicationCacheGroup(cache->group()->manifestURL(), true));
groupCopy->setNewestCache(cacheCopy);
ApplicationCacheStorage copyStorage;
copyStorage.setCacheDirectory(cacheDirectory);
// Empty the cache in case something was there before.
copyStorage.empty();
return copyStorage.storeNewestCache(groupCopy.get());
}
bool ApplicationCacheStorage::manifestURLs(Vector<KURL>* urls)
{
ASSERT(urls);
openDatabase(false);
if (!m_database.isOpen())
return false;
SQLiteStatement selectURLs(m_database, "SELECT manifestURL FROM CacheGroups");
if (selectURLs.prepare() != SQLResultOk)
return false;
while (selectURLs.step() == SQLResultRow)
urls->append(KURL(ParsedURLString, selectURLs.getColumnText(0)));
return true;
}
bool ApplicationCacheStorage::cacheGroupSize(const String& manifestURL, int64_t* size)
{
ASSERT(size);
openDatabase(false);
if (!m_database.isOpen())
return false;
SQLiteStatement statement(m_database, "SELECT sum(Caches.size) FROM Caches INNER JOIN CacheGroups ON Caches.cacheGroup=CacheGroups.id WHERE CacheGroups.manifestURL=?");
if (statement.prepare() != SQLResultOk)
return false;
statement.bindText(1, manifestURL);
int result = statement.step();
if (result == SQLResultDone)
return false;
if (result != SQLResultRow) {
LOG_ERROR("Could not get the size of the cache group, error \"%s\"", m_database.lastErrorMsg());
return false;
}
*size = statement.getColumnInt64(0);
return true;
}
bool ApplicationCacheStorage::deleteCacheGroup(const String& manifestURL)
{
SQLiteTransaction deleteTransaction(m_database);
// Check to see if the group is in memory.
ApplicationCacheGroup* group = m_cachesInMemory.get(manifestURL);
if (group)
cacheGroupMadeObsolete(group);
else {
// The cache group is not in memory, so remove it from the disk.
openDatabase(false);
if (!m_database.isOpen())
return false;
SQLiteStatement idStatement(m_database, "SELECT id FROM CacheGroups WHERE manifestURL=?");
if (idStatement.prepare() != SQLResultOk)
return false;
idStatement.bindText(1, manifestURL);
int result = idStatement.step();
if (result == SQLResultDone)
return false;
if (result != SQLResultRow) {
LOG_ERROR("Could not load cache group id, error \"%s\"", m_database.lastErrorMsg());
return false;
}
int64_t groupId = idStatement.getColumnInt64(0);
SQLiteStatement cacheStatement(m_database, "DELETE FROM Caches WHERE cacheGroup=?");
if (cacheStatement.prepare() != SQLResultOk)
return false;
SQLiteStatement groupStatement(m_database, "DELETE FROM CacheGroups WHERE id=?");
if (groupStatement.prepare() != SQLResultOk)
return false;
cacheStatement.bindInt64(1, groupId);
executeStatement(cacheStatement);
groupStatement.bindInt64(1, groupId);
executeStatement(groupStatement);
}
deleteTransaction.commit();
return true;
}
void ApplicationCacheStorage::vacuumDatabaseFile()
{
openDatabase(false);
if (!m_database.isOpen())
return;
m_database.runVacuumCommand();
}
void ApplicationCacheStorage::checkForMaxSizeReached()
{
if (m_database.lastError() == SQLResultFull)
m_isMaximumSizeReached = true;
}
ApplicationCacheStorage::ApplicationCacheStorage()
: m_maximumSize(INT_MAX)
, m_isMaximumSizeReached(false)
{
}
ApplicationCacheStorage& cacheStorage()
{
DEFINE_STATIC_LOCAL(ApplicationCacheStorage, storage, ());
return storage;
}
} // namespace WebCore
#endif // ENABLE(OFFLINE_WEB_APPLICATIONS)
|