Commit 0f378fd1 authored by Mugdha Lakhani's avatar Mugdha Lakhani Committed by Commit Bot

[Background Fetch] Part 1: Implement BackgroundFetchFailureReason.

This is step 1, everything needed to support this on the browser side.

Bug: 876691, 869918
Change-Id: I4c161ae404e24caae9d09b7ea4709a6eb161a95e
Reviewed-on: https://chromium-review.googlesource.com/1185006
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Reviewed-by: default avatarRayan Kanso <rayankans@chromium.org>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#585835}
parent 3cbe9284
...@@ -11,13 +11,26 @@ package content.proto; ...@@ -11,13 +11,26 @@ package content.proto;
// Stores per-registration (as opposed to per-request) data. // Stores per-registration (as opposed to per-request) data.
// https://wicg.github.io/background-fetch/#background-fetch-registration // https://wicg.github.io/background-fetch/#background-fetch-registration
// //
// Next Tag: 8 // Next Tag: 9
message BackgroundFetchRegistration { message BackgroundFetchRegistration {
enum BackgroundFetchState { enum BackgroundFetchState {
PENDING = 0; // Default value. PENDING = 0; // Default value.
FAILURE = 1; FAILURE = 1;
SUCCESS = 2; SUCCESS = 2;
} }
// This should be kept in sync with blink.mojom.BackgroundFetchFailureReason.
enum BackgroundFetchFailureReason {
NONE = 0; // Default value.
CANCELLED_FROM_UI = 1;
CANCELLED_BY_DEVELOPER = 2;
BAD_STATUS = 3;
FETCH_ERROR = 4;
SERVICE_WORKER_UNAVAILABLE = 5;
QUOTA_EXCEEDED = 6;
TOTAL_DOWNLOAD_SIZE_EXCEEDED = 7;
}
// See definition of |unique_id| in BackgroundFetchRegistrationId. // See definition of |unique_id| in BackgroundFetchRegistrationId.
optional string unique_id = 1; optional string unique_id = 1;
...@@ -29,6 +42,7 @@ message BackgroundFetchRegistration { ...@@ -29,6 +42,7 @@ message BackgroundFetchRegistration {
optional uint64 download_total = 5; optional uint64 download_total = 5;
optional uint64 downloaded = 6; optional uint64 downloaded = 6;
optional BackgroundFetchState state = 7; optional BackgroundFetchState state = 7;
optional BackgroundFetchFailureReason failure_reason = 8;
} }
// Developer provided options. // Developer provided options.
......
...@@ -25,6 +25,7 @@ void BackgroundFetchEmbeddedWorkerTestHelper::OnBackgroundFetchAbortEvent( ...@@ -25,6 +25,7 @@ void BackgroundFetchEmbeddedWorkerTestHelper::OnBackgroundFetchAbortEvent(
last_developer_id_ = registration.developer_id; last_developer_id_ = registration.developer_id;
last_unique_id_ = registration.unique_id; last_unique_id_ = registration.unique_id;
last_state_ = registration.state; last_state_ = registration.state;
last_failure_reason_ = registration.failure_reason;
if (fail_abort_event_) { if (fail_abort_event_) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::REJECTED, std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::REJECTED,
...@@ -44,6 +45,7 @@ void BackgroundFetchEmbeddedWorkerTestHelper::OnBackgroundFetchClickEvent( ...@@ -44,6 +45,7 @@ void BackgroundFetchEmbeddedWorkerTestHelper::OnBackgroundFetchClickEvent(
last_developer_id_ = registration.developer_id; last_developer_id_ = registration.developer_id;
last_unique_id_ = registration.unique_id; last_unique_id_ = registration.unique_id;
last_state_ = registration.state; last_state_ = registration.state;
last_failure_reason_ = registration.failure_reason;
if (fail_click_event_) { if (fail_click_event_) {
std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::REJECTED, std::move(callback).Run(blink::mojom::ServiceWorkerEventStatus::REJECTED,
...@@ -64,6 +66,7 @@ void BackgroundFetchEmbeddedWorkerTestHelper::OnBackgroundFetchFailEvent( ...@@ -64,6 +66,7 @@ void BackgroundFetchEmbeddedWorkerTestHelper::OnBackgroundFetchFailEvent(
last_developer_id_ = registration.developer_id; last_developer_id_ = registration.developer_id;
last_unique_id_ = registration.unique_id; last_unique_id_ = registration.unique_id;
last_state_ = registration.state; last_state_ = registration.state;
last_failure_reason_ = registration.failure_reason;
last_fetches_ = fetches; last_fetches_ = fetches;
if (fail_fetch_fail_event_) { if (fail_fetch_fail_event_) {
...@@ -86,6 +89,7 @@ void BackgroundFetchEmbeddedWorkerTestHelper::OnBackgroundFetchSuccessEvent( ...@@ -86,6 +89,7 @@ void BackgroundFetchEmbeddedWorkerTestHelper::OnBackgroundFetchSuccessEvent(
last_developer_id_ = registration.developer_id; last_developer_id_ = registration.developer_id;
last_unique_id_ = registration.unique_id; last_unique_id_ = registration.unique_id;
last_state_ = registration.state; last_state_ = registration.state;
last_failure_reason_ = registration.failure_reason;
last_fetches_ = fetches; last_fetches_ = fetches;
if (fail_fetched_event_) { if (fail_fetched_event_) {
......
...@@ -55,6 +55,10 @@ class BackgroundFetchEmbeddedWorkerTestHelper ...@@ -55,6 +55,10 @@ class BackgroundFetchEmbeddedWorkerTestHelper
const base::Optional<blink::mojom::BackgroundFetchState>& last_state() const { const base::Optional<blink::mojom::BackgroundFetchState>& last_state() const {
return last_state_; return last_state_;
} }
const base::Optional<blink::mojom::BackgroundFetchFailureReason>&
last_failure_reason() const {
return last_failure_reason_;
}
const base::Optional<std::vector<BackgroundFetchSettledFetch>> last_fetches() const base::Optional<std::vector<BackgroundFetchSettledFetch>> last_fetches()
const { const {
return last_fetches_; return last_fetches_;
...@@ -95,6 +99,8 @@ class BackgroundFetchEmbeddedWorkerTestHelper ...@@ -95,6 +99,8 @@ class BackgroundFetchEmbeddedWorkerTestHelper
base::Optional<std::string> last_developer_id_; base::Optional<std::string> last_developer_id_;
base::Optional<std::string> last_unique_id_; base::Optional<std::string> last_unique_id_;
base::Optional<blink::mojom::BackgroundFetchState> last_state_; base::Optional<blink::mojom::BackgroundFetchState> last_state_;
base::Optional<blink::mojom::BackgroundFetchFailureReason>
last_failure_reason_;
base::Optional<std::vector<BackgroundFetchSettledFetch>> last_fetches_; base::Optional<std::vector<BackgroundFetchSettledFetch>> last_fetches_;
DISALLOW_COPY_AND_ASSIGN(BackgroundFetchEmbeddedWorkerTestHelper); DISALLOW_COPY_AND_ASSIGN(BackgroundFetchEmbeddedWorkerTestHelper);
......
...@@ -48,7 +48,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchInvalidRegistration) { ...@@ -48,7 +48,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchInvalidRegistration) {
auto registration = CreateBackgroundFetchRegistration( auto registration = CreateBackgroundFetchRegistration(
invalid_registration_id.developer_id(), invalid_registration_id.developer_id(),
invalid_registration_id.unique_id(), invalid_registration_id.unique_id(),
blink::mojom::BackgroundFetchState::FAILURE); blink::mojom::BackgroundFetchState::FAILURE,
blink::mojom::BackgroundFetchFailureReason::QUOTA_EXCEEDED);
event_dispatcher_.DispatchBackgroundFetchAbortEvent( event_dispatcher_.DispatchBackgroundFetchAbortEvent(
invalid_registration_id, std::move(registration), run_loop.QuitClosure()); invalid_registration_id, std::move(registration), run_loop.QuitClosure());
...@@ -78,7 +79,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchAbortEvent) { ...@@ -78,7 +79,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchAbortEvent) {
base::RunLoop run_loop; base::RunLoop run_loop;
auto registration = CreateBackgroundFetchRegistration( auto registration = CreateBackgroundFetchRegistration(
kExampleDeveloperId, kExampleUniqueId, kExampleDeveloperId, kExampleUniqueId,
blink::mojom::BackgroundFetchState::FAILURE); blink::mojom::BackgroundFetchState::FAILURE,
blink::mojom::BackgroundFetchFailureReason::CANCELLED_FROM_UI);
event_dispatcher_.DispatchBackgroundFetchAbortEvent( event_dispatcher_.DispatchBackgroundFetchAbortEvent(
registration_id, std::move(registration), run_loop.QuitClosure()); registration_id, std::move(registration), run_loop.QuitClosure());
...@@ -91,6 +93,9 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchAbortEvent) { ...@@ -91,6 +93,9 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchAbortEvent) {
ASSERT_TRUE(embedded_worker_test_helper()->last_unique_id().has_value()); ASSERT_TRUE(embedded_worker_test_helper()->last_unique_id().has_value());
EXPECT_EQ(kExampleUniqueId, EXPECT_EQ(kExampleUniqueId,
embedded_worker_test_helper()->last_unique_id().value()); embedded_worker_test_helper()->last_unique_id().value());
ASSERT_TRUE(embedded_worker_test_helper()->last_failure_reason().has_value());
EXPECT_EQ(blink::mojom::BackgroundFetchFailureReason::CANCELLED_FROM_UI,
embedded_worker_test_helper()->last_failure_reason());
histogram_tester_.ExpectUniqueSample( histogram_tester_.ExpectUniqueSample(
"BackgroundFetch.EventDispatchResult.AbortEvent", "BackgroundFetch.EventDispatchResult.AbortEvent",
...@@ -106,7 +111,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchAbortEvent) { ...@@ -106,7 +111,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchAbortEvent) {
base::RunLoop run_loop; base::RunLoop run_loop;
auto registration = CreateBackgroundFetchRegistration( auto registration = CreateBackgroundFetchRegistration(
kExampleDeveloperId2, kExampleUniqueId2, kExampleDeveloperId2, kExampleUniqueId2,
blink::mojom::BackgroundFetchState::FAILURE); blink::mojom::BackgroundFetchState::FAILURE,
blink::mojom::BackgroundFetchFailureReason::QUOTA_EXCEEDED);
event_dispatcher_.DispatchBackgroundFetchAbortEvent(second_registration_id, event_dispatcher_.DispatchBackgroundFetchAbortEvent(second_registration_id,
std::move(registration), std::move(registration),
run_loop.QuitClosure()); run_loop.QuitClosure());
...@@ -145,7 +151,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchClickEvent) { ...@@ -145,7 +151,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchClickEvent) {
base::RunLoop run_loop; base::RunLoop run_loop;
auto registration = CreateBackgroundFetchRegistration( auto registration = CreateBackgroundFetchRegistration(
kExampleDeveloperId, kExampleUniqueId, kExampleDeveloperId, kExampleUniqueId,
blink::mojom::BackgroundFetchState::PENDING); blink::mojom::BackgroundFetchState::PENDING,
blink::mojom::BackgroundFetchFailureReason::NONE);
event_dispatcher_.DispatchBackgroundFetchClickEvent( event_dispatcher_.DispatchBackgroundFetchClickEvent(
registration_id, std::move(registration), run_loop.QuitClosure()); registration_id, std::move(registration), run_loop.QuitClosure());
...@@ -173,7 +180,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchClickEvent) { ...@@ -173,7 +180,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchClickEvent) {
base::RunLoop run_loop; base::RunLoop run_loop;
auto registration = CreateBackgroundFetchRegistration( auto registration = CreateBackgroundFetchRegistration(
kExampleDeveloperId2, kExampleUniqueId2, kExampleDeveloperId2, kExampleUniqueId2,
blink::mojom::BackgroundFetchState::FAILURE); blink::mojom::BackgroundFetchState::FAILURE,
blink::mojom::BackgroundFetchFailureReason::QUOTA_EXCEEDED);
event_dispatcher_.DispatchBackgroundFetchClickEvent(second_registration_id, event_dispatcher_.DispatchBackgroundFetchClickEvent(second_registration_id,
std::move(registration), std::move(registration),
run_loop.QuitClosure()); run_loop.QuitClosure());
...@@ -215,7 +223,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchFailEvent) { ...@@ -215,7 +223,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchFailEvent) {
base::RunLoop run_loop; base::RunLoop run_loop;
auto registration = CreateBackgroundFetchRegistration( auto registration = CreateBackgroundFetchRegistration(
kExampleDeveloperId, kExampleUniqueId, kExampleDeveloperId, kExampleUniqueId,
blink::mojom::BackgroundFetchState::FAILURE); blink::mojom::BackgroundFetchState::FAILURE,
blink::mojom::BackgroundFetchFailureReason::QUOTA_EXCEEDED);
event_dispatcher_.DispatchBackgroundFetchFailEvent( event_dispatcher_.DispatchBackgroundFetchFailEvent(
registration_id, std::move(registration), fetches, registration_id, std::move(registration), fetches,
run_loop.QuitClosure()); run_loop.QuitClosure());
...@@ -247,7 +256,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchFailEvent) { ...@@ -247,7 +256,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchFailEvent) {
base::RunLoop run_loop; base::RunLoop run_loop;
auto registration = CreateBackgroundFetchRegistration( auto registration = CreateBackgroundFetchRegistration(
kExampleDeveloperId2, kExampleUniqueId2, kExampleDeveloperId2, kExampleUniqueId2,
blink::mojom::BackgroundFetchState::FAILURE); blink::mojom::BackgroundFetchState::FAILURE,
blink::mojom::BackgroundFetchFailureReason::QUOTA_EXCEEDED);
event_dispatcher_.DispatchBackgroundFetchFailEvent( event_dispatcher_.DispatchBackgroundFetchFailEvent(
second_registration_id, std::move(registration), fetches, second_registration_id, std::move(registration), fetches,
run_loop.QuitClosure()); run_loop.QuitClosure());
...@@ -290,7 +300,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchFetchSuccessEvent) { ...@@ -290,7 +300,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchFetchSuccessEvent) {
base::RunLoop run_loop; base::RunLoop run_loop;
auto registration = CreateBackgroundFetchRegistration( auto registration = CreateBackgroundFetchRegistration(
kExampleDeveloperId, kExampleUniqueId, kExampleDeveloperId, kExampleUniqueId,
blink::mojom::BackgroundFetchState::SUCCESS); blink::mojom::BackgroundFetchState::SUCCESS,
blink::mojom::BackgroundFetchFailureReason::NONE);
event_dispatcher_.DispatchBackgroundFetchSuccessEvent( event_dispatcher_.DispatchBackgroundFetchSuccessEvent(
registration_id, std::move(registration), fetches, registration_id, std::move(registration), fetches,
run_loop.QuitClosure()); run_loop.QuitClosure());
...@@ -326,7 +337,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchFetchSuccessEvent) { ...@@ -326,7 +337,8 @@ TEST_F(BackgroundFetchEventDispatcherTest, DispatchFetchSuccessEvent) {
base::RunLoop run_loop; base::RunLoop run_loop;
auto registration = CreateBackgroundFetchRegistration( auto registration = CreateBackgroundFetchRegistration(
kExampleDeveloperId2, kExampleUniqueId2, kExampleDeveloperId2, kExampleUniqueId2,
blink::mojom::BackgroundFetchState::SUCCESS); blink::mojom::BackgroundFetchState::SUCCESS,
blink::mojom::BackgroundFetchFailureReason::NONE);
event_dispatcher_.DispatchBackgroundFetchSuccessEvent( event_dispatcher_.DispatchBackgroundFetchSuccessEvent(
second_registration_id, std::move(registration), fetches, second_registration_id, std::move(registration), fetches,
run_loop.QuitClosure()); run_loop.QuitClosure());
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "content/browser/background_fetch/background_fetch_job_controller.h" #include "content/browser/background_fetch/background_fetch_job_controller.h"
#include "third_party/blink/public/platform/modules/background_fetch/background_fetch.mojom.h"
#include <utility> #include <utility>
...@@ -143,15 +144,40 @@ BackgroundFetchJobController::NewRegistration( ...@@ -143,15 +144,40 @@ BackgroundFetchJobController::NewRegistration(
return std::make_unique<BackgroundFetchRegistration>( return std::make_unique<BackgroundFetchRegistration>(
registration_id().developer_id(), registration_id().unique_id(), registration_id().developer_id(), registration_id().unique_id(),
0 /* upload_total */, 0 /* uploaded */, total_downloads_size_, 0 /* upload_total */, 0 /* uploaded */, total_downloads_size_,
complete_requests_downloaded_bytes_cache_, state); complete_requests_downloaded_bytes_cache_, state, MojoFailureReason());
} }
uint64_t BackgroundFetchJobController::GetInProgressDownloadedBytes() { uint64_t BackgroundFetchJobController::GetInProgressDownloadedBytes() {
return active_request_downloaded_bytes_; return active_request_downloaded_bytes_;
} }
// TODO(crbug.com/876691): Get rid of BackgroundFetchReasonToAbort and remove
// this method.
blink::mojom::BackgroundFetchFailureReason
BackgroundFetchJobController::MojoFailureReason() const {
switch (reason_to_abort_) {
case BackgroundFetchReasonToAbort::NONE:
return blink::mojom::BackgroundFetchFailureReason::NONE;
case BackgroundFetchReasonToAbort::CANCELLED_FROM_UI:
return blink::mojom::BackgroundFetchFailureReason::CANCELLED_FROM_UI;
case BackgroundFetchReasonToAbort::ABORTED_BY_DEVELOPER:
return blink::mojom::BackgroundFetchFailureReason::CANCELLED_BY_DEVELOPER;
case BackgroundFetchReasonToAbort::TOTAL_DOWNLOAD_SIZE_EXCEEDED:
return blink::mojom::BackgroundFetchFailureReason::
TOTAL_DOWNLOAD_SIZE_EXCEEDED;
case BackgroundFetchReasonToAbort::SERVICE_WORKER_UNAVAILABLE:
return blink::mojom::BackgroundFetchFailureReason::
SERVICE_WORKER_UNAVAILABLE;
case BackgroundFetchReasonToAbort::QUOTA_EXCEEDED:
return blink::mojom::BackgroundFetchFailureReason::QUOTA_EXCEEDED;
}
NOTREACHED();
}
void BackgroundFetchJobController::Abort( void BackgroundFetchJobController::Abort(
BackgroundFetchReasonToAbort reason_to_abort) { BackgroundFetchReasonToAbort reason_to_abort) {
reason_to_abort_ = reason_to_abort;
// Stop propagating any in-flight events to the scheduler. // Stop propagating any in-flight events to the scheduler.
active_request_finished_callback_.Reset(); active_request_finished_callback_.Reset();
......
...@@ -113,6 +113,10 @@ class CONTENT_EXPORT BackgroundFetchJobController final ...@@ -113,6 +113,10 @@ class CONTENT_EXPORT BackgroundFetchJobController final
void Abort(BackgroundFetchReasonToAbort reason_to_abort) override; void Abort(BackgroundFetchReasonToAbort reason_to_abort) override;
private: private:
// Returns reason_to_abort_ as blink::mojom::BackgroundFetchFailureReason.
// TODO(crbug.com/876691): Get rid of BackgroundFetchReasonToAbort and remove
// this converter.
blink::mojom::BackgroundFetchFailureReason MojoFailureReason() const;
// Options for the represented background fetch registration. // Options for the represented background fetch registration.
BackgroundFetchOptions options_; BackgroundFetchOptions options_;
...@@ -146,6 +150,10 @@ class CONTENT_EXPORT BackgroundFetchJobController final ...@@ -146,6 +150,10 @@ class CONTENT_EXPORT BackgroundFetchJobController final
// Number of the requests that have been completed so far. // Number of the requests that have been completed so far.
int completed_downloads_ = 0; int completed_downloads_ = 0;
// The reason background fetch was aborted.
BackgroundFetchReasonToAbort reason_to_abort_ =
BackgroundFetchReasonToAbort::NONE;
base::WeakPtrFactory<BackgroundFetchJobController> weak_ptr_factory_; base::WeakPtrFactory<BackgroundFetchJobController> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(BackgroundFetchJobController); DISALLOW_COPY_AND_ASSIGN(BackgroundFetchJobController);
......
...@@ -167,10 +167,11 @@ std::unique_ptr<BackgroundFetchRegistration> ...@@ -167,10 +167,11 @@ std::unique_ptr<BackgroundFetchRegistration>
BackgroundFetchTestBase::CreateBackgroundFetchRegistration( BackgroundFetchTestBase::CreateBackgroundFetchRegistration(
const std::string& developer_id, const std::string& developer_id,
const std::string& unique_id, const std::string& unique_id,
blink::mojom::BackgroundFetchState state) { blink::mojom::BackgroundFetchState state,
blink::mojom::BackgroundFetchFailureReason failure_reason) {
auto registration = std::make_unique<BackgroundFetchRegistration>( auto registration = std::make_unique<BackgroundFetchRegistration>(
developer_id, unique_id, 0 /* upload_total */, 0 /* uploaded */, developer_id, unique_id, 0 /* upload_total */, 0 /* uploaded */,
0 /* download_total */, 0 /* downloaded */, state); 0 /* download_total */, 0 /* downloaded */, state, failure_reason);
return registration; return registration;
} }
......
...@@ -57,10 +57,12 @@ class BackgroundFetchTestBase : public ::testing::Test { ...@@ -57,10 +57,12 @@ class BackgroundFetchTestBase : public ::testing::Test {
std::unique_ptr<TestResponse> response); std::unique_ptr<TestResponse> response);
// Creates a BackgroundFetchRegistration object. // Creates a BackgroundFetchRegistration object.
static std::unique_ptr<BackgroundFetchRegistration> std::unique_ptr<BackgroundFetchRegistration>
CreateBackgroundFetchRegistration(const std::string& developer_id, CreateBackgroundFetchRegistration(
const std::string& unique_id, const std::string& developer_id,
blink::mojom::BackgroundFetchState state); const std::string& unique_id,
blink::mojom::BackgroundFetchState state,
blink::mojom::BackgroundFetchFailureReason failure_reason);
// Returns the embedded worker test helper instance, which can be used to // Returns the embedded worker test helper instance, which can be used to
// influence the behavior of the Service Worker events. // influence the behavior of the Service Worker events.
......
...@@ -103,6 +103,8 @@ void CreateMetadataTask::InitializeMetadataProto() { ...@@ -103,6 +103,8 @@ void CreateMetadataTask::InitializeMetadataProto() {
registration_proto->set_download_total(options_.download_total); registration_proto->set_download_total(options_.download_total);
registration_proto->set_state( registration_proto->set_state(
proto::BackgroundFetchRegistration_BackgroundFetchState_PENDING); proto::BackgroundFetchRegistration_BackgroundFetchState_PENDING);
registration_proto->set_failure_reason(
proto::BackgroundFetchRegistration_BackgroundFetchFailureReason_NONE);
// Set Options fields. // Set Options fields.
auto* options_proto = metadata_proto_->mutable_options(); auto* options_proto = metadata_proto_->mutable_options();
...@@ -223,7 +225,14 @@ void CreateMetadataTask::FinishWithError( ...@@ -223,7 +225,14 @@ void CreateMetadataTask::FinishWithError(
if (error == blink::mojom::BackgroundFetchError::NONE) { if (error == blink::mojom::BackgroundFetchError::NONE) {
DCHECK(metadata_proto_); DCHECK(metadata_proto_);
registration = ToBackgroundFetchRegistration(*metadata_proto_); bool converted =
ToBackgroundFetchRegistration(*metadata_proto_, &registration);
if (!converted) {
// Database corrupted.
SetStorageErrorAndFinish(
BackgroundFetchStorageError::kServiceWorkerStorageError);
return;
}
for (auto& observer : data_manager()->observers()) { for (auto& observer : data_manager()->observers()) {
observer.OnRegistrationCreated(registration_id_, registration, options_, observer.OnRegistrationCreated(registration_id_, registration, options_,
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#include "content/browser/background_fetch/storage/database_helpers.h" #include "content/browser/background_fetch/storage/database_helpers.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "content/browser/background_fetch/background_fetch.pb.h"
#include "third_party/blink/public/platform/modules/background_fetch/background_fetch.mojom.h" #include "third_party/blink/public/platform/modules/background_fetch/background_fetch.mojom.h"
namespace content { namespace content {
...@@ -89,31 +88,76 @@ DatabaseStatus ToDatabaseStatus(blink::ServiceWorkerStatusCode status) { ...@@ -89,31 +88,76 @@ DatabaseStatus ToDatabaseStatus(blink::ServiceWorkerStatusCode status) {
return DatabaseStatus::kFailed; return DatabaseStatus::kFailed;
} }
BackgroundFetchRegistration ToBackgroundFetchRegistration( bool ToBackgroundFetchRegistration(
const proto::BackgroundFetchMetadata& metadata_proto) { const proto::BackgroundFetchMetadata& metadata_proto,
BackgroundFetchRegistration* registration) {
DCHECK(registration);
const auto& registration_proto = metadata_proto.registration(); const auto& registration_proto = metadata_proto.registration();
BackgroundFetchRegistration registration; registration->developer_id = registration_proto.developer_id();
registration.developer_id = registration_proto.developer_id(); registration->unique_id = registration_proto.unique_id();
registration.unique_id = registration_proto.unique_id(); registration->upload_total = registration_proto.upload_total();
registration.upload_total = registration_proto.upload_total(); registration->uploaded = registration_proto.uploaded();
registration.uploaded = registration_proto.uploaded(); registration->download_total = registration_proto.download_total();
registration.download_total = registration_proto.download_total(); registration->downloaded = registration_proto.downloaded();
registration.downloaded = registration_proto.downloaded();
switch (registration_proto.state()) { switch (registration_proto.state()) {
case proto::BackgroundFetchRegistration_BackgroundFetchState_PENDING: case proto::BackgroundFetchRegistration_BackgroundFetchState_PENDING:
registration.state = blink::mojom::BackgroundFetchState::PENDING; registration->state = blink::mojom::BackgroundFetchState::PENDING;
break; break;
case proto::BackgroundFetchRegistration_BackgroundFetchState_FAILURE: case proto::BackgroundFetchRegistration_BackgroundFetchState_FAILURE:
registration.state = blink::mojom::BackgroundFetchState::FAILURE; registration->state = blink::mojom::BackgroundFetchState::FAILURE;
break; break;
case proto::BackgroundFetchRegistration_BackgroundFetchState_SUCCESS: case proto::BackgroundFetchRegistration_BackgroundFetchState_SUCCESS:
registration.state = blink::mojom::BackgroundFetchState::SUCCESS; registration->state = blink::mojom::BackgroundFetchState::SUCCESS;
break; break;
default: default:
NOTREACHED(); NOTREACHED();
} }
return registration;
bool did_convert = MojoFailureReasonFromRegistrationProto(
registration_proto.failure_reason(), &registration->failure_reason);
return did_convert;
}
bool MojoFailureReasonFromRegistrationProto(
proto::BackgroundFetchRegistration::BackgroundFetchFailureReason
proto_failure_reason,
blink::mojom::BackgroundFetchFailureReason* failure_reason) {
DCHECK(failure_reason);
switch (proto_failure_reason) {
case proto::BackgroundFetchRegistration::NONE:
*failure_reason = blink::mojom::BackgroundFetchFailureReason::NONE;
return true;
case proto::BackgroundFetchRegistration::CANCELLED_FROM_UI:
*failure_reason =
blink::mojom::BackgroundFetchFailureReason::CANCELLED_FROM_UI;
return true;
case proto::BackgroundFetchRegistration::CANCELLED_BY_DEVELOPER:
*failure_reason =
blink::mojom::BackgroundFetchFailureReason::CANCELLED_BY_DEVELOPER;
return true;
case proto::BackgroundFetchRegistration::SERVICE_WORKER_UNAVAILABLE:
*failure_reason = blink::mojom::BackgroundFetchFailureReason::
SERVICE_WORKER_UNAVAILABLE;
return true;
case proto::BackgroundFetchRegistration::QUOTA_EXCEEDED:
*failure_reason =
blink::mojom::BackgroundFetchFailureReason::QUOTA_EXCEEDED;
return true;
case proto::BackgroundFetchRegistration::TOTAL_DOWNLOAD_SIZE_EXCEEDED:
*failure_reason = blink::mojom::BackgroundFetchFailureReason::
TOTAL_DOWNLOAD_SIZE_EXCEEDED;
return true;
case proto::BackgroundFetchRegistration::FETCH_ERROR:
*failure_reason = blink::mojom::BackgroundFetchFailureReason::FETCH_ERROR;
return true;
case proto::BackgroundFetchRegistration::BAD_STATUS:
*failure_reason = blink::mojom::BackgroundFetchFailureReason::BAD_STATUS;
return true;
}
LOG(ERROR) << "BackgroundFetchFailureReason from the metadata proto doesn't"
<< " match any enum value. Possible database corruption.";
return false;
} }
} // namespace background_fetch } // namespace background_fetch
......
...@@ -7,17 +7,15 @@ ...@@ -7,17 +7,15 @@
#include <string> #include <string>
#include "content/browser/background_fetch/background_fetch.pb.h"
#include "content/common/background_fetch/background_fetch_types.h" #include "content/common/background_fetch/background_fetch_types.h"
#include "content/common/content_export.h" #include "content/common/content_export.h"
#include "content/common/service_worker/service_worker_types.h" #include "content/common/service_worker/service_worker_types.h"
#include "content/public/browser/background_fetch_delegate.h"
#include "third_party/blink/public/common/service_worker/service_worker_status_code.h" #include "third_party/blink/public/common/service_worker/service_worker_status_code.h"
namespace content { namespace content {
namespace proto {
class BackgroundFetchMetadata;
}
namespace background_fetch { namespace background_fetch {
// The database schema is content/browser/background_fetch/storage/README.md. // The database schema is content/browser/background_fetch/storage/README.md.
...@@ -61,8 +59,14 @@ enum class DatabaseStatus { kOk, kFailed, kNotFound }; ...@@ -61,8 +59,14 @@ enum class DatabaseStatus { kOk, kFailed, kNotFound };
DatabaseStatus ToDatabaseStatus(blink::ServiceWorkerStatusCode status); DatabaseStatus ToDatabaseStatus(blink::ServiceWorkerStatusCode status);
// Converts the |metadata_proto| to a BackgroundFetchRegistration object. // Converts the |metadata_proto| to a BackgroundFetchRegistration object.
BackgroundFetchRegistration ToBackgroundFetchRegistration( bool ToBackgroundFetchRegistration(
const proto::BackgroundFetchMetadata& metadata_proto); const proto::BackgroundFetchMetadata& metadata_proto,
BackgroundFetchRegistration* registration);
bool MojoFailureReasonFromRegistrationProto(
proto::BackgroundFetchRegistration_BackgroundFetchFailureReason
proto_failure_reason,
blink::mojom::BackgroundFetchFailureReason* failure_reason);
} // namespace background_fetch } // namespace background_fetch
......
...@@ -49,7 +49,14 @@ void GetRegistrationTask::FinishWithError( ...@@ -49,7 +49,14 @@ void GetRegistrationTask::FinishWithError(
if (error == blink::mojom::BackgroundFetchError::NONE) { if (error == blink::mojom::BackgroundFetchError::NONE) {
DCHECK(metadata_proto_); DCHECK(metadata_proto_);
registration = ToBackgroundFetchRegistration(*metadata_proto_); bool converted =
ToBackgroundFetchRegistration(*metadata_proto_, &registration);
if (!converted) {
// Database corrupted.
SetStorageErrorAndFinish(
BackgroundFetchStorageError::kServiceWorkerStorageError);
return;
}
} }
ReportStorageError(); ReportStorageError();
......
...@@ -39,6 +39,7 @@ bool StructTraits<blink::mojom::BackgroundFetchRegistrationDataView, ...@@ -39,6 +39,7 @@ bool StructTraits<blink::mojom::BackgroundFetchRegistrationDataView,
registration->download_total = data.download_total(); registration->download_total = data.download_total();
registration->downloaded = data.downloaded(); registration->downloaded = data.downloaded();
registration->state = data.state(); registration->state = data.state();
registration->failure_reason = data.failure_reason();
return true; return true;
} }
......
...@@ -68,6 +68,10 @@ struct CONTENT_EXPORT ...@@ -68,6 +68,10 @@ struct CONTENT_EXPORT
const content::BackgroundFetchRegistration& registration) { const content::BackgroundFetchRegistration& registration) {
return registration.state; return registration.state;
} }
static blink::mojom::BackgroundFetchFailureReason failure_reason(
const content::BackgroundFetchRegistration& registration) {
return registration.failure_reason;
}
static bool Read(blink::mojom::BackgroundFetchRegistrationDataView data, static bool Read(blink::mojom::BackgroundFetchRegistrationDataView data,
content::BackgroundFetchRegistration* registration); content::BackgroundFetchRegistration* registration);
......
...@@ -60,6 +60,8 @@ TEST(BackgroundFetchStructTraitsTest, BackgroundFetchRegistrationRoundTrip) { ...@@ -60,6 +60,8 @@ TEST(BackgroundFetchStructTraitsTest, BackgroundFetchRegistrationRoundTrip) {
registration.unique_id = "7e57ab1e-c0de-a150-ca75-1e75f005ba11"; registration.unique_id = "7e57ab1e-c0de-a150-ca75-1e75f005ba11";
registration.download_total = 9001; registration.download_total = 9001;
registration.state = blink::mojom::BackgroundFetchState::FAILURE; registration.state = blink::mojom::BackgroundFetchState::FAILURE;
registration.failure_reason =
blink::mojom::BackgroundFetchFailureReason::CANCELLED_FROM_UI;
BackgroundFetchRegistration roundtrip_registration; BackgroundFetchRegistration roundtrip_registration;
ASSERT_TRUE(blink::mojom::BackgroundFetchRegistration::Deserialize( ASSERT_TRUE(blink::mojom::BackgroundFetchRegistration::Deserialize(
...@@ -71,6 +73,7 @@ TEST(BackgroundFetchStructTraitsTest, BackgroundFetchRegistrationRoundTrip) { ...@@ -71,6 +73,7 @@ TEST(BackgroundFetchStructTraitsTest, BackgroundFetchRegistrationRoundTrip) {
EXPECT_EQ(roundtrip_registration.download_total, registration.download_total); EXPECT_EQ(roundtrip_registration.download_total, registration.download_total);
EXPECT_EQ(roundtrip_registration.state, registration.state); EXPECT_EQ(roundtrip_registration.state, registration.state);
EXPECT_EQ(roundtrip_registration.failure_reason, registration.failure_reason);
} }
TEST(BackgroundFetchStructTraitsTest, ImageResourceRoundtrip) { TEST(BackgroundFetchStructTraitsTest, ImageResourceRoundtrip) {
......
...@@ -29,7 +29,8 @@ BackgroundFetchOptions::BackgroundFetchOptions( ...@@ -29,7 +29,8 @@ BackgroundFetchOptions::BackgroundFetchOptions(
BackgroundFetchOptions::~BackgroundFetchOptions() = default; BackgroundFetchOptions::~BackgroundFetchOptions() = default;
BackgroundFetchRegistration::BackgroundFetchRegistration() BackgroundFetchRegistration::BackgroundFetchRegistration()
: state(blink::mojom::BackgroundFetchState::PENDING) {} : state(blink::mojom::BackgroundFetchState::PENDING),
failure_reason(blink::mojom::BackgroundFetchFailureReason::NONE) {}
BackgroundFetchRegistration::BackgroundFetchRegistration( BackgroundFetchRegistration::BackgroundFetchRegistration(
const std::string& developer_id, const std::string& developer_id,
...@@ -38,14 +39,16 @@ BackgroundFetchRegistration::BackgroundFetchRegistration( ...@@ -38,14 +39,16 @@ BackgroundFetchRegistration::BackgroundFetchRegistration(
uint64_t uploaded, uint64_t uploaded,
uint64_t download_total, uint64_t download_total,
uint64_t downloaded, uint64_t downloaded,
blink::mojom::BackgroundFetchState state) blink::mojom::BackgroundFetchState state,
blink::mojom::BackgroundFetchFailureReason failure_reason)
: developer_id(developer_id), : developer_id(developer_id),
unique_id(unique_id), unique_id(unique_id),
upload_total(upload_total), upload_total(upload_total),
uploaded(uploaded), uploaded(uploaded),
download_total(download_total), download_total(download_total),
downloaded(downloaded), downloaded(downloaded),
state(state) {} state(state),
failure_reason(failure_reason) {}
BackgroundFetchRegistration::BackgroundFetchRegistration( BackgroundFetchRegistration::BackgroundFetchRegistration(
const BackgroundFetchRegistration& other) = default; const BackgroundFetchRegistration& other) = default;
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
namespace blink { namespace blink {
namespace mojom { namespace mojom {
enum class BackgroundFetchFailureReason;
enum class BackgroundFetchState; enum class BackgroundFetchState;
} // namespace mojom } // namespace mojom
} // namespace blink } // namespace blink
...@@ -40,13 +41,15 @@ struct CONTENT_EXPORT BackgroundFetchOptions { ...@@ -40,13 +41,15 @@ struct CONTENT_EXPORT BackgroundFetchOptions {
// https://wicg.github.io/background-fetch/#background-fetch-registration // https://wicg.github.io/background-fetch/#background-fetch-registration
struct CONTENT_EXPORT BackgroundFetchRegistration { struct CONTENT_EXPORT BackgroundFetchRegistration {
BackgroundFetchRegistration(); BackgroundFetchRegistration();
BackgroundFetchRegistration(const std::string& developer_id, BackgroundFetchRegistration(
const std::string& unique_id, const std::string& developer_id,
uint64_t upload_total, const std::string& unique_id,
uint64_t uploaded, uint64_t upload_total,
uint64_t download_total, uint64_t uploaded,
uint64_t downloaded, uint64_t download_total,
blink::mojom::BackgroundFetchState state); uint64_t downloaded,
blink::mojom::BackgroundFetchState state,
blink::mojom::BackgroundFetchFailureReason failure_reason);
BackgroundFetchRegistration(const BackgroundFetchRegistration& other); BackgroundFetchRegistration(const BackgroundFetchRegistration& other);
~BackgroundFetchRegistration(); ~BackgroundFetchRegistration();
...@@ -63,6 +66,7 @@ struct CONTENT_EXPORT BackgroundFetchRegistration { ...@@ -63,6 +66,7 @@ struct CONTENT_EXPORT BackgroundFetchRegistration {
uint64_t download_total = 0; uint64_t download_total = 0;
uint64_t downloaded = 0; uint64_t downloaded = 0;
blink::mojom::BackgroundFetchState state; blink::mojom::BackgroundFetchState state;
blink::mojom::BackgroundFetchFailureReason failure_reason;
}; };
// Represents a request/response pair for a settled Background Fetch fetch. // Represents a request/response pair for a settled Background Fetch fetch.
......
...@@ -35,6 +35,29 @@ enum BackgroundFetchState { ...@@ -35,6 +35,29 @@ enum BackgroundFetchState {
SUCCESS SUCCESS
}; };
// https://wicg.github.io/background-fetch/#enumdef-backgroundfetchfailurereason
enum BackgroundFetchFailureReason {
// "":
NONE = 0,
// "aborted":
CANCELLED_FROM_UI = 1,
CANCELLED_BY_DEVELOPER = 2,
// "bad-status":
BAD_STATUS = 3,
// "fetch-error":
FETCH_ERROR = 4,
SERVICE_WORKER_UNAVAILABLE = 5,
// "quota-exceeded":
QUOTA_EXCEEDED = 6,
// "total-download-exceeded":
TOTAL_DOWNLOAD_SIZE_EXCEEDED = 7,
};
// Represents the optional options a developer can provide when starting a new // Represents the optional options a developer can provide when starting a new
// Background Fetch fetch. Analogous to the following structure in the spec: // Background Fetch fetch. Analogous to the following structure in the spec:
// https://wicg.github.io/background-fetch/#background-fetch-manager // https://wicg.github.io/background-fetch/#background-fetch-manager
...@@ -61,6 +84,7 @@ struct BackgroundFetchRegistration { ...@@ -61,6 +84,7 @@ struct BackgroundFetchRegistration {
uint64 download_total; uint64 download_total;
uint64 downloaded; uint64 downloaded;
BackgroundFetchState state; BackgroundFetchState state;
BackgroundFetchFailureReason failure_reason;
}; };
interface BackgroundFetchRegistrationObserver { interface BackgroundFetchRegistrationObserver {
......
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