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