Chromium Code Reviews| Index: chrome/test/chromedriver/chrome/network_conditions.cc | 
| diff --git a/chrome/test/chromedriver/chrome/network_conditions.cc b/chrome/test/chromedriver/chrome/network_conditions.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..db8257575123c46aad33b44576bf5f0721a53cd0 | 
| --- /dev/null | 
| +++ b/chrome/test/chromedriver/chrome/network_conditions.cc | 
| @@ -0,0 +1,83 @@ | 
| +// Copyright 2014 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include "base/json/json_reader.h" | 
| +#include "base/strings/string_number_conversions.h" | 
| +#include "base/strings/string_split.h" | 
| +#include "base/values.h" | 
| +#include "chrome/test/chromedriver/chrome/network_conditions.h" | 
| 
 
samuong
2015/03/13 22:30:51
nit: the #include ".../network_conditions.h" shoul
 
srawlins
2015/03/16 18:26:28
Done.
 
 | 
| +#include "chrome/test/chromedriver/chrome/network_list.h" | 
| +#include "chrome/test/chromedriver/chrome/status.h" | 
| + | 
| +NetworkConditions::NetworkConditions() {} | 
| +NetworkConditions::NetworkConditions( | 
| + bool offline, double latency, double download_throughput, | 
| + double upload_throughput) | 
| + : offline(offline), | 
| + latency(latency), | 
| + download_throughput(download_throughput), | 
| + upload_throughput(upload_throughput) {} | 
| +NetworkConditions::~NetworkConditions() {} | 
| + | 
| +Status FindPresetNetwork(std::string network_name, | 
| + scoped_ptr<NetworkConditions>* network_conditions) { | 
| + base::JSONReader json_reader(base::JSON_ALLOW_TRAILING_COMMAS); | 
| + scoped_ptr<base::Value> networks_value; | 
| + networks_value.reset(json_reader.ReadToValue(kNetworks)); | 
| + if (!networks_value.get()) | 
| + return Status(kUnknownError, | 
| + "could not parse network list because " + | 
| + json_reader.GetErrorMessage()); | 
| + | 
| + base::ListValue* networks; | 
| + if (!networks_value->GetAsList(&networks)) | 
| + return Status(kUnknownError, "malformed networks list"); | 
| + | 
| + for (base::ListValue::iterator it = networks->begin(); | 
| + it != networks->end(); | 
| + ++it) { | 
| + base::DictionaryValue* network = NULL; | 
| + if (!(*it)->GetAsDictionary(&network)) { | 
| + return Status(kUnknownError, | 
| + "malformed network in list: should be a dictionary"); | 
| + } | 
| + | 
| + if (network == NULL) | 
| + continue; | 
| + | 
| + std::string title; | 
| + if (!network->GetString("title", &title)) { | 
| + return Status(kUnknownError, | 
| + "malformed network title: should be a string"); | 
| + } | 
| + if (title != network_name) | 
| + continue; | 
| 
 
samuong
2015/03/13 22:30:51
is there any reason not to use base::ListValue::Fi
 
srawlins
2015/03/16 18:26:28
I don't know how I would use ListValue::Find() her
 
samuong
2015/03/16 18:43:10
Oh right, yeah too bad this isn't a dictionary. OK
 
 | 
| + | 
| + NetworkConditions tmp_network; | 
| + if (!network->GetDouble("latency", &tmp_network.latency)) { | 
| + return Status(kUnknownError, | 
| + "malformed network latency: should be a double"); | 
| + } | 
| + // Preset list maintains a single "throughput" attribute for each network, | 
| + // so we use that value for both |download_throughput| and | 
| + // |upload_throughput| in the NetworkConditions (as does Chrome). | 
| + if (!network->GetDouble("throughput", &tmp_network.download_throughput) || | 
| + !network->GetDouble("throughput", &tmp_network.upload_throughput)) { | 
| + return Status(kUnknownError, | 
| + "malformed network throughput: should be a double"); | 
| + } | 
| + | 
| + // The throughputs of the network presets are listed in kbps, but must be | 
| + // supplied to the OverrideNetworkConditions command as bps. | 
| + tmp_network.download_throughput *= 1024; | 
| + tmp_network.upload_throughput *= 1024; | 
| + | 
| + // |offline| is always false for now. | 
| 
 
samuong
2015/03/13 22:30:51
is |offline| also false when network_name == "offl
 
srawlins
2015/03/16 18:26:28
No, I tried this before :( Still no communication
 
 | 
| + tmp_network.offline = false; | 
| + network_conditions->reset(new NetworkConditions(tmp_network)); | 
| + return Status(kOk); | 
| + } | 
| + | 
| + return Status(kUnknownError, "must be a valid network"); | 
| +} |