Commit a096fedc authored by Kenichi Ishibashi's avatar Kenichi Ishibashi Committed by Commit Bot

Add {Get,Store,Clear}UserData() in ServiceWorkerStorageControl

These methods are wrappers of corresponding methods defined in
ServiceWorkerStorage.

Bug: 1055677
Change-Id: I9acefbd627af1f9b495b9e47815bb109d07dc711
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2141813
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@{#758444}
parent 2f3bb77d
...@@ -68,6 +68,15 @@ interface ServiceWorkerResourceMetadataWriter { ...@@ -68,6 +68,15 @@ interface ServiceWorkerResourceMetadataWriter {
WriteMetadata(mojo_base.mojom.BigBuffer data) => (int32 status); WriteMetadata(mojo_base.mojom.BigBuffer data) => (int32 status);
}; };
// Represents an entry of user data which is stored with a service worker
// registration. An entry of user data is an arbitrary key-vaule pair. The
// lifetime of user data is tied up with the registration.
// It will be deleted when the corresponding registration is deleted.
struct ServiceWorkerUserData {
string key;
string value;
};
// Controls the state of service worker storage within a partition. This is a // Controls the state of service worker storage within a partition. This is a
// privileged interface and must not be brokered to untrusted clients. // privileged interface and must not be brokered to untrusted clients.
// //
...@@ -122,4 +131,18 @@ interface ServiceWorkerStorageControl { ...@@ -122,4 +131,18 @@ interface ServiceWorkerStorageControl {
CreateResourceMetadataWriter( CreateResourceMetadataWriter(
int64 resource_id, int64 resource_id,
pending_receiver<ServiceWorkerResourceMetadataWriter> writer); pending_receiver<ServiceWorkerResourceMetadataWriter> writer);
// Gets user data associated with the given |registration_id|.
// Succeeds only when all keys are found. On success, the size and the order
// of |values| are the same as |keys|.
GetUserData(int64 registration_id, array<string> keys) =>
(ServiceWorkerDatabaseStatus status, array<string> values);
// Stores |user_data| on persistent storage.
StoreUserData(int64 registration_id,
url.mojom.Url origin,
array<ServiceWorkerUserData> user_data) =>
(ServiceWorkerDatabaseStatus status);
// Clears user data specified by |registration_id| and |keys|.
ClearUserData(int64 registration_id, array<string> keys) =>
(ServiceWorkerDatabaseStatus status);
}; };
...@@ -68,6 +68,15 @@ void DidGetRegistrationsForOrigin( ...@@ -68,6 +68,15 @@ void DidGetRegistrationsForOrigin(
std::move(callback).Run(status, std::move(registrations)); std::move(callback).Run(status, std::move(registrations));
} }
void DidGetUserData(
ServiceWorkerStorageControlImpl::GetUserDataCallback callback,
const std::vector<std::string>& values,
storage::mojom::ServiceWorkerDatabaseStatus status) {
// TODO(bashi): Change ServiceWorkerStorage::GetUserDataInDBCallback to remove
// this indirection (the order of |values| and |status| is different).
std::move(callback).Run(status, values);
}
} // namespace } // namespace
ServiceWorkerStorageControlImpl::ServiceWorkerStorageControlImpl( ServiceWorkerStorageControlImpl::ServiceWorkerStorageControlImpl(
...@@ -174,4 +183,35 @@ void ServiceWorkerStorageControlImpl::CreateResourceMetadataWriter( ...@@ -174,4 +183,35 @@ void ServiceWorkerStorageControlImpl::CreateResourceMetadataWriter(
std::move(writer)); std::move(writer));
} }
void ServiceWorkerStorageControlImpl::GetUserData(
int64_t registration_id,
const std::vector<std::string>& keys,
GetUserDataCallback callback) {
storage_->GetUserData(registration_id, keys,
base::BindOnce(&DidGetUserData, std::move(callback)));
}
void ServiceWorkerStorageControlImpl::StoreUserData(
int64_t registration_id,
const GURL& origin,
std::vector<storage::mojom::ServiceWorkerUserDataPtr> user_data,
StoreUserDataCallback callback) {
// TODO(bashi): Change ServiceWorkerStorage::StoreUserData to take
// |user_data| so that we don't need to convert it.
std::vector<std::pair<std::string, std::string>> key_value_pairs;
for (auto& entry : user_data) {
key_value_pairs.push_back(
std::make_pair(std::move(entry->key), std::move(entry->value)));
}
storage_->StoreUserData(registration_id, origin, std::move(key_value_pairs),
std::move(callback));
}
void ServiceWorkerStorageControlImpl::ClearUserData(
int64_t registration_id,
const std::vector<std::string>& keys,
ClearUserDataCallback callback) {
storage_->ClearUserData(registration_id, keys, std::move(callback));
}
} // namespace content } // namespace content
...@@ -69,6 +69,17 @@ class CONTENT_EXPORT ServiceWorkerStorageControlImpl ...@@ -69,6 +69,17 @@ class CONTENT_EXPORT ServiceWorkerStorageControlImpl
int64_t resource_id, int64_t resource_id,
mojo::PendingReceiver<storage::mojom::ServiceWorkerResourceMetadataWriter> mojo::PendingReceiver<storage::mojom::ServiceWorkerResourceMetadataWriter>
writer) override; writer) override;
void GetUserData(int64_t registration_id,
const std::vector<std::string>& keys,
GetUserDataCallback callback) override;
void StoreUserData(
int64_t registration_id,
const GURL& origin,
std::vector<storage::mojom::ServiceWorkerUserDataPtr> user_data,
StoreUserDataCallback callback) override;
void ClearUserData(int64_t registration_id,
const std::vector<std::string>& keys,
ClearUserDataCallback callback) override;
const std::unique_ptr<ServiceWorkerStorage> storage_; const std::unique_ptr<ServiceWorkerStorage> storage_;
}; };
......
...@@ -247,6 +247,52 @@ class ServiceWorkerStorageControlImplTest : public testing::Test { ...@@ -247,6 +247,52 @@ class ServiceWorkerStorageControlImplTest : public testing::Test {
return return_value; return return_value;
} }
void GetUserData(int64_t registration_id,
const std::vector<std::string>& keys,
DatabaseStatus& out_status,
std::vector<std::string>& out_values) {
base::RunLoop loop;
storage()->GetUserData(
registration_id, keys,
base::BindLambdaForTesting(
[&](DatabaseStatus status, const std::vector<std::string>& values) {
out_status = status;
out_values = values;
loop.Quit();
}));
loop.Run();
}
DatabaseStatus StoreUserData(
int64_t registration_id,
const GURL& origin,
std::vector<storage::mojom::ServiceWorkerUserDataPtr> user_data) {
DatabaseStatus return_value;
base::RunLoop loop;
storage()->StoreUserData(
registration_id, origin, std::move(user_data),
base::BindLambdaForTesting([&](DatabaseStatus status) {
return_value = status;
loop.Quit();
}));
loop.Run();
return return_value;
}
DatabaseStatus ClearUserData(int64_t registration_id,
const std::vector<std::string>& keys) {
DatabaseStatus return_value;
base::RunLoop loop;
storage()->ClearUserData(
registration_id, keys,
base::BindLambdaForTesting([&](DatabaseStatus status) {
return_value = status;
loop.Quit();
}));
loop.Run();
return return_value;
}
// Create a registration with a single resource and stores the registration. // Create a registration with a single resource and stores the registration.
DatabaseStatus CreateAndStoreRegistration(int64_t registration_id, DatabaseStatus CreateAndStoreRegistration(int64_t registration_id,
const GURL& scope, const GURL& scope,
...@@ -545,4 +591,97 @@ TEST_F(ServiceWorkerStorageControlImplTest, WriteAndReadResource) { ...@@ -545,4 +591,97 @@ TEST_F(ServiceWorkerStorageControlImplTest, WriteAndReadResource) {
} }
} }
// Tests that storing/getting user data works.
TEST_F(ServiceWorkerStorageControlImplTest, StoreAndGetUserData) {
const GURL kScope("https://www.example.com/");
const GURL kScriptUrl("https://www.example.com/sw.js");
const int64_t kScriptSize = 10;
LazyInitializeForTest();
const int64_t registration_id = GetNewRegistrationId();
DatabaseStatus status;
status = CreateAndStoreRegistration(registration_id, kScope, kScriptUrl,
kScriptSize);
ASSERT_EQ(status, DatabaseStatus::kOk);
// Store user data with two entries.
{
std::vector<storage::mojom::ServiceWorkerUserDataPtr> user_data;
user_data.push_back(
storage::mojom::ServiceWorkerUserData::New("key1", "value1"));
user_data.push_back(
storage::mojom::ServiceWorkerUserData::New("key2", "value2"));
status = StoreUserData(registration_id, kScope.GetOrigin(),
std::move(user_data));
ASSERT_EQ(status, DatabaseStatus::kOk);
}
// Get user data.
{
std::vector<std::string> keys = {"key1", "key2"};
std::vector<std::string> values;
GetUserData(registration_id, keys, status, values);
ASSERT_EQ(status, DatabaseStatus::kOk);
EXPECT_EQ(values.size(), 2UL);
EXPECT_EQ("value1", values[0]);
EXPECT_EQ("value2", values[1]);
}
// Try to get user data with an unknown key should fail.
{
std::vector<std::string> keys = {"key1", "key2", "key3"};
std::vector<std::string> values;
GetUserData(registration_id, keys, status, values);
ASSERT_EQ(status, DatabaseStatus::kErrorNotFound);
EXPECT_EQ(values.size(), 0UL);
}
// Clear the first entry.
{
std::vector<std::string> keys = {"key1"};
status = ClearUserData(registration_id, keys);
ASSERT_EQ(status, DatabaseStatus::kOk);
std::vector<std::string> values;
GetUserData(registration_id, keys, status, values);
ASSERT_EQ(status, DatabaseStatus::kErrorNotFound);
EXPECT_EQ(values.size(), 0UL);
}
// Getting the second entry should succeed.
{
std::vector<std::string> keys = {"key2"};
std::vector<std::string> values;
GetUserData(registration_id, keys, status, values);
ASSERT_EQ(status, DatabaseStatus::kOk);
EXPECT_EQ(values.size(), 1UL);
EXPECT_EQ("value2", values[0]);
}
// Delete the registration and store a new registration for the same
// scope.
const int64_t new_registration_id = GetNewRegistrationId();
{
storage::mojom::ServiceWorkerStorageOriginState origin_state;
DeleteRegistration(registration_id, kScope.GetOrigin(), status,
origin_state);
ASSERT_EQ(status, DatabaseStatus::kOk);
status = CreateAndStoreRegistration(new_registration_id, kScope, kScriptUrl,
kScriptSize);
ASSERT_EQ(status, DatabaseStatus::kOk);
}
// Try to get user data stored for the previous registration should fail.
{
std::vector<std::string> keys = {"key2"};
std::vector<std::string> values;
GetUserData(new_registration_id, keys, status, values);
ASSERT_EQ(status, DatabaseStatus::kErrorNotFound);
EXPECT_EQ(values.size(), 0UL);
}
}
} // namespace content } // namespace content
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