Commit 4a2e1006 authored by Rayan Kanso's avatar Rayan Kanso Committed by Commit Bot

[Background Fetch] Report uploadTotal in the registration

Also sets up some useful testing helpers for future upload features.

Bug: 774054
Change-Id: I9aea33086b228357d9a160bd35b0a37ceaa30592
Reviewed-on: https://chromium-review.googlesource.com/c/1329149
Commit-Queue: Rayan Kanso <rayankans@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#606851}
parent b02a7719
......@@ -25,6 +25,7 @@
#include "content/browser/background_fetch/background_fetch_test_data_manager.h"
#include "content/browser/background_fetch/storage/database_helpers.h"
#include "content/browser/background_fetch/storage/image_helpers.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/browser/cache_storage/cache_storage_cache_handle.h"
#include "content/browser/cache_storage/cache_storage_manager.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
......@@ -36,7 +37,9 @@
#include "content/public/browser/storage_partition.h"
#include "content/public/test/test_utils.h"
#include "services/network/public/mojom/fetch_api.mojom.h"
#include "storage/browser/blob/blob_data_builder.h"
#include "storage/browser/blob/blob_data_handle.h"
#include "storage/browser/blob/blob_storage_context.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/public/platform/modules/background_fetch/background_fetch.mojom.h"
#include "third_party/blink/public/platform/modules/cache_storage/cache_storage.mojom.h"
......@@ -704,6 +707,20 @@ class BackgroundFetchDataManagerTest
std::move(quit_closure).Run();
}
blink::mojom::SerializedBlobPtr BuildBlob(const std::string data) {
auto blob_data = std::make_unique<storage::BlobDataBuilder>(
"blob-id:" + base::GenerateGUID());
blob_data->AppendData(data);
std::unique_ptr<storage::BlobDataHandle> blob_handle =
background_fetch_data_manager_->blob_storage_context_->context()
->AddFinishedBlob(std::move(blob_data));
auto blob = blink::mojom::SerializedBlob::New();
blob->uuid = blob_handle->uuid();
blob->size = blob_handle->size();
return blob;
}
std::unique_ptr<BackgroundFetchTestDataManager>
background_fetch_data_manager_;
};
......@@ -955,6 +972,7 @@ TEST_F(BackgroundFetchDataManagerTest, GetRegistration) {
EXPECT_EQ(kExampleUniqueId, registration.unique_id);
EXPECT_EQ(kExampleDeveloperId, registration.developer_id);
EXPECT_EQ(0u, registration.upload_total);
// Verify that retrieving using the wrong developer id doesn't work.
registration =
......@@ -1009,6 +1027,38 @@ TEST_F(BackgroundFetchDataManagerTest, GetMetadata) {
EXPECT_EQ(metadata->num_fetches(), static_cast<int>(num_requests));
}
TEST_F(BackgroundFetchDataManagerTest, RegistrationUploadInfo) {
int64_t sw_id = RegisterServiceWorker();
ASSERT_NE(blink::mojom::kInvalidServiceWorkerRegistrationId, sw_id);
BackgroundFetchRegistrationId registration_id(
sw_id, origin(), kExampleDeveloperId, kExampleUniqueId);
BackgroundFetchOptions options;
blink::mojom::BackgroundFetchError error;
const std::string upload_data = "Upload!";
// Create a single registration.
{
// One upload and one download.
std::vector<blink::mojom::FetchAPIRequestPtr> requests =
CreateValidRequests(origin(), 2u);
requests[0]->blob = BuildBlob(upload_data);
EXPECT_CALL(*this, OnRegistrationCreated(registration_id, _, _, _, _, _));
CreateRegistration(registration_id, std::move(requests), options,
SkBitmap(), &error);
ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
}
auto registration =
GetRegistration(sw_id, origin(), kExampleDeveloperId, &error);
ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
EXPECT_EQ(registration.unique_id, kExampleUniqueId);
EXPECT_EQ(registration.developer_id, kExampleDeveloperId);
EXPECT_EQ(registration.upload_total, upload_data.size());
}
TEST_F(BackgroundFetchDataManagerTest, LargeIconNotPersisted) {
int64_t sw_id = RegisterServiceWorker();
ASSERT_NE(blink::mojom::kInvalidServiceWorkerRegistrationId, sw_id);
......
......@@ -58,8 +58,7 @@ BackgroundFetchTestDataManager::BackgroundFetchTestDataManager(
mock_fill_response_(mock_fill_response) {}
void BackgroundFetchTestDataManager::InitializeOnIOThread() {
ChromeBlobStorageContext* blob_storage_context(
ChromeBlobStorageContext::GetFor(browser_context_));
blob_storage_context_ = ChromeBlobStorageContext::GetFor(browser_context_);
// Wait for ChromeBlobStorageContext to finish initializing.
base::RunLoop().RunUntilIdle();
......@@ -78,7 +77,7 @@ void BackgroundFetchTestDataManager::InitializeOnIOThread() {
cache_manager_->SetBlobParametersForCache(
storage_partition_->GetURLRequestContext(),
blob_storage_context->context()->AsWeakPtr());
blob_storage_context_->context()->AsWeakPtr());
}
BackgroundFetchTestDataManager::~BackgroundFetchTestDataManager() = default;
......
......@@ -16,6 +16,7 @@ namespace content {
class BrowserContext;
class CacheStorageManager;
class ChromeBlobStorageContext;
class MockQuotaManager;
class ServiceWorkerContextWrapper;
class StoragePartition;
......@@ -44,6 +45,7 @@ class BackgroundFetchTestDataManager : public BackgroundFetchDataManager {
scoped_refptr<MockQuotaManager> mock_quota_manager_;
BrowserContext* browser_context_;
StoragePartition* storage_partition_;
scoped_refptr<ChromeBlobStorageContext> blob_storage_context_;
bool mock_fill_response_;
DISALLOW_COPY_AND_ASSIGN(BackgroundFetchTestDataManager);
......
......@@ -4,6 +4,7 @@
#include "content/browser/background_fetch/storage/create_metadata_task.h"
#include <numeric>
#include <set>
#include <utility>
......@@ -236,6 +237,11 @@ void CreateMetadataTask::InitializeMetadataProto() {
proto::BackgroundFetchRegistration_BackgroundFetchResult_UNSET);
registration_proto->set_failure_reason(
proto::BackgroundFetchRegistration_BackgroundFetchFailureReason_NONE);
registration_proto->set_upload_total(
std::accumulate(requests_.begin(), requests_.end(), 0u,
[](uint64_t sum, const auto& request) {
return sum + (request->blob ? request->blob->size : 0u);
}));
// Set Options fields.
auto* options_proto = metadata_proto_->mutable_options();
......
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