| 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 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 // "<namespaceid>" parts from the keys. | 233 // "<namespaceid>" parts from the keys. |
| 234 if (current_namespace_start_key.empty() || | 234 if (current_namespace_start_key.empty() || |
| 235 key.substr(0, current_namespace_start_key.length()) != | 235 key.substr(0, current_namespace_start_key.length()) != |
| 236 current_namespace_start_key) { | 236 current_namespace_start_key) { |
| 237 // The key is of the form "namespace-<namespaceid>-" for a new | 237 // The key is of the form "namespace-<namespaceid>-" for a new |
| 238 // <namespaceid>. | 238 // <namespaceid>. |
| 239 current_namespace_start_key = key; | 239 current_namespace_start_key = key; |
| 240 current_namespace_id = | 240 current_namespace_id = |
| 241 key.substr(namespace_prefix.length(), | 241 key.substr(namespace_prefix.length(), |
| 242 key.length() - namespace_prefix.length() - 1); | 242 key.length() - namespace_prefix.length() - 1); |
| 243 // Ensure that we keep track of the namespace even if it doesn't contain |
| 244 // any origins. |
| 245 namespaces_and_origins->insert( |
| 246 std::make_pair(current_namespace_id, std::vector<GURL>())); |
| 243 } else { | 247 } else { |
| 244 // The key is of the form "namespace-<namespaceid>-<origin>". | 248 // The key is of the form "namespace-<namespaceid>-<origin>". |
| 245 std::string origin = key.substr(current_namespace_start_key.length()); | 249 std::string origin = key.substr(current_namespace_start_key.length()); |
| 246 (*namespaces_and_origins)[current_namespace_id].push_back(GURL(origin)); | 250 (*namespaces_and_origins)[current_namespace_id].push_back(GURL(origin)); |
| 247 } | 251 } |
| 248 } | 252 } |
| 249 db_->ReleaseSnapshot(options.snapshot); | 253 db_->ReleaseSnapshot(options.snapshot); |
| 250 return true; | 254 return true; |
| 251 } | 255 } |
| 252 | 256 |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 bool exists; | 408 bool exists; |
| 405 if (!GetMapForArea(namespace_id, origin, leveldb::ReadOptions(), &exists, | 409 if (!GetMapForArea(namespace_id, origin, leveldb::ReadOptions(), &exists, |
| 406 &map_id)) | 410 &map_id)) |
| 407 return false; | 411 return false; |
| 408 if (!exists) | 412 if (!exists) |
| 409 return true; // Nothing to delete. | 413 return true; // Nothing to delete. |
| 410 if (!DecreaseMapRefCount(map_id, 1, batch)) | 414 if (!DecreaseMapRefCount(map_id, 1, batch)) |
| 411 return false; | 415 return false; |
| 412 std::string namespace_key = NamespaceKey(namespace_id, origin); | 416 std::string namespace_key = NamespaceKey(namespace_id, origin); |
| 413 batch->Delete(namespace_key); | 417 batch->Delete(namespace_key); |
| 418 |
| 419 // If this was the only area in the namespace, delete the namespace start key, |
| 420 // too. |
| 421 std::string namespace_start_key = NamespaceStartKey(namespace_id); |
| 422 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions())); |
| 423 it->Seek(namespace_start_key); |
| 424 if (!ConsistencyCheck(it->Valid())) |
| 425 return false; |
| 426 // Advance the iterator 2 times (we still haven't really deleted |
| 427 // namespace_key). |
| 428 it->Next(); |
| 429 if (!ConsistencyCheck(it->Valid())) |
| 430 return false; |
| 431 it->Next(); |
| 432 if (!it->Valid()) |
| 433 return true; |
| 434 std::string key = it->key().ToString(); |
| 435 if (key.find(namespace_start_key) != 0) |
| 436 batch->Delete(namespace_start_key); |
| 414 return true; | 437 return true; |
| 415 } | 438 } |
| 416 | 439 |
| 417 bool SessionStorageDatabase::GetMapForArea(const std::string& namespace_id, | 440 bool SessionStorageDatabase::GetMapForArea(const std::string& namespace_id, |
| 418 const std::string& origin, | 441 const std::string& origin, |
| 419 const leveldb::ReadOptions& options, | 442 const leveldb::ReadOptions& options, |
| 420 bool* exists, std::string* map_id) { | 443 bool* exists, std::string* map_id) { |
| 421 std::string namespace_key = NamespaceKey(namespace_id, origin); | 444 std::string namespace_key = NamespaceKey(namespace_id, origin); |
| 422 leveldb::Status s = db_->Get(options, namespace_key, map_id); | 445 leveldb::Status s = db_->Get(options, namespace_key, map_id); |
| 423 if (s.IsNotFound()) { | 446 if (s.IsNotFound()) { |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 std::string SessionStorageDatabase::MapKey(const std::string& map_id, | 641 std::string SessionStorageDatabase::MapKey(const std::string& map_id, |
| 619 const std::string& key) { | 642 const std::string& key) { |
| 620 return base::StringPrintf("map-%s-%s", map_id.c_str(), key.c_str()); | 643 return base::StringPrintf("map-%s-%s", map_id.c_str(), key.c_str()); |
| 621 } | 644 } |
| 622 | 645 |
| 623 const char* SessionStorageDatabase::NextMapIdKey() { | 646 const char* SessionStorageDatabase::NextMapIdKey() { |
| 624 return "next-map-id"; | 647 return "next-map-id"; |
| 625 } | 648 } |
| 626 | 649 |
| 627 } // namespace dom_storage | 650 } // namespace dom_storage |
| OLD | NEW |