Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 "base/file_path.h" | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "third_party/leveldatabase/src/include/leveldb/status.h" | |
| 13 #include "webkit/dom_storage/dom_storage_types.h" | |
| 14 | |
| 15 class GURL; | |
| 16 | |
| 17 namespace leveldb { | |
| 18 | |
| 19 struct DB; | |
| 20 struct WriteBatch; | |
| 21 | |
| 22 } // namespace leveldb | |
| 23 | |
| 24 namespace dom_storage { | |
| 25 | |
| 26 // SessionStorageDatabase holds the data from multiple namespaces and multiple | |
| 27 // origins. All DomStorageAreas for session storage share the same | |
| 28 // SessionStorageDatabase. | |
| 29 class SessionStorageDatabase : | |
| 30 public base::RefCountedThreadSafe<SessionStorageDatabase> { | |
| 31 public: | |
| 32 explicit SessionStorageDatabase(const FilePath& file_path); | |
| 33 | |
| 34 // Reads all the key, value pairs stored in the database and returns | |
| 35 // them. |result| is assumed to be empty and any duplicate keys will | |
| 36 // be overwritten. If the database exists on disk then it will be | |
| 37 // opened. If it does not exist then it will not be created and | |
| 38 // |result| will be unmodified. | |
| 39 void ReadAllValues(int64 namespace_id, | |
| 40 const GURL& origin, | |
| 41 ValuesMap* result); | |
| 42 | |
| 43 // Updates the backing database. Will remove all keys before updating | |
| 44 // the database if |clear_all_first| is set. Then all entries in | |
| 45 // |changes| will be examined - keys mapped to a null NullableString16 | |
| 46 // will be removed and all others will be inserted/updated as appropriate. | |
| 47 bool CommitChanges(int64 namespace_id, | |
| 48 const GURL& origin, | |
| 49 bool clear_all_first, | |
| 50 const ValuesMap& changes); | |
| 51 | |
| 52 // Creates shallow copies of the maps for |namespace_id| and associates them | |
| 53 // with |new_namespace_id|. | |
| 54 bool ShallowCopyNamespace(int64 namespace_id, int64 new_namespace_id); | |
| 55 | |
| 56 // Creates a deep copy of the map for the data associated with |origin| in | |
| 57 // |namespace_id|. | |
| 58 bool DeepCopy(int64 namespace_id, const GURL& origin); | |
| 59 | |
| 60 // Breaks the association of (|namespace_id|, |origin|) and its map. | |
| 61 bool DisassociateMap(int64 namespace_id, const GURL& origin); | |
| 62 | |
| 63 // Deletes the data for |namespace_id| and |origin|. | |
| 64 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.
| |
| 65 | |
| 66 // Deletes the data for |namespace_id|. | |
| 67 void DeleteNamespace(int64 namespace_id); | |
| 68 | |
| 69 // Deletes possible leftover data from the previous run. | |
| 70 void DeleteLeftoverData(); | |
| 71 | |
| 72 private: | |
| 73 friend class base::RefCountedThreadSafe<SessionStorageDatabase>; | |
| 74 ~SessionStorageDatabase(); | |
| 75 | |
| 76 bool LazyOpen(bool create_if_needed); | |
| 77 leveldb::Status TryToOpen(const FilePath& file_path, leveldb::DB** db); | |
| 78 bool IsOpen() const; | |
| 79 | |
| 80 // Helper for reading the contents of the map |map_id| into |result|. If | |
| 81 // |only_keys| is true, only keys are aread from the database and the values | |
| 82 // in |result| will be empty. | |
| 83 void ReadMap(const std::string& map_id, ValuesMap* result, bool only_keys); | |
| 84 | |
| 85 // Helper for creating a new map for |area_key|. |map_id| will hold the id of | |
| 86 // the created map. Retuns true if the creation succeeded, false otherwise. | |
| 87 bool CreateNewMap(const std::string& area_key, | |
| 88 leveldb::WriteBatch* batch, | |
| 89 std::string* map_id); | |
| 90 | |
| 91 // Helper for writing values into a map. | |
| 92 void WriteValuesToMap(const std::string& map_id, | |
| 93 const ValuesMap& values, | |
| 94 leveldb::WriteBatch* batch); | |
| 95 | |
| 96 // Helper for reading the next namespace id. | |
| 97 bool GetNextNamespaceId(int64* next_namespace_id); | |
| 98 | |
| 99 // Helper for keeping the next namespace id up to date when adding new | |
| 100 // namespaces. | |
| 101 void UpdateNextNamespaceId(int64 namespace_id, leveldb::WriteBatch* batch); | |
| 102 | |
| 103 // Helper for reading the ref count of a map. | |
| 104 bool GetRefCount(const std::string& map_key, int64* ref_count); | |
| 105 | |
| 106 // Helper for decreasing the ref count of a map. | |
| 107 void DecreaseRefCount(const std::string& map_id, leveldb::WriteBatch* batch); | |
| 108 | |
| 109 // Helper for deleting all values in a map. | |
| 110 void ClearMap(const std::string& map_id, leveldb::WriteBatch* batch); | |
| 111 | |
| 112 // Helper for deleting data for |namespace_id| and |origin|. | |
| 113 void DeleteOrigin(const std::string& namespace_id, | |
| 114 const std::string& origin, | |
| 115 leveldb::WriteBatch* batch); | |
| 116 | |
| 117 // Helper for deleting data for |namespace_id|. | |
| 118 void DeleteNamespace(const std::string& namespace_id, | |
| 119 leveldb::WriteBatch* batch); | |
| 120 | |
| 121 // Helper for debugging. | |
| 122 void DumpData(); | |
| 123 | |
| 124 // Helper for ensuring that the database is in a consistent state. | |
| 125 bool CheckConsistency(); | |
| 126 | |
| 127 scoped_ptr<leveldb::DB> db_; | |
| 128 | |
| 129 FilePath file_path_; | |
| 130 bool failed_to_open_; | |
| 131 | |
| 132 // On startup, we read the next ununsed namespace id from the database. It | |
| 133 // will be the offset for namespace ids. The actual id of a namespace in the | |
| 134 // database will be: id passed to the API function + namespace_offset_. | |
| 135 int64 namespace_offset_; | |
| 136 | |
| 137 DISALLOW_COPY_AND_ASSIGN(SessionStorageDatabase); | |
| 138 }; | |
| 139 | |
| 140 } // namespace dom_storage | |
| 141 | |
| 142 #endif // WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ | |
| OLD | NEW |