File: geolocation_service_impl.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (121 lines) | stat: -rw-r--r-- 4,382 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_SERVICE_IMPL_H_
#define CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_SERVICE_IMPL_H_

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/content_export.h"
#include "content/public/browser/permission_controller.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "services/device/public/mojom/geolocation.mojom.h"
#include "services/device/public/mojom/geolocation_context.mojom.h"
#include "third_party/blink/public/mojom/geolocation/geolocation_service.mojom.h"
#include "url/origin.h"

namespace blink {
namespace mojom {
enum class PermissionStatus;
}
}  // namespace blink

namespace content {
class RenderFrameHost;

class GeolocationServiceImplContext {
 public:
  GeolocationServiceImplContext();

  GeolocationServiceImplContext(const GeolocationServiceImplContext&) = delete;
  GeolocationServiceImplContext& operator=(
      const GeolocationServiceImplContext&) = delete;

  ~GeolocationServiceImplContext();
  using PermissionCallback =
      base::OnceCallback<void(blink::mojom::PermissionStatus)>;
  void RequestPermission(RenderFrameHost* render_frame_host,
                         bool user_gesture,
                         PermissionCallback callback);

 private:
  bool has_pending_permission_request_ = false;

  void HandlePermissionStatus(PermissionCallback callback,
                              blink::mojom::PermissionStatus permission_status);

  base::WeakPtrFactory<GeolocationServiceImplContext> weak_factory_{this};
};

class CONTENT_EXPORT GeolocationServiceImpl
    : public blink::mojom::GeolocationService {
 public:
  GeolocationServiceImpl(device::mojom::GeolocationContext* geolocation_context,
                         RenderFrameHost* render_frame_host);

  GeolocationServiceImpl(const GeolocationServiceImpl&) = delete;
  GeolocationServiceImpl& operator=(const GeolocationServiceImpl&) = delete;

  ~GeolocationServiceImpl() override;

  // Binds to the GeolocationService.
  void Bind(mojo::PendingReceiver<blink::mojom::GeolocationService> receiver);

  // Creates a Geolocation instance.
  // This may not be called a second time until the Geolocation instance has
  // been created.
  void CreateGeolocation(
      mojo::PendingReceiver<device::mojom::Geolocation> receiver,
      bool user_gesture,
      CreateGeolocationCallback callback) override;

  void HandlePermissionStatusChange(
      blink::mojom::PermissionStatus permission_status);

  void OnDisconnected();

 private:
  // Creates the Geolocation Service.
  void CreateGeolocationWithPermissionStatus(
      mojo::PendingReceiver<device::mojom::Geolocation> receiver,
      CreateGeolocationCallback callback,
      blink::mojom::PermissionStatus permission_status);

  void IncrementActivityCount();
  void DecrementActivityCount();

  raw_ptr<device::mojom::GeolocationContext, DanglingUntriaged>
      geolocation_context_;

  // Used to subscribe to permission status changes.
  PermissionController::SubscriptionId subscription_id_;

  // Tracks the origin for which a granted permission is being observed. Used to
  // terminate access upon permission revocation.
  url::Origin requesting_origin_;

  // Note: |render_frame_host_| owns |this| instance.
  const raw_ptr<RenderFrameHost, DanglingUntriaged> render_frame_host_;

  // Along with each GeolocationService, we store a
  // GeolocationServiceImplContext which primarily exists to manage a
  // Permission Request ID.
  mojo::ReceiverSet<blink::mojom::GeolocationService,
                    std::unique_ptr<GeolocationServiceImplContext>>
      receiver_set_;

  // Whether this service is dispatching geolocation updates to its listeners.
  // The creation/destruction of this class is usually the sign of whether
  // the frame is updating the geolocation information. However, it can also be
  // stopped because the permission status changed.
  bool is_sending_updates_ = false;

  base::WeakPtrFactory<GeolocationServiceImpl> weak_factory_{this};
};

}  // namespace content

#endif  // CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_SERVICE_IMPL_H_