| Index: third_party/WebKit/Source/core/loader/NetworkQuietDetector.cpp
|
| diff --git a/third_party/WebKit/Source/core/loader/NetworkQuietDetector.cpp b/third_party/WebKit/Source/core/loader/NetworkQuietDetector.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..729d3629a281b4aa4a66eabdd491c48ab8ec15f1
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/core/loader/NetworkQuietDetector.cpp
|
| @@ -0,0 +1,65 @@
|
| +// 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 "core/loader/NetworkQuietDetector.h"
|
| +
|
| +#include "core/dom/Document.h"
|
| +#include "core/dom/TaskRunnerHelper.h"
|
| +#include "core/frame/LocalFrame.h"
|
| +#include "core/loader/FrameFetchContext.h"
|
| +#include "platform/instrumentation/resource_coordinator/FrameResourceCoordinator.h"
|
| +#include "platform/loader/fetch/ResourceFetcher.h"
|
| +
|
| +namespace blink {
|
| +
|
| +NetworkQuietDetector::NetworkQuietDetector(Document* document)
|
| + : document_(document),
|
| + network2_quiet_timer_(
|
| + TaskRunnerHelper::Get(TaskType::kUnspecedTimer, document),
|
| + this,
|
| + &NetworkQuietDetector::Network2QuietTimerFired) {}
|
| +
|
| +int NetworkQuietDetector::ActiveConnections() {
|
| + ResourceFetcher* fetcher = document_->Fetcher();
|
| + return fetcher->BlockingRequestCount() + fetcher->NonblockingRequestCount();
|
| +}
|
| +
|
| +// This function is called when the number of active connections is decreased.
|
| +void NetworkQuietDetector::CheckNetworkStable() {
|
| + if (!document_->HasFinishedParsing())
|
| + return;
|
| +
|
| + SetNetworkQuietTimers(ActiveConnections());
|
| +}
|
| +
|
| +void NetworkQuietDetector::SetNetworkQuietTimers(int active_connections) {
|
| + if (network2_quiet_reached_ || active_connections > 2)
|
| + return;
|
| +
|
| + // If activeConnections < 2 and the timer is already running, current
|
| + // 2-quiet window continues; the timer shouldn't be restarted.
|
| + if (active_connections == 2 || !network2_quiet_timer_.IsActive()) {
|
| + network2_quiet_timer_.StartOneShot(kNetwork2QuietWindowSeconds,
|
| + BLINK_FROM_HERE);
|
| + }
|
| +}
|
| +
|
| +void NetworkQuietDetector::Network2QuietTimerFired(TimerBase*) {
|
| + if (!document_ || !document_->GetFrame() || network2_quiet_reached_ ||
|
| + ActiveConnections() > 2)
|
| + return;
|
| + network2_quiet_reached_ = true;
|
| + auto frame_resource_coordinator =
|
| + document_->GetFrame()->GetFrameResourceCoordinator();
|
| + if (frame_resource_coordinator) {
|
| + frame_resource_coordinator->SendEvent(
|
| + resource_coordinator::mojom::EventType::kOnLocalFrameNetworkIdle);
|
| + }
|
| +}
|
| +
|
| +DEFINE_TRACE(NetworkQuietDetector) {
|
| + visitor->Trace(document_);
|
| +}
|
| +
|
| +} // namespace blink
|
|
|