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

Unified Diff: webkit/dom_storage/session_storage_database.h

Issue 9963107: Persist sessionStorage on disk. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Draft: associate with session restore. 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 side-by-side diff with in-line comments
Download patch
Index: webkit/dom_storage/session_storage_database.h
diff --git a/webkit/dom_storage/session_storage_database.h b/webkit/dom_storage/session_storage_database.h
index 822dbc3d43668726047e2d67d07ea5172f5cd704..777bdfdb7144f83365ccc48538a8811033288237 100644
--- a/webkit/dom_storage/session_storage_database.h
+++ b/webkit/dom_storage/session_storage_database.h
@@ -9,6 +9,7 @@
#include <map>
#include <string>
+#include "base/callback.h"
#include "base/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
@@ -28,11 +29,24 @@ namespace dom_storage {
// SessionStorageDatabase holds the data from multiple namespaces and multiple
// origins. All DomStorageAreas for session storage share the same
// SessionStorageDatabase.
+
+// The namespace IDs used in the API (upper layer IDs) are different than the
+// namespace IDs used in the database (real IDs). All public functions deal with
+// upper layer IDs.
+
+// For private functions, the namespace IDs which are handled as int64 (named
+// namespace_id) are upper layer IDs. The namespaces IDs which are handled
+// as strings (named namesapce_id_str) are real IDs.
class SessionStorageDatabase :
public base::RefCountedThreadSafe<SessionStorageDatabase> {
public:
explicit SessionStorageDatabase(const FilePath& file_path);
+ typedef base::Callback<void(int64, int64)> SessionStorageAssociatedCallback;
+ SessionStorageDatabase(
+ const FilePath& file_path,
+ const SessionStorageAssociatedCallback& associated_callback);
+
// Reads the (key, value) pairs for |namespace_id| and |origin|. |result| is
// assumed to be empty and any duplicate keys will be overwritten. If the
// database exists on disk then it will be opened. If it does not exist then
@@ -62,6 +76,14 @@ class SessionStorageDatabase :
// Deletes the data for |namespace_id|.
bool DeleteNamespace(int64 namespace_id);
+ // Reads all namespace ids from the database. The returned IDs will be upper
+ // level IDs. If there are some databases without an associated upper layer
+ // ID, this creates new, negative upper layer IDs for them.
+ bool ReadNamespaceIds(std::vector<int64>* namespace_ids);
+
+ // Associates |namespace_id| with |real_id|. Used by session restore.
+ void AssociateNamespaceId(int64 namespace_id, int64 real_id);
+
private:
friend class base::RefCountedThreadSafe<SessionStorageDatabase>;
friend class SessionStorageDatabaseTest;
@@ -119,12 +141,12 @@ class SessionStorageDatabase :
leveldb::WriteBatch* batch);
// Helpers for deleting data for |namespace_id| and |origin|.
- bool DeleteArea(int64 namespace_id,
- const std::string& origin,
- leveldb::WriteBatch* batch);
- bool DeleteArea(const std::string& namespace_id_str,
- const std::string& origin,
- leveldb::WriteBatch* batch);
+ bool DeleteAreaHelper(int64 namespace_id,
+ const std::string& origin,
+ leveldb::WriteBatch* batch);
+ bool DeleteAreaHelper(const std::string& namespace_id_str,
+ const std::string& origin,
+ leveldb::WriteBatch* batch);
// Retrieves the map id for |namespace_id| and |origin|. It's not an error if
// the map doesn't exist.
@@ -177,16 +199,18 @@ class SessionStorageDatabase :
std::string* map_id,
leveldb::WriteBatch* batch);
+ // Helper functions for ID conversions.
+ bool AllocateNamespaceId(int64* namespace_id);
+ bool GetRealId(int64 namespace_id, int64* real_id);
+
// Helper functions for creating the keys needed for the schema.
static std::string NamespaceStartKey(const std::string& namespace_id_str);
- static std::string NamespaceStartKey(int64 namespace_id,
- int64 namespace_offset);
+ std::string NamespaceStartKey(int64 namespace_id);
static std::string NamespaceKey(const std::string& namespace_id_str,
const std::string& origin);
- static std::string NamespaceKey(int64 namespace_id,
- int64 namespace_offset,
- const GURL& origin);
- static std::string NamespaceIdStr(int64 namespace_id, int64 namespace_offset);
+ std::string NamespaceKey(int64 namespace_id,
+ const GURL& origin);
+ std::string NamespaceIdStr(int64 namespace_id);
static const char* NamespacePrefix();
static std::string MapRefCountKey(const std::string& map_id);
static std::string MapKey(const std::string& map_id, const std::string& key);
@@ -205,13 +229,19 @@ class SessionStorageDatabase :
// True if the database is in an inconsistent state.
bool is_inconsistent_;
- // On startup, we read the next ununsed namespace id from the database. It
- // will be the offset for namespace ids. The actual id of a namespace in the
- // database will be: id passed to the API function + namespace_offset_. The
- // namespace ids which are handled as int64 (named namespace_id) don't contain
- // the offset yet. The namespaces ids which are handled as strings (named
- // namesapce_id_str) contain the offset.
- int64 namespace_offset_;
+ // To track in-memory namespace id allocations (which don't necessarily end up
+ // in the db).
+ int64 next_namespace_id_;
+
+ // To track created upper layer namespace IDs.
+ int64 next_negative_namespace_id_;
+
+ // Track the correspondence between the upper layer IDs (used in the API) and
+ // the IDs in the database.
+ std::map<int64, int64> id_to_real_id_;
+ std::map<int64, int64> real_id_to_id_;
+
+ SessionStorageAssociatedCallback associated_callback_;
DISALLOW_COPY_AND_ASSIGN(SessionStorageDatabase);
};

Powered by Google App Engine
This is Rietveld 408576698