Skip to content

Commit

Permalink
Revert "Headless: remove devtools protocol network handler"
Browse files Browse the repository at this point in the history
This reverts commit a8d13c1.

Reason for revert: Suspected causing build failure on win32-rel:
https://ci.chromium.org/p/chromium/builders/luci.chromium.ci/win32-rel/3631

Original change's description:
> Headless: remove devtools protocol network handler
> 
> Network conditions management is now done in content, so support for
> this in headless is redundant.
> 
> Change-Id: I559de2c65e659c026b7e8e9d9021340e6271af92
> Reviewed-on: https://chromium-review.googlesource.com/1255610
> Reviewed-by: Dmitry Gozman <dgozman@chromium.org>
> Commit-Queue: Andrey Kosyakov <caseq@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#595637}

TBR=dgozman@chromium.org,caseq@chromium.org,pfeldman@chromium.org

Change-Id: I0601d25a065e06cff731df50e1dae614ee194dc8
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://chromium-review.googlesource.com/1256166
Reviewed-by: Thomas Anderson <thomasanderson@chromium.org>
Commit-Queue: Thomas Anderson <thomasanderson@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595650}
  • Loading branch information
tanderson-google authored and Commit Bot committed Oct 2, 2018
1 parent a38e472 commit e87c094
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 0 deletions.
4 changes: 4 additions & 0 deletions headless/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ jumbo_component("headless") {
"lib/browser/headless_devtools_client_impl.cc",
"lib/browser/headless_devtools_manager_delegate.cc",
"lib/browser/headless_devtools_manager_delegate.h",
"lib/browser/headless_network_conditions.cc",
"lib/browser/headless_network_conditions.h",
"lib/browser/headless_permission_manager.cc",
"lib/browser/headless_permission_manager.h",
"lib/browser/headless_platform_event_source.cc",
Expand All @@ -325,6 +327,8 @@ jumbo_component("headless") {
"lib/browser/protocol/headless_devtools_session.h",
"lib/browser/protocol/headless_handler.cc",
"lib/browser/protocol/headless_handler.h",
"lib/browser/protocol/network_handler.cc",
"lib/browser/protocol/network_handler.h",
"lib/browser/protocol/page_handler.cc",
"lib/browser/protocol/page_handler.h",
"lib/browser/protocol/protocol_string.cc",
Expand Down
9 changes: 9 additions & 0 deletions headless/lib/browser/headless_browser_context_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,15 @@ const std::string& HeadlessBrowserContextImpl::Id() const {
return UniqueId();
}

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())) {}
Expand Down
6 changes: 6 additions & 0 deletions headless/lib/browser/headless_browser_context_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "content/public/browser/global_routing_id.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"
Expand Down Expand Up @@ -109,6 +110,9 @@ class HEADLESS_EXPORT HeadlessBrowserContextImpl final
const base::UnguessableToken* GetDevToolsFrameTokenForFrameTreeNodeId(
int frame_tree_node_id) const;

void SetNetworkConditions(HeadlessNetworkConditions conditions);
HeadlessNetworkConditions GetNetworkConditions() override;

private:
HeadlessBrowserContextImpl(
HeadlessBrowserImpl* browser,
Expand Down Expand Up @@ -140,6 +144,8 @@ class HEADLESS_EXPORT HeadlessBrowserContextImpl final
std::unique_ptr<content::PermissionControllerDelegate>
permission_controller_delegate_;

HeadlessNetworkConditions network_conditions_;

DISALLOW_COPY_AND_ASSIGN(HeadlessBrowserContextImpl);
};

Expand Down
26 changes: 26 additions & 0 deletions headless/lib/browser/headless_network_conditions.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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() = default;

} // namespace headless
36 changes: 36 additions & 0 deletions headless/lib/browser/headless_network_conditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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_
2 changes: 2 additions & 0 deletions headless/lib/browser/protocol/headless_devtools_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "content/public/browser/devtools_agent_host_client.h"
#include "headless/lib/browser/protocol/browser_handler.h"
#include "headless/lib/browser/protocol/headless_handler.h"
#include "headless/lib/browser/protocol/network_handler.h"
#include "headless/lib/browser/protocol/page_handler.h"
#include "headless/lib/browser/protocol/target_handler.h"

Expand All @@ -31,6 +32,7 @@ HeadlessDevToolsSession::HeadlessDevToolsSession(
}
if (agent_host->GetType() == content::DevToolsAgentHost::kTypeBrowser)
AddHandler(std::make_unique<BrowserHandler>(browser_));
AddHandler(std::make_unique<NetworkHandler>(browser_));
AddHandler(std::make_unique<TargetHandler>(browser_));
}

