Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: webkit/dom_storage/session_storage_database.h

Issue 10176005: Add dom_storage::SessionStorageDatabase. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 <map>
10 #include <set>
michaeln 2012/04/26 05:07:43 is <set> needed here or maybe just in the .cc file
marja 2012/04/27 07:34:47 Done. (Needed nowhere.)
11 #include <string>
12
13 #include "base/file_path.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "third_party/leveldatabase/src/include/leveldb/status.h"
17 #include "webkit/dom_storage/dom_storage_types.h"
18
19 class GURL;
20
21 namespace leveldb {
22 class DB;
23 class WriteBatch;
24 } // namespace leveldb
25
26 namespace dom_storage {
27
28 // SessionStorageDatabase holds the data from multiple namespaces and multiple
29 // origins. All DomStorageAreas for session storage share the same
30 // SessionStorageDatabase.
31 class SessionStorageDatabase :
32 public base::RefCountedThreadSafe<SessionStorageDatabase> {
33 public:
34 explicit SessionStorageDatabase(const FilePath& file_path);
35
36 // 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 // 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 void ReadAreaValues(int64 namespace_id,
41 const GURL& origin,
42 ValuesMap* result);
43
44 // 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 // in |changes| will be examined - keys mapped to a null NullableString16 will
47 // be removed and all others will be inserted/updated as appropriate.
48 bool CommitAreaChanges(int64 namespace_id,
49 const GURL& origin,
50 bool clear_all_first,
51 const ValuesMap& changes);
52
53 // Creates shallow copies of the areas for |namespace_id| and associates them
54 // with |new_namespace_id|.
55 bool CloneNamespace(int64 namespace_id, int64 new_namespace_id);
56
57 // Creates a deep copy of the area for |namespace_id| and |origin|.
58 bool DeepCopyArea(int64 namespace_id, const GURL& origin);
59
60 // Deletes the data for |namespace_id| and |origin|.
61 bool DeleteArea(int64 namespace_id, const GURL& origin);
62
63 // Deletes the data for |namespace_id|.
64 bool DeleteNamespace(int64 namespace_id);
65
66 private:
67 friend class base::RefCountedThreadSafe<SessionStorageDatabase>;
68 friend class SessionStorageDatabaseTest;
69
70 ~SessionStorageDatabase();
71
72 bool LazyOpen(bool create_if_needed);
73 leveldb::Status TryToOpen(const FilePath& file_path, leveldb::DB** db);
74 bool IsOpen() const;
75
76 // For checking consistency invariants. Returns |ok|, for chaining.
77 bool ConsistencyCheck(bool ok);
78 // For checking database errors. If a database error occurs, the data is
79 // deleted and the database is not accessed during this run. Returns |ok|, for
80 // chaining.
81 bool DatabaseErrorCheck(bool ok);
82
83 // Helper functions. All return true if the operation succeeded, and false if
84 // it failed (a database error or a consistency error). If the return type is
85 // void, the operation cannot fail. If they return false, ConsistencyCheck or
86 // DatabaseErrorCheck have already been called.
87
88 // Creates a namespace for |namespace_id| and updates the next namespace id if
89 // needed. If |ok_if_exists| is false, checks that the namespace didn't exist
90 // before.
91 bool CreateNamespace(int64 namespace_id,
92 bool ok_if_exists,
93 leveldb::WriteBatch* batch);
94 // Reads the next namespace id.
95 bool GetNextNamespaceId(int64* next_namespace_id);
96 bool UpdateNextNamespaceId(int64 namespace_id,
97 leveldb::WriteBatch* batch);
98 // Reads the areas assoiated with |namespace_id| and puts the (origin, map_id)
99 // pairs into |areas|.
100 bool GetAreasInNamespace(int64 namespace_id,
101 std::map<std::string, std::string>* areas);
102 bool GetAreasInNamespace(const std::string& namespace_id_str,
103 std::map<std::string, std::string>* areas);
104
105 // Adds an association between |origin| and |map_id| into the namespace
106 // |namespace_id|.
107 void AddAreaToNamespace(int64 namespace_id,
108 const std::string& origin,
109 const std::string& map_id,
110 leveldb::WriteBatch* batch);
111
112 // Helpers for deleting data for |namespace_id| and |origin|.
113 bool DeleteArea(int64 namespace_id,
114 const std::string& origin,
115 leveldb::WriteBatch* batch);
116 bool DeleteArea(const std::string& namespace_id_str,
117 const std::string& origin,
118 leveldb::WriteBatch* batch);
119
120 // Retrieves the map id for |namespace_id| and |origin|. It's not an error if
121 // the map doesn't exist.
122 bool GetMapForArea(int64 namespace_id,
123 const GURL& origin,
124 bool* exists,
125 std::string* map_id);
126 bool GetMapForArea(const std::string& namespace_id_str,
127 const std::string& origin,
128 bool* exists,
129 std::string* map_id);
130
131 // Creates a new map for |namespace_id| and |origin|. |map_id| will hold the
132 // id of the created map. If there is a map for |namespace_id| and |origin|,
133 // this just overwrites the map id. The caller is responsible for decreasing
134 // the ref count.
135 bool CreateMapForArea(int64 namespace_id,
136 const GURL& origin,
137 std::string* map_id,
138 leveldb::WriteBatch* batch);
139 // Reads the contents of the map |map_id| into |result|. If |only_keys| is
140 // true, only keys are aread from the database and the values in |result| will
141 // be empty.
142 bool ReadMap(const std::string& map_id,
143 ValuesMap* result,
144 bool only_keys);
145 // Writes |values| into the map |map_id|.
146 void WriteValuesToMap(const std::string& map_id,
147 const ValuesMap& values,
148 leveldb::WriteBatch* batch);
149
150 bool GetMapRefCount(const std::string& map_id, int64* ref_count);
151 bool IncreaseMapRefCount(const std::string& map_id,
152 leveldb::WriteBatch* batch);
153 // Decreases the ref count of a map by |decrease|. If the ref count goes to 0,
154 // deletes the map.
155 bool DecreaseMapRefCount(const std::string& map_id,
156 int decrease,
157 leveldb::WriteBatch* batch);
158
159 // Deletes all values in |map_id|.
160 bool ClearMap(const std::string& map_id, leveldb::WriteBatch* batch);
161
162 // Helper functions for creating the keys needed for the schema.
163 static std::string NamespaceStartKey(const std::string& namespace_id_str);
164 static std::string NamespaceStartKey(int64 namespace_id,
165 int64 namespace_offset);
166 static std::string NamespaceKey(const std::string& namespace_id_str,
167 const std::string& origin);
168 static std::string NamespaceKey(int64 namespace_id,
169 int64 namespace_offset,
170 const GURL& origin);
171 static std::string NamespaceIdStr(int64 namespace_id, int64 namespace_offset);
172 static std::string NamespacePrefix();
173 static std::string MapRefCountKey(const std::string& map_id);
174 static std::string MapKey(const std::string& map_id, const std::string& key);
175 static std::string MapPrefix();
176 static std::string NextNamespaceIdKey();
177 static std::string NextMapIdKey();
178
179 scoped_ptr<leveldb::DB> db_;
180 FilePath file_path_;
181
182 // True if a database error has occurred (e.g., cannot read data).
183 bool db_error_;
184 // True if the database is in an inconsistent state.
185 bool is_inconsistent_;
186
187 // On startup, we read the next ununsed namespace id from the database. It
188 // will be the offset for namespace ids. The actual id of a namespace in the
189 // database will be: id passed to the API function + namespace_offset_. The
190 // namespace ids which are handled as int64 (named namespace_id) don't contain
191 // the offset yet. Tthe namespaces ids which are handled as strings (named
michaeln 2012/04/26 05:07:43 type: Tthe
marja 2012/04/27 07:34:47 Done.
192 // namesapce_id_str) contain the offset.
193 int64 namespace_offset_;
194
195 DISALLOW_COPY_AND_ASSIGN(SessionStorageDatabase);
196 };
197
198 } // namespace dom_storage
199
200 #endif // WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_
OLDNEW
« no previous file with comments | « no previous file | webkit/dom_storage/session_storage_database.cc » ('j') | webkit/dom_storage/session_storage_database.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698