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..7274d4d37f5e1456d268e260ad10d03b5d0f19b4 |
| --- /dev/null |
| +++ b/webkit/dom_storage/session_storage_database.h |
| @@ -0,0 +1,142 @@ |
| +// 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 { |
| + |
| +struct DB; |
| +struct WriteBatch; |
| + |
| +} // namespace leveldb |
| + |
| +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, |
| + 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); |
| + |
| + // 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|. |
| + void DeleteOrigin(int64 namespace_id, const GURL& origin); |
|
michaeln
2012/04/22 22:50:01
Might be useful to have this method invoked as a c
marja
2012/05/11 12:18:32
Done.
|
| + |
| + // Deletes the data for |namespace_id|. |
| + void DeleteNamespace(int64 namespace_id); |
| + |
| + // Deletes possible leftover data from the previous run. |
| + void DeleteLeftoverData(); |
| + |
| + private: |
| + friend class base::RefCountedThreadSafe<SessionStorageDatabase>; |
| + ~SessionStorageDatabase(); |
| + |
| + bool LazyOpen(bool create_if_needed); |
| + leveldb::Status TryToOpen(const FilePath& file_path, leveldb::DB** db); |
| + bool IsOpen() const; |
| + |
| + // 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. |
| + void 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. |
| + void 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. |
| + void DecreaseRefCount(const std::string& map_id, leveldb::WriteBatch* batch); |
| + |
| + // Helper for deleting all values in a map. |
| + void ClearMap(const std::string& map_id, leveldb::WriteBatch* batch); |
| + |
| + // Helper for deleting data for |namespace_id| and |origin|. |
| + void DeleteOrigin(const std::string& namespace_id, |
| + const std::string& origin, |
| + leveldb::WriteBatch* batch); |
| + |
| + // Helper for deleting data for |namespace_id|. |
| + void DeleteNamespace(const std::string& namespace_id, |
| + leveldb::WriteBatch* batch); |
| + |
| + // Helper for debugging. |
| + void DumpData(); |
| + |
| + // Helper for ensuring that the database is in a consistent state. |
| + bool CheckConsistency(); |
| + |
| + scoped_ptr<leveldb::DB> db_; |
| + |
| + FilePath file_path_; |
| + bool failed_to_open_; |
| + |
| + // 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_ |