1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef SERVICES_DEVICE_GEOLOCATION_NETWORK_LOCATION_REQUEST_H_ 6 #define SERVICES_DEVICE_GEOLOCATION_NETWORK_LOCATION_REQUEST_H_ 7 8 #include <memory> 9 10 #include "base/callback.h" 11 #include "base/macros.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/strings/string16.h" 14 #include "services/device/geolocation/wifi_data_provider.h" 15 #include "services/device/public/mojom/geoposition.mojom.h" 16 #include "url/gurl.h" 17 18 namespace net { 19 struct PartialNetworkTrafficAnnotationTag; 20 } // namespace net 21 22 namespace network { 23 class SharedURLLoaderFactory; 24 class SimpleURLLoader; 25 } // namespace network 26 27 namespace device { 28 29 // Takes wifi data and sends it to a server to get a position fix. 30 // It performs formatting of the request and interpretation of the response. 31 class NetworkLocationRequest { 32 public: 33 // Called when a new geo position is available. The second argument indicates 34 // whether there was a server error or not. It is true when there was a 35 // server or network error - either no response or a 500 error code. 36 using LocationResponseCallback = 37 base::RepeatingCallback<void(const mojom::Geoposition& /* position */, 38 bool /* server_error */, 39 const WifiData& /* wifi_data */)>; 40 41 NetworkLocationRequest( 42 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, 43 const std::string& api_key, 44 LocationResponseCallback callback); 45 ~NetworkLocationRequest(); 46 47 // Makes a new request using the specified |wifi_data|. Returns true if the 48 // new request was successfully started. In all cases, any currently pending 49 // request will be canceled. The specified |wifi_data| and |wifi_timestamp| 50 // are passed back to the client upon completion, via 51 // LocationResponseCallback's |wifi_data| and |position.timestamp| 52 // respectively. 53 bool MakeRequest(const WifiData& wifi_data, 54 const base::Time& wifi_timestamp, 55 const net::PartialNetworkTrafficAnnotationTag& 56 partial_traffic_annotation); 57 is_request_pending()58 bool is_request_pending() const { return bool(url_loader_); } api_key()59 const std::string api_key() const { return api_key_; } 60 61 private: 62 void OnRequestComplete(std::unique_ptr<std::string> data); 63 64 const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; 65 const std::string api_key_; 66 const LocationResponseCallback location_response_callback_; 67 std::unique_ptr<network::SimpleURLLoader> url_loader_; 68 69 // Keep a copy of the data sent in the request, so we can refer back to it 70 // when the response arrives. 71 WifiData wifi_data_; 72 base::Time wifi_timestamp_; 73 74 // The start time for the request. 75 base::TimeTicks request_start_time_; 76 77 DISALLOW_COPY_AND_ASSIGN(NetworkLocationRequest); 78 }; 79 80 } // namespace device 81 82 #endif // SERVICES_DEVICE_GEOLOCATION_NETWORK_LOCATION_REQUEST_H_ 83