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 | #ifndef CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_SERVICE_IMPL_H_ |
| 6 | #define CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_SERVICE_IMPL_H_ |
| 7 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 8 | #include "base/memory/raw_ptr.h" |
Raymes Khoury | 5bf2b08 | 2018-01-28 23:32:16 | [diff] [blame] | 9 | #include "base/memory/weak_ptr.h" |
Yifan Luo | 8e5d3d5 | 2024-10-22 19:18:16 | [diff] [blame] | 10 | #include "content/browser/web_contents/web_contents_impl.h" |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 11 | #include "content/common/content_export.h" |
Florian Jacky | 80e3530 | 2023-09-15 16:40:42 | [diff] [blame] | 12 | #include "content/public/browser/permission_controller.h" |
Florian Jacky | aaf4283 | 2025-08-19 04:03:26 | [diff] [blame] | 13 | #include "content/public/browser/permission_result.h" |
Mario Sanchez Prada | fa6dda8c | 2019-11-25 18:20:19 | [diff] [blame] | 14 | #include "mojo/public/cpp/bindings/pending_receiver.h" |
| 15 | #include "mojo/public/cpp/bindings/receiver_set.h" |
Ke He | 31d0bb0 | 2018-02-24 07:16:24 | [diff] [blame] | 16 | #include "services/device/public/mojom/geolocation.mojom.h" |
| 17 | #include "services/device/public/mojom/geolocation_context.mojom.h" |
Julie Jeongeun Kim | 162c2d28 | 2019-03-13 07:23:52 | [diff] [blame] | 18 | #include "third_party/blink/public/mojom/geolocation/geolocation_service.mojom.h" |
Florian Jacky | 80e3530 | 2023-09-15 16:40:42 | [diff] [blame] | 19 | #include "url/origin.h" |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 20 | |
| 21 | namespace blink { |
| 22 | namespace mojom { |
| 23 | enum class PermissionStatus; |
| 24 | } |
| 25 | } // namespace blink |
| 26 | |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 27 | namespace content { |
| 28 | class RenderFrameHost; |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 29 | |
| 30 | class GeolocationServiceImplContext { |
| 31 | public: |
Illia Klimov | a181b7d | 2022-03-15 08:17:47 | [diff] [blame] | 32 | GeolocationServiceImplContext(); |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 33 | |
| 34 | GeolocationServiceImplContext(const GeolocationServiceImplContext&) = delete; |
| 35 | GeolocationServiceImplContext& operator=( |
| 36 | const GeolocationServiceImplContext&) = delete; |
| 37 | |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 38 | ~GeolocationServiceImplContext(); |
Florian Jacky | aaf4283 | 2025-08-19 04:03:26 | [diff] [blame] | 39 | using PermissionCallback = base::OnceCallback<void(PermissionResult)>; |
WangHui | 4dafed92 | 2021-03-09 01:02:17 | [diff] [blame] | 40 | void RequestPermission(RenderFrameHost* render_frame_host, |
| 41 | bool user_gesture, |
| 42 | PermissionCallback callback); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 43 | |
| 44 | private: |
Balazs Engedy | e30e961 | 2021-04-02 10:37:29 | [diff] [blame] | 45 | bool has_pending_permission_request_ = false; |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 46 | |
Florian Jacky | aaf4283 | 2025-08-19 04:03:26 | [diff] [blame] | 47 | void HandlePermissionResult(PermissionCallback callback, |
| 48 | PermissionResult permission_result); |
Raymes Khoury | 5bf2b08 | 2018-01-28 23:32:16 | [diff] [blame] | 49 | |
Jeremy Roman | 3bca4bf | 2019-07-11 03:41:25 | [diff] [blame] | 50 | base::WeakPtrFactory<GeolocationServiceImplContext> weak_factory_{this}; |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | class CONTENT_EXPORT GeolocationServiceImpl |
Ke He | 26643642 | 2017-11-02 17:57:50 | [diff] [blame] | 54 | : public blink::mojom::GeolocationService { |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 55 | public: |
Ke He | 7319dbe | 2017-11-09 05:54:44 | [diff] [blame] | 56 | GeolocationServiceImpl(device::mojom::GeolocationContext* geolocation_context, |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 57 | RenderFrameHost* render_frame_host); |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 58 | |
| 59 | GeolocationServiceImpl(const GeolocationServiceImpl&) = delete; |
| 60 | GeolocationServiceImpl& operator=(const GeolocationServiceImpl&) = delete; |
| 61 | |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 62 | ~GeolocationServiceImpl() override; |
| 63 | |
| 64 | // Binds to the GeolocationService. |
Mario Sanchez Prada | fa6dda8c | 2019-11-25 18:20:19 | [diff] [blame] | 65 | void Bind(mojo::PendingReceiver<blink::mojom::GeolocationService> receiver); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 66 | |
| 67 | // Creates a Geolocation instance. |
| 68 | // This may not be called a second time until the Geolocation instance has |
| 69 | // been created. |
Gyuyoung Kim | 0c32116d | 2019-08-30 03:27:35 | [diff] [blame] | 70 | void CreateGeolocation( |
| 71 | mojo::PendingReceiver<device::mojom::Geolocation> receiver, |
| 72 | bool user_gesture, |
| 73 | CreateGeolocationCallback callback) override; |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 74 | |
Florian Jacky | 80e3530 | 2023-09-15 16:40:42 | [diff] [blame] | 75 | void HandlePermissionStatusChange( |
| 76 | blink::mojom::PermissionStatus permission_status); |
| 77 | |
Yifan Luo | 8e5d3d5 | 2024-10-22 19:18:16 | [diff] [blame] | 78 | void OnDisconnected(); |
| 79 | |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 80 | private: |
| 81 | // Creates the Geolocation Service. |
Florian Jacky | aaf4283 | 2025-08-19 04:03:26 | [diff] [blame] | 82 | void CreateGeolocationWithPermissionResult( |
Gyuyoung Kim | 0c32116d | 2019-08-30 03:27:35 | [diff] [blame] | 83 | mojo::PendingReceiver<device::mojom::Geolocation> receiver, |
Matt Reynolds | f10fd2f | 2019-04-01 19:39:29 | [diff] [blame] | 84 | CreateGeolocationCallback callback, |
Florian Jacky | aaf4283 | 2025-08-19 04:03:26 | [diff] [blame] | 85 | PermissionResult permission_result); |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 86 | |
Yifan Luo | 8e5d3d5 | 2024-10-22 19:18:16 | [diff] [blame] | 87 | void IncrementActivityCount(); |
| 88 | void DecrementActivityCount(); |
| 89 | |
Arthur Sonzogni | e98d214 | 2023-06-01 15:02:25 | [diff] [blame] | 90 | raw_ptr<device::mojom::GeolocationContext, DanglingUntriaged> |
| 91 | geolocation_context_; |
Florian Jacky | 80e3530 | 2023-09-15 16:40:42 | [diff] [blame] | 92 | |
| 93 | // Used to subscribe to permission status changes. |
| 94 | PermissionController::SubscriptionId subscription_id_; |
| 95 | |
| 96 | // Tracks the origin for which a granted permission is being observed. Used to |
| 97 | // terminate access upon permission revocation. |
| 98 | url::Origin requesting_origin_; |
| 99 | |
Illia Klimov | a181b7d | 2022-03-15 08:17:47 | [diff] [blame] | 100 | // Note: |render_frame_host_| owns |this| instance. |
Arthur Sonzogni | e98d214 | 2023-06-01 15:02:25 | [diff] [blame] | 101 | const raw_ptr<RenderFrameHost, DanglingUntriaged> render_frame_host_; |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 102 | |
| 103 | // Along with each GeolocationService, we store a |
| 104 | // GeolocationServiceImplContext which primarily exists to manage a |
| 105 | // Permission Request ID. |
Mario Sanchez Prada | fa6dda8c | 2019-11-25 18:20:19 | [diff] [blame] | 106 | mojo::ReceiverSet<blink::mojom::GeolocationService, |
| 107 | std::unique_ptr<GeolocationServiceImplContext>> |
| 108 | receiver_set_; |
Florian Jacky | 80e3530 | 2023-09-15 16:40:42 | [diff] [blame] | 109 | |
Yifan Luo | 8e5d3d5 | 2024-10-22 19:18:16 | [diff] [blame] | 110 | // Whether this service is dispatching geolocation updates to its listeners. |
| 111 | // The creation/destruction of this class is usually the sign of whether |
| 112 | // the frame is updating the geolocation information. However, it can also be |
| 113 | // stopped because the permission status changed. |
| 114 | bool is_sending_updates_ = false; |
| 115 | |
Florian Jacky | 80e3530 | 2023-09-15 16:40:42 | [diff] [blame] | 116 | base::WeakPtrFactory<GeolocationServiceImpl> weak_factory_{this}; |
Conley Owens | 47f4fbf1 | 2017-08-02 01:56:52 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | } // namespace content |
| 120 | |
| 121 | #endif // CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_SERVICE_IMPL_H_ |