| OLD | NEW |
| (Empty) |
| 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 DEVICE_GEOLOCATION_NETWORK_LOCATION_REQUEST_H_ | |
| 6 #define 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 "device/geolocation/geolocation_export.h" | |
| 15 #include "device/geolocation/wifi_data_provider.h" | |
| 16 #include "net/url_request/url_fetcher_delegate.h" | |
| 17 #include "url/gurl.h" | |
| 18 | |
| 19 namespace net { | |
| 20 class URLFetcher; | |
| 21 class URLRequestContextGetter; | |
| 22 } | |
| 23 | |
| 24 namespace device { | |
| 25 struct Geoposition; | |
| 26 | |
| 27 // Takes wifi data and sends it to a server to get a position fix. | |
| 28 // It performs formatting of the request and interpretation of the response. | |
| 29 class NetworkLocationRequest : private net::URLFetcherDelegate { | |
| 30 public: | |
| 31 // ID passed to URLFetcher::Create(). Used for testing. | |
| 32 DEVICE_GEOLOCATION_EXPORT static int url_fetcher_id_for_tests; | |
| 33 | |
| 34 // Called when a new geo position is available. The second argument indicates | |
| 35 // whether there was a server error or not. It is true when there was a | |
| 36 // server or network error - either no response or a 500 error code. | |
| 37 typedef base::Callback<void(const Geoposition& /* position */, | |
| 38 bool /* server_error */, | |
| 39 const base::string16& /* access_token */, | |
| 40 const WifiData& /* wifi_data */)> | |
| 41 LocationResponseCallback; | |
| 42 | |
| 43 // |url| is the server address to which the request wil be sent. | |
| 44 NetworkLocationRequest( | |
| 45 const scoped_refptr<net::URLRequestContextGetter>& context, | |
| 46 const GURL& url, | |
| 47 LocationResponseCallback callback); | |
| 48 ~NetworkLocationRequest() override; | |
| 49 | |
| 50 // Makes a new request. Returns true if the new request was successfully | |
| 51 // started. In all cases, any currently pending request will be canceled. | |
| 52 bool MakeRequest(const base::string16& access_token, | |
| 53 const WifiData& wifi_data, | |
| 54 const base::Time& wifi_timestamp); | |
| 55 | |
| 56 bool is_request_pending() const { return url_fetcher_ != NULL; } | |
| 57 const GURL& url() const { return url_; } | |
| 58 | |
| 59 private: | |
| 60 // net::URLFetcherDelegate | |
| 61 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
| 62 | |
| 63 const scoped_refptr<net::URLRequestContextGetter> url_context_; | |
| 64 const LocationResponseCallback location_response_callback_; | |
| 65 const GURL url_; | |
| 66 std::unique_ptr<net::URLFetcher> url_fetcher_; | |
| 67 | |
| 68 // Keep a copy of the data sent in the request, so we can refer back to it | |
| 69 // when the response arrives. | |
| 70 WifiData wifi_data_; | |
| 71 base::Time wifi_timestamp_; | |
| 72 | |
| 73 // The start time for the request. | |
| 74 base::TimeTicks request_start_time_; | |
| 75 | |
| 76 DISALLOW_COPY_AND_ASSIGN(NetworkLocationRequest); | |
| 77 }; | |
| 78 | |
| 79 } // namespace device | |
| 80 | |
| 81 #endif // DEVICE_GEOLOCATION_NETWORK_LOCATION_REQUEST_H_ | |
| OLD | NEW |