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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
content / browser / geolocation / geolocation_service_impl.cc [blame]
// 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.
#include "content/browser/geolocation/geolocation_service_impl.h"
#include <utility>
#include "base/functional/bind.h"
#include "content/browser/permissions/permission_controller_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/permission_controller.h"
#include "content/public/browser/permission_request_description.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "mojo/public/cpp/bindings/callback_helpers.h"
#include "services/device/public/mojom/geoposition.mojom.h"
#include "third_party/blink/public/common/permissions/permission_utils.h"
#include "third_party/blink/public/mojom/permissions_policy/permissions_policy.mojom.h"
#if BUILDFLAG(IS_IOS)
#include "services/device/public/cpp/geolocation/geolocation_system_permission_manager.h"
#endif
namespace content {
GeolocationServiceImplContext::GeolocationServiceImplContext() = default;
GeolocationServiceImplContext::~GeolocationServiceImplContext() = default;
void GeolocationServiceImplContext::RequestPermission(
RenderFrameHost* render_frame_host,
bool user_gesture,
PermissionCallback callback) {
if (has_pending_permission_request_) {
mojo::ReportBadMessage(
"GeolocationService client may only create one Geolocation at a "
"time.");
return;
}
has_pending_permission_request_ = true;
render_frame_host->GetBrowserContext()
->GetPermissionController()
->RequestPermissionFromCurrentDocument(
render_frame_host,
PermissionRequestDescription(blink::PermissionType::GEOLOCATION,
user_gesture),
base::BindOnce(&GeolocationServiceImplContext::HandlePermissionStatus,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
void GeolocationServiceImplContext::HandlePermissionStatus(
PermissionCallback callback,
blink::mojom::PermissionStatus permission_status) {
has_pending_permission_request_ = false;
std::move(callback).Run(permission_status);
}
GeolocationServiceImpl::GeolocationServiceImpl(
device::mojom::GeolocationContext* geolocation_context,
RenderFrameHost* render_frame_host)
: geolocation_context_(geolocation_context),
render_frame_host_(render_frame_host) {
DCHECK(geolocation_context);
DCHECK(render_frame_host);
}
GeolocationServiceImpl::~GeolocationServiceImpl() = default;
void GeolocationServiceImpl::Bind(
mojo::PendingReceiver<blink::mojom::GeolocationService> receiver) {
receiver_set_.Add(this, std::move(receiver),
std::make_unique<GeolocationServiceImplContext>());
#if BUILDFLAG(IS_IOS)
device::GeolocationSystemPermissionManager*
geolocation_system_permission_manager =
device::GeolocationSystemPermissionManager::GetInstance();
if (geolocation_system_permission_manager) {
geolocation_system_permission_manager->RequestSystemPermission();
}
#endif
}
void GeolocationServiceImpl::CreateGeolocation(
mojo::PendingReceiver<device::mojom::Geolocation> receiver,
bool user_gesture,
CreateGeolocationCallback callback) {
if (!render_frame_host_->IsFeatureEnabled(
blink::mojom::PermissionsPolicyFeature::kGeolocation)) {
std::move(callback).Run(blink::mojom::PermissionStatus::DENIED);
return;
}
// If the geolocation service is destroyed before the callback is run, ensure
// it is called with DENIED status.
auto scoped_callback = mojo::WrapCallbackWithDefaultInvokeIfNotRun(
std::move(callback), blink::mojom::PermissionStatus::DENIED);
receiver_set_.current_context()->RequestPermission(
render_frame_host_, user_gesture,
// There is an assumption here that the GeolocationServiceImplContext will
// outlive the GeolocationServiceImpl.
base::BindOnce(
&GeolocationServiceImpl::CreateGeolocationWithPermissionStatus,
base::Unretained(this), std::move(receiver),
std::move(scoped_callback)));
}
void GeolocationServiceImpl::CreateGeolocationWithPermissionStatus(
mojo::PendingReceiver<device::mojom::Geolocation> receiver,
CreateGeolocationCallback callback,
blink::mojom::PermissionStatus permission_status) {
std::move(callback).Run(permission_status);
if (permission_status != blink::mojom::PermissionStatus::GRANTED)
return;
requesting_origin_ =
render_frame_host_->GetMainFrame()->GetLastCommittedOrigin();
auto requesting_url =
render_frame_host_->GetMainFrame()->GetLastCommittedURL();
geolocation_context_->BindGeolocation(
std::move(receiver), requesting_url,
device::mojom::GeolocationClientId::kGeolocationServiceImpl);
subscription_id_ =
PermissionControllerImpl::FromBrowserContext(
render_frame_host_->GetBrowserContext())
->SubscribeToPermissionStatusChange(
blink::PermissionType::GEOLOCATION,
/*render_process_host=*/nullptr, render_frame_host_,
requesting_url,
/*should_include_device_status=*/false,
base::BindRepeating(
&GeolocationServiceImpl::HandlePermissionStatusChange,
weak_factory_.GetWeakPtr()));
}
void GeolocationServiceImpl::HandlePermissionStatusChange(
blink::mojom::PermissionStatus permission_status) {
if (permission_status != blink::mojom::PermissionStatus::GRANTED &&
subscription_id_.value()) {
PermissionControllerImpl::FromBrowserContext(
render_frame_host_->GetBrowserContext())
->UnsubscribeFromPermissionStatusChange(subscription_id_);
geolocation_context_->OnPermissionRevoked(requesting_origin_);
}
}
} // namespace content