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..afe259a31226fc001332fcac30961615a0bf97de |
--- /dev/null |
+++ b/chrome/test/chromedriver/chrome/network_conditions_override_manager.cc |
@@ -0,0 +1,71 @@ |
+// 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), |
+ overridden_network_conditions_(NULL) { |
+ client_->AddListener(this); |
+} |
+ |
+NetworkConditionsOverrideManager::~NetworkConditionsOverrideManager() { |
+} |
+ |
+Status NetworkConditionsOverrideManager::OverrideNetworkConditions( |
+ const NetworkConditions& network_conditions) { |
+ overridden_network_conditions_ = &network_conditions; |
+ 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
|
+} |
+ |
+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); |
+} |