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_SESSION_STORAGE_DATABASE_H_ | 5 #ifndef WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ |
6 #define WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ | 6 #define WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 | 11 |
| 12 #include "base/callback.h" |
12 #include "base/file_path.h" | 13 #include "base/file_path.h" |
13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
15 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
16 #include "third_party/leveldatabase/src/include/leveldb/status.h" | 17 #include "third_party/leveldatabase/src/include/leveldb/status.h" |
17 #include "webkit/dom_storage/dom_storage_types.h" | 18 #include "webkit/dom_storage/dom_storage_types.h" |
18 | 19 |
19 class GURL; | 20 class GURL; |
20 | 21 |
21 namespace leveldb { | 22 namespace leveldb { |
22 class DB; | 23 class DB; |
23 class WriteBatch; | 24 class WriteBatch; |
24 } // namespace leveldb | 25 } // namespace leveldb |
25 | 26 |
26 namespace dom_storage { | 27 namespace dom_storage { |
27 | 28 |
28 // SessionStorageDatabase holds the data from multiple namespaces and multiple | 29 // SessionStorageDatabase holds the data from multiple namespaces and multiple |
29 // origins. All DomStorageAreas for session storage share the same | 30 // origins. All DomStorageAreas for session storage share the same |
30 // SessionStorageDatabase. | 31 // SessionStorageDatabase. |
31 class SessionStorageDatabase : | 32 class SessionStorageDatabase : |
32 public base::RefCountedThreadSafe<SessionStorageDatabase> { | 33 public base::RefCountedThreadSafe<SessionStorageDatabase> { |
33 public: | 34 public: |
34 explicit SessionStorageDatabase(const FilePath& file_path); | 35 explicit SessionStorageDatabase(const FilePath& file_path); |
35 | 36 |
36 // Reads the (key, value) pairs for |namespace_id| and |origin|. |result| is | 37 // Reads the (key, value) pairs for |namespace_id| and |origin|. |result| is |
37 // assumed to be empty and any duplicate keys will be overwritten. If the | 38 // assumed to be empty and any duplicate keys will be overwritten. If the |
38 // database exists on disk then it will be opened. If it does not exist then | 39 // database exists on disk then it will be opened. If it does not exist then |
39 // it will not be created and |result| will be unmodified. | 40 // it will not be created and |result| will be unmodified. |
40 void ReadAreaValues(int64 namespace_id, | 41 void ReadAreaValues(const std::string& namespace_id, |
41 const GURL& origin, | 42 const GURL& origin, |
42 ValuesMap* result); | 43 ValuesMap* result); |
43 | 44 |
44 // Updates the data for |namespace_id| and |origin|. Will remove all keys | 45 // Updates the data for |namespace_id| and |origin|. Will remove all keys |
45 // before updating the database if |clear_all_first| is set. Then all entries | 46 // before updating the database if |clear_all_first| is set. Then all entries |
46 // in |changes| will be examined - keys mapped to a null NullableString16 will | 47 // in |changes| will be examined - keys mapped to a null NullableString16 will |
47 // be removed and all others will be inserted/updated as appropriate. It is | 48 // be removed and all others will be inserted/updated as appropriate. It is |
48 // allowed to write data into a shallow copy created by CloneNamespace, and in | 49 // allowed to write data into a shallow copy created by CloneNamespace, and in |
49 // that case the copy will be made deep before writing the values. | 50 // that case the copy will be made deep before writing the values. |
50 bool CommitAreaChanges(int64 namespace_id, | 51 bool CommitAreaChanges(const std::string& namespace_id, |
51 const GURL& origin, | 52 const GURL& origin, |
52 bool clear_all_first, | 53 bool clear_all_first, |
53 const ValuesMap& changes); | 54 const ValuesMap& changes); |
54 | 55 |
55 // Creates shallow copies of the areas for |namespace_id| and associates them | 56 // Creates shallow copies of the areas for |namespace_id| and associates them |
56 // with |new_namespace_id|. | 57 // with |new_namespace_id|. |
57 bool CloneNamespace(int64 namespace_id, int64 new_namespace_id); | 58 bool CloneNamespace(const std::string& namespace_id, |
| 59 const std::string& new_namespace_id); |
58 | 60 |
59 // Deletes the data for |namespace_id| and |origin|. | 61 // Deletes the data for |namespace_id| and |origin|. |
60 bool DeleteArea(int64 namespace_id, const GURL& origin); | 62 bool DeleteArea(const std::string& namespace_id, const GURL& origin); |
61 | 63 |
62 // Deletes the data for |namespace_id|. | 64 // Deletes the data for |namespace_id|. |
63 bool DeleteNamespace(int64 namespace_id); | 65 bool DeleteNamespace(const std::string& namespace_id); |
| 66 |
| 67 // Reads all namespace ids from the database. |
| 68 bool ReadNamespaceIds(std::vector<std::string>* namespace_ids); |
64 | 69 |
65 private: | 70 private: |
66 friend class base::RefCountedThreadSafe<SessionStorageDatabase>; | 71 friend class base::RefCountedThreadSafe<SessionStorageDatabase>; |
67 friend class SessionStorageDatabaseTest; | 72 friend class SessionStorageDatabaseTest; |
68 | 73 |
69 ~SessionStorageDatabase(); | 74 ~SessionStorageDatabase(); |
70 | 75 |
71 // Opens the database at file_path_ if it exists already and creates it if | 76 // Opens the database at file_path_ if it exists already and creates it if |
72 // |create_if_needed| is true. Returns true if the database was opened, false | 77 // |create_if_needed| is true. Returns true if the database was opened, false |
73 // if the opening failed or was not necessary (the database doesn't exist and | 78 // if the opening failed or was not necessary (the database doesn't exist and |
(...skipping 16 matching lines...) Expand all Loading... |
90 bool DatabaseErrorCheck(bool ok); | 95 bool DatabaseErrorCheck(bool ok); |
91 | 96 |
92 // Helper functions. All return true if the operation succeeded, and false if | 97 // Helper functions. All return true if the operation succeeded, and false if |
93 // it failed (a database error or a consistency error). If the return type is | 98 // it failed (a database error or a consistency error). If the return type is |
94 // void, the operation cannot fail. If they return false, ConsistencyCheck or | 99 // void, the operation cannot fail. If they return false, ConsistencyCheck or |
95 // DatabaseErrorCheck have already been called. | 100 // DatabaseErrorCheck have already been called. |
96 | 101 |
97 // Creates a namespace for |namespace_id| and updates the next namespace id if | 102 // Creates a namespace for |namespace_id| and updates the next namespace id if |
98 // needed. If |ok_if_exists| is false, checks that the namespace didn't exist | 103 // needed. If |ok_if_exists| is false, checks that the namespace didn't exist |
99 // before. | 104 // before. |
100 bool CreateNamespace(int64 namespace_id, | 105 bool CreateNamespace(const std::string& namespace_id, |
101 bool ok_if_exists, | 106 bool ok_if_exists, |
102 leveldb::WriteBatch* batch); | 107 leveldb::WriteBatch* batch); |
103 // Reads the next namespace id. | 108 |
104 bool GetNextNamespaceId(int64* next_namespace_id); | |
105 bool UpdateNextNamespaceId(int64 namespace_id, | |
106 leveldb::WriteBatch* batch); | |
107 // Reads the areas assoiated with |namespace_id| and puts the (origin, map_id) | 109 // Reads the areas assoiated with |namespace_id| and puts the (origin, map_id) |
108 // pairs into |areas|. | 110 // pairs into |areas|. |
109 bool GetAreasInNamespace(int64 namespace_id, | 111 bool GetAreasInNamespace(const std::string& namespace_id, |
110 std::map<std::string, std::string>* areas); | |
111 bool GetAreasInNamespace(const std::string& namespace_id_str, | |
112 std::map<std::string, std::string>* areas); | 112 std::map<std::string, std::string>* areas); |
113 | 113 |
114 // Adds an association between |origin| and |map_id| into the namespace | 114 // Adds an association between |origin| and |map_id| into the namespace |
115 // |namespace_id|. | 115 // |namespace_id|. |
116 void AddAreaToNamespace(int64 namespace_id, | 116 void AddAreaToNamespace(const std::string& namespace_id, |
117 const std::string& origin, | 117 const std::string& origin, |
118 const std::string& map_id, | 118 const std::string& map_id, |
119 leveldb::WriteBatch* batch); | 119 leveldb::WriteBatch* batch); |
120 | 120 |
121 // Helpers for deleting data for |namespace_id| and |origin|. | 121 // Helpers for deleting data for |namespace_id| and |origin|. |
122 bool DeleteArea(int64 namespace_id, | 122 bool DeleteAreaHelper(const std::string& namespace_id, |
123 const std::string& origin, | 123 const std::string& origin, |
124 leveldb::WriteBatch* batch); | 124 leveldb::WriteBatch* batch); |
125 bool DeleteArea(const std::string& namespace_id_str, | |
126 const std::string& origin, | |
127 leveldb::WriteBatch* batch); | |
128 | 125 |
129 // Retrieves the map id for |namespace_id| and |origin|. It's not an error if | 126 // Retrieves the map id for |namespace_id| and |origin|. It's not an error if |
130 // the map doesn't exist. | 127 // the map doesn't exist. |
131 bool GetMapForArea(int64 namespace_id, | 128 bool GetMapForArea(const std::string& namespace_id, |
132 const GURL& origin, | |
133 bool* exists, | |
134 std::string* map_id); | |
135 bool GetMapForArea(const std::string& namespace_id_str, | |
136 const std::string& origin, | 129 const std::string& origin, |
137 bool* exists, | 130 bool* exists, |
138 std::string* map_id); | 131 std::string* map_id); |
139 | 132 |
140 // Creates a new map for |namespace_id| and |origin|. |map_id| will hold the | 133 // Creates a new map for |namespace_id| and |origin|. |map_id| will hold the |
141 // id of the created map. If there is a map for |namespace_id| and |origin|, | 134 // id of the created map. If there is a map for |namespace_id| and |origin|, |
142 // this just overwrites the map id. The caller is responsible for decreasing | 135 // this just overwrites the map id. The caller is responsible for decreasing |
143 // the ref count. | 136 // the ref count. |
144 bool CreateMapForArea(int64 namespace_id, | 137 bool CreateMapForArea(const std::string& namespace_id, |
145 const GURL& origin, | 138 const GURL& origin, |
146 std::string* map_id, | 139 std::string* map_id, |
147 leveldb::WriteBatch* batch); | 140 leveldb::WriteBatch* batch); |
148 // Reads the contents of the map |map_id| into |result|. If |only_keys| is | 141 // Reads the contents of the map |map_id| into |result|. If |only_keys| is |
149 // true, only keys are aread from the database and the values in |result| will | 142 // true, only keys are aread from the database and the values in |result| will |
150 // be empty. | 143 // be empty. |
151 bool ReadMap(const std::string& map_id, | 144 bool ReadMap(const std::string& map_id, |
152 ValuesMap* result, | 145 ValuesMap* result, |
153 bool only_keys); | 146 bool only_keys); |
154 // Writes |values| into the map |map_id|. | 147 // Writes |values| into the map |map_id|. |
155 void WriteValuesToMap(const std::string& map_id, | 148 void WriteValuesToMap(const std::string& map_id, |
156 const ValuesMap& values, | 149 const ValuesMap& values, |
157 leveldb::WriteBatch* batch); | 150 leveldb::WriteBatch* batch); |
158 | 151 |
159 bool GetMapRefCount(const std::string& map_id, int64* ref_count); | 152 bool GetMapRefCount(const std::string& map_id, int64* ref_count); |
160 bool IncreaseMapRefCount(const std::string& map_id, | 153 bool IncreaseMapRefCount(const std::string& map_id, |
161 leveldb::WriteBatch* batch); | 154 leveldb::WriteBatch* batch); |
162 // Decreases the ref count of a map by |decrease|. If the ref count goes to 0, | 155 // Decreases the ref count of a map by |decrease|. If the ref count goes to 0, |
163 // deletes the map. | 156 // deletes the map. |
164 bool DecreaseMapRefCount(const std::string& map_id, | 157 bool DecreaseMapRefCount(const std::string& map_id, |
165 int decrease, | 158 int decrease, |
166 leveldb::WriteBatch* batch); | 159 leveldb::WriteBatch* batch); |
167 | 160 |
168 // Deletes all values in |map_id|. | 161 // Deletes all values in |map_id|. |
169 bool ClearMap(const std::string& map_id, leveldb::WriteBatch* batch); | 162 bool ClearMap(const std::string& map_id, leveldb::WriteBatch* batch); |
170 | 163 |
171 // Breaks the association between (|namespace_id|, |origin|) and |map_id| and | 164 // Breaks the association between (|namespace_id|, |origin|) and |map_id| and |
172 // creates a new map for (|namespace_id|, |origin|). Copies the data from the | 165 // creates a new map for (|namespace_id|, |origin|). Copies the data from the |
173 // old map if |copy_data| is true. | 166 // old map if |copy_data| is true. |
174 bool DeepCopyArea(int64 namespace_id, | 167 bool DeepCopyArea(const std::string& namespace_id, |
175 const GURL& origin, | 168 const GURL& origin, |
176 bool copy_data, | 169 bool copy_data, |
177 std::string* map_id, | 170 std::string* map_id, |
178 leveldb::WriteBatch* batch); | 171 leveldb::WriteBatch* batch); |
179 | 172 |
180 // Helper functions for creating the keys needed for the schema. | 173 // Helper functions for creating the keys needed for the schema. |
181 static std::string NamespaceStartKey(const std::string& namespace_id_str); | 174 static std::string NamespaceStartKey(const std::string& namespace_id); |
182 static std::string NamespaceStartKey(int64 namespace_id, | 175 static std::string NamespaceKey(const std::string& namespace_id, |
183 int64 namespace_offset); | |
184 static std::string NamespaceKey(const std::string& namespace_id_str, | |
185 const std::string& origin); | 176 const std::string& origin); |
186 static std::string NamespaceKey(int64 namespace_id, | |
187 int64 namespace_offset, | |
188 const GURL& origin); | |
189 static std::string NamespaceIdStr(int64 namespace_id, int64 namespace_offset); | |
190 static const char* NamespacePrefix(); | 177 static const char* NamespacePrefix(); |
191 static std::string MapRefCountKey(const std::string& map_id); | 178 static std::string MapRefCountKey(const std::string& map_id); |
192 static std::string MapKey(const std::string& map_id, const std::string& key); | 179 static std::string MapKey(const std::string& map_id, const std::string& key); |
193 static const char* MapPrefix(); | |
194 static const char* NextNamespaceIdKey(); | |
195 static const char* NextMapIdKey(); | 180 static const char* NextMapIdKey(); |
196 | 181 |
197 scoped_ptr<leveldb::DB> db_; | 182 scoped_ptr<leveldb::DB> db_; |
198 FilePath file_path_; | 183 FilePath file_path_; |
199 | 184 |
200 // For protecting the database opening code. | 185 // For protecting the database opening code. |
201 base::Lock db_lock_; | 186 base::Lock db_lock_; |
202 | 187 |
203 // True if a database error has occurred (e.g., cannot read data). | 188 // True if a database error has occurred (e.g., cannot read data). |
204 bool db_error_; | 189 bool db_error_; |
205 // True if the database is in an inconsistent state. | 190 // True if the database is in an inconsistent state. |
206 bool is_inconsistent_; | 191 bool is_inconsistent_; |
207 | 192 |
208 // On startup, we read the next ununsed namespace id from the database. It | |
209 // will be the offset for namespace ids. The actual id of a namespace in the | |
210 // database will be: id passed to the API function + namespace_offset_. The | |
211 // namespace ids which are handled as int64 (named namespace_id) don't contain | |
212 // the offset yet. The namespaces ids which are handled as strings (named | |
213 // namesapce_id_str) contain the offset. | |
214 int64 namespace_offset_; | |
215 | |
216 DISALLOW_COPY_AND_ASSIGN(SessionStorageDatabase); | 193 DISALLOW_COPY_AND_ASSIGN(SessionStorageDatabase); |
217 }; | 194 }; |
218 | 195 |
219 } // namespace dom_storage | 196 } // namespace dom_storage |
220 | 197 |
221 #endif // WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ | 198 #endif // WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ |
OLD | NEW |