simple_geolocation_provider.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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
  3. // found in the LICENSE file.
  4. #include "ash/components/geolocation/simple_geolocation_provider.h"
  5. #include <algorithm>
  6. #include <iterator>
  7. #include <memory>
  8. #include "ash/components/geolocation/geoposition.h"
  9. #include "base/bind.h"
  10. #include "base/memory/ptr_util.h"
  11. #include "chromeos/ash/components/network/geolocation_handler.h"
  12. #include "chromeos/ash/components/network/network_handler.h"
  13. #include "services/network/public/cpp/shared_url_loader_factory.h"
  14. namespace ash {
  15. namespace {
  16. const char kDefaultGeolocationProviderUrl[] =
  17. "https://www.googleapis.com/geolocation/v1/geolocate?";
  18. } // namespace
  19. SimpleGeolocationProvider::SimpleGeolocationProvider(
  20. scoped_refptr<network::SharedURLLoaderFactory> factory,
  21. const GURL& url)
  22. : shared_url_loader_factory_(std::move(factory)), url_(url) {}
  23. SimpleGeolocationProvider::~SimpleGeolocationProvider() {
  24. DCHECK(thread_checker_.CalledOnValidThread());
  25. }
  26. void SimpleGeolocationProvider::RequestGeolocation(
  27. base::TimeDelta timeout,
  28. bool send_wifi_access_points,
  29. bool send_cell_towers,
  30. SimpleGeolocationRequest::ResponseCallback callback) {
  31. DCHECK(thread_checker_.CalledOnValidThread());
  32. auto cell_vector = std::make_unique<CellTowerVector>();
  33. auto wifi_vector = std::make_unique<WifiAccessPointVector>();
  34. // Mostly necessary for testing and 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. GeolocationHandler* geolocation_handler = geolocation_handler_;
  38. if (!geolocation_handler)
  39. geolocation_handler = NetworkHandler::Get()->geolocation_handler();
  40. geolocation_handler->GetNetworkInformation(wifi_vector.get(),
  41. cell_vector.get());
  42. }
  43. if (!send_wifi_access_points || (wifi_vector->size() == 0))
  44. wifi_vector = nullptr;
  45. if (!send_cell_towers || (cell_vector->size() == 0))
  46. cell_vector = nullptr;
  47. SimpleGeolocationRequest* request(new SimpleGeolocationRequest(
  48. shared_url_loader_factory_, url_, timeout, std::move(wifi_vector),
  49. std::move(cell_vector)));
  50. requests_.push_back(base::WrapUnique(request));
  51. // SimpleGeolocationProvider owns all requests. It is safe to pass unretained
  52. // "this" because destruction of SimpleGeolocationProvider cancels all
  53. // requests.
  54. SimpleGeolocationRequest::ResponseCallback callback_tmp(
  55. base::BindOnce(&SimpleGeolocationProvider::OnGeolocationResponse,
  56. base::Unretained(this), request, std::move(callback)));
  57. request->MakeRequest(std::move(callback_tmp));
  58. }
  59. // static
  60. GURL SimpleGeolocationProvider::DefaultGeolocationProviderURL() {
  61. return GURL(kDefaultGeolocationProviderUrl);
  62. }
  63. void SimpleGeolocationProvider::OnGeolocationResponse(
  64. SimpleGeolocationRequest* request,
  65. SimpleGeolocationRequest::ResponseCallback callback,
  66. const Geoposition& geoposition,
  67. bool server_error,
  68. const base::TimeDelta elapsed) {
  69. DCHECK(thread_checker_.CalledOnValidThread());
  70. std::move(callback).Run(geoposition, server_error, elapsed);
  71. std::vector<std::unique_ptr<SimpleGeolocationRequest>>::iterator position =
  72. std::find_if(
  73. requests_.begin(), requests_.end(),
  74. [request](const std::unique_ptr<SimpleGeolocationRequest>& req) {
  75. return req.get() == request;
  76. });
  77. DCHECK(position != requests_.end());
  78. if (position != requests_.end()) {
  79. std::swap(*position, *requests_.rbegin());
  80. requests_.resize(requests_.size() - 1);
  81. }
  82. }
  83. } // namespace ash