Commit 88bb6375 authored by falken's avatar falken Committed by Commit bot

Make ServiceWorkerStorage's UserData functions call LazyInitialize

The callers can have a registration id even before storage is initialized,
for example if registration ids are stored in preferences.

BUG=672716

Review-Url: https://codereview.chromium.org/2569353003
Cr-Commit-Position: refs/heads/master@{#438748}
parent f824367e
......@@ -627,11 +627,14 @@ void ServiceWorkerStorage::StoreUserData(
const GURL& origin,
const std::vector<std::pair<std::string, std::string>>& key_value_pairs,
const StatusCallback& callback) {
DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_;
if (IsDisabled()) {
if (!LazyInitialize(base::Bind(&ServiceWorkerStorage::StoreUserData,
weak_factory_.GetWeakPtr(), registration_id,
origin, key_value_pairs, callback))) {
if (state_ != INITIALIZING)
RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_ABORT));
return;
}
DCHECK_EQ(INITIALIZED, state_);
if (registration_id == kInvalidServiceWorkerRegistrationId ||
key_value_pairs.empty()) {
......@@ -657,12 +660,16 @@ void ServiceWorkerStorage::StoreUserData(
void ServiceWorkerStorage::GetUserData(int64_t registration_id,
const std::vector<std::string>& keys,
const GetUserDataCallback& callback) {
DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_;
if (IsDisabled()) {
if (!LazyInitialize(base::Bind(&ServiceWorkerStorage::GetUserData,
weak_factory_.GetWeakPtr(), registration_id,
keys, callback))) {
if (state_ != INITIALIZING) {
RunSoon(FROM_HERE, base::Bind(callback, std::vector<std::string>(),
SERVICE_WORKER_ERROR_ABORT));
}
return;
}
DCHECK_EQ(INITIALIZED, state_);
if (registration_id == kInvalidServiceWorkerRegistrationId || keys.empty()) {
RunSoon(FROM_HERE, base::Bind(callback, std::vector<std::string>(),
......@@ -688,7 +695,15 @@ void ServiceWorkerStorage::GetUserData(int64_t registration_id,
void ServiceWorkerStorage::ClearUserData(int64_t registration_id,
const std::vector<std::string>& keys,
const StatusCallback& callback) {
DCHECK(state_ == INITIALIZED || state_ == DISABLED) << state_;
if (!LazyInitialize(base::Bind(&ServiceWorkerStorage::ClearUserData,
weak_factory_.GetWeakPtr(), registration_id,
keys, callback))) {
if (state_ != INITIALIZING)
RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_ABORT));
return;
}
DCHECK_EQ(INITIALIZED, state_);
if (IsDisabled()) {
RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_ABORT));
return;
......
......@@ -1096,6 +1096,35 @@ TEST_P(ServiceWorkerStorageTestP, StoreUserData) {
GetUserDataForAllRegistrations(std::string(), &data_list_out));
}
// The *_BeforeInitialize tests exercise the API before LazyInitialize() is
// called.
TEST_P(ServiceWorkerStorageTestP, StoreUserData_BeforeInitialize) {
const int kRegistrationId = 0;
EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
StoreUserData(kRegistrationId, GURL("https://example.com"),
{{"key", "data"}}));
}
TEST_P(ServiceWorkerStorageTestP, GetUserData_BeforeInitialize) {
const int kRegistrationId = 0;
std::vector<std::string> data_out;
EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND,
GetUserData(kRegistrationId, {"key"}, &data_out));
}
TEST_P(ServiceWorkerStorageTestP, ClearUserData_BeforeInitialize) {
const int kRegistrationId = 0;
EXPECT_EQ(SERVICE_WORKER_OK, ClearUserData(kRegistrationId, {"key"}));
}
TEST_P(ServiceWorkerStorageTestP,
GetUserDataForAllRegistrations_BeforeInitialize) {
std::vector<std::pair<int64_t, std::string>> data_list_out;
EXPECT_EQ(SERVICE_WORKER_OK,
GetUserDataForAllRegistrations("key", &data_list_out));
EXPECT_TRUE(data_list_out.empty());
}
class ServiceWorkerResourceStorageTest : public ServiceWorkerStorageTestP {
public:
void SetUp() override {
......
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