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