Commit 36b90380 authored by jkarlin's avatar jkarlin Committed by Commit bot

[BackgroundSync] Add new registration options from the spec

This CL brings the BackgroundSyncRegistration options up-to-date with the spec: https://github.com/slightlyoff/BackgroundSync/pull/74

TODO in followup CLs:
* Rename 'name' to 'tag': http://crrev.com/1068863003.
* Give periodic and one-shot tags their own namespaces in the map so that they don't conflict.

BUG=474573

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

Cr-Commit-Position: refs/heads/master@{#324077}
parent 8363aa5d
...@@ -8,10 +8,24 @@ option optimize_for = LITE_RUNTIME; ...@@ -8,10 +8,24 @@ option optimize_for = LITE_RUNTIME;
package content; package content;
enum SyncNetworkState {
NETWORK_STATE_ANY = 0;
NETWORK_STATE_AVOID_CELLULAR = 1;
NETWORK_STATE_ONLINE = 2;
}
enum SyncPowerState {
POWER_STATE_AUTO = 0;
POWER_STATE_AVOID_DRAINING = 1;
}
message BackgroundSyncRegistrationProto { message BackgroundSyncRegistrationProto {
required int64 id = 1; required int64 id = 1;
required string name = 2; required string name = 2;
optional int64 min_period = 3; required bool fire_once = 3;
required int64 min_period = 4;
required SyncNetworkState network_state = 5;
required SyncPowerState power_state = 6;
} }
message BackgroundSyncRegistrationsProto { message BackgroundSyncRegistrationsProto {
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#include "base/barrier_closure.h" #include "base/barrier_closure.h"
#include "base/bind.h" #include "base/bind.h"
#include "content/browser/background_sync/background_sync.pb.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h" #include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/service_worker/service_worker_storage.h" #include "content/browser/service_worker/service_worker_storage.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
...@@ -194,12 +193,15 @@ void BackgroundSyncManager::InitDidGetDataFromBackend( ...@@ -194,12 +193,15 @@ void BackgroundSyncManager::InitDidGetDataFromBackend(
break; break;
} }
BackgroundSyncRegistration registration(registration_proto.id(), BackgroundSyncRegistration* registration =
registration_proto.name()); &registrations->name_to_registration_map[registration_proto.name()];
if (registration_proto.has_min_period())
registration.min_period = registration_proto.min_period(); registration->id = registration_proto.id();
registrations->name_to_registration_map[registration_proto.name()] = registration->name = registration_proto.name();
registration; registration->fire_once = registration_proto.fire_once();
registration->min_period = registration_proto.min_period();
registration->network_state = registration_proto.network_state();
registration->power_state = registration_proto.power_state();
} }
} }
...@@ -342,8 +344,10 @@ void BackgroundSyncManager::StoreRegistrations( ...@@ -342,8 +344,10 @@ void BackgroundSyncManager::StoreRegistrations(
registrations_proto.add_registration(); registrations_proto.add_registration();
registration_proto->set_id(registration.id); registration_proto->set_id(registration.id);
registration_proto->set_name(registration.name); registration_proto->set_name(registration.name);
if (registration.min_period != 0) registration_proto->set_fire_once(registration.fire_once);
registration_proto->set_min_period(registration.min_period); registration_proto->set_min_period(registration.min_period);
registration_proto->set_network_state(registration.network_state);
registration_proto->set_power_state(registration.power_state);
} }
std::string serialized; std::string serialized;
bool success = registrations_proto.SerializeToString(&serialized); bool success = registrations_proto.SerializeToString(&serialized);
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/callback_forward.h" #include "base/callback_forward.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "content/browser/background_sync/background_sync.pb.h"
#include "content/browser/cache_storage/cache_storage_scheduler.h" #include "content/browser/cache_storage/cache_storage_scheduler.h"
#include "content/browser/service_worker/service_worker_context_observer.h" #include "content/browser/service_worker/service_worker_context_observer.h"
#include "content/browser/service_worker/service_worker_storage.h" #include "content/browser/service_worker/service_worker_storage.h"
...@@ -51,21 +52,21 @@ class CONTENT_EXPORT BackgroundSyncManager ...@@ -51,21 +52,21 @@ class CONTENT_EXPORT BackgroundSyncManager
struct CONTENT_EXPORT BackgroundSyncRegistration { struct CONTENT_EXPORT BackgroundSyncRegistration {
using RegistrationId = int64; using RegistrationId = int64;
static const RegistrationId kInvalidRegistrationId; static const RegistrationId kInvalidRegistrationId;
BackgroundSyncRegistration() {}
BackgroundSyncRegistration()
: BackgroundSyncRegistration(kInvalidRegistrationId, "") {}
explicit BackgroundSyncRegistration(const std::string& name)
: BackgroundSyncRegistration(kInvalidRegistrationId, name) {}
BackgroundSyncRegistration(int64 id, const std::string& name)
: id(id), min_period(0), name(name) {}
bool Equals(const BackgroundSyncRegistration& other) { bool Equals(const BackgroundSyncRegistration& other) {
return this->name == other.name && this->min_period == other.min_period; return this->name == other.name && this->fire_once == other.fire_once &&
this->min_period == other.min_period &&
network_state == other.network_state &&
power_state == other.power_state;
} }
RegistrationId id; RegistrationId id = kInvalidRegistrationId;
int64 min_period;
std::string name; std::string name;
bool fire_once = true;
int64 min_period = 0;
SyncNetworkState network_state = NETWORK_STATE_ONLINE;
SyncPowerState power_state = POWER_STATE_AVOID_DRAINING;
}; };
struct CONTENT_EXPORT BackgroundSyncRegistrations { struct CONTENT_EXPORT BackgroundSyncRegistrations {
......
...@@ -137,10 +137,13 @@ class BackgroundSyncManagerTest : public testing::Test { ...@@ -137,10 +137,13 @@ class BackgroundSyncManagerTest : public testing::Test {
public: public:
BackgroundSyncManagerTest() BackgroundSyncManagerTest()
: browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
sync_reg_1_(BackgroundSyncManager::BackgroundSyncRegistration("foo")), sync_reg_1_(BackgroundSyncManager::BackgroundSyncRegistration()),
sync_reg_2_(BackgroundSyncManager::BackgroundSyncRegistration("bar")), sync_reg_2_(BackgroundSyncManager::BackgroundSyncRegistration()),
callback_error_(BackgroundSyncManager::ERROR_TYPE_OK), callback_error_(BackgroundSyncManager::ERROR_TYPE_OK),
callback_sw_status_code_(SERVICE_WORKER_OK) {} callback_sw_status_code_(SERVICE_WORKER_OK) {
sync_reg_1_.name = "foo";
sync_reg_2_.name = "bar";
}
void SetUp() override { void SetUp() override {
helper_.reset( helper_.reset(
...@@ -619,4 +622,77 @@ TEST_F(BackgroundSyncManagerTest, DisabledManagerWorksAfterDeleteAndStartOver) { ...@@ -619,4 +622,77 @@ TEST_F(BackgroundSyncManagerTest, DisabledManagerWorksAfterDeleteAndStartOver) {
EXPECT_TRUE(GetRegistration(sync_reg_2_.name)); EXPECT_TRUE(GetRegistration(sync_reg_2_.name));
} }
TEST_F(BackgroundSyncManagerTest, RegistrationEqualsId) {
BackgroundSyncManager::BackgroundSyncRegistration reg_1;
BackgroundSyncManager::BackgroundSyncRegistration reg_2;
EXPECT_TRUE(reg_1.Equals(reg_2));
reg_2.id = reg_1.id + 1;
EXPECT_TRUE(reg_1.Equals(reg_2));
}
TEST_F(BackgroundSyncManagerTest, RegistrationEqualsName) {
BackgroundSyncManager::BackgroundSyncRegistration reg_1;
BackgroundSyncManager::BackgroundSyncRegistration reg_2;
EXPECT_TRUE(reg_1.Equals(reg_2));
reg_2.name = "bar";
EXPECT_FALSE(reg_1.Equals(reg_2));
}
TEST_F(BackgroundSyncManagerTest, RegistrationEqualsFireOnce) {
BackgroundSyncManager::BackgroundSyncRegistration reg_1;
BackgroundSyncManager::BackgroundSyncRegistration reg_2;
EXPECT_TRUE(reg_1.Equals(reg_2));
reg_2.fire_once = !reg_1.fire_once;
EXPECT_FALSE(reg_1.Equals(reg_2));
}
TEST_F(BackgroundSyncManagerTest, RegistrationEqualsMinPeriod) {
BackgroundSyncManager::BackgroundSyncRegistration reg_1;
BackgroundSyncManager::BackgroundSyncRegistration reg_2;
EXPECT_TRUE(reg_1.Equals(reg_2));
reg_2.min_period = reg_1.min_period + 1;
EXPECT_FALSE(reg_1.Equals(reg_2));
}
TEST_F(BackgroundSyncManagerTest, RegistrationEqualsNetworkState) {
BackgroundSyncManager::BackgroundSyncRegistration reg_1;
BackgroundSyncManager::BackgroundSyncRegistration reg_2;
EXPECT_TRUE(reg_1.Equals(reg_2));
reg_1.network_state = NETWORK_STATE_ANY;
reg_2.network_state = NETWORK_STATE_ONLINE;
EXPECT_FALSE(reg_1.Equals(reg_2));
}
TEST_F(BackgroundSyncManagerTest, RegistrationEqualsPowerState) {
BackgroundSyncManager::BackgroundSyncRegistration reg_1;
BackgroundSyncManager::BackgroundSyncRegistration reg_2;
EXPECT_TRUE(reg_1.Equals(reg_2));
reg_1.power_state = POWER_STATE_AUTO;
reg_2.power_state = POWER_STATE_AVOID_DRAINING;
EXPECT_FALSE(reg_1.Equals(reg_2));
}
TEST_F(BackgroundSyncManagerTest, StoreAndRetrievePreservesValues) {
BackgroundSyncManager::BackgroundSyncRegistration reg_1;
// Set non-default values for each field.
reg_1.name = "foo";
reg_1.fire_once = !reg_1.fire_once;
reg_1.min_period += 1;
EXPECT_NE(NETWORK_STATE_ANY, reg_1.network_state);
reg_1.network_state = NETWORK_STATE_ANY;
EXPECT_NE(POWER_STATE_AUTO, reg_1.power_state);
reg_1.power_state = POWER_STATE_AUTO;
// Store the registration.
EXPECT_TRUE(Register(reg_1));
// Simulate restarting the sync manager, forcing the next read to come from
// disk.
UseTestBackgroundSyncManager();
EXPECT_TRUE(GetRegistration(reg_1.name));
EXPECT_TRUE(reg_1.Equals(callback_registration_));
}
} // namespace content } // namespace content
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