OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "core/loader/NetworkQuietDetector.h" | |
6 | |
7 #include "core/dom/TaskRunnerHelper.h" | |
8 #include "core/frame/LocalFrame.h" | |
9 #include "platform/instrumentation/resource_coordinator/FrameResourceCoordinator .h" | |
10 #include "platform/loader/fetch/ResourceFetcher.h" | |
11 | |
12 namespace blink { | |
13 | |
14 static const char kSupplementName[] = "NetworkQuietDetector"; | |
15 | |
16 NetworkQuietDetector& NetworkQuietDetector::From(Document& document) { | |
17 NetworkQuietDetector* detector = static_cast<NetworkQuietDetector*>( | |
18 Supplement<Document>::From(document, kSupplementName)); | |
19 if (!detector) { | |
20 detector = new NetworkQuietDetector(document); | |
21 Supplement<Document>::ProvideTo(document, kSupplementName, detector); | |
22 } | |
23 return *detector; | |
24 } | |
25 | |
26 // 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.
| |
27 void NetworkQuietDetector::CheckNetworkStable() { | |
28 // Document finishes parsing after DomContentLoadedEventEnd is fired, | |
29 // check the status in order to avoid false signals. | |
30 if (!GetDocument()->HasFinishedParsing()) | |
31 return; | |
32 | |
33 SetNetworkQuietTimers(ActiveConnections()); | |
34 } | |
35 | |
36 NetworkQuietDetector::NetworkQuietDetector(Document& document) | |
37 : Supplement<Document>(document), | |
38 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
| |
39 TaskRunnerHelper::Get(TaskType::kUnspecedTimer, &document), | |
40 this, | |
41 &NetworkQuietDetector::Network2QuietTimerFired) {} | |
42 | |
43 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
| |
44 return GetSupplementable(); | |
45 } | |
46 | |
47 int NetworkQuietDetector::ActiveConnections() { | |
48 ResourceFetcher* fetcher = GetDocument()->Fetcher(); | |
49 return fetcher->BlockingRequestCount() + fetcher->NonblockingRequestCount(); | |
50 } | |
51 | |
52 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.
| |
53 if (network2_quiet_reached_ || active_connections > 2) | |
54 return; | |
55 | |
56 // If activeConnections < 2 and the timer is already running, current | |
57 // 2-quiet window continues; the timer shouldn't be restarted. | |
58 if (active_connections == 2 || !network2_quiet_timer_.IsActive()) { | |
59 network2_quiet_timer_.StartOneShot(kNetwork2QuietWindowSeconds, | |
60 BLINK_FROM_HERE); | |
61 } | |
62 } | |
63 | |
64 void NetworkQuietDetector::Network2QuietTimerFired(TimerBase*) { | |
65 if (!GetDocument() || !GetDocument()->GetFrame() || network2_quiet_reached_ || | |
66 ActiveConnections() > 2) | |
67 return; | |
68 network2_quiet_reached_ = true; | |
69 auto frame_resource_coordinator = | |
70 GetDocument()->GetFrame()->GetFrameResourceCoordinator(); | |
71 if (frame_resource_coordinator) { | |
72 frame_resource_coordinator->SendEvent( | |
73 resource_coordinator::mojom::EventType::kOnLocalFrameNetworkIdle); | |
74 } | |
75 } | |
76 | |
77 DEFINE_TRACE(NetworkQuietDetector) { | |
78 Supplement<Document>::Trace(visitor); | |
79 } | |
80 | |
81 } // namespace blink | |
OLD | NEW |