blob: 26111d12582a4bb5125b4b6d331e1bd6f0a38cff [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2017 The Chromium Authors
Conley Owens47f4fbf12017-08-02 01:56:522// 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 Hattori0e45c022021-11-27 09:25:528#include "base/memory/raw_ptr.h"
Raymes Khoury5bf2b082018-01-28 23:32:169#include "base/memory/weak_ptr.h"
Yifan Luo8e5d3d52024-10-22 19:18:1610#include "content/browser/web_contents/web_contents_impl.h"
Conley Owens47f4fbf12017-08-02 01:56:5211#include "content/common/content_export.h"
Florian Jacky80e35302023-09-15 16:40:4212#include "content/public/browser/permission_controller.h"
Florian Jackyaaf42832025-08-19 04:03:2613#include "content/public/browser/permission_result.h"
Mario Sanchez Pradafa6dda8c2019-11-25 18:20:1914#include "mojo/public/cpp/bindings/pending_receiver.h"
15#include "mojo/public/cpp/bindings/receiver_set.h"
Ke He31d0bb02018-02-24 07:16:2416#include "services/device/public/mojom/geolocation.mojom.h"
17#include "services/device/public/mojom/geolocation_context.mojom.h"
Julie Jeongeun Kim162c2d282019-03-13 07:23:5218#include "third_party/blink/public/mojom/geolocation/geolocation_service.mojom.h"
Florian Jacky80e35302023-09-15 16:40:4219#include "url/origin.h"
Conley Owens47f4fbf12017-08-02 01:56:5220
21namespace blink {
22namespace mojom {
23enum class PermissionStatus;
24}
25} // namespace blink
26
Conley Owens47f4fbf12017-08-02 01:56:5227namespace content {
28class RenderFrameHost;
Conley Owens47f4fbf12017-08-02 01:56:5229
30class GeolocationServiceImplContext {
31 public:
Illia Klimova181b7d2022-03-15 08:17:4732 GeolocationServiceImplContext();
Peter Boström828b9022021-09-21 02:28:4333
34 GeolocationServiceImplContext(const GeolocationServiceImplContext&) = delete;
35 GeolocationServiceImplContext& operator=(
36 const GeolocationServiceImplContext&) = delete;
37
Conley Owens47f4fbf12017-08-02 01:56:5238 ~GeolocationServiceImplContext();
Florian Jackyaaf42832025-08-19 04:03:2639 using PermissionCallback = base::OnceCallback<void(PermissionResult)>;
WangHui4dafed922021-03-09 01:02:1740 void RequestPermission(RenderFrameHost* render_frame_host,
41 bool user_gesture,
42 PermissionCallback callback);
Conley Owens47f4fbf12017-08-02 01:56:5243
44 private:
Balazs Engedye30e9612021-04-02 10:37:2945 bool has_pending_permission_request_ = false;
Conley Owens47f4fbf12017-08-02 01:56:5246
Florian Jackyaaf42832025-08-19 04:03:2647 void HandlePermissionResult(PermissionCallback callback,
48 PermissionResult permission_result);
Raymes Khoury5bf2b082018-01-28 23:32:1649
Jeremy Roman3bca4bf2019-07-11 03:41:2550 base::WeakPtrFactory<GeolocationServiceImplContext> weak_factory_{this};
Conley Owens47f4fbf12017-08-02 01:56:5251};
52
53class CONTENT_EXPORT GeolocationServiceImpl
Ke He266436422017-11-02 17:57:5054 : public blink::mojom::GeolocationService {
Conley Owens47f4fbf12017-08-02 01:56:5255 public:
Ke He7319dbe2017-11-09 05:54:4456 GeolocationServiceImpl(device::mojom::GeolocationContext* geolocation_context,
Conley Owens47f4fbf12017-08-02 01:56:5257 RenderFrameHost* render_frame_host);
Peter Boström828b9022021-09-21 02:28:4358
59 GeolocationServiceImpl(const GeolocationServiceImpl&) = delete;
60 GeolocationServiceImpl& operator=(const GeolocationServiceImpl&) = delete;
61
Conley Owens47f4fbf12017-08-02 01:56:5262 ~GeolocationServiceImpl() override;
63
64 // Binds to the GeolocationService.
Mario Sanchez Pradafa6dda8c2019-11-25 18:20:1965 void Bind(mojo::PendingReceiver<blink::mojom::GeolocationService> receiver);
Conley Owens47f4fbf12017-08-02 01:56:5266
67 // Creates a Geolocation instance.
68 // This may not be called a second time until the Geolocation instance has
69 // been created.
Gyuyoung Kim0c32116d2019-08-30 03:27:3570 void CreateGeolocation(
71 mojo::PendingReceiver<device::mojom::Geolocation> receiver,
72 bool user_gesture,
73 CreateGeolocationCallback callback) override;
Conley Owens47f4fbf12017-08-02 01:56:5274
Florian Jacky80e35302023-09-15 16:40:4275 void HandlePermissionStatusChange(
76 blink::mojom::PermissionStatus permission_status);
77
Yifan Luo8e5d3d52024-10-22 19:18:1678 void OnDisconnected();
79
Conley Owens47f4fbf12017-08-02 01:56:5280 private:
81 // Creates the Geolocation Service.
Florian Jackyaaf42832025-08-19 04:03:2682 void CreateGeolocationWithPermissionResult(
Gyuyoung Kim0c32116d2019-08-30 03:27:3583 mojo::PendingReceiver<device::mojom::Geolocation> receiver,
Matt Reynoldsf10fd2f2019-04-01 19:39:2984 CreateGeolocationCallback callback,
Florian Jackyaaf42832025-08-19 04:03:2685 PermissionResult permission_result);
Conley Owens47f4fbf12017-08-02 01:56:5286
Yifan Luo8e5d3d52024-10-22 19:18:1687 void IncrementActivityCount();
88 void DecrementActivityCount();
89
Arthur Sonzognie98d2142023-06-01 15:02:2590 raw_ptr<device::mojom::GeolocationContext, DanglingUntriaged>
91 geolocation_context_;
Florian Jacky80e35302023-09-15 16:40:4292
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 Klimova181b7d2022-03-15 08:17:47100 // Note: |render_frame_host_| owns |this| instance.
Arthur Sonzognie98d2142023-06-01 15:02:25101 const raw_ptr<RenderFrameHost, DanglingUntriaged> render_frame_host_;
Conley Owens47f4fbf12017-08-02 01:56:52102
103 // Along with each GeolocationService, we store a
104 // GeolocationServiceImplContext which primarily exists to manage a
105 // Permission Request ID.
Mario Sanchez Pradafa6dda8c2019-11-25 18:20:19106 mojo::ReceiverSet<blink::mojom::GeolocationService,
107 std::unique_ptr<GeolocationServiceImplContext>>
108 receiver_set_;
Florian Jacky80e35302023-09-15 16:40:42109
Yifan Luo8e5d3d52024-10-22 19:18:16110 // 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 Jacky80e35302023-09-15 16:40:42116 base::WeakPtrFactory<GeolocationServiceImpl> weak_factory_{this};
Conley Owens47f4fbf12017-08-02 01:56:52117};
118
119} // namespace content
120
121#endif // CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_SERVICE_IMPL_H_