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/Document.h" | |
8 #include "core/dom/TaskRunnerHelper.h" | |
9 #include "core/frame/LocalFrame.h" | |
10 #include "platform/instrumentation/resource_coordinator/FrameResourceCoordinator .h" | |
11 #include "platform/loader/fetch/ResourceFetcher.h" | |
12 | |
13 namespace blink { | |
14 | |
15 static const char kSupplementName[] = "NetworkQuietDetector"; | |
16 | |
17 NetworkQuietDetector& NetworkQuietDetector::From(Document& document) { | |
18 NetworkQuietDetector* detector = static_cast<NetworkQuietDetector*>( | |
19 Supplement<Document>::From(document, kSupplementName)); | |
20 if (!detector) { | |
21 detector = new NetworkQuietDetector(document); | |
22 Supplement<Document>::ProvideTo(document, kSupplementName, detector); | |
23 } | |
24 return *detector; | |
25 } | |
26 | |
27 // This function is called when the number of active connections is decreased. | |
28 void NetworkQuietDetector::CheckNetworkStable() { | |
29 // Document finishes parsing after DomContentLoadedEventEnd is fired, | |
30 // check the status in order to avoid false signals. | |
31 if (!GetDocument()->HasFinishedParsing()) | |
32 return; | |
33 | |
34 SetNetworkQuietTimers(ActiveConnections()); | |
zhenw
2017/06/07 22:23:49
Does it mean ActiveConnections() will decrease mon
| |
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 Document* NetworkQuietDetector::GetDocument() { | |
45 return GetSupplementable(); | |
46 } | |
47 | |
48 int NetworkQuietDetector::ActiveConnections() { | |
49 ResourceFetcher* fetcher = GetDocument()->Fetcher(); | |
50 return fetcher->BlockingRequestCount() + fetcher->NonblockingRequestCount(); | |
51 } | |
52 | |
53 void NetworkQuietDetector::SetNetworkQuietTimers(int active_connections) { | |
54 if (network2_quiet_reached_ || active_connections > 2) | |
zhenw
2017/06/07 22:23:49
If |active_connections| can increase here, I think
| |
55 return; | |
56 | |
57 // If activeConnections < 2 and the timer is already running, current | |
58 // 2-quiet window continues; the timer shouldn't be restarted. | |
59 if (active_connections == 2 || !network2_quiet_timer_.IsActive()) { | |
60 network2_quiet_timer_.StartOneShot(kNetwork2QuietWindowSeconds, | |
61 BLINK_FROM_HERE); | |
62 } | |
63 } | |
64 | |
65 void NetworkQuietDetector::Network2QuietTimerFired(TimerBase*) { | |
66 if (!GetDocument() || !GetDocument()->GetFrame() || network2_quiet_reached_ || | |
67 ActiveConnections() > 2) | |
68 return; | |
69 network2_quiet_reached_ = true; | |
70 auto frame_resource_coordinator = | |
71 GetDocument()->GetFrame()->GetFrameResourceCoordinator(); | |
72 if (frame_resource_coordinator) { | |
73 frame_resource_coordinator->SendEvent( | |
74 resource_coordinator::mojom::EventType::kOnLocalFrameNetworkIdle); | |
75 } | |
76 } | |
77 | |
78 DEFINE_TRACE(NetworkQuietDetector) { | |
79 Supplement<Document>::Trace(visitor); | |
80 } | |
81 | |
82 } // namespace blink | |
OLD | NEW |