Commit 119092f3 authored by Kenichi Ishibashi's avatar Kenichi Ishibashi Committed by Commit Bot

Add {Get,Clear}UserDataByKeyPrefix() to ServiceWorkerStorageControl

These methods are wrappers of corresponding methods defined in
ServiceWorkerStorage.

Added test patterns come from ServiceWorkerStorageTest.StoreUserData
with minor modifications.

Bug: 1055677
Change-Id: I6615ff14d10cb7302db7d388363672839998f633
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2142879
Commit-Queue: Kenichi Ishibashi <bashi@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#758685}
parent 4cc8c56e
...@@ -145,4 +145,18 @@ interface ServiceWorkerStorageControl { ...@@ -145,4 +145,18 @@ interface ServiceWorkerStorageControl {
// Clears user data specified by |registration_id| and |keys|. // Clears user data specified by |registration_id| and |keys|.
ClearUserData(int64 registration_id, array<string> keys) => ClearUserData(int64 registration_id, array<string> keys) =>
(ServiceWorkerDatabaseStatus status); (ServiceWorkerDatabaseStatus status);
// Gets user data values associated with the given |registration_id|
// filtered by |key_prefix|.
GetUserDataByKeyPrefix(int64 registration_id, string key_prefix) =>
(ServiceWorkerDatabaseStatus status, array<string> values);
// Gets user data associated with the given |registration_id| filtered by
// |key_prefix|. Returns user data as key-value pairs.
GetUserKeysAndDataByKeyPrefix(int64 registration_id, string key_prefix) =>
(ServiceWorkerDatabaseStatus status, map<string, string> user_data);
// Clears user data associated with the given |registration_id| filtered by
// |key_prefix|.
ClearUserDataByKeyPrefixes(int64 registratation_id,
array<string> key_prefixes) =>
(ServiceWorkerDatabaseStatus status);
}; };
...@@ -77,6 +77,17 @@ void DidGetUserData( ...@@ -77,6 +77,17 @@ void DidGetUserData(
std::move(callback).Run(status, values); std::move(callback).Run(status, values);
} }
void DidGetKeysAndUserData(
ServiceWorkerStorageControlImpl::GetUserKeysAndDataByKeyPrefixCallback
callback,
const base::flat_map<std::string, std::string>& user_data,
storage::mojom::ServiceWorkerDatabaseStatus status) {
// TODO(bashi): Change ServiceWorkerStorage::GetUserKeysAndDataInDBCallback to
// remove this indirection (the order of |user_data| and |status| is
// different).
std::move(callback).Run(status, user_data);
}
} // namespace } // namespace
ServiceWorkerStorageControlImpl::ServiceWorkerStorageControlImpl( ServiceWorkerStorageControlImpl::ServiceWorkerStorageControlImpl(
...@@ -214,4 +225,30 @@ void ServiceWorkerStorageControlImpl::ClearUserData( ...@@ -214,4 +225,30 @@ void ServiceWorkerStorageControlImpl::ClearUserData(
storage_->ClearUserData(registration_id, keys, std::move(callback)); storage_->ClearUserData(registration_id, keys, std::move(callback));
} }
void ServiceWorkerStorageControlImpl::GetUserDataByKeyPrefix(
int64_t registration_id,
const std::string& key_prefix,
GetUserDataByKeyPrefixCallback callback) {
storage_->GetUserDataByKeyPrefix(
registration_id, key_prefix,
base::BindOnce(&DidGetUserData, std::move(callback)));
}
void ServiceWorkerStorageControlImpl::GetUserKeysAndDataByKeyPrefix(
int64_t registration_id,
const std::string& key_prefix,
GetUserKeysAndDataByKeyPrefixCallback callback) {
storage_->GetUserKeysAndDataByKeyPrefix(
registration_id, key_prefix,
base::BindOnce(&DidGetKeysAndUserData, std::move(callback)));
}
void ServiceWorkerStorageControlImpl::ClearUserDataByKeyPrefixes(
int64_t registration_id,
const std::vector<std::string>& key_prefixes,
ClearUserDataByKeyPrefixesCallback callback) {
storage_->ClearUserDataByKeyPrefixes(registration_id, key_prefixes,
std::move(callback));
}
} // namespace content } // namespace content
...@@ -80,6 +80,17 @@ class CONTENT_EXPORT ServiceWorkerStorageControlImpl ...@@ -80,6 +80,17 @@ class CONTENT_EXPORT ServiceWorkerStorageControlImpl
void ClearUserData(int64_t registration_id, void ClearUserData(int64_t registration_id,
const std::vector<std::string>& keys, const std::vector<std::string>& keys,
ClearUserDataCallback callback) override; ClearUserDataCallback callback) override;
void GetUserDataByKeyPrefix(int64_t registration_id,
const std::string& key_prefix,
GetUserDataByKeyPrefixCallback callback) override;
void GetUserKeysAndDataByKeyPrefix(
int64_t registration_id,
const std::string& key_prefix,
GetUserKeysAndDataByKeyPrefixCallback callback) override;
void ClearUserDataByKeyPrefixes(
int64_t registration_id,
const std::vector<std::string>& key_prefixes,
ClearUserDataByKeyPrefixesCallback callback) override;
const std::unique_ptr<ServiceWorkerStorage> storage_; const std::unique_ptr<ServiceWorkerStorage> storage_;
}; };
......
...@@ -2,10 +2,13 @@ ...@@ -2,10 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include <string.h>
#include "content/browser/service_worker/service_worker_storage_control_impl.h" #include "content/browser/service_worker/service_worker_storage_control_impl.h"
#include <cstdint>
#include <string>
#include <vector>
#include "base/containers/flat_map.h"
#include "base/containers/span.h" #include "base/containers/span.h"
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/test/bind_test_util.h" #include "base/test/bind_test_util.h"
...@@ -21,6 +24,7 @@ ...@@ -21,6 +24,7 @@
#include "net/test/test_data_directory.h" #include "net/test/test_data_directory.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/service_worker/navigation_preload_state.mojom.h" #include "third_party/blink/public/mojom/service_worker/navigation_preload_state.mojom.h"
#include "url/gurl.h"
namespace content { namespace content {
...@@ -30,6 +34,16 @@ using FindRegistrationResult = ...@@ -30,6 +34,16 @@ using FindRegistrationResult =
namespace { namespace {
struct GetUserDataByKeyPrefixResult {
DatabaseStatus status;
std::vector<std::string> values;
};
struct GetUserKeysAndDataByKeyPrefixResult {
DatabaseStatus status;
base::flat_map<std::string, std::string> user_data;
};
int ReadResponseHead(storage::mojom::ServiceWorkerResourceReader* reader, int ReadResponseHead(storage::mojom::ServiceWorkerResourceReader* reader,
network::mojom::URLResponseHeadPtr& out_response_head, network::mojom::URLResponseHeadPtr& out_response_head,
base::Optional<mojo_base::BigBuffer>& out_metadata) { base::Optional<mojo_base::BigBuffer>& out_metadata) {
...@@ -293,6 +307,56 @@ class ServiceWorkerStorageControlImplTest : public testing::Test { ...@@ -293,6 +307,56 @@ class ServiceWorkerStorageControlImplTest : public testing::Test {
return return_value; return return_value;
} }
GetUserDataByKeyPrefixResult GetUserDataByKeyPrefix(
int64_t registration_id,
const std::string& key_prefix) {
GetUserDataByKeyPrefixResult result;
base::RunLoop loop;
storage()->GetUserDataByKeyPrefix(
registration_id, key_prefix,
base::BindLambdaForTesting(
[&](DatabaseStatus status, const std::vector<std::string>& values) {
result.status = status;
result.values = values;
loop.Quit();
}));
loop.Run();
return result;
}
GetUserKeysAndDataByKeyPrefixResult GetUserKeysAndDataByKeyPrefix(
int64_t registration_id,
const std::string& key_prefix) {
GetUserKeysAndDataByKeyPrefixResult result;
base::RunLoop loop;
storage()->GetUserKeysAndDataByKeyPrefix(
registration_id, key_prefix,
base::BindLambdaForTesting(
[&](DatabaseStatus status,
const base::flat_map<std::string, std::string>& user_data) {
result.status = status;
result.user_data = user_data;
loop.Quit();
}));
loop.Run();
return result;
}
DatabaseStatus ClearUserDataByKeyPrefixes(
int64_t registration_id,
const std::vector<std::string>& key_prefixes) {
DatabaseStatus return_value;
base::RunLoop loop;
storage()->ClearUserDataByKeyPrefixes(
registration_id, key_prefixes,
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,
...@@ -684,4 +748,75 @@ TEST_F(ServiceWorkerStorageControlImplTest, StoreAndGetUserData) { ...@@ -684,4 +748,75 @@ TEST_F(ServiceWorkerStorageControlImplTest, StoreAndGetUserData) {
} }
} }
// Tests that storing/getting user data by key prefix works.
TEST_F(ServiceWorkerStorageControlImplTest, StoreAndGetUserDataByKeyPrefix) {
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 some user data with prefixes.
std::vector<storage::mojom::ServiceWorkerUserDataPtr> user_data;
user_data.push_back(
storage::mojom::ServiceWorkerUserData::New("prefixA", "value1"));
user_data.push_back(
storage::mojom::ServiceWorkerUserData::New("prefixA2", "value2"));
user_data.push_back(
storage::mojom::ServiceWorkerUserData::New("prefixB", "value3"));
user_data.push_back(
storage::mojom::ServiceWorkerUserData::New("prefixC", "value4"));
status =
StoreUserData(registration_id, kScope.GetOrigin(), std::move(user_data));
ASSERT_EQ(status, DatabaseStatus::kOk);
{
GetUserDataByKeyPrefixResult result =
GetUserDataByKeyPrefix(registration_id, "prefix");
ASSERT_EQ(result.status, DatabaseStatus::kOk);
EXPECT_EQ(result.values.size(), 4UL);
EXPECT_EQ(result.values[0], "value1");
EXPECT_EQ(result.values[1], "value2");
EXPECT_EQ(result.values[2], "value3");
EXPECT_EQ(result.values[3], "value4");
}
{
GetUserKeysAndDataByKeyPrefixResult result =
GetUserKeysAndDataByKeyPrefix(registration_id, "prefix");
ASSERT_EQ(result.status, DatabaseStatus::kOk);
ASSERT_EQ(result.user_data.size(), 4UL);
EXPECT_EQ(result.user_data["A"], "value1");
EXPECT_EQ(result.user_data["A2"], "value2");
EXPECT_EQ(result.user_data["B"], "value3");
EXPECT_EQ(result.user_data["C"], "value4");
}
{
GetUserDataByKeyPrefixResult result =
GetUserDataByKeyPrefix(registration_id, "prefixA");
ASSERT_EQ(result.status, DatabaseStatus::kOk);
ASSERT_EQ(result.values.size(), 2UL);
EXPECT_EQ(result.values[0], "value1");
EXPECT_EQ(result.values[1], "value2");
}
status = ClearUserDataByKeyPrefixes(registration_id, {"prefixA", "prefixC"});
ASSERT_EQ(status, DatabaseStatus::kOk);
{
GetUserDataByKeyPrefixResult result =
GetUserDataByKeyPrefix(registration_id, "prefix");
ASSERT_EQ(result.status, DatabaseStatus::kOk);
ASSERT_EQ(result.values.size(), 1UL);
EXPECT_EQ(result.values[0], "value3");
}
}
} // 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