Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/geolocation/geolocation_service_impl.h" |
| 6 | |
Peter Boström | dd7e40ec | 2021-04-05 20:40:10 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 9 | #include "base/functional/bind.h" |
| 10 | #include "base/functional/callback_helpers.h" |
Gabriel Charette | 078e366 | 2017-08-28 22:59:04 | [diff] [blame] | 11 | #include "base/run_loop.h" |
Sean Maher | e672a66 | 2023-01-09 21:42:28 | [diff] [blame] | 12 | #include "base/task/single_thread_task_runner.h" |
Matt Reynolds | 5e784e9 | 2023-04-21 01:40:27 | [diff] [blame] | 13 | #include "base/test/test_future.h" |
Andrey Lushnikov | ebff044 | 2018-07-12 20:02:58 | [diff] [blame] | 14 | #include "content/browser/permissions/permission_controller_impl.h" |
Ken Rockot | ce010f0 | 2019-12-12 23:32:32 | [diff] [blame] | 15 | #include "content/public/browser/device_service.h" |
Andrey Lushnikov | ebff044 | 2018-07-12 20:02:58 | [diff] [blame] | 16 | #include "content/public/browser/permission_controller.h" |
Andy Paicu | 0a6d4b50 | 2023-08-29 15:13:09 | [diff] [blame] | 17 | #include "content/public/browser/permission_request_description.h" |
Florian Jacky | aaf4283 | 2025-08-19 04:03:26 | [diff] [blame] | 18 | #include "content/public/browser/permission_result.h" |
Douglas Creager | 5964cda | 2018-03-01 16:40:29 | [diff] [blame] | 19 | #include "content/public/test/mock_permission_manager.h" |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 20 | #include "content/public/test/navigation_simulator.h" |
Andrey Lushnikov | ebff044 | 2018-07-12 20:02:58 | [diff] [blame] | 21 | #include "content/public/test/test_browser_context.h" |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 22 | #include "content/test/test_render_frame_host.h" |
Miyoung Shin | 9d893fd | 2019-09-11 14:58:54 | [diff] [blame] | 23 | #include "mojo/public/cpp/bindings/remote.h" |
Ke He | f417339 | 2018-06-29 07:44:15 | [diff] [blame] | 24 | #include "services/device/public/cpp/test/scoped_geolocation_overrider.h" |
Ke He | 31d0bb0 | 2018-02-24 07:16:24 | [diff] [blame] | 25 | #include "services/device/public/mojom/geolocation.mojom.h" |
Ke He | 81d1533 | 2018-03-01 04:02:28 | [diff] [blame] | 26 | #include "services/device/public/mojom/geolocation_context.mojom.h" |
Ke He | 31d0bb0 | 2018-02-24 07:16:24 | [diff] [blame] | 27 | #include "services/device/public/mojom/geoposition.mojom.h" |
Sandor Major | ca47512a | 2025-02-11 16:58:04 | [diff] [blame] | 28 | #include "services/network/public/cpp/permissions_policy/origin_with_possible_wildcards.h" |
Sandor Major | 878f835 | 2025-02-18 20:16:02 | [diff] [blame] | 29 | #include "services/network/public/cpp/permissions_policy/permissions_policy_declaration.h" |
Sandor «Alex» Major | c41217f | 2025-02-14 23:33:13 | [diff] [blame] | 30 | #include "services/network/public/mojom/permissions_policy/permissions_policy_feature.mojom.h" |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 31 | #include "testing/gtest/include/gtest/gtest.h" |
Andy Paicu | a6d6d85 | 2022-04-28 18:08:36 | [diff] [blame] | 32 | #include "third_party/blink/public/common/permissions/permission_utils.h" |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 33 | |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 34 | namespace content { |
| 35 | namespace { |
| 36 | |
Matt Reynolds | 5e784e9 | 2023-04-21 01:40:27 | [diff] [blame] | 37 | using ::base::test::TestFuture; |
| 38 | using ::blink::mojom::GeolocationService; |
| 39 | using ::blink::mojom::PermissionStatus; |
| 40 | using ::device::mojom::Geolocation; |
| 41 | using ::device::mojom::GeopositionPtr; |
| 42 | using ::device::mojom::GeopositionResultPtr; |
| 43 | |
Florian Jacky | aaf4283 | 2025-08-19 04:03:26 | [diff] [blame] | 44 | using PermissionCallback = |
| 45 | base::OnceCallback<void(const std::vector<PermissionResult>&)>; |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 46 | |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 47 | double kMockLatitude = 1.0; |
| 48 | double kMockLongitude = 10.0; |
Michael Thiessen | 3067e4e | 2020-01-17 19:13:24 | [diff] [blame] | 49 | |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 50 | class TestPermissionManager : public MockPermissionManager { |
| 51 | public: |
Balazs Engedy | e30e961 | 2021-04-02 10:37:29 | [diff] [blame] | 52 | TestPermissionManager() = default; |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 53 | ~TestPermissionManager() override = default; |
| 54 | |
Illia Klimov | 27239edc | 2022-05-11 17:14:59 | [diff] [blame] | 55 | void RequestPermissionsFromCurrentDocument( |
Andy Paicu | 0a6d4b50 | 2023-08-29 15:13:09 | [diff] [blame] | 56 | RenderFrameHost* render_frame_host, |
| 57 | const PermissionRequestDescription& request_description, |
Florian Jacky | aaf4283 | 2025-08-19 04:03:26 | [diff] [blame] | 58 | base::OnceCallback<void(const std::vector<content::PermissionResult>&)> |
| 59 | callback) override { |
Andy Paicu | 0a6d4b50 | 2023-08-29 15:13:09 | [diff] [blame] | 60 | ASSERT_EQ(request_description.permissions.size(), 1u); |
Florian Jacky | 65b7d10 | 2025-04-07 10:02:52 | [diff] [blame] | 61 | EXPECT_EQ(blink::PermissionDescriptorToPermissionType( |
| 62 | request_description.permissions[0]), |
Andy Paicu | 0a6d4b50 | 2023-08-29 15:13:09 | [diff] [blame] | 63 | blink::PermissionType::GEOLOCATION); |
| 64 | EXPECT_TRUE(request_description.user_gesture); |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 65 | request_callback_.Run(std::move(callback)); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 66 | } |
| 67 | |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 68 | void SetRequestCallback( |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 69 | base::RepeatingCallback<void(PermissionCallback)> request_callback) { |
| 70 | request_callback_ = std::move(request_callback); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 71 | } |
| 72 | |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 73 | private: |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 74 | base::RepeatingCallback<void(PermissionCallback)> request_callback_; |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | class GeolocationServiceTest : public RenderViewHostImplTestHarness { |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 78 | public: |
| 79 | GeolocationServiceTest(const GeolocationServiceTest&) = delete; |
| 80 | GeolocationServiceTest& operator=(const GeolocationServiceTest&) = delete; |
| 81 | |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 82 | protected: |
| 83 | GeolocationServiceTest() {} |
| 84 | |
| 85 | ~GeolocationServiceTest() override {} |
| 86 | |
| 87 | void SetUp() override { |
| 88 | RenderViewHostImplTestHarness::SetUp(); |
Peter Kasting | 8bdc8281 | 2020-02-17 18:25:16 | [diff] [blame] | 89 | NavigateAndCommit(GURL("https://www.google.com/maps")); |
Takumi Fujimoto | 66a5e300 | 2023-09-14 22:58:31 | [diff] [blame] | 90 | static_cast<TestBrowserContext*>(GetBrowserContext()) |
| 91 | ->SetPermissionControllerDelegate( |
| 92 | std::make_unique<TestPermissionManager>()); |
Ke He | 81d1533 | 2018-03-01 04:02:28 | [diff] [blame] | 93 | |
Ke He | 81d1533 | 2018-03-01 04:02:28 | [diff] [blame] | 94 | geolocation_overrider_ = |
| 95 | std::make_unique<device::ScopedGeolocationOverrider>(kMockLatitude, |
| 96 | kMockLongitude); |
Ken Rockot | ce010f0 | 2019-12-12 23:32:32 | [diff] [blame] | 97 | GetDeviceService().BindGeolocationContext( |
| 98 | context_.BindNewPipeAndPassReceiver()); |
Ke He | 81d1533 | 2018-03-01 04:02:28 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void TearDown() override { |
Yifan Luo | 8e5d3d5 | 2024-10-22 19:18:16 | [diff] [blame] | 102 | service_.reset(); |
Miyoung Shin | 0f4480d | 2019-09-12 01:15:26 | [diff] [blame] | 103 | context_.reset(); |
Ke He | 81d1533 | 2018-03-01 04:02:28 | [diff] [blame] | 104 | geolocation_overrider_.reset(); |
Ke He | 81d1533 | 2018-03-01 04:02:28 | [diff] [blame] | 105 | RenderViewHostImplTestHarness::TearDown(); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 106 | } |
| 107 | |
Charlie Hu | e20fe2f | 2021-03-07 03:39:59 | [diff] [blame] | 108 | void CreateEmbeddedFrameAndGeolocationService( |
| 109 | bool allow_via_permissions_policy) { |
Peter Kasting | 8bdc8281 | 2020-02-17 18:25:16 | [diff] [blame] | 110 | const GURL kEmbeddedUrl("https://embeddables.com/someframe"); |
Sandor Major | 878f835 | 2025-02-18 20:16:02 | [diff] [blame] | 111 | network::ParsedPermissionsPolicy frame_policy = {}; |
Charlie Hu | e20fe2f | 2021-03-07 03:39:59 | [diff] [blame] | 112 | if (allow_via_permissions_policy) { |
Ian Clelland | 156a520 | 2020-09-30 17:48:21 | [diff] [blame] | 113 | frame_policy.push_back( |
Sandor «Alex» Major | e9545a7 | 2025-01-31 20:40:46 | [diff] [blame] | 114 | {network::mojom::PermissionsPolicyFeature::kGeolocation, |
Sandor Major | ca47512a | 2025-02-11 16:58:04 | [diff] [blame] | 115 | std::vector{*network::OriginWithPossibleWildcards::FromOrigin( |
Ari Chivukula | 30c4e91 | 2023-05-25 12:35:50 | [diff] [blame] | 116 | url::Origin::Create(kEmbeddedUrl))}, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 117 | /*self_if_matches=*/std::nullopt, |
Ari Chivukula | 04f6ff7e | 2023-03-22 18:02:00 | [diff] [blame] | 118 | /*matches_all_origins=*/false, |
| 119 | /*matches_opaque_src=*/false}); |
Raymes Khoury | 4361358 | 2017-09-28 09:22:36 | [diff] [blame] | 120 | } |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 121 | RenderFrameHost* embedded_rfh = |
Ian Clelland | 156a520 | 2020-09-30 17:48:21 | [diff] [blame] | 122 | RenderFrameHostTester::For(main_rfh()) |
| 123 | ->AppendChildWithPolicy("", frame_policy); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 124 | RenderFrameHostTester::For(embedded_rfh)->InitializeRenderFrameIfNeeded(); |
| 125 | auto navigation_simulator = NavigationSimulator::CreateRendererInitiated( |
Peter Kasting | 8bdc8281 | 2020-02-17 18:25:16 | [diff] [blame] | 126 | kEmbeddedUrl, embedded_rfh); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 127 | navigation_simulator->Commit(); |
| 128 | embedded_rfh = navigation_simulator->GetFinalRenderFrameHost(); |
Peter Boström | dd7e40ec | 2021-04-05 20:40:10 | [diff] [blame] | 129 | service_ = |
| 130 | std::make_unique<GeolocationServiceImpl>(context_.get(), embedded_rfh); |
Mario Sanchez Prada | fa6dda8c | 2019-11-25 18:20:19 | [diff] [blame] | 131 | service_->Bind(service_remote_.BindNewPipeAndPassReceiver()); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 132 | } |
| 133 | |
Mario Sanchez Prada | fa6dda8c | 2019-11-25 18:20:19 | [diff] [blame] | 134 | mojo::Remote<blink::mojom::GeolocationService>& service_remote() { |
| 135 | return service_remote_; |
| 136 | } |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 137 | |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 138 | TestPermissionManager* permission_manager() { |
Andrey Lushnikov | ebff044 | 2018-07-12 20:02:58 | [diff] [blame] | 139 | return static_cast<TestPermissionManager*>( |
Takumi Fujimoto | 66a5e300 | 2023-09-14 22:58:31 | [diff] [blame] | 140 | GetBrowserContext()->GetPermissionControllerDelegate()); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | private: |
Ke He | 81d1533 | 2018-03-01 04:02:28 | [diff] [blame] | 144 | std::unique_ptr<device::ScopedGeolocationOverrider> geolocation_overrider_; |
Conley Owens | 9613ff9a | 2017-11-27 21:06:55 | [diff] [blame] | 145 | std::unique_ptr<GeolocationServiceImpl> service_; |
Mario Sanchez Prada | fa6dda8c | 2019-11-25 18:20:19 | [diff] [blame] | 146 | mojo::Remote<blink::mojom::GeolocationService> service_remote_; |
Miyoung Shin | 0f4480d | 2019-09-12 01:15:26 | [diff] [blame] | 147 | mojo::Remote<device::mojom::GeolocationContext> context_; |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | } // namespace |
| 151 | |
| 152 | TEST_F(GeolocationServiceTest, PermissionGrantedPolicyViolation) { |
Aaron Colwell | da68cea | 2019-10-10 02:55:26 | [diff] [blame] | 153 | // The embedded frame is not allowed. |
Charlie Hu | e20fe2f | 2021-03-07 03:39:59 | [diff] [blame] | 154 | CreateEmbeddedFrameAndGeolocationService( |
| 155 | /*allow_via_permissions_policy=*/false); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 156 | |
| 157 | permission_manager()->SetRequestCallback( |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 158 | base::BindRepeating([](PermissionCallback callback) { |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 159 | ADD_FAILURE() << "Permissions checked unexpectedly."; |
| 160 | })); |
Miyoung Shin | 9d893fd | 2019-09-11 14:58:54 | [diff] [blame] | 161 | mojo::Remote<Geolocation> geolocation; |
Mario Sanchez Prada | fa6dda8c | 2019-11-25 18:20:19 | [diff] [blame] | 162 | service_remote()->CreateGeolocation( |
Miyoung Shin | 9d893fd | 2019-09-11 14:58:54 | [diff] [blame] | 163 | geolocation.BindNewPipeAndPassReceiver(), true, |
Jan Wilken Dörrie | 8c74db02 | 2020-04-20 09:05:00 | [diff] [blame] | 164 | base::BindOnce([](blink::mojom::PermissionStatus status) { |
Matt Reynolds | f10fd2f | 2019-04-01 19:39:29 | [diff] [blame] | 165 | EXPECT_EQ(blink::mojom::PermissionStatus::DENIED, status); |
| 166 | })); |
Alvin Ji | 9e137ea | 2025-02-20 05:03:02 | [diff] [blame] | 167 | TestFuture<void> disconnect_future; |
| 168 | geolocation.set_disconnect_handler(disconnect_future.GetCallback()); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 169 | |
Matt Reynolds | 5e784e9 | 2023-04-21 01:40:27 | [diff] [blame] | 170 | geolocation->QueryNextPosition( |
| 171 | base::BindOnce([](GeopositionResultPtr result) { |
| 172 | ADD_FAILURE() << "Position updated unexpectedly"; |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 173 | })); |
Alvin Ji | 9e137ea | 2025-02-20 05:03:02 | [diff] [blame] | 174 | EXPECT_TRUE(disconnect_future.Wait()); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | TEST_F(GeolocationServiceTest, PermissionGrantedSync) { |
Charlie Hu | e20fe2f | 2021-03-07 03:39:59 | [diff] [blame] | 178 | CreateEmbeddedFrameAndGeolocationService( |
| 179 | /*allow_via_permissions_policy=*/true); |
Alvin Ji | 9e137ea | 2025-02-20 05:03:02 | [diff] [blame] | 180 | TestFuture<PermissionCallback> permission_request_future; |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 181 | permission_manager()->SetRequestCallback( |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 182 | base::BindRepeating([](PermissionCallback callback) { |
Florian Jacky | aaf4283 | 2025-08-19 04:03:26 | [diff] [blame] | 183 | std::move(callback).Run(std::vector{content::PermissionResult( |
| 184 | PermissionStatus::GRANTED, PermissionStatusSource::UNSPECIFIED)}); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 185 | })); |
Miyoung Shin | 9d893fd | 2019-09-11 14:58:54 | [diff] [blame] | 186 | mojo::Remote<Geolocation> geolocation; |
Mario Sanchez Prada | fa6dda8c | 2019-11-25 18:20:19 | [diff] [blame] | 187 | service_remote()->CreateGeolocation( |
Miyoung Shin | 9d893fd | 2019-09-11 14:58:54 | [diff] [blame] | 188 | geolocation.BindNewPipeAndPassReceiver(), true, |
Jan Wilken Dörrie | 8c74db02 | 2020-04-20 09:05:00 | [diff] [blame] | 189 | base::BindOnce([](blink::mojom::PermissionStatus status) { |
Matt Reynolds | f10fd2f | 2019-04-01 19:39:29 | [diff] [blame] | 190 | EXPECT_EQ(blink::mojom::PermissionStatus::GRANTED, status); |
| 191 | })); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 192 | |
Miyoung Shin | 9d893fd | 2019-09-11 14:58:54 | [diff] [blame] | 193 | geolocation.set_disconnect_handler(base::BindOnce( |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 194 | [] { ADD_FAILURE() << "Connection error handler called unexpectedly"; })); |
| 195 | |
Matt Reynolds | 5e784e9 | 2023-04-21 01:40:27 | [diff] [blame] | 196 | TestFuture<GeopositionResultPtr> result_future; |
| 197 | geolocation->QueryNextPosition(result_future.GetCallback()); |
| 198 | ASSERT_TRUE(result_future.Get()->is_position()); |
| 199 | const auto& position = *result_future.Get()->get_position(); |
| 200 | EXPECT_DOUBLE_EQ(kMockLatitude, position.latitude); |
| 201 | EXPECT_DOUBLE_EQ(kMockLongitude, position.longitude); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | TEST_F(GeolocationServiceTest, PermissionDeniedSync) { |
Charlie Hu | e20fe2f | 2021-03-07 03:39:59 | [diff] [blame] | 205 | CreateEmbeddedFrameAndGeolocationService( |
| 206 | /*allow_via_permissions_policy=*/true); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 207 | permission_manager()->SetRequestCallback( |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 208 | base::BindRepeating([](PermissionCallback callback) { |
Florian Jacky | aaf4283 | 2025-08-19 04:03:26 | [diff] [blame] | 209 | std::move(callback).Run(std::vector{content::PermissionResult( |
| 210 | PermissionStatus::DENIED, PermissionStatusSource::UNSPECIFIED)}); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 211 | })); |
Miyoung Shin | 9d893fd | 2019-09-11 14:58:54 | [diff] [blame] | 212 | mojo::Remote<Geolocation> geolocation; |
Mario Sanchez Prada | fa6dda8c | 2019-11-25 18:20:19 | [diff] [blame] | 213 | service_remote()->CreateGeolocation( |
Miyoung Shin | 9d893fd | 2019-09-11 14:58:54 | [diff] [blame] | 214 | geolocation.BindNewPipeAndPassReceiver(), true, |
Jan Wilken Dörrie | 8c74db02 | 2020-04-20 09:05:00 | [diff] [blame] | 215 | base::BindOnce([](blink::mojom::PermissionStatus status) { |
Matt Reynolds | f10fd2f | 2019-04-01 19:39:29 | [diff] [blame] | 216 | EXPECT_EQ(blink::mojom::PermissionStatus::DENIED, status); |
| 217 | })); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 218 | |
Alvin Ji | 9e137ea | 2025-02-20 05:03:02 | [diff] [blame] | 219 | TestFuture<void> disconnect_future; |
| 220 | geolocation.set_disconnect_handler(disconnect_future.GetCallback()); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 221 | |
Matt Reynolds | 5e784e9 | 2023-04-21 01:40:27 | [diff] [blame] | 222 | geolocation->QueryNextPosition( |
| 223 | base::BindOnce([](GeopositionResultPtr result) { |
| 224 | ADD_FAILURE() << "Position updated unexpectedly"; |
| 225 | })); |
Alvin Ji | 9e137ea | 2025-02-20 05:03:02 | [diff] [blame] | 226 | EXPECT_TRUE(disconnect_future.Wait()); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | TEST_F(GeolocationServiceTest, PermissionGrantedAsync) { |
Charlie Hu | e20fe2f | 2021-03-07 03:39:59 | [diff] [blame] | 230 | CreateEmbeddedFrameAndGeolocationService( |
| 231 | /*allow_via_permissions_policy=*/true); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 232 | permission_manager()->SetRequestCallback( |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 233 | base::BindRepeating([](PermissionCallback permission_callback) { |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 234 | base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
Florian Jacky | aaf4283 | 2025-08-19 04:03:26 | [diff] [blame] | 235 | FROM_HERE, |
| 236 | base::BindOnce(std::move(permission_callback), |
| 237 | std::vector{content::PermissionResult( |
| 238 | PermissionStatus::GRANTED, |
| 239 | PermissionStatusSource::UNSPECIFIED)})); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 240 | })); |
Miyoung Shin | 9d893fd | 2019-09-11 14:58:54 | [diff] [blame] | 241 | mojo::Remote<Geolocation> geolocation; |
Mario Sanchez Prada | fa6dda8c | 2019-11-25 18:20:19 | [diff] [blame] | 242 | service_remote()->CreateGeolocation( |
Miyoung Shin | 9d893fd | 2019-09-11 14:58:54 | [diff] [blame] | 243 | geolocation.BindNewPipeAndPassReceiver(), true, |
Jan Wilken Dörrie | 8c74db02 | 2020-04-20 09:05:00 | [diff] [blame] | 244 | base::BindOnce([](blink::mojom::PermissionStatus status) { |
Matt Reynolds | f10fd2f | 2019-04-01 19:39:29 | [diff] [blame] | 245 | EXPECT_EQ(blink::mojom::PermissionStatus::GRANTED, status); |
| 246 | })); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 247 | |
Miyoung Shin | 9d893fd | 2019-09-11 14:58:54 | [diff] [blame] | 248 | geolocation.set_disconnect_handler(base::BindOnce( |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 249 | [] { ADD_FAILURE() << "Connection error handler called unexpectedly"; })); |
| 250 | |
Matt Reynolds | 5e784e9 | 2023-04-21 01:40:27 | [diff] [blame] | 251 | TestFuture<GeopositionResultPtr> result_future; |
| 252 | geolocation->QueryNextPosition(result_future.GetCallback()); |
| 253 | ASSERT_TRUE(result_future.Get()->is_position()); |
| 254 | const auto& position = *result_future.Get()->get_position(); |
| 255 | EXPECT_DOUBLE_EQ(kMockLatitude, position.latitude); |
| 256 | EXPECT_DOUBLE_EQ(kMockLongitude, position.longitude); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | TEST_F(GeolocationServiceTest, PermissionDeniedAsync) { |
Charlie Hu | e20fe2f | 2021-03-07 03:39:59 | [diff] [blame] | 260 | CreateEmbeddedFrameAndGeolocationService( |
| 261 | /*allow_via_permissions_policy=*/true); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 262 | permission_manager()->SetRequestCallback( |
danakj | 47c8fb5 | 2019-05-02 16:34:36 | [diff] [blame] | 263 | base::BindRepeating([](PermissionCallback permission_callback) { |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 264 | base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
Florian Jacky | aaf4283 | 2025-08-19 04:03:26 | [diff] [blame] | 265 | FROM_HERE, |
| 266 | base::BindOnce(std::move(permission_callback), |
| 267 | std::vector{content::PermissionResult( |
| 268 | PermissionStatus::DENIED, |
| 269 | PermissionStatusSource::UNSPECIFIED)})); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 270 | })); |
Miyoung Shin | 9d893fd | 2019-09-11 14:58:54 | [diff] [blame] | 271 | mojo::Remote<Geolocation> geolocation; |
Mario Sanchez Prada | fa6dda8c | 2019-11-25 18:20:19 | [diff] [blame] | 272 | service_remote()->CreateGeolocation( |
Miyoung Shin | 9d893fd | 2019-09-11 14:58:54 | [diff] [blame] | 273 | geolocation.BindNewPipeAndPassReceiver(), true, |
Jan Wilken Dörrie | 8c74db02 | 2020-04-20 09:05:00 | [diff] [blame] | 274 | base::BindOnce([](blink::mojom::PermissionStatus status) { |
Matt Reynolds | f10fd2f | 2019-04-01 19:39:29 | [diff] [blame] | 275 | EXPECT_EQ(blink::mojom::PermissionStatus::DENIED, status); |
| 276 | })); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 277 | |
Alvin Ji | 9e137ea | 2025-02-20 05:03:02 | [diff] [blame] | 278 | TestFuture<void> disconnect_future; |
| 279 | geolocation.set_disconnect_handler(disconnect_future.GetCallback()); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 280 | |
Matt Reynolds | 5e784e9 | 2023-04-21 01:40:27 | [diff] [blame] | 281 | geolocation->QueryNextPosition( |
| 282 | base::BindOnce([](GeopositionResultPtr result) { |
| 283 | ADD_FAILURE() << "Position updated unexpectedly"; |
| 284 | })); |
Alvin Ji | 9e137ea | 2025-02-20 05:03:02 | [diff] [blame] | 285 | EXPECT_TRUE(disconnect_future.Wait()); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | TEST_F(GeolocationServiceTest, ServiceClosedBeforePermissionResponse) { |
Charlie Hu | e20fe2f | 2021-03-07 03:39:59 | [diff] [blame] | 289 | CreateEmbeddedFrameAndGeolocationService( |
| 290 | /*allow_via_permissions_policy=*/true); |
Miyoung Shin | 9d893fd | 2019-09-11 14:58:54 | [diff] [blame] | 291 | mojo::Remote<Geolocation> geolocation; |
Mario Sanchez Prada | fa6dda8c | 2019-11-25 18:20:19 | [diff] [blame] | 292 | service_remote()->CreateGeolocation( |
Miyoung Shin | 9d893fd | 2019-09-11 14:58:54 | [diff] [blame] | 293 | geolocation.BindNewPipeAndPassReceiver(), true, |
Jan Wilken Dörrie | 8c74db02 | 2020-04-20 09:05:00 | [diff] [blame] | 294 | base::BindOnce([](blink::mojom::PermissionStatus) { |
Matt Reynolds | f10fd2f | 2019-04-01 19:39:29 | [diff] [blame] | 295 | ADD_FAILURE() << "PositionStatus received unexpectedly."; |
| 296 | })); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 297 | // Don't immediately respond to the request. |
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 298 | permission_manager()->SetRequestCallback(base::DoNothing()); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 299 | |
| 300 | base::RunLoop loop; |
Mario Sanchez Prada | fa6dda8c | 2019-11-25 18:20:19 | [diff] [blame] | 301 | service_remote().reset(); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 302 | |
Matt Reynolds | 5e784e9 | 2023-04-21 01:40:27 | [diff] [blame] | 303 | geolocation->QueryNextPosition( |
| 304 | base::BindOnce([](GeopositionResultPtr result) { |
| 305 | ADD_FAILURE() << "Position updated unexpectedly"; |
| 306 | })); |
Raymes Khoury | 5bf2b08 | 2018-01-28 23:32:16 | [diff] [blame] | 307 | loop.RunUntilIdle(); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | } // namespace content |