Bug 536141 - [Webkit2][Gtk] BrowserFunction lost after page reload on Linux/GTK
Summary: [Webkit2][Gtk] BrowserFunction lost after page reload on Linux/GTK
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 4.8   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: 4.9 M3   Edit
Assignee: Eric Williams CLA
QA Contact:
URL:
Whiteboard:
Keywords: triaged
Depends on:
Blocks:
 
Reported: 2018-06-21 08:40 EDT by Jeff MAURY CLA
Modified: 2021-11-23 10:51 EST (History)
3 users (show)

See Also:


Attachments
Example plugin (7.69 KB, application/x-zip-compressed)
2018-06-21 08:40 EDT, Jeff MAURY CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jeff MAURY CLA 2018-06-21 08:40:20 EDT
Created attachment 274572 [details]
Example plugin

When a browser function is defined in Java code, it is available to the JS part of the loaded page. However, if the page is reload thanks to the context menu provided by the browser widget, the function is not available anymore.
An example plugin is provided.
This seems to be Photon specific as we got this on JBossTools 4.6.x which is based on Photon and can't reproduce on JBossTools 4.5.x which is based on Oxygen
Comment 1 Eric Williams CLA 2018-06-21 10:50:19 EDT
I can reproduce the bug using SWT from master as of this morning, will investigate further.
Comment 2 Nick Boldt CLA 2018-06-26 09:41:48 EDT
Any news on this one? It would be great if we could get a fix in time for the last target platform freeze of devstudio 12.0.0.GA (next Tuesday).
Comment 3 Nick Boldt CLA 2018-06-26 09:48:40 EDT
Reported on Windows 10 but downstream issue says cannot be reproduced on Win10, only on Fedora.

https://issues.jboss.org/browse/JBDS-4710?focusedCommentId=13593067&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-13593067
Comment 4 Leo Ufimtsev CLA 2018-06-28 15:05:31 EDT
I haven't investigated, but last time I looked into this I added:

Webkit.java: registerBrowserFunctions()

It needs to be called somewhere after a page was re-loaded. At the moment it's already called in webkit_load_changed(), so we'd have to investigate why it's not webkit_load_changed() is not called in this particular case. Maybe another callback needs to be called in this situation.
Comment 5 Leo Ufimtsev CLA 2018-06-29 12:16:46 EDT
I've investigated with Eric.

Fixing this is not straight forward because webkitgtk doesn't provide us with synchronous ways of registering browser functions. It would take 2-4 weeks to fix probably.

We tested the snippet on windows. It works. So it's a webkit2 specific issue.

It's a timing issue. BrowserFunctions with webkit2 are registered asynchronously near the end of the page load, where as the init() function wants the functions to be available before page is loaded.


Idea for fix:
- At the moment we execute system and user javascript asynchronously. The reason for this is user given javascript can take a long time to execute and causes all of eclipse to hang.
- The solution would be to implement a mechanism by which we execute *system* javascript (e.g registering browser functions) *synchronously* (since we know they are quick).

This can be done via the following mechanism:
1) Add a G-DBus server to webextension. (The name must be unique)
2) Upon initialization of webextension, the webextension should call
   the main process via DBus and tell it it's unique bus-name.
3) in webkitgtk_extension.c, when window_object_cleared_callback() is called,
   it should call the main process via DBus and get a string of the functions
   to register.
4) the extension should then execute the javascript synchronously via JSEvaluateScript(..).
5) Update browser-function registration mechanism to register browserfunctions synchronously before page load starts.

Note, 
Sami did some work on this:
https://git.eclipse.org/r/#/c/23416/16/bundles/org.eclipse.swt/Eclipse+SWT+WebKit/gtk/library/webkit_extension.c

Some elements of the above can be re-used, but only for *system* javascript, since *user* javascript can take a long time to execute and blocks eclipse ui.
Namely, the following can be re-used:
 - Webkit extension registers a dbus name. (this must be unique thou as there can be multiple eclipse instances).
 - code for java to call webextension via dbus. (at the moment we only have webextension calling java/main process).
 - webextension code to execute javascript. See 'webkit_extension_execute_script()' in patch above.
 - WebkitGDBus.java has code to convert java string to DBus types. Much of the code there can be re-used.
