Commit efa6bb89 authored by nhiroki@chromium.org's avatar nhiroki@chromium.org

ServiceWorker: DB functions should return status code instead of boolean (3)

Part1: https://codereview.chromium.org/275103004/
Part2: https://codereview.chromium.org/287843002/

This change makes all remaining functions return status code instead of boolean.


BUG=372704
TEST=should pass all existing tests

Review URL: https://codereview.chromium.org/284123003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@271342 0039d316-1c4b-4281-b951-d872f2087c98
parent 6e9b5e20
...@@ -97,19 +97,43 @@ class CONTENT_EXPORT ServiceWorkerDatabase { ...@@ -97,19 +97,43 @@ class CONTENT_EXPORT ServiceWorkerDatabase {
// (resource ids will be added/removed from the uncommitted/purgeable // (resource ids will be added/removed from the uncommitted/purgeable
// lists as needed) // lists as needed)
bool ReadRegistration(int64 registration_id, // Reads a registration for |registration_id| and resource records associated
// with it from the database. Returns OK if they are successfully read.
// Otherwise, returns an error.
Status ReadRegistration(
int64 registration_id,
const GURL& origin, const GURL& origin,
RegistrationData* registration, RegistrationData* registration,
std::vector<ResourceRecord>* resources); std::vector<ResourceRecord>* resources);
bool WriteRegistration(const RegistrationData& registration,
// Writes |registration| and |resources| into the database and does following
// things:
// - Deletes an old version of the registration if exists.
// - Bumps the next registration id and the next version id if needed.
// - Removes |resources| from the uncommitted list if exist.
// Returns OK they are successfully written. Otherwise, returns an error.
Status WriteRegistration(
const RegistrationData& registration,
const std::vector<ResourceRecord>& resources); const std::vector<ResourceRecord>& resources);
bool UpdateVersionToActive(int64 registration_id, // Updates a registration for |registration_id| to an active state. Returns OK
// if it's successfully updated. Otherwise, returns an error.
Status UpdateVersionToActive(
int64 registration_id,
const GURL& origin); const GURL& origin);
bool UpdateLastCheckTime(int64 registration_id,
// Updates last check time of a registration for |registration_id| by |time|.
// Returns OK if it's successfully updated. Otherwise, returns an error.
Status UpdateLastCheckTime(
int64 registration_id,
const GURL& origin, const GURL& origin,
const base::Time& time); const base::Time& time);
bool DeleteRegistration(int64 registration_id,
// Deletes a registration for |registration_id| and moves resource records
// associated with it into the purgeable list. Returns OK if it's successfully
// deleted or not found in the database. Otherwise, returns an error.
Status DeleteRegistration(
int64 registration_id,
const GURL& origin); const GURL& origin);
// As new resources are put into the diskcache, they go into an uncommitted // As new resources are put into the diskcache, they go into an uncommitted
...@@ -119,33 +143,35 @@ class CONTENT_EXPORT ServiceWorkerDatabase { ...@@ -119,33 +143,35 @@ class CONTENT_EXPORT ServiceWorkerDatabase {
// the purgeable list can be purged from the diskcache. At system startup, all // the purgeable list can be purged from the diskcache. At system startup, all
// uncommitted ids are moved to the purgeable list. // uncommitted ids are moved to the purgeable list.
// Reads uncommitted resource ids from the database. Returns true on success. // Reads uncommitted resource ids from the database. Returns OK on success.
// Otherwise clears |ids| and returns false. // Otherwise clears |ids| and returns an error.
bool GetUncommittedResourceIds(std::set<int64>* ids); Status GetUncommittedResourceIds(std::set<int64>* ids);
// Writes |ids| into the database as uncommitted resources. Returns true on // Writes |ids| into the database as uncommitted resources. Returns OK on
// success. Otherwise writes nothing and returns false. // success. Otherwise writes nothing and returns an error.
bool WriteUncommittedResourceIds(const std::set<int64>& ids); Status WriteUncommittedResourceIds(const std::set<int64>& ids);
// Deletes uncommitted resource ids specified by |ids| from the database. // Deletes uncommitted resource ids specified by |ids| from the database.
// Returns true on success. Otherwise deletes nothing and returns false. // Returns OK on success. Otherwise deletes nothing and returns an error.
bool ClearUncommittedResourceIds(const std::set<int64>& ids); Status ClearUncommittedResourceIds(const std::set<int64>& ids);
// Reads purgeable resource ids from the database. Returns true on success. // Reads purgeable resource ids from the database. Returns OK on success.
// Otherwise clears |ids| and returns false. // Otherwise clears |ids| and returns an error.
bool GetPurgeableResourceIds(std::set<int64>* ids); Status GetPurgeableResourceIds(std::set<int64>* ids);
// Writes |ids| into the database as purgeable resources. Returns true on // Writes |ids| into the database as purgeable resources. Returns OK on
// success. Otherwise writes nothing and returns false. // success. Otherwise writes nothing and returns an error.
bool WritePurgeableResourceIds(const std::set<int64>& ids); Status WritePurgeableResourceIds(const std::set<int64>& ids);
// Deletes purgeable resource ids specified by |ids| from the database. // Deletes purgeable resource ids specified by |ids| from the database.
// Returns true on success. Otherwise deletes nothing and returns false. // Returns OK on success. Otherwise deletes nothing and returns an error.
bool ClearPurgeableResourceIds(const std::set<int64>& ids); Status ClearPurgeableResourceIds(const std::set<int64>& ids);
// Delete all data for |origin|, namely, unique origin, registrations and // Deletes all data for |origin|, namely, unique origin, registrations and
// resource records. Resources are moved to the purgeable list. // resource records. Resources are moved to the purgeable list. Returns OK if
bool DeleteAllDataForOrigin(const GURL& origin); // they are successfully deleted or not found in the database. Otherwise,
// returns an error.
Status DeleteAllDataForOrigin(const GURL& origin);
bool is_disabled() const { return is_disabled_; } bool is_disabled() const { return is_disabled_; }
bool was_corruption_detected() const { return was_corruption_detected_; } bool was_corruption_detected() const { return was_corruption_detected_; }
...@@ -169,39 +195,69 @@ class CONTENT_EXPORT ServiceWorkerDatabase { ...@@ -169,39 +195,69 @@ class CONTENT_EXPORT ServiceWorkerDatabase {
const char* id_key, const char* id_key,
int64* next_avail_id); int64* next_avail_id);
bool ReadRegistrationData(int64 registration_id, // Reads registration data for |registration_id| from the database. Returns OK
// if successfully reads. Otherwise, returns an error.
Status ReadRegistrationData(
int64 registration_id,
const GURL& origin, const GURL& origin,
RegistrationData* registration); RegistrationData* registration);
bool ReadResourceRecords(int64 version_id,
// Reads resource records for |version_id| from the database. Returns OK if
// it's successfully read or not found in the database. Otherwise, returns an
// error.
Status ReadResourceRecords(
int64 version_id,
std::vector<ResourceRecord>* resources); std::vector<ResourceRecord>* resources);
bool DeleteResourceRecords(int64 version_id,
// Deletes resource records for |version_id| from the database. Returns OK if
// they are successfully deleted or not found in the database. Otherwise,
// returns an error.
Status DeleteResourceRecords(
int64 version_id,
leveldb::WriteBatch* batch); leveldb::WriteBatch* batch);
bool ReadResourceIds(const char* id_key_prefix,
// Reads resource ids for |id_key_prefix| from the database. Returns OK if
// it's successfully read or not found in the database. Otherwise, returns an
// error.
Status ReadResourceIds(
const char* id_key_prefix,
std::set<int64>* ids); std::set<int64>* ids);
bool WriteResourceIds(const char* id_key_prefix,
// Write resource ids for |id_key_prefix| into the database. Returns OK on
// success. Otherwise, returns writes nothing and returns an error.
Status WriteResourceIds(
const char* id_key_prefix,
const std::set<int64>& ids); const std::set<int64>& ids);
bool DeleteResourceIds(const char* id_key_prefix,
// Deletes resource ids for |id_key_prefix| from the database. Returns OK if
// it's successfully deleted or not found in the database. Otherwise, returns
// an error.
Status DeleteResourceIds(
const char* id_key_prefix,
const std::set<int64>& ids); const std::set<int64>& ids);
// Reads the current schema version from the database. If the database hasn't // Reads the current schema version from the database. If the database hasn't
// been written anything yet, sets |db_version| to 0 and returns OK. // been written anything yet, sets |db_version| to 0 and returns OK.
Status ReadDatabaseVersion(int64* db_version); Status ReadDatabaseVersion(int64* db_version);
// Write a batch into the database. // Writes a batch into the database.
// NOTE: You must call this when you want to put something into the database // NOTE: You must call this when you want to put something into the database
// because this initializes the database if needed. // because this initializes the database if needed.
bool WriteBatch(leveldb::WriteBatch* batch); Status WriteBatch(leveldb::WriteBatch* batch);
// Bumps the next available id if |used_id| is greater than or equal to the // Bumps the next available id if |used_id| is greater than or equal to the
// cached one. // cached one.
void BumpNextRegistrationIdIfNeeded(int64 used_id, void BumpNextRegistrationIdIfNeeded(
int64 used_id,
leveldb::WriteBatch* batch); leveldb::WriteBatch* batch);
void BumpNextVersionIdIfNeeded(int64 used_id, void BumpNextVersionIdIfNeeded(
int64 used_id,
leveldb::WriteBatch* batch); leveldb::WriteBatch* batch);
bool IsOpen(); bool IsOpen();
void HandleError(const tracked_objects::Location& from_here, void HandleError(
const tracked_objects::Location& from_here,
const leveldb::Status& status); const leveldb::Status& status);
base::FilePath path_; base::FilePath path_;
......
...@@ -100,28 +100,6 @@ void ReadInitialDataFromDB( ...@@ -100,28 +100,6 @@ void ReadInitialDataFromDB(
FROM_HERE, base::Bind(callback, base::Owned(data.release()), status)); FROM_HERE, base::Bind(callback, base::Owned(data.release()), status));
} }
void ReadRegistrationFromDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
int64 registration_id,
const GURL& origin,
const ReadRegistrationCallback& callback) {
DCHECK(database);
ServiceWorkerDatabase::RegistrationData data;
std::vector<ServiceWorkerDatabase::ResourceRecord> resources;
// TODO(nhiroki): The database should return more detailed status like
// ServiceWorkerStatusCode instead of bool value.
ServiceWorkerDatabase::Status status = ServiceWorkerDatabase::STATUS_OK;
if (!database->ReadRegistration(registration_id, origin, &data, &resources)) {
status = database->is_disabled()
? ServiceWorkerDatabase::STATUS_ERROR_FAILED
: ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND;
}
original_task_runner->PostTask(
FROM_HERE, base::Bind(callback, data, resources, status));
}
void DeleteRegistrationFromDB( void DeleteRegistrationFromDB(
ServiceWorkerDatabase* database, ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner, scoped_refptr<base::SequencedTaskRunner> original_task_runner,
...@@ -129,18 +107,18 @@ void DeleteRegistrationFromDB( ...@@ -129,18 +107,18 @@ void DeleteRegistrationFromDB(
const GURL& origin, const GURL& origin,
const DeleteRegistrationCallback& callback) { const DeleteRegistrationCallback& callback) {
DCHECK(database); DCHECK(database);
if (!database->DeleteRegistration(registration_id, origin)) { ServiceWorkerDatabase::Status status =
database->DeleteRegistration(registration_id, origin);
if (status != ServiceWorkerDatabase::STATUS_OK) {
original_task_runner->PostTask( original_task_runner->PostTask(
FROM_HERE, base::Bind(callback, false, FROM_HERE, base::Bind(callback, false, status));
ServiceWorkerDatabase::STATUS_ERROR_FAILED));
return; return;
} }
// TODO(nhiroki): Add convenient method to ServiceWorkerDatabase to check the // TODO(nhiroki): Add convenient method to ServiceWorkerDatabase to check the
// unique origin list. // unique origin list.
std::vector<ServiceWorkerDatabase::RegistrationData> registrations; std::vector<ServiceWorkerDatabase::RegistrationData> registrations;
ServiceWorkerDatabase::Status status = status = database->GetRegistrationsForOrigin(origin, &registrations);
database->GetRegistrationsForOrigin(origin, &registrations);
if (status != ServiceWorkerDatabase::STATUS_OK) { if (status != ServiceWorkerDatabase::STATUS_OK) {
original_task_runner->PostTask( original_task_runner->PostTask(
FROM_HERE, base::Bind(callback, false, status)); FROM_HERE, base::Bind(callback, false, status));
...@@ -149,26 +127,7 @@ void DeleteRegistrationFromDB( ...@@ -149,26 +127,7 @@ void DeleteRegistrationFromDB(
bool deletable = registrations.empty(); bool deletable = registrations.empty();
original_task_runner->PostTask( original_task_runner->PostTask(
FROM_HERE, base::Bind(callback, deletable, FROM_HERE, base::Bind(callback, deletable, status));
ServiceWorkerDatabase::STATUS_OK));
}
void UpdateToActiveStateInDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
int64 registration_id,
const GURL& origin,
const ServiceWorkerStorage::StatusCallback& callback) {
DCHECK(database);
// TODO(nhiroki): The database should return more detailed status like
// ServiceWorkerStatusCode instead of bool value.
ServiceWorkerStatusCode status = SERVICE_WORKER_OK;
if (!database->UpdateVersionToActive(registration_id, origin)) {
status = database->is_disabled() ? SERVICE_WORKER_ERROR_FAILED
: SERVICE_WORKER_ERROR_NOT_FOUND;
}
original_task_runner->PostTask(FROM_HERE, base::Bind(callback, status));
} }
} // namespace } // namespace
...@@ -327,14 +286,20 @@ void ServiceWorkerStorage::FindRegistrationForId( ...@@ -327,14 +286,20 @@ void ServiceWorkerStorage::FindRegistrationForId(
return; return;
} }
database_task_runner_->PostTask( ServiceWorkerDatabase::RegistrationData* data =
new ServiceWorkerDatabase::RegistrationData;
ResourceList* resources = new ResourceList;
PostTaskAndReplyWithResult(
database_task_runner_,
FROM_HERE, FROM_HERE,
base::Bind(&ReadRegistrationFromDB, base::Bind(&ServiceWorkerDatabase::ReadRegistration,
database_.get(), base::Unretained(database_.get()),
base::MessageLoopProxy::current(),
registration_id, origin, registration_id, origin,
base::Unretained(data),
base::Unretained(resources)),
base::Bind(&ServiceWorkerStorage::DidReadRegistrationForId, base::Bind(&ServiceWorkerStorage::DidReadRegistrationForId,
weak_factory_.GetWeakPtr(), callback))); weak_factory_.GetWeakPtr(),
callback, base::Owned(data), base::Owned(resources)));
} }
void ServiceWorkerStorage::GetAllRegistrations( void ServiceWorkerStorage::GetAllRegistrations(
...@@ -408,13 +373,15 @@ void ServiceWorkerStorage::UpdateToActiveState( ...@@ -408,13 +373,15 @@ void ServiceWorkerStorage::UpdateToActiveState(
return; return;
} }
database_task_runner_->PostTask( PostTaskAndReplyWithResult(
database_task_runner_,
FROM_HERE, FROM_HERE,
base::Bind(&UpdateToActiveStateInDB, base::Bind(&ServiceWorkerDatabase::UpdateVersionToActive,
database_.get(), base::Unretained(database_.get()),
base::MessageLoopProxy::current(),
registration->id(), registration->id(),
registration->script_url().GetOrigin(), registration->script_url().GetOrigin()),
base::Bind(&ServiceWorkerStorage::DidUpdateToActiveState,
weak_factory_.GetWeakPtr(),
callback)); callback));
} }
...@@ -624,18 +591,21 @@ void ServiceWorkerStorage::DidGetRegistrationsForDocument( ...@@ -624,18 +591,21 @@ void ServiceWorkerStorage::DidGetRegistrationsForDocument(
void ServiceWorkerStorage::DidReadRegistrationForId( void ServiceWorkerStorage::DidReadRegistrationForId(
const FindRegistrationCallback& callback, const FindRegistrationCallback& callback,
const ServiceWorkerDatabase::RegistrationData& registration, ServiceWorkerDatabase::RegistrationData* registration,
const ResourceList& resources, ResourceList* resources,
ServiceWorkerDatabase::Status status) { ServiceWorkerDatabase::Status status) {
DCHECK(registration);
DCHECK(resources);
if (status == ServiceWorkerDatabase::STATUS_OK) { if (status == ServiceWorkerDatabase::STATUS_OK) {
callback.Run(SERVICE_WORKER_OK, CreateRegistration(registration)); callback.Run(SERVICE_WORKER_OK, CreateRegistration(*registration));
return; return;
} }
if (status == ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) { if (status == ServiceWorkerDatabase::STATUS_ERROR_NOT_FOUND) {
// Look for somthing currently being installed. // Look for somthing currently being installed.
scoped_refptr<ServiceWorkerRegistration> installing_registration = scoped_refptr<ServiceWorkerRegistration> installing_registration =
FindInstallingRegistrationForId(registration.registration_id); FindInstallingRegistrationForId(registration->registration_id);
if (installing_registration) { if (installing_registration) {
callback.Run(SERVICE_WORKER_OK, installing_registration); callback.Run(SERVICE_WORKER_OK, installing_registration);
return; return;
...@@ -645,6 +615,7 @@ void ServiceWorkerStorage::DidReadRegistrationForId( ...@@ -645,6 +615,7 @@ void ServiceWorkerStorage::DidReadRegistrationForId(
return; return;
} }
// TODO(nhiroki): Handle database error (http://crbug.com/371675).
callback.Run(DatabaseStatusToStatusCode(status), callback.Run(DatabaseStatusToStatusCode(status),
scoped_refptr<ServiceWorkerRegistration>()); scoped_refptr<ServiceWorkerRegistration>());
return; return;
...@@ -699,23 +670,36 @@ void ServiceWorkerStorage::DidGetAllRegistrations( ...@@ -699,23 +670,36 @@ void ServiceWorkerStorage::DidGetAllRegistrations(
void ServiceWorkerStorage::DidStoreRegistration( void ServiceWorkerStorage::DidStoreRegistration(
const GURL& origin, const GURL& origin,
const StatusCallback& callback, const StatusCallback& callback,
bool success) { ServiceWorkerDatabase::Status status) {
if (!success) { if (status != ServiceWorkerDatabase::STATUS_OK) {
callback.Run(SERVICE_WORKER_ERROR_FAILED); // TODO(nhiroki): Handle database error (http://crbug.com/371675).
callback.Run(DatabaseStatusToStatusCode(status));
return; return;
} }
registered_origins_.insert(origin); registered_origins_.insert(origin);
callback.Run(SERVICE_WORKER_OK); callback.Run(SERVICE_WORKER_OK);
} }
void ServiceWorkerStorage::DidUpdateToActiveState(
const StatusCallback& callback,
ServiceWorkerDatabase::Status status) {
// TODO(nhiroki): Handle database error (http://crbug.com/371675).
callback.Run(DatabaseStatusToStatusCode(status));
}
void ServiceWorkerStorage::DidDeleteRegistration( void ServiceWorkerStorage::DidDeleteRegistration(
const GURL& origin, const GURL& origin,
const StatusCallback& callback, const StatusCallback& callback,
bool origin_is_deletable, bool origin_is_deletable,
ServiceWorkerDatabase::Status status) { ServiceWorkerDatabase::Status status) {
if (status != ServiceWorkerDatabase::STATUS_OK) {
// TODO(nhiroki): Handle database error (http://crbug.com/371675).
callback.Run(DatabaseStatusToStatusCode(status));
return;
}
if (origin_is_deletable) if (origin_is_deletable)
registered_origins_.erase(origin); registered_origins_.erase(origin);
callback.Run(DatabaseStatusToStatusCode(status)); callback.Run(SERVICE_WORKER_OK);
} }
scoped_refptr<ServiceWorkerRegistration> scoped_refptr<ServiceWorkerRegistration>
......
...@@ -151,8 +151,8 @@ class CONTENT_EXPORT ServiceWorkerStorage { ...@@ -151,8 +151,8 @@ class CONTENT_EXPORT ServiceWorkerStorage {
ServiceWorkerDatabase::Status status); ServiceWorkerDatabase::Status status);
void DidReadRegistrationForId( void DidReadRegistrationForId(
const FindRegistrationCallback& callback, const FindRegistrationCallback& callback,
const ServiceWorkerDatabase::RegistrationData& registration, ServiceWorkerDatabase::RegistrationData* registration,
const ResourceList& resources, ResourceList* resources,
ServiceWorkerDatabase::Status status); ServiceWorkerDatabase::Status status);
void DidGetAllRegistrations( void DidGetAllRegistrations(
const GetAllRegistrationInfosCallback& callback, const GetAllRegistrationInfosCallback& callback,
...@@ -161,7 +161,10 @@ class CONTENT_EXPORT ServiceWorkerStorage { ...@@ -161,7 +161,10 @@ class CONTENT_EXPORT ServiceWorkerStorage {
void DidStoreRegistration( void DidStoreRegistration(
const GURL& origin, const GURL& origin,
const StatusCallback& callback, const StatusCallback& callback,
bool success); ServiceWorkerDatabase::Status status);
void DidUpdateToActiveState(
const StatusCallback& callback,
ServiceWorkerDatabase::Status status);
void DidDeleteRegistration( void DidDeleteRegistration(
const GURL& origin, const GURL& origin,
const StatusCallback& callback, const StatusCallback& callback,
......
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