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 #include "content/browser/dom_storage/session_storage_database.h" | 5 #include "content/browser/dom_storage/session_storage_database.h" |
6 | 6 |
| 7 #include <stddef.h> |
| 8 |
7 #include <vector> | 9 #include <vector> |
8 | 10 |
9 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
10 #include "base/logging.h" | 12 #include "base/logging.h" |
11 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
12 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
14 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
15 #include "third_party/leveldatabase/env_chromium.h" | 17 #include "third_party/leveldatabase/env_chromium.h" |
16 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 18 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 const bool kOkIfExists = true; | 143 const bool kOkIfExists = true; |
142 if (!CreateNamespace(namespace_id, kOkIfExists, &batch)) | 144 if (!CreateNamespace(namespace_id, kOkIfExists, &batch)) |
143 return false; | 145 return false; |
144 | 146 |
145 std::string map_id; | 147 std::string map_id; |
146 bool exists; | 148 bool exists; |
147 if (!GetMapForArea(namespace_id, origin.spec(), leveldb::ReadOptions(), | 149 if (!GetMapForArea(namespace_id, origin.spec(), leveldb::ReadOptions(), |
148 &exists, &map_id)) | 150 &exists, &map_id)) |
149 return false; | 151 return false; |
150 if (exists) { | 152 if (exists) { |
151 int64 ref_count; | 153 int64_t ref_count; |
152 if (!GetMapRefCount(map_id, &ref_count)) | 154 if (!GetMapRefCount(map_id, &ref_count)) |
153 return false; | 155 return false; |
154 if (ref_count > 1) { | 156 if (ref_count > 1) { |
155 if (!DeepCopyArea(namespace_id, origin, !clear_all_first, | 157 if (!DeepCopyArea(namespace_id, origin, !clear_all_first, |
156 &map_id, &batch)) | 158 &map_id, &batch)) |
157 return false; | 159 return false; |
158 } else if (clear_all_first) { | 160 } else if (clear_all_first) { |
159 if (!ClearMap(map_id, &batch)) | 161 if (!ClearMap(map_id, &batch)) |
160 return false; | 162 return false; |
161 } | 163 } |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 } | 530 } |
529 | 531 |
530 bool SessionStorageDatabase::CreateMapForArea(const std::string& namespace_id, | 532 bool SessionStorageDatabase::CreateMapForArea(const std::string& namespace_id, |
531 const GURL& origin, | 533 const GURL& origin, |
532 std::string* map_id, | 534 std::string* map_id, |
533 leveldb::WriteBatch* batch) { | 535 leveldb::WriteBatch* batch) { |
534 leveldb::Slice next_map_id_key = NextMapIdKey(); | 536 leveldb::Slice next_map_id_key = NextMapIdKey(); |
535 leveldb::Status s = db_->Get(leveldb::ReadOptions(), next_map_id_key, map_id); | 537 leveldb::Status s = db_->Get(leveldb::ReadOptions(), next_map_id_key, map_id); |
536 if (!DatabaseErrorCheck(s.ok() || s.IsNotFound())) | 538 if (!DatabaseErrorCheck(s.ok() || s.IsNotFound())) |
537 return false; | 539 return false; |
538 int64 next_map_id = 0; | 540 int64_t next_map_id = 0; |
539 if (s.IsNotFound()) { | 541 if (s.IsNotFound()) { |
540 *map_id = "0"; | 542 *map_id = "0"; |
541 } else { | 543 } else { |
542 bool conversion_ok = base::StringToInt64(*map_id, &next_map_id); | 544 bool conversion_ok = base::StringToInt64(*map_id, &next_map_id); |
543 if (!ConsistencyCheck(conversion_ok)) | 545 if (!ConsistencyCheck(conversion_ok)) |
544 return false; | 546 return false; |
545 } | 547 } |
546 batch->Put(next_map_id_key, base::Int64ToString(++next_map_id)); | 548 batch->Put(next_map_id_key, base::Int64ToString(++next_map_id)); |
547 std::string namespace_key = NamespaceKey(namespace_id, origin.spec()); | 549 std::string namespace_key = NamespaceKey(namespace_id, origin.spec()); |
548 batch->Put(namespace_key, *map_id); | 550 batch->Put(namespace_key, *map_id); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
603 // Convert the raw data stored in base::string16 to raw data stored in | 605 // Convert the raw data stored in base::string16 to raw data stored in |
604 // std::string. | 606 // std::string. |
605 const char* data = reinterpret_cast<const char*>(value.string().data()); | 607 const char* data = reinterpret_cast<const char*>(value.string().data()); |
606 size_t size = value.string().size() * 2; | 608 size_t size = value.string().size() * 2; |
607 batch->Put(key, leveldb::Slice(data, size)); | 609 batch->Put(key, leveldb::Slice(data, size)); |
608 } | 610 } |
609 } | 611 } |
610 } | 612 } |
611 | 613 |
612 bool SessionStorageDatabase::GetMapRefCount(const std::string& map_id, | 614 bool SessionStorageDatabase::GetMapRefCount(const std::string& map_id, |
613 int64* ref_count) { | 615 int64_t* ref_count) { |
614 std::string ref_count_string; | 616 std::string ref_count_string; |
615 leveldb::Status s = db_->Get(leveldb::ReadOptions(), | 617 leveldb::Status s = db_->Get(leveldb::ReadOptions(), |
616 MapRefCountKey(map_id), &ref_count_string); | 618 MapRefCountKey(map_id), &ref_count_string); |
617 if (!ConsistencyCheck(s.ok())) | 619 if (!ConsistencyCheck(s.ok())) |
618 return false; | 620 return false; |
619 bool conversion_ok = base::StringToInt64(ref_count_string, ref_count); | 621 bool conversion_ok = base::StringToInt64(ref_count_string, ref_count); |
620 return ConsistencyCheck(conversion_ok); | 622 return ConsistencyCheck(conversion_ok); |
621 } | 623 } |
622 | 624 |
623 bool SessionStorageDatabase::IncreaseMapRefCount(const std::string& map_id, | 625 bool SessionStorageDatabase::IncreaseMapRefCount(const std::string& map_id, |
624 leveldb::WriteBatch* batch) { | 626 leveldb::WriteBatch* batch) { |
625 // Increase the ref count for the map. | 627 // Increase the ref count for the map. |
626 int64 old_ref_count; | 628 int64_t old_ref_count; |
627 if (!GetMapRefCount(map_id, &old_ref_count)) | 629 if (!GetMapRefCount(map_id, &old_ref_count)) |
628 return false; | 630 return false; |
629 batch->Put(MapRefCountKey(map_id), base::Int64ToString(++old_ref_count)); | 631 batch->Put(MapRefCountKey(map_id), base::Int64ToString(++old_ref_count)); |
630 return true; | 632 return true; |
631 } | 633 } |
632 | 634 |
633 bool SessionStorageDatabase::DecreaseMapRefCount(const std::string& map_id, | 635 bool SessionStorageDatabase::DecreaseMapRefCount(const std::string& map_id, |
634 int decrease, | 636 int decrease, |
635 leveldb::WriteBatch* batch) { | 637 leveldb::WriteBatch* batch) { |
636 // Decrease the ref count for the map. | 638 // Decrease the ref count for the map. |
637 int64 ref_count; | 639 int64_t ref_count; |
638 if (!GetMapRefCount(map_id, &ref_count)) | 640 if (!GetMapRefCount(map_id, &ref_count)) |
639 return false; | 641 return false; |
640 if (!ConsistencyCheck(decrease <= ref_count)) | 642 if (!ConsistencyCheck(decrease <= ref_count)) |
641 return false; | 643 return false; |
642 ref_count -= decrease; | 644 ref_count -= decrease; |
643 if (ref_count > 0) { | 645 if (ref_count > 0) { |
644 batch->Put(MapRefCountKey(map_id), base::Int64ToString(ref_count)); | 646 batch->Put(MapRefCountKey(map_id), base::Int64ToString(ref_count)); |
645 } else { | 647 } else { |
646 // Clear all keys in the map. | 648 // Clear all keys in the map. |
647 if (!ClearMap(map_id, batch)) | 649 if (!ClearMap(map_id, batch)) |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
721 std::string SessionStorageDatabase::MapKey(const std::string& map_id, | 723 std::string SessionStorageDatabase::MapKey(const std::string& map_id, |
722 const std::string& key) { | 724 const std::string& key) { |
723 return base::StringPrintf("map-%s-%s", map_id.c_str(), key.c_str()); | 725 return base::StringPrintf("map-%s-%s", map_id.c_str(), key.c_str()); |
724 } | 726 } |
725 | 727 |
726 const char* SessionStorageDatabase::NextMapIdKey() { | 728 const char* SessionStorageDatabase::NextMapIdKey() { |
727 return "next-map-id"; | 729 return "next-map-id"; |
728 } | 730 } |
729 | 731 |
730 } // namespace content | 732 } // namespace content |
OLD | NEW |