Bug 227133 - Trying to take empty snapshots of the view should not cause crashes
Summary: Trying to take empty snapshots of the view should not cause crashes
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit2 (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Chris Dumez
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2021-06-17 09:47 PDT by Chris Dumez
Modified: 2021-06-17 14:23 PDT (History)
6 users (show)

See Also:


Attachments
Returns a NSError (7.05 KB, patch)
2021-06-17 09:50 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Returns an empty image (7.28 KB, patch)
2021-06-17 11:03 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Dumez 2021-06-17 09:47:06 PDT
Trying to take empty snapshots of the view should not cause crashes.
Comment 1 Chris Dumez 2021-06-17 09:50:38 PDT
Created attachment 431685 [details]
Returns a NSError
Comment 2 Geoffrey Garen 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.
Comment 3 Chris Dumez 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).
Comment 4 Chris Dumez 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.
Comment 5 Chris Dumez 2021-06-17 10:28:14 PDT
Found in the context of rdar://79424132.
Comment 6 Chris Dumez 2021-06-17 11:03:17 PDT
Created attachment 431692 [details]
Returns an empty image
Comment 7 Tim Horton 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)
Comment 8 Chris Dumez 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?
Comment 9 Chris Dumez 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 :)
Comment 10 Tim Horton 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.
Comment 11 Tim Horton 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.
Comment 12 EWS 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].
Comment 13 Radar WebKit Bug Importer 2021-06-17 13:49:29 PDT
<rdar://problem/79468888>
Comment 14 Geoffrey Garen 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.