Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/geolocation/simple_geolocation_provider.h" | 5 #include "chromeos/geolocation/simple_geolocation_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "chromeos/geolocation/geoposition.h" | 12 #include "chromeos/geolocation/geoposition.h" |
| 13 #include "chromeos/network/geolocation_handler.h" | 13 #include "chromeos/network/geolocation_handler.h" |
| 14 #include "chromeos/network/network_handler.h" | 14 #include "chromeos/network/network_handler.h" |
| 15 #include "net/url_request/url_request_context_getter.h" | 15 #include "net/url_request/url_request_context_getter.h" |
| 16 | 16 |
| 17 namespace chromeos { | 17 namespace chromeos { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | |
| 21 struct WifiCellTowerPair { | |
| 22 std::unique_ptr<WifiAccessPointVector> wifi_vector; | |
| 23 std::unique_ptr<CellTowerVector> cell_vector; | |
| 24 }; | |
| 25 | |
| 20 const char kDefaultGeolocationProviderUrl[] = | 26 const char kDefaultGeolocationProviderUrl[] = |
| 21 "https://www.googleapis.com/geolocation/v1/geolocate?"; | 27 "https://www.googleapis.com/geolocation/v1/geolocate?"; |
| 22 | 28 |
| 23 std::unique_ptr<WifiAccessPointVector> GetAccessPointData() { | 29 WifiCellTowerPair GetNetworkInfo(bool send_wifi_access_points, |
|
stevenjb
2017/02/07 23:54:29
use unique_ptr<WifiCellTowerPair> (also, see note
can Skylar cook
2017/02/08 21:15:49
Done.
| |
| 24 if (!chromeos::NetworkHandler::Get()->geolocation_handler()->wifi_enabled()) | 30 bool send_cell_towers) { |
| 25 return nullptr; | 31 WifiCellTowerPair network_info; |
| 32 network_info.wifi_vector = nullptr; | |
| 33 network_info.cell_vector = nullptr; | |
| 34 // Mostly necessary for testing, or rare cases where NetworkHandler is not | |
| 35 // initialized: in that case, calls to Get() will fail. | |
| 36 if (!send_wifi_access_points && !send_cell_towers) { | |
| 37 return network_info; | |
| 38 } | |
| 26 | 39 |
| 27 std::unique_ptr<WifiAccessPointVector> result( | 40 auto cell_vector = base::MakeUnique<chromeos::CellTowerVector>(); |
| 28 new chromeos::WifiAccessPointVector); | 41 auto wifi_vector = base::MakeUnique<chromeos::WifiAccessPointVector>(); |
| 29 int64_t age_ms = 0; | 42 |
| 30 if (!NetworkHandler::Get()->geolocation_handler()->GetWifiAccessPoints( | 43 NetworkHandler::Get()->geolocation_handler()->GetNetworkInformation( |
| 31 result.get(), &age_ms)) { | 44 wifi_vector.get(), cell_vector.get()); |
| 32 return nullptr; | 45 |
| 33 } | 46 if (send_wifi_access_points && (wifi_vector->size() > 0)) |
| 34 return result; | 47 network_info.wifi_vector = std::move(wifi_vector); |
| 48 | |
| 49 if (send_cell_towers && (cell_vector->size() > 0)) | |
| 50 network_info.cell_vector = std::move(cell_vector); | |
| 51 | |
| 52 return network_info; | |
| 35 } | 53 } |
| 36 | 54 |
| 37 } // namespace | 55 } // namespace |
| 38 | 56 |
| 39 SimpleGeolocationProvider::SimpleGeolocationProvider( | 57 SimpleGeolocationProvider::SimpleGeolocationProvider( |
| 40 net::URLRequestContextGetter* url_context_getter, | 58 net::URLRequestContextGetter* url_context_getter, |
| 41 const GURL& url) | 59 const GURL& url) |
| 42 : url_context_getter_(url_context_getter), url_(url) { | 60 : url_context_getter_(url_context_getter), url_(url) { |
| 43 } | 61 } |
| 44 | 62 |
| 45 SimpleGeolocationProvider::~SimpleGeolocationProvider() { | 63 SimpleGeolocationProvider::~SimpleGeolocationProvider() { |
| 46 DCHECK(thread_checker_.CalledOnValidThread()); | 64 DCHECK(thread_checker_.CalledOnValidThread()); |
| 47 } | 65 } |
| 48 | 66 |
| 49 void SimpleGeolocationProvider::RequestGeolocation( | 67 void SimpleGeolocationProvider::RequestGeolocation( |
| 50 base::TimeDelta timeout, | 68 base::TimeDelta timeout, |
| 51 bool send_wifi_access_points, | 69 bool send_wifi_access_points, |
| 70 bool send_cell_towers, | |
| 52 SimpleGeolocationRequest::ResponseCallback callback) { | 71 SimpleGeolocationRequest::ResponseCallback callback) { |
| 53 DCHECK(thread_checker_.CalledOnValidThread()); | 72 DCHECK(thread_checker_.CalledOnValidThread()); |
| 54 | 73 |
| 55 SimpleGeolocationRequest* request(new SimpleGeolocationRequest( | 74 auto network_info = GetNetworkInfo(send_wifi_access_points, send_cell_towers); |
|
stevenjb
2017/02/07 23:54:29
We tend to avoid this use of auto since it isn't o
can Skylar cook
2017/02/08 21:15:49
Acknowledged.
| |
| 56 url_context_getter_.get(), url_, timeout, | 75 |
| 57 send_wifi_access_points ? GetAccessPointData() : nullptr)); | 76 SimpleGeolocationRequest* request( |
| 77 new SimpleGeolocationRequest(url_context_getter_.get(), url_, timeout, | |
| 78 std::move(network_info.wifi_vector), | |
| 79 std::move(network_info.cell_vector))); | |
|
stevenjb
2017/02/07 23:54:29
It would be more clear to make WifiCellTowerPair a
can Skylar cook
2017/02/08 21:15:49
Done.
| |
| 58 requests_.push_back(base::WrapUnique(request)); | 80 requests_.push_back(base::WrapUnique(request)); |
| 59 | 81 |
| 60 // SimpleGeolocationProvider owns all requests. It is safe to pass unretained | 82 // SimpleGeolocationProvider owns all requests. It is safe to pass unretained |
| 61 // "this" because destruction of SimpleGeolocationProvider cancels all | 83 // "this" because destruction of SimpleGeolocationProvider cancels all |
| 62 // requests. | 84 // requests. |
| 63 SimpleGeolocationRequest::ResponseCallback callback_tmp( | 85 SimpleGeolocationRequest::ResponseCallback callback_tmp( |
| 64 base::Bind(&SimpleGeolocationProvider::OnGeolocationResponse, | 86 base::Bind(&SimpleGeolocationProvider::OnGeolocationResponse, |
| 65 base::Unretained(this), | 87 base::Unretained(this), |
| 66 request, | 88 request, |
| 67 callback)); | 89 callback)); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 90 return req.get() == request; | 112 return req.get() == request; |
| 91 }); | 113 }); |
| 92 DCHECK(position != requests_.end()); | 114 DCHECK(position != requests_.end()); |
| 93 if (position != requests_.end()) { | 115 if (position != requests_.end()) { |
| 94 std::swap(*position, *requests_.rbegin()); | 116 std::swap(*position, *requests_.rbegin()); |
| 95 requests_.resize(requests_.size() - 1); | 117 requests_.resize(requests_.size() - 1); |
| 96 } | 118 } |
| 97 } | 119 } |
| 98 | 120 |
| 99 } // namespace chromeos | 121 } // namespace chromeos |
| OLD | NEW |