OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/json/json_reader.h" |
| 6 #include "base/strings/string_number_conversions.h" |
| 7 #include "base/strings/string_split.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/test/chromedriver/chrome/mobile_device.h" |
| 10 #include "chrome/test/chromedriver/chrome/mobile_device_list.h" |
| 11 #include "chrome/test/chromedriver/chrome/status.h" |
| 12 |
| 13 MobileDevice::MobileDevice() {} |
| 14 MobileDevice::~MobileDevice() {} |
| 15 |
| 16 Status FindMobileDevice(std::string device_name, |
| 17 scoped_ptr<MobileDevice>* mobile_device) { |
| 18 base::JSONReader json_reader(base::JSON_ALLOW_TRAILING_COMMAS); |
| 19 scoped_ptr<base::Value> devices_value; |
| 20 devices_value.reset(json_reader.ReadToValue(kMobileDevices)); |
| 21 if (!devices_value.get()) |
| 22 return Status(kUnknownError, |
| 23 "could not parse mobile device list because " + |
| 24 json_reader.GetErrorMessage()); |
| 25 |
| 26 base::ListValue* mobile_devices; |
| 27 if (!devices_value->GetAsList(&mobile_devices)) |
| 28 return Status(kUnknownError, "malformed device metrics list"); |
| 29 |
| 30 for (base::ListValue::iterator it = mobile_devices->begin(); |
| 31 it != mobile_devices->end(); |
| 32 ++it) { |
| 33 base::ListValue* device = NULL; |
| 34 if (!(*it)->GetAsList(&device)) { |
| 35 return Status(kUnknownError, |
| 36 "malformed device in list: should be an array"); |
| 37 } |
| 38 |
| 39 if (device != NULL) { |
| 40 std::string name; |
| 41 if (!device->GetString(0, &name)) { |
| 42 return Status(kUnknownError, |
| 43 "malformed device name: should be a string"); |
| 44 } |
| 45 if (name != device_name) |
| 46 continue; |
| 47 |
| 48 scoped_ptr<MobileDevice> tmp_mobile_device(new MobileDevice()); |
| 49 std::string device_metrics_string; |
| 50 if (!device->GetString(1, &tmp_mobile_device->user_agent)) { |
| 51 return Status(kUnknownError, |
| 52 "malformed device user agent: should be a string"); |
| 53 } |
| 54 if (!device->GetString(2, &device_metrics_string)) { |
| 55 return Status(kUnknownError, |
| 56 "malformed device metrics: should be a string"); |
| 57 } |
| 58 std::vector<std::string> metrics_vector; |
| 59 base::SplitString(device_metrics_string, 'x', &metrics_vector); |
| 60 if (metrics_vector.size() < 3) |
| 61 return Status(kUnknownError, "malformed device metrics string"); |
| 62 |
| 63 int width = 0; |
| 64 int height = 0; |
| 65 double device_scale_factor = 0.0; |
| 66 if (!base::StringToInt(metrics_vector[0], &width)) { |
| 67 return Status(kUnknownError, |
| 68 "malformed device width: should be an integer"); |
| 69 } |
| 70 if (!base::StringToInt(metrics_vector[1], &height)) { |
| 71 return Status(kUnknownError, |
| 72 "malformed device height: should be an integer"); |
| 73 } |
| 74 if (!base::StringToDouble(metrics_vector[2], &device_scale_factor)) { |
| 75 return Status(kUnknownError, |
| 76 "malformed device scale factor: should be a double"); |
| 77 } |
| 78 tmp_mobile_device->device_metrics.reset( |
| 79 new DeviceMetrics(width, height, device_scale_factor)); |
| 80 |
| 81 *mobile_device = tmp_mobile_device.Pass(); |
| 82 return Status(kOk); |
| 83 } |
| 84 } |
| 85 |
| 86 return Status(kUnknownError, "must be a valid device"); |
| 87 } |
OLD | NEW |