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..f2e14439130a365cdf0eb3b3cf0ceea0e76f916c |
--- /dev/null |
+++ b/third_party/WebKit/Source/core/loader/NetworkQuietDetector.cpp |
@@ -0,0 +1,82 @@ |
+// 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 "platform/instrumentation/resource_coordinator/FrameResourceCoordinator.h" |
+#include "platform/loader/fetch/ResourceFetcher.h" |
+ |
+namespace blink { |
+ |
+static const char kSupplementName[] = "NetworkQuietDetector"; |
+ |
+NetworkQuietDetector& NetworkQuietDetector::From(Document& document) { |
+ NetworkQuietDetector* detector = static_cast<NetworkQuietDetector*>( |
+ Supplement<Document>::From(document, kSupplementName)); |
+ if (!detector) { |
+ detector = new NetworkQuietDetector(document); |
+ Supplement<Document>::ProvideTo(document, kSupplementName, detector); |
+ } |
+ return *detector; |
+} |
+ |
+// This function is called when the number of active connections is decreased. |
+void NetworkQuietDetector::CheckNetworkStable() { |
+ // Document finishes parsing after DomContentLoadedEventEnd is fired, |
+ // check the status in order to avoid false signals. |
+ if (!GetDocument()->HasFinishedParsing()) |
+ return; |
+ |
+ SetNetworkQuietTimers(ActiveConnections()); |
zhenw
2017/06/07 22:23:49
Does it mean ActiveConnections() will decrease mon
|
+} |
+ |
+NetworkQuietDetector::NetworkQuietDetector(Document& document) |
+ : Supplement<Document>(document), |
+ network2_quiet_timer_( |
+ TaskRunnerHelper::Get(TaskType::kUnspecedTimer, &document), |
+ this, |
+ &NetworkQuietDetector::Network2QuietTimerFired) {} |
+ |
+Document* NetworkQuietDetector::GetDocument() { |
+ return GetSupplementable(); |
+} |
+ |
+int NetworkQuietDetector::ActiveConnections() { |
+ ResourceFetcher* fetcher = GetDocument()->Fetcher(); |
+ return fetcher->BlockingRequestCount() + fetcher->NonblockingRequestCount(); |
+} |
+ |
+void NetworkQuietDetector::SetNetworkQuietTimers(int active_connections) { |
+ if (network2_quiet_reached_ || active_connections > 2) |
zhenw
2017/06/07 22:23:49
If |active_connections| can increase here, I think
|
+ 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 (!GetDocument() || !GetDocument()->GetFrame() || network2_quiet_reached_ || |
+ ActiveConnections() > 2) |
+ return; |
+ network2_quiet_reached_ = true; |
+ auto frame_resource_coordinator = |
+ GetDocument()->GetFrame()->GetFrameResourceCoordinator(); |
+ if (frame_resource_coordinator) { |
+ frame_resource_coordinator->SendEvent( |
+ resource_coordinator::mojom::EventType::kOnLocalFrameNetworkIdle); |
+ } |
+} |
+ |
+DEFINE_TRACE(NetworkQuietDetector) { |
+ Supplement<Document>::Trace(visitor); |
+} |
+ |
+} // namespace blink |