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) : client_(client) { |
| 14 client_->AddListener(this); |
| 15 } |
| 16 |
| 17 NetworkConditionsOverrideManager::~NetworkConditionsOverrideManager() { |
| 18 } |
| 19 |
| 20 Status NetworkConditionsOverrideManager::OverrideNetworkConditions( |
| 21 const NetworkConditions& network_conditions) { |
| 22 overridden_network_conditions_.reset( |
| 23 new NetworkConditions(network_conditions)); |
| 24 return ApplyOverrideIfNeeded(); |
| 25 } |
| 26 |
| 27 Status NetworkConditionsOverrideManager::OnConnected(DevToolsClient* client) { |
| 28 return ApplyOverrideIfNeeded(); |
| 29 } |
| 30 |
| 31 Status NetworkConditionsOverrideManager::OnEvent( |
| 32 DevToolsClient* client, |
| 33 const std::string& method, |
| 34 const base::DictionaryValue& params) { |
| 35 if (method == "Page.frameNavigated") { |
| 36 const base::Value* unused_value; |
| 37 if (!params.Get("frame.parentId", &unused_value)) |
| 38 return ApplyOverrideIfNeeded(); |
| 39 } |
| 40 return Status(kOk); |
| 41 } |
| 42 |
| 43 Status NetworkConditionsOverrideManager::ApplyOverrideIfNeeded() { |
| 44 if (!overridden_network_conditions_) |
| 45 return Status(kOk); |
| 46 |
| 47 base::DictionaryValue params, empty_params; |
| 48 params.SetBoolean("offline", overridden_network_conditions_->offline); |
| 49 params.SetDouble("latency", overridden_network_conditions_->latency); |
| 50 params.SetDouble("downloadThroughput", |
| 51 overridden_network_conditions_->download_throughput); |
| 52 params.SetDouble("uploadThroughput", |
| 53 overridden_network_conditions_->upload_throughput); |
| 54 |
| 55 Status status = client_->SendCommand("Network.enable", empty_params); |
| 56 if (status.IsError()) |
| 57 return status; |
| 58 |
| 59 scoped_ptr<base::DictionaryValue> result; |
| 60 bool can; |
| 61 status = client_->SendCommandAndGetResult( |
| 62 "Network.canEmulateNetworkConditions", empty_params, &result); |
| 63 if (status.IsError() || !result->GetBoolean("result", &can)) |
| 64 return Status(kUnknownError, |
| 65 "unable to detect if chrome can emulate network conditions", status); |
| 66 if (!can) |
| 67 return Status(kUnknownError, "Cannot EmulateNetworkConditions"); |
| 68 |
| 69 return client_->SendCommand("Network.emulateNetworkConditions", params); |
| 70 } |
OLD | NEW |