Expand Down
61 changes: 61 additions & 0 deletions headless/lib/browser/protocol/network_handler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2014 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/protocol/network_handler.h"

#include "headless/lib/browser/headless_browser_context_impl.h"
#include "headless/lib/browser/headless_browser_impl.h"

namespace headless {
namespace protocol {

NetworkHandler::NetworkHandler(base::WeakPtr<HeadlessBrowserImpl> browser)
: DomainHandler(Network::Metainfo::domainName, browser) {}

NetworkHandler::~NetworkHandler() = default;

void NetworkHandler::Wire(UberDispatcher* dispatcher) {
Network::Dispatcher::wire(dispatcher, this);
}

Response NetworkHandler::EmulateNetworkConditions(
bool offline,
double latency,
double download_throughput,
double upload_throughput,
Maybe<Network::ConnectionType>) {
// Associate NetworkConditions to context
std::vector<HeadlessBrowserContext*> browser_contexts =
browser()->GetAllBrowserContexts();
HeadlessNetworkConditions conditions(HeadlessNetworkConditions(
offline, std::max(latency, 0.0), std::max(download_throughput, 0.0),
std::max(upload_throughput, 0.0)));
SetNetworkConditions(browser_contexts, conditions);
return Response::FallThrough();
}

Response NetworkHandler::Disable() {
// Can be a part of the shutdown cycle.
if (!browser())
return Response::OK();
std::vector<HeadlessBrowserContext*> browser_contexts =
browser()->GetAllBrowserContexts();
SetNetworkConditions(browser_contexts, HeadlessNetworkConditions());
return Response::FallThrough();
}

void NetworkHandler::SetNetworkConditions(
std::vector<HeadlessBrowserContext*> browser_contexts,
HeadlessNetworkConditions conditions) {
for (std::vector<HeadlessBrowserContext*>::iterator it =
browser_contexts.begin();
it != browser_contexts.end(); ++it) {
HeadlessBrowserContextImpl* context =
static_cast<HeadlessBrowserContextImpl*>(*it);
context->SetNetworkConditions(conditions);
}
}

} // namespace protocol
} // namespace headless
44 changes: 44 additions & 0 deletions headless/lib/browser/protocol/network_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2014 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_PROTOCOL_NETWORK_HANDLER_H_
#define HEADLESS_LIB_BROWSER_PROTOCOL_NETWORK_HANDLER_H_

#include "headless/lib/browser/headless_network_conditions.h"
#include "headless/lib/browser/protocol/domain_handler.h"
#include "headless/lib/browser/protocol/dp_network.h"

namespace headless {

class HeadlessBrowserContext;

namespace protocol {

class NetworkHandler : public DomainHandler, public Network::Backend {
public:
explicit NetworkHandler(base::WeakPtr<HeadlessBrowserImpl> browser);
~NetworkHandler() override;

void Wire(UberDispatcher* dispatcher) override;

// Network::Backend implementation
Response EmulateNetworkConditions(
bool offline,
double latency,
double download_throughput,
double upload_throughput,
Maybe<Network::ConnectionType> connection_type) override;
Response Disable() override;

private:
void SetNetworkConditions(
std::vector<HeadlessBrowserContext*> browser_contexts,
HeadlessNetworkConditions conditions);
DISALLOW_COPY_AND_ASSIGN(NetworkHandler);
};

} // namespace protocol
} // namespace headless

#endif // HEADLESS_LIB_BROWSER_PROTOCOL_NETWORK_HANDLER_H_
5 changes: 5 additions & 0 deletions headless/protocol_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
"domain": "HeadlessExperimental",
"async": ["beginFrame"]
},
{
"domain": "Network",
"include": ["emulateNetworkConditions", "disable"],
"include_events": []
},
{
"domain": "Page",
"include": ["printToPDF"],
Expand Down
3 changes: 3 additions & 0 deletions headless/public/headless_browser_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "base/optional.h"
#include "content/public/browser/browser_context.h"
#include "content/public/common/web_preferences.h"
#include "headless/lib/browser/headless_network_conditions.h"
#include "headless/public/headless_export.h"
#include "headless/public/headless_web_contents.h"
#include "net/proxy_resolution/proxy_resolution_service.h"
Expand Down Expand Up @@ -64,6 +65,8 @@ class HEADLESS_EXPORT HeadlessBrowserContext {
// GUID for this browser context.
virtual const std::string& Id() const = 0;

virtual HeadlessNetworkConditions GetNetworkConditions() = 0;

// TODO(skyostil): Allow saving and restoring contexts (crbug.com/617931).

protected:
Expand Down

0 comments on commit e87c094

Please sign in to comment.