| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ | 5 #ifndef WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ |
| 6 #define WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ | 6 #define WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "third_party/leveldatabase/src/include/leveldb/status.h" | 15 #include "third_party/leveldatabase/src/include/leveldb/status.h" |
| 16 #include "webkit/dom_storage/dom_storage_types.h" | 16 #include "webkit/dom_storage/dom_storage_types.h" |
| 17 | 17 |
| 18 class GURL; | 18 class GURL; |
| 19 | 19 |
| 20 namespace leveldb { | 20 namespace leveldb { |
| 21 class DB; | 21 class DB; |
| 22 struct ReadOptions; |
| 22 class WriteBatch; | 23 class WriteBatch; |
| 23 } // namespace leveldb | 24 } // namespace leveldb |
| 24 | 25 |
| 25 namespace dom_storage { | 26 namespace dom_storage { |
| 26 | 27 |
| 27 // SessionStorageDatabase holds the data from multiple namespaces and multiple | 28 // SessionStorageDatabase holds the data from multiple namespaces and multiple |
| 28 // origins. All DomStorageAreas for session storage share the same | 29 // origins. All DomStorageAreas for session storage share the same |
| 29 // SessionStorageDatabase. | 30 // SessionStorageDatabase. |
| 31 |
| 32 // Only one thread is allowed to call the public functions other than |
| 33 // ReadAreaValues. Other threads area allowed to call ReadAreaValues. |
| 30 class SessionStorageDatabase : | 34 class SessionStorageDatabase : |
| 31 public base::RefCountedThreadSafe<SessionStorageDatabase> { | 35 public base::RefCountedThreadSafe<SessionStorageDatabase> { |
| 32 public: | 36 public: |
| 33 explicit SessionStorageDatabase(const FilePath& file_path); | 37 explicit SessionStorageDatabase(const FilePath& file_path); |
| 34 | 38 |
| 35 // Reads the (key, value) pairs for |namespace_id| and |origin|. |result| is | 39 // Reads the (key, value) pairs for |namespace_id| and |origin|. |result| is |
| 36 // assumed to be empty and any duplicate keys will be overwritten. If the | 40 // assumed to be empty and any duplicate keys will be overwritten. If the |
| 37 // database exists on disk then it will be opened. If it does not exist then | 41 // database exists on disk then it will be opened. If it does not exist then |
| 38 // it will not be created and |result| will be unmodified. | 42 // it will not be created and |result| will be unmodified. |
| 39 void ReadAreaValues(const std::string& namespace_id, | 43 void ReadAreaValues(const std::string& namespace_id, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 58 | 62 |
| 59 // Deletes the data for |namespace_id| and |origin|. | 63 // Deletes the data for |namespace_id| and |origin|. |
| 60 bool DeleteArea(const std::string& namespace_id, const GURL& origin); | 64 bool DeleteArea(const std::string& namespace_id, const GURL& origin); |
| 61 | 65 |
| 62 // Deletes the data for |namespace_id|. | 66 // Deletes the data for |namespace_id|. |
| 63 bool DeleteNamespace(const std::string& namespace_id); | 67 bool DeleteNamespace(const std::string& namespace_id); |
| 64 | 68 |
| 65 // Reads all namespace IDs from the database. | 69 // Reads all namespace IDs from the database. |
| 66 bool ReadNamespaceIds(std::vector<std::string>* namespace_ids); | 70 bool ReadNamespaceIds(std::vector<std::string>* namespace_ids); |
| 67 | 71 |
| 72 // Reads all origins which have data stored in |namespace_id|. |
| 73 bool ReadOriginsInNamespace(const std::string& namespace_id, |
| 74 std::vector<GURL>* origins); |
| 75 |
| 68 private: | 76 private: |
| 69 friend class base::RefCountedThreadSafe<SessionStorageDatabase>; | 77 friend class base::RefCountedThreadSafe<SessionStorageDatabase>; |
| 70 friend class SessionStorageDatabaseTest; | 78 friend class SessionStorageDatabaseTest; |
| 71 | 79 |
| 72 ~SessionStorageDatabase(); | 80 ~SessionStorageDatabase(); |
| 73 | 81 |
| 74 // Opens the database at file_path_ if it exists already and creates it if | 82 // Opens the database at file_path_ if it exists already and creates it if |
| 75 // |create_if_needed| is true. Returns true if the database was opened, false | 83 // |create_if_needed| is true. Returns true if the database was opened, false |
| 76 // if the opening failed or was not necessary (the database doesn't exist and | 84 // if the opening failed or was not necessary (the database doesn't exist and |
| 77 // |create_if_needed| is false). The possible failures are: | 85 // |create_if_needed| is false). The possible failures are: |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 | 126 |
| 119 // Helpers for deleting data for |namespace_id| and |origin|. | 127 // Helpers for deleting data for |namespace_id| and |origin|. |
| 120 bool DeleteAreaHelper(const std::string& namespace_id, | 128 bool DeleteAreaHelper(const std::string& namespace_id, |
| 121 const std::string& origin, | 129 const std::string& origin, |
| 122 leveldb::WriteBatch* batch); | 130 leveldb::WriteBatch* batch); |
| 123 | 131 |
| 124 // Retrieves the map id for |namespace_id| and |origin|. It's not an error if | 132 // Retrieves the map id for |namespace_id| and |origin|. It's not an error if |
| 125 // the map doesn't exist. | 133 // the map doesn't exist. |
| 126 bool GetMapForArea(const std::string& namespace_id, | 134 bool GetMapForArea(const std::string& namespace_id, |
| 127 const std::string& origin, | 135 const std::string& origin, |
| 136 const leveldb::ReadOptions& options, |
| 128 bool* exists, | 137 bool* exists, |
| 129 std::string* map_id); | 138 std::string* map_id); |
| 130 | 139 |
| 131 // Creates a new map for |namespace_id| and |origin|. |map_id| will hold the | 140 // Creates a new map for |namespace_id| and |origin|. |map_id| will hold the |
| 132 // id of the created map. If there is a map for |namespace_id| and |origin|, | 141 // id of the created map. If there is a map for |namespace_id| and |origin|, |
| 133 // this just overwrites the map id. The caller is responsible for decreasing | 142 // this just overwrites the map id. The caller is responsible for decreasing |
| 134 // the ref count. | 143 // the ref count. |
| 135 bool CreateMapForArea(const std::string& namespace_id, | 144 bool CreateMapForArea(const std::string& namespace_id, |
| 136 const GURL& origin, | 145 const GURL& origin, |
| 137 std::string* map_id, | 146 std::string* map_id, |
| 138 leveldb::WriteBatch* batch); | 147 leveldb::WriteBatch* batch); |
| 139 // Reads the contents of the map |map_id| into |result|. If |only_keys| is | 148 // Reads the contents of the map |map_id| into |result|. If |only_keys| is |
| 140 // true, only keys are aread from the database and the values in |result| will | 149 // true, only keys are aread from the database and the values in |result| will |
| 141 // be empty. | 150 // be empty. |
| 142 bool ReadMap(const std::string& map_id, | 151 bool ReadMap(const std::string& map_id, |
| 152 const leveldb::ReadOptions& options, |
| 143 ValuesMap* result, | 153 ValuesMap* result, |
| 144 bool only_keys); | 154 bool only_keys); |
| 145 // Writes |values| into the map |map_id|. | 155 // Writes |values| into the map |map_id|. |
| 146 void WriteValuesToMap(const std::string& map_id, | 156 void WriteValuesToMap(const std::string& map_id, |
| 147 const ValuesMap& values, | 157 const ValuesMap& values, |
| 148 leveldb::WriteBatch* batch); | 158 leveldb::WriteBatch* batch); |
| 149 | 159 |
| 150 bool GetMapRefCount(const std::string& map_id, int64* ref_count); | 160 bool GetMapRefCount(const std::string& map_id, int64* ref_count); |
| 151 bool IncreaseMapRefCount(const std::string& map_id, | 161 bool IncreaseMapRefCount(const std::string& map_id, |
| 152 leveldb::WriteBatch* batch); | 162 leveldb::WriteBatch* batch); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 bool db_error_; | 197 bool db_error_; |
| 188 // True if the database is in an inconsistent state. | 198 // True if the database is in an inconsistent state. |
| 189 bool is_inconsistent_; | 199 bool is_inconsistent_; |
| 190 | 200 |
| 191 DISALLOW_COPY_AND_ASSIGN(SessionStorageDatabase); | 201 DISALLOW_COPY_AND_ASSIGN(SessionStorageDatabase); |
| 192 }; | 202 }; |
| 193 | 203 |
| 194 } // namespace dom_storage | 204 } // namespace dom_storage |
| 195 | 205 |
| 196 #endif // WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ | 206 #endif // WEBKIT_DOM_STORAGE_SESSION_STORAGE_DATABASE_H_ |
| OLD | NEW |