Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(695)

Side by Side Diff: chromeos/geolocation/simple_geolocation_provider.cc

Issue 2624843003: Add support for cellular geolocation (Closed)
Patch Set: Update device tests to use correct dict key Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 NetworkHandler::Get()->geolocation_handler()->GetNetworkInformation(
50 wifi_vector.get(), cell_vector.get());
51 }
52
53 if (!send_wifi_access_points || (wifi_vector->size() == 0))
54 wifi_vector = nullptr;
55
56 if (!send_cell_towers || (cell_vector->size() == 0))
57 cell_vector = nullptr;
58
55 SimpleGeolocationRequest* request(new SimpleGeolocationRequest( 59 SimpleGeolocationRequest* request(new SimpleGeolocationRequest(
56 url_context_getter_.get(), url_, timeout, 60 url_context_getter_.get(), url_, timeout, std::move(wifi_vector),
57 send_wifi_access_points ? GetAccessPointData() : nullptr)); 61 std::move(cell_vector)));
58 requests_.push_back(base::WrapUnique(request)); 62 requests_.push_back(base::WrapUnique(request));
59 63
60 // SimpleGeolocationProvider owns all requests. It is safe to pass unretained 64 // SimpleGeolocationProvider owns all requests. It is safe to pass unretained
61 // "this" because destruction of SimpleGeolocationProvider cancels all 65 // "this" because destruction of SimpleGeolocationProvider cancels all
62 // requests. 66 // requests.
63 SimpleGeolocationRequest::ResponseCallback callback_tmp( 67 SimpleGeolocationRequest::ResponseCallback callback_tmp(
64 base::Bind(&SimpleGeolocationProvider::OnGeolocationResponse, 68 base::Bind(&SimpleGeolocationProvider::OnGeolocationResponse,
65 base::Unretained(this), 69 base::Unretained(this),
66 request, 70 request,
67 callback)); 71 callback));
(...skipping 22 matching lines...) Expand all
90 return req.get() == request; 94 return req.get() == request;
91 }); 95 });
92 DCHECK(position != requests_.end()); 96 DCHECK(position != requests_.end());
93 if (position != requests_.end()) { 97 if (position != requests_.end()) {
94 std::swap(*position, *requests_.rbegin()); 98 std::swap(*position, *requests_.rbegin());
95 requests_.resize(requests_.size() - 1); 99 requests_.resize(requests_.size() - 1);
96 } 100 }
97 } 101 }
98 102
99 } // namespace chromeos 103 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/geolocation/simple_geolocation_provider.h ('k') | chromeos/geolocation/simple_geolocation_request.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698