Bug 195223 - Add a WebViewDidMoveToWindowObserver for WKWebView
Summary: Add a WebViewDidMoveToWindowObserver for WKWebView
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit Misc. (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Jiewen Tan
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2019-03-01 13:02 PST by Jiewen Tan
Modified: 2019-03-04 15:50 PST (History)
6 users (show)

See Also:


Attachments
Patch (11.23 KB, patch)
2019-03-01 13:29 PST, Jiewen Tan
darin: review+
Details | Formatted Diff | Diff
Patch for landing (11.09 KB, patch)
2019-03-03 20:01 PST, Jiewen Tan
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jiewen Tan 2019-03-01 13:02:27 PST
Add a WebViewDidMoveToWindowObserver for WKWebView.
Comment 1 Radar WebKit Bug Importer 2019-03-01 13:11:35 PST
<rdar://problem/48520161>
Comment 2 Jiewen Tan 2019-03-01 13:29:10 PST
Created attachment 363365 [details]
Patch
Comment 3 Darin Adler 2019-03-03 00:06:54 PST
Comment on attachment 363365 [details]
Patch

What is the "load optimizer"?
Comment 4 Darin Adler 2019-03-03 12:19:15 PST
Comment on attachment 363365 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=363365&action=review

> Source/WebKit/UIProcess/WebPageProxy.h:1467
> +    void addWebViewDidMoveToWindowObserver(WebViewDidMoveToWindowObserver&);
> +    void removeWebViewDidMoveToWindowObserver(WebViewDidMoveToWindowObserver*);

I think these functions could have shorter names. C++ overloading means they could have names as short as "add/remove", but I was thinking "addObserver/removeObserver".

Can the remove function take a reference instead of a pointer? Is there a caller that needs to call it with null?

> Source/WebKit/UIProcess/WebPageProxy.h:2389
> +    HashMap<WebViewDidMoveToWindowObserver*, WeakPtr<WebViewDidMoveToWindowObserver>> m_webViewDidMoveToWindowObservers;

Is a WeakPtr the optimal pattern here? Where else are we using that for observers?

Since this entire observer function is just a "call this function" thing, maybe we could do something with functions instead? Unless this is a common pattern.
Comment 5 Jiewen Tan 2019-03-03 19:49:13 PST
Comment on attachment 363365 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=363365&action=review

Thanks Darin for r+ the patch.

>> Source/WebKit/UIProcess/WebPageProxy.h:1467
>> +    void removeWebViewDidMoveToWindowObserver(WebViewDidMoveToWindowObserver*);
> 
> I think these functions could have shorter names. C++ overloading means they could have names as short as "add/remove", but I was thinking "addObserver/removeObserver".
> 
> Can the remove function take a reference instead of a pointer? Is there a caller that needs to call it with null?

Fixed.

>> Source/WebKit/UIProcess/WebPageProxy.h:2389
>> +    HashMap<WebViewDidMoveToWindowObserver*, WeakPtr<WebViewDidMoveToWindowObserver>> m_webViewDidMoveToWindowObservers;
> 
> Is a WeakPtr the optimal pattern here? Where else are we using that for observers?
> 
> Since this entire observer function is just a "call this function" thing, maybe we could do something with functions instead? Unless this is a common pattern.

A more common pattern is to use HashSet<WebViewDidMoveToWindowObserver*> without weak ptrs. However, that relies on the observers to remove themselves from the set when they are about to destroy. Otherwise, UAF could happen.

I could see pros and cons of using Functions here.
Pros: More clean code; Leave UAF from the WebPageProxy to Functions suppliers.
Cons: More memory.

I don't really have opinions on which is significantly better than which. It looks like that it is a bigger topic to discuss than this patch. If you don't have strong opinions with my current approach, I will go ahead to land this patch. Maybe we could open a discussion on webkit-dev@lists.webkit.org?
Comment 6 Jiewen Tan 2019-03-03 20:01:05 PST
Created attachment 363478 [details]
Patch for landing
Comment 7 WebKit Commit Bot 2019-03-03 20:38:31 PST
Comment on attachment 363478 [details]
Patch for landing

Clearing flags on attachment: 363478

Committed r242336: <https://trac.webkit.org/changeset/242336>
Comment 8 Jiewen Tan 2019-03-04 15:50:40 PST
A follow up from Tim's comment is committed:
Committed r242398: <https://trac.webkit.org/changeset/242398>