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

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: Code review. 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 <string>
11
12 #include "base/file_path.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/synchronization/lock.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 // Helpers for checking caller erros, invariants and database errors. All
77 // these return |ok|, for chaining.
78 bool CallerErrorCheck(bool ok) const;
79 bool ConsistencyCheck(bool ok);
80 bool DatabaseErrorCheck(bool ok);
81
82 // Helper functions. All return true if the operation succeeded, and false if
83 // it failed (a database error or a consistency error). If the return type is
84 // void, the operation cannot fail. If they return false, ConsistencyCheck or
85 // DatabaseErrorCheck have already been called.
86
87 // Creates a namespace for |namespace_id| and updates the next namespace id if
88 // needed. If |ok_if_exists| is false, checks that the namespace didn't exist
89 // before.
90 bool CreateNamespace(int64 namespace_id,
91 bool ok_if_exists,
92 leveldb::WriteBatch* batch);
93 // Reads the next namespace id.
94 bool GetNextNamespaceId(int64* next_namespace_id);
95 bool UpdateNextNamespaceId(int64 namespace_id,
96 leveldb::WriteBatch* batch);
97 // Reads the areas assoiated with |namespace_id| and puts the (origin, map_id)
98 // pairs into |areas|.
99 bool GetAreasInNamespace(int64 namespace_id,
100 std::map<std::string, std::string>* areas);
101 bool GetAreasInNamespace(const std::string& namespace_id_str,
102 std::map<std::string, std::string>* areas);
103
104 // Adds an association between |origin| and |map_id| into the namespace
105 // |namespace_id|.
106 void AddAreaToNamespace(int64 namespace_id,
107 const std::string& origin,
108 const std::string& map_id,
109 leveldb::WriteBatch* batch);
110
111 // Helpers for deleting data for |namespace_id| and |origin|.
112 bool DeleteArea(int64 namespace_id,
113 const std::string& origin,
114 leveldb::WriteBatch* batch);
115 bool DeleteArea(const std::string& namespace_id_str,
116 const std::string& origin,
117 leveldb::WriteBatch* batch);
118
119 // Retrieves the map id for |namespace_id| and |origin|. It's not an error if
120 // the map doesn't exist.
121 bool GetMapForArea(int64 namespace_id,
122 const GURL& origin,
123 bool* exists,
124 std::string* map_id);
125 bool GetMapForArea(const std::string& namespace_id_str,
126 const std::string& origin,
127 bool* exists,
128 std::string* map_id);
129
130 // Creates a new map for |namespace_id| and |origin|. |map_id| will hold the
131 // id of the created map. If there is a map for |namespace_id| and |origin|,
132 // this just overwrites the map id. The caller is responsible for decreasing
133 // the ref count.
134 bool CreateMapForArea(int64 namespace_id,
135 const GURL& origin,
136 std::string* map_id,
137 leveldb::WriteBatch* batch);
138 // Reads the contents of the map |map_id| into |result|. If |only_keys| is
139 // true, only keys are aread from the database and the values in |result| will
140 // be empty.
141 bool ReadMap(const std::string& map_id,
142 ValuesMap* result,
143 bool only_keys);
144 // Writes |values| into the map |map_id|.
145 void WriteValuesToMap(const std::string& map_id,
146 const ValuesMap& values,
147 leveldb::WriteBatch* batch);
148
149 bool GetMapRefCount(const std::string& map_id, int64* ref_count);
150 bool IncreaseMapRefCount(const std::string& map_id,
151 leveldb::WriteBatch* batch);
152 // Decreases the ref count of a map by |decrease|. If the ref count goes to 0,
153 // deletes the map.
154 bool DecreaseMapRefCount(const std::string& map_id,
155 int decrease,
156 leveldb::WriteBatch* batch);
157
158 // Deletes all values in |map_id|.
159 bool ClearMap(const std::string& map_id, leveldb::WriteBatch* batch);
160
161 // Helper functions for creating the keys needed for the schema.
162 static std::string NamespaceStartKey(const std::string& namespace_id_str);
163 static std::string NamespaceStartKey(int64 namespace_id,
164 int64 namespace_offset);
165 static std::string NamespaceKey(const std::string& namespace_id_str,
166 const std::string& origin);
167 static std::string NamespaceKey(int64 namespace_id,
168 int64 namespace_offset,
169 const GURL& origin);
170 static std::string NamespaceIdStr(int64 namespace_id, int64 namespace_offset);
171 static const char* NamespacePrefix();
172 static std::string MapRefCountKey(const std::string& map_id);
173 static std::string MapKey(const std::string& map_id, const std::string& key);
174 static const char* MapPrefix();
175 static const char* NextNamespaceIdKey();
176 static const char* NextMapIdKey();
177
178 scoped_ptr<leveldb::DB> db_;
179 FilePath file_path_;
180
181 // For protecting the database opening code.
182 base::Lock db_lock_;
183
184 // True if a database error has occurred (e.g., cannot read data).
185 bool db_error_;
186 // True if the database is in an inconsistent state.
187 bool is_inconsistent_;
188
189 // On startup, we read the next ununsed namespace id from the database. It
190 // will be the offset for namespace ids. The actual id of a namespace in the
191 // database will be: id passed to the API function + namespace_offset_. The
192 // namespace ids which are handled as int64 (named namespace_id) don't contain
193 // the offset yet. The namespaces ids which are handled as strings (named
194 // namesapce_id_str) contain the offset.
195 int64 namespace_offset_;
196
197 DISALLOW_COPY_AND_ASSIGN(SessionStorageDatabase);
198 };
199
200 } // namespace dom_storage
201
202 #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