Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: chrome/browser/geolocation/network_location_request.cc

Issue 3091005: Rename GetStringAsUTF16() to GetString() (and overload), etc. (Closed)
Patch Set: Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "base/values.h" 11 #include "base/values.h"
12 #include "chrome/common/geoposition.h" 12 #include "chrome/common/geoposition.h"
13 #include "chrome/common/net/url_request_context_getter.h" 13 #include "chrome/common/net/url_request_context_getter.h"
14 #include "net/base/load_flags.h" 14 #include "net/base/load_flags.h"
15 #include "net/url_request/url_request_status.h" 15 #include "net/url_request/url_request_status.h"
16 16
17 namespace { 17 namespace {
18 const char kMimeApplicationJson[] = "application/json"; 18 const char kMimeApplicationJson[] = "application/json";
19 19
20 // See http://code.google.com/apis/gears/geolocation_network_protocol.html 20 // See http://code.google.com/apis/gears/geolocation_network_protocol.html
21 const char kGeoLocationNetworkProtocolVersion[] = "1.1.0"; 21 const char kGeoLocationNetworkProtocolVersion[] = "1.1.0";
22 22
23 const wchar_t kAccessTokenString[] = L"access_token"; 23 const char kAccessTokenString[] = "access_token";
24 const wchar_t kLocationString[] = L"location"; 24 const char kLocationString[] = "location";
25 const wchar_t kLatitudeString[] = L"latitude"; 25 const char kLatitudeString[] = "latitude";
26 const wchar_t kLongitudeString[] = L"longitude"; 26 const char kLongitudeString[] = "longitude";
27 const wchar_t kAltitudeString[] = L"altitude"; 27 const char kAltitudeString[] = "altitude";
28 const wchar_t kAccuracyString[] = L"accuracy"; 28 const char kAccuracyString[] = "accuracy";
29 const wchar_t kAltitudeAccuracyString[] = L"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 RadioData& radio_data, 35 const RadioData& radio_data,
36 const WifiData& wifi_data, 36 const WifiData& wifi_data,
37 const base::Time& timestamp, 37 const base::Time& timestamp,
38 std::string* data); 38 std::string* data);
39 // Parsers the server response. 39 // Parsers the server response.
40 void GetLocationFromResponse(bool http_post_result, 40 void GetLocationFromResponse(bool http_post_result,
41 int status_code, 41 int status_code,
42 const std::string& response_body, 42 const std::string& response_body,
43 const base::Time& timestamp, 43 const base::Time& timestamp,
44 const GURL& server_url, 44 const GURL& server_url,
45 Geoposition* position, 45 Geoposition* position,
46 string16* access_token); 46 string16* access_token);
47 47
48 const char* RadioTypeToString(RadioType type); 48 const char* RadioTypeToString(RadioType type);
49 // Adds a string if it's valid to the JSON object. 49 // Adds a string if it's valid to the JSON object.
50 void AddString(const std::wstring& property_name, 50 void AddString(const std::string& property_name,
51 const string16& value, 51 const string16& value,
52 DictionaryValue* object); 52 DictionaryValue* object);
53 // Adds an integer if it's valid to the JSON object. 53 // Adds an integer if it's valid to the JSON object.
54 void AddInteger(const std::wstring& property_name, 54 void AddInteger(const std::string& property_name,
55 int value, 55 int value,
56 DictionaryValue* object); 56 DictionaryValue* object);
57 // Parses the server response body. Returns true if parsing was successful. 57 // Parses the server response body. Returns true if parsing was successful.
58 // Sets |*position| to the parsed location if a valid fix was received, 58 // Sets |*position| to the parsed location if a valid fix was received,
59 // otherwise leaves it unchanged (i.e. IsInitialized() == false). 59 // otherwise leaves it unchanged (i.e. IsInitialized() == false).
60 bool ParseServerResponse(const std::string& response_body, 60 bool ParseServerResponse(const std::string& response_body,
61 const base::Time& timestamp, 61 const base::Time& timestamp,
62 Geoposition* position, 62 Geoposition* position,
63 string16* access_token); 63 string16* access_token);
64 void AddRadioData(const RadioData& radio_data, 64 void AddRadioData(const RadioData& radio_data,
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 const WifiData& wifi_data, 143 const WifiData& wifi_data,
144 const base::Time& timestamp, 144 const base::Time& timestamp,
145 std::string* data) { 145 std::string* data) {
146 DCHECK(data); 146 DCHECK(data);
147 147
148 DictionaryValue body_object; 148 DictionaryValue body_object;
149 // Version and host are required. 149 // Version and host are required.
150 COMPILE_ASSERT(sizeof(kGeoLocationNetworkProtocolVersion) > 1, 150 COMPILE_ASSERT(sizeof(kGeoLocationNetworkProtocolVersion) > 1,
151 must_include_valid_version); 151 must_include_valid_version);
152 DCHECK(!host_name.empty()); 152 DCHECK(!host_name.empty());
153 body_object.SetString(L"version", kGeoLocationNetworkProtocolVersion); 153 body_object.SetString("version", kGeoLocationNetworkProtocolVersion);
154 body_object.SetString(L"host", host_name); 154 body_object.SetString("host", host_name);
155 155
156 AddString(L"access_token", access_token, &body_object); 156 AddString("access_token", access_token, &body_object);
157 157
158 body_object.SetBoolean(L"request_address", false); 158 body_object.SetBoolean("request_address", false);
159 159
160 int age = kint32min; // Invalid so AddInteger() will ignore. 160 int age = kint32min; // Invalid so AddInteger() will ignore.
161 if (!timestamp.is_null()) { 161 if (!timestamp.is_null()) {
162 // Convert absolute timestamps into a relative age. 162 // Convert absolute timestamps into a relative age.
163 int64 delta_ms = (base::Time::Now() - timestamp).InMilliseconds(); 163 int64 delta_ms = (base::Time::Now() - timestamp).InMilliseconds();
164 if (delta_ms >= 0 && delta_ms < kint32max) 164 if (delta_ms >= 0 && delta_ms < kint32max)
165 age = static_cast<int>(delta_ms); 165 age = static_cast<int>(delta_ms);
166 } 166 }
167 AddRadioData(radio_data, age, &body_object); 167 AddRadioData(radio_data, age, &body_object);
168 AddWifiData(wifi_data, age, &body_object); 168 AddWifiData(wifi_data, age, &body_object);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 case RADIO_TYPE_CDMA: 232 case RADIO_TYPE_CDMA:
233 return "cdma"; 233 return "cdma";
234 case RADIO_TYPE_WCDMA: 234 case RADIO_TYPE_WCDMA:
235 return "wcdma"; 235 return "wcdma";
236 default: 236 default:
237 LOG(DFATAL) << "Bad RadioType"; 237 LOG(DFATAL) << "Bad RadioType";
238 } 238 }
239 return "unknown"; 239 return "unknown";
240 } 240 }
241 241
242 void AddString(const std::wstring& property_name, 242 void AddString(const std::string& property_name,
243 const string16& value, 243 const string16& value,
244 DictionaryValue* object) { 244 DictionaryValue* object) {
245 DCHECK(object); 245 DCHECK(object);
246 if (!value.empty()) { 246 if (!value.empty()) {
247 object->SetString(property_name, value); 247 object->SetString(property_name, value);
248 } 248 }
249 } 249 }
250 250
251 void AddInteger(const std::wstring& property_name, 251 void AddInteger(const std::string& property_name,
252 int value, 252 int value,
253 DictionaryValue* object) { 253 DictionaryValue* object) {
254 DCHECK(object); 254 DCHECK(object);
255 if (kint32min != value) { 255 if (kint32min != value) {
256 object->SetInteger(property_name, value); 256 object->SetInteger(property_name, value);
257 } 257 }
258 } 258 }
259 259
260 // Numeric values without a decimal point have type integer and IsDouble() will 260 // Numeric values without a decimal point have type integer and IsDouble() will
261 // return false. This is convenience function for detecting integer or floating 261 // return false. This is convenience function for detecting integer or floating
262 // point numeric values. Note that isIntegral() includes boolean values, which 262 // point numeric values. Note that isIntegral() includes boolean values, which
263 // is not what we want. 263 // is not what we want.
264 bool GetAsDouble(const DictionaryValue& object, 264 bool GetAsDouble(const DictionaryValue& object,
265 const std::wstring& property_name, 265 const std::string& property_name,
266 double* out) { 266 double* out) {
267 DCHECK(out); 267 DCHECK(out);
268 Value* value = NULL; 268 Value* value = NULL;
269 if (!object.Get(property_name, &value)) 269 if (!object.Get(property_name, &value))
270 return false; 270 return false;
271 int value_as_int; 271 int value_as_int;
272 DCHECK(value); 272 DCHECK(value);
273 if (value->GetAsInteger(&value_as_int)) { 273 if (value->GetAsInteger(&value_as_int)) {
274 *out = value_as_int; 274 *out = value_as_int;
275 return true; 275 return true;
(...skipping 29 matching lines...) Expand all
305 305
306 if (!response_value->IsType(Value::TYPE_DICTIONARY)) { 306 if (!response_value->IsType(Value::TYPE_DICTIONARY)) {
307 LOG(INFO) << "ParseServerResponse() : Unexpected resopnse type " 307 LOG(INFO) << "ParseServerResponse() : Unexpected resopnse type "
308 << response_value->GetType() << ".\n"; 308 << response_value->GetType() << ".\n";
309 return false; 309 return false;
310 } 310 }
311 const DictionaryValue* response_object = 311 const DictionaryValue* response_object =
312 static_cast<DictionaryValue*>(response_value.get()); 312 static_cast<DictionaryValue*>(response_value.get());
313 313
314 // Get the access token, if any. 314 // Get the access token, if any.
315 response_object->GetStringAsUTF16(kAccessTokenString, access_token); 315 response_object->GetString(kAccessTokenString, access_token);
316 316
317 // Get the location 317 // Get the location
318 Value* location_value = NULL; 318 Value* location_value = NULL;
319 if (!response_object->Get(kLocationString, &location_value)) { 319 if (!response_object->Get(kLocationString, &location_value)) {
320 LOG(INFO) << "ParseServerResponse() : Missing location attribute.\n"; 320 LOG(INFO) << "ParseServerResponse() : Missing location attribute.\n";
321 // GLS returns a response with no location property to represent 321 // GLS returns a response with no location property to represent
322 // no fix available; return true to indicate successful parse. 322 // no fix available; return true to indicate successful parse.
323 return true; 323 return true;
324 } 324 }
325 DCHECK(location_value); 325 DCHECK(location_value);
(...skipping 30 matching lines...) Expand all
356 &position->altitude_accuracy); 356 &position->altitude_accuracy);
357 357
358 return true; 358 return true;
359 } 359 }
360 360
361 void AddRadioData(const RadioData& radio_data, 361 void AddRadioData(const RadioData& radio_data,
362 int age_milliseconds, 362 int age_milliseconds,
363 DictionaryValue* body_object) { 363 DictionaryValue* body_object) {
364 DCHECK(body_object); 364 DCHECK(body_object);
365 365
366 AddInteger(L"home_mobile_country_code", radio_data.home_mobile_country_code, 366 AddInteger("home_mobile_country_code", radio_data.home_mobile_country_code,
367 body_object); 367 body_object);
368 AddInteger(L"home_mobile_network_code", radio_data.home_mobile_network_code, 368 AddInteger("home_mobile_network_code", radio_data.home_mobile_network_code,
369 body_object); 369 body_object);
370 AddString(L"radio_type", 370 AddString("radio_type",
371 ASCIIToUTF16(RadioTypeToString(radio_data.radio_type)), 371 ASCIIToUTF16(RadioTypeToString(radio_data.radio_type)),
372 body_object); 372 body_object);
373 AddString(L"carrier", radio_data.carrier, body_object); 373 AddString("carrier", radio_data.carrier, body_object);
374 374
375 const int num_cell_towers = static_cast<int>(radio_data.cell_data.size()); 375 const int num_cell_towers = static_cast<int>(radio_data.cell_data.size());
376 if (num_cell_towers == 0) { 376 if (num_cell_towers == 0) {
377 return; 377 return;
378 } 378 }
379 ListValue* cell_towers = new ListValue; 379 ListValue* cell_towers = new ListValue;
380 for (int i = 0; i < num_cell_towers; ++i) { 380 for (int i = 0; i < num_cell_towers; ++i) {
381 DictionaryValue* cell_tower = new DictionaryValue; 381 DictionaryValue* cell_tower = new DictionaryValue;
382 AddInteger(L"cell_id", radio_data.cell_data[i].cell_id, cell_tower); 382 AddInteger("cell_id", radio_data.cell_data[i].cell_id, cell_tower);
383 AddInteger(L"location_area_code", 383 AddInteger("location_area_code",
384 radio_data.cell_data[i].location_area_code, cell_tower); 384 radio_data.cell_data[i].location_area_code, cell_tower);
385 AddInteger(L"mobile_country_code", 385 AddInteger("mobile_country_code",
386 radio_data.cell_data[i].mobile_country_code, cell_tower); 386 radio_data.cell_data[i].mobile_country_code, cell_tower);
387 AddInteger(L"mobile_network_code", 387 AddInteger("mobile_network_code",
388 radio_data.cell_data[i].mobile_network_code, cell_tower); 388 radio_data.cell_data[i].mobile_network_code, cell_tower);
389 AddInteger(L"age", age_milliseconds, cell_tower); 389 AddInteger("age", age_milliseconds, cell_tower);
390 AddInteger(L"signal_strength", 390 AddInteger("signal_strength",
391 radio_data.cell_data[i].radio_signal_strength, cell_tower); 391 radio_data.cell_data[i].radio_signal_strength, cell_tower);
392 AddInteger(L"timing_advance", radio_data.cell_data[i].timing_advance, 392 AddInteger("timing_advance", radio_data.cell_data[i].timing_advance,
393 cell_tower); 393 cell_tower);
394 cell_towers->Append(cell_tower); 394 cell_towers->Append(cell_tower);
395 } 395 }
396 body_object->Set(L"cell_towers", cell_towers); 396 body_object->Set("cell_towers", cell_towers);
397 } 397 }
398 398
399 void AddWifiData(const WifiData& wifi_data, 399 void AddWifiData(const WifiData& wifi_data,
400 int age_milliseconds, 400 int age_milliseconds,
401 DictionaryValue* body_object) { 401 DictionaryValue* body_object) {
402 DCHECK(body_object); 402 DCHECK(body_object);
403 403
404 if (wifi_data.access_point_data.empty()) { 404 if (wifi_data.access_point_data.empty()) {
405 return; 405 return;
406 } 406 }
407 407
408 ListValue* wifi_towers = new ListValue; 408 ListValue* wifi_towers = new ListValue;
409 for (WifiData::AccessPointDataSet::const_iterator iter = 409 for (WifiData::AccessPointDataSet::const_iterator iter =
410 wifi_data.access_point_data.begin(); 410 wifi_data.access_point_data.begin();
411 iter != wifi_data.access_point_data.end(); 411 iter != wifi_data.access_point_data.end();
412 iter++) { 412 iter++) {
413 DictionaryValue* wifi_tower = new DictionaryValue; 413 DictionaryValue* wifi_tower = new DictionaryValue;
414 AddString(L"mac_address", iter->mac_address, wifi_tower); 414 AddString("mac_address", iter->mac_address, wifi_tower);
415 AddInteger(L"signal_strength", iter->radio_signal_strength, wifi_tower); 415 AddInteger("signal_strength", iter->radio_signal_strength, wifi_tower);
416 AddInteger(L"age", age_milliseconds, wifi_tower); 416 AddInteger("age", age_milliseconds, wifi_tower);
417 AddInteger(L"channel", iter->channel, wifi_tower); 417 AddInteger("channel", iter->channel, wifi_tower);
418 AddInteger(L"signal_to_noise", iter->signal_to_noise, wifi_tower); 418 AddInteger("signal_to_noise", iter->signal_to_noise, wifi_tower);
419 AddString(L"ssid", iter->ssid, wifi_tower); 419 AddString("ssid", iter->ssid, wifi_tower);
420 wifi_towers->Append(wifi_tower); 420 wifi_towers->Append(wifi_tower);
421 } 421 }
422 body_object->Set(L"wifi_towers", wifi_towers); 422 body_object->Set("wifi_towers", wifi_towers);
423 } 423 }
424 } // namespace 424 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/geolocation/network_location_provider_unittest.cc ('k') | chrome/browser/plugin_updater.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698