Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: content/browser/background_sync/background_sync_manager_unittest.cc

Issue 1471763003: [BackgroundSync] Only allow SyncManager.register to occur from main frame (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@split
Patch Set: Address comments from PS5 Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/background_sync/background_sync_manager.h" 5 #include "content/browser/background_sync/background_sync_manager.h"
6 6
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/metrics/field_trial.h" 10 #include "base/metrics/field_trial.h"
(...skipping 21 matching lines...) Expand all
32 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
33 33
34 namespace content { 34 namespace content {
35 35
36 namespace { 36 namespace {
37 37
38 const char kPattern1[] = "https://example.com/a"; 38 const char kPattern1[] = "https://example.com/a";
39 const char kPattern2[] = "https://example.com/b"; 39 const char kPattern2[] = "https://example.com/b";
40 const char kScript1[] = "https://example.com/a/script.js"; 40 const char kScript1[] = "https://example.com/a/script.js";
41 const char kScript2[] = "https://example.com/b/script.js"; 41 const char kScript2[] = "https://example.com/b/script.js";
42 const int kProviderId1 = 1;
43 const int kProviderId2 = 2;
44 42
45 void RegisterServiceWorkerCallback(bool* called, 43 void RegisterServiceWorkerCallback(bool* called,
46 int64* store_registration_id, 44 int64* store_registration_id,
47 ServiceWorkerStatusCode status, 45 ServiceWorkerStatusCode status,
48 const std::string& status_message, 46 const std::string& status_message,
49 int64 registration_id) { 47 int64 registration_id) {
50 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); 48 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status);
51 *called = true; 49 *called = true;
52 *store_registration_id = registration_id; 50 *store_registration_id = registration_id;
53 } 51 }
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 void set_delay_backend(bool delay_backend) { delay_backend_ = delay_backend; } 192 void set_delay_backend(bool delay_backend) { delay_backend_ = delay_backend; }
195 void set_one_shot_callback(const OneShotCallback& callback) { 193 void set_one_shot_callback(const OneShotCallback& callback) {
196 one_shot_callback_ = callback; 194 one_shot_callback_ = callback;
197 } 195 }
198 196
199 base::Closure delayed_task() const { return delayed_task_; } 197 base::Closure delayed_task() const { return delayed_task_; }
200 base::TimeDelta delayed_task_delta() const { return delayed_task_delta_; } 198 base::TimeDelta delayed_task_delta() const { return delayed_task_delta_; }
201 199
202 BackgroundSyncEventLastChance last_chance() const { return last_chance_; } 200 BackgroundSyncEventLastChance last_chance() const { return last_chance_; }
203 201
202 void set_has_main_frame_provider_host(bool value) {
203 has_main_frame_provider_host_ = value;
204 }
205
204 protected: 206 protected:
205 void StoreDataInBackend( 207 void StoreDataInBackend(
206 int64 sw_registration_id, 208 int64 sw_registration_id,
207 const GURL& origin, 209 const GURL& origin,
208 const std::string& key, 210 const std::string& key,
209 const std::string& data, 211 const std::string& data,
210 const ServiceWorkerStorage::StatusCallback& callback) override { 212 const ServiceWorkerStorage::StatusCallback& callback) override {
211 EXPECT_TRUE(continuation_.is_null()); 213 EXPECT_TRUE(continuation_.is_null());
212 if (corrupt_backend_) { 214 if (corrupt_backend_) {
213 base::ThreadTaskRunnerHandle::Get()->PostTask( 215 base::ThreadTaskRunnerHandle::Get()->PostTask(
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 last_chance_ = last_chance; 256 last_chance_ = last_chance;
255 one_shot_callback_.Run(active_version, callback); 257 one_shot_callback_.Run(active_version, callback);
256 } 258 }
257 259
258 void ScheduleDelayedTask(const base::Closure& callback, 260 void ScheduleDelayedTask(const base::Closure& callback,
259 base::TimeDelta delay) override { 261 base::TimeDelta delay) override {
260 delayed_task_ = callback; 262 delayed_task_ = callback;
261 delayed_task_delta_ = delay; 263 delayed_task_delta_ = delay;
262 } 264 }
263 265
266 void HasMainFrameProviderHost(const GURL& origin,
267 const BoolCallback& callback) override {
268 callback.Run(has_main_frame_provider_host_);
269 }
270
264 private: 271 private:
265 bool corrupt_backend_ = false; 272 bool corrupt_backend_ = false;
266 bool delay_backend_ = false; 273 bool delay_backend_ = false;
274 bool has_main_frame_provider_host_ = true;
267 BackgroundSyncEventLastChance last_chance_ = 275 BackgroundSyncEventLastChance last_chance_ =
268 BACKGROUND_SYNC_EVENT_LAST_CHANCE_IS_NOT_LAST_CHANCE; 276 BACKGROUND_SYNC_EVENT_LAST_CHANCE_IS_NOT_LAST_CHANCE;
269 base::Closure continuation_; 277 base::Closure continuation_;
270 OneShotCallback one_shot_callback_; 278 OneShotCallback one_shot_callback_;
271 base::Closure delayed_task_; 279 base::Closure delayed_task_;
272 base::TimeDelta delayed_task_delta_; 280 base::TimeDelta delayed_task_delta_;
273 }; 281 };
274 282
275 class BackgroundSyncManagerTest : public testing::Test { 283 class BackgroundSyncManagerTest : public testing::Test {
276 public: 284 public:
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 &sw_registration_id_1_)); 349 &sw_registration_id_1_));
342 350
343 helper_->context()->RegisterServiceWorker( 351 helper_->context()->RegisterServiceWorker(
344 GURL(kPattern2), GURL(kScript2), NULL, 352 GURL(kPattern2), GURL(kScript2), NULL,
345 base::Bind(&RegisterServiceWorkerCallback, &called_2, 353 base::Bind(&RegisterServiceWorkerCallback, &called_2,
346 &sw_registration_id_2_)); 354 &sw_registration_id_2_));
347 base::RunLoop().RunUntilIdle(); 355 base::RunLoop().RunUntilIdle();
348 EXPECT_TRUE(called_1); 356 EXPECT_TRUE(called_1);
349 EXPECT_TRUE(called_2); 357 EXPECT_TRUE(called_2);
350 358
351 // Register window clients for the service workers
352 ServiceWorkerProviderHost* host_1 = new ServiceWorkerProviderHost(
353 helper_->mock_render_process_id(),
354 MSG_ROUTING_NONE /* render_frame_id */, kProviderId1,
355 SERVICE_WORKER_PROVIDER_FOR_WINDOW, helper_->context()->AsWeakPtr(),
356 nullptr);
357 host_1->SetDocumentUrl(GURL(kPattern1));
358
359 ServiceWorkerProviderHost* host_2 = new ServiceWorkerProviderHost(
360 helper_->mock_render_process_id(),
361 MSG_ROUTING_NONE /* render_frame_id */, kProviderId2,
362 SERVICE_WORKER_PROVIDER_FOR_WINDOW, helper_->context()->AsWeakPtr(),
363 nullptr);
364 host_2->SetDocumentUrl(GURL(kPattern2));
365
366 helper_->context()->AddProviderHost(make_scoped_ptr(host_1));
367 helper_->context()->AddProviderHost(make_scoped_ptr(host_2));
368
369 // Hang onto the registrations as they need to be "live" when 359 // Hang onto the registrations as they need to be "live" when
370 // calling BackgroundSyncManager::Register. 360 // calling BackgroundSyncManager::Register.
371 helper_->context_wrapper()->FindReadyRegistrationForId( 361 helper_->context_wrapper()->FindReadyRegistrationForId(
372 sw_registration_id_1_, GURL(kPattern1).GetOrigin(), 362 sw_registration_id_1_, GURL(kPattern1).GetOrigin(),
373 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_1_)); 363 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_1_));
374 364
375 helper_->context_wrapper()->FindReadyRegistrationForId( 365 helper_->context_wrapper()->FindReadyRegistrationForId(
376 sw_registration_id_2_, GURL(kPattern1).GetOrigin(), 366 sw_registration_id_2_, GURL(kPattern1).GetOrigin(),
377 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_2_)); 367 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_2_));
378 base::RunLoop().RunUntilIdle(); 368 base::RunLoop().RunUntilIdle();
379 EXPECT_TRUE(sw_registration_1_); 369 EXPECT_TRUE(sw_registration_1_);
380 EXPECT_TRUE(sw_registration_2_); 370 EXPECT_TRUE(sw_registration_2_);
381 } 371 }
382 372
383 void RemoveWindowClients() {
384 helper_->context()->RemoveAllProviderHostsForProcess(
385 helper_->mock_render_process_id());
386 }
387
388 void SetNetwork(net::NetworkChangeNotifier::ConnectionType connection_type) { 373 void SetNetwork(net::NetworkChangeNotifier::ConnectionType connection_type) {
389 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( 374 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
390 connection_type); 375 connection_type);
391 if (test_background_sync_manager_) { 376 if (test_background_sync_manager_) {
392 BackgroundSyncNetworkObserver* network_observer = 377 BackgroundSyncNetworkObserver* network_observer =
393 test_background_sync_manager_->GetNetworkObserverForTesting(); 378 test_background_sync_manager_->GetNetworkObserverForTesting();
394 network_observer->NotifyManagerIfNetworkChangedForTesting( 379 network_observer->NotifyManagerIfNetworkChangedForTesting(
395 connection_type); 380 connection_type);
396 base::RunLoop().RunUntilIdle(); 381 base::RunLoop().RunUntilIdle();
397 } 382 }
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 } 450 }
466 451
467 void SetupDelayedBackgroundSyncManager() { 452 void SetupDelayedBackgroundSyncManager() {
468 CreateBackgroundSyncManager(); 453 CreateBackgroundSyncManager();
469 test_background_sync_manager_->set_delay_backend(true); 454 test_background_sync_manager_->set_delay_backend(true);
470 InitBackgroundSyncManager(); 455 InitBackgroundSyncManager();
471 } 456 }
472 457
473 void DeleteBackgroundSyncManager() { 458 void DeleteBackgroundSyncManager() {
474 ClearRegistrationHandles(); 459 ClearRegistrationHandles();
475 RemoveWindowClients();
476 background_sync_manager_.reset(); 460 background_sync_manager_.reset();
477 test_background_sync_manager_ = nullptr; 461 test_background_sync_manager_ = nullptr;
478 test_clock_ = nullptr; 462 test_clock_ = nullptr;
479 } 463 }
480 464
481 bool Register(const BackgroundSyncRegistrationOptions& sync_options) { 465 bool Register(const BackgroundSyncRegistrationOptions& sync_options) {
482 return RegisterWithServiceWorkerId(sw_registration_id_1_, sync_options); 466 return RegisterWithServiceWorkerId(sw_registration_id_1_, sync_options);
483 } 467 }
484 468
485 bool RegisterWithServiceWorkerId( 469 bool RegisterWithServiceWorkerId(
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
639 623
640 EXPECT_TRUE(Register(sync_options)); 624 EXPECT_TRUE(Register(sync_options));
641 625
642 EXPECT_EQ(sync_events_called + 1, sync_events_called_); 626 EXPECT_EQ(sync_events_called + 1, sync_events_called_);
643 EXPECT_TRUE(GetRegistration(sync_options_1_)); 627 EXPECT_TRUE(GetRegistration(sync_options_1_));
644 EXPECT_FALSE(sync_fired_callback_.is_null()); 628 EXPECT_FALSE(sync_fired_callback_.is_null());
645 } 629 }
646 630
647 void DeleteServiceWorkerAndStartOver() { 631 void DeleteServiceWorkerAndStartOver() {
648 helper_->context()->ScheduleDeleteAndStartOver(); 632 helper_->context()->ScheduleDeleteAndStartOver();
649 RemoveWindowClients();
650 base::RunLoop().RunUntilIdle(); 633 base::RunLoop().RunUntilIdle();
651 } 634 }
652 635
653 int MaxTagLength() const { return BackgroundSyncManager::kMaxTagLength; } 636 int MaxTagLength() const { return BackgroundSyncManager::kMaxTagLength; }
654 637
655 TestBrowserThreadBundle browser_thread_bundle_; 638 TestBrowserThreadBundle browser_thread_bundle_;
656 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; 639 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
657 TestPowerSource* power_monitor_source_ = nullptr; // owned by power_monitor_ 640 TestPowerSource* power_monitor_source_ = nullptr; // owned by power_monitor_
658 scoped_ptr<base::PowerMonitor> power_monitor_; 641 scoped_ptr<base::PowerMonitor> power_monitor_;
659 scoped_ptr<EmbeddedWorkerTestHelper> helper_; 642 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
(...skipping 1036 matching lines...) Expand 10 before | Expand all | Expand 10 after
1696 1679
1697 RegisterAndVerifySyncEventDelayed(sync_options_1_); 1680 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1698 1681
1699 // Create a new manager which should fire the sync again on init. 1682 // Create a new manager which should fire the sync again on init.
1700 SetupBackgroundSyncManager(); 1683 SetupBackgroundSyncManager();
1701 InitSyncEventTest(); 1684 InitSyncEventTest();
1702 EXPECT_FALSE(GetRegistration(sync_options_1_)); 1685 EXPECT_FALSE(GetRegistration(sync_options_1_));
1703 EXPECT_EQ(2, sync_events_called_); 1686 EXPECT_EQ(2, sync_events_called_);
1704 } 1687 }
1705 1688
1706 TEST_F(BackgroundSyncManagerTest, RegisterWithClientWindowForWrongOrigin) { 1689 TEST_F(BackgroundSyncManagerTest, RegisterFromServiceWorkerWithoutMainFrame) {
1707 RemoveWindowClients(); 1690 test_background_sync_manager_->set_has_main_frame_provider_host(false);
1708 ServiceWorkerProviderHost* host = new ServiceWorkerProviderHost(
1709 helper_->mock_render_process_id(), MSG_ROUTING_NONE /* render_frame_id */,
1710 kProviderId1, SERVICE_WORKER_PROVIDER_FOR_WINDOW,
1711 helper_->context()->AsWeakPtr(), nullptr);
1712 host->SetDocumentUrl(GURL("http://example.com:9999"));
1713 helper_->context()->AddProviderHost(make_scoped_ptr(host));
1714 EXPECT_FALSE(Register(sync_options_1_)); 1691 EXPECT_FALSE(Register(sync_options_1_));
1692 }
1693
1694 TEST_F(BackgroundSyncManagerTest,
1695 RegisterFromDocumentWithoutMainFrameProviderHost) {
1696 test_background_sync_manager_->set_has_main_frame_provider_host(false);
1715 EXPECT_TRUE(RegisterFromDocumentWithServiceWorkerId(sw_registration_id_1_, 1697 EXPECT_TRUE(RegisterFromDocumentWithServiceWorkerId(sw_registration_id_1_,
1716 sync_options_1_)); 1698 sync_options_1_));
1717 } 1699 }
1718 1700
1719 TEST_F(BackgroundSyncManagerTest, RegisterWithNoClientWindows) { 1701 TEST_F(BackgroundSyncManagerTest,
1720 RemoveWindowClients(); 1702 RegisterExistingFromServiceWorkerWithoutMainFrame) {
1703 EXPECT_TRUE(Register(sync_options_1_));
1704 test_background_sync_manager_->set_has_main_frame_provider_host(false);
1721 EXPECT_FALSE(Register(sync_options_1_)); 1705 EXPECT_FALSE(Register(sync_options_1_));
1722 EXPECT_TRUE(RegisterFromDocumentWithServiceWorkerId(sw_registration_id_1_,
1723 sync_options_1_));
1724 } 1706 }
1725 1707
1726 TEST_F(BackgroundSyncManagerTest, RegisterExistingWithNoClientWindows) { 1708 TEST_F(BackgroundSyncManagerTest, UnregisterSucceedsWithoutMainFrame) {
1727 EXPECT_TRUE(Register(sync_options_1_)); 1709 EXPECT_TRUE(Register(sync_options_1_));
1728 RemoveWindowClients(); 1710 test_background_sync_manager_->set_has_main_frame_provider_host(false);
1729 EXPECT_FALSE(Register(sync_options_1_));
1730 EXPECT_TRUE(RegisterFromDocumentWithServiceWorkerId(sw_registration_id_1_,
1731 sync_options_1_));
1732 }
1733
1734 TEST_F(BackgroundSyncManagerTest, UnregisterSucceedsWithoutWindow) {
1735 EXPECT_TRUE(Register(sync_options_1_));
1736 RemoveWindowClients();
1737 EXPECT_TRUE(Unregister(callback_registration_handle_.get())); 1711 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1738 EXPECT_FALSE(GetRegistration(sync_options_1_)); 1712 EXPECT_FALSE(GetRegistration(sync_options_1_));
1739 } 1713 }
1740 1714
1741 TEST_F(BackgroundSyncManagerTest, FieldTrialDisablesManager) { 1715 TEST_F(BackgroundSyncManagerTest, FieldTrialDisablesManager) {
1742 EXPECT_TRUE(Register(sync_options_1_)); 1716 EXPECT_TRUE(Register(sync_options_1_));
1743 1717
1744 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); 1718 base::FieldTrialList field_trial_list(new base::MockEntropyProvider());
1745 base::FieldTrialList::CreateFieldTrial("BackgroundSync", 1719 base::FieldTrialList::CreateFieldTrial("BackgroundSync",
1746 "ExperimentDisabled"); 1720 "ExperimentDisabled");
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
1979 // Run it again. 1953 // Run it again.
1980 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta()); 1954 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta());
1981 test_background_sync_manager_->delayed_task().Run(); 1955 test_background_sync_manager_->delayed_task().Run();
1982 base::RunLoop().RunUntilIdle(); 1956 base::RunLoop().RunUntilIdle();
1983 EXPECT_FALSE(GetRegistration(sync_options_1_)); 1957 EXPECT_FALSE(GetRegistration(sync_options_1_));
1984 EXPECT_EQ(BACKGROUND_SYNC_EVENT_LAST_CHANCE_IS_LAST_CHANCE, 1958 EXPECT_EQ(BACKGROUND_SYNC_EVENT_LAST_CHANCE_IS_LAST_CHANCE,
1985 test_background_sync_manager_->last_chance()); 1959 test_background_sync_manager_->last_chance());
1986 } 1960 }
1987 1961
1988 } // namespace content 1962 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698