Dispatch resource load statistics telemetry on the main thread
authorwilander@apple.com <wilander@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 12 Dec 2017 23:54:34 +0000 (23:54 +0000)
committerwilander@apple.com <wilander@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 12 Dec 2017 23:54:34 +0000 (23:54 +0000)
https://bugs.webkit.org/show_bug.cgi?id=180602
<rdar://problem/35942205>

Reviewed by Brent Fulgham.

* UIProcess/WebResourceLoadStatisticsTelemetry.cpp:
(WebKit::WebResourceLoadStatisticsTelemetry::calculateAndSubmit):
    Now switches to the main thread for the telemetry submission
    through a webpage proxy. The reason is that the webpage we
    use may go away while we're still using it. This kind of
    telemetry isn't associated with a specific webpage but the
    infrastructure requires a webpage proxy.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@225818 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit/ChangeLog
Source/WebKit/UIProcess/WebResourceLoadStatisticsTelemetry.cpp

index 929ada0..4706fe6 100644 (file)
@@ -1,3 +1,19 @@
+2017-12-12  John Wilander  <wilander@apple.com>
+
+        Dispatch resource load statistics telemetry on the main thread
+        https://bugs.webkit.org/show_bug.cgi?id=180602
+        <rdar://problem/35942205>
+
+        Reviewed by Brent Fulgham.
+
+        * UIProcess/WebResourceLoadStatisticsTelemetry.cpp:
+        (WebKit::WebResourceLoadStatisticsTelemetry::calculateAndSubmit):
+            Now switches to the main thread for the telemetry submission
+            through a webpage proxy. The reason is that the webpage we
+            use may go away while we're still using it. This kind of
+            telemetry isn't associated with a specific webpage but the
+            infrastructure requires a webpage proxy.
+
 2017-12-12  Myles C. Maxfield  <mmaxfield@apple.com>
 
         IPC code doesn't understand NSDictionaries with non-NSString keys
index 5302aee..3e8ec41 100644 (file)
@@ -246,28 +246,31 @@ void WebResourceLoadStatisticsTelemetry::calculateAndSubmit(const WebResourceLoa
             sortedPrevalentResourcesWithoutUserInteraction.uncheckedAppend(prevalentResource);
     }
     
-    auto webPageProxy = nonEphemeralWebPageProxy();
-    if (!webPageProxy) {
-        if (notifyPagesWhenTelemetryWasCaptured)
-            notifyPages(0, 0, 0);
-        return;
-    }
-    
-    if (notifyPagesWhenTelemetryWasCaptured) {
-        notifyPages(sortedPrevalentResources, sortedPrevalentResourcesWithoutUserInteraction, prevalentResourcesDaysSinceUserInteraction.size());
-        // The notify pages function is for testing so we don't need to do an actual submission.
-        return;
-    }
-    
-    webPageProxy->logDiagnosticMessageWithValue(DiagnosticLoggingKeys::resourceLoadStatisticsTelemetryKey(), ASCIILiteral("totalNumberOfPrevalentResources"), sortedPrevalentResources.size(), significantFiguresForLoggedValues, ShouldSample::No);
-    webPageProxy->logDiagnosticMessageWithValue(DiagnosticLoggingKeys::resourceLoadStatisticsTelemetryKey(), ASCIILiteral("totalNumberOfPrevalentResourcesWithUserInteraction"), prevalentResourcesDaysSinceUserInteraction.size(), significantFiguresForLoggedValues, ShouldSample::No);
-    
-    if (prevalentResourcesDaysSinceUserInteraction.size() > 0)
-        webPageProxy->logDiagnosticMessageWithValue(DiagnosticLoggingKeys::resourceLoadStatisticsTelemetryKey(), ASCIILiteral("topPrevalentResourceWithUserInteractionDaysSinceUserInteraction"), prevalentResourcesDaysSinceUserInteraction[0], significantFiguresForLoggedValues, ShouldSample::No);
-    if (prevalentResourcesDaysSinceUserInteraction.size() > 1)
-        webPageProxy->logDiagnosticMessageWithValue(DiagnosticLoggingKeys::resourceLoadStatisticsTelemetryKey(), ASCIILiteral("medianPrevalentResourcesWithUserInteractionDaysSinceUserInteraction"), median(prevalentResourcesDaysSinceUserInteraction), significantFiguresForLoggedValues, ShouldSample::No);
-    
-    submitTopLists(sortedPrevalentResources, sortedPrevalentResourcesWithoutUserInteraction, *webPageProxy);
+    // Dispatch on the main thread to make sure the WebPageProxy we're using doesn't go away.
+    RunLoop::main().dispatch([sortedPrevalentResources = WTFMove(sortedPrevalentResources), sortedPrevalentResourcesWithoutUserInteraction = WTFMove(sortedPrevalentResourcesWithoutUserInteraction), prevalentResourcesDaysSinceUserInteraction = WTFMove(prevalentResourcesDaysSinceUserInteraction)] () {
+        auto webPageProxy = nonEphemeralWebPageProxy();
+        if (!webPageProxy) {
+            if (notifyPagesWhenTelemetryWasCaptured)
+                notifyPages(0, 0, 0);
+            return;
+        }
+        
+        if (notifyPagesWhenTelemetryWasCaptured) {
+            notifyPages(sortedPrevalentResources, sortedPrevalentResourcesWithoutUserInteraction, prevalentResourcesDaysSinceUserInteraction.size());
+            // The notify pages function is for testing so we don't need to do an actual submission.
+            return;
+        }
+        
+        webPageProxy->logDiagnosticMessageWithValue(DiagnosticLoggingKeys::resourceLoadStatisticsTelemetryKey(), ASCIILiteral("totalNumberOfPrevalentResources"), sortedPrevalentResources.size(), significantFiguresForLoggedValues, ShouldSample::No);
+        webPageProxy->logDiagnosticMessageWithValue(DiagnosticLoggingKeys::resourceLoadStatisticsTelemetryKey(), ASCIILiteral("totalNumberOfPrevalentResourcesWithUserInteraction"), prevalentResourcesDaysSinceUserInteraction.size(), significantFiguresForLoggedValues, ShouldSample::No);
+        
+        if (prevalentResourcesDaysSinceUserInteraction.size() > 0)
+            webPageProxy->logDiagnosticMessageWithValue(DiagnosticLoggingKeys::resourceLoadStatisticsTelemetryKey(), ASCIILiteral("topPrevalentResourceWithUserInteractionDaysSinceUserInteraction"), prevalentResourcesDaysSinceUserInteraction[0], significantFiguresForLoggedValues, ShouldSample::No);
+        if (prevalentResourcesDaysSinceUserInteraction.size() > 1)
+            webPageProxy->logDiagnosticMessageWithValue(DiagnosticLoggingKeys::resourceLoadStatisticsTelemetryKey(), ASCIILiteral("medianPrevalentResourcesWithUserInteractionDaysSinceUserInteraction"), median(prevalentResourcesDaysSinceUserInteraction), significantFiguresForLoggedValues, ShouldSample::No);
+        
+        submitTopLists(sortedPrevalentResources, sortedPrevalentResourcesWithoutUserInteraction, *webPageProxy);
+    });
 }
     
 void WebResourceLoadStatisticsTelemetry::setNotifyPagesWhenTelemetryWasCaptured(bool always)