| 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. |
| 32 |
| 33 // The namespace IDs used in the API (upper layer IDs) are different than the |
| 34 // namespace IDs used in the database (real IDs). All public functions deal with |
| 35 // upper layer IDs. |
| 36 |
| 37 // For private functions, the namespace IDs which are handled as int64 (named |
| 38 // namespace_id) are upper layer IDs. The namespaces IDs which are handled |
| 39 // as strings (named namesapce_id_str) are real IDs. |
| 31 class SessionStorageDatabase : | 40 class SessionStorageDatabase : |
| 32 public base::RefCountedThreadSafe<SessionStorageDatabase> { | 41 public base::RefCountedThreadSafe<SessionStorageDatabase> { |
| 33 public: | 42 public: |
| 34 explicit SessionStorageDatabase(const FilePath& file_path); | 43 explicit SessionStorageDatabase(const FilePath& file_path); |
| 35 | 44 |
| 45 typedef base::Callback<void(int64, int64)> SessionStorageAssociatedCallback; |
| 46 SessionStorageDatabase( |
| 47 const FilePath& file_path, |
| 48 const SessionStorageAssociatedCallback& associated_callback); |
| 49 |
| 36 // Reads the (key, value) pairs for |namespace_id| and |origin|. |result| is | 50 // 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 | 51 // 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 | 52 // 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. | 53 // it will not be created and |result| will be unmodified. |
| 40 void ReadAreaValues(int64 namespace_id, | 54 void ReadAreaValues(int64 namespace_id, |
| 41 const GURL& origin, | 55 const GURL& origin, |
| 42 ValuesMap* result); | 56 ValuesMap* result); |
| 43 | 57 |
| 44 // Updates the data for |namespace_id| and |origin|. Will remove all keys | 58 // 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 | 59 // 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 | 60 // 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 | 61 // 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 | 62 // 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. | 63 // that case the copy will be made deep before writing the values. |
| 50 bool CommitAreaChanges(int64 namespace_id, | 64 bool CommitAreaChanges(int64 namespace_id, |
| 51 const GURL& origin, | 65 const GURL& origin, |
| 52 bool clear_all_first, | 66 bool clear_all_first, |
| 53 const ValuesMap& changes); | 67 const ValuesMap& changes); |
| 54 | 68 |
| 55 // Creates shallow copies of the areas for |namespace_id| and associates them | 69 // Creates shallow copies of the areas for |namespace_id| and associates them |
| 56 // with |new_namespace_id|. | 70 // with |new_namespace_id|. |
| 57 bool CloneNamespace(int64 namespace_id, int64 new_namespace_id); | 71 bool CloneNamespace(int64 namespace_id, int64 new_namespace_id); |
| 58 | 72 |
| 59 // Deletes the data for |namespace_id| and |origin|. | 73 // Deletes the data for |namespace_id| and |origin|. |
| 60 bool DeleteArea(int64 namespace_id, const GURL& origin); | 74 bool DeleteArea(int64 namespace_id, const GURL& origin); |
| 61 | 75 |
| 62 // Deletes the data for |namespace_id|. | 76 // Deletes the data for |namespace_id|. |
| 63 bool DeleteNamespace(int64 namespace_id); | 77 bool DeleteNamespace(int64 namespace_id); |
| 64 | 78 |
| 79 // Reads all namespace ids from the database. The returned IDs will be upper |
| 80 // level IDs. If there are some databases without an associated upper layer |
| 81 // ID, this creates new, negative upper layer IDs for them. |
| 82 bool ReadNamespaceIds(std::vector<int64>* namespace_ids); |
| 83 |
| 84 // Associates |namespace_id| with |real_id|. Used by session restore. |
| 85 void AssociateNamespaceId(int64 namespace_id, int64 real_id); |
| 86 |
| 65 private: | 87 private: |
| 66 friend class base::RefCountedThreadSafe<SessionStorageDatabase>; | 88 friend class base::RefCountedThreadSafe<SessionStorageDatabase>; |
| 67 friend class SessionStorageDatabaseTest; | 89 friend class SessionStorageDatabaseTest; |
| 68 | 90 |
| 69 ~SessionStorageDatabase(); | 91 ~SessionStorageDatabase(); |
| 70 | 92 |
| 71 // Opens the database at file_path_ if it exists already and creates it if | 93 // 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 | 94 // |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 | 95 // if the opening failed or was not necessary (the database doesn't exist and |
| 74 // |create_if_needed| is false). The possible failures are: | 96 // |create_if_needed| is false). The possible failures are: |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 std::map<std::string, std::string>* areas); | 134 std::map<std::string, std::string>* areas); |
| 113 | 135 |
| 114 // Adds an association between |origin| and |map_id| into the namespace | 136 // Adds an association between |origin| and |map_id| into the namespace |
| 115 // |namespace_id|. | 137 // |namespace_id|. |
| 116 void AddAreaToNamespace(int64 namespace_id, | 138 void AddAreaToNamespace(int64 namespace_id, |
| 117 const std::string& origin, | 139 const std::string& origin, |
| 118 const std::string& map_id, | 140 const std::string& map_id, |
| 119 leveldb::WriteBatch* batch); | 141 leveldb::WriteBatch* batch); |
| 120 | 142 |
| 121 // Helpers for deleting data for |namespace_id| and |origin|. | 143 // Helpers for deleting data for |namespace_id| and |origin|. |
| 122 bool DeleteArea(int64 namespace_id, | 144 bool DeleteAreaHelper(int64 namespace_id, |
| 123 const std::string& origin, | 145 const std::string& origin, |
| 124 leveldb::WriteBatch* batch); | 146 leveldb::WriteBatch* batch); |
| 125 bool DeleteArea(const std::string& namespace_id_str, | 147 bool DeleteAreaHelper(const std::string& namespace_id_str, |
| 126 const std::string& origin, | 148 const std::string& origin, |
| 127 leveldb::WriteBatch* batch); | 149 leveldb::WriteBatch* batch); |
| 128 | 150 |
| 129 // Retrieves the map id for |namespace_id| and |origin|. It's not an error if | 151 // Retrieves the map id for |namespace_id| and |origin|. It's not an error if |
| 130 // the map doesn't exist. | 152 // the map doesn't exist. |
| 131 bool GetMapForArea(int64 namespace_id, | 153 bool GetMapForArea(int64 namespace_id, |
| 132 const GURL& origin, | 154 const GURL& origin, |
| 133 bool* exists, | 155 bool* exists, |
| 134 std::string* map_id); | 156 std::string* map_id); |
| 135 bool GetMapForArea(const std::string& namespace_id_str, | 157 bool GetMapForArea(const std::string& namespace_id_str, |
| 136 const std::string& origin, | 158 const std::string& origin, |
| 137 bool* exists, | 159 bool* exists, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 | 192 |
| 171 // Breaks the association between (|namespace_id|, |origin|) and |map_id| and | 193 // 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 | 194 // creates a new map for (|namespace_id|, |origin|). Copies the data from the |
| 173 // old map if |copy_data| is true. | 195 // old map if |copy_data| is true. |
| 174 bool DeepCopyArea(int64 namespace_id, | 196 bool DeepCopyArea(int64 namespace_id, |
| 175 const GURL& origin, | 197 const GURL& origin, |
| 176 bool copy_data, | 198 bool copy_data, |
| 177 std::string* map_id, | 199 std::string* map_id, |
| 178 leveldb::WriteBatch* batch); | 200 leveldb::WriteBatch* batch); |
| 179 | 201 |
| 202 // Helper functions for ID conversions. |
| 203 bool AllocateNamespaceId(int64* namespace_id); |
| 204 bool GetRealId(int64 namespace_id, int64* real_id); |
| 205 |
| 180 // Helper functions for creating the keys needed for the schema. | 206 // Helper functions for creating the keys needed for the schema. |
| 181 static std::string NamespaceStartKey(const std::string& namespace_id_str); | 207 static std::string NamespaceStartKey(const std::string& namespace_id_str); |
| 182 static std::string NamespaceStartKey(int64 namespace_id, | 208 std::string NamespaceStartKey(int64 namespace_id); |
| 183 int64 namespace_offset); | |
| 184 static std::string NamespaceKey(const std::string& namespace_id_str, | 209 static std::string NamespaceKey(const std::string& namespace_id_str, |
| 185 const std::string& origin); | 210 const std::string& origin); |
| 186 static std::string NamespaceKey(int64 namespace_id, | 211 std::string NamespaceKey(int64 namespace_id, |
| 187 int64 namespace_offset, | 212 const GURL& origin); |
| 188 const GURL& origin); | 213 std::string NamespaceIdStr(int64 namespace_id); |
| 189 static std::string NamespaceIdStr(int64 namespace_id, int64 namespace_offset); | |
| 190 static const char* NamespacePrefix(); | 214 static const char* NamespacePrefix(); |
| 191 static std::string MapRefCountKey(const std::string& map_id); | 215 static std::string MapRefCountKey(const std::string& map_id); |
| 192 static std::string MapKey(const std::string& map_id, const std::string& key); | 216 static std::string MapKey(const std::string& map_id, const std::string& key); |
| 193 static const char* MapPrefix(); | 217 static const char* MapPrefix(); |
| 194 static const char* NextNamespaceIdKey(); | 218 static const char* NextNamespaceIdKey(); |
| 195 static const char* NextMapIdKey(); | 219 static const char* NextMapIdKey(); |
| 196 | 220 |
| 197 scoped_ptr<leveldb::DB> db_; | 221 scoped_ptr<leveldb::DB> db_; |
| 198 FilePath file_path_; | 222 FilePath file_path_; |
| 199 | 223 |
| 200 // For protecting the database opening code. | 224 // For protecting the database opening code. |
| 201 base::Lock db_lock_; | 225 base::Lock db_lock_; |
| 202 | 226 |
| 203 // True if a database error has occurred (e.g., cannot read data). | 227 // True if a database error has occurred (e.g., cannot read data). |
| 204 bool db_error_; | 228 bool db_error_; |
| 205 // True if the database is in an inconsistent state. | 229 // True if the database is in an inconsistent state. |
| 206 bool is_inconsistent_; | 230 bool is_inconsistent_; |
| 207 | 231 |
| 208 // On startup, we read the next ununsed namespace id from the database. It | 232 // To track in-memory namespace id allocations (which don't necessarily end up |
| 209 // will be the offset for namespace ids. The actual id of a namespace in the | 233 // in the db). |
| 210 // database will be: id passed to the API function + namespace_offset_. The | 234 int64 next_namespace_id_; |
| 211 // namespace ids which are handled as int64 (named namespace_id) don't contain | 235 |
| 212 // the offset yet. The namespaces ids which are handled as strings (named | 236 // To track created upper layer namespace IDs. |
| 213 // namesapce_id_str) contain the offset. | 237 int64 next_negative_namespace_id_; |
| 214 int64 namespace_offset_; | 238 |
| 239 // Track the correspondence between the upper layer IDs (used in the API) and |
| 240 // the IDs in the database. |
| 241 std::map<int64, int64> id_to_real_id_; |
| 242 std::map<int64, int64> real_id_to_id_; |
| 243 |
| 244 SessionStorageAssociatedCallback associated_callback_; |
| 215 | 245 |
| 216 DISALLOW_COPY_AND_ASSIGN(SessionStorageDatabase); | 246 DISALLOW_COPY_AND_ASSIGN(SessionStorageDatabase); |
| 217 }; | 247 }; |
| 218 | 248 |
| 219 } // namespace dom_storage | 249 } // namespace dom_storage |
| 220 | 250 |
| 221 #endif // WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ | 251 #endif // WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ |
| OLD | NEW |