#ifndef SERVICES_DEVICE_GEOLOCATION_NETWORK_LOCATION_REQUEST_H_
#define SERVICES_DEVICE_GEOLOCATION_NETWORK_LOCATION_REQUEST_H_
#include <memory>
#include <optional>
#include <string>
#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/time/time.h"
#include "base/values.h"
#include "services/device/geolocation/wifi_data_provider.h"
#include "services/device/public/cpp/geolocation/network_location_request_source.h"
#include "services/device/public/mojom/geolocation_internals.mojom.h"
#include "services/device/public/mojom/geoposition.mojom.h"
namespace net {
struct PartialNetworkTrafficAnnotationTag;
}
namespace network {
class SharedURLLoaderFactory;
class SimpleURLLoader;
}
namespace device {
enum class NetworkLocationRequestResult {
kSuccess = 0,
kCanceled = 1,
kNetworkError = 2,
kResponseNotOk = 3,
kResponseEmpty = 4,
kResponseMalformed = 5,
kInvalidPosition = 6,
kMaxValue = kInvalidPosition,
};
struct LocationResponseResult {
LocationResponseResult(mojom::GeopositionResultPtr position,
NetworkLocationRequestResult result_code,
mojom::NetworkLocationResponsePtr raw_response);
LocationResponseResult(LocationResponseResult&& other);
LocationResponseResult& operator=(LocationResponseResult&& other);
~LocationResponseResult();
mojom::GeopositionResultPtr position;
NetworkLocationRequestResult result_code;
mojom::NetworkLocationResponsePtr raw_response;
};
class NetworkLocationRequest {
public:
using LocationResponseCallback =
base::RepeatingCallback<void(LocationResponseResult ,
const WifiData& )>;
NetworkLocationRequest(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::string& api_key,
LocationResponseCallback callback);
NetworkLocationRequest(const NetworkLocationRequest&) = delete;
NetworkLocationRequest& operator=(const NetworkLocationRequest&) = delete;
~NetworkLocationRequest();
void MakeRequest(
const WifiData& wifi_data,
const base::Time& wifi_timestamp,
const net::PartialNetworkTrafficAnnotationTag& partial_traffic_annotation,
NetworkLocationRequestSource network_location_request_source);
bool is_request_pending() const { return bool(url_loader_); }
std::vector<mojom::AccessPointDataPtr> GetRequestDataForDiagnostics() const;
private:
void OnRequestComplete(std::optional<std::string> data);
const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
const std::string api_key_;
const LocationResponseCallback location_response_callback_;
std::unique_ptr<network::SimpleURLLoader> url_loader_;
WifiData wifi_data_;
base::Value::Dict request_data_;
base::Time wifi_timestamp_;
};
}
#endif