[WK2] -[WKContentView doAfterPositionInformationUpdate:atPosition:] should be robust...
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 20 Apr 2017 19:59:46 +0000 (19:59 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 20 Apr 2017 19:59:46 +0000 (19:59 +0000)
commit17a5b791f86538422b83f1067644cda5f6733c09
tree0e6188d10f2ba10f10b2398c4aee991ad5b2f6ae
parentc66bfb0cbaea8f9288513c933ee82f19ccbcf872
[WK2] -[WKContentView doAfterPositionInformationUpdate:atPosition:] should be robust against synchronous reentrancy
https://bugs.webkit.org/show_bug.cgi?id=170922
<rdar://problem/31634990>

Reviewed by Tim Horton.

Source/WebKit2:

Refactors part of the asynchronous position information mechanism introduced in
<https://trac.webkit.org/changeset/215171>, and introduces infrastructure for unit testing UI-side position
information requests.

_invokeAndRemovePendingHandlersValidForCurrentPositionInformation is a helper method on WKContentView
responsible for invoking queued position information handlers after receiving a position information response
from the web process. Previously, this method would iterate over the list of pending callbacks and invoke all
callbacks whose requests are satisfied by the incoming position information update, saving the indices of these
handled callbacks in the process. At the end, it would then remove callbacks at these indices from the array of
pending callbacks. This is problematic when a synchronous position update (via
ensurePositionInformationIsUpToDate:) is triggered from within one of these callbacks, since
_invokeAndRemovePendingHandlersValidForCurrentPositionInformation will be called from within itself, and then we
will attempt to remove a callback at the same index twice.

To fix this, we change the array of pending position information handlers to be an array of optionals instead.
When invoking and removing pending handlers after a position information response arrives, we now mark callbacks
as handled by setting them to std::nullopt. Then, when the top-level invocation to
_invokeAndRemovePendingHandlersValidForCurrentPositionInformation is finished, we remove all marked handlers
from the pending vector.

Covered by 6 new unit tests:
- PositionInformationTests.FindDraggableLinkAtPosition
- PositionInformationTests.RequestDraggableLinkAtPosition
- PositionInformationTests.FindDraggableLinkAtDifferentPositionWithinRequestBlock
- PositionInformationTests.FindDraggableLinkAtSamePositionWithinRequestBlock
- PositionInformationTests.RequestDraggableLinkAtSamePositionWithinRequestBlock
- PositionInformationTests.RequestDraggableLinkAtDifferentPositionWithinRequestBlock

* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView draggableElementAtPosition:]):
(-[WKWebView requestDraggableElementAtPosition:completionBlock:]):

Uses WKContentView's position information request helpers to search for draggable elements on the page. There
are both synchronous and asynchronous versions of this, both of which retrieve a _WKDraggableElementInfo.

* UIProcess/API/Cocoa/WKWebViewPrivate.h:
* UIProcess/API/Cocoa/_WKDraggableElementInfo.h: Added.
* UIProcess/API/Cocoa/_WKDraggableElementInfo.mm: Added.

Exposes what elements are draggable at a given position as SPI (only for use in testing code, at the moment).

(-[_WKDraggableElementInfo initWithInteractionInformationAtPosition:]):
(-[_WKDraggableElementInfo copyWithZone:]):
* UIProcess/API/Cocoa/_WKDraggableElementInfoInternal.h: Added.
* UIProcess/ios/WKContentViewInteraction.h:
* UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView currentPositionInformation]):
(-[WKContentView doAfterPositionInformationUpdate:forRequest:]):
(-[WKContentView _invokeAndRemovePendingHandlersValidForCurrentPositionInformation]):
* WebKit2.xcodeproj/project.pbxproj:

Tools:

Adds six new unit tests for retrieving interaction information at a given position in the UI process. See
WebKit2 ChangeLog for more details.

* TestWebKitAPI/Tests/ios/PositionInformationTests.mm:
(-[_WKDraggableElementInfo expectToBeLink:image:atPoint:]):
(TestWebKitAPI::TEST):
(TestWebKitAPI::expectCGPointsToBeEqual): Deleted.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@215573 268f45cc-cd09-0410-ab3c-d52691b4dbfc
12 files changed:
Source/WebKit2/ChangeLog
Source/WebKit2/UIProcess/API/Cocoa/WKWebView.mm
Source/WebKit2/UIProcess/API/Cocoa/WKWebViewPrivate.h
Source/WebKit2/UIProcess/API/Cocoa/_WKDraggableElementInfo.h [new file with mode: 0644]
Source/WebKit2/UIProcess/API/Cocoa/_WKDraggableElementInfo.mm [new file with mode: 0644]
Source/WebKit2/UIProcess/API/Cocoa/_WKDraggableElementInfoInternal.h [new file with mode: 0644]
Source/WebKit2/UIProcess/ios/WKContentViewInteraction.h
Source/WebKit2/UIProcess/ios/WKContentViewInteraction.mm
Source/WebKit2/WebKit2.xcodeproj/project.pbxproj
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/ios/PositionInformationTests.mm [new file with mode: 0644]