OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015 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_override_manager.h" | |
6 | |
7 #include "base/values.h" | |
8 #include "chrome/test/chromedriver/chrome/devtools_client.h" | |
9 #include "chrome/test/chromedriver/chrome/network_conditions.h" | |
10 #include "chrome/test/chromedriver/chrome/status.h" | |
11 | |
12 NetworkConditionsOverrideManager::NetworkConditionsOverrideManager( | |
13 DevToolsClient* client) | |
14 : client_(client), | |
15 overridden_network_conditions_(NULL) { | |
16 client_->AddListener(this); | |
17 } | |
18 | |
19 NetworkConditionsOverrideManager::~NetworkConditionsOverrideManager() { | |
20 } | |
21 | |
22 Status NetworkConditionsOverrideManager::OverrideNetworkConditions( | |
23 const NetworkConditions& network_conditions) { | |
24 Status status = ApplyOverride(&network_conditions); | |
25 if (status.IsOk()) | |
26 overridden_network_conditions_ = &network_conditions; | |
27 | |
samuong
2015/03/05 23:43:20
nit: delete blank line
srawlins
2015/03/05 23:50:22
Done.
| |
28 return status; | |
29 } | |
30 | |
31 Status NetworkConditionsOverrideManager::OnConnected(DevToolsClient* client) { | |
32 return ApplyOverrideIfNeeded(); | |
33 } | |
34 | |
35 Status NetworkConditionsOverrideManager::OnEvent( | |
36 DevToolsClient* client, | |
37 const std::string& method, | |
38 const base::DictionaryValue& params) { | |
39 if (method == "Page.frameNavigated") { | |
40 const base::Value* unused_value; | |
41 if (!params.Get("frame.parentId", &unused_value)) | |
42 return ApplyOverrideIfNeeded(); | |
43 } | |
44 return Status(kOk); | |
45 } | |
46 | |
47 Status NetworkConditionsOverrideManager::ApplyOverrideIfNeeded() { | |
48 if (overridden_network_conditions_) | |
49 return ApplyOverride(overridden_network_conditions_); | |
50 | |
samuong
2015/03/05 23:43:20
nit: delete blank line
srawlins
2015/03/05 23:50:22
Done.
| |
51 return Status(kOk); | |
52 } | |
53 | |
54 Status NetworkConditionsOverrideManager::ApplyOverride( | |
55 const NetworkConditions* network_conditions) { | |
56 | |
samuong
2015/03/05 23:43:20
nit: delete blank line
srawlins
2015/03/05 23:50:22
Done.
| |
57 base::DictionaryValue params, empty_params; | |
58 params.SetBoolean("offline", network_conditions->offline); | |
59 params.SetDouble("latency", network_conditions->latency); | |
60 params.SetDouble("downloadThroughput", | |
61 network_conditions->download_throughput); | |
62 params.SetDouble("uploadThroughput", network_conditions->upload_throughput); | |
63 | |
64 Status status = client_->SendCommand("Network.enable", empty_params); | |
65 if (status.IsError()) | |
66 return status; | |
67 | |
68 scoped_ptr<base::DictionaryValue> result; | |
69 bool can; | |
70 status = client_->SendCommandAndGetResult( | |
71 "Network.canEmulateNetworkConditions", empty_params, &result); | |
72 if (status.IsError() || !result->GetBoolean("result", &can)) | |
73 return Status(kUnknownError, | |
74 "unable to detect if chrome can emulate network conditions", status); | |
75 if (!can) | |
76 return Status(kUnknownError, "Cannot emulate network conditions"); | |
77 | |
78 return client_->SendCommand("Network.emulateNetworkConditions", params); | |
79 } | |
OLD | NEW |