Commit 25d85d5e authored by Kenichi Ishibashi's avatar Kenichi Ishibashi Committed by Commit Bot

service worker: Expose resource ids getters for tests

This CL removes some direct use of ServiceWorker{Storage,Database}
from service_worker_storage_unittest.cc by exposing getters of
uncommitted/purgeable/purging resource ids.

The motivation of the removal of direct use is:
* Some tests checks whether resources are
  uncommitted/purgeable/purging in various situations.
* These tests depend on in-memory representations like
  ServiceWorkerRegistration.
* These tests will be moved to service_worker_registry_unittest.cc
  because _storage_unittest.cc will be moved to the Storage Service
  where we can't depend on in-memory representations.
* There is no access to ServiceWorker{Storage,Database} from
  service_worker_registry_unittest.cc because these are behind
  a mojo interface.

Bug: 1016064
Change-Id: I1104cd10226941aacc11a4cd9228a81e94ee5d2a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2460531
Commit-Queue: Kenichi Ishibashi <bashi@chromium.org>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#816050}
parent 6aba93c3
......@@ -268,6 +268,15 @@ interface ServiceWorkerStorageControl {
// This is analogous to LocalStorageControl::ApplyPolicyUpdates.
ApplyPolicyUpdates(array<LocalStoragePolicyUpdate> policy_updates);
// Get resource ids which are scheduled to purge.
GetPurgingResourceIdsForTest() => (ServiceWorkerDatabaseStatus status,
array<int64> resource_ids);
// Gets resource ids which are purgeable.
GetPurgeableResourceIdsForTest() => (ServiceWorkerDatabaseStatus status,
array<int64> resource_ids);
// Gets resource ids which are uncommitted.
GetUncommittedResourceIdsForTest() => (ServiceWorkerDatabaseStatus status,
array<int64> resource_ids);
// Sets a callback which is executed when purging resources completes.
// Only a single callback can be set at a time. Overlapped calls are not
// allowed.
......
......@@ -1130,6 +1130,29 @@ void ServiceWorkerStorage::SetPurgingCompleteCallbackForTest(
purging_complete_callback_for_test_ = std::move(callback);
}
void ServiceWorkerStorage::GetPurgingResourceIdsForTest(
ResourceIdsCallback callback) {
std::move(callback).Run(ServiceWorkerDatabase::Status::kOk,
std::vector<int64_t>(purgeable_resource_ids_.begin(),
purgeable_resource_ids_.end()));
}
void ServiceWorkerStorage::GetPurgeableResourceIdsForTest(
ResourceIdsCallback callback) {
database_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&GetPurgeableResourceIdsFromDB, database_.get(),
base::ThreadTaskRunnerHandle::Get(), std::move(callback)));
}
void ServiceWorkerStorage::GetUncommittedResourceIdsForTest(
ResourceIdsCallback callback) {
database_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&GetUncommittedResourceIdsFromDB, database_.get(),
base::ThreadTaskRunnerHandle::Get(), std::move(callback)));
}
void ServiceWorkerStorage::LazyInitialize(base::OnceClosure callback) {
DCHECK(state_ == STORAGE_STATE_UNINITIALIZED ||
state_ == STORAGE_STATE_INITIALIZING)
......@@ -1739,6 +1762,32 @@ void ServiceWorkerStorage::PerformStorageCleanupInDB(
database->RewriteDB();
}
// static
void ServiceWorkerStorage::GetPurgeableResourceIdsFromDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
ServiceWorkerStorage::ResourceIdsCallback callback) {
std::vector<int64_t> resource_ids;
ServiceWorkerDatabase::Status status =
database->GetPurgeableResourceIds(&resource_ids);
original_task_runner->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), status, std::move(resource_ids)));
}
// static
void ServiceWorkerStorage::GetUncommittedResourceIdsFromDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
ServiceWorkerStorage::ResourceIdsCallback callback) {
std::vector<int64_t> resource_ids;
ServiceWorkerDatabase::Status status =
database->GetUncommittedResourceIds(&resource_ids);
original_task_runner->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), status, std::move(resource_ids)));
}
void ServiceWorkerStorage::DidDeleteDatabase(
DatabaseStatusCallback callback,
ServiceWorkerDatabase::Status status) {
......
......@@ -91,6 +91,9 @@ class CONTENT_EXPORT ServiceWorkerStorage {
OriginState origin_state,
int64_t deleted_version_id,
const std::vector<int64_t>& newly_purgeable_resources)>;
using ResourceIdsCallback =
base::OnceCallback<void(ServiceWorkerDatabase::Status status,
const std::vector<int64_t>& resource_ids)>;
using DatabaseStatusCallback =
base::OnceCallback<void(ServiceWorkerDatabase::Status status)>;
......@@ -287,6 +290,10 @@ class CONTENT_EXPORT ServiceWorkerStorage {
void SetPurgingCompleteCallbackForTest(base::OnceClosure callback);
void GetPurgingResourceIdsForTest(ResourceIdsCallback callback);
void GetPurgeableResourceIdsForTest(ResourceIdsCallback callback);
void GetUncommittedResourceIdsForTest(ResourceIdsCallback callback);
private:
friend class ServiceWorkerStorageControlImplTest;
friend class service_worker_storage_unittest::ServiceWorkerStorageTest;
......@@ -493,6 +500,14 @@ class CONTENT_EXPORT ServiceWorkerStorage {
static void DeleteAllDataForOriginsFromDB(ServiceWorkerDatabase* database,
const std::set<GURL>& origins);
static void PerformStorageCleanupInDB(ServiceWorkerDatabase* database);
static void GetPurgeableResourceIdsFromDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
ServiceWorkerStorage::ResourceIdsCallback callback);
static void GetUncommittedResourceIdsFromDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
ServiceWorkerStorage::ResourceIdsCallback callback);
// Posted by the underlying cache implementation after it finishes making
// disk changes upon its destruction.
......
......@@ -368,6 +368,21 @@ void ServiceWorkerStorageControlImpl::ApplyPolicyUpdates(
storage_->ApplyPolicyUpdates(std::move(policy_updates));
}
void ServiceWorkerStorageControlImpl::GetPurgingResourceIdsForTest(
GetPurgeableResourceIdsForTestCallback callback) {
storage_->GetPurgingResourceIdsForTest(std::move(callback)); // IN-TEST
}
void ServiceWorkerStorageControlImpl::GetPurgeableResourceIdsForTest(
GetPurgeableResourceIdsForTestCallback callback) {
storage_->GetPurgeableResourceIdsForTest(std::move(callback)); // IN-TEST
}
void ServiceWorkerStorageControlImpl::GetUncommittedResourceIdsForTest(
GetPurgeableResourceIdsForTestCallback callback) {
storage_->GetUncommittedResourceIdsForTest(std::move(callback)); // IN-TEST
}
void ServiceWorkerStorageControlImpl::SetPurgingCompleteCallbackForTest(
SetPurgingCompleteCallbackForTestCallback callback) {
storage_->SetPurgingCompleteCallbackForTest(std::move(callback)); // IN-TEST
......
......@@ -150,6 +150,12 @@ class CONTENT_EXPORT ServiceWorkerStorageControlImpl
void ApplyPolicyUpdates(
const std::vector<storage::mojom::LocalStoragePolicyUpdatePtr>
policy_updates) override;
void GetPurgingResourceIdsForTest(
GetPurgingResourceIdsForTestCallback callback) override;
void GetPurgeableResourceIdsForTest(
GetPurgeableResourceIdsForTestCallback callback) override;
void GetUncommittedResourceIdsForTest(
GetUncommittedResourceIdsForTestCallback callback) override;
void SetPurgingCompleteCallbackForTest(
SetPurgingCompleteCallbackForTestCallback callback) override;
......
......@@ -621,8 +621,18 @@ class ServiceWorkerStorageTest : public testing::Test {
return result;
}
base::circular_deque<int64_t> GetPurgingResources() {
return storage()->purgeable_resource_ids_;
std::vector<int64_t> GetPurgingResources() {
std::vector<int64_t> ids;
base::RunLoop loop;
storage()->GetPurgingResourceIdsForTest(base::BindLambdaForTesting(
[&](ServiceWorkerDatabase::Status status,
const std::vector<int64_t>& resource_ids) {
EXPECT_EQ(status, ServiceWorkerDatabase::Status::kOk);
ids = resource_ids;
loop.Quit();
}));
loop.Run();
return ids;
}
// Directly writes a registration using
......@@ -647,11 +657,11 @@ class ServiceWorkerStorageTest : public testing::Test {
std::vector<int64_t> GetPurgeableResourceIdsFromDB() {
std::vector<int64_t> ids;
base::RunLoop loop;
ServiceWorkerDatabase* database_raw = database();
storage()->database_task_runner_->PostTask(
FROM_HERE, base::BindLambdaForTesting([&]() {
EXPECT_EQ(ServiceWorkerDatabase::Status::kOk,
database_raw->GetPurgeableResourceIds(&ids));
storage()->GetPurgeableResourceIdsForTest(base::BindLambdaForTesting(
[&](ServiceWorkerDatabase::Status status,
const std::vector<int64_t>& resource_ids) {
EXPECT_EQ(status, ServiceWorkerDatabase::Status::kOk);
ids = resource_ids;
loop.Quit();
}));
loop.Run();
......@@ -661,11 +671,11 @@ class ServiceWorkerStorageTest : public testing::Test {
std::vector<int64_t> GetUncommittedResourceIdsFromDB() {
std::vector<int64_t> ids;
base::RunLoop loop;
ServiceWorkerDatabase* database_raw = database();
storage()->database_task_runner_->PostTask(
FROM_HERE, base::BindLambdaForTesting([&]() {
EXPECT_EQ(ServiceWorkerDatabase::Status::kOk,
database_raw->GetUncommittedResourceIds(&ids));
storage()->GetUncommittedResourceIdsForTest(base::BindLambdaForTesting(
[&](ServiceWorkerDatabase::Status status,
const std::vector<int64_t>& resource_ids) {
EXPECT_EQ(status, ServiceWorkerDatabase::Status::kOk);
ids = resource_ids;
loop.Quit();
}));
loop.Run();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment