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