| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ui/website_settings/permission_bubble_manager.h" | 5 #include "chrome/browser/permissions/permission_request_manager.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "chrome/browser/permissions/permission_context_base.h" | 10 #include "chrome/browser/permissions/permission_context_base.h" |
| 11 #include "chrome/browser/permissions/permission_util.h" | 11 #include "chrome/browser/permissions/permission_util.h" |
| 12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 14 #include "chrome/browser/ui/website_settings/mock_permission_bubble_factory.h" | 14 #include "chrome/browser/ui/website_settings/mock_permission_bubble_factory.h" |
| 15 #include "chrome/test/base/in_process_browser_test.h" | 15 #include "chrome/test/base/in_process_browser_test.h" |
| 16 #include "chrome/test/base/ui_test_utils.h" | 16 #include "chrome/test/base/ui_test_utils.h" |
| 17 #include "components/variations/variations_associated_data.h" | 17 #include "components/variations/variations_associated_data.h" |
| 18 #include "content/public/browser/permission_type.h" | 18 #include "content/public/browser/permission_type.h" |
| 19 #include "content/public/test/browser_test_utils.h" | 19 #include "content/public/test/browser_test_utils.h" |
| 20 #include "content/public/test/test_utils.h" | 20 #include "content/public/test/test_utils.h" |
| 21 #include "net/test/embedded_test_server/embedded_test_server.h" | 21 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 const char* kPermissionsKillSwitchFieldStudy = | 25 const char* kPermissionsKillSwitchFieldStudy = |
| 26 PermissionContextBase::kPermissionsKillSwitchFieldStudy; | 26 PermissionContextBase::kPermissionsKillSwitchFieldStudy; |
| 27 const char* kPermissionsKillSwitchBlockedValue = | 27 const char* kPermissionsKillSwitchBlockedValue = |
| 28 PermissionContextBase::kPermissionsKillSwitchBlockedValue; | 28 PermissionContextBase::kPermissionsKillSwitchBlockedValue; |
| 29 const char kPermissionsKillSwitchTestGroup[] = "TestGroup"; | 29 const char kPermissionsKillSwitchTestGroup[] = "TestGroup"; |
| 30 | 30 |
| 31 class PermissionBubbleManagerBrowserTest : public InProcessBrowserTest { | 31 class PermissionRequestManagerBrowserTest : public InProcessBrowserTest { |
| 32 public: | 32 public: |
| 33 PermissionBubbleManagerBrowserTest() = default; | 33 PermissionRequestManagerBrowserTest() = default; |
| 34 ~PermissionBubbleManagerBrowserTest() override = default; | 34 ~PermissionRequestManagerBrowserTest() override = default; |
| 35 | 35 |
| 36 void SetUpOnMainThread() override { | 36 void SetUpOnMainThread() override { |
| 37 InProcessBrowserTest::SetUpOnMainThread(); | 37 InProcessBrowserTest::SetUpOnMainThread(); |
| 38 PermissionBubbleManager* manager = GetPermissionBubbleManager(); | 38 PermissionRequestManager* manager = GetPermissionRequestManager(); |
| 39 mock_permission_bubble_factory_.reset( | 39 mock_permission_bubble_factory_.reset( |
| 40 new MockPermissionBubbleFactory(true, manager)); | 40 new MockPermissionBubbleFactory(true, manager)); |
| 41 manager->DisplayPendingRequests(); | 41 manager->DisplayPendingRequests(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 void TearDownOnMainThread() override { | 44 void TearDownOnMainThread() override { |
| 45 mock_permission_bubble_factory_.reset(); | 45 mock_permission_bubble_factory_.reset(); |
| 46 InProcessBrowserTest::TearDownOnMainThread(); | 46 InProcessBrowserTest::TearDownOnMainThread(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 PermissionBubbleManager* GetPermissionBubbleManager() { | 49 PermissionRequestManager* GetPermissionRequestManager() { |
| 50 return PermissionBubbleManager::FromWebContents( | 50 return PermissionRequestManager::FromWebContents( |
| 51 browser()->tab_strip_model()->GetActiveWebContents()); | 51 browser()->tab_strip_model()->GetActiveWebContents()); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void WaitForPermissionBubble() { | 54 void WaitForPermissionRequest() { |
| 55 if (bubble_factory()->is_visible()) | 55 if (bubble_factory()->is_visible()) |
| 56 return; | 56 return; |
| 57 content::RunMessageLoop(); | 57 content::RunMessageLoop(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 MockPermissionBubbleFactory* bubble_factory() { | 60 MockPermissionBubbleFactory* bubble_factory() { |
| 61 return mock_permission_bubble_factory_.get(); | 61 return mock_permission_bubble_factory_.get(); |
| 62 } | 62 } |
| 63 | 63 |
| 64 void EnableKillSwitch(content::PermissionType permission_type) { | 64 void EnableKillSwitch(content::PermissionType permission_type) { |
| 65 std::map<std::string, std::string> params; | 65 std::map<std::string, std::string> params; |
| 66 params[PermissionUtil::GetPermissionString(permission_type)] = | 66 params[PermissionUtil::GetPermissionString(permission_type)] = |
| 67 kPermissionsKillSwitchBlockedValue; | 67 kPermissionsKillSwitchBlockedValue; |
| 68 variations::AssociateVariationParams( | 68 variations::AssociateVariationParams( |
| 69 kPermissionsKillSwitchFieldStudy, kPermissionsKillSwitchTestGroup, | 69 kPermissionsKillSwitchFieldStudy, kPermissionsKillSwitchTestGroup, |
| 70 params); | 70 params); |
| 71 base::FieldTrialList::CreateFieldTrial(kPermissionsKillSwitchFieldStudy, | 71 base::FieldTrialList::CreateFieldTrial(kPermissionsKillSwitchFieldStudy, |
| 72 kPermissionsKillSwitchTestGroup); | 72 kPermissionsKillSwitchTestGroup); |
| 73 } | 73 } |
| 74 | 74 |
| 75 private: | 75 private: |
| 76 std::unique_ptr<MockPermissionBubbleFactory> mock_permission_bubble_factory_; | 76 std::unique_ptr<MockPermissionBubbleFactory> mock_permission_bubble_factory_; |
| 77 }; | 77 }; |
| 78 | 78 |
| 79 // Requests before the load event should be bundled into one bubble. | 79 // Requests before the load event should be bundled into one bubble. |
| 80 // http://crbug.com/512849 flaky | 80 // http://crbug.com/512849 flaky |
| 81 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest, | 81 IN_PROC_BROWSER_TEST_F(PermissionRequestManagerBrowserTest, |
| 82 DISABLED_RequestsBeforeLoad) { | 82 DISABLED_RequestsBeforeLoad) { |
| 83 ASSERT_TRUE(embedded_test_server()->Start()); | 83 ASSERT_TRUE(embedded_test_server()->Start()); |
| 84 | 84 |
| 85 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 85 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 86 browser(), | 86 browser(), |
| 87 embedded_test_server()->GetURL("/permissions/requests-before-load.html"), | 87 embedded_test_server()->GetURL("/permissions/requests-before-load.html"), |
| 88 1); | 88 1); |
| 89 WaitForPermissionBubble(); | 89 WaitForPermissionRequest(); |
| 90 | 90 |
| 91 EXPECT_EQ(1, bubble_factory()->show_count()); | 91 EXPECT_EQ(1, bubble_factory()->show_count()); |
| 92 EXPECT_EQ(2, bubble_factory()->total_request_count()); | 92 EXPECT_EQ(2, bubble_factory()->total_request_count()); |
| 93 } | 93 } |
| 94 | 94 |
| 95 // Requests before the load should not be bundled with a request after the load. | 95 // Requests before the load should not be bundled with a request after the load. |
| 96 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest, | 96 IN_PROC_BROWSER_TEST_F(PermissionRequestManagerBrowserTest, |
| 97 RequestsBeforeAfterLoad) { | 97 RequestsBeforeAfterLoad) { |
| 98 ASSERT_TRUE(embedded_test_server()->Start()); | 98 ASSERT_TRUE(embedded_test_server()->Start()); |
| 99 | 99 |
| 100 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 100 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 101 browser(), | 101 browser(), |
| 102 embedded_test_server()->GetURL( | 102 embedded_test_server()->GetURL( |
| 103 "/permissions/requests-before-after-load.html"), | 103 "/permissions/requests-before-after-load.html"), |
| 104 1); | 104 1); |
| 105 WaitForPermissionBubble(); | 105 WaitForPermissionRequest(); |
| 106 | 106 |
| 107 EXPECT_EQ(1, bubble_factory()->show_count()); | 107 EXPECT_EQ(1, bubble_factory()->show_count()); |
| 108 EXPECT_EQ(1, bubble_factory()->total_request_count()); | 108 EXPECT_EQ(1, bubble_factory()->total_request_count()); |
| 109 } | 109 } |
| 110 | 110 |
| 111 // Navigating twice to the same URL should be equivalent to refresh. This means | 111 // Navigating twice to the same URL should be equivalent to refresh. This means |
| 112 // showing the bubbles twice. | 112 // showing the bubbles twice. |
| 113 // http://crbug.com/512849 flaky | 113 // http://crbug.com/512849 flaky |
| 114 #if defined(OS_WIN) | 114 #if defined(OS_WIN) |
| 115 #define MAYBE_NavTwice DISABLED_NavTwice | 115 #define MAYBE_NavTwice DISABLED_NavTwice |
| 116 #else | 116 #else |
| 117 #define MAYBE_NavTwice NavTwice | 117 #define MAYBE_NavTwice NavTwice |
| 118 #endif | 118 #endif |
| 119 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest, MAYBE_NavTwice) { | 119 IN_PROC_BROWSER_TEST_F(PermissionRequestManagerBrowserTest, MAYBE_NavTwice) { |
| 120 ASSERT_TRUE(embedded_test_server()->Start()); | 120 ASSERT_TRUE(embedded_test_server()->Start()); |
| 121 | 121 |
| 122 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 122 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 123 browser(), | 123 browser(), |
| 124 embedded_test_server()->GetURL("/permissions/requests-before-load.html"), | 124 embedded_test_server()->GetURL("/permissions/requests-before-load.html"), |
| 125 1); | 125 1); |
| 126 WaitForPermissionBubble(); | 126 WaitForPermissionRequest(); |
| 127 | 127 |
| 128 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 128 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 129 browser(), | 129 browser(), |
| 130 embedded_test_server()->GetURL("/permissions/requests-before-load.html"), | 130 embedded_test_server()->GetURL("/permissions/requests-before-load.html"), |
| 131 1); | 131 1); |
| 132 WaitForPermissionBubble(); | 132 WaitForPermissionRequest(); |
| 133 | 133 |
| 134 EXPECT_EQ(2, bubble_factory()->show_count()); | 134 EXPECT_EQ(2, bubble_factory()->show_count()); |
| 135 EXPECT_EQ(4, bubble_factory()->total_request_count()); | 135 EXPECT_EQ(4, bubble_factory()->total_request_count()); |
| 136 } | 136 } |
| 137 | 137 |
| 138 // Navigating twice to the same URL with a hash should be navigation within the | 138 // Navigating twice to the same URL with a hash should be navigation within the |
| 139 // page. This means the bubble is only shown once. | 139 // page. This means the bubble is only shown once. |
| 140 // http://crbug.com/512849 flaky | 140 // http://crbug.com/512849 flaky |
| 141 #if defined(OS_WIN) | 141 #if defined(OS_WIN) |
| 142 #define MAYBE_NavTwiceWithHash DISABLED_NavTwiceWithHash | 142 #define MAYBE_NavTwiceWithHash DISABLED_NavTwiceWithHash |
| 143 #else | 143 #else |
| 144 #define MAYBE_NavTwiceWithHash NavTwiceWithHash | 144 #define MAYBE_NavTwiceWithHash NavTwiceWithHash |
| 145 #endif | 145 #endif |
| 146 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest, | 146 IN_PROC_BROWSER_TEST_F(PermissionRequestManagerBrowserTest, |
| 147 MAYBE_NavTwiceWithHash) { | 147 MAYBE_NavTwiceWithHash) { |
| 148 ASSERT_TRUE(embedded_test_server()->Start()); | 148 ASSERT_TRUE(embedded_test_server()->Start()); |
| 149 | 149 |
| 150 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 150 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 151 browser(), | 151 browser(), |
| 152 embedded_test_server()->GetURL("/permissions/requests-before-load.html"), | 152 embedded_test_server()->GetURL("/permissions/requests-before-load.html"), |
| 153 1); | 153 1); |
| 154 WaitForPermissionBubble(); | 154 WaitForPermissionRequest(); |
| 155 | 155 |
| 156 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 156 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 157 browser(), | 157 browser(), |
| 158 embedded_test_server()->GetURL( | 158 embedded_test_server()->GetURL( |
| 159 "/permissions/requests-before-load.html#0"), | 159 "/permissions/requests-before-load.html#0"), |
| 160 1); | 160 1); |
| 161 WaitForPermissionBubble(); | 161 WaitForPermissionRequest(); |
| 162 | 162 |
| 163 EXPECT_EQ(1, bubble_factory()->show_count()); | 163 EXPECT_EQ(1, bubble_factory()->show_count()); |
| 164 EXPECT_EQ(2, bubble_factory()->total_request_count()); | 164 EXPECT_EQ(2, bubble_factory()->total_request_count()); |
| 165 } | 165 } |
| 166 | 166 |
| 167 // Bubble requests should be shown after in-page navigation. | 167 // Bubble requests should be shown after in-page navigation. |
| 168 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest, InPageNavigation) { | 168 IN_PROC_BROWSER_TEST_F(PermissionRequestManagerBrowserTest, InPageNavigation) { |
| 169 ASSERT_TRUE(embedded_test_server()->Start()); | 169 ASSERT_TRUE(embedded_test_server()->Start()); |
| 170 | 170 |
| 171 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 171 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 172 browser(), | 172 browser(), |
| 173 embedded_test_server()->GetURL("/empty.html"), | 173 embedded_test_server()->GetURL("/empty.html"), |
| 174 1); | 174 1); |
| 175 | 175 |
| 176 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 176 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 177 browser(), | 177 browser(), |
| 178 embedded_test_server()->GetURL("/empty.html#0"), | 178 embedded_test_server()->GetURL("/empty.html#0"), |
| 179 1); | 179 1); |
| 180 | 180 |
| 181 // Request 'geolocation' permission. | 181 // Request 'geolocation' permission. |
| 182 ExecuteScriptAndGetValue( | 182 ExecuteScriptAndGetValue( |
| 183 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), | 183 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), |
| 184 "navigator.geolocation.getCurrentPosition(function(){});"); | 184 "navigator.geolocation.getCurrentPosition(function(){});"); |
| 185 WaitForPermissionBubble(); | 185 WaitForPermissionRequest(); |
| 186 | 186 |
| 187 EXPECT_EQ(1, bubble_factory()->show_count()); | 187 EXPECT_EQ(1, bubble_factory()->show_count()); |
| 188 EXPECT_EQ(1, bubble_factory()->total_request_count()); | 188 EXPECT_EQ(1, bubble_factory()->total_request_count()); |
| 189 } | 189 } |
| 190 | 190 |
| 191 // Bubble requests should not be shown when the killswitch is on. | 191 // Bubble requests should not be shown when the killswitch is on. |
| 192 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest, | 192 IN_PROC_BROWSER_TEST_F(PermissionRequestManagerBrowserTest, |
| 193 KillSwitchGeolocation) { | 193 KillSwitchGeolocation) { |
| 194 ASSERT_TRUE(embedded_test_server()->Start()); | 194 ASSERT_TRUE(embedded_test_server()->Start()); |
| 195 | 195 |
| 196 ui_test_utils::NavigateToURL( | 196 ui_test_utils::NavigateToURL( |
| 197 browser(), | 197 browser(), |
| 198 embedded_test_server()->GetURL("/permissions/killswitch_tester.html")); | 198 embedded_test_server()->GetURL("/permissions/killswitch_tester.html")); |
| 199 | 199 |
| 200 // Now enable the geolocation killswitch. | 200 // Now enable the geolocation killswitch. |
| 201 EnableKillSwitch(content::PermissionType::GEOLOCATION); | 201 EnableKillSwitch(content::PermissionType::GEOLOCATION); |
| 202 content::WebContents* web_contents = | 202 content::WebContents* web_contents = |
| 203 browser()->tab_strip_model()->GetActiveWebContents(); | 203 browser()->tab_strip_model()->GetActiveWebContents(); |
| 204 | 204 |
| 205 std::string result; | 205 std::string result; |
| 206 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 206 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 207 web_contents, "requestGeolocation();", &result)); | 207 web_contents, "requestGeolocation();", &result)); |
| 208 EXPECT_EQ("denied", result); | 208 EXPECT_EQ("denied", result); |
| 209 EXPECT_EQ(0, bubble_factory()->show_count()); | 209 EXPECT_EQ(0, bubble_factory()->show_count()); |
| 210 EXPECT_EQ(0, bubble_factory()->total_request_count()); | 210 EXPECT_EQ(0, bubble_factory()->total_request_count()); |
| 211 | 211 |
| 212 // Disable the trial. | 212 // Disable the trial. |
| 213 variations::testing::ClearAllVariationParams(); | 213 variations::testing::ClearAllVariationParams(); |
| 214 | 214 |
| 215 // Reload the page to get around blink layer caching for geolocation | 215 // Reload the page to get around blink layer caching for geolocation |
| 216 // requests. | 216 // requests. |
| 217 ui_test_utils::NavigateToURL( | 217 ui_test_utils::NavigateToURL( |
| 218 browser(), | 218 browser(), |
| 219 embedded_test_server()->GetURL("/permissions/killswitch_tester.html")); | 219 embedded_test_server()->GetURL("/permissions/killswitch_tester.html")); |
| 220 | 220 |
| 221 EXPECT_TRUE(content::ExecuteScript(web_contents, "requestGeolocation();")); | 221 EXPECT_TRUE(content::ExecuteScript(web_contents, "requestGeolocation();")); |
| 222 WaitForPermissionBubble(); | 222 WaitForPermissionRequest(); |
| 223 EXPECT_EQ(1, bubble_factory()->show_count()); | 223 EXPECT_EQ(1, bubble_factory()->show_count()); |
| 224 EXPECT_EQ(1, bubble_factory()->total_request_count()); | 224 EXPECT_EQ(1, bubble_factory()->total_request_count()); |
| 225 } | 225 } |
| 226 | 226 |
| 227 // Bubble requests should not be shown when the killswitch is on. | 227 // Bubble requests should not be shown when the killswitch is on. |
| 228 IN_PROC_BROWSER_TEST_F(PermissionBubbleManagerBrowserTest, | 228 IN_PROC_BROWSER_TEST_F(PermissionRequestManagerBrowserTest, |
| 229 KillSwitchNotifications) { | 229 KillSwitchNotifications) { |
| 230 ASSERT_TRUE(embedded_test_server()->Start()); | 230 ASSERT_TRUE(embedded_test_server()->Start()); |
| 231 | 231 |
| 232 ui_test_utils::NavigateToURL( | 232 ui_test_utils::NavigateToURL( |
| 233 browser(), | 233 browser(), |
| 234 embedded_test_server()->GetURL("/permissions/killswitch_tester.html")); | 234 embedded_test_server()->GetURL("/permissions/killswitch_tester.html")); |
| 235 | 235 |
| 236 // Now enable the notifications killswitch. | 236 // Now enable the notifications killswitch. |
| 237 EnableKillSwitch(content::PermissionType::NOTIFICATIONS); | 237 EnableKillSwitch(content::PermissionType::NOTIFICATIONS); |
| 238 content::WebContents* web_contents = | 238 content::WebContents* web_contents = |
| 239 browser()->tab_strip_model()->GetActiveWebContents(); | 239 browser()->tab_strip_model()->GetActiveWebContents(); |
| 240 | 240 |
| 241 std::string result; | 241 std::string result; |
| 242 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | 242 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 243 web_contents, "requestNotification();", &result)); | 243 web_contents, "requestNotification();", &result)); |
| 244 EXPECT_EQ("denied", result); | 244 EXPECT_EQ("denied", result); |
| 245 EXPECT_EQ(0, bubble_factory()->show_count()); | 245 EXPECT_EQ(0, bubble_factory()->show_count()); |
| 246 EXPECT_EQ(0, bubble_factory()->total_request_count()); | 246 EXPECT_EQ(0, bubble_factory()->total_request_count()); |
| 247 | 247 |
| 248 // Disable the trial. | 248 // Disable the trial. |
| 249 variations::testing::ClearAllVariationParams(); | 249 variations::testing::ClearAllVariationParams(); |
| 250 | 250 |
| 251 EXPECT_TRUE(content::ExecuteScript(web_contents, "requestNotification();")); | 251 EXPECT_TRUE(content::ExecuteScript(web_contents, "requestNotification();")); |
| 252 WaitForPermissionBubble(); | 252 WaitForPermissionRequest(); |
| 253 EXPECT_EQ(1, bubble_factory()->show_count()); | 253 EXPECT_EQ(1, bubble_factory()->show_count()); |
| 254 EXPECT_EQ(1, bubble_factory()->total_request_count()); | 254 EXPECT_EQ(1, bubble_factory()->total_request_count()); |
| 255 } | 255 } |
| 256 | 256 |
| 257 } // anonymous namespace | 257 } // anonymous namespace |
| OLD | NEW |