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 overridden_network_conditions_ = &network_conditions; | |
25 return ApplyOverrideIfNeeded(); | |
samuong
2015/02/26 23:07:44
I find it a bit strange that NetworkConditionsOver
srawlins
2015/03/04 00:34:29
ApplyOverrideIfNeeded is also used in the OnConnec
samuong
2015/03/04 06:37:57
Sure. I think we're on the same page here, except
| |
26 } | |
27 | |
28 Status NetworkConditionsOverrideManager::OnConnected(DevToolsClient* client) { | |
29 return ApplyOverrideIfNeeded(); | |
30 } | |
31 | |
32 Status NetworkConditionsOverrideManager::OnEvent( | |
33 DevToolsClient* client, | |
34 const std::string& method, | |
35 const base::DictionaryValue& params) { | |
36 if (method == "Page.frameNavigated") { | |
37 const base::Value* unused_value; | |
38 if (!params.Get("frame.parentId", &unused_value)) | |
39 return ApplyOverrideIfNeeded(); | |
40 } | |
41 return Status(kOk); | |
42 } | |
43 | |
44 Status NetworkConditionsOverrideManager::ApplyOverrideIfNeeded() { | |
45 if (!overridden_network_conditions_) | |
46 return Status(kOk); | |
47 | |
48 base::DictionaryValue params, empty_params; | |
49 params.SetBoolean("offline", overridden_network_conditions_->offline); | |
50 params.SetDouble("latency", overridden_network_conditions_->latency); | |
51 params.SetDouble("downloadThroughput", | |
52 overridden_network_conditions_->download_throughput); | |
53 params.SetDouble("uploadThroughput", | |
54 overridden_network_conditions_->upload_throughput); | |
55 | |
56 Status status = client_->SendCommand("Network.enable", empty_params); | |
57 if (status.IsError()) | |
58 return status; | |
59 | |
60 scoped_ptr<base::DictionaryValue> result; | |
61 bool can; | |
62 status = client_->SendCommandAndGetResult( | |
63 "Network.canEmulateNetworkConditions", empty_params, &result); | |
64 if (status.IsError() || !result->GetBoolean("result", &can)) | |
65 return Status(kUnknownError, | |
66 "unable to detect if chrome can emulate network conditions", status); | |
67 if (!can) | |
68 return Status(kUnknownError, "Cannot EmulateNetworkConditions"); | |
69 | |
70 return client_->SendCommand("Network.emulateNetworkConditions", params); | |
71 } | |
OLD | NEW |