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. | |
27 // Note that the number of active connections doesn't decrease monotonically. | |
28 void NetworkQuietDetector::CheckNetworkStable() { | |
29 // Document finishes parsing after DomContentLoadedEventEnd is fired, | |
30 // check the status in order to avoid false signals. | |
31 if (!GetSupplementable()->HasFinishedParsing()) | |
32 return; | |
33 | |
34 SetNetworkQuietTimers(ActiveConnections()); | |
35 } | |
36 | |
37 NetworkQuietDetector::NetworkQuietDetector(Document& document) | |
38 : Supplement<Document>(document), | |
39 network2_quiet_timer_( | |
40 TaskRunnerHelper::Get(TaskType::kUnspecedTimer, &document), | |
41 this, | |
42 &NetworkQuietDetector::Network2QuietTimerFired) {} | |
43 | |
44 int NetworkQuietDetector::ActiveConnections() { | |
45 ResourceFetcher* fetcher = GetSupplementable()->Fetcher(); | |
46 return fetcher->BlockingRequestCount() + fetcher->NonblockingRequestCount(); | |
47 } | |
48 | |
49 void NetworkQuietDetector::SetNetworkQuietTimers(int active_connections) { | |
50 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.
| |
51 return; | |
52 | |
53 // If activeConnections < 2 and the timer is already running, current | |
54 // 2-quiet window continues; the timer shouldn't be restarted. | |
55 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.
| |
56 network2_quiet_timer_.StartOneShot(kNetwork2QuietWindowSeconds, | |
57 BLINK_FROM_HERE); | |
58 } | |
59 } | |
60 | |
61 void NetworkQuietDetector::Network2QuietTimerFired(TimerBase*) { | |
62 if (!GetSupplementable() || !GetSupplementable()->GetFrame() || | |
63 network2_quiet_reached_ || ActiveConnections() > 2) | |
64 return; | |
65 network2_quiet_reached_ = true; | |
66 auto frame_resource_coordinator = | |
67 GetSupplementable()->GetFrame()->GetFrameResourceCoordinator(); | |
68 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
| |
69 frame_resource_coordinator->SendEvent( | |
70 resource_coordinator::mojom::EventType::kOnLocalFrameNetworkIdle); | |
71 } | |
72 } | |
73 | |
74 DEFINE_TRACE(NetworkQuietDetector) { | |
75 Supplement<Document>::Trace(visitor); | |
76 } | |
77 | |
78 } // namespace blink | |
OLD | NEW |