OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 | 5 |
6 #include "content/browser/dom_storage/session_storage_database.h" | 6 #include "content/browser/dom_storage/session_storage_database.h" |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <algorithm> | 11 #include <algorithm> |
12 #include <map> | 12 #include <map> |
13 #include <string> | 13 #include <string> |
14 | 14 |
15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
16 #include "base/files/scoped_temp_dir.h" | 16 #include "base/files/scoped_temp_dir.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
20 #include "base/strings/string_util.h" | |
21 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
22 #include "content/common/dom_storage/dom_storage_types.h" | 21 #include "content/common/dom_storage/dom_storage_types.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 23 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
25 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" | 24 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" |
26 #include "third_party/leveldatabase/src/include/leveldb/options.h" | 25 #include "third_party/leveldatabase/src/include/leveldb/options.h" |
27 #include "url/gurl.h" | 26 #include "url/gurl.h" |
28 | 27 |
29 namespace content { | 28 namespace content { |
30 | 29 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 106 |
108 void SessionStorageDatabaseTest::ResetDatabase() { | 107 void SessionStorageDatabaseTest::ResetDatabase() { |
109 db_ = new SessionStorageDatabase(temp_dir_.path()); | 108 db_ = new SessionStorageDatabase(temp_dir_.path()); |
110 ASSERT_TRUE(db_->LazyOpen(true)); | 109 ASSERT_TRUE(db_->LazyOpen(true)); |
111 } | 110 } |
112 | 111 |
113 // static | 112 // static |
114 bool SessionStorageDatabaseTest::IsNamespaceKey(const std::string& key, | 113 bool SessionStorageDatabaseTest::IsNamespaceKey(const std::string& key, |
115 std::string* namespace_id) { | 114 std::string* namespace_id) { |
116 std::string namespace_prefix = SessionStorageDatabase::NamespacePrefix(); | 115 std::string namespace_prefix = SessionStorageDatabase::NamespacePrefix(); |
117 if (!base::StartsWith(key, namespace_prefix, base::CompareCase::SENSITIVE)) | 116 if (key.find(namespace_prefix) != 0) |
118 return false; | 117 return false; |
119 if (key == namespace_prefix) | 118 if (key == namespace_prefix) |
120 return false; | 119 return false; |
121 | 120 |
122 size_t second_dash = key.find('-', namespace_prefix.length()); | 121 size_t second_dash = key.find('-', namespace_prefix.length()); |
123 if (second_dash != key.length() - 1) | 122 if (second_dash != key.length() - 1) |
124 return false; | 123 return false; |
125 | 124 |
126 // Key is of the form "namespace-<namespaceid>-". | 125 // Key is of the form "namespace-<namespaceid>-". |
127 *namespace_id = key.substr( | 126 *namespace_id = key.substr( |
128 namespace_prefix.length(), | 127 namespace_prefix.length(), |
129 second_dash - namespace_prefix.length()); | 128 second_dash - namespace_prefix.length()); |
130 return true; | 129 return true; |
131 } | 130 } |
132 | 131 |
133 // static | 132 // static |
134 bool SessionStorageDatabaseTest::IsNamespaceOriginKey( | 133 bool SessionStorageDatabaseTest::IsNamespaceOriginKey( |
135 const std::string& key, | 134 const std::string& key, |
136 std::string* namespace_id) { | 135 std::string* namespace_id) { |
137 std::string namespace_prefix = SessionStorageDatabase::NamespacePrefix(); | 136 std::string namespace_prefix = SessionStorageDatabase::NamespacePrefix(); |
138 if (!base::StartsWith(key, namespace_prefix, base::CompareCase::SENSITIVE)) | 137 if (key.find(namespace_prefix) != 0) |
139 return false; | 138 return false; |
140 size_t second_dash = key.find('-', namespace_prefix.length()); | 139 size_t second_dash = key.find('-', namespace_prefix.length()); |
141 if (second_dash == std::string::npos || second_dash == key.length() - 1) | 140 if (second_dash == std::string::npos || second_dash == key.length() - 1) |
142 return false; | 141 return false; |
143 | 142 |
144 // Key is of the form "namespace-<namespaceid>-<origin>", and the value | 143 // Key is of the form "namespace-<namespaceid>-<origin>", and the value |
145 // is the map id. | 144 // is the map id. |
146 *namespace_id = key.substr( | 145 *namespace_id = key.substr( |
147 namespace_prefix.length(), | 146 namespace_prefix.length(), |
148 second_dash - namespace_prefix.length()); | 147 second_dash - namespace_prefix.length()); |
149 return true; | 148 return true; |
150 } | 149 } |
151 | 150 |
152 // static | 151 // static |
153 bool SessionStorageDatabaseTest::IsMapRefCountKey(const std::string& key, | 152 bool SessionStorageDatabaseTest::IsMapRefCountKey(const std::string& key, |
154 int64_t* map_id) { | 153 int64_t* map_id) { |
155 std::string map_prefix = "map-"; | 154 std::string map_prefix = "map-"; |
156 if (!base::StartsWith(key, map_prefix, base::CompareCase::SENSITIVE)) | 155 if (key.find(map_prefix) != 0) |
157 return false; | 156 return false; |
158 size_t second_dash = key.find('-', map_prefix.length()); | 157 size_t second_dash = key.find('-', map_prefix.length()); |
159 if (second_dash != key.length() - 1) | 158 if (second_dash != key.length() - 1) |
160 return false; | 159 return false; |
161 // Key is of the form "map-<mapid>-" and the value is the ref count. | 160 // Key is of the form "map-<mapid>-" and the value is the ref count. |
162 std::string map_id_str = key.substr(map_prefix.length(), | 161 std::string map_id_str = key.substr(map_prefix.length(), |
163 second_dash - map_prefix.length()); | 162 second_dash - map_prefix.length()); |
164 bool conversion_ok = base::StringToInt64(map_id_str, map_id); | 163 bool conversion_ok = base::StringToInt64(map_id_str, map_id); |
165 EXPECT_TRUE(conversion_ok); | 164 EXPECT_TRUE(conversion_ok); |
166 return true; | 165 return true; |
167 } | 166 } |
168 | 167 |
169 // static | 168 // static |
170 bool SessionStorageDatabaseTest::IsMapValueKey(const std::string& key, | 169 bool SessionStorageDatabaseTest::IsMapValueKey(const std::string& key, |
171 int64_t* map_id) { | 170 int64_t* map_id) { |
172 std::string map_prefix = "map-"; | 171 std::string map_prefix = "map-"; |
173 if (!base::StartsWith(key, map_prefix, base::CompareCase::SENSITIVE)) | 172 if (key.find(map_prefix) != 0) |
174 return false; | 173 return false; |
175 size_t second_dash = key.find('-', map_prefix.length()); | 174 size_t second_dash = key.find('-', map_prefix.length()); |
176 if (second_dash == std::string::npos || second_dash == key.length() - 1) | 175 if (second_dash == std::string::npos || second_dash == key.length() - 1) |
177 return false; | 176 return false; |
178 // Key is of the form "map-<mapid>-key". | 177 // Key is of the form "map-<mapid>-key". |
179 std::string map_id_str = key.substr(map_prefix.length(), | 178 std::string map_id_str = key.substr(map_prefix.length(), |
180 second_dash - map_prefix.length()); | 179 second_dash - map_prefix.length()); |
181 bool conversion_ok = base::StringToInt64(map_id_str, map_id); | 180 bool conversion_ok = base::StringToInt64(map_id_str, map_id); |
182 EXPECT_TRUE(conversion_ok); | 181 EXPECT_TRUE(conversion_ok); |
183 return true; | 182 return true; |
(...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
793 ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin2, false, data2)); | 792 ASSERT_TRUE(db_->CommitAreaChanges(kNamespace1, kOrigin2, false, data2)); |
794 | 793 |
795 EXPECT_TRUE(db_->DeleteArea(kNamespace1, kOrigin1)); | 794 EXPECT_TRUE(db_->DeleteArea(kNamespace1, kOrigin1)); |
796 EXPECT_TRUE(db_->DeleteArea(kNamespace1, kOrigin2)); | 795 EXPECT_TRUE(db_->DeleteArea(kNamespace1, kOrigin2)); |
797 // Check that also the namespace start key was deleted. | 796 // Check that also the namespace start key was deleted. |
798 CheckDatabaseConsistency(); | 797 CheckDatabaseConsistency(); |
799 } | 798 } |
800 | 799 |
801 | 800 |
802 } // namespace content | 801 } // namespace content |
OLD | NEW |