Commit 3a3f0e03 authored by Mugdha Lakhani's avatar Mugdha Lakhani Committed by Commit Bot

[Background Sync] Track suspended origins

Add logic to track origins whose periodic Background Sync registrations
have been suspended due to the users engagement with their site falling
to zero.

This will be used to revive the origins we care about.

Bug: 925297, 982378
Change-Id: I04c2644feeab77092435d0db223ca3cee841e406
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1710548
Commit-Queue: Avi Drissman <avi@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarRayan Kanso <rayankans@chromium.org>
Auto-Submit: Mugdha Lakhani <nator@chromium.org>
Cr-Commit-Position: refs/heads/master@{#680018}
parent e936f65f
......@@ -191,10 +191,13 @@ void BackgroundSyncControllerImpl::ScheduleBrowserWakeUp(
#endif
}
int BackgroundSyncControllerImpl::GetSiteEngagementPenalty(
const GURL& url) const {
int BackgroundSyncControllerImpl::GetSiteEngagementPenalty(const GURL& url) {
blink::mojom::EngagementLevel engagement_level =
site_engagement_service_->GetEngagementLevel(url);
if (engagement_level == blink::mojom::EngagementLevel::NONE) {
suspended_periodic_sync_origins_.insert(
url::Origin::Create(url.GetOrigin()));
}
switch (engagement_level) {
case blink::mojom::EngagementLevel::NONE:
......@@ -283,3 +286,11 @@ BackgroundSyncControllerImpl::BackgroundSyncEventKeepAliveImpl::
BackgroundSyncControllerImpl::BackgroundSyncEventKeepAliveImpl::
~BackgroundSyncEventKeepAliveImpl() = default;
#endif
void BackgroundSyncControllerImpl::NoteSuspendedPeriodicSyncOrigins(
std::set<url::Origin> suspended_origins) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
for (auto& origin : suspended_origins)
suspended_periodic_sync_origins_.insert(std::move(origin));
}
......@@ -9,6 +9,8 @@
#include <stdint.h>
#include <set>
#include "base/macros.h"
#include "base/time/time.h"
#include "build/build_config.h"
......@@ -19,6 +21,7 @@
#include "content/public/browser/background_sync_registration.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/blink/public/mojom/background_sync/background_sync.mojom.h"
#include "url/gurl.h"
namespace content {
struct BackgroundSyncParameters;
......@@ -92,6 +95,8 @@ class BackgroundSyncControllerImpl : public content::BackgroundSyncController,
content::BackgroundSyncParameters* parameters) override;
std::unique_ptr<BackgroundSyncEventKeepAlive>
CreateBackgroundSyncEventKeepAlive() override;
void NoteSuspendedPeriodicSyncOrigins(
std::set<url::Origin> suspended_origins) override;
private:
// Gets the site engagement penalty for |url|, which is inversely proportional
......@@ -99,7 +104,7 @@ class BackgroundSyncControllerImpl : public content::BackgroundSyncController,
// the less often periodic sync events will be fired.
// Returns kEngagementLevelNonePenalty if the engagement level is
// blink::mojom::EngagementLevel::NONE.
int GetSiteEngagementPenalty(const GURL& url) const;
int GetSiteEngagementPenalty(const GURL& url);
// Once we've identified the minimum number of hours between each periodicsync
// event for an origin, every delay calculated for the origin should be a
......@@ -114,6 +119,8 @@ class BackgroundSyncControllerImpl : public content::BackgroundSyncController,
BackgroundSyncMetrics background_sync_metrics_;
std::set<url::Origin> suspended_periodic_sync_origins_;
DISALLOW_COPY_AND_ASSIGN(BackgroundSyncControllerImpl);
};
......
......@@ -603,6 +603,7 @@ void BackgroundSyncManager::InitDidGetDataFromBackend(
return;
}
std::set<url::Origin> suspended_periodic_sync_origins;
for (const std::pair<int64_t, std::string>& data : user_data) {
BackgroundSyncRegistrationsProto registrations_proto;
if (registrations_proto.ParseFromString(data.second)) {
......@@ -638,6 +639,9 @@ void BackgroundSyncManager::InitDidGetDataFromBackend(
registration->set_num_attempts(registration_proto.num_attempts());
registration->set_delay_until(
base::Time::FromInternalValue(registration_proto.delay_until()));
if (registration->is_suspended()) {
suspended_periodic_sync_origins.insert(registration->origin());
}
registration->set_resolved();
if (registration_proto.has_max_attempts())
registration->set_max_attempts(registration_proto.max_attempts());
......@@ -649,6 +653,8 @@ void BackgroundSyncManager::InitDidGetDataFromBackend(
FireReadyEvents(BackgroundSyncType::ONE_SHOT, base::DoNothing::Once());
FireReadyEvents(BackgroundSyncType::PERIODIC, base::DoNothing::Once());
proxy_.SendSuspendedPeriodicSyncOrigins(
std::move(suspended_periodic_sync_origins));
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, std::move(callback));
}
......
......@@ -65,6 +65,18 @@ class BackgroundSyncProxy::Core {
controller->ScheduleBrowserWakeUp(sync_type);
}
void SendSuspendedPeriodicSyncOrigins(
std::set<url::Origin> suspended_origins) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!browser_context())
return;
auto* controller = browser_context()->GetBackgroundSyncController();
DCHECK(controller);
controller->NoteSuspendedPeriodicSyncOrigins(std::move(suspended_origins));
}
private:
base::WeakPtr<BackgroundSyncProxy> io_parent_;
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
......@@ -95,4 +107,14 @@ void BackgroundSyncProxy::ScheduleBrowserWakeUp(
ui_core_weak_ptr_, sync_type));
}
void BackgroundSyncProxy::SendSuspendedPeriodicSyncOrigins(
std::set<url::Origin> suspended_origins) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(&Core::SendSuspendedPeriodicSyncOrigins, ui_core_weak_ptr_,
std::move(suspended_origins)));
}
} // namespace content
......@@ -6,6 +6,7 @@
#define CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_PROXY_H_
#include <memory>
#include <set>
#include "base/callback.h"
#include "base/memory/weak_ptr.h"
......@@ -13,6 +14,7 @@
#include "content/common/content_export.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/blink/public/mojom/background_sync/background_sync.mojom.h"
#include "url/origin.h"
namespace content {
......@@ -29,6 +31,8 @@ class CONTENT_EXPORT BackgroundSyncProxy {
~BackgroundSyncProxy();
void ScheduleBrowserWakeUp(blink::mojom::BackgroundSyncType sync_type);
void SendSuspendedPeriodicSyncOrigins(
std::set<url::Origin> suspended_origins);
private:
// Constructed on the IO thread, lives and dies on the UI thread.
......
......@@ -7,6 +7,8 @@
#include <stdint.h>
#include <set>
#include "base/time/time.h"
#include "content/common/content_export.h"
#include "content/public/browser/background_sync_registration.h"
......@@ -91,6 +93,13 @@ class CONTENT_EXPORT BackgroundSyncController {
// to finish firing one sync event.
virtual std::unique_ptr<BackgroundSyncEventKeepAlive>
CreateBackgroundSyncEventKeepAlive() = 0;
// Updates its internal list of origins for which we have suspended periodic
// Background Sync registrations. This is compiled from each
// BackgroundSyncManager when they are initialized. This list used to ignore
// changes concerning origins we don't care about.
virtual void NoteSuspendedPeriodicSyncOrigins(
std::set<url::Origin> suspended_origins) = 0;
};
} // namespace content
......
......@@ -62,6 +62,12 @@ class CONTENT_EXPORT BackgroundSyncRegistration {
void set_origin(const url::Origin& origin) { origin_ = origin; }
bool is_suspended() const {
if (sync_type() == blink::mojom::BackgroundSyncType::ONE_SHOT)
return false;
return delay_until_.is_max();
}
private:
blink::mojom::SyncRegistrationOptions options_;
blink::mojom::BackgroundSyncState sync_state_ =
......
......@@ -6,6 +6,9 @@
namespace content {
MockBackgroundSyncController::MockBackgroundSyncController() = default;
MockBackgroundSyncController::~MockBackgroundSyncController() = default;
void MockBackgroundSyncController::NotifyOneShotBackgroundSyncRegistered(
const url::Origin& origin,
bool can_fire,
......@@ -59,4 +62,11 @@ MockBackgroundSyncController::CreateBackgroundSyncEventKeepAlive() {
return nullptr;
}
void MockBackgroundSyncController::NoteSuspendedPeriodicSyncOrigins(
std::set<url::Origin> suspended_origins) {
for (auto& origin : suspended_origins) {
suspended_periodic_sync_origins_.insert(std::move(origin));
}
}
} // namespace content
......@@ -20,8 +20,8 @@ namespace content {
// Mocks a BackgroundSyncController, tracking state for use in tests.
class MockBackgroundSyncController : public BackgroundSyncController {
public:
MockBackgroundSyncController() = default;
~MockBackgroundSyncController() override = default;
MockBackgroundSyncController();
~MockBackgroundSyncController() override;
// BackgroundSyncController:
void NotifyOneShotBackgroundSyncRegistered(const url::Origin& origin,
......@@ -35,6 +35,8 @@ class MockBackgroundSyncController : public BackgroundSyncController {
BackgroundSyncParameters* parameters) override;
std::unique_ptr<BackgroundSyncController::BackgroundSyncEventKeepAlive>
CreateBackgroundSyncEventKeepAlive() override;
void NoteSuspendedPeriodicSyncOrigins(
std::set<url::Origin> suspended_registrations) override;
int registration_count() const { return registration_count_; }
const url::Origin& registration_origin() const {
......@@ -57,6 +59,7 @@ class MockBackgroundSyncController : public BackgroundSyncController {
int run_in_background_for_one_shot_sync_count_ = 0;
int run_in_background_for_periodic_sync_count_ = 0;
BackgroundSyncParameters background_sync_parameters_;
std::set<url::Origin> suspended_periodic_sync_origins_;
DISALLOW_COPY_AND_ASSIGN(MockBackgroundSyncController);
};
......
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