| OLD | NEW |
| 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 #ifndef WEBKIT_DOM_STORAGE_DOM_STORAGE_DATABASE_H_ | 5 #ifndef WEBKIT_DOM_STORAGE_DOM_STORAGE_DATABASE_H_ |
| 6 #define WEBKIT_DOM_STORAGE_DOM_STORAGE_DATABASE_H_ | 6 #define WEBKIT_DOM_STORAGE_DOM_STORAGE_DATABASE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 | 10 |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/nullable_string16.h" | 14 #include "base/nullable_string16.h" |
| 15 #include "base/string16.h" | 15 #include "base/string16.h" |
| 16 #include "sql/connection.h" | 16 #include "sql/connection.h" |
| 17 #include "webkit/dom_storage/dom_storage_types.h" | 17 #include "webkit/dom_storage/dom_storage_types.h" |
| 18 | 18 |
| 19 namespace dom_storage { | 19 namespace dom_storage { |
| 20 | 20 |
| 21 // Represents a SQLite based backing for DOM storage data. This | 21 // Represents a SQLite based backing for DOM storage data. This |
| 22 // class is designed to be used on a single thread. | 22 // class is designed to be used on a single thread. |
| 23 class DomStorageDatabase { | 23 class DomStorageDatabase { |
| 24 public: | 24 public: |
| 25 explicit DomStorageDatabase(const FilePath& file_path); | 25 explicit DomStorageDatabase(const FilePath& file_path); |
| 26 ~DomStorageDatabase(); | 26 virtual ~DomStorageDatabase(); // virtual for unit testing |
| 27 | 27 |
| 28 // Reads all the key, value pairs stored in the database and returns | 28 // Reads all the key, value pairs stored in the database and returns |
| 29 // them. |result| is assumed to be empty and any duplicate keys will | 29 // them. |result| is assumed to be empty and any duplicate keys will |
| 30 // be overwritten. If the database exists on disk then it will be | 30 // be overwritten. If the database exists on disk then it will be |
| 31 // opened. If it does not exist then it will not be created and | 31 // opened. If it does not exist then it will not be created and |
| 32 // |result| will be unmodified. | 32 // |result| will be unmodified. |
| 33 void ReadAllValues(ValuesMap* result); | 33 void ReadAllValues(ValuesMap* result); |
| 34 | 34 |
| 35 // Updates the backing database. Will remove all keys before updating | 35 // Updates the backing database. Will remove all keys before updating |
| 36 // the database if |clear_all_first| is set. Then all entries in | 36 // the database if |clear_all_first| is set. Then all entries in |
| 37 // |changes| will be examined - keys mapped to a null NullableString16 | 37 // |changes| will be examined - keys mapped to a null NullableString16 |
| 38 // will be removed and all others will be inserted/updated as appropriate. | 38 // will be removed and all others will be inserted/updated as appropriate. |
| 39 bool CommitChanges(bool clear_all_first, const ValuesMap& changes); | 39 bool CommitChanges(bool clear_all_first, const ValuesMap& changes); |
| 40 | 40 |
| 41 protected: |
| 42 // Constructor that uses an in-memory sqlite database, for testing. |
| 43 DomStorageDatabase(); |
| 44 |
| 41 private: | 45 private: |
| 42 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, SimpleOpenAndClose); | 46 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, SimpleOpenAndClose); |
| 43 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestLazyOpenIsLazy); | 47 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestLazyOpenIsLazy); |
| 44 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestDetectSchemaVersion); | 48 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestDetectSchemaVersion); |
| 45 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, | 49 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, |
| 46 TestLazyOpenUpgradesDatabase); | 50 TestLazyOpenUpgradesDatabase); |
| 47 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, SimpleWriteAndReadBack); | 51 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, SimpleWriteAndReadBack); |
| 48 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, WriteWithClear); | 52 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, WriteWithClear); |
| 49 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, | 53 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, |
| 50 UpgradeFromV1ToV2WithData); | 54 UpgradeFromV1ToV2WithData); |
| 51 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestSimpleRemoveOneValue); | 55 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestSimpleRemoveOneValue); |
| 52 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, | 56 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, |
| 53 TestCanOpenAndReadWebCoreDatabase); | 57 TestCanOpenAndReadWebCoreDatabase); |
| 54 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, | 58 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, |
| 55 TestCanOpenFileThatIsNotADatabase); | 59 TestCanOpenFileThatIsNotADatabase); |
| 56 FRIEND_TEST_ALL_PREFIXES(DomStorageAreaTest, BackingDatabaseOpened); | 60 FRIEND_TEST_ALL_PREFIXES(DomStorageAreaTest, BackingDatabaseOpened); |
| 61 FRIEND_TEST_ALL_PREFIXES(DomStorageAreaTest, CommitTasks); |
| 57 | 62 |
| 58 enum SchemaVersion { | 63 enum SchemaVersion { |
| 59 INVALID, | 64 INVALID, |
| 60 V1, | 65 V1, |
| 61 V2 | 66 V2 |
| 62 }; | 67 }; |
| 63 | 68 |
| 64 // Open the database at file_path_ if it exists already and creates it if | 69 // Open the database at file_path_ if it exists already and creates it if |
| 65 // |create_if_needed| is true. | 70 // |create_if_needed| is true. |
| 66 // Ensures we are at the correct database version and creates or updates | 71 // Ensures we are at the correct database version and creates or updates |
| (...skipping 17 matching lines...) Expand all Loading... |
| 84 | 89 |
| 85 // Version 1 -> 2 migrates the value column in the ItemTable from a TEXT | 90 // Version 1 -> 2 migrates the value column in the ItemTable from a TEXT |
| 86 // to a BLOB. Exisitng data is preserved on success. Returns false if the | 91 // to a BLOB. Exisitng data is preserved on success. Returns false if the |
| 87 // upgrade failed. If true is returned, the database is guaranteed to be at | 92 // upgrade failed. If true is returned, the database is guaranteed to be at |
| 88 // version 2. | 93 // version 2. |
| 89 bool UpgradeVersion1To2(); | 94 bool UpgradeVersion1To2(); |
| 90 | 95 |
| 91 void Close(); | 96 void Close(); |
| 92 bool IsOpen() const { return db_.get() ? db_->is_open() : false; } | 97 bool IsOpen() const { return db_.get() ? db_->is_open() : false; } |
| 93 | 98 |
| 94 #ifdef UNIT_TEST | |
| 95 // This constructor allows us to bypass the DCHECK in the public | |
| 96 // constructor that normally verifies a valid file path was passed to | |
| 97 // back the database on disk. We want to be able to run unit tests | |
| 98 // from in-memory databases where possible, so we use an empty | |
| 99 // backing file path to signify we should open the database in memory | |
| 100 // inside LazyOpen. This constructor will allow us to bypass the | |
| 101 // DCHECK when running the unit tests. | |
| 102 DomStorageDatabase(); | |
| 103 #endif | |
| 104 | |
| 105 // Initialization code shared between the two constructors of this class. | 99 // Initialization code shared between the two constructors of this class. |
| 106 void Init(); | 100 void Init(); |
| 107 | 101 |
| 108 // Path to the database on disk. | 102 // Path to the database on disk. |
| 109 FilePath file_path_; | 103 FilePath file_path_; |
| 110 scoped_ptr<sql::Connection> db_; | 104 scoped_ptr<sql::Connection> db_; |
| 111 bool failed_to_open_; | 105 bool failed_to_open_; |
| 112 bool tried_to_recreate_; | 106 bool tried_to_recreate_; |
| 113 bool known_to_be_empty_; | 107 bool known_to_be_empty_; |
| 114 }; | 108 }; |
| 115 | 109 |
| 116 } // namespace dom_storage | 110 } // namespace dom_storage |
| 117 | 111 |
| 118 #endif // WEBKIT_DOM_STORAGE_DOM_STORAGE_DATABASE_H_ | 112 #endif // WEBKIT_DOM_STORAGE_DOM_STORAGE_DATABASE_H_ |
| OLD | NEW |