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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/geolocation/simple_geolocation_provider.h"
#include <algorithm>
#include <iterator>
#include <memory>
#include "ash/constants/geolocation_access_level.h"
#include "base/check_is_test.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/time/time.h"
#include "chromeos/ash/components/geolocation/geoposition.h"
#include "chromeos/ash/components/geolocation/simple_geolocation_request.h"
#include "chromeos/ash/components/network/geolocation_handler.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace ash {
namespace {
SimpleGeolocationProvider* g_geolocation_provider = nullptr;
// Maximum interval (in hours) to record in the request interval histogram.
// We track intervals within a day to identify clients that excessively request
// geolocation updates (e.g., hourly). Longer intervals are considered normal.
constexpr static int kMaxRequestIntervalHistogramHours = 24;
std::string_view GetClientIdUmaName(
SimpleGeolocationProvider::ClientId client_id) {
switch (client_id) {
case SimpleGeolocationProvider::ClientId::kGeolocationController:
return "SimpleGeolocation.Provider.GeolocationControllerRequestInterval";
case SimpleGeolocationProvider::ClientId::kWizardController:
return "SimpleGeolocation.Provider.WizardControllerRequestInterval";
case SimpleGeolocationProvider::ClientId::kTimezoneResolver:
return "SimpleGeolocation.Provider.TimezoneResolverRequestInterval";
case SimpleGeolocationProvider::ClientId::kForTesting:
// This case is unused but required to avoid a compiler warning about
// missing 'default' case in the switch.
break;
}
NOTREACHED() << "GetClientIdUmaName: An invalid client_id is passed";
}
} // namespace
SimpleGeolocationProvider::SimpleGeolocationProvider(
scoped_refptr<network::SharedURLLoaderFactory> factory)
: shared_url_loader_factory_(factory) {}
SimpleGeolocationProvider::~SimpleGeolocationProvider() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
}
// static
void SimpleGeolocationProvider::Initialize(
scoped_refptr<network::SharedURLLoaderFactory> factory) {
CHECK_EQ(g_geolocation_provider, nullptr);
g_geolocation_provider = new SimpleGeolocationProvider(factory);
}
// static
SimpleGeolocationProvider* SimpleGeolocationProvider::GetInstance() {
CHECK_NE(g_geolocation_provider, nullptr);
return g_geolocation_provider;
}
GeolocationAccessLevel SimpleGeolocationProvider::GetGeolocationAccessLevel()
const {
return geolocation_access_level_;
}
void SimpleGeolocationProvider::SetGeolocationAccessLevel(
GeolocationAccessLevel geolocation_access_level) {
bool system_geo_usage_allowed = IsGeolocationUsageAllowedForSystem();
geolocation_access_level_ = geolocation_access_level;
if (system_geo_usage_allowed != IsGeolocationUsageAllowedForSystem()) {
NotifyObservers();
}
}
void SimpleGeolocationProvider::AddObserver(Observer* obs) {
CHECK(obs);
CHECK(!observer_list_.HasObserver(obs));
observer_list_.AddObserver(obs);
}
void SimpleGeolocationProvider::RemoveObserver(Observer* obs) {
CHECK(obs);
CHECK(observer_list_.HasObserver(obs));
observer_list_.RemoveObserver(obs);
}
void SimpleGeolocationProvider::RequestGeolocation(
base::TimeDelta timeout,
bool send_wifi_access_points,
bool send_cell_towers,
SimpleGeolocationRequest::ResponseCallback callback,
ClientId client_id) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
RecordClientIdUma(client_id);
// Drop request if the system geolocation permission is not granted for
// system services.
if (!IsGeolocationUsageAllowedForSystem()) {
return;
}
// System permission is granted:
auto cell_vector = std::make_unique<CellTowerVector>();
auto wifi_vector = std::make_unique<WifiAccessPointVector>();
if (send_wifi_access_points || send_cell_towers) {
// Mostly necessary for testing and rare cases where NetworkHandler is not
// initialized: in that case, calls to Get() will fail.
GeolocationHandler* geolocation_handler = geolocation_handler_;
if (!geolocation_handler)
geolocation_handler = NetworkHandler::Get()->geolocation_handler();
geolocation_handler->GetNetworkInformation(wifi_vector.get(),
cell_vector.get());
}
if (!send_wifi_access_points || (wifi_vector->size() == 0))
wifi_vector = nullptr;
if (!send_cell_towers || (cell_vector->size() == 0))
cell_vector = nullptr;
SimpleGeolocationRequest* request(new SimpleGeolocationRequest(
shared_url_loader_factory_, GURL(GetGeolocationProviderUrl()), timeout,
std::move(wifi_vector), std::move(cell_vector)));
requests_.push_back(base::WrapUnique(request));
// SimpleGeolocationProvider owns all requests. It is safe to pass unretained
// "this" because destruction of SimpleGeolocationProvider cancels all
// requests.
SimpleGeolocationRequest::ResponseCallback callback_tmp(
base::BindOnce(&SimpleGeolocationProvider::OnGeolocationResponse,
base::Unretained(this), request, std::move(callback)));
request->MakeRequest(std::move(callback_tmp));
}
// static
void SimpleGeolocationProvider::DestroyForTesting() {
CHECK_IS_TEST();
CHECK_NE(g_geolocation_provider, nullptr);
delete g_geolocation_provider;
g_geolocation_provider = nullptr;
}
void SimpleGeolocationProvider::SetSharedUrlLoaderFactoryForTesting(
scoped_refptr<network::SharedURLLoaderFactory> factory) {
CHECK_IS_TEST();
shared_url_loader_factory_ = factory;
}
void SimpleGeolocationProvider::SetGeolocationProviderUrlForTesting(
const char* url) {
CHECK_IS_TEST();
url_for_testing_ = url;
}
bool SimpleGeolocationProvider::IsGeolocationUsageAllowedForSystem() {
switch (geolocation_access_level_) {
case GeolocationAccessLevel::kAllowed:
case GeolocationAccessLevel::kOnlyAllowedForSystem:
return true;
case GeolocationAccessLevel::kDisallowed:
return false;
}
}
void SimpleGeolocationProvider::OnGeolocationResponse(
SimpleGeolocationRequest* request,
SimpleGeolocationRequest::ResponseCallback callback,
const Geoposition& geoposition,
bool server_error,
const base::TimeDelta elapsed) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
std::move(callback).Run(geoposition, server_error, elapsed);
std::vector<std::unique_ptr<SimpleGeolocationRequest>>::iterator position =
std::ranges::find(requests_, request,
&std::unique_ptr<SimpleGeolocationRequest>::get);
DCHECK(position != requests_.end());
if (position != requests_.end()) {
std::swap(*position, *requests_.rbegin());
requests_.resize(requests_.size() - 1);
}
}
std::string SimpleGeolocationProvider::GetGeolocationProviderUrl() const {
// URL provider is overridden in tests.
if (!url_for_testing_.empty()) {
CHECK_IS_TEST();
return url_for_testing_;
}
return kGeolocationProviderUrl;
}
void SimpleGeolocationProvider::NotifyObservers() {
for (auto& obs : observer_list_) {
obs.OnGeolocationPermissionChanged(IsGeolocationUsageAllowedForSystem());
}
}
void SimpleGeolocationProvider::RecordClientIdUma(ClientId client_id) {
if (client_id == ClientId::kForTesting) {
// Requests from tests are not relevant for metrics and can be skipped
return;
}
base::UmaHistogramEnumeration("SimpleGeolocation.Provider.ClientId",
client_id);
base::TimeTicks now = base::TimeTicks::Now();
auto it = last_request_times_.find(client_id);
if (it != last_request_times_.end()) {
base::UmaHistogramExactLinear(GetClientIdUmaName(client_id),
(now - it->second).InHours(),
kMaxRequestIntervalHistogramHours);
}
last_request_times_[client_id] = now;
}
} // namespace ash
|