RESOLVED FIXED 227133
Trying to take empty snapshots of the view should not cause crashes
https://bugs.webkit.org/show_bug.cgi?id=227133
Summary Trying to take empty snapshots of the view should not cause crashes
Chris Dumez
Reported 2021-06-17 09:47:06 PDT
Trying to take empty snapshots of the view should not cause crashes.
Attachments
Returns a NSError (7.05 KB, patch)
2021-06-17 09:50 PDT, Chris Dumez
no flags
Returns an empty image (7.28 KB, patch)
2021-06-17 11:03 PDT, Chris Dumez
no flags
Chris Dumez
Comment 1 2021-06-17 09:50:38 PDT
Created attachment 431685 [details] Returns a NSError
Geoffrey Garen
Comment 2 2021-06-17 10:12:39 PDT
Comment on attachment 431685 [details] Returns a NSError View in context: https://bugs.webkit.org/attachment.cgi?id=431685&action=review > Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:1178 > + if (CGRectIsEmpty(rectInViewCoordinates) || !snapshotWidth) { > + RunLoop::main().dispatch([completionHandler = WTFMove(completionHandler)]() mutable { > + completionHandler(nil, createNSError(WKErrorUnknown).get()); > + }); > + return; > + } Is returning WKErrorUnknown better than returning an empty image? NSImage appears to support an empty image constructor, with the caveat that using the image later might cause you problems: "It is permissible to initialize the image object by passing a size of (0.0, 0.0); however, you must set the size to a non-zero value before using it or an exception will be raised." [ https://developer.apple.com/documentation/appkit/nsimage/1520033-initwithsize?language=objc ] Perhaps those problems will be clearer than WKErrorUnknown.
Chris Dumez
Comment 3 2021-06-17 10:18:41 PDT
(In reply to Geoffrey Garen from comment #2) > Comment on attachment 431685 [details] > Patch > > View in context: > https://bugs.webkit.org/attachment.cgi?id=431685&action=review > > > Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:1178 > > + if (CGRectIsEmpty(rectInViewCoordinates) || !snapshotWidth) { > > + RunLoop::main().dispatch([completionHandler = WTFMove(completionHandler)]() mutable { > > + completionHandler(nil, createNSError(WKErrorUnknown).get()); > > + }); > > + return; > > + } > > Is returning WKErrorUnknown better than returning an empty image? NSImage > appears to support an empty image constructor, with the caveat that using > the image later might cause you problems: > > "It is permissible to initialize the image object by passing a size of (0.0, > 0.0); however, you must set the size to a non-zero value before using it or > an exception will be raised." [ > https://developer.apple.com/documentation/appkit/nsimage/1520033- > initwithsize?language=objc ] > > Perhaps those problems will be clearer than WKErrorUnknown. If you prefer, I can certainly do that. I tried to maintain pre-existing behavior in release builds (we would hit the assertion in debug).
Chris Dumez
Comment 4 2021-06-17 10:25:05 PDT
(In reply to Chris Dumez from comment #3) > (In reply to Geoffrey Garen from comment #2) > > Comment on attachment 431685 [details] > > Patch > > > > View in context: > > https://bugs.webkit.org/attachment.cgi?id=431685&action=review > > > > > Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:1178 > > > + if (CGRectIsEmpty(rectInViewCoordinates) || !snapshotWidth) { > > > + RunLoop::main().dispatch([completionHandler = WTFMove(completionHandler)]() mutable { > > > + completionHandler(nil, createNSError(WKErrorUnknown).get()); > > > + }); > > > + return; > > > + } > > > > Is returning WKErrorUnknown better than returning an empty image? NSImage > > appears to support an empty image constructor, with the caveat that using > > the image later might cause you problems: > > > > "It is permissible to initialize the image object by passing a size of (0.0, > > 0.0); however, you must set the size to a non-zero value before using it or > > an exception will be raised." [ > > https://developer.apple.com/documentation/appkit/nsimage/1520033- > > initwithsize?language=objc ] > > > > Perhaps those problems will be clearer than WKErrorUnknown. > > If you prefer, I can certainly do that. I tried to maintain pre-existing > behavior in release builds (we would hit the assertion in debug). I am also unsure how to create an empty UIImage. I don't see a similar API as for NSImage.
Chris Dumez
Comment 5 2021-06-17 10:28:14 PDT
Found in the context of rdar://79424132.
Chris Dumez
Comment 6 2021-06-17 11:03:17 PDT
Created attachment 431692 [details] Returns an empty image
Tim Horton
Comment 7 2021-06-17 11:26:16 PDT
Comment on attachment 431692 [details] Returns an empty image View in context: https://bugs.webkit.org/attachment.cgi?id=431692&action=review > Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:1180 > + completionHandler(image.get(), nil); I'm a little confused. The image param is nullable, shouldn't we just do that? (as a consumer of many cocoa APIs, that seems less surprising to me than this)
Chris Dumez
Comment 8 2021-06-17 11:31:08 PDT
(In reply to Tim Horton from comment #7) > Comment on attachment 431692 [details] > Patch > > View in context: > https://bugs.webkit.org/attachment.cgi?id=431692&action=review > > > Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:1180 > > + completionHandler(image.get(), nil); > > I'm a little confused. The image param is nullable, shouldn't we just do > that? (as a consumer of many cocoa APIs, that seems less surprising to me > than this) So your preference would be a nil image AND a nil NSError?
Chris Dumez
Comment 9 2021-06-17 11:34:46 PDT
(In reply to Chris Dumez from comment #8) > (In reply to Tim Horton from comment #7) > > Comment on attachment 431692 [details] > > Patch > > > > View in context: > > https://bugs.webkit.org/attachment.cgi?id=431692&action=review > > > > > Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:1180 > > > + completionHandler(image.get(), nil); > > > > I'm a little confused. The image param is nullable, shouldn't we just do > > that? (as a consumer of many cocoa APIs, that seems less surprising to me > > than this) > > So your preference would be a nil image AND a nil NSError? Our documentation says "The completionHandler is passed the image of the viewport contents or an error." My first patch was passing an error, my second one is passing an empty image. I think either would be fine but we need to agree on something and fix the crashes on the bots :)
Tim Horton
Comment 10 2021-06-17 11:38:04 PDT
(In reply to Chris Dumez from comment #9) > (In reply to Chris Dumez from comment #8) > > (In reply to Tim Horton from comment #7) > > > Comment on attachment 431692 [details] > > > Patch > > > > > > View in context: > > > https://bugs.webkit.org/attachment.cgi?id=431692&action=review > > > > > > > Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:1180 > > > > + completionHandler(image.get(), nil); > > > > > > I'm a little confused. The image param is nullable, shouldn't we just do > > > that? (as a consumer of many cocoa APIs, that seems less surprising to me > > > than this) > > > > So your preference would be a nil image AND a nil NSError? > > Our documentation says "The completionHandler is passed the image of the > viewport contents or an error." > > My first patch was passing an error, my second one is passing an empty > image. I think either would be fine but we need to agree on something and > fix the crashes on the bots :) Ahaa, fair :) I think either is definitely fine.
Tim Horton
Comment 11 2021-06-17 11:39:05 PDT
I guess.. you did ask for an empty image, and you got an empty image, so I will r+ that one.
EWS
Comment 12 2021-06-17 13:48:52 PDT
Committed r279006 (238931@main): <https://commits.webkit.org/238931@main> All reviewed patches have been landed. Closing bug and clearing flags on attachment 431692 [details].
Radar WebKit Bug Importer
Comment 13 2021-06-17 13:49:29 PDT
Geoffrey Garen
Comment 14 2021-06-17 14:23:32 PDT
> I guess.. you did ask for an empty image, and you got an empty image FWIW, that was my thinking too.
Note You need to log in before you can comment on or make changes to this bug.