Chromium Code Reviews| 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 network_quiet_timer_( | |
| 40 TaskRunnerHelper::Get(TaskType::kUnspecedTimer, &document), | |
| 41 this, | |
| 42 &NetworkQuietDetector::NetworkQuietTimerFired) {} | |
| 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 (network_quiet_reached_ || | |
| 51 active_connections > kNetworkQuietMaximumConnections) | |
| 52 return; | |
| 53 | |
| 54 // If activeConnections < 2 and the timer is already running, current | |
| 55 // quiet window continues; the timer shouldn't be restarted. | |
| 56 // The timer should be restarted when |active_connections| == 2 because it | |
| 57 // means the number of active connections has increased to more than 2 after | |
| 58 // the timer was started. | |
| 59 if (active_connections == kNetworkQuietMaximumConnections || | |
| 60 !network_quiet_timer_.IsActive()) { | |
| 61 network_quiet_timer_.StartOneShot(kNetworkQuietWindowSeconds, | |
| 62 BLINK_FROM_HERE); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void NetworkQuietDetector::NetworkQuietTimerFired(TimerBase*) { | |
| 67 if (!GetSupplementable() || !GetSupplementable()->GetFrame() || | |
| 68 network_quiet_reached_ || | |
| 69 ActiveConnections() > kNetworkQuietMaximumConnections) | |
| 70 return; | |
| 71 network_quiet_reached_ = true; | |
| 72 auto frame_resource_coordinator = | |
| 73 GetSupplementable()->GetFrame()->GetFrameResourceCoordinator(); | |
| 74 if (frame_resource_coordinator) { | |
|
dcheng
2017/06/14 21:34:43
Note: I believe that by installing the supplement
| |
| 75 frame_resource_coordinator->SendEvent( | |
| 76 resource_coordinator::mojom::EventType::kOnLocalFrameNetworkIdle); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 DEFINE_TRACE(NetworkQuietDetector) { | |
| 81 Supplement<Document>::Trace(visitor); | |
| 82 } | |
| 83 | |
| 84 } // namespace blink | |
| OLD | NEW |