Commit 93178c5d authored by iclelland's avatar iclelland Committed by Commit bot

[Background Sync] Add proper error status to API calls and callbacks

BUG=484306

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

Cr-Commit-Position: refs/heads/master@{#329625}
parent 9b3ffc87
......@@ -93,7 +93,7 @@ void BackgroundSyncProvider::getRegistration(
GetBackgroundSyncServicePtr()->GetRegistration(
mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), tag.utf8(),
service_worker_registration_id,
base::Bind(&BackgroundSyncProvider::RegisterCallback,
base::Bind(&BackgroundSyncProvider::GetRegistrationCallback,
base::Unretained(this), base::Passed(callbacksPtr.Pass())));
}
......@@ -118,42 +118,117 @@ void BackgroundSyncProvider::getRegistrations(
void BackgroundSyncProvider::RegisterCallback(
scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks,
BackgroundSyncError error,
const SyncRegistrationPtr& options) {
// TODO(iclelland): Pass through the various errors from the manager to here
// and handle them appropriately.
// TODO(iclelland): Determine the correct error message to return in each case
scoped_ptr<blink::WebSyncRegistration> result;
if (!options.is_null())
result = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options);
callbacks->onSuccess(result.release());
switch (error) {
case BACKGROUND_SYNC_ERROR_NONE:
if (!options.is_null())
result =
mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options);
callbacks->onSuccess(result.release());
break;
case BACKGROUND_SYNC_ERROR_NOT_FOUND:
NOTREACHED();
break;
case BACKGROUND_SYNC_ERROR_STORAGE:
callbacks->onError(
new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
"Background Sync is disabled."));
break;
case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
callbacks->onError(
new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
"No service worker is active."));
break;
}
}
void BackgroundSyncProvider::UnregisterCallback(
scoped_ptr<blink::WebSyncUnregistrationCallbacks> callbacks,
bool success) {
// TODO(iclelland): Pass through the various errors from the manager to here
// and handle them appropriately.
if (success) {
callbacks->onSuccess(new bool(success));
} else {
callbacks->onError(
new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
"Sync registration does not exist"));
BackgroundSyncError error) {
// TODO(iclelland): Determine the correct error message to return in each case
switch (error) {
case BACKGROUND_SYNC_ERROR_NONE:
callbacks->onSuccess(new bool(true));
break;
case BACKGROUND_SYNC_ERROR_NOT_FOUND:
callbacks->onSuccess(new bool(false));
break;
case BACKGROUND_SYNC_ERROR_STORAGE:
callbacks->onError(
new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
"Background Sync is disabled."));
break;
case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
callbacks->onError(
new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
"No service worker is active."));
break;
}
}
void BackgroundSyncProvider::GetRegistrationCallback(
scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks,
BackgroundSyncError error,
const SyncRegistrationPtr& options) {
// TODO(iclelland): Determine the correct error message to return in each case
scoped_ptr<blink::WebSyncRegistration> result;
switch (error) {
case BACKGROUND_SYNC_ERROR_NONE:
if (!options.is_null())
result =
mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options);
callbacks->onSuccess(result.release());
break;
case BACKGROUND_SYNC_ERROR_NOT_FOUND:
callbacks->onSuccess(nullptr);
break;
case BACKGROUND_SYNC_ERROR_STORAGE:
callbacks->onError(
new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
"Background Sync is disabled."));
break;
case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
callbacks->onError(
new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
"No service worker is active."));
break;
}
}
void BackgroundSyncProvider::GetRegistrationsCallback(
scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacks,
BackgroundSyncError error,
const mojo::Array<SyncRegistrationPtr>& registrations) {
// TODO(iclelland): Pass through the various errors from the manager to here
// and handle them appropriately.
blink::WebVector<blink::WebSyncRegistration*>* results =
new blink::WebVector<blink::WebSyncRegistration*>(registrations.size());
for (size_t i = 0; i < registrations.size(); ++i) {
(*results)[i] = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(
registrations[i]).release();
// TODO(iclelland): Determine the correct error message to return in each case
blink::WebVector<blink::WebSyncRegistration*>* results;
switch (error) {
case BACKGROUND_SYNC_ERROR_NONE:
results = new blink::WebVector<blink::WebSyncRegistration*>(
registrations.size());
for (size_t i = 0; i < registrations.size(); ++i) {
(*results)[i] = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(
registrations[i]).release();
}
callbacks->onSuccess(results);
case BACKGROUND_SYNC_ERROR_NOT_FOUND:
// This error should never be returned from
// BackgroundSyncManager::GetRegistrations
NOTREACHED();
break;
case BACKGROUND_SYNC_ERROR_STORAGE:
callbacks->onError(
new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
"Background Sync is disabled."));
break;
case BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER:
callbacks->onError(
new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
"No service worker is active."));
break;
}
callbacks->onSuccess(results);
}
BackgroundSyncServicePtr&
......
......@@ -52,12 +52,18 @@ class BackgroundSyncProvider : public blink::WebSyncProvider {
// Callback handlers
void RegisterCallback(
scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks,
BackgroundSyncError error,
const SyncRegistrationPtr& options);
void UnregisterCallback(
scoped_ptr<blink::WebSyncUnregistrationCallbacks> callbacks,
bool success);
BackgroundSyncError error);
void GetRegistrationCallback(
scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks,
BackgroundSyncError error,
const SyncRegistrationPtr& options);
void GetRegistrationsCallback(
scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacks,
BackgroundSyncError error,
const mojo::Array<SyncRegistrationPtr>& registrations);
// Helper method that returns an initialized BackgroundSyncServicePtr.
......
......@@ -6,17 +6,25 @@ module content;
import "content/public/common/background_sync.mojom";
enum BackgroundSyncError {
NONE,
STORAGE,
NOT_FOUND,
NO_SERVICE_WORKER,
MAX=NO_SERVICE_WORKER
};
interface BackgroundSyncService {
Register(SyncRegistration options, int64 service_worker_registration_id)
=> (SyncRegistration options);
=> (BackgroundSyncError err, SyncRegistration options);
GetRegistration(BackgroundSyncPeriodicity periodicity, string tag,
int64 service_worker_registration_id)
=> (SyncRegistration? registration);
=> (BackgroundSyncError err, SyncRegistration? registration);
GetRegistrations(BackgroundSyncPeriodicity periodicity,
int64 service_worker_registration_id)
=> (array<SyncRegistration> registrations);
=> (BackgroundSyncError err, array<SyncRegistration> registrations);
Unregister(BackgroundSyncPeriodicity periodicity, int64 id, string tag,
int64 service_worker_registration_id) => (bool success);
int64 service_worker_registration_id) => (BackgroundSyncError err);
};
interface BackgoundSyncServiceClient {
......
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