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 "googleurl/src/gurl.h" | |
16 #include "sql/connection.h" | 17 #include "sql/connection.h" |
17 #include "webkit/dom_storage/dom_storage_types.h" | 18 #include "webkit/dom_storage/dom_storage_types.h" |
18 | 19 |
19 namespace dom_storage { | 20 namespace dom_storage { |
20 | 21 |
21 // Represents a SQLite based backing for DOM storage data. This | 22 class DomStorageDatabase { |
23 public: | |
24 DomStorageDatabase(const FilePath& file_path); | |
25 virtual ~DomStorageDatabase(); | |
26 | |
27 // Reads all the key, value pairs stored for |namespace_id| and |origin| in | |
28 // the database and returns them. |result| is assumed to be empty and any | |
29 // duplicate keys will be overwritten. If the database exists on disk then it | |
30 // will be opened. If it does not exist then it will not be created and | |
31 // |result| will be unmodified. | |
32 virtual void ReadAllValues(int64 namespace_id, | |
michaeln
2012/04/12 01:48:46
I think this existing class for localstorage backe
marja
2012/04/19 10:20:50
Done. Though, the DomStorageArea still has 2 point
| |
33 const GURL& origin, | |
34 ValuesMap* result) = 0; | |
35 | |
36 // Updates the backing database. Will remove all keys before updating | |
37 // the database if |clear_all_first| is set. Then all entries in | |
38 // |changes| will be examined - keys mapped to a null NullableString16 | |
39 // will be removed and all others will be inserted/updated as appropriate. | |
40 virtual bool CommitChanges(int64 namespace_id, | |
41 const GURL& origin, | |
42 bool clear_all_first, | |
43 const ValuesMap& changes) = 0; | |
44 | |
45 // Simple getter for the path we were constructed with. | |
46 const FilePath& file_path() const { return file_path_; } | |
47 | |
48 protected: | |
49 // Open the database at file_path_ if it exists already and creates it if | |
50 // |create_if_needed| is true. | |
51 // Ensures we are at the correct database version and creates or updates | |
52 // tables as necessary. Returns false on failure. | |
53 virtual bool LazyOpen(bool create_if_needed) = 0; | |
54 | |
55 virtual bool IsOpen() const = 0; | |
56 | |
57 FilePath file_path_; | |
58 bool failed_to_open_; | |
59 }; | |
60 | |
61 // Represents a SQLite based backing for localStorage data. This | |
22 // class is designed to be used on a single thread. | 62 // class is designed to be used on a single thread. |
23 class DomStorageDatabase { | 63 class LocalStorageDatabase : public DomStorageDatabase { |
24 public: | 64 public: |
25 static FilePath GetJournalFilePath(const FilePath& database_path); | 65 static FilePath GetJournalFilePath(const FilePath& database_path); |
26 | 66 |
27 explicit DomStorageDatabase(const FilePath& file_path); | 67 explicit LocalStorageDatabase(const FilePath& file_path); |
28 virtual ~DomStorageDatabase(); // virtual for unit testing | 68 virtual ~LocalStorageDatabase(); |
29 | 69 |
30 // Reads all the key, value pairs stored in the database and returns | 70 // 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 | 71 // 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 | 72 // 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 | 73 // opened. If it does not exist then it will not be created and |
34 // |result| will be unmodified. | 74 // |result| will be unmodified. |
35 void ReadAllValues(ValuesMap* result); | 75 virtual void ReadAllValues(int64 namespace_id, |
76 const GURL& origin, | |
77 ValuesMap* result) OVERRIDE; | |
36 | 78 |
37 // Updates the backing database. Will remove all keys before updating | 79 // Updates the backing database. Will remove all keys before updating |
38 // the database if |clear_all_first| is set. Then all entries in | 80 // the database if |clear_all_first| is set. Then all entries in |
39 // |changes| will be examined - keys mapped to a null NullableString16 | 81 // |changes| will be examined - keys mapped to a null NullableString16 |
40 // will be removed and all others will be inserted/updated as appropriate. | 82 // will be removed and all others will be inserted/updated as appropriate. |
41 bool CommitChanges(bool clear_all_first, const ValuesMap& changes); | 83 virtual bool CommitChanges(int64 namespace_id, |
42 | 84 const GURL& origin, |
43 // Simple getter for the path we were constructed with. | 85 bool clear_all_first, |
44 const FilePath& file_path() const { return file_path_; } | 86 const ValuesMap& changes) OVERRIDE; |
45 | 87 |
46 protected: | 88 protected: |
47 // Constructor that uses an in-memory sqlite database, for testing. | 89 // Constructor that uses an in-memory sqlite database, for testing. |
48 DomStorageDatabase(); | 90 LocalStorageDatabase(); |
49 | 91 |
50 private: | 92 private: |
51 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, SimpleOpenAndClose); | 93 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, SimpleOpenAndClose); |
52 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestLazyOpenIsLazy); | 94 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestLazyOpenIsLazy); |
53 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestDetectSchemaVersion); | 95 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestDetectSchemaVersion); |
54 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, | 96 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, |
55 TestLazyOpenUpgradesDatabase); | 97 TestLazyOpenUpgradesDatabase); |
56 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, SimpleWriteAndReadBack); | 98 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, SimpleWriteAndReadBack); |
57 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, WriteWithClear); | 99 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, WriteWithClear); |
58 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, | 100 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, |
59 UpgradeFromV1ToV2WithData); | 101 UpgradeFromV1ToV2WithData); |
60 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestSimpleRemoveOneValue); | 102 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, TestSimpleRemoveOneValue); |
61 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, | 103 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, |
62 TestCanOpenAndReadWebCoreDatabase); | 104 TestCanOpenAndReadWebCoreDatabase); |
63 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, | 105 FRIEND_TEST_ALL_PREFIXES(DomStorageDatabaseTest, |
64 TestCanOpenFileThatIsNotADatabase); | 106 TestCanOpenFileThatIsNotADatabase); |
65 FRIEND_TEST_ALL_PREFIXES(DomStorageAreaTest, BackingDatabaseOpened); | 107 FRIEND_TEST_ALL_PREFIXES(DomStorageAreaTest, BackingDatabaseOpened); |
66 FRIEND_TEST_ALL_PREFIXES(DomStorageAreaTest, CommitTasks); | 108 FRIEND_TEST_ALL_PREFIXES(DomStorageAreaTest, CommitTasks); |
67 FRIEND_TEST_ALL_PREFIXES(DomStorageAreaTest, PurgeMemory); | 109 FRIEND_TEST_ALL_PREFIXES(DomStorageAreaTest, PurgeMemory); |
68 | 110 |
69 enum SchemaVersion { | 111 enum SchemaVersion { |
70 INVALID, | 112 INVALID, |
71 V1, | 113 V1, |
72 V2 | 114 V2 |
73 }; | 115 }; |
74 | |
75 // Open the database at file_path_ if it exists already and creates it if | 116 // Open the database at file_path_ if it exists already and creates it if |
76 // |create_if_needed| is true. | 117 // |create_if_needed| is true. |
77 // Ensures we are at the correct database version and creates or updates | 118 // Ensures we are at the correct database version and creates or updates |
78 // tables as necessary. Returns false on failure. | 119 // tables as necessary. Returns false on failure. |
79 bool LazyOpen(bool create_if_needed); | 120 virtual bool LazyOpen(bool create_if_needed) OVERRIDE; |
80 | 121 |
81 // Analyses the database to verify that the connection that is open is indeed | 122 // Analyses the database to verify that the connection that is open is indeed |
82 // a valid database and works out the schema version. | 123 // a valid database and works out the schema version. |
83 SchemaVersion DetectSchemaVersion(); | 124 SchemaVersion DetectSchemaVersion(); |
84 | 125 |
85 // Creates the database table at V2. Returns true if the table was created | 126 // Creates the database table at V2. Returns true if the table was created |
86 // successfully, false otherwise. Will return false if the table already | 127 // successfully, false otherwise. Will return false if the table already |
87 // exists. | 128 // exists. |
88 bool CreateTableV2(); | 129 bool CreateTableV2(); |
89 | 130 |
90 // If we have issues while trying to open the file (corrupted databse, | 131 // If we have issues while trying to open the file (corrupted databse, |
91 // failing to upgrade, that sort of thing) this function will remove | 132 // failing to upgrade, that sort of thing) this function will remove |
92 // the file from disk and attempt to create a new database from | 133 // the file from disk and attempt to create a new database from |
93 // scratch. | 134 // scratch. |
94 bool DeleteFileAndRecreate(); | 135 bool DeleteFileAndRecreate(); |
95 | 136 |
96 // Version 1 -> 2 migrates the value column in the ItemTable from a TEXT | 137 // Version 1 -> 2 migrates the value column in the ItemTable from a TEXT |
97 // to a BLOB. Exisitng data is preserved on success. Returns false if the | 138 // to a BLOB. Exisitng data is preserved on success. Returns false if the |
98 // upgrade failed. If true is returned, the database is guaranteed to be at | 139 // upgrade failed. If true is returned, the database is guaranteed to be at |
99 // version 2. | 140 // version 2. |
100 bool UpgradeVersion1To2(); | 141 bool UpgradeVersion1To2(); |
101 | 142 |
102 void Close(); | 143 void Close(); |
103 bool IsOpen() const { return db_.get() ? db_->is_open() : false; } | 144 virtual bool IsOpen() const; |
104 | 145 |
105 // Initialization code shared between the two constructors of this class. | 146 // Initialization code shared between the two constructors of this class. |
106 void Init(); | 147 void Init(); |
107 | 148 |
108 // Path to the database on disk. | |
109 const FilePath file_path_; | |
110 scoped_ptr<sql::Connection> db_; | 149 scoped_ptr<sql::Connection> db_; |
111 bool failed_to_open_; | |
112 bool tried_to_recreate_; | 150 bool tried_to_recreate_; |
113 bool known_to_be_empty_; | 151 bool known_to_be_empty_; |
114 }; | 152 }; |
115 | 153 |
116 } // namespace dom_storage | 154 } // namespace dom_storage |
117 | 155 |
118 #endif // WEBKIT_DOM_STORAGE_DOM_STORAGE_DATABASE_H_ | 156 #endif // WEBKIT_DOM_STORAGE_DOM_STORAGE_DATABASE_H_ |
OLD | NEW |