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 | |
| 20 const char kDefaultGeolocationProviderUrl[] = | 21 const char kDefaultGeolocationProviderUrl[] = |
| 21 "https://www.googleapis.com/geolocation/v1/geolocate?"; | 22 "https://www.googleapis.com/geolocation/v1/geolocate?"; |
| 22 | 23 |
| 23 std::unique_ptr<WifiAccessPointVector> GetAccessPointData() { | |
| 24 if (!chromeos::NetworkHandler::Get()->geolocation_handler()->wifi_enabled()) | |
| 25 return nullptr; | |
| 26 | |
| 27 std::unique_ptr<WifiAccessPointVector> result( | |
| 28 new chromeos::WifiAccessPointVector); | |
| 29 int64_t age_ms = 0; | |
| 30 if (!NetworkHandler::Get()->geolocation_handler()->GetWifiAccessPoints( | |
| 31 result.get(), &age_ms)) { | |
| 32 return nullptr; | |
| 33 } | |
| 34 return result; | |
| 35 } | |
| 36 | |
| 37 } // namespace | 24 } // namespace |
| 38 | 25 |
| 39 SimpleGeolocationProvider::SimpleGeolocationProvider( | 26 SimpleGeolocationProvider::SimpleGeolocationProvider( |
| 40 net::URLRequestContextGetter* url_context_getter, | 27 net::URLRequestContextGetter* url_context_getter, |
| 41 const GURL& url) | 28 const GURL& url) |
| 42 : url_context_getter_(url_context_getter), url_(url) { | 29 : url_context_getter_(url_context_getter), url_(url) { |
| 43 } | 30 } |
| 44 | 31 |
| 45 SimpleGeolocationProvider::~SimpleGeolocationProvider() { | 32 SimpleGeolocationProvider::~SimpleGeolocationProvider() { |
| 46 DCHECK(thread_checker_.CalledOnValidThread()); | 33 DCHECK(thread_checker_.CalledOnValidThread()); |
| 47 } | 34 } |
| 48 | 35 |
| 49 void SimpleGeolocationProvider::RequestGeolocation( | 36 void SimpleGeolocationProvider::RequestGeolocation( |
| 50 base::TimeDelta timeout, | 37 base::TimeDelta timeout, |
| 51 bool send_wifi_access_points, | 38 bool send_wifi_access_points, |
| 39 bool send_cell_towers, | |
| 52 SimpleGeolocationRequest::ResponseCallback callback) { | 40 SimpleGeolocationRequest::ResponseCallback callback) { |
| 53 DCHECK(thread_checker_.CalledOnValidThread()); | 41 DCHECK(thread_checker_.CalledOnValidThread()); |
| 54 | 42 |
| 43 auto cell_vector = base::MakeUnique<chromeos::CellTowerVector>(); | |
| 44 auto wifi_vector = base::MakeUnique<chromeos::WifiAccessPointVector>(); | |
| 45 | |
| 46 // Mostly necessary for testing and rare cases where NetworkHandler is not | |
| 47 // initialized: in that case, calls to Get() will fail. | |
| 48 if (!send_wifi_access_points && !send_cell_towers) { | |
| 49 wifi_vector = nullptr; | |
| 50 cell_vector = nullptr; | |
|
stevenjb (google-dont-use)
2017/02/09 00:13:28
No need to do this explicitly, just only make the
can Skylar cook
2017/02/09 19:44:30
Ah, clever. Done.
| |
| 51 } else { | |
| 52 NetworkHandler::Get()->geolocation_handler()->GetNetworkInformation( | |
| 53 wifi_vector.get(), cell_vector.get()); | |
| 54 | |
| 55 if (!send_wifi_access_points || (wifi_vector->size() == 0)) | |
| 56 wifi_vector = nullptr; | |
| 57 | |
| 58 if (!send_cell_towers || (cell_vector->size() == 0)) | |
| 59 cell_vector = nullptr; | |
| 60 } | |
| 61 | |
| 55 SimpleGeolocationRequest* request(new SimpleGeolocationRequest( | 62 SimpleGeolocationRequest* request(new SimpleGeolocationRequest( |
| 56 url_context_getter_.get(), url_, timeout, | 63 url_context_getter_.get(), url_, timeout, std::move(wifi_vector), |
| 57 send_wifi_access_points ? GetAccessPointData() : nullptr)); | 64 std::move(cell_vector))); |
| 58 requests_.push_back(base::WrapUnique(request)); | 65 requests_.push_back(base::WrapUnique(request)); |
| 59 | 66 |
| 60 // SimpleGeolocationProvider owns all requests. It is safe to pass unretained | 67 // SimpleGeolocationProvider owns all requests. It is safe to pass unretained |
| 61 // "this" because destruction of SimpleGeolocationProvider cancels all | 68 // "this" because destruction of SimpleGeolocationProvider cancels all |
| 62 // requests. | 69 // requests. |
| 63 SimpleGeolocationRequest::ResponseCallback callback_tmp( | 70 SimpleGeolocationRequest::ResponseCallback callback_tmp( |
| 64 base::Bind(&SimpleGeolocationProvider::OnGeolocationResponse, | 71 base::Bind(&SimpleGeolocationProvider::OnGeolocationResponse, |
| 65 base::Unretained(this), | 72 base::Unretained(this), |
| 66 request, | 73 request, |
| 67 callback)); | 74 callback)); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 90 return req.get() == request; | 97 return req.get() == request; |
| 91 }); | 98 }); |
| 92 DCHECK(position != requests_.end()); | 99 DCHECK(position != requests_.end()); |
| 93 if (position != requests_.end()) { | 100 if (position != requests_.end()) { |
| 94 std::swap(*position, *requests_.rbegin()); | 101 std::swap(*position, *requests_.rbegin()); |
| 95 requests_.resize(requests_.size() - 1); | 102 requests_.resize(requests_.size() - 1); |
| 96 } | 103 } |
| 97 } | 104 } |
| 98 | 105 |
| 99 } // namespace chromeos | 106 } // namespace chromeos |
| OLD | NEW |