Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(571)

Unified Diff: third_party/WebKit/Source/core/loader/NetworkQuietDetector.cpp

Issue 2925963002: Create NetworkQuietDetector. (Closed)
Patch Set: Add kNetworkIdle state. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..e4189f41f0a1195fa4b909dec996ce189f6588ab
--- /dev/null
+++ b/third_party/WebKit/Source/core/loader/NetworkQuietDetector.cpp
@@ -0,0 +1,81 @@
+// 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.
Zhen Wang 2017/06/08 16:25:56 nit: This comment could be misleading that active
lpy 2017/06/12 18:06:29 Done.
+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());
+}
+
+NetworkQuietDetector::NetworkQuietDetector(Document& document)
+ : Supplement<Document>(document),
+ network2_quiet_timer_(
Nate Chapin 2017/06/12 17:16:39 What's the significance of '2' in this variable na
lpy 2017/06/12 18:06:29 It means it has no more than 2 active connection.
Nate Chapin 2017/06/12 18:12:17 I'd be inclined to leave the number out of the var
dcheng 2017/06/14 00:12:52 (I'd echo this comment, except for basically every
+ TaskRunnerHelper::Get(TaskType::kUnspecedTimer, &document),
+ this,
+ &NetworkQuietDetector::Network2QuietTimerFired) {}
+
+Document* NetworkQuietDetector::GetDocument() {
Nate Chapin 2017/06/12 17:16:39 I don't feel strongly, but it's not clear to me th
lpy 2017/06/12 18:06:29 Done. I don't think we need it for now, so removed
+ return GetSupplementable();
+}
+
+int NetworkQuietDetector::ActiveConnections() {
+ ResourceFetcher* fetcher = GetDocument()->Fetcher();
+ return fetcher->BlockingRequestCount() + fetcher->NonblockingRequestCount();
+}
+
+void NetworkQuietDetector::SetNetworkQuietTimers(int active_connections) {
Nate Chapin 2017/06/12 17:16:39 This is only called once. Maybe inline it in Check
lpy 2017/06/12 18:06:29 We need it in tests.
+ 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 (!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

Powered by Google App Engine
This is Rietveld 408576698