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