OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/geolocation/geolocation_permission_context.h" | 5 #include "chrome/browser/geolocation/geolocation_permission_context.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
16 #include "base/test/simple_test_clock.h" | |
17 #include "base/time/clock.h" | |
16 #include "chrome/browser/chrome_notification_types.h" | 18 #include "chrome/browser/chrome_notification_types.h" |
17 #include "chrome/browser/content_settings/host_content_settings_map.h" | 19 #include "chrome/browser/content_settings/host_content_settings_map.h" |
18 #include "chrome/browser/content_settings/permission_request_id.h" | 20 #include "chrome/browser/content_settings/permission_request_id.h" |
19 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 21 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
20 #include "chrome/browser/geolocation/geolocation_permission_context_factory.h" | 22 #include "chrome/browser/geolocation/geolocation_permission_context_factory.h" |
21 #include "chrome/browser/infobars/infobar_service.h" | 23 #include "chrome/browser/infobars/infobar_service.h" |
22 #include "chrome/browser/infobars/infobar_service.h" | 24 #include "chrome/browser/infobars/infobar_service.h" |
23 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 25 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
24 #include "chrome/test/base/testing_profile.h" | 26 #include "chrome/test/base/testing_profile.h" |
25 #include "components/infobars/core/confirm_infobar_delegate.h" | 27 #include "components/infobars/core/confirm_infobar_delegate.h" |
(...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
689 EXPECT_FALSE(infobar_delegate->ShouldExpire( | 691 EXPECT_FALSE(infobar_delegate->ShouldExpire( |
690 InfoBarService::NavigationDetailsFromLoadCommittedDetails(details))); | 692 InfoBarService::NavigationDetailsFromLoadCommittedDetails(details))); |
691 // Ensure the infobar will expire when we commit the pending navigation. | 693 // Ensure the infobar will expire when we commit the pending navigation. |
692 details.entry = web_contents()->GetController().GetActiveEntry(); | 694 details.entry = web_contents()->GetController().GetActiveEntry(); |
693 EXPECT_TRUE(infobar_delegate->ShouldExpire( | 695 EXPECT_TRUE(infobar_delegate->ShouldExpire( |
694 InfoBarService::NavigationDetailsFromLoadCommittedDetails(details))); | 696 InfoBarService::NavigationDetailsFromLoadCommittedDetails(details))); |
695 | 697 |
696 // Delete the tab contents. | 698 // Delete the tab contents. |
697 DeleteContents(); | 699 DeleteContents(); |
698 } | 700 } |
701 | |
702 TEST_F(GeolocationPermissionContextTests, LastUsageAudited) { | |
703 GURL requesting_frame("http://www.example.com/geolocation"); | |
704 NavigateAndCommit(requesting_frame); | |
705 | |
706 base::SimpleTestClock* test_clock = new base::SimpleTestClock; | |
Bernhard Bauer
2014/07/17 09:11:48
And here.
| |
707 test_clock->SetNow(base::Time::UnixEpoch() + | |
708 base::TimeDelta::FromSeconds(10)); | |
709 | |
710 HostContentSettingsMap* map = profile()->GetHostContentSettingsMap(); | |
711 map->SetPrefClockForTesting(scoped_ptr<base::Clock>(test_clock)); | |
712 | |
713 // The permission shouldn't have been used yet. | |
714 EXPECT_EQ(map->GetLastUsage(requesting_frame.GetOrigin(), | |
715 requesting_frame.GetOrigin(), | |
716 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), | |
717 0); | |
718 | |
719 EXPECT_EQ(0U, infobar_service()->infobar_count()); | |
720 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame); | |
721 ASSERT_EQ(1U, infobar_service()->infobar_count()); | |
722 infobars::InfoBar* infobar = infobar_service()->infobar_at(0); | |
723 ConfirmInfoBarDelegate* infobar_delegate = | |
724 infobar->delegate()->AsConfirmInfoBarDelegate(); | |
725 ASSERT_TRUE(infobar_delegate); | |
726 infobar_delegate->Accept(); | |
727 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW); | |
728 CheckPermissionMessageSent(0, true); | |
729 | |
730 // Permission has been used at the starting time. | |
731 EXPECT_EQ(map->GetLastUsage(requesting_frame.GetOrigin(), | |
732 requesting_frame.GetOrigin(), | |
733 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), | |
734 10); | |
735 | |
736 test_clock->Advance(base::TimeDelta::FromSeconds(3)); | |
737 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame); | |
738 | |
739 // Permission has been used three seconds later. | |
740 EXPECT_EQ(map->GetLastUsage(requesting_frame.GetOrigin(), | |
741 requesting_frame.GetOrigin(), | |
742 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), | |
743 13); | |
744 } | |
745 | |
746 TEST_F(GeolocationPermissionContextTests, LastUsageAuditedMultipleFrames) { | |
747 base::SimpleTestClock* test_clock = new base::SimpleTestClock; | |
748 test_clock->SetNow(base::Time::UnixEpoch() + | |
749 base::TimeDelta::FromSeconds(10)); | |
750 | |
751 HostContentSettingsMap* map = profile()->GetHostContentSettingsMap(); | |
752 map->SetPrefClockForTesting(scoped_ptr<base::Clock>(test_clock)); | |
753 | |
754 GURL requesting_frame_0("http://www.example.com/geolocation"); | |
755 GURL requesting_frame_1("http://www.example-2.com/geolocation"); | |
756 | |
757 // The permission shouldn't have been used yet. | |
758 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(), | |
759 requesting_frame_0.GetOrigin(), | |
760 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), | |
761 0); | |
762 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(), | |
763 requesting_frame_0.GetOrigin(), | |
764 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), | |
765 0); | |
766 | |
767 NavigateAndCommit(requesting_frame_0); | |
768 EXPECT_EQ(0U, infobar_service()->infobar_count()); | |
769 | |
770 // Request permission for two frames. | |
771 RequestGeolocationPermission( | |
772 web_contents(), RequestID(0), requesting_frame_0); | |
773 RequestGeolocationPermission( | |
774 web_contents(), RequestID(1), requesting_frame_1); | |
775 | |
776 // Ensure only one infobar is created. | |
777 ASSERT_EQ(1U, infobar_service()->infobar_count()); | |
778 infobars::InfoBar* infobar_0 = infobar_service()->infobar_at(0); | |
779 ConfirmInfoBarDelegate* infobar_delegate_0 = | |
780 infobar_0->delegate()->AsConfirmInfoBarDelegate(); | |
781 | |
782 // Accept the first frame. | |
783 infobar_delegate_0->Accept(); | |
784 CheckTabContentsState(requesting_frame_0, CONTENT_SETTING_ALLOW); | |
785 CheckPermissionMessageSent(0, true); | |
786 infobar_service()->RemoveInfoBar(infobar_0); | |
787 | |
788 // Verify that accepting the first didn't accept because it's embedder | |
789 // in the other. | |
790 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(), | |
791 requesting_frame_0.GetOrigin(), | |
792 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), | |
793 10); | |
794 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(), | |
795 requesting_frame_0.GetOrigin(), | |
796 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), | |
797 0); | |
798 | |
799 ASSERT_EQ(1U, infobar_service()->infobar_count()); | |
800 infobars::InfoBar* infobar_1 = infobar_service()->infobar_at(0); | |
801 ConfirmInfoBarDelegate* infobar_delegate_1 = | |
802 infobar_1->delegate()->AsConfirmInfoBarDelegate(); | |
803 | |
804 test_clock->Advance(base::TimeDelta::FromSeconds(1)); | |
805 | |
806 // Allow the second frame. | |
807 infobar_delegate_1->Accept(); | |
808 CheckTabContentsState(requesting_frame_1, CONTENT_SETTING_ALLOW); | |
809 CheckPermissionMessageSent(1, true); | |
810 infobar_service()->RemoveInfoBar(infobar_1); | |
811 | |
812 // Verify that the times are different. | |
813 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(), | |
814 requesting_frame_0.GetOrigin(), | |
815 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), | |
816 10); | |
817 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(), | |
818 requesting_frame_0.GetOrigin(), | |
819 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), | |
820 11); | |
821 | |
822 test_clock->Advance(base::TimeDelta::FromSeconds(2)); | |
823 RequestGeolocationPermission( | |
824 web_contents(), RequestID(0), requesting_frame_0); | |
825 | |
826 // Verify that requesting permission in one frame doesn't update other where | |
827 // it is the embedder. | |
828 EXPECT_EQ(map->GetLastUsage(requesting_frame_0.GetOrigin(), | |
829 requesting_frame_0.GetOrigin(), | |
830 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), | |
831 13); | |
832 EXPECT_EQ(map->GetLastUsage(requesting_frame_1.GetOrigin(), | |
833 requesting_frame_0.GetOrigin(), | |
834 CONTENT_SETTINGS_TYPE_GEOLOCATION).ToDoubleT(), | |
835 11); | |
836 } | |
OLD | NEW |