OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/dom_storage/session_storage_database.h" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/stringprintf.h" |
| 10 #include "base/string_number_conversions.h" |
| 11 #include "base/utf_string_conversions.h" |
| 12 #include "googleurl/src/gurl.h" |
| 13 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 14 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" |
| 15 #include "third_party/leveldatabase/src/include/leveldb/status.h" |
| 16 #include "third_party/leveldatabase/src/include/leveldb/options.h" |
| 17 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 18 |
| 19 // Layout of the database: |
| 20 // | key | value | |
| 21 // ----------------------------------------------------------------------- |
| 22 // | map-1 | 2 (refcount, start of map-1-* keys)| |
| 23 // | map-1-a | b (a = b in map 1) | |
| 24 // | ... | | |
| 25 // | namespace- | dummy (start of namespace-* keys) | |
| 26 // | namespace-1 (1 = namespace id) | dummy (start of namespace-1-* keys)| |
| 27 // | namespace-1-origin1 | 1 (mapid) | |
| 28 // | namespace-1-origin2 | 2 | |
| 29 // | namespace-2 | dummy | |
| 30 // | namespace-2-origin1 | 1 (shallow copy) | |
| 31 // | namespace-2-origin2 | 2 (shallow copy) | |
| 32 // | namespace-3 | dummy | |
| 33 // | namespace-3-origin1 | 3 (deep copy) | |
| 34 // | namespace-3-origin2 | 2 (shallow copy) | |
| 35 // | next-namespace-id | 4 | |
| 36 // | next-map-id | 4 | |
| 37 |
| 38 namespace dom_storage { |
| 39 |
| 40 SessionStorageDatabase::SessionStorageDatabase(const FilePath& file_path) |
| 41 : file_path_(file_path), |
| 42 db_error_(false), |
| 43 is_inconsistent_(false), |
| 44 namespace_offset_(0) { } |
| 45 |
| 46 SessionStorageDatabase::~SessionStorageDatabase() { |
| 47 } |
| 48 |
| 49 void SessionStorageDatabase::ReadAreaValues(int64 namespace_id, |
| 50 const GURL& origin, |
| 51 ValuesMap* result) { |
| 52 // We don't create a database if it doesn't exist. In that case, there is |
| 53 // nothing to be added to the result. |
| 54 if (!LazyOpen(false)) |
| 55 return; |
| 56 std::string map_id; |
| 57 bool exists; |
| 58 if (!GetMapForArea(namespace_id, origin, &exists, &map_id)) |
| 59 return; |
| 60 if (exists) |
| 61 ReadMap(map_id, result, false); |
| 62 } |
| 63 |
| 64 bool SessionStorageDatabase::CommitAreaChanges(int64 namespace_id, |
| 65 const GURL& origin, |
| 66 bool clear_all_first, |
| 67 const ValuesMap& changes) { |
| 68 // Even if |changes| is empty, we need to write the appropriate placeholders |
| 69 // in the database, so that it can be later shallow-copied succssfully. |
| 70 if (!LazyOpen(true)) |
| 71 return false; |
| 72 |
| 73 leveldb::WriteBatch batch; |
| 74 // Ensure that the keys "namespace-" "namespace-N" (see the schema above) |
| 75 // exist. |
| 76 const bool kOkIfExists = true; |
| 77 if (!CreateNamespace(namespace_id, kOkIfExists, &batch)) |
| 78 return false; |
| 79 |
| 80 std::string map_id; |
| 81 bool exists; |
| 82 if (!GetMapForArea(namespace_id, origin, &exists, &map_id)) |
| 83 return false; |
| 84 if (exists) { |
| 85 // We shouldn't write data into a shallow copy. If this is a shallow copy, |
| 86 // it's a caller error (not an inconsistency in the database). |
| 87 int64 ref_count; |
| 88 if (!GetMapRefCount(map_id, &ref_count)) |
| 89 return false; |
| 90 if (!CallerErrorCheck(ref_count == 1)) |
| 91 return false; |
| 92 |
| 93 if (clear_all_first) { |
| 94 if (!ClearMap(map_id, &batch)) |
| 95 return false; |
| 96 } |
| 97 } else { |
| 98 // Map doesn't exist, create it now if needed. |
| 99 if (!changes.empty()) { |
| 100 if (!CreateMapForArea(namespace_id, origin, &map_id, &batch)) |
| 101 return false; |
| 102 } |
| 103 } |
| 104 |
| 105 WriteValuesToMap(map_id, changes, &batch); |
| 106 |
| 107 leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch); |
| 108 return DatabaseErrorCheck(s.ok()); |
| 109 } |
| 110 |
| 111 bool SessionStorageDatabase::CloneNamespace(int64 namespace_id, |
| 112 int64 new_namespace_id) { |
| 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. |
| 115 |
| 116 // Example, data before shallow copy: |
| 117 // | map-1 | 1 (refcount) | |
| 118 // | map-1-a | b | |
| 119 // | namespace-1 (1 = namespace id) | dummy | |
| 120 // | namespace-1-origin1 | 1 (mapid) | |
| 121 |
| 122 // Example, data after shallow copy: |
| 123 // | map-1 | 2 (inc. refcount) | |
| 124 // | map-1-a | b | |
| 125 // | namespace-1 (1 = namespace id) | dummy | |
| 126 // | namespace-1-origin1 | 1 (mapid) | |
| 127 // | namespace-2 | dummy | |
| 128 // | namespace-2-origin1 | 1 (mapid) << references the same map |
| 129 |
| 130 if (!LazyOpen(true)) |
| 131 return false; |
| 132 |
| 133 leveldb::WriteBatch batch; |
| 134 const bool kOkIfExists = false; |
| 135 if (!CreateNamespace(new_namespace_id, kOkIfExists, &batch)) |
| 136 return false; |
| 137 |
| 138 std::map<std::string, std::string> areas; |
| 139 if (!GetAreasInNamespace(namespace_id, &areas)) |
| 140 return false; |
| 141 |
| 142 for (std::map<std::string, std::string>::const_iterator it = areas.begin(); |
| 143 it != areas.end(); ++it) { |
| 144 const std::string& origin = it->first; |
| 145 const std::string& map_id = it->second; |
| 146 if (!IncreaseMapRefCount(map_id, &batch)) |
| 147 return false; |
| 148 AddAreaToNamespace(new_namespace_id, origin, map_id, &batch); |
| 149 } |
| 150 leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch); |
| 151 return DatabaseErrorCheck(s.ok()); |
| 152 } |
| 153 |
| 154 bool SessionStorageDatabase::DeepCopyArea(int64 namespace_id, |
| 155 const GURL& origin) { |
| 156 // Example, data before deep copy: |
| 157 // | namespace-1 (1 = namespace id) | dummy | |
| 158 // | namespace-1-origin1 | 1 (mapid) | |
| 159 // | namespace-2 | dummy | |
| 160 // | namespace-2-origin1 | 1 (mapid) << references the same map |
| 161 // | map-1 | 2 (refcount) | |
| 162 // | map-1-a | b | |
| 163 |
| 164 // Example, data after deep copy copy: |
| 165 // | namespace-1 (1 = namespace id) | dummy | |
| 166 // | namespace-1-origin1 | 1 (mapid) | |
| 167 // | namespace-2 | dummy | |
| 168 // | namespace-2-origin1 | 2 (mapid) << references the new map |
| 169 // | map-1 | 1 (dec. refcount) | |
| 170 // | map-1-a | b | |
| 171 // | map-2 | 1 (refcount) | |
| 172 // | map-2-a | b | |
| 173 |
| 174 if (!LazyOpen(true)) |
| 175 return false; |
| 176 |
| 177 std::string old_map_id; |
| 178 bool exists; |
| 179 if (!GetMapForArea(namespace_id, origin, &exists, &old_map_id)) |
| 180 return false; |
| 181 |
| 182 // If the area doesn't exist, or if it's not a shallow copy, it's a caller |
| 183 // error. |
| 184 if (!CallerErrorCheck(exists)) |
| 185 return false; |
| 186 int64 ref_count; |
| 187 if (!GetMapRefCount(old_map_id, &ref_count)) |
| 188 return false; |
| 189 if (!CallerErrorCheck(ref_count > 1)) |
| 190 return false; |
| 191 |
| 192 leveldb::WriteBatch batch; |
| 193 std::string new_map_id; |
| 194 if (!CreateMapForArea(namespace_id, origin, &new_map_id, &batch)) |
| 195 return false; |
| 196 |
| 197 // Copy the values in the map. |
| 198 ValuesMap values; |
| 199 if (!ReadMap(old_map_id, &values, false)) |
| 200 return false; |
| 201 WriteValuesToMap(new_map_id, values, &batch); |
| 202 |
| 203 if (!DecreaseMapRefCount(old_map_id, 1, &batch)) |
| 204 return false; |
| 205 |
| 206 leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch); |
| 207 return DatabaseErrorCheck(s.ok()); |
| 208 } |
| 209 |
| 210 bool SessionStorageDatabase::DeleteArea(int64 namespace_id, |
| 211 const GURL& origin) { |
| 212 if (!LazyOpen(false)) { |
| 213 // No need to create the database if it doesn't exist. |
| 214 return true; |
| 215 } |
| 216 leveldb::WriteBatch batch; |
| 217 if (!DeleteArea(namespace_id, origin.spec(), &batch)) |
| 218 return false; |
| 219 leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch); |
| 220 return DatabaseErrorCheck(s.ok()); |
| 221 } |
| 222 |
| 223 bool SessionStorageDatabase::DeleteNamespace(int64 namespace_id) { |
| 224 if (!LazyOpen(false)) { |
| 225 // No need to create the database if it doesn't exist. |
| 226 return true; |
| 227 } |
| 228 // Itereate through the areas in the namespace. |
| 229 leveldb::WriteBatch batch; |
| 230 std::map<std::string, std::string> areas; |
| 231 if (!GetAreasInNamespace(namespace_id, &areas)) |
| 232 return false; |
| 233 for (std::map<std::string, std::string>::const_iterator it = areas.begin(); |
| 234 it != areas.end(); ++it) { |
| 235 const std::string& origin = it->first; |
| 236 if (!DeleteArea(namespace_id, origin, &batch)) |
| 237 return false; |
| 238 } |
| 239 batch.Delete(NamespaceStartKey(namespace_id, namespace_offset_)); |
| 240 leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch); |
| 241 return DatabaseErrorCheck(s.ok()); |
| 242 } |
| 243 |
| 244 bool SessionStorageDatabase::LazyOpen(bool create_if_needed) { |
| 245 base::AutoLock auto_lock(db_lock_); |
| 246 if (db_error_ || is_inconsistent_) { |
| 247 // Don't try to open a database that we know has failed already. |
| 248 return false; |
| 249 } |
| 250 if (IsOpen()) |
| 251 return true; |
| 252 |
| 253 if (!create_if_needed && |
| 254 (!file_util::PathExists(file_path_) || |
| 255 file_util::IsDirectoryEmpty(file_path_))) { |
| 256 // If the directory doesn't exist already and we haven't been asked to |
| 257 // create a file on disk, then we don't bother opening the database. This |
| 258 // means we wait until we absolutely need to put something onto disk before |
| 259 // we do so. |
| 260 return false; |
| 261 } |
| 262 |
| 263 leveldb::DB* db; |
| 264 leveldb::Status s = TryToOpen(file_path_, &db); |
| 265 if (!s.ok()) { |
| 266 LOG(WARNING) << "Failed to open leveldb in " << file_path_.value() |
| 267 << ", error: " << s.ToString(); |
| 268 DCHECK(db == NULL); |
| 269 |
| 270 // Clear the directory and try again. |
| 271 file_util::Delete(file_path_, true); |
| 272 s = TryToOpen(file_path_, &db); |
| 273 if (!s.ok()) { |
| 274 LOG(WARNING) << "Failed to open leveldb in " << file_path_.value() |
| 275 << ", error: " << s.ToString(); |
| 276 DCHECK(db == NULL); |
| 277 db_error_ = true; |
| 278 return false; |
| 279 } |
| 280 } |
| 281 db_.reset(db); |
| 282 |
| 283 return GetNextNamespaceId(&namespace_offset_); |
| 284 } |
| 285 |
| 286 leveldb::Status SessionStorageDatabase::TryToOpen(const FilePath& file_path, |
| 287 leveldb::DB** db) { |
| 288 leveldb::Options options; |
| 289 // The directory exists but a valid leveldb database might not exist inside it |
| 290 // (e.g., a subset of the needed files might be missing). Handle this |
| 291 // situation gracefully by creating the database now. |
| 292 options.create_if_missing = true; |
| 293 #if defined(OS_WIN) |
| 294 return leveldb::DB::Open(options, WideToUTF8(file_path.value()), db); |
| 295 #elif defined(OS_POSIX) |
| 296 return leveldb::DB::Open(options, file_path.value(), db); |
| 297 #endif |
| 298 } |
| 299 |
| 300 bool SessionStorageDatabase::IsOpen() const { |
| 301 return db_.get() != NULL; |
| 302 } |
| 303 |
| 304 bool SessionStorageDatabase::CallerErrorCheck(bool ok) const { |
| 305 DCHECK(ok); |
| 306 return ok; |
| 307 } |
| 308 |
| 309 bool SessionStorageDatabase::ConsistencyCheck(bool ok) { |
| 310 if (ok) |
| 311 return true; |
| 312 base::AutoLock auto_lock(db_lock_); |
| 313 DCHECK(false); |
| 314 is_inconsistent_ = true; |
| 315 // We cannot recover the database during this run, e.g., the upper layer can |
| 316 // have a different understanding of the database state (shallow and deep |
| 317 // copies). |
| 318 // TODO(marja): Error handling. |
| 319 return false; |
| 320 } |
| 321 |
| 322 bool SessionStorageDatabase::DatabaseErrorCheck(bool ok) { |
| 323 if (ok) |
| 324 return true; |
| 325 base::AutoLock auto_lock(db_lock_); |
| 326 db_error_ = true; |
| 327 // TODO(marja): Error handling. |
| 328 return false; |
| 329 } |
| 330 |
| 331 bool SessionStorageDatabase::CreateNamespace(int64 namespace_id, |
| 332 bool ok_if_exists, |
| 333 leveldb::WriteBatch* batch) { |
| 334 std::string namespace_prefix = NamespacePrefix(); |
| 335 std::string dummy; |
| 336 leveldb::Status s = db_->Get(leveldb::ReadOptions(), namespace_prefix, |
| 337 &dummy); |
| 338 if (!DatabaseErrorCheck(s.ok() || s.IsNotFound())) |
| 339 return false; |
| 340 if (s.IsNotFound()) |
| 341 batch->Put(namespace_prefix, ""); |
| 342 |
| 343 std::string namespace_start_key = |
| 344 NamespaceStartKey(namespace_id, namespace_offset_); |
| 345 s = db_->Get(leveldb::ReadOptions(), namespace_start_key, &dummy); |
| 346 if (!DatabaseErrorCheck(s.ok() || s.IsNotFound())) |
| 347 return false; |
| 348 if (s.IsNotFound()) { |
| 349 batch->Put(namespace_start_key, ""); |
| 350 return UpdateNextNamespaceId(namespace_id, batch); |
| 351 } |
| 352 return CallerErrorCheck(ok_if_exists); |
| 353 } |
| 354 |
| 355 bool SessionStorageDatabase::GetNextNamespaceId(int64* next_namespace_id) { |
| 356 std::string next_namespace_id_string; |
| 357 leveldb::Status s = db_->Get(leveldb::ReadOptions(), NextNamespaceIdKey(), |
| 358 &next_namespace_id_string); |
| 359 if (!DatabaseErrorCheck(s.ok() || s.IsNotFound())) |
| 360 return false; |
| 361 if (s.IsNotFound()) { |
| 362 *next_namespace_id = 0; |
| 363 return true; |
| 364 } |
| 365 bool conversion_ok = |
| 366 base::StringToInt64(next_namespace_id_string, next_namespace_id); |
| 367 return ConsistencyCheck(conversion_ok); |
| 368 } |
| 369 |
| 370 bool SessionStorageDatabase::UpdateNextNamespaceId(int64 namespace_id, |
| 371 leveldb::WriteBatch* batch) { |
| 372 int64 next_namespace_id; |
| 373 if (!GetNextNamespaceId(&next_namespace_id)) |
| 374 return false; |
| 375 if (next_namespace_id < namespace_id + namespace_offset_ + 1) { |
| 376 next_namespace_id = namespace_id + namespace_offset_ + 1; |
| 377 batch->Put(NextNamespaceIdKey(), base::Int64ToString(next_namespace_id)); |
| 378 } |
| 379 return true; |
| 380 } |
| 381 |
| 382 bool SessionStorageDatabase::GetAreasInNamespace( |
| 383 int64 namespace_id, |
| 384 std::map<std::string, std::string>* areas) { |
| 385 return GetAreasInNamespace(NamespaceIdStr(namespace_id, namespace_offset_), |
| 386 areas); |
| 387 } |
| 388 |
| 389 bool SessionStorageDatabase::GetAreasInNamespace( |
| 390 const std::string& namespace_id_str, |
| 391 std::map<std::string, std::string>* areas) { |
| 392 std::string namespace_start_key = NamespaceStartKey(namespace_id_str); |
| 393 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions())); |
| 394 it->Seek(namespace_start_key); |
| 395 if (it->status().IsNotFound()) { |
| 396 // The namespace_start_key is not found when the namespace doesn't contain |
| 397 // any areas. We don't need to do anything. |
| 398 return true; |
| 399 } |
| 400 if (!DatabaseErrorCheck(it->status().ok())) |
| 401 return false; |
| 402 |
| 403 // Skip the dummy entry "namespace-<namespaceid>" and iterate the origins. |
| 404 for (it->Next(); it->Valid(); it->Next()) { |
| 405 std::string key = it->key().ToString(); |
| 406 if (key.find(namespace_start_key) != 0) { |
| 407 // Iterated past the origins for this namespace. |
| 408 break; |
| 409 } |
| 410 size_t second_dash = key.find('-', namespace_start_key.length()); |
| 411 if (!ConsistencyCheck(second_dash != std::string::npos)) |
| 412 return false; |
| 413 std::string origin = key.substr(second_dash + 1); |
| 414 std::string map_id = it->value().ToString(); |
| 415 (*areas)[origin] = map_id; |
| 416 } |
| 417 return true; |
| 418 } |
| 419 |
| 420 void SessionStorageDatabase::AddAreaToNamespace(int64 namespace_id, |
| 421 const std::string& origin, |
| 422 const std::string& map_id, |
| 423 leveldb::WriteBatch* batch) { |
| 424 std::string namespace_key = NamespaceKey( |
| 425 NamespaceIdStr(namespace_id, namespace_offset_), origin); |
| 426 batch->Put(namespace_key, map_id); |
| 427 } |
| 428 |
| 429 bool SessionStorageDatabase::DeleteArea(int64 namespace_id, |
| 430 const std::string& origin, |
| 431 leveldb::WriteBatch* batch) { |
| 432 return DeleteArea(NamespaceIdStr(namespace_id, namespace_offset_), |
| 433 origin, batch); |
| 434 } |
| 435 |
| 436 bool SessionStorageDatabase::DeleteArea(const std::string& namespace_id_str, |
| 437 const std::string& origin, |
| 438 leveldb::WriteBatch* batch) { |
| 439 std::string map_id; |
| 440 bool exists; |
| 441 if (!GetMapForArea(namespace_id_str, origin, &exists, &map_id)) |
| 442 return false; |
| 443 if (!exists) |
| 444 return true; // Nothing to delete. |
| 445 if (!DecreaseMapRefCount(map_id, 1, batch)) |
| 446 return false; |
| 447 std::string namespace_key = NamespaceKey(namespace_id_str, origin); |
| 448 batch->Delete(namespace_key); |
| 449 return true; |
| 450 } |
| 451 |
| 452 bool SessionStorageDatabase::GetMapForArea(int64 namespace_id, |
| 453 const GURL& origin, |
| 454 bool* exists, |
| 455 std::string* map_id) { |
| 456 return GetMapForArea( |
| 457 base::Int64ToString(namespace_id + namespace_offset_), |
| 458 origin.spec(), exists, map_id); |
| 459 } |
| 460 |
| 461 bool SessionStorageDatabase::GetMapForArea(const std::string& namespace_id_str, |
| 462 const std::string& origin, |
| 463 bool* exists, std::string* map_id) { |
| 464 std::string namespace_key = NamespaceKey(namespace_id_str, origin); |
| 465 leveldb::Status s = db_->Get(leveldb::ReadOptions(), namespace_key, map_id); |
| 466 if (s.IsNotFound()) { |
| 467 *exists = false; |
| 468 return true; |
| 469 } |
| 470 *exists = true; |
| 471 return DatabaseErrorCheck(s.ok()); |
| 472 } |
| 473 |
| 474 bool SessionStorageDatabase::CreateMapForArea(int64 namespace_id, |
| 475 const GURL& origin, |
| 476 std::string* map_id, |
| 477 leveldb::WriteBatch* batch) { |
| 478 std::string next_map_id_key = NextMapIdKey(); |
| 479 leveldb::Status s = db_->Get(leveldb::ReadOptions(), next_map_id_key, map_id); |
| 480 if (!DatabaseErrorCheck(s.ok() || s.IsNotFound())) |
| 481 return false; |
| 482 int64 next_map_id = 0; |
| 483 if (s.IsNotFound()) { |
| 484 *map_id = "0"; |
| 485 } else { |
| 486 bool conversion_ok = base::StringToInt64(*map_id, &next_map_id); |
| 487 if (!ConsistencyCheck(conversion_ok)) |
| 488 return false; |
| 489 } |
| 490 batch->Put(next_map_id_key, base::Int64ToString(++next_map_id)); |
| 491 std::string namespace_key = |
| 492 NamespaceKey(namespace_id, namespace_offset_, origin); |
| 493 batch->Put(namespace_key, *map_id); |
| 494 batch->Put(MapRefCountKey(*map_id), "1"); |
| 495 return true; |
| 496 } |
| 497 |
| 498 bool SessionStorageDatabase::ReadMap(const std::string& map_id, |
| 499 ValuesMap* result, |
| 500 bool only_keys) { |
| 501 scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions())); |
| 502 std::string map_start_key = MapRefCountKey(map_id); |
| 503 it->Seek(map_start_key); |
| 504 // The map needs to exist, otherwise we have a stale map_id in the database. |
| 505 if (!ConsistencyCheck(!it->status().IsNotFound())) |
| 506 return false; |
| 507 if (!DatabaseErrorCheck(it->status().ok())) |
| 508 return false; |
| 509 const int kPrefixLength = std::string(MapPrefix()).length(); |
| 510 // Skip the dummy entry "map-<mapid>". |
| 511 for (it->Next(); it->Valid(); it->Next()) { |
| 512 // Key is of the form "map-<mapid>-<key>". |
| 513 std::string key = it->key().ToString(); |
| 514 size_t second_dash = key.find('-', kPrefixLength); |
| 515 if (second_dash == std::string::npos || |
| 516 key.substr(kPrefixLength, second_dash - kPrefixLength) != map_id) { |
| 517 // Iterated beyond the keys in this map. |
| 518 break; |
| 519 } |
| 520 string16 key16 = UTF8ToUTF16(key.substr(second_dash + 1)); |
| 521 if (only_keys) { |
| 522 (*result)[key16] = NullableString16(true); |
| 523 } else { |
| 524 // Convert the raw data stored in std::string (it->value()) to raw data |
| 525 // stored in string16. |
| 526 size_t len = it->value().size() / sizeof(char16); |
| 527 const char16* data_ptr = |
| 528 reinterpret_cast<const char16*>(it->value().data()); |
| 529 (*result)[key16] = NullableString16(string16(data_ptr, len), false); |
| 530 } |
| 531 } |
| 532 return true; |
| 533 } |
| 534 |
| 535 void SessionStorageDatabase::WriteValuesToMap(const std::string& map_id, |
| 536 const ValuesMap& values, |
| 537 leveldb::WriteBatch* batch) { |
| 538 for (ValuesMap::const_iterator it = values.begin(); it != values.end(); |
| 539 ++it) { |
| 540 NullableString16 value = it->second; |
| 541 std::string key = MapKey(map_id, UTF16ToUTF8(it->first)); |
| 542 if (value.is_null()) { |
| 543 batch->Delete(key); |
| 544 } else { |
| 545 // Convert the raw data stored in string16 to raw data stored in |
| 546 // std::string. |
| 547 const char* data = reinterpret_cast<const char*>(value.string().data()); |
| 548 size_t size = value.string().size() * 2; |
| 549 batch->Put(key, leveldb::Slice(data, size)); |
| 550 } |
| 551 } |
| 552 } |
| 553 |
| 554 bool SessionStorageDatabase::GetMapRefCount(const std::string& map_id, |
| 555 int64* ref_count) { |
| 556 std::string ref_count_string; |
| 557 leveldb::Status s = db_->Get(leveldb::ReadOptions(), |
| 558 MapRefCountKey(map_id), &ref_count_string); |
| 559 if (!ConsistencyCheck(s.ok())) |
| 560 return false; |
| 561 bool conversion_ok = base::StringToInt64(ref_count_string, ref_count); |
| 562 return ConsistencyCheck(conversion_ok); |
| 563 } |
| 564 |
| 565 bool SessionStorageDatabase::IncreaseMapRefCount(const std::string& map_id, |
| 566 leveldb::WriteBatch* batch) { |
| 567 // Increase the ref count for the map. |
| 568 int64 old_ref_count; |
| 569 if (!GetMapRefCount(map_id, &old_ref_count)) |
| 570 return false; |
| 571 batch->Put(MapRefCountKey(map_id), base::Int64ToString(++old_ref_count)); |
| 572 return true; |
| 573 } |
| 574 |
| 575 bool SessionStorageDatabase::DecreaseMapRefCount(const std::string& map_id, |
| 576 int decrease, |
| 577 leveldb::WriteBatch* batch) { |
| 578 // Decrease the ref count for the map. |
| 579 int64 ref_count; |
| 580 if (!GetMapRefCount(map_id, &ref_count)) |
| 581 return false; |
| 582 if (!ConsistencyCheck(decrease <= ref_count)) |
| 583 return false; |
| 584 ref_count -= decrease; |
| 585 if (ref_count > 0) { |
| 586 batch->Put(MapRefCountKey(map_id), base::Int64ToString(ref_count)); |
| 587 } else { |
| 588 // Clear all keys in the map. |
| 589 if (!ClearMap(map_id, batch)) |
| 590 return false; |
| 591 batch->Delete(MapRefCountKey(map_id)); |
| 592 } |
| 593 return true; |
| 594 } |
| 595 |
| 596 bool SessionStorageDatabase::ClearMap(const std::string& map_id, |
| 597 leveldb::WriteBatch* batch) { |
| 598 ValuesMap values; |
| 599 if (!ReadMap(map_id, &values, true)) |
| 600 return false; |
| 601 for (ValuesMap::const_iterator it = values.begin(); it != values.end(); ++it) |
| 602 batch->Delete(MapKey(map_id, UTF16ToUTF8(it->first))); |
| 603 return true; |
| 604 } |
| 605 |
| 606 std::string SessionStorageDatabase::NamespaceStartKey( |
| 607 const std::string& namespace_id_str) { |
| 608 return base::StringPrintf("namespace-%s", namespace_id_str.c_str()); |
| 609 } |
| 610 |
| 611 std::string SessionStorageDatabase::NamespaceStartKey(int64 namespace_id, |
| 612 int64 namespace_offset) { |
| 613 return NamespaceStartKey(NamespaceIdStr(namespace_id, namespace_offset)); |
| 614 } |
| 615 |
| 616 std::string SessionStorageDatabase::NamespaceKey( |
| 617 const std::string& namespace_id_str, const std::string& origin) { |
| 618 return base::StringPrintf("namespace-%s-%s", namespace_id_str.c_str(), |
| 619 origin.c_str()); |
| 620 } |
| 621 |
| 622 std::string SessionStorageDatabase::NamespaceKey( |
| 623 int64 namespace_id, int64 namespace_offset, const GURL& origin) { |
| 624 return NamespaceKey(NamespaceIdStr(namespace_id, namespace_offset), |
| 625 origin.spec()); |
| 626 } |
| 627 |
| 628 std::string SessionStorageDatabase::NamespaceIdStr(int64 namespace_id, |
| 629 int64 namespace_offset) { |
| 630 return base::Int64ToString(namespace_id + namespace_offset); |
| 631 } |
| 632 |
| 633 const char* SessionStorageDatabase::NamespacePrefix() { |
| 634 return "namespace-"; |
| 635 } |
| 636 |
| 637 std::string SessionStorageDatabase::MapRefCountKey(const std::string& map_id) { |
| 638 return base::StringPrintf("map-%s", map_id.c_str()); |
| 639 } |
| 640 |
| 641 std::string SessionStorageDatabase::MapKey(const std::string& map_id, |
| 642 const std::string& key) { |
| 643 return base::StringPrintf("map-%s-%s", map_id.c_str(), key.c_str()); |
| 644 } |
| 645 |
| 646 const char* SessionStorageDatabase::MapPrefix() { |
| 647 return "map-"; |
| 648 } |
| 649 |
| 650 const char* SessionStorageDatabase::NextNamespaceIdKey() { |
| 651 return "next-namespace-id"; |
| 652 } |
| 653 |
| 654 const char* SessionStorageDatabase::NextMapIdKey() { |
| 655 return "next-map-id"; |
| 656 } |
| 657 |
| 658 } // namespace dom_storage |
OLD | NEW |