OLD | NEW |
(Empty) | |
| 1 // Copyright 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 "chrome/test/chromedriver/chrome/network_conditions.h" |
| 6 |
| 7 #include "base/json/json_reader.h" |
| 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_split.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/test/chromedriver/chrome/network_list.h" |
| 12 #include "chrome/test/chromedriver/chrome/status.h" |
| 13 |
| 14 NetworkConditions::NetworkConditions() {} |
| 15 NetworkConditions::NetworkConditions( |
| 16 bool offline, double latency, double download_throughput, |
| 17 double upload_throughput) |
| 18 : offline(offline), |
| 19 latency(latency), |
| 20 download_throughput(download_throughput), |
| 21 upload_throughput(upload_throughput) {} |
| 22 NetworkConditions::~NetworkConditions() {} |
| 23 |
| 24 Status FindPresetNetwork(std::string network_name, |
| 25 NetworkConditions* network_conditions) { |
| 26 base::JSONReader json_reader(base::JSON_ALLOW_TRAILING_COMMAS); |
| 27 scoped_ptr<base::Value> networks_value; |
| 28 networks_value.reset(json_reader.ReadToValue(kNetworks)); |
| 29 if (!networks_value.get()) |
| 30 return Status(kUnknownError, |
| 31 "could not parse network list because " + |
| 32 json_reader.GetErrorMessage()); |
| 33 |
| 34 base::ListValue* networks; |
| 35 if (!networks_value->GetAsList(&networks)) |
| 36 return Status(kUnknownError, "malformed networks list"); |
| 37 |
| 38 for (base::ListValue::iterator it = networks->begin(); |
| 39 it != networks->end(); |
| 40 ++it) { |
| 41 |
| 42 base::DictionaryValue* network = NULL; |
| 43 if (!(*it)->GetAsDictionary(&network)) { |
| 44 return Status(kUnknownError, |
| 45 "malformed network in list: should be a dictionary"); |
| 46 } |
| 47 |
| 48 if (network == NULL) |
| 49 continue; |
| 50 |
| 51 std::string title; |
| 52 if (!network->GetString("title", &title)) { |
| 53 return Status(kUnknownError, |
| 54 "malformed network title: should be a string"); |
| 55 } |
| 56 if (title != network_name) |
| 57 continue; |
| 58 |
| 59 if (!network->GetDouble("latency", &network_conditions->latency)) { |
| 60 return Status(kUnknownError, |
| 61 "malformed network latency: should be a double"); |
| 62 } |
| 63 // Preset list maintains a single "throughput" attribute for each network, |
| 64 // so we use that value for both |download_throughput| and |
| 65 // |upload_throughput| in the NetworkConditions (as does Chrome). |
| 66 if (!network->GetDouble("throughput", |
| 67 &network_conditions->download_throughput) || |
| 68 !network->GetDouble("throughput", |
| 69 &network_conditions->upload_throughput)) { |
| 70 return Status(kUnknownError, |
| 71 "malformed network throughput: should be a double"); |
| 72 } |
| 73 |
| 74 // The throughputs of the network presets are listed in kbps, but must be |
| 75 // supplied to the OverrideNetworkConditions command as bps. |
| 76 network_conditions->download_throughput *= 1024; |
| 77 network_conditions->upload_throughput *= 1024; |
| 78 |
| 79 // |offline| is always false for now. |
| 80 network_conditions->offline = false; |
| 81 return Status(kOk); |
| 82 } |
| 83 |
| 84 return Status(kUnknownError, "must be a valid network"); |
| 85 } |
OLD | NEW |