Chromium Code Reviews| 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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c2e05c89f88d7efaeea13444338b8192c03659a2 |
| --- /dev/null |
| +++ b/webkit/dom_storage/session_storage_database.h |
| @@ -0,0 +1,162 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ |
| +#define WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ |
| +#pragma once |
| + |
| +#include "base/file_path.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "third_party/leveldatabase/src/include/leveldb/status.h" |
| +#include "webkit/dom_storage/dom_storage_types.h" |
| + |
| +class GURL; |
| + |
| +namespace leveldb { |
| + |
| +class DB; |
| +class WriteBatch; |
| + |
| +} // 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.
|
| + |
| +namespace dom_storage { |
| + |
| +// SessionStorageDatabase holds the data from multiple namespaces and multiple |
| +// origins. All DomStorageAreas for session storage share the same |
| +// SessionStorageDatabase. |
| +class SessionStorageDatabase : |
| + public base::RefCountedThreadSafe<SessionStorageDatabase> { |
| + public: |
| + explicit SessionStorageDatabase(const FilePath& file_path); |
| + |
| + // Reads all the key, value pairs stored in the database and returns |
| + // them. |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 it will not be created and |
| + // |result| will be unmodified. |
| + 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.
|
| + const GURL& origin, |
| + ValuesMap* result); |
| + |
| + // Updates the backing database. Will remove all keys before updating |
| + // the database if |clear_all_first| is set. Then all entries in |
| + // |changes| will be examined - keys mapped to a null NullableString16 |
| + // will be removed and all others will be inserted/updated as appropriate. |
| + bool CommitChanges(int64 namespace_id, |
| + const GURL& origin, |
| + bool clear_all_first, |
| + const ValuesMap& changes); |
| + |
| + // Creates shallow copies of the maps for |namespace_id| and associates them |
| + // with |new_namespace_id|. |
| + 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.
|
| + |
| + // Creates a deep copy of the map for the data associated with |origin| in |
| + // |namespace_id|. |
| + bool DeepCopy(int64 namespace_id, const GURL& origin); |
| + |
| + // Breaks the association of (|namespace_id|, |origin|) and its map. |
| + bool DisassociateMap(int64 namespace_id, const GURL& origin); |
| + |
| + // Deletes the data for |namespace_id| and |origin|. |
| + bool DeleteOrigin(int64 namespace_id, const GURL& origin); |
| + |
| + // Deletes the data for |namespace_id|. |
| + bool DeleteNamespace(int64 namespace_id); |
| + |
| + // Deletes possible leftover data from the previous run. |
| + 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).
|
| + |
| + private: |
| + friend class base::RefCountedThreadSafe<SessionStorageDatabase>; |
| + friend class SessionStorageDatabaseTest; |
| + |
| + ~SessionStorageDatabase(); |
| + |
| + bool LazyOpen(bool create_if_needed); |
| + leveldb::Status TryToOpen(const FilePath& file_path, leveldb::DB** db); |
| + bool IsOpen() const; |
| + |
| + // For checking consistency invariants. Returns |ok|, for chaining. |
| + bool ConsistencyCheck(bool ok); |
| + // For checking database errors. If a database error occurs, the data is |
| + // deleted and the database is not accessed during this run. Returns |ok|, for |
| + // chaining. |
| + bool DatabaseErrorCheck(bool ok); |
| + |
| + // Helper for reading the contents of the map |map_id| into |result|. If |
| + // |only_keys| is true, only keys are aread from the database and the values |
| + // in |result| will be empty. |
| + bool ReadMap(const std::string& map_id, ValuesMap* result, bool only_keys); |
| + |
| + // Helper for creating a new map for |area_key|. |map_id| will hold the id of |
| + // the created map. Retuns true if the creation succeeded, false otherwise. |
| + bool CreateNewMap(const std::string& area_key, |
| + leveldb::WriteBatch* batch, |
| + std::string* map_id); |
| + |
| + // Helper for writing values into a map. |
| + void WriteValuesToMap(const std::string& map_id, |
| + const ValuesMap& values, |
| + leveldb::WriteBatch* batch); |
| + |
| + // Helper for reading the next namespace id. |
| + bool GetNextNamespaceId(int64* next_namespace_id); |
| + |
| + // Helper for keeping the next namespace id up to date when adding new |
| + // namespaces. |
| + bool UpdateNextNamespaceId(int64 namespace_id, leveldb::WriteBatch* batch); |
| + |
| + // Helper for reading the ref count of a map. |
| + bool GetRefCount(const std::string& map_key, int64* ref_count); |
| + |
| + // Helper for decreasing the ref count of a map. |
| + bool DecreaseRefCount(const std::string& map_id, |
| + int decrease, |
| + leveldb::WriteBatch* batch); |
| + |
| + // Helper for deleting all values in a map. |
| + bool ClearMap(const std::string& map_id, leveldb::WriteBatch* batch); |
| + |
| + // Helper for deleting data for |namespace_id| and |origin|. |
| + bool DeleteOrigin(const std::string& namespace_id, |
| + const std::string& origin, |
| + leveldb::WriteBatch* batch); |
| + |
| + // Helper functions for creating the keys needed for the schema. |
| + static std::string NamespaceStartKey(const std::string& namespace_id); |
| + static std::string NamespaceStartKey(int64 namespace_id, |
| + int64 namespace_offset); |
| + static std::string NamespaceKey(const std::string& namespace_id, |
| + const std::string& origin); |
| + static std::string NamespaceKey(int64 namespace_id, |
| + int64 namespace_offset, |
| + const GURL& origin); |
| + static std::string NamespacePrefix(); |
| + static std::string MapRefCountKey(const std::string& map_id); |
| + static std::string MapKey(const std::string& map_id, const std::string& key); |
| + static std::string MapPrefix(); |
| + static std::string NextNamespaceIdKey(); |
| + static std::string NextMapIdKey(); |
| + |
| + scoped_ptr<leveldb::DB> db_; |
| + FilePath file_path_; |
| + |
| + // True if a database error has occurred (e.g., cannot read data). |
| + bool db_error_; |
| + // 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_. |
| + int64 namespace_offset_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SessionStorageDatabase); |
| +}; |
| + |
| +} // namespace dom_storage |
| + |
| +#endif // WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ |