Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 5 | #ifndef CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_DATABASE_H_ |
| 6 | #define CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_DATABASE_H_ |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | |
| 10 | #include "base/files/file_path.h" |
| 11 | #include "base/gtest_prod_util.h" |
| 12 | #include "base/memory/scoped_ptr.h" |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 13 | #include "base/strings/nullable_string16.h" |
Torne (Richard Coles) | 5e3f23d | 2013-06-11 16:24:11 +0100 | [diff] [blame] | 14 | #include "base/strings/string16.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 15 | #include "content/common/content_export.h" |
| 16 | #include "content/common/dom_storage/dom_storage_types.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 17 | #include "sql/connection.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 18 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 19 | namespace content { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 20 | |
| 21 | // Represents a SQLite based backing for DOM storage data. This |
| 22 | // class is designed to be used on a single thread. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 23 | class CONTENT_EXPORT DOMStorageDatabase { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 24 | public: |
| 25 | static base::FilePath GetJournalFilePath(const base::FilePath& database_path); |
| 26 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 27 | explicit DOMStorageDatabase(const base::FilePath& file_path); |
| 28 | virtual ~DOMStorageDatabase(); // virtual for unit testing |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 29 | |
| 30 | // Reads all the key, value pairs stored in the database and returns |
| 31 | // them. |result| is assumed to be empty and any duplicate keys will |
| 32 | // be overwritten. If the database exists on disk then it will be |
| 33 | // opened. If it does not exist then it will not be created and |
| 34 | // |result| will be unmodified. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 35 | void ReadAllValues(DOMStorageValuesMap* result); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 36 | |
| 37 | // Updates the backing database. Will remove all keys before updating |
| 38 | // the database if |clear_all_first| is set. Then all entries in |
| 39 | // |changes| will be examined - keys mapped to a null NullableString16 |
| 40 | // will be removed and all others will be inserted/updated as appropriate. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 41 | bool CommitChanges(bool clear_all_first, const DOMStorageValuesMap& changes); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 42 | |
| 43 | // Simple getter for the path we were constructed with. |
| 44 | const base::FilePath& file_path() const { return file_path_; } |
| 45 | |
| 46 | protected: |
| 47 | // Constructor that uses an in-memory sqlite database, for testing. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 48 | DOMStorageDatabase(); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 49 | |
| 50 | private: |
| 51 | friend class LocalStorageDatabaseAdapter; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 52 | FRIEND_TEST_ALL_PREFIXES(DOMStorageDatabaseTest, SimpleOpenAndClose); |
| 53 | FRIEND_TEST_ALL_PREFIXES(DOMStorageDatabaseTest, TestLazyOpenIsLazy); |
| 54 | FRIEND_TEST_ALL_PREFIXES(DOMStorageDatabaseTest, TestDetectSchemaVersion); |
| 55 | FRIEND_TEST_ALL_PREFIXES(DOMStorageDatabaseTest, |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 56 | TestLazyOpenUpgradesDatabase); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 57 | FRIEND_TEST_ALL_PREFIXES(DOMStorageDatabaseTest, SimpleWriteAndReadBack); |
| 58 | FRIEND_TEST_ALL_PREFIXES(DOMStorageDatabaseTest, WriteWithClear); |
| 59 | FRIEND_TEST_ALL_PREFIXES(DOMStorageDatabaseTest, |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 60 | UpgradeFromV1ToV2WithData); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 61 | FRIEND_TEST_ALL_PREFIXES(DOMStorageDatabaseTest, TestSimpleRemoveOneValue); |
| 62 | FRIEND_TEST_ALL_PREFIXES(DOMStorageDatabaseTest, |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 63 | TestCanOpenAndReadWebCoreDatabase); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 64 | FRIEND_TEST_ALL_PREFIXES(DOMStorageDatabaseTest, |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 65 | TestCanOpenFileThatIsNotADatabase); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 66 | FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, BackingDatabaseOpened); |
| 67 | FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, CommitTasks); |
| 68 | FRIEND_TEST_ALL_PREFIXES(DOMStorageAreaTest, PurgeMemory); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 69 | |
| 70 | enum SchemaVersion { |
| 71 | INVALID, |
| 72 | V1, |
| 73 | V2 |
| 74 | }; |
| 75 | |
| 76 | // Open the database at file_path_ if it exists already and creates it if |
| 77 | // |create_if_needed| is true. |
| 78 | // Ensures we are at the correct database version and creates or updates |
| 79 | // tables as necessary. Returns false on failure. |
| 80 | bool LazyOpen(bool create_if_needed); |
| 81 | |
| 82 | // Analyses the database to verify that the connection that is open is indeed |
| 83 | // a valid database and works out the schema version. |
| 84 | SchemaVersion DetectSchemaVersion(); |
| 85 | |
| 86 | // Creates the database table at V2. Returns true if the table was created |
| 87 | // successfully, false otherwise. Will return false if the table already |
| 88 | // exists. |
| 89 | bool CreateTableV2(); |
| 90 | |
| 91 | // If we have issues while trying to open the file (corrupted databse, |
| 92 | // failing to upgrade, that sort of thing) this function will remove |
| 93 | // the file from disk and attempt to create a new database from |
| 94 | // scratch. |
| 95 | bool DeleteFileAndRecreate(); |
| 96 | |
| 97 | // Version 1 -> 2 migrates the value column in the ItemTable from a TEXT |
| 98 | // to a BLOB. Exisitng data is preserved on success. Returns false if the |
| 99 | // upgrade failed. If true is returned, the database is guaranteed to be at |
| 100 | // version 2. |
| 101 | bool UpgradeVersion1To2(); |
| 102 | |
| 103 | void Close(); |
| 104 | bool IsOpen() const { return db_.get() ? db_->is_open() : false; } |
| 105 | |
| 106 | // Initialization code shared between the two constructors of this class. |
| 107 | void Init(); |
| 108 | |
| 109 | // Path to the database on disk. |
| 110 | const base::FilePath file_path_; |
| 111 | scoped_ptr<sql::Connection> db_; |
| 112 | bool failed_to_open_; |
| 113 | bool tried_to_recreate_; |
| 114 | bool known_to_be_empty_; |
| 115 | }; |
| 116 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 117 | } // namespace content |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 118 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 119 | #endif // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_DATABASE_H_ |