| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/geolocation/network_location_request.h" | 5 #include "content/browser/geolocation/network_location_request.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 UMA_HISTOGRAM_CUSTOM_COUNTS("Geolocation.NetworkLocationRequest.AccessPoints", | 65 UMA_HISTOGRAM_CUSTOM_COUNTS("Geolocation.NetworkLocationRequest.AccessPoints", |
| 66 count, min, max, buckets); | 66 count, min, max, buckets); |
| 67 } | 67 } |
| 68 | 68 |
| 69 // Local functions | 69 // Local functions |
| 70 // Creates the request url to send to the server. | 70 // Creates the request url to send to the server. |
| 71 GURL FormRequestURL(const GURL& url); | 71 GURL FormRequestURL(const GURL& url); |
| 72 | 72 |
| 73 void FormUploadData(const WifiData& wifi_data, | 73 void FormUploadData(const WifiData& wifi_data, |
| 74 const base::Time& timestamp, | 74 const base::Time& timestamp, |
| 75 const string16& access_token, | 75 const base::string16& access_token, |
| 76 std::string* upload_data); | 76 std::string* upload_data); |
| 77 | 77 |
| 78 // Attempts to extract a position from the response. Detects and indicates | 78 // Attempts to extract a position from the response. Detects and indicates |
| 79 // various failure cases. | 79 // various failure cases. |
| 80 void GetLocationFromResponse(bool http_post_result, | 80 void GetLocationFromResponse(bool http_post_result, |
| 81 int status_code, | 81 int status_code, |
| 82 const std::string& response_body, | 82 const std::string& response_body, |
| 83 const base::Time& timestamp, | 83 const base::Time& timestamp, |
| 84 const GURL& server_url, | 84 const GURL& server_url, |
| 85 Geoposition* position, | 85 Geoposition* position, |
| 86 string16* access_token); | 86 base::string16* access_token); |
| 87 | 87 |
| 88 // Parses the server response body. Returns true if parsing was successful. | 88 // Parses the server response body. Returns true if parsing was successful. |
| 89 // Sets |*position| to the parsed location if a valid fix was received, | 89 // Sets |*position| to the parsed location if a valid fix was received, |
| 90 // otherwise leaves it unchanged. | 90 // otherwise leaves it unchanged. |
| 91 bool ParseServerResponse(const std::string& response_body, | 91 bool ParseServerResponse(const std::string& response_body, |
| 92 const base::Time& timestamp, | 92 const base::Time& timestamp, |
| 93 Geoposition* position, | 93 Geoposition* position, |
| 94 string16* access_token); | 94 base::string16* access_token); |
| 95 void AddWifiData(const WifiData& wifi_data, | 95 void AddWifiData(const WifiData& wifi_data, |
| 96 int age_milliseconds, | 96 int age_milliseconds, |
| 97 base::DictionaryValue* request); | 97 base::DictionaryValue* request); |
| 98 } // namespace | 98 } // namespace |
| 99 | 99 |
| 100 int NetworkLocationRequest::url_fetcher_id_for_tests = 0; | 100 int NetworkLocationRequest::url_fetcher_id_for_tests = 0; |
| 101 | 101 |
| 102 NetworkLocationRequest::NetworkLocationRequest( | 102 NetworkLocationRequest::NetworkLocationRequest( |
| 103 net::URLRequestContextGetter* context, | 103 net::URLRequestContextGetter* context, |
| 104 const GURL& url, | 104 const GURL& url, |
| 105 LocationResponseCallback callback) | 105 LocationResponseCallback callback) |
| 106 : url_context_(context), | 106 : url_context_(context), |
| 107 callback_(callback), | 107 callback_(callback), |
| 108 url_(url) { | 108 url_(url) { |
| 109 } | 109 } |
| 110 | 110 |
| 111 NetworkLocationRequest::~NetworkLocationRequest() { | 111 NetworkLocationRequest::~NetworkLocationRequest() { |
| 112 } | 112 } |
| 113 | 113 |
| 114 bool NetworkLocationRequest::MakeRequest(const string16& access_token, | 114 bool NetworkLocationRequest::MakeRequest(const base::string16& access_token, |
| 115 const WifiData& wifi_data, | 115 const WifiData& wifi_data, |
| 116 const base::Time& timestamp) { | 116 const base::Time& timestamp) { |
| 117 RecordUmaEvent(NETWORK_LOCATION_REQUEST_EVENT_REQUEST_START); | 117 RecordUmaEvent(NETWORK_LOCATION_REQUEST_EVENT_REQUEST_START); |
| 118 RecordUmaAccessPoints(wifi_data.access_point_data.size()); | 118 RecordUmaAccessPoints(wifi_data.access_point_data.size()); |
| 119 if (url_fetcher_ != NULL) { | 119 if (url_fetcher_ != NULL) { |
| 120 DVLOG(1) << "NetworkLocationRequest : Cancelling pending request"; | 120 DVLOG(1) << "NetworkLocationRequest : Cancelling pending request"; |
| 121 RecordUmaEvent(NETWORK_LOCATION_REQUEST_EVENT_REQUEST_CANCEL); | 121 RecordUmaEvent(NETWORK_LOCATION_REQUEST_EVENT_REQUEST_CANCEL); |
| 122 url_fetcher_.reset(); | 122 url_fetcher_.reset(); |
| 123 } | 123 } |
| 124 wifi_data_ = wifi_data; | 124 wifi_data_ = wifi_data; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 143 | 143 |
| 144 void NetworkLocationRequest::OnURLFetchComplete( | 144 void NetworkLocationRequest::OnURLFetchComplete( |
| 145 const net::URLFetcher* source) { | 145 const net::URLFetcher* source) { |
| 146 DCHECK_EQ(url_fetcher_.get(), source); | 146 DCHECK_EQ(url_fetcher_.get(), source); |
| 147 | 147 |
| 148 net::URLRequestStatus status = source->GetStatus(); | 148 net::URLRequestStatus status = source->GetStatus(); |
| 149 int response_code = source->GetResponseCode(); | 149 int response_code = source->GetResponseCode(); |
| 150 RecordUmaResponseCode(response_code); | 150 RecordUmaResponseCode(response_code); |
| 151 | 151 |
| 152 Geoposition position; | 152 Geoposition position; |
| 153 string16 access_token; | 153 base::string16 access_token; |
| 154 std::string data; | 154 std::string data; |
| 155 source->GetResponseAsString(&data); | 155 source->GetResponseAsString(&data); |
| 156 GetLocationFromResponse(status.is_success(), | 156 GetLocationFromResponse(status.is_success(), |
| 157 response_code, | 157 response_code, |
| 158 data, | 158 data, |
| 159 timestamp_, | 159 timestamp_, |
| 160 source->GetURL(), | 160 source->GetURL(), |
| 161 &position, | 161 &position, |
| 162 &access_token); | 162 &access_token); |
| 163 const bool server_error = | 163 const bool server_error = |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 GURL::Replacements replacements; | 200 GURL::Replacements replacements; |
| 201 replacements.SetQueryStr(query); | 201 replacements.SetQueryStr(query); |
| 202 return url.ReplaceComponents(replacements); | 202 return url.ReplaceComponents(replacements); |
| 203 } | 203 } |
| 204 } | 204 } |
| 205 return url; | 205 return url; |
| 206 } | 206 } |
| 207 | 207 |
| 208 void FormUploadData(const WifiData& wifi_data, | 208 void FormUploadData(const WifiData& wifi_data, |
| 209 const base::Time& timestamp, | 209 const base::Time& timestamp, |
| 210 const string16& access_token, | 210 const base::string16& access_token, |
| 211 std::string* upload_data) { | 211 std::string* upload_data) { |
| 212 int age = kint32min; // Invalid so AddInteger() will ignore. | 212 int age = kint32min; // Invalid so AddInteger() will ignore. |
| 213 if (!timestamp.is_null()) { | 213 if (!timestamp.is_null()) { |
| 214 // Convert absolute timestamps into a relative age. | 214 // Convert absolute timestamps into a relative age. |
| 215 int64 delta_ms = (base::Time::Now() - timestamp).InMilliseconds(); | 215 int64 delta_ms = (base::Time::Now() - timestamp).InMilliseconds(); |
| 216 if (delta_ms >= 0 && delta_ms < kint32max) | 216 if (delta_ms >= 0 && delta_ms < kint32max) |
| 217 age = static_cast<int>(delta_ms); | 217 age = static_cast<int>(delta_ms); |
| 218 } | 218 } |
| 219 | 219 |
| 220 base::DictionaryValue request; | 220 base::DictionaryValue request; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 VLOG(1) << "NetworkLocationRequest::GetLocationFromResponse() : " | 284 VLOG(1) << "NetworkLocationRequest::GetLocationFromResponse() : " |
| 285 << position->error_message; | 285 << position->error_message; |
| 286 } | 286 } |
| 287 | 287 |
| 288 void GetLocationFromResponse(bool http_post_result, | 288 void GetLocationFromResponse(bool http_post_result, |
| 289 int status_code, | 289 int status_code, |
| 290 const std::string& response_body, | 290 const std::string& response_body, |
| 291 const base::Time& timestamp, | 291 const base::Time& timestamp, |
| 292 const GURL& server_url, | 292 const GURL& server_url, |
| 293 Geoposition* position, | 293 Geoposition* position, |
| 294 string16* access_token) { | 294 base::string16* access_token) { |
| 295 DCHECK(position); | 295 DCHECK(position); |
| 296 DCHECK(access_token); | 296 DCHECK(access_token); |
| 297 | 297 |
| 298 // HttpPost can fail for a number of reasons. Most likely this is because | 298 // HttpPost can fail for a number of reasons. Most likely this is because |
| 299 // we're offline, or there was no response. | 299 // we're offline, or there was no response. |
| 300 if (!http_post_result) { | 300 if (!http_post_result) { |
| 301 FormatPositionError(server_url, "No response received", position); | 301 FormatPositionError(server_url, "No response received", position); |
| 302 RecordUmaEvent(NETWORK_LOCATION_REQUEST_EVENT_RESPONSE_EMPTY); | 302 RecordUmaEvent(NETWORK_LOCATION_REQUEST_EVENT_RESPONSE_EMPTY); |
| 303 return; | 303 return; |
| 304 } | 304 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 if (value->GetAsInteger(&value_as_int)) { | 344 if (value->GetAsInteger(&value_as_int)) { |
| 345 *out = value_as_int; | 345 *out = value_as_int; |
| 346 return true; | 346 return true; |
| 347 } | 347 } |
| 348 return value->GetAsDouble(out); | 348 return value->GetAsDouble(out); |
| 349 } | 349 } |
| 350 | 350 |
| 351 bool ParseServerResponse(const std::string& response_body, | 351 bool ParseServerResponse(const std::string& response_body, |
| 352 const base::Time& timestamp, | 352 const base::Time& timestamp, |
| 353 Geoposition* position, | 353 Geoposition* position, |
| 354 string16* access_token) { | 354 base::string16* access_token) { |
| 355 DCHECK(position); | 355 DCHECK(position); |
| 356 DCHECK(!position->Validate()); | 356 DCHECK(!position->Validate()); |
| 357 DCHECK(position->error_code == Geoposition::ERROR_CODE_NONE); | 357 DCHECK(position->error_code == Geoposition::ERROR_CODE_NONE); |
| 358 DCHECK(access_token); | 358 DCHECK(access_token); |
| 359 DCHECK(!timestamp.is_null()); | 359 DCHECK(!timestamp.is_null()); |
| 360 | 360 |
| 361 if (response_body.empty()) { | 361 if (response_body.empty()) { |
| 362 LOG(WARNING) << "ParseServerResponse() : Response was empty."; | 362 LOG(WARNING) << "ParseServerResponse() : Response was empty."; |
| 363 return false; | 363 return false; |
| 364 } | 364 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 | 422 |
| 423 // Other fields are optional. | 423 // Other fields are optional. |
| 424 GetAsDouble(*response_object, kAccuracyString, &position->accuracy); | 424 GetAsDouble(*response_object, kAccuracyString, &position->accuracy); |
| 425 | 425 |
| 426 return true; | 426 return true; |
| 427 } | 427 } |
| 428 | 428 |
| 429 } // namespace | 429 } // namespace |
| 430 | 430 |
| 431 } // namespace content | 431 } // namespace content |
| OLD | NEW |