| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/geolocation/network_location_request.h" | 5 #include "chrome/browser/geolocation/network_location_request.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 void AddRadioData(const RadioData& radio_data, DictionaryValue* body_object); | 59 void AddRadioData(const RadioData& radio_data, DictionaryValue* body_object); |
| 60 void AddWifiData(const WifiData& wifi_data, DictionaryValue* body_object); | 60 void AddWifiData(const WifiData& wifi_data, DictionaryValue* body_object); |
| 61 } // namespace | 61 } // namespace |
| 62 | 62 |
| 63 NetworkLocationRequest::NetworkLocationRequest(URLRequestContextGetter* context, | 63 NetworkLocationRequest::NetworkLocationRequest(URLRequestContextGetter* context, |
| 64 const GURL& url, | 64 const GURL& url, |
| 65 const string16& host_name, | 65 const string16& host_name, |
| 66 ListenerInterface* listener) | 66 ListenerInterface* listener) |
| 67 : url_context_(context), timestamp_(kint64min), listener_(listener), | 67 : url_context_(context), timestamp_(kint64min), listener_(listener), |
| 68 url_(url), host_name_(host_name) { | 68 url_(url), host_name_(host_name) { |
| 69 // DCHECK(url_context_); |
| 69 DCHECK(listener); | 70 DCHECK(listener); |
| 70 } | 71 } |
| 71 | 72 |
| 72 NetworkLocationRequest::~NetworkLocationRequest() { | 73 NetworkLocationRequest::~NetworkLocationRequest() { |
| 73 } | 74 } |
| 74 | 75 |
| 75 bool NetworkLocationRequest::MakeRequest(const string16& access_token, | 76 bool NetworkLocationRequest::MakeRequest(const string16& access_token, |
| 76 const RadioData& radio_data, | 77 const RadioData& radio_data, |
| 77 const WifiData& wifi_data, | 78 const WifiData& wifi_data, |
| 78 int64 timestamp) { | 79 int64 timestamp) { |
| 79 if (url_fetcher_ != NULL) { | 80 if (url_fetcher_ != NULL) { |
| 80 DLOG(INFO) << "NetworkLocationRequest : Cancelling pending request"; | 81 DLOG(INFO) << "NetworkLocationRequest : Cancelling pending request"; |
| 81 url_fetcher_.reset(); | 82 url_fetcher_.reset(); |
| 82 } | 83 } |
| 83 std::string post_body; | 84 std::string post_body; |
| 84 if (!FormRequestBody(host_name_, access_token, radio_data, wifi_data, | 85 if (!FormRequestBody(host_name_, access_token, radio_data, wifi_data, |
| 85 &post_body)) { | 86 &post_body)) { |
| 86 return false; | 87 return false; |
| 87 } | 88 } |
| 88 timestamp_ = timestamp; | 89 timestamp_ = timestamp; |
| 89 | 90 |
| 90 url_fetcher_.reset(URLFetcher::Create( | 91 url_fetcher_.reset(URLFetcher::Create( |
| 91 wifi_data.access_point_data.size(), // Used for testing | 92 host_name_.size(), // Used for testing |
| 92 url_, URLFetcher::POST, this)); | 93 url_, URLFetcher::POST, this)); |
| 93 url_fetcher_->set_upload_data(kMimeApplicationJson, post_body); | 94 url_fetcher_->set_upload_data(kMimeApplicationJson, post_body); |
| 94 url_fetcher_->set_request_context(url_context_); | 95 url_fetcher_->set_request_context(url_context_); |
| 95 url_fetcher_->Start(); | 96 url_fetcher_->Start(); |
| 96 return true; | 97 return true; |
| 97 } | 98 } |
| 98 | 99 |
| 99 void NetworkLocationRequest::OnURLFetchComplete(const URLFetcher* source, | 100 void NetworkLocationRequest::OnURLFetchComplete(const URLFetcher* source, |
| 100 const GURL& url, | 101 const GURL& url, |
| 101 const URLRequestStatus& status, | 102 const URLRequestStatus& status, |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 << response_value->GetType() << ".\n"; | 305 << response_value->GetType() << ".\n"; |
| 305 return false; | 306 return false; |
| 306 } | 307 } |
| 307 const DictionaryValue* response_object = | 308 const DictionaryValue* response_object = |
| 308 static_cast<DictionaryValue*>(response_value.get()); | 309 static_cast<DictionaryValue*>(response_value.get()); |
| 309 | 310 |
| 310 // Get the access token, if any. | 311 // Get the access token, if any. |
| 311 response_object->GetStringAsUTF16(kAccessTokenString, access_token); | 312 response_object->GetStringAsUTF16(kAccessTokenString, access_token); |
| 312 | 313 |
| 313 // Get the location | 314 // Get the location |
| 314 Value* location_value = NULL; | 315 DictionaryValue* location_object; |
| 315 if (!response_object->Get(kLocationString, &location_value)) { | 316 if (!response_object->GetDictionary(kLocationString, &location_object)) { |
| 316 LOG(INFO) << "ParseServerResponse() : Missing location attribute.\n"; | 317 Value* value = NULL; |
| 317 return false; | 318 // If the network provider was unable to provide a position fix, it should |
| 319 // return a 200 with location == null. Otherwise it's an error. |
| 320 return response_object->Get(kLocationString, &value) |
| 321 && value && value->IsType(Value::TYPE_NULL); |
| 318 } | 322 } |
| 319 DCHECK(location_value); | 323 DCHECK(location_object); |
| 320 | |
| 321 if (!location_value->IsType(Value::TYPE_DICTIONARY)) { | |
| 322 if (!location_value->IsType(Value::TYPE_NULL)) { | |
| 323 LOG(INFO) << "ParseServerResponse() : Unexpected location type" | |
| 324 << location_value->GetType() << ".\n"; | |
| 325 // If the network provider was unable to provide a position fix, it should | |
| 326 // return a HTTP 200, with "location" : null. Otherwise it's an error. | |
| 327 return false; | |
| 328 } | |
| 329 return true; // Successfully parsed response containing no fix. | |
| 330 } | |
| 331 DictionaryValue* location_object = | |
| 332 static_cast<DictionaryValue*>(location_value); | |
| 333 | 324 |
| 334 // latitude and longitude fields are always required. | 325 // latitude and longitude fields are always required. |
| 335 double latitude, longitude; | 326 double latitude, longitude; |
| 336 if (!GetAsDouble(*location_object, kLatitudeString, &latitude) || | 327 if (!GetAsDouble(*location_object, kLatitudeString, &latitude) || |
| 337 !GetAsDouble(*location_object, kLongitudeString, &longitude)) { | 328 !GetAsDouble(*location_object, kLongitudeString, &longitude)) { |
| 338 LOG(INFO) << "ParseServerResponse() : location lacks lat and/or long.\n"; | |
| 339 return false; | 329 return false; |
| 340 } | 330 } |
| 341 // All error paths covered: now start actually modifying postion. | 331 // All error paths covered: now start actually modifying postion. |
| 342 position->latitude = latitude; | 332 position->latitude = latitude; |
| 343 position->longitude = longitude; | 333 position->longitude = longitude; |
| 344 position->timestamp = timestamp; | 334 position->timestamp = timestamp; |
| 345 | 335 |
| 346 // Other fields are optional. | 336 // Other fields are optional. |
| 347 GetAsDouble(*location_object, kAccuracyString, &position->accuracy); | 337 GetAsDouble(*location_object, kAccuracyString, &position->accuracy); |
| 348 GetAsDouble(*location_object, kAltitudeString, &position->altitude); | 338 GetAsDouble(*location_object, kAltitudeString, &position->altitude); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 AddInteger(L"signal_strength", iter->radio_signal_strength, wifi_tower); | 395 AddInteger(L"signal_strength", iter->radio_signal_strength, wifi_tower); |
| 406 AddInteger(L"age", iter->age, wifi_tower); | 396 AddInteger(L"age", iter->age, wifi_tower); |
| 407 AddInteger(L"channel", iter->channel, wifi_tower); | 397 AddInteger(L"channel", iter->channel, wifi_tower); |
| 408 AddInteger(L"signal_to_noise", iter->signal_to_noise, wifi_tower); | 398 AddInteger(L"signal_to_noise", iter->signal_to_noise, wifi_tower); |
| 409 AddString(L"ssid", iter->ssid, wifi_tower); | 399 AddString(L"ssid", iter->ssid, wifi_tower); |
| 410 wifi_towers->Append(wifi_tower); | 400 wifi_towers->Append(wifi_tower); |
| 411 } | 401 } |
| 412 body_object->Set(L"wifi_towers", wifi_towers); | 402 body_object->Set(L"wifi_towers", wifi_towers); |
| 413 } | 403 } |
| 414 } // namespace | 404 } // namespace |
| OLD | NEW |