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

Side by Side Diff: chrome/browser/services/gcm/push_messaging_browsertest.cc

Issue 930083002: Unregister with push service and SW database when permission is lost. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 10 months 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 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 <map> 5 #include <map>
6 #include <string> 6 #include <string>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/infobars/infobar_service.h" 12 #include "chrome/browser/infobars/infobar_service.h"
13 #include "chrome/browser/notifications/notification_test_util.h" 13 #include "chrome/browser/notifications/notification_test_util.h"
14 #include "chrome/browser/notifications/platform_notification_service_impl.h" 14 #include "chrome/browser/notifications/platform_notification_service_impl.h"
15 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h" 16 #include "chrome/browser/services/gcm/fake_gcm_profile_service.h"
17 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" 17 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
18 #include "chrome/browser/services/gcm/push_messaging_application_id.h" 18 #include "chrome/browser/services/gcm/push_messaging_application_id.h"
19 #include "chrome/browser/services/gcm/push_messaging_constants.h" 19 #include "chrome/browser/services/gcm/push_messaging_constants.h"
20 #include "chrome/browser/ui/browser.h" 20 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h" 21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/test/base/in_process_browser_test.h" 22 #include "chrome/test/base/in_process_browser_test.h"
23 #include "chrome/test/base/ui_test_utils.h" 23 #include "chrome/test/base/ui_test_utils.h"
24 #include "components/content_settings/core/browser/host_content_settings_map.h" 24 #include "components/content_settings/core/browser/host_content_settings_map.h"
25 #include "components/content_settings/core/common/content_settings.h"
25 #include "components/content_settings/core/common/content_settings_types.h" 26 #include "components/content_settings/core/common/content_settings_types.h"
26 #include "components/gcm_driver/gcm_client.h" 27 #include "components/gcm_driver/gcm_client.h"
27 #include "components/infobars/core/confirm_infobar_delegate.h" 28 #include "components/infobars/core/confirm_infobar_delegate.h"
28 #include "components/infobars/core/infobar.h" 29 #include "components/infobars/core/infobar.h"
29 #include "components/infobars/core/infobar_manager.h" 30 #include "components/infobars/core/infobar_manager.h"
30 #include "content/public/browser/web_contents.h" 31 #include "content/public/browser/web_contents.h"
31 #include "content/public/common/content_switches.h" 32 #include "content/public/common/content_switches.h"
32 #include "content/public/test/browser_test_utils.h" 33 #include "content/public/test/browser_test_utils.h"
33 #include "ui/base/window_open_disposition.h" 34 #include "ui/base/window_open_disposition.h"
34 35
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 EXPECT_TRUE(RunScript("requestNotificationPermission()", &script_result)); 293 EXPECT_TRUE(RunScript("requestNotificationPermission()", &script_result));
293 EXPECT_EQ("permission status - granted", script_result); 294 EXPECT_EQ("permission status - granted", script_result);
294 295
295 EXPECT_TRUE(RunScript("registerPush()", &script_result)); 296 EXPECT_TRUE(RunScript("registerPush()", &script_result));
296 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - " 297 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - "
297 + expected_push_registration_id, script_result); 298 + expected_push_registration_id, script_result);
298 } 299 }
299 300
300 PushMessagingApplicationId PushMessagingBrowserTest::GetServiceWorkerAppId( 301 PushMessagingApplicationId PushMessagingBrowserTest::GetServiceWorkerAppId(
301 int64 service_worker_registration_id) { 302 int64 service_worker_registration_id) {
302 GURL origin = https_server()->GetURL("").GetOrigin(); 303 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
303 PushMessagingApplicationId application_id = PushMessagingApplicationId::Get( 304 PushMessagingApplicationId application_id = PushMessagingApplicationId::Get(
304 browser()->profile(), origin, service_worker_registration_id); 305 browser()->profile(), origin, service_worker_registration_id);
305 EXPECT_TRUE(application_id.IsValid()); 306 EXPECT_TRUE(application_id.IsValid());
306 return application_id; 307 return application_id;
307 } 308 }
308 309
309 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 310 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
310 RegisterSuccessNotificationsGranted) { 311 RegisterSuccessNotificationsGranted) {
311 if (!IsPushSupported()) 312 if (!IsPushSupported())
312 return; 313 return;
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after
793 // is not available on that version of Android. 794 // is not available on that version of Android.
794 if (IsPushSupported()) 795 if (IsPushSupported())
795 return; 796 return;
796 797
797 std::string script_result; 798 std::string script_result;
798 ASSERT_TRUE(RunScript("window.PushManager", &script_result)); 799 ASSERT_TRUE(RunScript("window.PushManager", &script_result));
799 EXPECT_EQ("undefined", script_result); 800 EXPECT_EQ("undefined", script_result);
800 } 801 }
801 #endif 802 #endif
802 803
804 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
805 GlobalResetPushPermissionUnregisters) {
806 std::string script_result;
807
808 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
809
810 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
811 EXPECT_EQ("true - registered", script_result);
812
813 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
814 EXPECT_EQ("permission status - granted", script_result);
815
816 browser()->profile()->GetHostContentSettingsMap()->
817 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING);
818
819 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
820 EXPECT_EQ("permission status - default", script_result);
821
822 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
823 EXPECT_EQ("false - not registered", script_result);
824 }
825
826 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
827 LocalResetPushPermissionUnregisters) {
828 std::string script_result;
829
830 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
831
832 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
833 EXPECT_EQ("true - registered", script_result);
834
835 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
836 EXPECT_EQ("permission status - granted", script_result);
837
838 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
839 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
840 ContentSettingsPattern::FromURLNoWildcard(origin),
841 ContentSettingsPattern::FromURLNoWildcard(origin),
842 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
843 std::string(),
844 CONTENT_SETTING_DEFAULT);
845
846 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
847 EXPECT_EQ("permission status - default", script_result);
848
849 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
850 EXPECT_EQ("false - not registered", script_result);
851 }
852
853 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
854 DenyPushPermissionUnregisters) {
855 std::string script_result;
856
857 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
858
859 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
860 EXPECT_EQ("true - registered", script_result);
861
862 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
863 EXPECT_EQ("permission status - granted", script_result);
864
865 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
866 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
867 ContentSettingsPattern::FromURLNoWildcard(origin),
868 ContentSettingsPattern::FromURLNoWildcard(origin),
869 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
870 std::string(),
871 CONTENT_SETTING_BLOCK);
872
873 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
874 EXPECT_EQ("permission status - denied", script_result);
875
876 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
877 EXPECT_EQ("false - not registered", script_result);
878 }
879
880 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
881 GlobalResetNotificationsPermissionUnregisters) {
882 std::string script_result;
883
884 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
885
886 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
887 EXPECT_EQ("true - registered", script_result);
888
889 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
890 EXPECT_EQ("permission status - granted", script_result);
891
892 browser()->profile()->GetHostContentSettingsMap()->
893 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
894
895 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
896 EXPECT_EQ("permission status - default", script_result);
897
898 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
899 EXPECT_EQ("false - not registered", script_result);
900 }
901
902 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
903 LocalResetNotificationsPermissionUnregisters) {
904 std::string script_result;
905
906 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
907
908 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
909 EXPECT_EQ("true - registered", script_result);
910
911 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
912 EXPECT_EQ("permission status - granted", script_result);
913
914 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
915 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
916 ContentSettingsPattern::FromURLNoWildcard(origin),
917 ContentSettingsPattern::Wildcard(),
918 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
919 std::string(),
920 CONTENT_SETTING_DEFAULT);
921
922 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
923 EXPECT_EQ("permission status - default", script_result);
924
925 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
926 EXPECT_EQ("false - not registered", script_result);
927 }
928
929 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
930 DenyNotificationsPermissionUnregisters) {
931 std::string script_result;
932
933 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
934
935 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
936 EXPECT_EQ("true - registered", script_result);
937
938 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
939 EXPECT_EQ("permission status - granted", script_result);
940
941 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
942 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
943 ContentSettingsPattern::FromURLNoWildcard(origin),
944 ContentSettingsPattern::Wildcard(),
945 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
946 std::string(),
947 CONTENT_SETTING_BLOCK);
948
949 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
950 EXPECT_EQ("permission status - denied", script_result);
951
952 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
953 EXPECT_EQ("false - not registered", script_result);
954 }
955
956 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
957 GrantAlreadyGrantedPermissionDoesNotUnregister) {
958 std::string script_result;
959
960 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
961
962 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
963 EXPECT_EQ("true - registered", script_result);
964
965 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
966 EXPECT_EQ("permission status - granted", script_result);
967
968 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
969 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
970 ContentSettingsPattern::FromURLNoWildcard(origin),
971 ContentSettingsPattern::Wildcard(),
972 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
973 std::string(),
974 CONTENT_SETTING_ALLOW);
975 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
976 ContentSettingsPattern::FromURLNoWildcard(origin),
977 ContentSettingsPattern::FromURLNoWildcard(origin),
978 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
979 std::string(),
980 CONTENT_SETTING_ALLOW);
981
982 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
983 EXPECT_EQ("permission status - granted", script_result);
984
985 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
986 EXPECT_EQ("true - registered", script_result);
987 }
988
989 // This test is testing some non-trivial content settings rules and make sure
990 // that they are respected with regards to automatic unregistration. In other
991 // words, it checks that the push service does not end up unregistering origins
992 // that have push permission with some non-common rules.
993 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
994 AutomaticUnregistrationFollowsContentSettingRules) {
995 std::string script_result;
996
997 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
998
999 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
1000 EXPECT_EQ("true - registered", script_result);
1001
1002 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
1003 EXPECT_EQ("permission status - granted", script_result);
1004
1005 GURL origin = https_server()->GetURL(std::string()).GetOrigin();
1006 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
1007 ContentSettingsPattern::Wildcard(),
1008 ContentSettingsPattern::Wildcard(),
1009 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
1010 std::string(),
1011 CONTENT_SETTING_ALLOW);
1012 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
1013 ContentSettingsPattern::FromString("https://*"),
1014 ContentSettingsPattern::FromString("https://*"),
1015 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
1016 std::string(),
1017 CONTENT_SETTING_ALLOW);
1018 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
1019 ContentSettingsPattern::FromURLNoWildcard(origin),
1020 ContentSettingsPattern::Wildcard(),
1021 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
1022 std::string(),
1023 CONTENT_SETTING_DEFAULT);
1024 browser()->profile()->GetHostContentSettingsMap()->SetContentSetting(
1025 ContentSettingsPattern::FromURLNoWildcard(origin),
1026 ContentSettingsPattern::FromURLNoWildcard(origin),
1027 CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
1028 std::string(),
1029 CONTENT_SETTING_DEFAULT);
1030
1031 // The two first rules should give |origin| the permission to use Push even
1032 // if the rules it used to have have been reset.
1033 // The Push service should not unsubcribe |origin| because at no point it was
1034 // left without permission to use Push.
1035
1036 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
1037 EXPECT_EQ("permission status - granted", script_result);
1038
1039 ASSERT_TRUE(RunScript("hasRegistration()", &script_result));
1040 EXPECT_EQ("true - registered", script_result);
1041 }
1042
803 } // namespace gcm 1043 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698