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 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 } | 286 } |
287 | 287 |
288 // Numeric values without a decimal point have type integer and IsDouble() will | 288 // Numeric values without a decimal point have type integer and IsDouble() will |
289 // return false. This is convenience function for detecting integer or floating | 289 // return false. This is convenience function for detecting integer or floating |
290 // point numeric values. Note that isIntegral() includes boolean values, which | 290 // point numeric values. Note that isIntegral() includes boolean values, which |
291 // is not what we want. | 291 // is not what we want. |
292 bool GetAsDouble(const DictionaryValue& object, | 292 bool GetAsDouble(const DictionaryValue& object, |
293 const std::string& property_name, | 293 const std::string& property_name, |
294 double* out) { | 294 double* out) { |
295 DCHECK(out); | 295 DCHECK(out); |
296 Value* value = NULL; | 296 const Value* value = NULL; |
297 if (!object.Get(property_name, &value)) | 297 if (!object.Get(property_name, &value)) |
298 return false; | 298 return false; |
299 int value_as_int; | 299 int value_as_int; |
300 DCHECK(value); | 300 DCHECK(value); |
301 if (value->GetAsInteger(&value_as_int)) { | 301 if (value->GetAsInteger(&value_as_int)) { |
302 *out = value_as_int; | 302 *out = value_as_int; |
303 return true; | 303 return true; |
304 } | 304 } |
305 return value->GetAsDouble(out); | 305 return value->GetAsDouble(out); |
306 } | 306 } |
(...skipping 26 matching lines...) Expand all Loading... |
333 | 333 |
334 if (!response_value->IsType(Value::TYPE_DICTIONARY)) { | 334 if (!response_value->IsType(Value::TYPE_DICTIONARY)) { |
335 VLOG(1) << "ParseServerResponse() : Unexpected response type " | 335 VLOG(1) << "ParseServerResponse() : Unexpected response type " |
336 << response_value->GetType(); | 336 << response_value->GetType(); |
337 return false; | 337 return false; |
338 } | 338 } |
339 const DictionaryValue* response_object = | 339 const DictionaryValue* response_object = |
340 static_cast<DictionaryValue*>(response_value.get()); | 340 static_cast<DictionaryValue*>(response_value.get()); |
341 | 341 |
342 // Check the status code. | 342 // Check the status code. |
343 Value* status_value = NULL; | 343 const Value* status_value = NULL; |
344 if (!response_object->Get(kStatusString, &status_value)) { | 344 if (!response_object->Get(kStatusString, &status_value)) { |
345 VLOG(1) << "ParseServerResponse() : Missing status attribute."; | 345 VLOG(1) << "ParseServerResponse() : Missing status attribute."; |
346 // The status attribute is required. | 346 // The status attribute is required. |
347 return false; | 347 return false; |
348 } | 348 } |
349 DCHECK(status_value); | 349 DCHECK(status_value); |
350 | 350 |
351 if (!status_value->IsType(Value::TYPE_STRING)) { | 351 if (!status_value->IsType(Value::TYPE_STRING)) { |
352 VLOG(1) << "ParseServerResponse() : Unexpected status type " | 352 VLOG(1) << "ParseServerResponse() : Unexpected status type " |
353 << status_value->GetType(); | 353 << status_value->GetType(); |
354 // The status attribute is required to be a string. | 354 // The status attribute is required to be a string. |
355 return false; | 355 return false; |
356 } | 356 } |
357 StringValue* status_object = static_cast<StringValue*>(status_value); | 357 const StringValue* status_object = |
| 358 static_cast<const StringValue*>(status_value); |
358 | 359 |
359 std::string status; | 360 std::string status; |
360 if (!status_object->GetAsString(&status)) { | 361 if (!status_object->GetAsString(&status)) { |
361 VLOG(1) << "ParseServerResponse() : Error parsing the status value."; | 362 VLOG(1) << "ParseServerResponse() : Error parsing the status value."; |
362 return false; | 363 return false; |
363 } | 364 } |
364 | 365 |
365 if (status != kStatusOKString) { | 366 if (status != kStatusOKString) { |
366 VLOG(1) << "ParseServerResponse() : Request failed with status " | 367 VLOG(1) << "ParseServerResponse() : Request failed with status " |
367 << status; | 368 << status; |
368 return false; | 369 return false; |
369 } | 370 } |
370 | 371 |
371 // Get the access token, if any. | 372 // Get the access token, if any. |
372 response_object->GetString(kAccessTokenString, access_token); | 373 response_object->GetString(kAccessTokenString, access_token); |
373 | 374 |
374 // Get the location | 375 // Get the location |
375 Value* location_value = NULL; | 376 const Value* location_value = NULL; |
376 if (!response_object->Get(kLocationString, &location_value)) { | 377 if (!response_object->Get(kLocationString, &location_value)) { |
377 VLOG(1) << "ParseServerResponse() : Missing location attribute."; | 378 VLOG(1) << "ParseServerResponse() : Missing location attribute."; |
378 // GLS returns a response with no location property to represent | 379 // GLS returns a response with no location property to represent |
379 // no fix available; return true to indicate successful parse. | 380 // no fix available; return true to indicate successful parse. |
380 return true; | 381 return true; |
381 } | 382 } |
382 DCHECK(location_value); | 383 DCHECK(location_value); |
383 | 384 |
384 if (!location_value->IsType(Value::TYPE_DICTIONARY)) { | 385 if (!location_value->IsType(Value::TYPE_DICTIONARY)) { |
385 if (!location_value->IsType(Value::TYPE_NULL)) { | 386 if (!location_value->IsType(Value::TYPE_NULL)) { |
386 VLOG(1) << "ParseServerResponse() : Unexpected location type " | 387 VLOG(1) << "ParseServerResponse() : Unexpected location type " |
387 << location_value->GetType(); | 388 << location_value->GetType(); |
388 // If the network provider was unable to provide a position fix, it should | 389 // If the network provider was unable to provide a position fix, it should |
389 // return a HTTP 200, with "location" : null. Otherwise it's an error. | 390 // return a HTTP 200, with "location" : null. Otherwise it's an error. |
390 return false; | 391 return false; |
391 } | 392 } |
392 return true; // Successfully parsed response containing no fix. | 393 return true; // Successfully parsed response containing no fix. |
393 } | 394 } |
394 DictionaryValue* location_object = | 395 const DictionaryValue* location_object = |
395 static_cast<DictionaryValue*>(location_value); | 396 static_cast<const DictionaryValue*>(location_value); |
396 | 397 |
397 // latitude and longitude fields are always required. | 398 // latitude and longitude fields are always required. |
398 double latitude, longitude; | 399 double latitude, longitude; |
399 if (!GetAsDouble(*location_object, kLatitudeString, &latitude) || | 400 if (!GetAsDouble(*location_object, kLatitudeString, &latitude) || |
400 !GetAsDouble(*location_object, kLongitudeString, &longitude)) { | 401 !GetAsDouble(*location_object, kLongitudeString, &longitude)) { |
401 VLOG(1) << "ParseServerResponse() : location lacks lat and/or long."; | 402 VLOG(1) << "ParseServerResponse() : location lacks lat and/or long."; |
402 return false; | 403 return false; |
403 } | 404 } |
404 // All error paths covered: now start actually modifying postion. | 405 // All error paths covered: now start actually modifying postion. |
405 position->latitude = latitude; | 406 position->latitude = latitude; |
406 position->longitude = longitude; | 407 position->longitude = longitude; |
407 position->timestamp = timestamp; | 408 position->timestamp = timestamp; |
408 | 409 |
409 // Other fields are optional. | 410 // Other fields are optional. |
410 GetAsDouble(*response_object, kAccuracyString, &position->accuracy); | 411 GetAsDouble(*response_object, kAccuracyString, &position->accuracy); |
411 | 412 |
412 return true; | 413 return true; |
413 } | 414 } |
414 | 415 |
415 } // namespace | 416 } // namespace |
OLD | NEW |