Comment 7 Eric Williams CLA 2018-08-14 11:13:28 EDT
(In reply to Eclipse Genie from comment #6)
> Gerrit change https://git.eclipse.org/r/126407 was merged to [master].
> Commit:
> http://git.eclipse.org/c/platform/eclipse.platform.swt.git/commit/
> ?id=f0fe56670826e5b46e2c9a52c132ec39f531e136

Patch is in master now.
Comment 8 Eric Williams CLA 2018-08-23 08:29:37 EDT
Verified in I20180821-2000.
Comment 9 Jeff MAURY CLA 2018-10-11 08:12:54 EDT
Tested in DevStudio 12.9.0.GA which is based on 2018-09 on Fedora 27 and this causes a crash in webkitgtk4
Comment 10 Eric Williams CLA 2018-10-11 10:08:23 EDT
(In reply to Jeff MAURY from comment #9)
> Tested in DevStudio 12.9.0.GA which is based on 2018-09 on Fedora 27 and
> this causes a crash in webkitgtk4

Any logs/stack trace? Does the issue happen on Fedora 28?
Comment 11 Jeff MAURY CLA 2018-10-12 03:03:03 EDT
Idem on F28. See https://issues.jboss.org/secure/attachment/12441730/screenshot-2.png
Comment 12 Eric Williams CLA 2018-10-12 08:23:26 EDT
(In reply to Jeff MAURY from comment #11)
> Idem on F28. See
> https://issues.jboss.org/secure/attachment/12441730/screenshot-2.png

Does the issue happen with 4.9 Eclipse? What are the steps to reproduce this crash?
Comment 13 Jeff MAURY CLA 2018-10-12 08:46:28 EDT
Yes because DevStudio 12.9.0.GA is based on 2018-09.

Steps to reproduce:

1) Launch DevStudio 12.9.0.GA or 2018-09 + JBoss Tools 4.10.0.Final (from the marketplace)
2) The Red Hat Central page is displayed
3) Right click and select Reload
4) Try to click on one of the links of the page
Comment 14 Nick Boldt CLA 2018-10-16 09:36:00 EDT
Reopening since this appears to still exist in 2018-09.
Comment 15 Eric Williams CLA 2018-10-16 11:11:16 EDT
(In reply to Nick Boldt from comment #14)
> Reopening since this appears to still exist in 2018-09.

I can reproduce the issue on my machine, will investigate.
Comment 16 Eric Williams CLA 2018-10-16 11:19:02 EDT
I am getting an error: 

(WebKitWebProcess:18896): ERROR **: 11:16:31.054: SWT web extension: failed to execute script exception: SyntaxError: Unexpected end of script

which looks like it might be triggered if some JS is too long. Can you attach the JS for the BrowserFunction that is being used by the RH Central Page?
Comment 17 Eric Williams CLA 2018-10-16 13:57:18 EDT
(In reply to Eric Williams from comment #16)
> I am getting an error: 
> 
> (WebKitWebProcess:18896): ERROR **: 11:16:31.054: SWT web extension: failed
> to execute script exception: SyntaxError: Unexpected end of script
> 
> which looks like it might be triggered if some JS is too long. Can you
> attach the JS for the BrowserFunction that is being used by the RH Central
> Page?

Update: the issue reproduces only when first hovering over one of the links. For example, refreshing the page repeatedly without having hovered over a link does not reproduce the crash. As soon as I hover over one of the links, then refresh again, the bug happens.

It seems that when re-registering the BrowserFunctions on refresh, the JS is corrupted/truncated. There are two functions registered, and on refresh after hovering the first one is mostly missing except for the first 8 characters. This of course is a malformed JS function, and webkit will then crash.

I'll continue to investigate. Jeff, if you are on a Fedora 27 system can you try to see if it reproduces with Webkit1? I.e. SWT_WEBKIT2=0.
Comment 18 Jeff MAURY CLA 2018-10-16 15:59:20 EDT
Red Hat Central refuses to load because no GTK toolkit are found. Seems webkit1 is not provided anymore on F27 so what's the purpose of this test ?
Comment 19 Eric Williams CLA 2018-10-16 16:04:59 EDT
(In reply to Jeff MAURY from comment #18)
> Red Hat Central refuses to load because no GTK toolkit are found. Seems
> webkit1 is not provided anymore on F27 so what's the purpose of this test ?

Ah that's right, they removed Webkit1 in Fedora 27 already, not 28. So this test is meaningless.

I'm going to open a new ticket for this issue since I think there is a bit more going on (see comment 17).