| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/barrier_closure.h" | 11 #include "base/barrier_closure.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/browsing_data/browsing_data_helper.h" | 20 #include "chrome/browser/browsing_data/browsing_data_helper.h" |
| 21 #include "chrome/browser/browsing_data/browsing_data_remover.h" | 21 #include "chrome/browser/browsing_data/browsing_data_remover.h" |
| 22 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" | 22 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" |
| 23 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h" | 23 #include "chrome/browser/browsing_data/browsing_data_remover_test_util.h" |
| 24 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 24 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 25 #include "chrome/browser/engagement/site_engagement_service.h" |
| 25 #include "chrome/browser/notifications/message_center_display_service.h" | 26 #include "chrome/browser/notifications/message_center_display_service.h" |
| 26 #include "chrome/browser/notifications/notification_test_util.h" | 27 #include "chrome/browser/notifications/notification_test_util.h" |
| 27 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 28 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 28 #include "chrome/browser/profiles/profile.h" | 29 #include "chrome/browser/profiles/profile.h" |
| 29 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" | 30 #include "chrome/browser/push_messaging/push_messaging_app_identifier.h" |
| 30 #include "chrome/browser/push_messaging/push_messaging_constants.h" | 31 #include "chrome/browser/push_messaging/push_messaging_constants.h" |
| 31 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" | 32 #include "chrome/browser/push_messaging/push_messaging_service_factory.h" |
| 32 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" | 33 #include "chrome/browser/push_messaging/push_messaging_service_impl.h" |
| 33 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" | 34 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" |
| 34 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 35 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 return notification_manager_.get(); | 233 return notification_manager_.get(); |
| 233 } | 234 } |
| 234 | 235 |
| 235 PlatformNotificationServiceImpl* notification_service() const { | 236 PlatformNotificationServiceImpl* notification_service() const { |
| 236 return PlatformNotificationServiceImpl::GetInstance(); | 237 return PlatformNotificationServiceImpl::GetInstance(); |
| 237 } | 238 } |
| 238 #endif | 239 #endif |
| 239 | 240 |
| 240 PushMessagingServiceImpl* push_service() const { return push_service_; } | 241 PushMessagingServiceImpl* push_service() const { return push_service_; } |
| 241 | 242 |
| 243 void SetSiteEngagementScore(const GURL& url, double score) { |
| 244 SiteEngagementService* service = |
| 245 SiteEngagementService::Get(GetBrowser()->profile()); |
| 246 service->ResetScoreForURL(url, score); |
| 247 } |
| 248 |
| 242 protected: | 249 protected: |
| 243 virtual std::string GetTestURL() { return "/push_messaging/test.html"; } | 250 virtual std::string GetTestURL() { return "/push_messaging/test.html"; } |
| 244 | 251 |
| 245 virtual Browser* GetBrowser() const { return browser(); } | 252 virtual Browser* GetBrowser() const { return browser(); } |
| 246 | 253 |
| 247 private: | 254 private: |
| 248 std::unique_ptr<net::EmbeddedTestServer> https_server_; | 255 std::unique_ptr<net::EmbeddedTestServer> https_server_; |
| 249 gcm::FakeGCMProfileService* gcm_service_; | 256 gcm::FakeGCMProfileService* gcm_service_; |
| 250 PushMessagingServiceImpl* push_service_; | 257 PushMessagingServiceImpl* push_service_; |
| 251 | 258 |
| (...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 743 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); | 750 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result)); |
| 744 EXPECT_EQ("testdata", script_result); | 751 EXPECT_EQ("testdata", script_result); |
| 745 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); | 752 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 746 } | 753 } |
| 747 | 754 |
| 748 // Open a blank foreground tab so site is no longer visible. | 755 // Open a blank foreground tab so site is no longer visible. |
| 749 ui_test_utils::NavigateToURLWithDisposition( | 756 ui_test_utils::NavigateToURLWithDisposition( |
| 750 GetBrowser(), GURL("about:blank"), NEW_FOREGROUND_TAB, | 757 GetBrowser(), GURL("about:blank"), NEW_FOREGROUND_TAB, |
| 751 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | 758 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); |
| 752 | 759 |
| 760 // Set the site engagement score for the site. Setting it to 2 means we should |
| 761 // have enough budget for two non-shown notification. |
| 762 SetSiteEngagementScore(web_contents->GetURL(), 2.0); |
| 763 |
| 764 // If the Service Worker push event handler shows a notification, we |
| 765 // should not show a forced one. |
| 766 message.raw_data = "shownotification"; |
| 767 SendMessageAndWaitUntilHandled(app_identifier, message); |
| 768 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 769 EXPECT_EQ("shownotification", script_result); |
| 770 EXPECT_EQ(1u, notification_manager()->GetNotificationCount()); |
| 771 EXPECT_EQ("push_test_tag", |
| 772 notification_manager()->GetNotificationAt(0).tag()); |
| 773 notification_manager()->CancelAll(); |
| 774 |
| 753 // If the Service Worker push event handler does not show a notification, we | 775 // If the Service Worker push event handler does not show a notification, we |
| 754 // should show a forced one, but only on the 2nd occurrence since we allow one | 776 // should show a forced one, but only once the origin is out of budget. |
| 755 // mistake per 10 push events. | 777 message.raw_data = "testdata"; |
| 778 for (int n = 0; n < 2; n++) { |
| 779 // First two missed notifications shouldn't force a default one. |
| 780 SendMessageAndWaitUntilHandled(app_identifier, message); |
| 781 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 782 EXPECT_EQ("testdata", script_result); |
| 783 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); |
| 784 } |
| 785 |
| 786 // Third missed notification should trigger a default notification, since the |
| 787 // origin will be out of budget. |
| 756 message.raw_data = "testdata"; | 788 message.raw_data = "testdata"; |
| 757 SendMessageAndWaitUntilHandled(app_identifier, message); | 789 SendMessageAndWaitUntilHandled(app_identifier, message); |
| 758 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | 790 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 759 EXPECT_EQ("testdata", script_result); | |
| 760 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); | |
| 761 message.raw_data = "testdata"; | |
| 762 SendMessageAndWaitUntilHandled(app_identifier, message); | |
| 763 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | |
| 764 EXPECT_EQ("testdata", script_result); | 791 EXPECT_EQ("testdata", script_result); |
| 765 | 792 |
| 766 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); | 793 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); |
| 767 { | 794 { |
| 768 const Notification& forced_notification = | 795 const Notification& forced_notification = |
| 769 notification_manager()->GetNotificationAt(0); | 796 notification_manager()->GetNotificationAt(0); |
| 770 | 797 |
| 771 EXPECT_EQ(kPushMessagingForcedNotificationTag, forced_notification.tag()); | 798 EXPECT_EQ(kPushMessagingForcedNotificationTag, forced_notification.tag()); |
| 772 EXPECT_TRUE(forced_notification.silent()); | 799 EXPECT_TRUE(forced_notification.silent()); |
| 773 } | 800 } |
| 774 | 801 |
| 775 // The notification will be automatically dismissed when the developer shows | 802 // The notification will be automatically dismissed when the developer shows |
| 776 // a new notification themselves at a later point in time. | 803 // a new notification themselves at a later point in time. |
| 777 message.raw_data = "shownotification"; | 804 message.raw_data = "shownotification"; |
| 778 SendMessageAndWaitUntilHandled(app_identifier, message); | 805 SendMessageAndWaitUntilHandled(app_identifier, message); |
| 779 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | 806 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); |
| 780 EXPECT_EQ("shownotification", script_result); | 807 EXPECT_EQ("shownotification", script_result); |
| 781 | 808 |
| 782 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); | 809 ASSERT_EQ(1u, notification_manager()->GetNotificationCount()); |
| 783 { | 810 { |
| 784 const Notification& first_notification = | 811 const Notification& first_notification = |
| 785 notification_manager()->GetNotificationAt(0); | 812 notification_manager()->GetNotificationAt(0); |
| 786 | 813 |
| 787 EXPECT_NE(kPushMessagingForcedNotificationTag, first_notification.tag()); | 814 EXPECT_NE(kPushMessagingForcedNotificationTag, first_notification.tag()); |
| 788 } | 815 } |
| 789 | |
| 790 notification_manager()->CancelAll(); | |
| 791 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); | |
| 792 | |
| 793 // However if the Service Worker push event handler shows a notification, we | |
| 794 // should not show a forced one. | |
| 795 message.raw_data = "shownotification"; | |
| 796 for (int n = 0; n < 9; n++) { | |
| 797 SendMessageAndWaitUntilHandled(app_identifier, message); | |
| 798 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | |
| 799 EXPECT_EQ("shownotification", script_result); | |
| 800 EXPECT_EQ(1u, notification_manager()->GetNotificationCount()); | |
| 801 EXPECT_EQ("push_test_tag", | |
| 802 notification_manager()->GetNotificationAt(0).tag()); | |
| 803 notification_manager()->CancelAll(); | |
| 804 } | |
| 805 | |
| 806 // Now that 10 push messages in a row have shown notifications, we should | |
| 807 // allow the next one to mistakenly not show a notification. | |
| 808 message.raw_data = "testdata"; | |
| 809 SendMessageAndWaitUntilHandled(app_identifier, message); | |
| 810 ASSERT_TRUE(RunScript("resultQueue.pop()", &script_result, web_contents)); | |
| 811 EXPECT_EQ("testdata", script_result); | |
| 812 EXPECT_EQ(0u, notification_manager()->GetNotificationCount()); | |
| 813 } | 816 } |
| 814 | 817 |
| 815 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, | 818 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, |
| 816 PushEventEnforcesUserVisibleNotificationAfterQueue) { | 819 PushEventEnforcesUserVisibleNotificationAfterQueue) { |
| 817 std::string script_result; | 820 std::string script_result; |
| 818 | 821 |
| 819 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); | 822 TryToSubscribeSuccessfully("1-0" /* expected_push_subscription_id */); |
| 820 | 823 |
| 821 PushMessagingAppIdentifier app_identifier = | 824 PushMessagingAppIdentifier app_identifier = |
| 822 GetAppIdentifierForServiceWorkerRegistration(0LL); | 825 GetAppIdentifierForServiceWorkerRegistration(0LL); |
| (...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1455 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); | 1458 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); |
| 1456 | 1459 |
| 1457 // After dropping the last subscription background mode is still inactive. | 1460 // After dropping the last subscription background mode is still inactive. |
| 1458 std::string script_result; | 1461 std::string script_result; |
| 1459 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS); | 1462 gcm_service()->AddExpectedUnregisterResponse(gcm::GCMClient::SUCCESS); |
| 1460 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); | 1463 ASSERT_TRUE(RunScript("unsubscribePush()", &script_result)); |
| 1461 EXPECT_EQ("unsubscribe result: true", script_result); | 1464 EXPECT_EQ("unsubscribe result: true", script_result); |
| 1462 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); | 1465 ASSERT_FALSE(background_mode_manager->IsBackgroundModeActive()); |
| 1463 } | 1466 } |
| 1464 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) | 1467 #endif // BUILDFLAG(ENABLE_BACKGROUND) && !defined(OS_CHROMEOS) |
| OLD | NEW |