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; |
| 707 test_clock_->SetNow(base::Time::UnixEpoch() + |
| 708 base::TimeDelta::FromSeconds(10)); |
| 709 profile()->GetHostContentSettingsMap()->SetPrefClockForTesting(test_clock_); |
| 710 |
| 711 // The permission shouldn't have been used yet. |
| 712 EXPECT_EQ(profile() |
| 713 ->GetHostContentSettingsMap() |
| 714 ->GetLastUsage(requesting_frame.GetOrigin(), |
| 715 requesting_frame.GetOrigin(), |
| 716 CONTENT_SETTINGS_TYPE_GEOLOCATION) |
| 717 .ToDoubleT(), |
| 718 0); |
| 719 |
| 720 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 721 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame); |
| 722 ASSERT_EQ(1U, infobar_service()->infobar_count()); |
| 723 infobars::InfoBar* infobar = infobar_service()->infobar_at(0); |
| 724 ConfirmInfoBarDelegate* infobar_delegate = |
| 725 infobar->delegate()->AsConfirmInfoBarDelegate(); |
| 726 ASSERT_TRUE(infobar_delegate); |
| 727 infobar_delegate->Accept(); |
| 728 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW); |
| 729 CheckPermissionMessageSent(0, true); |
| 730 |
| 731 // Permission has been used at the starting time. |
| 732 EXPECT_EQ(profile() |
| 733 ->GetHostContentSettingsMap() |
| 734 ->GetLastUsage(requesting_frame.GetOrigin(), |
| 735 requesting_frame.GetOrigin(), |
| 736 CONTENT_SETTINGS_TYPE_GEOLOCATION) |
| 737 .ToDoubleT(), |
| 738 10); |
| 739 |
| 740 test_clock_->Advance(base::TimeDelta::FromSeconds(3)); |
| 741 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame); |
| 742 |
| 743 // Permission has been used three seconds later. |
| 744 EXPECT_EQ(profile() |
| 745 ->GetHostContentSettingsMap() |
| 746 ->GetLastUsage(requesting_frame.GetOrigin(), |
| 747 requesting_frame.GetOrigin(), |
| 748 CONTENT_SETTINGS_TYPE_GEOLOCATION) |
| 749 .ToDoubleT(), |
| 750 13); |
| 751 } |
| 752 |
| 753 TEST_F(GeolocationPermissionContextTests, LastUsageAuditedMultipleFrames) { |
| 754 base::SimpleTestClock* test_clock_ = new base::SimpleTestClock; |
| 755 test_clock_->SetNow(base::Time::UnixEpoch() + |
| 756 base::TimeDelta::FromSeconds(10)); |
| 757 profile()->GetHostContentSettingsMap()->SetPrefClockForTesting(test_clock_); |
| 758 |
| 759 GURL requesting_frame_0("http://www.example.com/geolocation"); |
| 760 GURL requesting_frame_1("http://www.example-2.com/geolocation"); |
| 761 // The permission shouldn't have been used yet. |
| 762 EXPECT_EQ(profile() |
| 763 ->GetHostContentSettingsMap() |
| 764 ->GetLastUsage(requesting_frame_0.GetOrigin(), |
| 765 requesting_frame_0.GetOrigin(), |
| 766 CONTENT_SETTINGS_TYPE_GEOLOCATION) |
| 767 .ToDoubleT(), |
| 768 0); |
| 769 EXPECT_EQ(profile() |
| 770 ->GetHostContentSettingsMap() |
| 771 ->GetLastUsage(requesting_frame_1.GetOrigin(), |
| 772 requesting_frame_0.GetOrigin(), |
| 773 CONTENT_SETTINGS_TYPE_GEOLOCATION) |
| 774 .ToDoubleT(), |
| 775 0); |
| 776 |
| 777 NavigateAndCommit(requesting_frame_0); |
| 778 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 779 |
| 780 // Request permission for two frames. |
| 781 RequestGeolocationPermission( |
| 782 web_contents(), RequestID(0), requesting_frame_0); |
| 783 RequestGeolocationPermission( |
| 784 web_contents(), RequestID(1), requesting_frame_1); |
| 785 |
| 786 // Ensure only one infobar is created. |
| 787 ASSERT_EQ(1U, infobar_service()->infobar_count()); |
| 788 infobars::InfoBar* infobar_0 = infobar_service()->infobar_at(0); |
| 789 ConfirmInfoBarDelegate* infobar_delegate_0 = |
| 790 infobar_0->delegate()->AsConfirmInfoBarDelegate(); |
| 791 |
| 792 // Accept the first frame. |
| 793 infobar_delegate_0->Accept(); |
| 794 CheckTabContentsState(requesting_frame_0, CONTENT_SETTING_ALLOW); |
| 795 CheckPermissionMessageSent(0, true); |
| 796 infobar_service()->RemoveInfoBar(infobar_0); |
| 797 |
| 798 // Verify that accepting the first didn't accept because it's embedder |
| 799 // in the other. |
| 800 EXPECT_EQ(profile() |
| 801 ->GetHostContentSettingsMap() |
| 802 ->GetLastUsage(requesting_frame_0.GetOrigin(), |
| 803 requesting_frame_0.GetOrigin(), |
| 804 CONTENT_SETTINGS_TYPE_GEOLOCATION) |
| 805 .ToDoubleT(), |
| 806 10); |
| 807 EXPECT_EQ(profile() |
| 808 ->GetHostContentSettingsMap() |
| 809 ->GetLastUsage(requesting_frame_1.GetOrigin(), |
| 810 requesting_frame_0.GetOrigin(), |
| 811 CONTENT_SETTINGS_TYPE_GEOLOCATION) |
| 812 .ToDoubleT(), |
| 813 0); |
| 814 |
| 815 ASSERT_EQ(1U, infobar_service()->infobar_count()); |
| 816 infobars::InfoBar* infobar_1 = infobar_service()->infobar_at(0); |
| 817 ConfirmInfoBarDelegate* infobar_delegate_1 = |
| 818 infobar_1->delegate()->AsConfirmInfoBarDelegate(); |
| 819 |
| 820 test_clock_->Advance(base::TimeDelta::FromSeconds(1)); |
| 821 |
| 822 // Allow the second frame. |
| 823 infobar_delegate_1->Accept(); |
| 824 CheckTabContentsState(requesting_frame_1, CONTENT_SETTING_ALLOW); |
| 825 CheckPermissionMessageSent(0, true); |
| 826 infobar_service()->RemoveInfoBar(infobar_1); |
| 827 |
| 828 // Verify that the times are different. |
| 829 EXPECT_EQ(profile() |
| 830 ->GetHostContentSettingsMap() |
| 831 ->GetLastUsage(requesting_frame_0.GetOrigin(), |
| 832 requesting_frame_0.GetOrigin(), |
| 833 CONTENT_SETTINGS_TYPE_GEOLOCATION) |
| 834 .ToDoubleT(), |
| 835 10); |
| 836 EXPECT_EQ(profile() |
| 837 ->GetHostContentSettingsMap() |
| 838 ->GetLastUsage(requesting_frame_1.GetOrigin(), |
| 839 requesting_frame_0.GetOrigin(), |
| 840 CONTENT_SETTINGS_TYPE_GEOLOCATION) |
| 841 .ToDoubleT(), |
| 842 11); |
| 843 |
| 844 test_clock_->Advance(base::TimeDelta::FromSeconds(2)); |
| 845 RequestGeolocationPermission( |
| 846 web_contents(), RequestID(0), requesting_frame_0); |
| 847 |
| 848 // Verify that requesting permission in one frame doesn't update other where |
| 849 // it is the embedder. |
| 850 EXPECT_EQ(profile() |
| 851 ->GetHostContentSettingsMap() |
| 852 ->GetLastUsage(requesting_frame_0.GetOrigin(), |
| 853 requesting_frame_0.GetOrigin(), |
| 854 CONTENT_SETTINGS_TYPE_GEOLOCATION) |
| 855 .ToDoubleT(), |
| 856 13); |
| 857 EXPECT_EQ(profile() |
| 858 ->GetHostContentSettingsMap() |
| 859 ->GetLastUsage(requesting_frame_1.GetOrigin(), |
| 860 requesting_frame_0.GetOrigin(), |
| 861 CONTENT_SETTINGS_TYPE_GEOLOCATION) |
| 862 .ToDoubleT(), |
| 863 11); |
| 864 } |
OLD | NEW |