Commit b697dcff authored by Iris Uy's avatar Iris Uy Committed by Commit Bot

Store emulated network conditions in HeadlessBrowserContextImpl.

This is needed so that the network conditions can be visibile to
GenericURLRequestJob in headless/public/util. We need to store
it in the HeadlessBrowserContextImpl, since GenericURLRequestJob
does not reuse the network stack (e.g. DevToolsNetworkTransaction,
etc).

Bug: 
Change-Id: I531acda73bffad2e504efbfbaa4fd8df2ef4b23e
Reviewed-on: https://chromium-review.googlesource.com/696887
Commit-Queue: Iris Uy <irisu@chromium.org>
Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Reviewed-by: default avatarDavid Vallet <dvallet@chromium.org>
Cr-Commit-Position: refs/heads/master@{#506980}
parent 15303d0c
......@@ -274,6 +274,8 @@ component("headless") {
"lib/browser/headless_devtools_manager_delegate.h",
"lib/browser/headless_net_log.cc",
"lib/browser/headless_net_log.h",
"lib/browser/headless_network_conditions.cc",
"lib/browser/headless_network_conditions.h",
"lib/browser/headless_network_delegate.cc",
"lib/browser/headless_network_delegate.h",
"lib/browser/headless_permission_manager.cc",
......
......@@ -383,6 +383,15 @@ void HeadlessBrowserContextImpl::NotifyUrlRequestFailed(
observer.UrlRequestFailed(request, net_error, canceled_by_devtools);
}
void HeadlessBrowserContextImpl::SetNetworkConditions(
HeadlessNetworkConditions conditions) {
network_conditions_ = conditions;
}
HeadlessNetworkConditions HeadlessBrowserContextImpl::GetNetworkConditions() {
return network_conditions_;
}
HeadlessBrowserContext::Builder::Builder(HeadlessBrowserImpl* browser)
: browser_(browser),
options_(new HeadlessBrowserContextOptions(browser->options())),
......
......@@ -15,6 +15,7 @@
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/resource_context.h"
#include "headless/lib/browser/headless_browser_context_options.h"
#include "headless/lib/browser/headless_network_conditions.h"
#include "headless/lib/browser/headless_url_request_context_getter.h"
#include "headless/public/headless_browser.h"
#include "headless/public/headless_browser_context.h"
......@@ -109,6 +110,9 @@ class HeadlessBrowserContextImpl : public HeadlessBrowserContext,
int net_error,
bool canceled_by_devtools);
void SetNetworkConditions(HeadlessNetworkConditions conditions);
HeadlessNetworkConditions GetNetworkConditions();
private:
HeadlessBrowserContextImpl(
HeadlessBrowserImpl* browser,
......@@ -139,6 +143,8 @@ class HeadlessBrowserContextImpl : public HeadlessBrowserContext,
std::string id_;
HeadlessNetworkConditions network_conditions_;
DISALLOW_COPY_AND_ASSIGN(HeadlessBrowserContextImpl);
};
......
......@@ -16,6 +16,7 @@
#include "headless/grit/headless_lib_resources.h"
#include "headless/lib/browser/headless_browser_context_impl.h"
#include "headless/lib/browser/headless_browser_impl.h"
#include "headless/lib/browser/headless_network_conditions.h"
#include "headless/lib/browser/headless_web_contents_impl.h"
#include "headless/public/devtools/domains/target.h"
#include "printing/units.h"
......@@ -200,6 +201,12 @@ HeadlessDevToolsManagerDelegate::HeadlessDevToolsManagerDelegate(
base::Bind(&HeadlessDevToolsManagerDelegate::SetWindowBounds,
base::Unretained(this));
unhandled_command_map_["Network.emulateNetworkConditions"] =
base::Bind(&HeadlessDevToolsManagerDelegate::EmulateNetworkConditions,
base::Unretained(this));
unhandled_command_map_["Network.disable"] = base::Bind(
&HeadlessDevToolsManagerDelegate::NetworkDisable, base::Unretained(this));
async_command_map_["Page.printToPDF"] = base::Bind(
&HeadlessDevToolsManagerDelegate::PrintToPDF, base::Unretained(this));
}
......@@ -220,17 +227,24 @@ bool HeadlessDevToolsManagerDelegate::HandleCommand(
if (!command->GetInteger("id", &id) || !command->GetString("method", &method))
return false;
const base::DictionaryValue* params = nullptr;
command->GetDictionary("params", &params);
auto find_it = command_map_.find(method);
if (find_it == command_map_.end())
if (find_it == command_map_.end()) {
// Check for any commands that are actioned then passed on to devtools to
// handle.
find_it = unhandled_command_map_.find(method);
if (find_it != unhandled_command_map_.end())
find_it->second.Run(id, params);
return false;
}
// Handle Browser domain commands only from Browser DevToolsAgentHost.
if (method.find("Browser.") == 0 &&
agent_host->GetType() != content::DevToolsAgentHost::kTypeBrowser)
return false;
const base::DictionaryValue* params = nullptr;
command->GetDictionary("params", &params);
auto cmd_result = find_it->second.Run(id, params);
if (!cmd_result)
return false;
......@@ -530,4 +544,36 @@ HeadlessDevToolsManagerDelegate::SetWindowBounds(
return CreateSuccessResponse(command_id, nullptr);
}
std::unique_ptr<base::DictionaryValue>
HeadlessDevToolsManagerDelegate::EmulateNetworkConditions(
int command_id,
const base::DictionaryValue* params) {
// Associate NetworkConditions to context
HeadlessBrowserContextImpl* browser_context =
static_cast<HeadlessBrowserContextImpl*>(
browser_->GetDefaultBrowserContext());
bool offline = false;
double latency = 0, download_throughput = 0, upload_throughput = 0;
params->GetBoolean("offline", &offline);
params->GetDouble("latency", &latency);
params->GetDouble("downloadThroughput", &download_throughput);
params->GetDouble("uploadThroughput", &upload_throughput);
HeadlessNetworkConditions conditions(HeadlessNetworkConditions(
offline, std::max(latency, 0.0), std::max(download_throughput, 0.0),
std::max(upload_throughput, 0.0)));
browser_context->SetNetworkConditions(conditions);
return CreateSuccessResponse(command_id, nullptr);
}
std::unique_ptr<base::DictionaryValue>
HeadlessDevToolsManagerDelegate::NetworkDisable(
int command_id,
const base::DictionaryValue* params) {
HeadlessBrowserContextImpl* browser_context =
static_cast<HeadlessBrowserContextImpl*>(
browser_->GetDefaultBrowserContext());
browser_context->SetNetworkConditions(HeadlessNetworkConditions());
return CreateSuccessResponse(command_id, nullptr);
}
} // namespace headless
......@@ -73,6 +73,13 @@ class HeadlessDevToolsManagerDelegate
std::unique_ptr<base::DictionaryValue> SetWindowBounds(
int command_id,
const base::DictionaryValue* params);
std::unique_ptr<base::DictionaryValue> EmulateNetworkConditions(
int command_id,
const base::DictionaryValue* params);
std::unique_ptr<base::DictionaryValue> NetworkDisable(
int command_id,
const base::DictionaryValue* params);
void PrintToPDF(content::DevToolsAgentHost* agent_host,
int command_id,
const base::DictionaryValue* params,
......@@ -91,6 +98,9 @@ class HeadlessDevToolsManagerDelegate
const CommandCallback& callback)>;
std::map<std::string, CommandMemberCallback> command_map_;
std::map<std::string, AsyncCommandMemberCallback> async_command_map_;
// These commands are passed on for devtools to handle.
std::map<std::string, CommandMemberCallback> unhandled_command_map_;
};
} // namespace headless
......
// Copyright 2017 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 "headless/lib/browser/headless_network_conditions.h"
namespace headless {
HeadlessNetworkConditions::HeadlessNetworkConditions()
: HeadlessNetworkConditions(false) {}
HeadlessNetworkConditions::HeadlessNetworkConditions(bool offline)
: HeadlessNetworkConditions(offline, 0, 0, 0) {}
HeadlessNetworkConditions::HeadlessNetworkConditions(bool offline,
double latency,
double download_throughput,
double upload_throughput)
: offline(offline),
latency(latency),
download_throughput(download_throughput),
upload_throughput(upload_throughput) {}
HeadlessNetworkConditions::~HeadlessNetworkConditions() {}
} // namespace headless
// Copyright 2017 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.
#ifndef HEADLESS_LIB_BROWSER_HEADLESS_NETWORK_CONDITIONS_H_
#define HEADLESS_LIB_BROWSER_HEADLESS_NETWORK_CONDITIONS_H_
#include <string>
#include <vector>
#include "base/macros.h"
#include "headless/public/headless_export.h"
namespace headless {
// HeadlessNetworkConditions holds information about desired network conditions.
struct HEADLESS_EXPORT HeadlessNetworkConditions {
HeadlessNetworkConditions();
~HeadlessNetworkConditions();
explicit HeadlessNetworkConditions(bool offline);
HeadlessNetworkConditions(bool offline,
double latency,
double download_throughput,
double upload_throughput);
bool offline;
double latency;
double download_throughput;
double upload_throughput;
};
} // namespace headless
#endif // HEADLESS_LIB_BROWSER_HEADLESS_NETWORK_CONDITIONS_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment