| 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_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 const char kLatitudeString[] = "latitude"; | 25 const char kLatitudeString[] = "latitude"; |
| 26 const char kLongitudeString[] = "longitude"; | 26 const char kLongitudeString[] = "longitude"; |
| 27 const char kAltitudeString[] = "altitude"; | 27 const char kAltitudeString[] = "altitude"; |
| 28 const char kAccuracyString[] = "accuracy"; | 28 const char kAccuracyString[] = "accuracy"; |
| 29 const char kAltitudeAccuracyString[] = "altitude_accuracy"; | 29 const char kAltitudeAccuracyString[] = "altitude_accuracy"; |
| 30 | 30 |
| 31 // Local functions | 31 // Local functions |
| 32 // Creates the request payload to send to the server. | 32 // Creates the request payload to send to the server. |
| 33 void FormRequestBody(const std::string& host_name, | 33 void FormRequestBody(const std::string& host_name, |
| 34 const string16& access_token, | 34 const string16& access_token, |
| 35 const GatewayData& gateway_data, |
| 35 const RadioData& radio_data, | 36 const RadioData& radio_data, |
| 36 const WifiData& wifi_data, | 37 const WifiData& wifi_data, |
| 37 const base::Time& timestamp, | 38 const base::Time& timestamp, |
| 38 std::string* data); | 39 std::string* data); |
| 39 // Parsers the server response. | 40 // Parsers the server response. |
| 40 void GetLocationFromResponse(bool http_post_result, | 41 void GetLocationFromResponse(bool http_post_result, |
| 41 int status_code, | 42 int status_code, |
| 42 const std::string& response_body, | 43 const std::string& response_body, |
| 43 const base::Time& timestamp, | 44 const base::Time& timestamp, |
| 44 const GURL& server_url, | 45 const GURL& server_url, |
| 45 Geoposition* position, | 46 Geoposition* position, |
| 46 string16* access_token); | 47 string16* access_token); |
| 47 | 48 |
| 48 const char* RadioTypeToString(RadioType type); | 49 const char* RadioTypeToString(RadioType type); |
| 49 // Adds a string if it's valid to the JSON object. | 50 // Adds a string if it's valid to the JSON object. |
| 50 void AddString(const std::string& property_name, | 51 void AddString(const std::string& property_name, |
| 51 const string16& value, | 52 const string16& value, |
| 52 DictionaryValue* object); | 53 DictionaryValue* object); |
| 53 // Adds an integer if it's valid to the JSON object. | 54 // Adds an integer if it's valid to the JSON object. |
| 54 void AddInteger(const std::string& property_name, | 55 void AddInteger(const std::string& property_name, |
| 55 int value, | 56 int value, |
| 56 DictionaryValue* object); | 57 DictionaryValue* object); |
| 57 // Parses the server response body. Returns true if parsing was successful. | 58 // Parses the server response body. Returns true if parsing was successful. |
| 58 // Sets |*position| to the parsed location if a valid fix was received, | 59 // Sets |*position| to the parsed location if a valid fix was received, |
| 59 // otherwise leaves it unchanged (i.e. IsInitialized() == false). | 60 // otherwise leaves it unchanged (i.e. IsInitialized() == false). |
| 60 bool ParseServerResponse(const std::string& response_body, | 61 bool ParseServerResponse(const std::string& response_body, |
| 61 const base::Time& timestamp, | 62 const base::Time& timestamp, |
| 62 Geoposition* position, | 63 Geoposition* position, |
| 63 string16* access_token); | 64 string16* access_token); |
| 65 void AddGatewayData(const GatewayData& gateway_data, |
| 66 int age_milliseconds, |
| 67 DictionaryValue* body_object); |
| 64 void AddRadioData(const RadioData& radio_data, | 68 void AddRadioData(const RadioData& radio_data, |
| 65 int age_milliseconds, | 69 int age_milliseconds, |
| 66 DictionaryValue* body_object); | 70 DictionaryValue* body_object); |
| 67 void AddWifiData(const WifiData& wifi_data, | 71 void AddWifiData(const WifiData& wifi_data, |
| 68 int age_milliseconds, | 72 int age_milliseconds, |
| 69 DictionaryValue* body_object); | 73 DictionaryValue* body_object); |
| 70 } // namespace | 74 } // namespace |
| 71 | 75 |
| 72 int NetworkLocationRequest::url_fetcher_id_for_tests = 0; | 76 int NetworkLocationRequest::url_fetcher_id_for_tests = 0; |
| 73 | 77 |
| 74 NetworkLocationRequest::NetworkLocationRequest(URLRequestContextGetter* context, | 78 NetworkLocationRequest::NetworkLocationRequest(URLRequestContextGetter* context, |
| 75 const GURL& url, | 79 const GURL& url, |
| 76 ListenerInterface* listener) | 80 ListenerInterface* listener) |
| 77 : url_context_(context), listener_(listener), | 81 : url_context_(context), listener_(listener), |
| 78 url_(url) { | 82 url_(url) { |
| 79 DCHECK(listener); | 83 DCHECK(listener); |
| 80 } | 84 } |
| 81 | 85 |
| 82 NetworkLocationRequest::~NetworkLocationRequest() { | 86 NetworkLocationRequest::~NetworkLocationRequest() { |
| 83 } | 87 } |
| 84 | 88 |
| 85 bool NetworkLocationRequest::MakeRequest(const std::string& host_name, | 89 bool NetworkLocationRequest::MakeRequest(const std::string& host_name, |
| 86 const string16& access_token, | 90 const string16& access_token, |
| 91 const GatewayData& gateway_data, |
| 87 const RadioData& radio_data, | 92 const RadioData& radio_data, |
| 88 const WifiData& wifi_data, | 93 const WifiData& wifi_data, |
| 89 const base::Time& timestamp) { | 94 const base::Time& timestamp) { |
| 90 if (url_fetcher_ != NULL) { | 95 if (url_fetcher_ != NULL) { |
| 91 DLOG(INFO) << "NetworkLocationRequest : Cancelling pending request"; | 96 DLOG(INFO) << "NetworkLocationRequest : Cancelling pending request"; |
| 92 url_fetcher_.reset(); | 97 url_fetcher_.reset(); |
| 93 } | 98 } |
| 99 gateway_data_ = gateway_data; |
| 94 radio_data_ = radio_data; | 100 radio_data_ = radio_data; |
| 95 wifi_data_ = wifi_data; | 101 wifi_data_ = wifi_data; |
| 96 timestamp_ = timestamp; | 102 timestamp_ = timestamp; |
| 97 std::string post_body; | 103 std::string post_body; |
| 98 FormRequestBody(host_name, access_token, radio_data_, wifi_data_, | 104 FormRequestBody(host_name, access_token, gateway_data, radio_data_, |
| 99 timestamp_, &post_body); | 105 wifi_data_, timestamp_, &post_body); |
| 100 | 106 |
| 101 url_fetcher_.reset(URLFetcher::Create( | 107 url_fetcher_.reset(URLFetcher::Create( |
| 102 url_fetcher_id_for_tests, url_, URLFetcher::POST, this)); | 108 url_fetcher_id_for_tests, url_, URLFetcher::POST, this)); |
| 103 url_fetcher_->set_upload_data(kMimeApplicationJson, post_body); | 109 url_fetcher_->set_upload_data(kMimeApplicationJson, post_body); |
| 104 url_fetcher_->set_request_context(url_context_); | 110 url_fetcher_->set_request_context(url_context_); |
| 105 url_fetcher_->set_load_flags( | 111 url_fetcher_->set_load_flags( |
| 106 net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | | 112 net::LOAD_BYPASS_CACHE | net::LOAD_DISABLE_CACHE | |
| 107 net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_COOKIES | | 113 net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_COOKIES | |
| 108 net::LOAD_DO_NOT_SEND_AUTH_DATA); | 114 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
| 109 url_fetcher_->Start(); | 115 url_fetcher_->Start(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 124 GetLocationFromResponse(status.is_success(), response_code, data, | 130 GetLocationFromResponse(status.is_success(), response_code, data, |
| 125 timestamp_, url, &position, &access_token); | 131 timestamp_, url, &position, &access_token); |
| 126 const bool server_error = | 132 const bool server_error = |
| 127 !status.is_success() || (response_code >= 500 && response_code < 600); | 133 !status.is_success() || (response_code >= 500 && response_code < 600); |
| 128 url_fetcher_.reset(); | 134 url_fetcher_.reset(); |
| 129 | 135 |
| 130 DCHECK(listener_); | 136 DCHECK(listener_); |
| 131 DLOG(INFO) << "NetworkLocationRequest::Run() : " | 137 DLOG(INFO) << "NetworkLocationRequest::Run() : " |
| 132 "Calling listener with position.\n"; | 138 "Calling listener with position.\n"; |
| 133 listener_->LocationResponseAvailable(position, server_error, access_token, | 139 listener_->LocationResponseAvailable(position, server_error, access_token, |
| 134 radio_data_, wifi_data_); | 140 gateway_data_, radio_data_, wifi_data_); |
| 135 } | 141 } |
| 136 | 142 |
| 137 // Local functions. | 143 // Local functions. |
| 138 namespace { | 144 namespace { |
| 139 | 145 |
| 140 void FormRequestBody(const std::string& host_name, | 146 void FormRequestBody(const std::string& host_name, |
| 141 const string16& access_token, | 147 const string16& access_token, |
| 148 const GatewayData& gateway_data, |
| 142 const RadioData& radio_data, | 149 const RadioData& radio_data, |
| 143 const WifiData& wifi_data, | 150 const WifiData& wifi_data, |
| 144 const base::Time& timestamp, | 151 const base::Time& timestamp, |
| 145 std::string* data) { | 152 std::string* data) { |
| 146 DCHECK(data); | 153 DCHECK(data); |
| 147 | 154 |
| 148 DictionaryValue body_object; | 155 DictionaryValue body_object; |
| 149 // Version and host are required. | 156 // Version and host are required. |
| 150 COMPILE_ASSERT(sizeof(kGeoLocationNetworkProtocolVersion) > 1, | 157 COMPILE_ASSERT(sizeof(kGeoLocationNetworkProtocolVersion) > 1, |
| 151 must_include_valid_version); | 158 must_include_valid_version); |
| 152 DCHECK(!host_name.empty()); | 159 DCHECK(!host_name.empty()); |
| 153 body_object.SetString("version", kGeoLocationNetworkProtocolVersion); | 160 body_object.SetString("version", kGeoLocationNetworkProtocolVersion); |
| 154 body_object.SetString("host", host_name); | 161 body_object.SetString("host", host_name); |
| 155 | 162 |
| 156 AddString("access_token", access_token, &body_object); | 163 AddString("access_token", access_token, &body_object); |
| 157 | 164 |
| 158 body_object.SetBoolean("request_address", false); | 165 body_object.SetBoolean("request_address", false); |
| 159 | 166 |
| 160 int age = kint32min; // Invalid so AddInteger() will ignore. | 167 int age = kint32min; // Invalid so AddInteger() will ignore. |
| 161 if (!timestamp.is_null()) { | 168 if (!timestamp.is_null()) { |
| 162 // Convert absolute timestamps into a relative age. | 169 // Convert absolute timestamps into a relative age. |
| 163 int64 delta_ms = (base::Time::Now() - timestamp).InMilliseconds(); | 170 int64 delta_ms = (base::Time::Now() - timestamp).InMilliseconds(); |
| 164 if (delta_ms >= 0 && delta_ms < kint32max) | 171 if (delta_ms >= 0 && delta_ms < kint32max) |
| 165 age = static_cast<int>(delta_ms); | 172 age = static_cast<int>(delta_ms); |
| 166 } | 173 } |
| 167 AddRadioData(radio_data, age, &body_object); | 174 AddRadioData(radio_data, age, &body_object); |
| 168 AddWifiData(wifi_data, age, &body_object); | 175 AddWifiData(wifi_data, age, &body_object); |
| 176 AddGatewayData(gateway_data, age, &body_object); |
| 169 | 177 |
| 170 base::JSONWriter::Write(&body_object, false, data); | 178 base::JSONWriter::Write(&body_object, false, data); |
| 171 DLOG(INFO) << "NetworkLocationRequest::FormRequestBody(): Formed body " | 179 DLOG(INFO) << "NetworkLocationRequest::FormRequestBody(): Formed body " |
| 172 << *data << ".\n"; | 180 << *data << ".\n"; |
| 173 } | 181 } |
| 174 | 182 |
| 175 void FormatPositionError(const GURL& server_url, | 183 void FormatPositionError(const GURL& server_url, |
| 176 const std::string& message, | 184 const std::string& message, |
| 177 Geoposition* position) { | 185 Geoposition* position) { |
| 178 position->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 186 position->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 AddString("mac_address", iter->mac_address, wifi_tower); | 422 AddString("mac_address", iter->mac_address, wifi_tower); |
| 415 AddInteger("signal_strength", iter->radio_signal_strength, wifi_tower); | 423 AddInteger("signal_strength", iter->radio_signal_strength, wifi_tower); |
| 416 AddInteger("age", age_milliseconds, wifi_tower); | 424 AddInteger("age", age_milliseconds, wifi_tower); |
| 417 AddInteger("channel", iter->channel, wifi_tower); | 425 AddInteger("channel", iter->channel, wifi_tower); |
| 418 AddInteger("signal_to_noise", iter->signal_to_noise, wifi_tower); | 426 AddInteger("signal_to_noise", iter->signal_to_noise, wifi_tower); |
| 419 AddString("ssid", iter->ssid, wifi_tower); | 427 AddString("ssid", iter->ssid, wifi_tower); |
| 420 wifi_towers->Append(wifi_tower); | 428 wifi_towers->Append(wifi_tower); |
| 421 } | 429 } |
| 422 body_object->Set("wifi_towers", wifi_towers); | 430 body_object->Set("wifi_towers", wifi_towers); |
| 423 } | 431 } |
| 432 |
| 433 void AddGatewayData(const GatewayData& gateway_data, |
| 434 int age_milliseconds, |
| 435 DictionaryValue* body_object) { |
| 436 DCHECK(body_object); |
| 437 |
| 438 if (gateway_data.router_data.empty()) { |
| 439 return; |
| 440 } |
| 441 |
| 442 ListValue* gateways = new ListValue; |
| 443 for (GatewayData::RouterDataSet::const_iterator iter = |
| 444 gateway_data.router_data.begin(); |
| 445 iter != gateway_data.router_data.end(); |
| 446 iter++) { |
| 447 DictionaryValue* gateway = new DictionaryValue; |
| 448 AddString("mac_address", iter->mac_address, gateway); |
| 449 gateways->Append(gateway); |
| 450 } |
| 451 body_object->Set("gateways", gateways); |
| 452 } |
| 424 } // namespace | 453 } // namespace |
| OLD | NEW |