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..6bceb1d5e11e13126872a72a1baf4476a31aeb9f |
--- /dev/null |
+++ b/third_party/WebKit/Source/core/loader/NetworkQuietDetector.cpp |
@@ -0,0 +1,78 @@ |
+// 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/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. |
+// Note that the number of active connections doesn't decrease monotonically. |
+void NetworkQuietDetector::CheckNetworkStable() { |
+ // Document finishes parsing after DomContentLoadedEventEnd is fired, |
+ // check the status in order to avoid false signals. |
+ if (!GetSupplementable()->HasFinishedParsing()) |
+ return; |
+ |
+ SetNetworkQuietTimers(ActiveConnections()); |
+} |
+ |
+NetworkQuietDetector::NetworkQuietDetector(Document& document) |
+ : Supplement<Document>(document), |
+ network2_quiet_timer_( |
+ TaskRunnerHelper::Get(TaskType::kUnspecedTimer, &document), |
+ this, |
+ &NetworkQuietDetector::Network2QuietTimerFired) {} |
+ |
+int NetworkQuietDetector::ActiveConnections() { |
+ ResourceFetcher* fetcher = GetSupplementable()->Fetcher(); |
+ return fetcher->BlockingRequestCount() + fetcher->NonblockingRequestCount(); |
+} |
+ |
+void NetworkQuietDetector::SetNetworkQuietTimers(int active_connections) { |
+ if (network2_quiet_reached_ || active_connections > 2) |
dcheng
2017/06/14 00:12:52
I would suggest putting "2" as a constant somewher
lpy
2017/06/14 18:19:35
Done.
|
+ 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()) { |
dcheng
2017/06/14 00:12:52
The comment says < 2, but the condition says == 2.
lpy
2017/06/14 18:19:35
Done.
|
+ network2_quiet_timer_.StartOneShot(kNetwork2QuietWindowSeconds, |
+ BLINK_FROM_HERE); |
+ } |
+} |
+ |
+void NetworkQuietDetector::Network2QuietTimerFired(TimerBase*) { |
+ if (!GetSupplementable() || !GetSupplementable()->GetFrame() || |
+ network2_quiet_reached_ || ActiveConnections() > 2) |
+ return; |
+ network2_quiet_reached_ = true; |
+ auto frame_resource_coordinator = |
+ GetSupplementable()->GetFrame()->GetFrameResourceCoordinator(); |
+ if (frame_resource_coordinator) { |
dcheng
2017/06/14 00:12:52
It's not really clear to me how this supplementabl
lpy
2017/06/14 18:19:35
Isn't the supplementable object installed when it'
dcheng
2017/06/14 18:32:40
Right, I missed the call in DidLoadResource.
But
|
+ frame_resource_coordinator->SendEvent( |
+ resource_coordinator::mojom::EventType::kOnLocalFrameNetworkIdle); |
+ } |
+} |
+ |
+DEFINE_TRACE(NetworkQuietDetector) { |
+ Supplement<Document>::Trace(visitor); |
+} |
+ |
+} // namespace blink |