| 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" |
| 11 #include "chrome/browser/net/url_request_context_getter.h" | 11 #include "chrome/browser/net/url_request_context_getter.h" |
| 12 #include "chrome/common/geoposition.h" | 12 #include "chrome/common/geoposition.h" |
| 13 #include "net/base/load_flags.h" |
| 13 #include "net/url_request/url_request_status.h" | 14 #include "net/url_request/url_request_status.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 const char* const kMimeApplicationJson = "application/json"; | 17 const char* const kMimeApplicationJson = "application/json"; |
| 17 | 18 |
| 18 // See http://code.google.com/apis/gears/geolocation_network_protocol.html | 19 // See http://code.google.com/apis/gears/geolocation_network_protocol.html |
| 19 const char* kGeoLocationNetworkProtocolVersion = "1.1.0"; | 20 const char* kGeoLocationNetworkProtocolVersion = "1.1.0"; |
| 20 | 21 |
| 21 const wchar_t* kAccessTokenString = L"access_token"; | 22 const wchar_t* kAccessTokenString = L"access_token"; |
| 22 const wchar_t* kLocationString = L"location"; | 23 const wchar_t* kLocationString = L"location"; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 wifi_data.access_point_data.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_); |
| 96 url_fetcher_->set_load_flags( |
| 97 net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | |
| 98 net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_COOKIES | |
| 99 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
| 95 url_fetcher_->Start(); | 100 url_fetcher_->Start(); |
| 96 return true; | 101 return true; |
| 97 } | 102 } |
| 98 | 103 |
| 99 void NetworkLocationRequest::OnURLFetchComplete(const URLFetcher* source, | 104 void NetworkLocationRequest::OnURLFetchComplete(const URLFetcher* source, |
| 100 const GURL& url, | 105 const GURL& url, |
| 101 const URLRequestStatus& status, | 106 const URLRequestStatus& status, |
| 102 int response_code, | 107 int response_code, |
| 103 const ResponseCookies& cookies, | 108 const ResponseCookies& cookies, |
| 104 const std::string& data) { | 109 const std::string& data) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 body_object.SetString(L"version", kGeoLocationNetworkProtocolVersion); | 142 body_object.SetString(L"version", kGeoLocationNetworkProtocolVersion); |
| 138 AddString(L"host", host_name, &body_object); | 143 AddString(L"host", host_name, &body_object); |
| 139 | 144 |
| 140 AddString(L"access_token", access_token, &body_object); | 145 AddString(L"access_token", access_token, &body_object); |
| 141 | 146 |
| 142 body_object.SetBoolean(L"request_address", false); | 147 body_object.SetBoolean(L"request_address", false); |
| 143 | 148 |
| 144 AddRadioData(radio_data, &body_object); | 149 AddRadioData(radio_data, &body_object); |
| 145 AddWifiData(wifi_data, &body_object); | 150 AddWifiData(wifi_data, &body_object); |
| 146 | 151 |
| 147 // TODO(joth): Do we need to mess with locales? | |
| 148 // We always use the platform independent 'C' locale when writing the JSON | |
| 149 // request, irrespective of the browser's locale. This avoids the need for | |
| 150 // the network location provider to determine the locale of the request and | |
| 151 // parse the JSON accordingly. | |
| 152 // char* current_locale = setlocale(LC_NUMERIC, "C"); | |
| 153 | |
| 154 base::JSONWriter::Write(&body_object, false, data); | 152 base::JSONWriter::Write(&body_object, false, data); |
| 155 | |
| 156 // setlocale(LC_NUMERIC, current_locale); | |
| 157 | |
| 158 DLOG(INFO) << "NetworkLocationRequest::FormRequestBody(): Formed body " | 153 DLOG(INFO) << "NetworkLocationRequest::FormRequestBody(): Formed body " |
| 159 << data << ".\n"; | 154 << data << ".\n"; |
| 160 return true; | 155 return true; |
| 161 } | 156 } |
| 162 | 157 |
| 163 void FormatPositionError(const GURL& server_url, | 158 void FormatPositionError(const GURL& server_url, |
| 164 const std::wstring& message, | 159 const std::wstring& message, |
| 165 Geoposition* position) { | 160 Geoposition* position) { |
| 166 position->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 161 position->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
| 167 position->error_message = L"Network location provider at '"; | 162 position->error_message = L"Network location provider at '"; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 DCHECK(timestamp != kint64min); | 270 DCHECK(timestamp != kint64min); |
| 276 | 271 |
| 277 if (response_body.empty()) { | 272 if (response_body.empty()) { |
| 278 LOG(WARNING) << "ParseServerResponse() : Response was empty.\n"; | 273 LOG(WARNING) << "ParseServerResponse() : Response was empty.\n"; |
| 279 return false; | 274 return false; |
| 280 } | 275 } |
| 281 DLOG(INFO) << "ParseServerResponse() : Parsing response " | 276 DLOG(INFO) << "ParseServerResponse() : Parsing response " |
| 282 << response_body << ".\n"; | 277 << response_body << ".\n"; |
| 283 | 278 |
| 284 // Parse the response, ignoring comments. | 279 // Parse the response, ignoring comments. |
| 285 // TODO(joth): Gears version stated: The JSON reponse from the network | |
| 286 // location provider should always use the 'C' locale. | |
| 287 // Chromium JSON parser works in UTF8 so hopefully we can ignore setlocale? | |
| 288 | |
| 289 // char* current_locale = setlocale(LC_NUMERIC, "C"); | |
| 290 std::string error_msg; | 280 std::string error_msg; |
| 291 scoped_ptr<Value> response_value(base::JSONReader::ReadAndReturnError( | 281 scoped_ptr<Value> response_value(base::JSONReader::ReadAndReturnError( |
| 292 response_body, false, &error_msg)); | 282 response_body, false, &error_msg)); |
| 293 | |
| 294 // setlocale(LC_NUMERIC, current_locale); | |
| 295 | |
| 296 if (response_value == NULL) { | 283 if (response_value == NULL) { |
| 297 LOG(WARNING) << "ParseServerResponse() : JSONReader failed : " | 284 LOG(WARNING) << "ParseServerResponse() : JSONReader failed : " |
| 298 << error_msg << ".\n"; | 285 << error_msg << ".\n"; |
| 299 return false; | 286 return false; |
| 300 } | 287 } |
| 301 | 288 |
| 302 if (!response_value->IsType(Value::TYPE_DICTIONARY)) { | 289 if (!response_value->IsType(Value::TYPE_DICTIONARY)) { |
| 303 LOG(INFO) << "ParseServerResponse() : Unexpected resopnse type " | 290 LOG(INFO) << "ParseServerResponse() : Unexpected resopnse type " |
| 304 << response_value->GetType() << ".\n"; | 291 << response_value->GetType() << ".\n"; |
| 305 return false; | 292 return false; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 AddInteger(L"signal_strength", iter->radio_signal_strength, wifi_tower); | 392 AddInteger(L"signal_strength", iter->radio_signal_strength, wifi_tower); |
| 406 AddInteger(L"age", iter->age, wifi_tower); | 393 AddInteger(L"age", iter->age, wifi_tower); |
| 407 AddInteger(L"channel", iter->channel, wifi_tower); | 394 AddInteger(L"channel", iter->channel, wifi_tower); |
| 408 AddInteger(L"signal_to_noise", iter->signal_to_noise, wifi_tower); | 395 AddInteger(L"signal_to_noise", iter->signal_to_noise, wifi_tower); |
| 409 AddString(L"ssid", iter->ssid, wifi_tower); | 396 AddString(L"ssid", iter->ssid, wifi_tower); |
| 410 wifi_towers->Append(wifi_tower); | 397 wifi_towers->Append(wifi_tower); |
| 411 } | 398 } |
| 412 body_object->Set(L"wifi_towers", wifi_towers); | 399 body_object->Set(L"wifi_towers", wifi_towers); |
| 413 } | 400 } |
| 414 } // namespace | 401 } // namespace |
| OLD | NEW |