| 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 #include "webkit/dom_storage/session_storage_database.h" | 5 #include "webkit/dom_storage/session_storage_database.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 return false; | 101 return false; |
| 102 } | 102 } |
| 103 } | 103 } |
| 104 | 104 |
| 105 WriteValuesToMap(map_id, changes, &batch); | 105 WriteValuesToMap(map_id, changes, &batch); |
| 106 | 106 |
| 107 leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch); | 107 leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch); |
| 108 return DatabaseErrorCheck(s.ok()); | 108 return DatabaseErrorCheck(s.ok()); |
| 109 } | 109 } |
| 110 | 110 |
| 111 bool SessionStorageDatabase::CloneNamespace(int64 namespace_id, | 111 void SessionStorageDatabase::CloneNamespace(int64 namespace_id, |
| 112 int64 new_namespace_id) { | 112 int64 new_namespace_id) { |
| 113 // Go through all origins in the namespace |namespace_id|, create placeholders | 113 // Go through all origins in the namespace |namespace_id|, create placeholders |
| 114 // for them in |new_namespace_id|, and associate them with the existing maps. | 114 // for them in |new_namespace_id|, and associate them with the existing maps. |
| 115 | 115 |
| 116 // Example, data before shallow copy: | 116 // Example, data before shallow copy: |
| 117 // | map-1 | 1 (refcount) | | 117 // | map-1 | 1 (refcount) | |
| 118 // | map-1-a | b | | 118 // | map-1-a | b | |
| 119 // | namespace-1 (1 = namespace id) | dummy | | 119 // | namespace-1 (1 = namespace id) | dummy | |
| 120 // | namespace-1-origin1 | 1 (mapid) | | 120 // | namespace-1-origin1 | 1 (mapid) | |
| 121 | 121 |
| 122 // Example, data after shallow copy: | 122 // Example, data after shallow copy: |
| 123 // | map-1 | 2 (inc. refcount) | | 123 // | map-1 | 2 (inc. refcount) | |
| 124 // | map-1-a | b | | 124 // | map-1-a | b | |
| 125 // | namespace-1 (1 = namespace id) | dummy | | 125 // | namespace-1 (1 = namespace id) | dummy | |
| 126 // | namespace-1-origin1 | 1 (mapid) | | 126 // | namespace-1-origin1 | 1 (mapid) | |
| 127 // | namespace-2 | dummy | | 127 // | namespace-2 | dummy | |
| 128 // | namespace-2-origin1 | 1 (mapid) << references the same map | 128 // | namespace-2-origin1 | 1 (mapid) << references the same map |
| 129 | 129 |
| 130 if (!LazyOpen(true)) | 130 if (!LazyOpen(true)) |
| 131 return false; | 131 return; |
| 132 | 132 |
| 133 leveldb::WriteBatch batch; | 133 leveldb::WriteBatch batch; |
| 134 const bool kOkIfExists = false; | 134 const bool kOkIfExists = false; |
| 135 if (!CreateNamespace(new_namespace_id, kOkIfExists, &batch)) | 135 if (!CreateNamespace(new_namespace_id, kOkIfExists, &batch)) |
| 136 return false; | 136 return; |
| 137 | 137 |
| 138 std::map<std::string, std::string> areas; | 138 std::map<std::string, std::string> areas; |
| 139 if (!GetAreasInNamespace(namespace_id, &areas)) | 139 if (!GetAreasInNamespace(namespace_id, &areas)) |
| 140 return false; | 140 return; |
| 141 | 141 |
| 142 for (std::map<std::string, std::string>::const_iterator it = areas.begin(); | 142 for (std::map<std::string, std::string>::const_iterator it = areas.begin(); |
| 143 it != areas.end(); ++it) { | 143 it != areas.end(); ++it) { |
| 144 const std::string& origin = it->first; | 144 const std::string& origin = it->first; |
| 145 const std::string& map_id = it->second; | 145 const std::string& map_id = it->second; |
| 146 if (!IncreaseMapRefCount(map_id, &batch)) | 146 if (!IncreaseMapRefCount(map_id, &batch)) |
| 147 return false; | 147 return; |
| 148 AddAreaToNamespace(new_namespace_id, origin, map_id, &batch); | 148 AddAreaToNamespace(new_namespace_id, origin, map_id, &batch); |
| 149 } | 149 } |
| 150 leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch); | 150 leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch); |
| 151 return DatabaseErrorCheck(s.ok()); | 151 DatabaseErrorCheck(s.ok()); |
| 152 } | 152 } |
| 153 | 153 |
| 154 bool SessionStorageDatabase::DeleteArea(int64 namespace_id, | 154 void SessionStorageDatabase::DeleteArea(int64 namespace_id, |
| 155 const GURL& origin) { | 155 const GURL& origin) { |
| 156 if (!LazyOpen(false)) { | 156 if (!LazyOpen(false)) { |
| 157 // No need to create the database if it doesn't exist. | 157 // No need to create the database if it doesn't exist. |
| 158 return true; | 158 return; |
| 159 } | 159 } |
| 160 leveldb::WriteBatch batch; | 160 leveldb::WriteBatch batch; |
| 161 if (!DeleteArea(namespace_id, origin.spec(), &batch)) | 161 if (!DeleteAreaHelper(namespace_id, origin.spec(), &batch)) |
| 162 return false; | 162 return; |
| 163 leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch); | 163 leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch); |
| 164 return DatabaseErrorCheck(s.ok()); | 164 DatabaseErrorCheck(s.ok()); |
| 165 } | 165 } |
| 166 | 166 |
| 167 bool SessionStorageDatabase::DeleteNamespace(int64 namespace_id) { | 167 bool SessionStorageDatabase::DeleteNamespace(int64 namespace_id) { |
| 168 if (!LazyOpen(false)) { | 168 if (!LazyOpen(false)) { |
| 169 // No need to create the database if it doesn't exist. | 169 // No need to create the database if it doesn't exist. |
| 170 return true; | 170 return true; |
| 171 } | 171 } |
| 172 // Itereate through the areas in the namespace. | 172 // Itereate through the areas in the namespace. |
| 173 leveldb::WriteBatch batch; | 173 leveldb::WriteBatch batch; |
| 174 std::map<std::string, std::string> areas; | 174 std::map<std::string, std::string> areas; |
| 175 if (!GetAreasInNamespace(namespace_id, &areas)) | 175 if (!GetAreasInNamespace(namespace_id, &areas)) |
| 176 return false; | 176 return false; |
| 177 for (std::map<std::string, std::string>::const_iterator it = areas.begin(); | 177 for (std::map<std::string, std::string>::const_iterator it = areas.begin(); |
| 178 it != areas.end(); ++it) { | 178 it != areas.end(); ++it) { |
| 179 const std::string& origin = it->first; | 179 const std::string& origin = it->first; |
| 180 if (!DeleteArea(namespace_id, origin, &batch)) | 180 if (!DeleteAreaHelper(namespace_id, origin, &batch)) |
| 181 return false; | 181 return false; |
| 182 } | 182 } |
| 183 batch.Delete(NamespaceStartKey(namespace_id, namespace_offset_)); | 183 batch.Delete(NamespaceStartKey(namespace_id, namespace_offset_)); |
| 184 leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch); | 184 leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch); |
| 185 return DatabaseErrorCheck(s.ok()); | 185 return DatabaseErrorCheck(s.ok()); |
| 186 } | 186 } |
| 187 | 187 |
| 188 bool SessionStorageDatabase::ReadNamespaceIds( |
| 189 std::vector<int64>* namespace_ids) { |
| 190 if (!LazyOpen(true)) |
| 191 return false; |
| 192 |
| 193 std::string namespace_prefix = NamespacePrefix(); |
| 194 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions())); |
| 195 it->Seek(namespace_prefix); |
| 196 if (it->status().IsNotFound()) |
| 197 return true; |
| 198 |
| 199 if (!DatabaseErrorCheck(it->status().ok())) |
| 200 return false; |
| 201 |
| 202 // Skip the dummy entry "namespace-" and iterate the namespaces. |
| 203 for (it->Next(); it->Valid(); it->Next()) { |
| 204 std::string key = it->key().ToString(); |
| 205 if (key.find(namespace_prefix) != 0) { |
| 206 // Iterated past the "namespace-" keys. |
| 207 break; |
| 208 } |
| 209 size_t second_dash = key.find('-', namespace_prefix.length()); |
| 210 if (second_dash != std::string::npos) |
| 211 continue; |
| 212 |
| 213 // The key is of the form "namespace-<namespaceid>". |
| 214 std::string namespace_id_str = key.substr(namespace_prefix.length()); |
| 215 int64 namespace_id; |
| 216 bool conversion_ok = |
| 217 base::StringToInt64(namespace_id_str, &namespace_id); |
| 218 if (!ConsistencyCheck(conversion_ok)) |
| 219 return false; |
| 220 namespace_ids->push_back(namespace_id - namespace_offset_); |
| 221 } |
| 222 return true; |
| 223 } |
| 224 |
| 188 bool SessionStorageDatabase::LazyOpen(bool create_if_needed) { | 225 bool SessionStorageDatabase::LazyOpen(bool create_if_needed) { |
| 189 base::AutoLock auto_lock(db_lock_); | 226 base::AutoLock auto_lock(db_lock_); |
| 190 if (db_error_ || is_inconsistent_) { | 227 if (db_error_ || is_inconsistent_) { |
| 191 // Don't try to open a database that we know has failed already. | 228 // Don't try to open a database that we know has failed already. |
| 192 return false; | 229 return false; |
| 193 } | 230 } |
| 194 if (IsOpen()) | 231 if (IsOpen()) |
| 195 return true; | 232 return true; |
| 196 | 233 |
| 197 if (!create_if_needed && | 234 if (!create_if_needed && |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 | 400 |
| 364 void SessionStorageDatabase::AddAreaToNamespace(int64 namespace_id, | 401 void SessionStorageDatabase::AddAreaToNamespace(int64 namespace_id, |
| 365 const std::string& origin, | 402 const std::string& origin, |
| 366 const std::string& map_id, | 403 const std::string& map_id, |
| 367 leveldb::WriteBatch* batch) { | 404 leveldb::WriteBatch* batch) { |
| 368 std::string namespace_key = NamespaceKey( | 405 std::string namespace_key = NamespaceKey( |
| 369 NamespaceIdStr(namespace_id, namespace_offset_), origin); | 406 NamespaceIdStr(namespace_id, namespace_offset_), origin); |
| 370 batch->Put(namespace_key, map_id); | 407 batch->Put(namespace_key, map_id); |
| 371 } | 408 } |
| 372 | 409 |
| 373 bool SessionStorageDatabase::DeleteArea(int64 namespace_id, | 410 bool SessionStorageDatabase::DeleteAreaHelper(int64 namespace_id, |
| 374 const std::string& origin, | 411 const std::string& origin, |
| 375 leveldb::WriteBatch* batch) { | 412 leveldb::WriteBatch* batch) { |
| 376 return DeleteArea(NamespaceIdStr(namespace_id, namespace_offset_), | 413 return DeleteAreaHelper(NamespaceIdStr(namespace_id, namespace_offset_), |
| 377 origin, batch); | 414 origin, batch); |
| 378 } | 415 } |
| 379 | 416 |
| 380 bool SessionStorageDatabase::DeleteArea(const std::string& namespace_id_str, | 417 bool SessionStorageDatabase::DeleteAreaHelper( |
| 381 const std::string& origin, | 418 const std::string& namespace_id_str, |
| 382 leveldb::WriteBatch* batch) { | 419 const std::string& origin, |
| 420 leveldb::WriteBatch* batch) { |
| 383 std::string map_id; | 421 std::string map_id; |
| 384 bool exists; | 422 bool exists; |
| 385 if (!GetMapForArea(namespace_id_str, origin, &exists, &map_id)) | 423 if (!GetMapForArea(namespace_id_str, origin, &exists, &map_id)) |
| 386 return false; | 424 return false; |
| 387 if (!exists) | 425 if (!exists) |
| 388 return true; // Nothing to delete. | 426 return true; // Nothing to delete. |
| 389 if (!DecreaseMapRefCount(map_id, 1, batch)) | 427 if (!DecreaseMapRefCount(map_id, 1, batch)) |
| 390 return false; | 428 return false; |
| 391 std::string namespace_key = NamespaceKey(namespace_id_str, origin); | 429 std::string namespace_key = NamespaceKey(namespace_id_str, origin); |
| 392 batch->Delete(namespace_key); | 430 batch->Delete(namespace_key); |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 | 668 |
| 631 const char* SessionStorageDatabase::NextNamespaceIdKey() { | 669 const char* SessionStorageDatabase::NextNamespaceIdKey() { |
| 632 return "next-namespace-id"; | 670 return "next-namespace-id"; |
| 633 } | 671 } |
| 634 | 672 |
| 635 const char* SessionStorageDatabase::NextMapIdKey() { | 673 const char* SessionStorageDatabase::NextMapIdKey() { |
| 636 return "next-map-id"; | 674 return "next-map-id"; |
| 637 } | 675 } |
| 638 | 676 |
| 639 } // namespace dom_storage | 677 } // namespace dom_storage |
| OLD | NEW |