ResourceLoadStatisticsClassifierCocoa::singletonPredictionModel() should check the...
authorwilander@apple.com <wilander@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 2 Aug 2017 17:45:52 +0000 (17:45 +0000)
committerwilander@apple.com <wilander@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 2 Aug 2017 17:45:52 +0000 (17:45 +0000)
https://bugs.webkit.org/show_bug.cgi?id=175055
<rdar://problem/32671352>

Reviewed by David Kilzer.

* Platform/classifier/cocoa/ResourceLoadStatisticsClassifierCocoa.cpp:
(WebKit::ResourceLoadStatisticsClassifierCocoa::singletonPredictionModel):
    Now uses dispatch_once() instead of NeverDestroyed and checks the
    return value of storagePath() before using it to load the model.

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

Source/WebKit/ChangeLog
Source/WebKit/Platform/classifier/cocoa/ResourceLoadStatisticsClassifierCocoa.cpp

index b3351c499503bc501b191c9dcb63cde5bbd95515..24622cbfc728b4608e87db32c2c38395780b17bd 100644 (file)
@@ -1,3 +1,16 @@
+2017-08-02  John Wilander  <wilander@apple.com>
+
+        ResourceLoadStatisticsClassifierCocoa::singletonPredictionModel() should check the return value of storagePath()
+        https://bugs.webkit.org/show_bug.cgi?id=175055
+        <rdar://problem/32671352>
+
+        Reviewed by David Kilzer.
+
+        * Platform/classifier/cocoa/ResourceLoadStatisticsClassifierCocoa.cpp:
+        (WebKit::ResourceLoadStatisticsClassifierCocoa::singletonPredictionModel):
+            Now uses dispatch_once() instead of NeverDestroyed and checks the
+            return value of storagePath() before using it to load the model.
+
 2017-08-01  Brian Burg  <bburg@apple.com>
 
         HTML file input elements do not support file extensions in the "accept" attribute
index fdeaef187763087d839eaafa42a4bd64ab3d6fd6..b6f7fc3822f669efb57428e694b725a977b766b1 100644 (file)
@@ -103,10 +103,18 @@ bool ResourceLoadStatisticsClassifierCocoa::canUseCorePrediction()
 
 const struct svm_model* ResourceLoadStatisticsClassifierCocoa::singletonPredictionModel()
 {
-    static NeverDestroyed<struct svm_model*> corePredictionModel = svm_load_model(storagePath().utf8().data());
-
-    if (corePredictionModel)
-        return corePredictionModel;
+    static std::optional<struct svm_model*> corePredictionModel;
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+        auto path = storagePath();
+        if (path.isEmpty())
+            return;
+
+        corePredictionModel = svm_load_model(path.utf8().data());
+    });
+
+    if (corePredictionModel && corePredictionModel.value())
+        return corePredictionModel.value();
 
     WTFLogAlways("ResourceLoadStatisticsClassifierCocoa::singletonPredictionModel(): Couldn't load model file at path %s.", storagePath().utf8().data());
     m_useCorePrediction = false;