Index: webkit/dom_storage/dom_storage_database.h |
diff --git a/webkit/dom_storage/dom_storage_database.h b/webkit/dom_storage/dom_storage_database.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..066a97a6b157655c8ca7e713faeefb37589adfdd |
--- /dev/null |
+++ b/webkit/dom_storage/dom_storage_database.h |
@@ -0,0 +1,110 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef WEBKIT_DOM_STORAGE_DOM_STORAGE_DATABASE_H_ |
+#define WEBKIT_DOM_STORAGE_DOM_STORAGE_DATABASE_H_ |
+#pragma once |
+ |
+#include <map> |
+ |
+#include "base/file_path.h" |
+#include "base/gtest_prod_util.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/nullable_string16.h" |
+#include "base/string16.h" |
+#include "sql/connection.h" |
+#include "webkit/dom_storage/dom_storage_types.h" |
+ |
+namespace dom_storage { |
+ |
+// Represents a SQLite based backing for DOM storage data. This |
+// class is designed to be used on a single thread. |
+class DomStorageDatabase { |
+ public: |
+ explicit DomStorageDatabase(const FilePath& file_path); |
+ ~DomStorageDatabase(); |
+ |
+ // Reads all the key, value pairs stored in the database and returns |
+ // them. |result| is assumed to be empty and any duplicate keys will |
+ // be overwritten. If the database exists on disk then it will be |
+ // opened. If it does not exist then it will not be created and |
+ // |result| will be unmodified. |
+ void ReadAllValues(ValuesMap* result); |
+ |
+ // Updates the backing database. Will remove all keys before updating |
+ // the database if |clear_all_first| is set. Then all entries in |
+ // |changes| will be examined - keys mapped to a null NullableString16 |
+ // will be removed and all others will be inserted/updated as appropriate. |
+ bool CommitChanges(bool clear_all_first, const ValuesMap& changes); |
+ |
+ private: |
+ FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, SimpleOpenAndClose); |
+ FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestLazyOpenIsLazy); |
+ FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, |
+ TestLazyOpenUpgradesV1TableToV2); |
+ FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestFailedUpgrade); |
+ FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestIsOpen); |
+ FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, SimpleWriteAndReadBack); |
+ FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, WriteWithClear); |
+ FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, |
+ UpgradeFromV1ToV2WithData); |
+ FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, |
+ TestOpenCloseDataPreserved); |
+ FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestSimpleRemoveOneValue); |
+ FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, |
+ TestCanOpenAndReadWebCoreDatabase); |
+ FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, |
+ TestCanOpenFileThatIsNotADatabase); |
+ |
+ // Open the database at file_path_ if it exists already and creates it if |
+ // |create_if_needed| is true. |
+ // Ensures we are at the correct database version and creates or updates |
+ // tables as necessary. Returns false on failure. |
+ bool LazyOpen(bool create_if_needed); |
+ |
+ // Creates the ItemTable at the current version of the database. Used |
+ // when creating the table from scratch. |
+ bool CreateTable(); |
+ |
+ // Creates the database table at V2. Used when upgrading the database |
+ // table to V2. |
+ bool CreateTableV2(); |
+ |
+ // If we have issues while trying to open the file (corrupted databse, |
+ // failing to upgrade, that sort of thing) this function will remove |
+ // the file from disk and attempt to create a new database from |
+ // scratch. |
+ bool DeleteFileAndRecreate(); |
+ |
+ // Version 1 -> 2 migrates the value column in the ItemTable from a TEXT |
+ // to a BLOB. Exisitng data is preserved on success. Returns false if the |
+ // upgrade failed. If true is returned, the database is guaranteed to be at |
+ // version 2 (either it already was, or the upgrade from version 1 was |
+ // successful). |
+ bool UpgradeVersion1To2IfNeeded(); |
+ |
+ void Close(); |
+ bool IsOpen() const { return db_.get() ? db_->is_open() : false; } |
+ |
+#ifdef UNIT_TEST |
+ // This constructor allows us to bypass the DCHECK in the public |
+ // constructor that normally verifies a valid file path was passed to |
+ // back the database on disk. We want to be able to run unit tests |
+ // from in-memory databases where possible so we use an empty |
+ // backing file path to signify we should open the database in memory |
+ // inside LazyOpen. This constructor will allow us to bypass the |
+ // DCHECK when running the unit tests. |
michaeln
2012/02/08 06:11:33
ah... i see the comment moved to here!
|
+ DomStorageDatabase(); |
+#endif |
+ |
+ // Path to the database on disk. |
+ FilePath file_path_; |
+ scoped_ptr<sql::Connection> db_; |
+ bool failed_to_open_; |
+ bool tried_to_recreate_; |
+}; |
+ |
+} // namespace dom_storage |
+ |
+#endif // WEBKIT_DOM_STORAGE_DOM_STORAGE_DATABASE_H_ |