Commit c1a361d9 authored by jkarlin's avatar jkarlin Committed by Commit bot

[BackgroundSyncManager] Remove origin argument from public BackgroundSyncManager methods

The origin can be determined from the live ServiceWorkerRegistration,
use that instead of requiring the renderer to pass it.

Also, verify that the live registration is active to prevent
BackgroundSyncManager::Register from being called prematurely.

BUG=482012

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

Cr-Commit-Position: refs/heads/master@{#327371}
parent 019a57a8
...@@ -62,7 +62,6 @@ BackgroundSyncManager::RegistrationKey::RegistrationKey( ...@@ -62,7 +62,6 @@ BackgroundSyncManager::RegistrationKey::RegistrationKey(
} }
void BackgroundSyncManager::Register( void BackgroundSyncManager::Register(
const GURL& origin,
int64 sw_registration_id, int64 sw_registration_id,
const BackgroundSyncRegistration& sync_registration, const BackgroundSyncRegistration& sync_registration,
const StatusAndRegistrationCallback& callback) { const StatusAndRegistrationCallback& callback) {
...@@ -79,12 +78,11 @@ void BackgroundSyncManager::Register( ...@@ -79,12 +78,11 @@ void BackgroundSyncManager::Register(
op_scheduler_.ScheduleOperation(base::Bind( op_scheduler_.ScheduleOperation(base::Bind(
&BackgroundSyncManager::RegisterImpl, weak_ptr_factory_.GetWeakPtr(), &BackgroundSyncManager::RegisterImpl, weak_ptr_factory_.GetWeakPtr(),
origin, sw_registration_id, sync_registration, sw_registration_id, sync_registration,
MakeStatusAndRegistrationCompletion(callback))); MakeStatusAndRegistrationCompletion(callback)));
} }
void BackgroundSyncManager::Unregister( void BackgroundSyncManager::Unregister(
const GURL& origin,
int64 sw_registration_id, int64 sw_registration_id,
const std::string& sync_registration_tag, const std::string& sync_registration_tag,
SyncPeriodicity periodicity, SyncPeriodicity periodicity,
...@@ -102,12 +100,11 @@ void BackgroundSyncManager::Unregister( ...@@ -102,12 +100,11 @@ void BackgroundSyncManager::Unregister(
op_scheduler_.ScheduleOperation(base::Bind( op_scheduler_.ScheduleOperation(base::Bind(
&BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(),
origin, sw_registration_id, registration_key, sync_registration_id, sw_registration_id, registration_key, sync_registration_id,
MakeStatusCompletion(callback))); MakeStatusCompletion(callback)));
} }
void BackgroundSyncManager::GetRegistration( void BackgroundSyncManager::GetRegistration(
const GURL& origin,
int64 sw_registration_id, int64 sw_registration_id,
const std::string sync_registration_tag, const std::string sync_registration_tag,
SyncPeriodicity periodicity, SyncPeriodicity periodicity,
...@@ -125,8 +122,8 @@ void BackgroundSyncManager::GetRegistration( ...@@ -125,8 +122,8 @@ void BackgroundSyncManager::GetRegistration(
op_scheduler_.ScheduleOperation(base::Bind( op_scheduler_.ScheduleOperation(base::Bind(
&BackgroundSyncManager::GetRegistrationImpl, &BackgroundSyncManager::GetRegistrationImpl,
weak_ptr_factory_.GetWeakPtr(), origin, sw_registration_id, weak_ptr_factory_.GetWeakPtr(), sw_registration_id, registration_key,
registration_key, MakeStatusAndRegistrationCompletion(callback))); MakeStatusAndRegistrationCompletion(callback)));
} }
void BackgroundSyncManager::OnRegistrationDeleted(int64 registration_id, void BackgroundSyncManager::OnRegistrationDeleted(int64 registration_id,
...@@ -248,7 +245,6 @@ void BackgroundSyncManager::InitDidGetDataFromBackend( ...@@ -248,7 +245,6 @@ void BackgroundSyncManager::InitDidGetDataFromBackend(
} }
void BackgroundSyncManager::RegisterImpl( void BackgroundSyncManager::RegisterImpl(
const GURL& origin,
int64 sw_registration_id, int64 sw_registration_id,
const BackgroundSyncRegistration& sync_registration, const BackgroundSyncRegistration& sync_registration,
const StatusAndRegistrationCallback& callback) { const StatusAndRegistrationCallback& callback) {
...@@ -273,7 +269,18 @@ void BackgroundSyncManager::RegisterImpl( ...@@ -273,7 +269,18 @@ void BackgroundSyncManager::RegisterImpl(
&sw_to_registrations_map_[sw_registration_id]; &sw_to_registrations_map_[sw_registration_id];
new_registration.id = registrations->next_id++; new_registration.id = registrations->next_id++;
AddRegistrationToMap(sw_registration_id, origin, new_registration); ServiceWorkerRegistration* sw_registration =
service_worker_context_->GetLiveRegistration(sw_registration_id);
if (!sw_registration || !sw_registration->active_version()) {
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(callback, ERROR_TYPE_NO_SERVICE_WORKER,
BackgroundSyncRegistration()));
return;
}
AddRegistrationToMap(sw_registration_id,
sw_registration->pattern().GetOrigin(),
new_registration);
StoreRegistrations( StoreRegistrations(
sw_registration_id, sw_registration_id,
...@@ -454,7 +461,6 @@ void BackgroundSyncManager::GetDataFromBackend( ...@@ -454,7 +461,6 @@ void BackgroundSyncManager::GetDataFromBackend(
} }
void BackgroundSyncManager::UnregisterImpl( void BackgroundSyncManager::UnregisterImpl(
const GURL& origin,
int64 sw_registration_id, int64 sw_registration_id,
const RegistrationKey& registration_key, const RegistrationKey& registration_key,
BackgroundSyncRegistration::RegistrationId sync_registration_id, BackgroundSyncRegistration::RegistrationId sync_registration_id,
...@@ -506,7 +512,6 @@ void BackgroundSyncManager::UnregisterDidStore( ...@@ -506,7 +512,6 @@ void BackgroundSyncManager::UnregisterDidStore(
} }
void BackgroundSyncManager::GetRegistrationImpl( void BackgroundSyncManager::GetRegistrationImpl(
const GURL& origin,
int64 sw_registration_id, int64 sw_registration_id,
const RegistrationKey& registration_key, const RegistrationKey& registration_key,
const StatusAndRegistrationCallback& callback) { const StatusAndRegistrationCallback& callback) {
......
...@@ -45,7 +45,8 @@ class CONTENT_EXPORT BackgroundSyncManager ...@@ -45,7 +45,8 @@ class CONTENT_EXPORT BackgroundSyncManager
enum ErrorType { enum ErrorType {
ERROR_TYPE_OK = 0, ERROR_TYPE_OK = 0,
ERROR_TYPE_STORAGE, ERROR_TYPE_STORAGE,
ERROR_TYPE_NOT_FOUND ERROR_TYPE_NOT_FOUND,
ERROR_TYPE_NO_SERVICE_WORKER
}; };
// TODO(jkarlin): Remove this and use the struct from IPC messages once it // TODO(jkarlin): Remove this and use the struct from IPC messages once it
...@@ -85,8 +86,7 @@ class CONTENT_EXPORT BackgroundSyncManager ...@@ -85,8 +86,7 @@ class CONTENT_EXPORT BackgroundSyncManager
// with ErrorTypeOK and the accepted registration on success. The accepted // with ErrorTypeOK and the accepted registration on success. The accepted
// registration will have a unique id. It may also have altered parameters if // registration will have a unique id. It may also have altered parameters if
// the user or UA chose different parameters than those supplied. // the user or UA chose different parameters than those supplied.
void Register(const GURL& origin, void Register(int64 sw_registration_id,
int64 sw_registration_id,
const BackgroundSyncRegistration& sync_registration, const BackgroundSyncRegistration& sync_registration,
const StatusAndRegistrationCallback& callback); const StatusAndRegistrationCallback& callback);
...@@ -95,7 +95,6 @@ class CONTENT_EXPORT BackgroundSyncManager ...@@ -95,7 +95,6 @@ class CONTENT_EXPORT BackgroundSyncManager
// ErrorTypeNotFound if no match is found. Calls |callback| with ErrorTypeOK // ErrorTypeNotFound if no match is found. Calls |callback| with ErrorTypeOK
// on success. // on success.
void Unregister( void Unregister(
const GURL& origin,
int64 sw_registration_id, int64 sw_registration_id,
const std::string& sync_registration_tag, const std::string& sync_registration_tag,
SyncPeriodicity periodicity, SyncPeriodicity periodicity,
...@@ -106,8 +105,7 @@ class CONTENT_EXPORT BackgroundSyncManager ...@@ -106,8 +105,7 @@ class CONTENT_EXPORT BackgroundSyncManager
// |sw_registration_id| with periodicity |periodicity|. Calls // |sw_registration_id| with periodicity |periodicity|. Calls
// |callback| with ErrorTypeNotFound if it doesn't exist. Calls |callback| // |callback| with ErrorTypeNotFound if it doesn't exist. Calls |callback|
// with ErrorTypeOK on success. // with ErrorTypeOK on success.
void GetRegistration(const GURL& origin, void GetRegistration(int64 sw_registration_id,
int64 sw_registration_id,
const std::string sync_registration_tag, const std::string sync_registration_tag,
SyncPeriodicity periodicity, SyncPeriodicity periodicity,
const StatusAndRegistrationCallback& callback); const StatusAndRegistrationCallback& callback);
...@@ -206,8 +204,7 @@ class CONTENT_EXPORT BackgroundSyncManager ...@@ -206,8 +204,7 @@ class CONTENT_EXPORT BackgroundSyncManager
ServiceWorkerStatusCode status); ServiceWorkerStatusCode status);
// Register callbacks // Register callbacks
void RegisterImpl(const GURL& origin, void RegisterImpl(int64 sw_registration_id,
int64 sw_registration_id,
const BackgroundSyncRegistration& sync_registration, const BackgroundSyncRegistration& sync_registration,
const StatusAndRegistrationCallback& callback); const StatusAndRegistrationCallback& callback);
void RegisterDidStore(int64 sw_registration_id, void RegisterDidStore(int64 sw_registration_id,
...@@ -217,7 +214,6 @@ class CONTENT_EXPORT BackgroundSyncManager ...@@ -217,7 +214,6 @@ class CONTENT_EXPORT BackgroundSyncManager
// Unregister callbacks // Unregister callbacks
void UnregisterImpl( void UnregisterImpl(
const GURL& origin,
int64 sw_registration_id, int64 sw_registration_id,
const RegistrationKey& registration_key, const RegistrationKey& registration_key,
BackgroundSyncRegistration::RegistrationId sync_registration_id, BackgroundSyncRegistration::RegistrationId sync_registration_id,
...@@ -228,8 +224,7 @@ class CONTENT_EXPORT BackgroundSyncManager ...@@ -228,8 +224,7 @@ class CONTENT_EXPORT BackgroundSyncManager
ServiceWorkerStatusCode status); ServiceWorkerStatusCode status);
// GetRegistration callbacks // GetRegistration callbacks
void GetRegistrationImpl(const GURL& origin, void GetRegistrationImpl(int64 sw_registration_id,
int64 sw_registration_id,
const RegistrationKey& registration_key, const RegistrationKey& registration_key,
const StatusAndRegistrationCallback& callback); const StatusAndRegistrationCallback& callback);
......
...@@ -20,7 +20,6 @@ namespace content { ...@@ -20,7 +20,6 @@ namespace content {
namespace { namespace {
const char kOrigin[] = "https://example.com";
const char kPattern1[] = "https://example.com/a"; const char kPattern1[] = "https://example.com/a";
const char kPattern2[] = "https://example.com/b"; const char kPattern2[] = "https://example.com/b";
const char kScript1[] = "https://example.com/a/script.js"; const char kScript1[] = "https://example.com/a/script.js";
...@@ -37,6 +36,14 @@ void RegisterServiceWorkerCallback(bool* called, ...@@ -37,6 +36,14 @@ void RegisterServiceWorkerCallback(bool* called,
*store_registration_id = registration_id; *store_registration_id = registration_id;
} }
void FindServiceWorkerRegistrationCallback(
scoped_refptr<ServiceWorkerRegistration>* out_registration,
ServiceWorkerStatusCode status,
const scoped_refptr<ServiceWorkerRegistration>& registration) {
EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status);
*out_registration = registration;
}
void UnregisterServiceWorkerCallback(bool* called, void UnregisterServiceWorkerCallback(bool* called,
ServiceWorkerStatusCode code) { ServiceWorkerStatusCode code) {
EXPECT_EQ(SERVICE_WORKER_OK, code); EXPECT_EQ(SERVICE_WORKER_OK, code);
...@@ -155,7 +162,10 @@ class BackgroundSyncManagerTest : public testing::Test { ...@@ -155,7 +162,10 @@ class BackgroundSyncManagerTest : public testing::Test {
// Wait for storage to finish initializing before registering service // Wait for storage to finish initializing before registering service
// workers. // workers.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
RegisterServiceWorkers();
}
void RegisterServiceWorkers() {
bool called_1 = false; bool called_1 = false;
bool called_2 = false; bool called_2 = false;
helper_->context()->RegisterServiceWorker( helper_->context()->RegisterServiceWorker(
...@@ -170,6 +180,19 @@ class BackgroundSyncManagerTest : public testing::Test { ...@@ -170,6 +180,19 @@ class BackgroundSyncManagerTest : public testing::Test {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called_1); EXPECT_TRUE(called_1);
EXPECT_TRUE(called_2); EXPECT_TRUE(called_2);
// Hang onto the registrations as they need to be "live" when
// calling BackgroundSyncMasnager::Register.
helper_->context_wrapper()->FindRegistrationForId(
sw_registration_id_1_, GURL(kPattern1).GetOrigin(),
base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_1_));
helper_->context_wrapper()->FindRegistrationForId(
sw_registration_id_2_, GURL(kPattern1).GetOrigin(),
base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_2_));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(sw_registration_1_);
EXPECT_TRUE(sw_registration_2_);
} }
void StatusAndRegistrationCallback( void StatusAndRegistrationCallback(
...@@ -208,7 +231,7 @@ class BackgroundSyncManagerTest : public testing::Test { ...@@ -208,7 +231,7 @@ class BackgroundSyncManagerTest : public testing::Test {
sync_registration) { sync_registration) {
bool was_called = false; bool was_called = false;
background_sync_manager_->Register( background_sync_manager_->Register(
GURL(kOrigin), sw_registration_id, sync_registration, sw_registration_id, sync_registration,
base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
base::Unretained(this), &was_called)); base::Unretained(this), &was_called));
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
...@@ -228,7 +251,7 @@ class BackgroundSyncManagerTest : public testing::Test { ...@@ -228,7 +251,7 @@ class BackgroundSyncManagerTest : public testing::Test {
sync_registration) { sync_registration) {
bool was_called = false; bool was_called = false;
background_sync_manager_->Unregister( background_sync_manager_->Unregister(
GURL(kOrigin), sw_registration_id, sync_registration.tag, sw_registration_id, sync_registration.tag,
sync_registration.periodicity, sync_registration.id, sync_registration.periodicity, sync_registration.id,
base::Bind(&BackgroundSyncManagerTest::StatusCallback, base::Bind(&BackgroundSyncManagerTest::StatusCallback,
base::Unretained(this), &was_called)); base::Unretained(this), &was_called));
...@@ -249,7 +272,7 @@ class BackgroundSyncManagerTest : public testing::Test { ...@@ -249,7 +272,7 @@ class BackgroundSyncManagerTest : public testing::Test {
sync_registration) { sync_registration) {
bool was_called = false; bool was_called = false;
background_sync_manager_->GetRegistration( background_sync_manager_->GetRegistration(
GURL(kOrigin), sw_registration_id, sync_registration.tag, sw_registration_id, sync_registration.tag,
sync_registration.periodicity, sync_registration.periodicity,
base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
base::Unretained(this), &was_called)); base::Unretained(this), &was_called));
...@@ -289,6 +312,8 @@ class BackgroundSyncManagerTest : public testing::Test { ...@@ -289,6 +312,8 @@ class BackgroundSyncManagerTest : public testing::Test {
int64 sw_registration_id_1_; int64 sw_registration_id_1_;
int64 sw_registration_id_2_; int64 sw_registration_id_2_;
scoped_refptr<ServiceWorkerRegistration> sw_registration_1_;
scoped_refptr<ServiceWorkerRegistration> sw_registration_2_;
BackgroundSyncManager::BackgroundSyncRegistration sync_reg_1_; BackgroundSyncManager::BackgroundSyncRegistration sync_reg_1_;
BackgroundSyncManager::BackgroundSyncRegistration sync_reg_2_; BackgroundSyncManager::BackgroundSyncRegistration sync_reg_2_;
...@@ -311,6 +336,20 @@ TEST_F(BackgroundSyncManagerTest, RegistractionIntact) { ...@@ -311,6 +336,20 @@ TEST_F(BackgroundSyncManagerTest, RegistractionIntact) {
callback_registration_.id); callback_registration_.id);
} }
TEST_F(BackgroundSyncManagerTest, RegisterWithoutLiveSWRegistration) {
sw_registration_1_ = nullptr;
EXPECT_FALSE(Register(sync_reg_1_));
EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_NO_SERVICE_WORKER,
callback_error_);
}
TEST_F(BackgroundSyncManagerTest, RegisterWithoutActiveSWRegistration) {
sw_registration_1_->UnsetVersion(sw_registration_1_->active_version());
EXPECT_FALSE(Register(sync_reg_1_));
EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_NO_SERVICE_WORKER,
callback_error_);
}
TEST_F(BackgroundSyncManagerTest, RegisterExistingKeepsId) { TEST_F(BackgroundSyncManagerTest, RegisterExistingKeepsId) {
EXPECT_TRUE(Register(sync_reg_1_)); EXPECT_TRUE(Register(sync_reg_1_));
BackgroundSyncManager::BackgroundSyncRegistration first_registration = BackgroundSyncManager::BackgroundSyncRegistration first_registration =
...@@ -507,16 +546,15 @@ TEST_F(BackgroundSyncManagerTest, SequentialOperations) { ...@@ -507,16 +546,15 @@ TEST_F(BackgroundSyncManagerTest, SequentialOperations) {
bool unregister_called = false; bool unregister_called = false;
bool get_registration_called = false; bool get_registration_called = false;
manager->Register( manager->Register(
GURL(kOrigin), sw_registration_id_1_, sync_reg_1_, sw_registration_id_1_, sync_reg_1_,
base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
base::Unretained(this), &register_called)); base::Unretained(this), &register_called));
manager->Unregister(GURL(kOrigin), sw_registration_id_1_, sync_reg_1_.tag, manager->Unregister(sw_registration_id_1_, sync_reg_1_.tag,
sync_reg_1_.periodicity, kExpectedInitialId, sync_reg_1_.periodicity, kExpectedInitialId,
base::Bind(&BackgroundSyncManagerTest::StatusCallback, base::Bind(&BackgroundSyncManagerTest::StatusCallback,
base::Unretained(this), &unregister_called)); base::Unretained(this), &unregister_called));
manager->GetRegistration( manager->GetRegistration(
GURL(kOrigin), sw_registration_id_1_, sync_reg_1_.tag, sw_registration_id_1_, sync_reg_1_.tag, sync_reg_1_.periodicity,
sync_reg_1_.periodicity,
base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
base::Unretained(this), &get_registration_called)); base::Unretained(this), &get_registration_called));
...@@ -569,7 +607,7 @@ TEST_F(BackgroundSyncManagerTest, ...@@ -569,7 +607,7 @@ TEST_F(BackgroundSyncManagerTest,
manager->set_delay_backend(true); manager->set_delay_backend(true);
bool callback_called = false; bool callback_called = false;
manager->Register( manager->Register(
GURL(kOrigin), sw_registration_id_1_, sync_reg_2_, sw_registration_id_1_, sync_reg_2_,
base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
base::Unretained(this), &callback_called)); base::Unretained(this), &callback_called));
...@@ -630,13 +668,7 @@ TEST_F(BackgroundSyncManagerTest, DisabledManagerWorksAfterDeleteAndStartOver) { ...@@ -630,13 +668,7 @@ TEST_F(BackgroundSyncManagerTest, DisabledManagerWorksAfterDeleteAndStartOver) {
helper_->context()->ScheduleDeleteAndStartOver(); helper_->context()->ScheduleDeleteAndStartOver();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
bool called = false; RegisterServiceWorkers();
helper_->context()->RegisterServiceWorker(
GURL(kPattern1), GURL(kScript1), NULL,
base::Bind(&RegisterServiceWorkerCallback, &called,
&sw_registration_id_1_));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
EXPECT_TRUE(Register(sync_reg_2_)); EXPECT_TRUE(Register(sync_reg_2_));
EXPECT_FALSE(GetRegistration(sync_reg_1_)); EXPECT_FALSE(GetRegistration(sync_reg_1_));
......
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