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