ImageAnalysisQueue should analyze image elements that are loaded after the call to...
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 17 Jan 2022 20:17:20 +0000 (20:17 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 17 Jan 2022 20:17:20 +0000 (20:17 +0000)
commite661a6ad63a9bf57393ebeed789eaa9e232bdf9d
tree3881dc5fd34fbbfe58bd853356883c2660ba92c2
parent20174857ee8225b48637f1f0ecd6f065b1a748c7
ImageAnalysisQueue should analyze image elements that are loaded after the call to enqueueAllImages()
https://bugs.webkit.org/show_bug.cgi?id=233266
rdar://85731875

Reviewed by Darin Adler.

Source/WebCore:

Make a few adjustments to ImageAnalysisQueue, such that it continues to analyze images in the document that have
finished loading after triggering the initial call to `enqueueAllImages()` in the document.

Test:   ImageAnalysisTests.AnalyzeDynamicallyLoadedImages
        ImageAnalysisTests.ResetImageAnalysisAfterNavigation

* page/ImageAnalysisQueue.cpp:
(WebCore::ImageAnalysisQueue::enqueueIfNeeded):

Pull logic for queueing an image element for analysis into a separate helper method; we only attempt to analyze
image elements that have successfully loaded and contain a `CachedImage` that is larger than an arbitrarily
chosen size of 20px by 20px.

(WebCore::ImageAnalysisQueue::enqueueAllImages):

Refactor this to use the new `enqueueIfNeeded` method above.

(WebCore::ImageAnalysisQueue::resumeProcessing):
(WebCore::ImageAnalysisQueue::clear):
* page/ImageAnalysisQueue.h:

Add a weak hashset of elements that we've already added to the image analysis queue at some point. This prevents
us from continually performing image analysis on the same image if it's only being removed and reinserted in the
document.

* page/Page.cpp:
(WebCore::Page::didCommitLoad):

Additionally reset the image analysis queue when committing page load, so that the image analysis queue doesn't
persist and continue analyzing images even after reloading the page (or navigating away).

(WebCore::Page::didFinishLoadingImageForElement):

If it exists, tell the page's image analysis queue to add the newly loaded image.

(WebCore::Page::resetImageAnalysisQueue):
* page/Page.h:

Tools:

Add new API tests to check that image elements that are created and inserted after kicking off the image
analysis queue are successfully added to the queue, and result in platform image analysis requests, and to also
verify that the image analysis queue is reset after navigation.

* TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm:
(TestWebKitAPI::processRequestWithResults):
(TestWebKitAPI::makeImageAnalysisRequestSwizzler):
(TestWebKitAPI::processRequestWithError):
(TestWebKitAPI::TEST):
(TestWebKitAPI::swizzledProcessRequestWithResults): Deleted.
(TestWebKitAPI::swizzledProcessRequestWithError): Deleted.
* TestWebKitAPI/Tests/WebKitCocoa/multiple-images.html:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@288102 268f45cc-cd09-0410-ab3c-d52691b4dbfc
Source/WebCore/ChangeLog
Source/WebCore/page/ImageAnalysisQueue.cpp
Source/WebCore/page/ImageAnalysisQueue.h
Source/WebCore/page/Page.cpp
Source/WebCore/page/Page.h
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKitCocoa/ImageAnalysisTests.mm
Tools/TestWebKitAPI/Tests/WebKitCocoa/multiple-images.html