OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ | |
6 #define WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ | |
7 #pragma once | |
8 | |
9 #include "base/file_path.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "third_party/leveldatabase/src/include/leveldb/status.h" | |
13 #include "webkit/dom_storage/dom_storage_types.h" | |
14 | |
15 class GURL; | |
16 | |
17 namespace leveldb { | |
18 | |
19 class DB; | |
20 class WriteBatch; | |
21 | |
22 } // namespace leveldb | |
michaeln
2012/04/25 08:32:12
style nit: generally we forward declare more in a
marja
2012/04/25 15:44:27
Done.
| |
23 | |
24 namespace dom_storage { | |
25 | |
26 // SessionStorageDatabase holds the data from multiple namespaces and multiple | |
27 // origins. All DomStorageAreas for session storage share the same | |
28 // SessionStorageDatabase. | |
29 class SessionStorageDatabase : | |
30 public base::RefCountedThreadSafe<SessionStorageDatabase> { | |
31 public: | |
32 explicit SessionStorageDatabase(const FilePath& file_path); | |
33 | |
34 // Reads all the key, value pairs stored in the database and returns | |
35 // them. |result| is assumed to be empty and any duplicate keys will | |
36 // be overwritten. If the database exists on disk then it will be | |
37 // opened. If it does not exist then it will not be created and | |
38 // |result| will be unmodified. | |
39 void ReadAllValues(int64 namespace_id, | |
michaeln
2012/04/25 08:32:12
Since this class works with multiple object types
marja
2012/04/25 15:44:27
Done.
| |
40 const GURL& origin, | |
41 ValuesMap* result); | |
42 | |
43 // Updates the backing database. Will remove all keys before updating | |
44 // the database if |clear_all_first| is set. Then all entries in | |
45 // |changes| will be examined - keys mapped to a null NullableString16 | |
46 // will be removed and all others will be inserted/updated as appropriate. | |
47 bool CommitChanges(int64 namespace_id, | |
48 const GURL& origin, | |
49 bool clear_all_first, | |
50 const ValuesMap& changes); | |
51 | |
52 // Creates shallow copies of the maps for |namespace_id| and associates them | |
53 // with |new_namespace_id|. | |
54 bool ShallowCopyNamespace(int64 namespace_id, int64 new_namespace_id); | |
michaeln
2012/04/25 08:32:12
consider calling this CloneNamespace to be more co
marja
2012/04/25 15:44:27
Done.
| |
55 | |
56 // Creates a deep copy of the map for the data associated with |origin| in | |
57 // |namespace_id|. | |
58 bool DeepCopy(int64 namespace_id, const GURL& origin); | |
59 | |
60 // Breaks the association of (|namespace_id|, |origin|) and its map. | |
61 bool DisassociateMap(int64 namespace_id, const GURL& origin); | |
62 | |
63 // Deletes the data for |namespace_id| and |origin|. | |
64 bool DeleteOrigin(int64 namespace_id, const GURL& origin); | |
65 | |
66 // Deletes the data for |namespace_id|. | |
67 bool DeleteNamespace(int64 namespace_id); | |
68 | |
69 // Deletes possible leftover data from the previous run. | |
70 bool DeleteLeftoverData(); | |
michaeln
2012/04/25 08:32:12
Feels like it might be premature to add this metho
marja
2012/04/25 15:44:27
Done (deleted it).
| |
71 | |
72 private: | |
73 friend class base::RefCountedThreadSafe<SessionStorageDatabase>; | |
74 friend class SessionStorageDatabaseTest; | |
75 | |
76 ~SessionStorageDatabase(); | |
77 | |
78 bool LazyOpen(bool create_if_needed); | |
79 leveldb::Status TryToOpen(const FilePath& file_path, leveldb::DB** db); | |
80 bool IsOpen() const; | |
81 | |
82 // For checking consistency invariants. Returns |ok|, for chaining. | |
83 bool ConsistencyCheck(bool ok); | |
84 // For checking database errors. If a database error occurs, the data is | |
85 // deleted and the database is not accessed during this run. Returns |ok|, for | |
86 // chaining. | |
87 bool DatabaseErrorCheck(bool ok); | |
88 | |
89 // Helper for reading the contents of the map |map_id| into |result|. If | |
90 // |only_keys| is true, only keys are aread from the database and the values | |
91 // in |result| will be empty. | |
92 bool ReadMap(const std::string& map_id, ValuesMap* result, bool only_keys); | |
93 | |
94 // Helper for creating a new map for |area_key|. |map_id| will hold the id of | |
95 // the created map. Retuns true if the creation succeeded, false otherwise. | |
96 bool CreateNewMap(const std::string& area_key, | |
97 leveldb::WriteBatch* batch, | |
98 std::string* map_id); | |
99 | |
100 // Helper for writing values into a map. | |
101 void WriteValuesToMap(const std::string& map_id, | |
102 const ValuesMap& values, | |
103 leveldb::WriteBatch* batch); | |
104 | |
105 // Helper for reading the next namespace id. | |
106 bool GetNextNamespaceId(int64* next_namespace_id); | |
107 | |
108 // Helper for keeping the next namespace id up to date when adding new | |
109 // namespaces. | |
110 bool UpdateNextNamespaceId(int64 namespace_id, leveldb::WriteBatch* batch); | |
111 | |
112 // Helper for reading the ref count of a map. | |
113 bool GetRefCount(const std::string& map_key, int64* ref_count); | |
114 | |
115 // Helper for decreasing the ref count of a map. | |
116 bool DecreaseRefCount(const std::string& map_id, | |
117 int decrease, | |
118 leveldb::WriteBatch* batch); | |
119 | |
120 // Helper for deleting all values in a map. | |
121 bool ClearMap(const std::string& map_id, leveldb::WriteBatch* batch); | |
122 | |
123 // Helper for deleting data for |namespace_id| and |origin|. | |
124 bool DeleteOrigin(const std::string& namespace_id, | |
125 const std::string& origin, | |
126 leveldb::WriteBatch* batch); | |
127 | |
128 // Helper functions for creating the keys needed for the schema. | |
129 static std::string NamespaceStartKey(const std::string& namespace_id); | |
130 static std::string NamespaceStartKey(int64 namespace_id, | |
131 int64 namespace_offset); | |
132 static std::string NamespaceKey(const std::string& namespace_id, | |
133 const std::string& origin); | |
134 static std::string NamespaceKey(int64 namespace_id, | |
135 int64 namespace_offset, | |
136 const GURL& origin); | |
137 static std::string NamespacePrefix(); | |
138 static std::string MapRefCountKey(const std::string& map_id); | |
139 static std::string MapKey(const std::string& map_id, const std::string& key); | |
140 static std::string MapPrefix(); | |
141 static std::string NextNamespaceIdKey(); | |
142 static std::string NextMapIdKey(); | |
143 | |
144 scoped_ptr<leveldb::DB> db_; | |
145 FilePath file_path_; | |
146 | |
147 // True if a database error has occurred (e.g., cannot read data). | |
148 bool db_error_; | |
149 // True if the database is in an inconsistent state. | |
150 bool is_inconsistent_; | |
151 | |
152 // On startup, we read the next ununsed namespace id from the database. It | |
153 // will be the offset for namespace ids. The actual id of a namespace in the | |
154 // database will be: id passed to the API function + namespace_offset_. | |
155 int64 namespace_offset_; | |
156 | |
157 DISALLOW_COPY_AND_ASSIGN(SessionStorageDatabase); | |
158 }; | |
159 | |
160 } // namespace dom_storage | |
161 | |
162 #endif // WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ | |
OLD | NEW |