| Index: chrome/test/chromedriver/chrome/network_conditions_override_manager.cc
|
| diff --git a/chrome/test/chromedriver/chrome/network_conditions_override_manager.cc b/chrome/test/chromedriver/chrome/network_conditions_override_manager.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..404e5eefd8109c62c0e9e2abef882a4350e95b79
|
| --- /dev/null
|
| +++ b/chrome/test/chromedriver/chrome/network_conditions_override_manager.cc
|
| @@ -0,0 +1,70 @@
|
| +// Copyright (c) 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/test/chromedriver/chrome/network_conditions_override_manager.h"
|
| +
|
| +#include "base/values.h"
|
| +#include "chrome/test/chromedriver/chrome/devtools_client.h"
|
| +#include "chrome/test/chromedriver/chrome/network_conditions.h"
|
| +#include "chrome/test/chromedriver/chrome/status.h"
|
| +
|
| +NetworkConditionsOverrideManager::NetworkConditionsOverrideManager(
|
| + DevToolsClient* client) : client_(client) {
|
| + client_->AddListener(this);
|
| +}
|
| +
|
| +NetworkConditionsOverrideManager::~NetworkConditionsOverrideManager() {
|
| +}
|
| +
|
| +Status NetworkConditionsOverrideManager::OverrideNetworkConditions(
|
| + const NetworkConditions& network_conditions) {
|
| + overridden_network_conditions_.reset(
|
| + new NetworkConditions(network_conditions));
|
| + return ApplyOverrideIfNeeded();
|
| +}
|
| +
|
| +Status NetworkConditionsOverrideManager::OnConnected(DevToolsClient* client) {
|
| + return ApplyOverrideIfNeeded();
|
| +}
|
| +
|
| +Status NetworkConditionsOverrideManager::OnEvent(
|
| + DevToolsClient* client,
|
| + const std::string& method,
|
| + const base::DictionaryValue& params) {
|
| + if (method == "Page.frameNavigated") {
|
| + const base::Value* unused_value;
|
| + if (!params.Get("frame.parentId", &unused_value))
|
| + return ApplyOverrideIfNeeded();
|
| + }
|
| + return Status(kOk);
|
| +}
|
| +
|
| +Status NetworkConditionsOverrideManager::ApplyOverrideIfNeeded() {
|
| + if (!overridden_network_conditions_)
|
| + return Status(kOk);
|
| +
|
| + base::DictionaryValue params, empty_params;
|
| + params.SetBoolean("offline", overridden_network_conditions_->offline);
|
| + params.SetDouble("latency", overridden_network_conditions_->latency);
|
| + params.SetDouble("downloadThroughput",
|
| + overridden_network_conditions_->download_throughput);
|
| + params.SetDouble("uploadThroughput",
|
| + overridden_network_conditions_->upload_throughput);
|
| +
|
| + Status status = client_->SendCommand("Network.enable", empty_params);
|
| + if (status.IsError())
|
| + return status;
|
| +
|
| + scoped_ptr<base::DictionaryValue> result;
|
| + bool can;
|
| + status = client_->SendCommandAndGetResult(
|
| + "Network.canEmulateNetworkConditions", empty_params, &result);
|
| + if (status.IsError() || !result->GetBoolean("result", &can))
|
| + return Status(kUnknownError,
|
| + "unable to detect if chrome can emulate network conditions", status);
|
| + if (!can)
|
| + return Status(kUnknownError, "Cannot EmulateNetworkConditions");
|
| +
|
| + return client_->SendCommand("Network.emulateNetworkConditions", params);
|
| +}
|
|
|