Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/local_storage_context_mojo.h" | 5 #include "content/browser/dom_storage/local_storage_context_mojo.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 334 } else { | 334 } else { |
| 335 OnShutdownComplete(leveldb::mojom::DatabaseError::OK); | 335 OnShutdownComplete(leveldb::mojom::DatabaseError::OK); |
| 336 } | 336 } |
| 337 } | 337 } |
| 338 | 338 |
| 339 void LocalStorageContextMojo::PurgeMemory() { | 339 void LocalStorageContextMojo::PurgeMemory() { |
| 340 for (const auto& it : level_db_wrappers_) | 340 for (const auto& it : level_db_wrappers_) |
| 341 it.second->level_db_wrapper()->PurgeMemory(); | 341 it.second->level_db_wrapper()->PurgeMemory(); |
| 342 } | 342 } |
| 343 | 343 |
| 344 leveldb::mojom::LevelDBDatabaseAssociatedRequest | 344 void LocalStorageContextMojo::SetDatabaseForTesting( |
| 345 LocalStorageContextMojo::DatabaseRequestForTesting() { | 345 leveldb::mojom::LevelDBDatabaseAssociatedPtr database) { |
| 346 DCHECK_EQ(connection_state_, NO_CONNECTION); | 346 DCHECK_EQ(connection_state_, NO_CONNECTION); |
| 347 connection_state_ = CONNECTION_IN_PROGRESS; | 347 connection_state_ = CONNECTION_IN_PROGRESS; |
| 348 leveldb::mojom::LevelDBDatabaseAssociatedRequest request = | 348 database_ = std::move(database); |
| 349 MakeIsolatedRequest(&database_); | |
| 350 OnDatabaseOpened(true, leveldb::mojom::DatabaseError::OK); | 349 OnDatabaseOpened(true, leveldb::mojom::DatabaseError::OK); |
| 351 return request; | |
| 352 } | 350 } |
| 353 | 351 |
| 354 // static | 352 // static |
| 355 std::vector<uint8_t> LocalStorageContextMojo::MigrateString( | 353 std::vector<uint8_t> LocalStorageContextMojo::MigrateString( |
| 356 const base::string16& input) { | 354 const base::string16& input) { |
| 357 static const uint8_t kUTF16Format = 0; | 355 static const uint8_t kUTF16Format = 0; |
| 358 | 356 |
| 359 const uint8_t* data = reinterpret_cast<const uint8_t*>(input.data()); | 357 const uint8_t* data = reinterpret_cast<const uint8_t*>(input.data()); |
| 360 std::vector<uint8_t> result; | 358 std::vector<uint8_t> result; |
| 361 result.reserve(input.size() * sizeof(base::char16) + 1); | 359 result.reserve(input.size() * sizeof(base::char16) + 1); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 381 // Queue this OpenLocalStorage call for when we have a level db pointer. | 379 // Queue this OpenLocalStorage call for when we have a level db pointer. |
| 382 on_database_opened_callbacks_.push_back(std::move(callback)); | 380 on_database_opened_callbacks_.push_back(std::move(callback)); |
| 383 return; | 381 return; |
| 384 } | 382 } |
| 385 | 383 |
| 386 std::move(callback).Run(); | 384 std::move(callback).Run(); |
| 387 } | 385 } |
| 388 | 386 |
| 389 void LocalStorageContextMojo::InitiateConnection(bool in_memory_only) { | 387 void LocalStorageContextMojo::InitiateConnection(bool in_memory_only) { |
| 390 DCHECK_EQ(connection_state_, CONNECTION_IN_PROGRESS); | 388 DCHECK_EQ(connection_state_, CONNECTION_IN_PROGRESS); |
| 391 CHECK(connector_); | 389 |
| 390 // Unit tests might not always have a Connector, use in-memory only if that | |
| 391 // happens. | |
|
dcheng
2017/06/06 22:22:55
I think I've mentioned it elsewhere but it'd be ni
Marijn Kruisselbrink
2017/06/06 22:52:35
It is definitely possible to do so. All of the Loc
dcheng
2017/06/06 23:02:44
Meh. I'll turn my eye the other way and pretend th
| |
| 392 if (!connector_) { | |
| 393 OnDatabaseOpened(false, leveldb::mojom::DatabaseError::OK); | |
| 394 return; | |
| 395 } | |
| 396 | |
| 392 if (!subdirectory_.empty() && !in_memory_only) { | 397 if (!subdirectory_.empty() && !in_memory_only) { |
| 393 // We were given a subdirectory to write to. Get it and use a disk backed | 398 // We were given a subdirectory to write to. Get it and use a disk backed |
| 394 // database. | 399 // database. |
| 395 connector_->BindInterface(file::mojom::kServiceName, &file_system_); | 400 connector_->BindInterface(file::mojom::kServiceName, &file_system_); |
| 396 file_system_->GetSubDirectory( | 401 file_system_->GetSubDirectory( |
| 397 subdirectory_.AsUTF8Unsafe(), MakeRequest(&directory_), | 402 subdirectory_.AsUTF8Unsafe(), MakeRequest(&directory_), |
| 398 base::Bind(&LocalStorageContextMojo::OnDirectoryOpened, | 403 base::Bind(&LocalStorageContextMojo::OnDirectoryOpened, |
| 399 weak_ptr_factory_.GetWeakPtr())); | 404 weak_ptr_factory_.GetWeakPtr())); |
| 400 } else { | 405 } else { |
| 401 // We were not given a subdirectory. Use a memory backed database. | 406 // We were not given a subdirectory. Use a memory backed database. |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 600 return found->second->level_db_wrapper(); | 605 return found->second->level_db_wrapper(); |
| 601 | 606 |
| 602 auto holder = base::MakeUnique<LevelDBWrapperHolder>(this, origin); | 607 auto holder = base::MakeUnique<LevelDBWrapperHolder>(this, origin); |
| 603 LevelDBWrapperImpl* wrapper_ptr = holder->level_db_wrapper(); | 608 LevelDBWrapperImpl* wrapper_ptr = holder->level_db_wrapper(); |
| 604 level_db_wrappers_[origin] = std::move(holder); | 609 level_db_wrappers_[origin] = std::move(holder); |
| 605 return wrapper_ptr; | 610 return wrapper_ptr; |
| 606 } | 611 } |
| 607 | 612 |
| 608 void LocalStorageContextMojo::RetrieveStorageUsage( | 613 void LocalStorageContextMojo::RetrieveStorageUsage( |
| 609 GetStorageUsageCallback callback) { | 614 GetStorageUsageCallback callback) { |
| 615 if (!database_) { | |
| 616 // If for whatever reason no leveldb database is available, no storage is | |
| 617 // used, so return an empty array. | |
|
dcheng
2017/06/06 22:22:55
Does this mean the renderer did something bad?
Marijn Kruisselbrink
2017/06/06 22:52:35
No, this is not related to connections to the rend
| |
| 618 std::move(callback).Run(std::vector<LocalStorageUsageInfo>()); | |
| 619 return; | |
| 620 } | |
| 621 | |
| 610 database_->GetPrefixed( | 622 database_->GetPrefixed( |
| 611 std::vector<uint8_t>(kMetaPrefix, kMetaPrefix + arraysize(kMetaPrefix)), | 623 std::vector<uint8_t>(kMetaPrefix, kMetaPrefix + arraysize(kMetaPrefix)), |
| 612 base::Bind(&LocalStorageContextMojo::OnGotMetaData, | 624 base::Bind(&LocalStorageContextMojo::OnGotMetaData, |
| 613 weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback))); | 625 weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback))); |
| 614 } | 626 } |
| 615 | 627 |
| 616 void LocalStorageContextMojo::OnGotMetaData( | 628 void LocalStorageContextMojo::OnGotMetaData( |
| 617 GetStorageUsageCallback callback, | 629 GetStorageUsageCallback callback, |
| 618 leveldb::mojom::DatabaseError status, | 630 leveldb::mojom::DatabaseError status, |
| 619 std::vector<leveldb::mojom::KeyValuePtr> data) { | 631 std::vector<leveldb::mojom::KeyValuePtr> data) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 672 OnShutdownComplete(leveldb::mojom::DatabaseError::OK); | 684 OnShutdownComplete(leveldb::mojom::DatabaseError::OK); |
| 673 } | 685 } |
| 674 } | 686 } |
| 675 | 687 |
| 676 void LocalStorageContextMojo::OnShutdownComplete( | 688 void LocalStorageContextMojo::OnShutdownComplete( |
| 677 leveldb::mojom::DatabaseError error) { | 689 leveldb::mojom::DatabaseError error) { |
| 678 delete this; | 690 delete this; |
| 679 } | 691 } |
| 680 | 692 |
| 681 } // namespace content | 693 } // namespace content |
| OLD | NEW |