Commit 794a2f14 authored by Mugdha Lakhani's avatar Mugdha Lakhani Committed by Commit Bot

[BackgroundSync] Add BackgroundSyncScheduler

The logic to decide delay for each Background Sync registration
is now completely in the chrome:// layer.

The logic to calculate the soonest_delay across registrations across
storage partitions for the browser context can be moved to content://
in a new class called BackgroundSyncScheduler.

This reduces an unnecessary interaction between chrome:// and content://
layer, allows more code to be shared with embedders of content://
and this logic can be completely tested by browser tests on
ContentShell.

This simplifies scheduling logic, and also improves testability.

Bug: 996166

Change-Id: I0aa15b9bcc35c0f2642c89d1d776668054992647
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1806576
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Reviewed-by: default avatarRayan Kanso <rayankans@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702300}
parent 2fc2bce6
......@@ -501,6 +501,8 @@ jumbo_source_set("browser") {
"background_sync/background_sync_proxy.h",
"background_sync/background_sync_registration_helper.cc",
"background_sync/background_sync_registration_helper.h",
"background_sync/background_sync_scheduler.cc",
"background_sync/background_sync_scheduler.h",
"background_sync/background_sync_status.h",
"background_sync/one_shot_background_sync_service_impl.cc",
"background_sync/one_shot_background_sync_service_impl.h",
......
......@@ -588,7 +588,7 @@ BackgroundSyncManager::BackgroundSyncManager(
: op_scheduler_(CacheStorageSchedulerClient::kBackgroundSync,
base::ThreadTaskRunnerHandle::Get()),
service_worker_context_(std::move(service_worker_context)),
proxy_(service_worker_context_),
proxy_(std::make_unique<BackgroundSyncProxy>(service_worker_context_)),
devtools_context_(std::move(devtools_context)),
parameters_(std::make_unique<BackgroundSyncParameters>()),
disabled_(false),
......@@ -738,7 +738,7 @@ void BackgroundSyncManager::InitDidGetDataFromBackend(
base::DoNothing::Once());
FireReadyEvents(BackgroundSyncType::PERIODIC, /* reschedule= */ true,
base::DoNothing::Once());
proxy_.SendSuspendedPeriodicSyncOrigins(
proxy_->SendSuspendedPeriodicSyncOrigins(
std::move(suspended_periodic_sync_origins));
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, std::move(callback));
}
......@@ -1407,36 +1407,6 @@ void BackgroundSyncManager::DispatchPeriodicSyncEvent(
}
}
base::CancelableOnceClosure& BackgroundSyncManager::get_delayed_task(
BackgroundSyncType sync_type) {
if (sync_type == BackgroundSyncType::ONE_SHOT)
return delayed_one_shot_sync_task_;
return delayed_periodic_sync_task_;
}
void BackgroundSyncManager::ScheduleDelayedTask(BackgroundSyncType sync_type,
base::TimeDelta delay) {
base::OnceClosure callback = get_delayed_task(sync_type).callback();
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, std::move(callback), delay);
}
void BackgroundSyncManager::ResetAndScheduleDelayedSyncTask(
BackgroundSyncType sync_type,
base::TimeDelta soonest_wakeup_delta) {
if (soonest_wakeup_delta.is_max() || soonest_wakeup_delta.is_zero())
return;
auto fire_events_callback = base::BindOnce(
&BackgroundSyncManager::FireReadyEvents, weak_ptr_factory_.GetWeakPtr(),
sync_type, /* reschedule= */ true, base::DoNothing::Once(),
/* keepalive= */ nullptr);
get_delayed_task(sync_type).Reset(std::move(fire_events_callback));
ScheduleDelayedTask(sync_type, soonest_wakeup_delta);
}
void BackgroundSyncManager::HasMainFrameProviderHost(const url::Origin& origin,
BoolCallback callback) {
service_worker_context_->HasMainFrameProviderHost(origin.GetURL(),
......@@ -1860,11 +1830,16 @@ void BackgroundSyncManager::ScheduleDelayedProcessingOfRegistrations(
blink::mojom::BackgroundSyncType sync_type) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
ResetAndScheduleDelayedSyncTask(
auto fire_events_callback = base::BindOnce(
&BackgroundSyncManager::FireReadyEvents, weak_ptr_factory_.GetWeakPtr(),
sync_type, /* reschedule= */ true, base::DoNothing::Once(),
/* keepalive= */ nullptr);
proxy_->ScheduleDelayedProcessing(
sync_type,
GetSoonestWakeupDelta(sync_type,
/* last_browser_wakeup_time= */ base::Time()));
proxy_.ScheduleBrowserWakeUp(sync_type);
/* last_browser_wakeup_time= */ base::Time()),
std::move(fire_events_callback));
}
void BackgroundSyncManager::FireReadyEvents(
......
......@@ -125,6 +125,11 @@ class CONTENT_EXPORT BackgroundSyncManager
clock_ = clock;
}
void set_proxy_for_testing(std::unique_ptr<BackgroundSyncProxy> proxy) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
proxy_ = std::move(proxy);
}
// Called from DevTools
void EmulateDispatchSyncEvent(
const std::string& tag,
......@@ -208,8 +213,6 @@ class CONTENT_EXPORT BackgroundSyncManager
const std::string& tag,
scoped_refptr<ServiceWorkerVersion> active_version,
ServiceWorkerVersion::StatusCallback callback);
virtual void ScheduleDelayedTask(blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay);
virtual void HasMainFrameProviderHost(const url::Origin& origin,
BoolCallback callback);
......@@ -336,13 +339,6 @@ class CONTENT_EXPORT BackgroundSyncManager
void ScheduleDelayedProcessingOfRegistrations(
blink::mojom::BackgroundSyncType sync_type);
base::CancelableOnceClosure& get_delayed_task(
blink::mojom::BackgroundSyncType sync_type);
void ResetAndScheduleDelayedSyncTask(
blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta soonest_wakeup_delta);
void FireReadyEventsImpl(
blink::mojom::BackgroundSyncType sync_type,
bool reschedule,
......@@ -458,7 +454,7 @@ class CONTENT_EXPORT BackgroundSyncManager
CacheStorageScheduler op_scheduler_;
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
BackgroundSyncProxy proxy_;
std::unique_ptr<BackgroundSyncProxy> proxy_;
scoped_refptr<DevToolsBackgroundServicesContextImpl> devtools_context_;
std::unique_ptr<BackgroundSyncParameters> parameters_;
......@@ -470,9 +466,6 @@ class CONTENT_EXPORT BackgroundSyncManager
int num_firing_registrations_one_shot_;
int num_firing_registrations_periodic_;
base::CancelableOnceClosure delayed_one_shot_sync_task_;
base::CancelableOnceClosure delayed_periodic_sync_task_;
bool delayed_processing_scheduled_one_shot_sync_ = false;
bool delayed_processing_scheduled_periodic_sync_ = false;
......
......@@ -11,6 +11,7 @@
#include "base/macros.h"
#include "base/task/post_task.h"
#include "base/task/task_traits.h"
#include "content/browser/background_sync/background_sync_scheduler.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/background_sync_context.h"
......@@ -51,17 +52,35 @@ class BackgroundSyncProxy::Core {
return storage_partition_impl->browser_context();
}
// TODO(crbug.com/982378): Schedule a task to periodically revive suspended
// periodic Background Sync registrations.
void ScheduleBrowserWakeUp(blink::mojom::BackgroundSyncType sync_type) {
void ScheduleDelayedProcessing(blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay,
base::OnceClosure delayed_task) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!browser_context())
return;
auto* scheduler = BackgroundSyncScheduler::GetFor(browser_context());
DCHECK(scheduler);
if (delay == base::TimeDelta::Max()) {
scheduler->CancelDelayedProcessing(
service_worker_context_->storage_partition(), sync_type);
} else {
scheduler->ScheduleDelayedProcessing(
service_worker_context_->storage_partition(), sync_type, delay,
base::BindOnce(
[](base::OnceClosure delayed_task) {
RunOrPostTaskOnThread(FROM_HERE,
ServiceWorkerContext::GetCoreThreadId(),
std::move(delayed_task));
},
std::move(delayed_task)));
}
// TODO(crbug.com/996166): Remove this call once the logic to schedule
// browser wakeup has moved to BackgroundTaskScheduler.
auto* controller = browser_context()->GetBackgroundSyncController();
DCHECK(controller);
controller->ScheduleBrowserWakeUp(sync_type);
}
......@@ -97,14 +116,17 @@ BackgroundSyncProxy::BackgroundSyncProxy(
BackgroundSyncProxy::~BackgroundSyncProxy() = default;
void BackgroundSyncProxy::ScheduleBrowserWakeUp(
blink::mojom::BackgroundSyncType sync_type) {
void BackgroundSyncProxy::ScheduleDelayedProcessing(
blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay,
base::OnceClosure delayed_task) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
// Schedule Chrome wakeup.
RunOrPostTaskOnThread(FROM_HERE, BrowserThread::UI,
base::BindOnce(&Core::ScheduleBrowserWakeUp,
ui_core_weak_ptr_, sync_type));
RunOrPostTaskOnThread(
FROM_HERE, BrowserThread::UI,
base::BindOnce(&Core::ScheduleDelayedProcessing, ui_core_weak_ptr_,
sync_type, delay, std::move(delayed_task)));
}
void BackgroundSyncProxy::SendSuspendedPeriodicSyncOrigins(
......
......@@ -31,9 +31,12 @@ class CONTENT_EXPORT BackgroundSyncProxy {
public:
explicit BackgroundSyncProxy(
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context);
~BackgroundSyncProxy();
virtual ~BackgroundSyncProxy();
void ScheduleBrowserWakeUp(blink::mojom::BackgroundSyncType sync_type);
virtual void ScheduleDelayedProcessing(
blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay,
base::OnceClosure delayed_task);
void SendSuspendedPeriodicSyncOrigins(
std::set<url::Origin> suspended_origins);
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/background_sync/background_sync_scheduler.h"
#include "base/memory/scoped_refptr.h"
#include "base/supports_user_data.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
namespace {
const char kBackgroundSyncSchedulerKey[] = "background-sync-scheduler";
} // namespace
namespace content {
using DelayedProcessingInfo =
std::map<StoragePartition*, std::unique_ptr<base::OneShotTimer>>;
// static
BackgroundSyncScheduler* BackgroundSyncScheduler::GetFor(
BrowserContext* browser_context) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(browser_context);
if (!browser_context->GetUserData(kBackgroundSyncSchedulerKey)) {
scoped_refptr<BackgroundSyncScheduler> scheduler =
base::MakeRefCounted<BackgroundSyncScheduler>();
browser_context->SetUserData(
kBackgroundSyncSchedulerKey,
std::make_unique<base::UserDataAdapter<BackgroundSyncScheduler>>(
scheduler.get()));
}
return base::UserDataAdapter<BackgroundSyncScheduler>::Get(
browser_context, kBackgroundSyncSchedulerKey);
}
BackgroundSyncScheduler::BackgroundSyncScheduler() = default;
BackgroundSyncScheduler::~BackgroundSyncScheduler() {
for (auto& one_shot_processing_info : delayed_processing_info_one_shot_)
one_shot_processing_info.second->Stop();
for (auto& periodic_processing_info : delayed_processing_info_one_shot_)
periodic_processing_info.second->Stop();
}
void BackgroundSyncScheduler::ScheduleDelayedProcessing(
StoragePartition* storage_partition,
blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay,
base::OnceClosure delayed_task) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(storage_partition);
auto& delayed_processing_info = GetDelayedProcessingInfo(sync_type);
delayed_processing_info.emplace(storage_partition,
std::make_unique<base::OneShotTimer>());
if (!delay.is_zero() && !delay.is_max()) {
delayed_processing_info[storage_partition]->Start(FROM_HERE, delay,
std::move(delayed_task));
}
// TODO(crbug.com/996166) Move logic to schedule a browser wakeup task on
// Android from BackgroundSycnProxy to here.
}
void BackgroundSyncScheduler::CancelDelayedProcessing(
StoragePartition* storage_partition,
blink::mojom::BackgroundSyncType sync_type) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(storage_partition);
auto& delayed_processing_info = GetDelayedProcessingInfo(sync_type);
delayed_processing_info.erase(storage_partition);
// TODO(crbug.com/996166) Move logic to cancel a browser wakeup task on
// Android from BackgroundSycnProxy to here.
}
DelayedProcessingInfo& BackgroundSyncScheduler::GetDelayedProcessingInfo(
blink::mojom::BackgroundSyncType sync_type) {
if (sync_type == blink::mojom::BackgroundSyncType::ONE_SHOT)
return delayed_processing_info_one_shot_;
else
return delayed_processing_info_one_shot_;
}
} // namespace content
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_SCHEDULER_H_
#define CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_SCHEDULER_H_
#include <map>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "content/common/content_export.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/blink/public/mojom/background_sync/background_sync.mojom.h"
namespace content {
class StoragePartition;
// This contains the logic to schedule delayed processing of (periodic)
// Background Sync registrations.
// It keeps track of all storage partitions, and the soonest time we should
// attempt to fire (periodic)sync events for it.
class CONTENT_EXPORT BackgroundSyncScheduler
: public base::RefCountedThreadSafe<BackgroundSyncScheduler,
BrowserThread::DeleteOnUIThread> {
public:
static BackgroundSyncScheduler* GetFor(BrowserContext* browser_context);
BackgroundSyncScheduler();
// Schedules delayed_processing for |sync_type| for |storage_partition|.
// On non-Android platforms, runs |delayed_task| after |delay| has passed.
// TODO(crbug.com/996166): Add logic to schedule browser wakeup on Android.
// Must be called on the UI thread.
virtual void ScheduleDelayedProcessing(
StoragePartition* storage_partition,
blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay,
base::OnceClosure delayed_task);
// Cancels delayed_processing for |sync_type| for |storage_partition|.
// Must be called on the UI thread.
virtual void CancelDelayedProcessing(
StoragePartition* storage_partition,
blink::mojom::BackgroundSyncType sync_type);
private:
virtual ~BackgroundSyncScheduler();
friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;
friend class base::DeleteHelper<BackgroundSyncScheduler>;
std::map<StoragePartition*, std::unique_ptr<base::OneShotTimer>>&
GetDelayedProcessingInfo(blink::mojom::BackgroundSyncType sync_type);
std::map<StoragePartition*, std::unique_ptr<base::OneShotTimer>>
delayed_processing_info_one_shot_;
std::map<StoragePartition*, std::unique_ptr<base::OneShotTimer>>
delayed_processing_info_periodic_;
DISALLOW_COPY_AND_ASSIGN(BackgroundSyncScheduler);
};
} // namespace content
#endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_SCHEDULER_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/background_sync/background_sync_scheduler.h"
#include <map>
#include <vector>
#include "base/bind.h"
#include "base/callback_forward.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "content/browser/background_sync/background_sync_scheduler.h"
#include "content/browser/storage_partition_impl.h"
#include "content/common/content_export.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/content_client.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace content {
namespace {
const char kUrl_1[] = "https://example.com";
const char kUrl_2[] = "https://whereswaldo.com";
class TestBrowserClient : public ContentBrowserClient {
public:
TestBrowserClient() = default;
~TestBrowserClient() override = default;
void GetStoragePartitionConfigForSite(BrowserContext* browser_context,
const GURL& site,
bool can_be_default,
std::string* partition_domain,
std::string* partition_name,
bool* in_memory) override {
*partition_domain = "PartitionDomain" + site.spec();
*partition_name = "Partition" + site.spec();
*in_memory = false;
}
};
} // namespace
class BackgroundSyncSchedulerTest : public testing::Test {
public:
BackgroundSyncSchedulerTest()
: task_environment_(BrowserTaskEnvironment::MainThreadType::UI) {}
void ScheduleDelayedProcessing(const GURL& url,
blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay,
base::OnceClosure delayed_task) {
auto* scheduler = BackgroundSyncScheduler::GetFor(&test_browser_context_);
DCHECK(scheduler);
auto* storage_partition =
BrowserContext::GetStoragePartitionForSite(&test_browser_context_, url);
DCHECK(storage_partition);
scheduler->ScheduleDelayedProcessing(storage_partition, sync_type, delay,
std::move(delayed_task));
}
void CancelDelayedProcessing(const GURL& url,
blink::mojom::BackgroundSyncType sync_type) {
auto* scheduler = BackgroundSyncScheduler::GetFor(&test_browser_context_);
DCHECK(scheduler);
auto* storage_partition =
BrowserContext::GetStoragePartitionForSite(&test_browser_context_, url);
DCHECK(storage_partition);
scheduler->CancelDelayedProcessing(storage_partition, sync_type);
}
void SetUp() override {
original_client_ = SetBrowserClientForTesting(&browser_client_);
}
void TearDown() override { SetBrowserClientForTesting(original_client_); }
protected:
BrowserTaskEnvironment task_environment_;
TestBrowserClient browser_client_;
ContentBrowserClient* original_client_;
TestBrowserContext test_browser_context_;
};
TEST_F(BackgroundSyncSchedulerTest, ScheduleInvokesCallback) {
base::RunLoop run_loop;
ScheduleDelayedProcessing(
GURL(kUrl_1), blink::mojom::BackgroundSyncType::ONE_SHOT,
base::TimeDelta::FromMilliseconds(1), run_loop.QuitClosure());
run_loop.Run();
}
TEST_F(BackgroundSyncSchedulerTest, ZeroDelayScheduleDoesNotInvokeCallback) {
bool was_called = false;
ScheduleDelayedProcessing(
GURL(kUrl_1), blink::mojom::BackgroundSyncType::ONE_SHOT,
base::TimeDelta(),
base::BindOnce([](bool* was_called) { *was_called = true; },
&was_called));
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(was_called);
}
TEST_F(BackgroundSyncSchedulerTest, MaxDelayScheduleDoesNotInvokeCallback) {
bool was_called = false;
ScheduleDelayedProcessing(
GURL(kUrl_1), blink::mojom::BackgroundSyncType::ONE_SHOT,
base::TimeDelta::Max(),
base::BindOnce([](bool* was_called) { *was_called = true; },
&was_called));
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(was_called);
}
TEST_F(BackgroundSyncSchedulerTest, CancelDoesNotInvokeCallback) {
bool was_called = false;
ScheduleDelayedProcessing(
GURL(kUrl_1), blink::mojom::BackgroundSyncType::ONE_SHOT,
base::TimeDelta::FromMinutes(1),
base::BindOnce([](bool* was_called) { *was_called = true; },
&was_called));
base::RunLoop().RunUntilIdle();
ASSERT_FALSE(was_called);
CancelDelayedProcessing(GURL(kUrl_1),
blink::mojom::BackgroundSyncType::ONE_SHOT);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(was_called);
}
TEST_F(BackgroundSyncSchedulerTest, SchedulingTwiceOverwritesTimer) {
bool was_called = false;
ScheduleDelayedProcessing(
GURL(kUrl_1), blink::mojom::BackgroundSyncType::ONE_SHOT,
base::TimeDelta::FromSeconds(1),
base::BindOnce([](bool* was_called) { *was_called = true; },
&was_called));
base::RunLoop().RunUntilIdle();
ASSERT_FALSE(was_called);
base::RunLoop run_loop;
ScheduleDelayedProcessing(
GURL(kUrl_1), blink::mojom::BackgroundSyncType::ONE_SHOT,
base::TimeDelta::FromMilliseconds(1), run_loop.QuitClosure());
run_loop.Run();
EXPECT_FALSE(was_called);
}
TEST_F(BackgroundSyncSchedulerTest, MultipleStoragePartitions) {
base::RunLoop run_loop_1, run_loop_2;
ScheduleDelayedProcessing(
GURL(kUrl_1), blink::mojom::BackgroundSyncType::ONE_SHOT,
base::TimeDelta::FromSeconds(1), run_loop_1.QuitClosure());
ScheduleDelayedProcessing(
GURL(kUrl_2), blink::mojom::BackgroundSyncType::ONE_SHOT,
base::TimeDelta::FromMilliseconds(1), run_loop_2.QuitClosure());
run_loop_1.Run();
run_loop_2.Run();
}
} // namespace content
......@@ -304,6 +304,8 @@ jumbo_static_library("test_support") {
"test_background_sync_context.h",
"test_background_sync_manager.cc",
"test_background_sync_manager.h",
"test_background_sync_proxy.cc",
"test_background_sync_proxy.h",
"test_blink_web_unit_test_support.cc",
"test_blink_web_unit_test_support.h",
"test_content_browser_client.cc",
......@@ -1547,6 +1549,7 @@ test("content_unittests") {
"../browser/background_sync/background_sync_manager_unittest.cc",
"../browser/background_sync/background_sync_metrics_unittest.cc",
"../browser/background_sync/background_sync_network_observer_unittest.cc",
"../browser/background_sync/background_sync_scheduler_unittest.cc",
"../browser/background_sync/background_sync_service_impl_test_harness.cc",
"../browser/background_sync/background_sync_service_impl_test_harness.h",
"../browser/background_sync/one_shot_background_sync_service_impl_unittest.cc",
......
......@@ -17,7 +17,7 @@ namespace content {
TestBackgroundSyncManager::TestBackgroundSyncManager(
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context,
scoped_refptr<DevToolsBackgroundServicesContextImpl> devtools_context)
: BackgroundSyncManager(std::move(service_worker_context),
: BackgroundSyncManager(service_worker_context,
std::move(devtools_context)) {}
TestBackgroundSyncManager::~TestBackgroundSyncManager() {}
......@@ -94,15 +94,6 @@ void TestBackgroundSyncManager::DispatchPeriodicSyncEvent(
dispatch_periodic_sync_callback_.Run(active_version, std::move(callback));
}
void TestBackgroundSyncManager::ScheduleDelayedTask(
blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay) {
if (sync_type == blink::mojom::BackgroundSyncType::ONE_SHOT)
delayed_one_shot_sync_task_delta_ = delay;
else
delayed_periodic_sync_task_delta_ = delay;
}
void TestBackgroundSyncManager::HasMainFrameProviderHost(
const url::Origin& origin,
BoolCallback callback) {
......
......@@ -83,47 +83,12 @@ class TestBackgroundSyncManager : public BackgroundSyncManager {
has_main_frame_provider_host_ = value;
}
bool IsDelayedTaskScheduledOneShotSync() const {
return !delayed_one_shot_sync_task_.callback().is_null();
}
bool IsDelayedTaskScheduledPeriodicSync() const {
return !delayed_periodic_sync_task_.callback().is_null();
}
void RunOneShotSyncDelayedTask() {
std::move(delayed_one_shot_sync_task_.callback()).Run();
}
void RunPeriodicSyncDelayedTask() {
std::move(delayed_periodic_sync_task_.callback()).Run();
}
// Accessors to internal state
base::TimeDelta delayed_one_shot_sync_task_delta() const {
return delayed_one_shot_sync_task_delta_;
}
base::TimeDelta delayed_periodic_sync_task_delta() const {
return delayed_periodic_sync_task_delta_;
}
bool last_chance() const { return last_chance_; }
const BackgroundSyncParameters* background_sync_parameters() const {
return parameters_.get();
}
bool IsBrowserWakeupForOneShotSyncScheduled() const {
return delayed_processing_scheduled_one_shot_sync_;
}
bool IsBrowserWakeupForPeriodicSyncScheduled() const {
return delayed_processing_scheduled_periodic_sync_;
}
bool EqualsSoonestOneShotWakeupDelta(base::TimeDelta compare_to) const {
return soonest_one_shot_sync_wakeup_delta_ == compare_to;
}
bool EqualsSoonestPeriodicSyncWakeupDelta(base::TimeDelta compare_to) const {
return soonest_periodic_sync_wakeup_delta_ == compare_to;
}
void DispatchPeriodicSyncEvent(
const std::string& tag,
scoped_refptr<ServiceWorkerVersion> active_version,
......@@ -157,11 +122,6 @@ class TestBackgroundSyncManager : public BackgroundSyncManager {
bool last_chance,
ServiceWorkerVersion::StatusCallback callback) override;
// Override to just store delayed task, and allow tests to control the clock
// and when delayed tasks are executed.
void ScheduleDelayedTask(blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay) override;
// Override to avoid actual check for main frame, instead return the value set
// by tests.
void HasMainFrameProviderHost(const url::Origin& origin,
......@@ -190,8 +150,6 @@ class TestBackgroundSyncManager : public BackgroundSyncManager {
base::OnceClosure continuation_;
DispatchSyncCallback dispatch_sync_callback_;
DispatchSyncCallback dispatch_periodic_sync_callback_;
base::TimeDelta delayed_one_shot_sync_task_delta_;
base::TimeDelta delayed_periodic_sync_task_delta_;
base::TimeDelta soonest_one_shot_sync_wakeup_delta_;
base::TimeDelta soonest_periodic_sync_wakeup_delta_;
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <content/test/test_background_sync_proxy.h>
namespace content {
TestBackgroundSyncProxy::~TestBackgroundSyncProxy() {}
void TestBackgroundSyncProxy::ScheduleDelayedProcessing(
blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay,
base::OnceClosure delayed_task) {
auto& delay_timer = GetDelayedTimer(sync_type);
if (delay.is_max())
delay_timer.AbandonAndStop();
else if (!delay.is_zero())
delay_timer.Start(FROM_HERE, delay, std::move(delayed_task));
}
} // namespace content
\ No newline at end of file
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_TEST_TEST_BACKGROUND_SYNC_PROXY_H_
#define CONTENT_TEST_TEST_BACKGROUND_SYNC_PROXY_H_
#include "base/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "base/time/time.h"
#include "content/browser/background_sync/background_sync_proxy.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
namespace content {
class TestBackgroundSyncProxy : public BackgroundSyncProxy {
public:
explicit TestBackgroundSyncProxy(
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context)
: BackgroundSyncProxy(service_worker_context) {}
~TestBackgroundSyncProxy() override;
void ScheduleDelayedProcessing(blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay,
base::OnceClosure delayed_task) override;
base::OneShotTimer& GetDelayedTimer(
blink::mojom::BackgroundSyncType sync_type) {
if (sync_type == blink::mojom::BackgroundSyncType::ONE_SHOT)
return one_shot_sync_delay_timer_;
else
return periodic_sync_delay_timer_;
}
void RunDelayedTask(blink::mojom::BackgroundSyncType sync_type) {
GetDelayedTimer(sync_type).FireNow();
}
bool IsDelayedTaskSet(blink::mojom::BackgroundSyncType sync_type) {
return GetDelayedTimer(sync_type).IsRunning();
}
base::TimeDelta GetDelay(blink::mojom::BackgroundSyncType sync_type) {
return GetDelayedTimer(sync_type).GetCurrentDelay();
}
private:
base::OneShotTimer one_shot_sync_delay_timer_;
base::OneShotTimer periodic_sync_delay_timer_;
};
} // namespace content
#endif // CONTENT_TEST_TEST_BACKGROUND_SYNC_PROXY_H_
\ No newline at end of file
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