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