| 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 "chromeos/geolocation/geoposition.h" | 11 #include "chromeos/geolocation/geoposition.h" |
| 12 #include "chromeos/network/geolocation_handler.h" |
| 13 #include "chromeos/network/network_handler.h" |
| 12 #include "net/url_request/url_request_context_getter.h" | 14 #include "net/url_request/url_request_context_getter.h" |
| 13 | 15 |
| 14 namespace chromeos { | 16 namespace chromeos { |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 const char kDefaultGeolocationProviderUrl[] = | 19 const char kDefaultGeolocationProviderUrl[] = |
| 18 "https://www.googleapis.com/geolocation/v1/geolocate?"; | 20 "https://www.googleapis.com/geolocation/v1/geolocate?"; |
| 21 |
| 22 scoped_ptr<WifiAccessPointVector> GetAccessPointData() { |
| 23 if (!chromeos::NetworkHandler::Get()->geolocation_handler()->wifi_enabled()) |
| 24 return nullptr; |
| 25 |
| 26 scoped_ptr<WifiAccessPointVector> result(new chromeos::WifiAccessPointVector); |
| 27 int64_t age_ms = 0; |
| 28 if (!NetworkHandler::Get()->geolocation_handler()->GetWifiAccessPoints( |
| 29 result.get(), &age_ms)) { |
| 30 return nullptr; |
| 31 } |
| 32 return result; |
| 33 } |
| 34 |
| 19 } // namespace | 35 } // namespace |
| 20 | 36 |
| 21 SimpleGeolocationProvider::SimpleGeolocationProvider( | 37 SimpleGeolocationProvider::SimpleGeolocationProvider( |
| 22 net::URLRequestContextGetter* url_context_getter, | 38 net::URLRequestContextGetter* url_context_getter, |
| 23 const GURL& url) | 39 const GURL& url) |
| 24 : url_context_getter_(url_context_getter), url_(url) { | 40 : url_context_getter_(url_context_getter), url_(url) { |
| 25 } | 41 } |
| 26 | 42 |
| 27 SimpleGeolocationProvider::~SimpleGeolocationProvider() { | 43 SimpleGeolocationProvider::~SimpleGeolocationProvider() { |
| 28 DCHECK(thread_checker_.CalledOnValidThread()); | 44 DCHECK(thread_checker_.CalledOnValidThread()); |
| 29 } | 45 } |
| 30 | 46 |
| 31 void SimpleGeolocationProvider::RequestGeolocation( | 47 void SimpleGeolocationProvider::RequestGeolocation( |
| 32 base::TimeDelta timeout, | 48 base::TimeDelta timeout, |
| 49 bool send_wifi_access_points, |
| 33 SimpleGeolocationRequest::ResponseCallback callback) { | 50 SimpleGeolocationRequest::ResponseCallback callback) { |
| 34 DCHECK(thread_checker_.CalledOnValidThread()); | 51 DCHECK(thread_checker_.CalledOnValidThread()); |
| 35 SimpleGeolocationRequest* request( | 52 |
| 36 new SimpleGeolocationRequest(url_context_getter_.get(), url_, timeout)); | 53 SimpleGeolocationRequest* request(new SimpleGeolocationRequest( |
| 54 url_context_getter_.get(), url_, timeout, |
| 55 send_wifi_access_points ? GetAccessPointData() : nullptr)); |
| 37 requests_.push_back(request); | 56 requests_.push_back(request); |
| 38 | 57 |
| 39 // SimpleGeolocationProvider owns all requests. It is safe to pass unretained | 58 // SimpleGeolocationProvider owns all requests. It is safe to pass unretained |
| 40 // "this" because destruction of SimpleGeolocationProvider cancels all | 59 // "this" because destruction of SimpleGeolocationProvider cancels all |
| 41 // requests. | 60 // requests. |
| 42 SimpleGeolocationRequest::ResponseCallback callback_tmp( | 61 SimpleGeolocationRequest::ResponseCallback callback_tmp( |
| 43 base::Bind(&SimpleGeolocationProvider::OnGeolocationResponse, | 62 base::Bind(&SimpleGeolocationProvider::OnGeolocationResponse, |
| 44 base::Unretained(this), | 63 base::Unretained(this), |
| 45 request, | 64 request, |
| 46 callback)); | 65 callback)); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 65 ScopedVector<SimpleGeolocationRequest>::iterator position = | 84 ScopedVector<SimpleGeolocationRequest>::iterator position = |
| 66 std::find(requests_.begin(), requests_.end(), request); | 85 std::find(requests_.begin(), requests_.end(), request); |
| 67 DCHECK(position != requests_.end()); | 86 DCHECK(position != requests_.end()); |
| 68 if (position != requests_.end()) { | 87 if (position != requests_.end()) { |
| 69 std::swap(*position, *requests_.rbegin()); | 88 std::swap(*position, *requests_.rbegin()); |
| 70 requests_.resize(requests_.size() - 1); | 89 requests_.resize(requests_.size() - 1); |
| 71 } | 90 } |
| 72 } | 91 } |
| 73 | 92 |
| 74 } // namespace chromeos | 93 } // namespace chromeos |
| OLD | NEW |