forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a --deterministic-fetch flag to headless_shell
Adds a -deterministic-fetch flag to headless_shell which causes network reuests to complete in the same order they're created in. This removes a significant source of network related non-determinism at the cost of slower page loads. BUG=546953 Review-Url: https://codereview.chromium.org/2352663003 Cr-Commit-Position: refs/heads/master@{#420657}
- Loading branch information
alexclarke
authored and
Commit bot
committed
Sep 23, 2016
1 parent
330ba42
commit ad54054
Showing
14 changed files
with
429 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
headless/public/util/deterministic_http_protocol_handler.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2016 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/public/util/deterministic_http_protocol_handler.h" | ||
|
||
#include "base/macros.h" | ||
#include "base/memory/ptr_util.h" | ||
#include "headless/public/headless_browser_context.h" | ||
#include "headless/public/util/deterministic_dispatcher.h" | ||
#include "headless/public/util/generic_url_request_job.h" | ||
#include "headless/public/util/http_url_fetcher.h" | ||
#include "net/url_request/url_request_context.h" | ||
#include "net/url_request/url_request_job_factory_impl.h" | ||
|
||
namespace headless { | ||
|
||
class DeterministicHttpProtocolHandler::NopGenericURLRequestJobDelegate | ||
: public GenericURLRequestJob::Delegate { | ||
public: | ||
NopGenericURLRequestJobDelegate() {} | ||
~NopGenericURLRequestJobDelegate() override {} | ||
|
||
// GenericURLRequestJob::Delegate methods: | ||
bool BlockOrRewriteRequest( | ||
const GURL& url, | ||
const std::string& method, | ||
const std::string& referrer, | ||
GenericURLRequestJob::RewriteCallback callback) override { | ||
return false; | ||
} | ||
|
||
const GenericURLRequestJob::HttpResponse* MaybeMatchResource( | ||
const GURL& url, | ||
const std::string& method, | ||
const net::HttpRequestHeaders& request_headers) override { | ||
return nullptr; | ||
} | ||
|
||
void OnResourceLoadComplete(const GURL& final_url, | ||
const std::string& mime_type, | ||
int http_response_code) override {} | ||
|
||
private: | ||
DISALLOW_COPY_AND_ASSIGN(NopGenericURLRequestJobDelegate); | ||
}; | ||
|
||
DeterministicHttpProtocolHandler::DeterministicHttpProtocolHandler( | ||
DeterministicDispatcher* deterministic_dispatcher, | ||
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) | ||
: deterministic_dispatcher_(deterministic_dispatcher), | ||
io_task_runner_(io_task_runner), | ||
nop_delegate_(new NopGenericURLRequestJobDelegate()) {} | ||
|
||
DeterministicHttpProtocolHandler::~DeterministicHttpProtocolHandler() { | ||
if (url_request_context_) | ||
io_task_runner_->DeleteSoon(FROM_HERE, url_request_context_.release()); | ||
if (url_request_job_factory_) | ||
io_task_runner_->DeleteSoon(FROM_HERE, url_request_job_factory_.release()); | ||
} | ||
|
||
net::URLRequestJob* DeterministicHttpProtocolHandler::MaybeCreateJob( | ||
net::URLRequest* request, | ||
net::NetworkDelegate* network_delegate) const { | ||
if (!url_request_context_) { | ||
DCHECK(io_task_runner_->BelongsToCurrentThread()); | ||
// Create our own URLRequestContext with an empty URLRequestJobFactoryImpl | ||
// which lets us use the default http(s) RequestJobs. | ||
url_request_context_.reset(new net::URLRequestContext()); | ||
url_request_context_->CopyFrom(request->context()); | ||
url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl()); | ||
url_request_context_->set_job_factory(url_request_job_factory_.get()); | ||
} | ||
return new GenericURLRequestJob( | ||
request, network_delegate, deterministic_dispatcher_, | ||
base::MakeUnique<HttpURLFetcher>(url_request_context_.get()), | ||
nop_delegate_.get()); | ||
} | ||
|
||
} // namespace headless |
59 changes: 59 additions & 0 deletions
59
headless/public/util/deterministic_http_protocol_handler.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2016 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_PUBLIC_UTIL_DETERMINISTIC_HTTP_PROTOCOL_HANDLER_H_ | ||
#define HEADLESS_PUBLIC_UTIL_DETERMINISTIC_HTTP_PROTOCOL_HANDLER_H_ | ||
|
||
#include <memory> | ||
|
||
#include "base/single_thread_task_runner.h" | ||
#include "net/url_request/url_request_job_factory.h" | ||
|
||
namespace net { | ||
class URLRequestContext; | ||
class URLRequestJobFactory; | ||
} // namespace | ||
|
||
namespace headless { | ||
class DeterministicDispatcher; | ||
class HeadlessBrowserContext; | ||
|
||
// A deterministic protocol handler. Requests made to this protocol handler | ||
// will return in order of creation, regardless of what order the network | ||
// returns them in. This helps remove one large source of network related | ||
// non determinism at the cost of slower page loads. | ||
class DeterministicHttpProtocolHandler | ||
: public net::URLRequestJobFactory::ProtocolHandler { | ||
public: | ||
// Note |deterministic_dispatcher| is expected to be shared across a number of | ||
// protocol handlers, e.g. for http & https protocols. | ||
DeterministicHttpProtocolHandler( | ||
DeterministicDispatcher* deterministic_dispatcher, | ||
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); | ||
~DeterministicHttpProtocolHandler() override; | ||
|
||
net::URLRequestJob* MaybeCreateJob( | ||
net::URLRequest* request, | ||
net::NetworkDelegate* network_delegate) const override; | ||
|
||
private: | ||
class NopGenericURLRequestJobDelegate; | ||
|
||
DeterministicDispatcher* deterministic_dispatcher_; // NOT OWNED. | ||
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | ||
std::unique_ptr<NopGenericURLRequestJobDelegate> nop_delegate_; | ||
|
||
// |url_request_context_| and |url_request_job_factory_| are lazily created on | ||
// the IO thread. The URLRequestContext is setup to bypass any user-specified | ||
// protocol handlers including this one. This is necessary to actually fetch | ||
// http resources. | ||
mutable std::unique_ptr<net::URLRequestContext> url_request_context_; | ||
mutable std::unique_ptr<net::URLRequestJobFactory> url_request_job_factory_; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(DeterministicHttpProtocolHandler); | ||
}; | ||
|
||
} // namespace headless | ||
|
||
#endif // HEADLESS_PUBLIC_UTIL_DETERMINISTIC_HTTP_PROTOCOL_HANDLER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.