You are not logged in.

#1 2017-04-21 12:33:40

l1nuxfr3@k
Member
From: Novi Sad, Serbia
Registered: 2016-06-12
Posts: 20

[SOLVED] Problems with using webkit2gtk-4.0 and Vala

Hi all. smile I have an unusual problem, and I've been trying to solve it for a while now. I even posted a similar post to stackoverflow but the guys there just won't respond. I understand that Vala is relatively "unused" programming language, but being a guy with the Linux background, I think that it's very important language in development and I hope some of you here have mastered it enough to try to help me at least. I am not looking for code samples.. I just need to know what is going on with my project, because it behaves really weird. Here's the problem, and.. thanks in advance for your help:

So, recently I've been playing around with Vala and Gtk+3.0 and I decided to create a really simple web browser as a part of one of my apps, in order to show one of my webpages which uses Google Maps API to show some markers. I've created a class that looks like this:

using Gtk;
using WebKit;

public class WebBrowser : Window {

     private string URL = "";
     private WebView view;

    // Constructor
        public WebBrowser () {
           this.view = new WebView();
           var settings = this.view.get_settings();
           settings.enable_plugins = true;
           settings.enable_javascript = true;
           this.add(view);
    }

    public void visit (string url) {
         this.URL = url;
         this.view.load_uri(this.URL);
     }

 }

In the compile flags i used

--pkg gtk+-3.0 --pkg webkit2gtk-4.0

The class is used like this (will use random data here):

var browser = new WebBrowser();
browser.set_title ("Custom window title!");
browser.default_width = 1200;
browser.default_height = 800;
browser.visit ("[url]http://acid3.acidtests.org/[/url]");
browser.show_all ();

and it works. It scores 100/100 on the given test. The only problem is, I cannot use the actual WebKit object. When I resize the window, WebKit behaves normally and it resizes the webpage correctly, but I cannot close the window, nor interact with the webpage in any way. Simply put: close button doesn't work, which is weird because in the same app I have other windows, called exactly in the same way and those close correctly! As far as I know, clicking on the X automatically calls the destroy event which deals with window's delete-event. I don't really have to put my code in there. Another problem is that the WebKit itself doesn't allow me to use it in any way - touch and scroll are disabled. Besides that, window behaves normally and does not appear frozen. I've read a lot on this topic and 'till this day I couldn't solve my case. Some say I need the access to DOM and I don't know how to do it. Other's say I need some invisible layers on top of the browser in order to detect mouse clicks (... ?) . Why did they make things so weird with WebKit2? Maybe this is a bug: I am using Arch Linux with Cinnamon that I updated a month ago, even though I doubt there are any bugs in there. Thanks for your patience and have a nice day!

Last edited by l1nuxfr3@k (2017-04-30 09:00:41)


Proud Arch user, programmer & guitarist.

Offline

#2 2017-04-21 17:10:04

JohnBobSmith
Member
From: Canada
Registered: 2014-11-29
Posts: 804

Re: [SOLVED] Problems with using webkit2gtk-4.0 and Vala

As a programmer (no experience with vala/web coding) I see one flaw with your logic:

l1nuxfr3@k wrote:

...
WebKit behaves normally and it resizes the webpage correctly, but I cannot close the window, nor interact with the webpage in any way. Simply put: close button doesn't work ...

In most applications, you should require a "main loop". In this main loop, you would then check for events. For example, if we receive the close event, application.close(). If we interact with the window by clicking the "Whats on the menu?" button, display today's food choices. You are looking for possibly an event class, handler, or union. AFAIK, most libraries wont handle event handling automatically. This is good and even required. As the programmer, you can then control which events to respond to and which ones to ignore. Hopefully this helps smile


I am diagnosed with bipolar disorder. As it turns out, what I thought was my greatest weakness is now my greatest strength.

Everyday, I make a conscious choice to overcome my challenges and my problems. It's not easy, but its better than the alternative...

Offline

#3 2017-04-21 18:57:02

Steef435
Member
Registered: 2013-08-29
Posts: 577
Website

Re: [SOLVED] Problems with using webkit2gtk-4.0 and Vala

JohnBobSmith, GTK+ handles all this main loop stuff rather "automagically" using a callback/signal/observer system (unless you intentionally want to fiddle with it), so this is irrelevant in this case. Unless l1nuxfr3@k does not call the GTK main loop at all.

Most likely, the WebView works splendidly, but you just chose a painful test website. The only point of interaction is the link to the reference rendering. Clicking that will send you to a page which looks exactly like the page you came from (because WebKit passes the Acid3 test perfectly). The whole process is so fast that you don't see any flickering or whatever from the loading. Try a different webpage, it will most likely work. It should allow scrolling though, if you make the view small enough, and clicking the right mouse button should give you a default context menu as well.

If it still doesn't work, show a complete example. There's probably something going wrong with your invocation of GTK+ and the main loop.

Don't create "invisible layers on top of the browser", that sounds like a horrible idea. WebKit2 feels awkward because it uses a multi-process model: all the web processing is handled in a separate process, the WebView in your application process is simply an image of what the web process generates. Therefore, if you want to access the DOM, you'll have to do so from the web process side.

There are several ways to do this. One is to simply inject JavaScript from the application side, see WebView.run_javascript. This is the quickest way, but also not the prettiest because if JavaScript is disabled in the view, this code won't fly either. Another way is to literally enter the web process by writing a so-called web process extension (object file loaded by the web process at runtime). There is a discouraging small amount of documentation on the subject (this is probably the best, but it's C, the Epiphany browser is a good reference, but it's fat and also C), feel free to reach out once you get to that point and can't figure it out (I have more experience with WebKit2GTK+ and Vala than I'd like to admit given the results). Spoiler: you'll need something like this as the entry point of an extension:

[CCode (cname="G_MODULE_EXPORT webkit_web_extension_initialize_with_user_data", has_target = false)]
public static void webkit_web_extension_initialize_with_user_data(WebKit.WebExtension extension, Variant user_data) {
    extension.page_created.connect((page) => message("Page %" + uint64.FORMAT + " created", page.get_id()));
}

Last edited by Steef435 (2017-04-21 18:57:28)

Offline

#4 2017-04-21 19:37:05

progandy
Member
Registered: 2012-05-17
Posts: 5,184

Re: [SOLVED] Problems with using webkit2gtk-4.0 and Vala

In your example I can use the webpage, I just threw your example code in a static init main. Scrolling with my mouse works without problems. I can't test the close button, since I use i3 as my window manager. Closing it with "wmctrl -c" works.
Edit: Of course, it only closes the window. The process doesn't quit unless you connetc a quit call to the destroy event

using Gtk;
using WebKit;

public class WebBrowser : Window {

     private string URL = "";
     private WebView view;

    // Constructor
        public WebBrowser () {
           this.view = new WebView();
           var settings = this.view.get_settings();
           settings.enable_plugins = true;
           settings.enable_javascript = true;
           this.add(view);

           // optional: close process on window close.
           // this.destroy.connect(Gtk.main_quit);
    }

    public void visit (string url) {
         this.URL = url;
         this.view.load_uri(this.URL);
     }


public static int main(string[] args) {
    Gtk.init(ref args);

    var browser = new WebBrowser();
    browser.set_title ("Custom window title!");
    browser.default_width = 1200;
    browser.default_height = 800;
    browser.visit ("https://duckduckgo.com/");
    browser.show_all ();
    Gtk.main();

    return 0;
}
}

Last edited by progandy (2017-04-21 19:56:21)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#5 2017-04-22 16:34:08

l1nuxfr3@k
Member
From: Novi Sad, Serbia
Registered: 2016-06-12
Posts: 20

Re: [SOLVED] Problems with using webkit2gtk-4.0 and Vala

Thank you all for nice responses. smile I will try to respond to each one of you here in this post so it might get long! Now, I would love to send some more samples but I don't really know where to start from, because I've managed to create a huge project which uses a lot of dependencies (libarchive, gee, json, mysql, webkit etc.. ) that are managed with autotools and Anjuta IDE. First off, @progandy, I've tested the code and it works (I can interact with the webpage), but to do so I had to make a separate file, and I compiled it in a separate directory (so, nowhere near my project). That means there are no bugs in the webkit itself, but somewhere with my windows and loops. I used an async function to send a GET request to a web server over TCP, then I retreived a JSON string as a response to that GET request, then parsed it and made a TreeView entry from it. Once I click on the entry, it is supposed to load a browser window and it fails there. The window shows, the page loads, but I just cannot use the web view. It doesn't respond to touches or keyboard events. On top of that, the window that is the parent of that web view cannot be closed/destroyed. So, I repeat: the webkit is not frozen. If I was to visit a YouTube video, it would run smoothly, but I would not be able to interact with it in any way, nor close the window. Now I will try to send some samples and I hope you guys figure it out. And one more thing, I've had this issue for a long time: 

Gtk-CRITICAL **: gtk_main_quit: assertion 'main_loops != NULL' failed

. Could this be what's causing the issue? (this happens when I try to close the main window) I've found a nasty workaround by literally killing the app when I press the X button on my main window. I bet this has something to do with it, but why do other windows work well and the browser doesn't? To explain this a little more, I will add: when I launch the app, I have the main window with a treeview. Once I select something there, I get an another window with an another treeview (you must say that I really like treeviews big_smile).. Then, when I select something there, an another treeview pops up in the same window! Then, when I select something there, I should see a browser window. It's confusing but looks neat and is useful because it organizes a bunch of data.

This is the entry point:

static int main (string[] args)  {
    Gtk.init (ref args);
    new Main ();
    Gtk.main ();
    return 0;
}

Main is an object which reads from my .ui file and setups the environment (main window), and yes, I have connected the window destroy event with

Gtk.main_quit ()

!

So, this is how I get some data from a treeview responsible for the call of the browser:

private void on_bustree_activated (Gtk.TreeView treeview , Gtk.TreePath path, Gtk.TreeViewColumn column) {
        try {
			Gtk.TreeIter iter;
		    if (treeview.model.get_iter (out iter, path)) {
		        string result;
				treeview.model.get (iter, 0, out result);
				stdout.printf("%s\n", result);
				var regex = new Regex("(.+) \\| (.+) \\| @ (.+), (.+) < (.+)met \\/ (\\d+)?h?(\\d+)?m?(\\d+)s");
				string lon_s = "", lat_s = "", bid_s = "";
				MatchInfo info;
				if(regex.match(result, 0, out info)) {
					lon_s = info.fetch(4);
					lat_s = info.fetch(3);
					bid_s = info.fetch(1);
				}
				stdout.printf ("lat %s lon %s\n", lat_s, lon_s);
				track_bus (double.parse (lat_s), double.parse (lon_s), int.parse(bid_s) , selected_station.id);
		    }
		} catch (GLib.Error e) {
			stderr.printf("Error: %s\n", e.message);
		}
    }

and this is how I show the browser window:

public void track_bus (double latitude, double longitude, int busid, int stationid) {
        var browser = new WebBrowser(stationwindow);
        browser.set_title (@"Tracking: $(busid)");
        browser.default_width = 1200;
        browser.default_height = 800;
        browser.visit(@"file://$(utils.cachedir)/tragac/tragac.html?lat=$(latitude)&log=$(longitude)&bid=$(busid)&sid=$(stationid)");
        browser.show_all ();
}

what my app does is: it works with huge MySQL databases, and allows users to search them (that's why I needed treeviews).
Once they select a bus they would like to track, the browser would have to load a page from the .cache directory which uses ajax and Google Maps API to refresh the bus location every 5 seconds. The webpage works on Google Chrome, so, let's not talk about that. big_smile I hope I helped. Sorry if I didn't. I just don't have an idea where to look for the solution.  This has been bugging me for days...


@Steef435 and to test the main loop, I replaced the code that calls my Main object with the one that calls the browser. It works.
So, the error must be in the way I call the browser when the treeview gets activated. Or, there must be some other part of the code that somehow fails.

Last edited by l1nuxfr3@k (2017-04-27 10:23:50)


Proud Arch user, programmer & guitarist.

Offline

#6 2017-04-23 14:15:44

Steef435
Member
Registered: 2013-08-29
Posts: 577
Website

Re: [SOLVED] Problems with using webkit2gtk-4.0 and Vala

Do you perhaps use GApplication? In that case, you shouldn't call Gtk.main() manually and Gtk.main_quit() isn't the correct way to "close" the GApplication either. It may have something to do with that. If you use GApplication, you should also "add" the windows to the application using Application.add_window. (The application will quit if all windows added using add_window() are closed).

EDIT: I suppose you don't override the main event handler of the window anywhere, right?

Last edited by Steef435 (2017-04-23 14:17:54)

Offline

#7 2017-04-23 16:15:23

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: [SOLVED] Problems with using webkit2gtk-4.0 and Vala

l1nuxfr3@k, please consider using BBCode code tags around program output and file contents.  It makes nice little boxes around your code like teef435's post #3 and progandy's post #4.  monospaced fonts and scroll bars are automatic.  Your readers will appreciate it.  That link is also available under every message post box on the forums as a convenient reference.

Thanks


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Online

#8 2017-04-27 11:04:05

l1nuxfr3@k
Member
From: Novi Sad, Serbia
Registered: 2016-06-12
Posts: 20

Re: [SOLVED] Problems with using webkit2gtk-4.0 and Vala

All right, chief! I've fixed it, hope it now looks better! @Steef435, thanks for a valuable comment. I haven't used it but it's always good to learn something new! I've fixed my issue since when I last posted, and I have no idea what caused it, but since you guys put a lot of effort into responsing me with ideas, I owe you to at least try to explain what caused my problems.. All of those issues were proven to exist, so this is my hypothesis: I have a class that servers as a utility class. The class uses other classes that I've made, and some of them use signals! Now, once for example, a web page is downloaded, my TCPDownloader class, which downloads files asynchronously, emits a signal that says "okey, the page was downloaded successfully". This class was used in the Utilities class and the

download_finished()

signal was connected to a lambda expression (function/method). This method would then emit the

web_page_downloaded()

signal. No, those two weren't connected directly, because the other method actually needed one more argument so the methods were incompatible. Summed up: once page is ready, the Utilities class would emit a signal. This worked, but it caused a weird behavior when called from my Timer class, my self-made timer which proved to be working properly (even though, it's not one of my proudest codes yet big_smile). It would emit a

tick()

signal every 10 seconds. This would call those utility methods (to download web pages) which used TCPDownloader class.. This caused something I've never seen in my life before: every time my timer would tick, number of calls to my client (downloader) would be okay, but number of responses from the client would rise exponentially. The response was usually in JSON, so within the first minute, my parser had to struggle with like a 100 parallel tasks, and the number would rise and eat my RAM in a few minutes! Every time a JSON string was processed, it would run a javascript on the active WebView page. I've fixed this issue by copying the part of the utilities class that deals with downloads to my working class, which got rid of those lambda expressions, made a direct connection between signals and corresponding methods, and lowered the number of signals necessary to complete the task. Also, I've realized that my browser's parent window blocked my keyboard with the

on_key_pressed()

method which returned true. Everything that I would type in the browser was actually in a search box on the other window big_smile Also, one of my loops failed at the login screen which caused the critical error I posted earlier about. Phew.. big_smile Thanks for assistance! I will leave this thread open for a little bit, then mark it as solved. smile


Proud Arch user, programmer & guitarist.

Offline

#9 2017-04-27 11:08:42

l1nuxfr3@k
Member
From: Novi Sad, Serbia
Registered: 2016-06-12
Posts: 20

Re: [SOLVED] Problems with using webkit2gtk-4.0 and Vala

One more question, does anyone here know how to properly debug Vala applications using Anjuta IDE with autotools? (or should I post a new thread for this question?) big_smile


Proud Arch user, programmer & guitarist.

Offline

#10 2017-04-27 15:00:49

Steef435
Member
Registered: 2013-08-29
Posts: 577
Website

Re: [SOLVED] Problems with using webkit2gtk-4.0 and Vala

Good thing you got it fixed ^^

Not a clue. You need to pass the -g option to valac for the symbols at least. Not a solution, but I believe GNOME Builder has better Vala support. (I don't use either)

Offline

#11 2017-04-28 13:01:24

l1nuxfr3@k
Member
From: Novi Sad, Serbia
Registered: 2016-06-12
Posts: 20

Re: [SOLVED] Problems with using webkit2gtk-4.0 and Vala

The thing is, I've read about GNOME Builder, but have avoided it since because of the "heavy development". It's good to know a tool is under development but that also means it's really buggy and I was not sure if it was worth it. Now, I think it's time to twist it since my no. 1 choice, Anjuta IDE disappointed me. Debug option simply doesn't work and they blame Vala for it because of some "bad integration with autotools". Maybe that's true but I would like to try other alternatives. I've found one solution but it doesn't do it for me since I use Gtk:

valac -g --save-temps debug-demo.vala
nemiver debug-demo

it's okay if you use plain Vala, but nemiver cannot handle Gtk for some reason (it just freezes after a few seconds)


Proud Arch user, programmer & guitarist.

Offline

#12 2017-04-28 15:47:51

Steef435
Member
Registered: 2013-08-29
Posts: 577
Website

Re: [SOLVED] Problems with using webkit2gtk-4.0 and Vala

Weird, I would expect it to work with GTK+ as it's a GNOME project, and I believe it's the one used in Builder as well. Could be that it's just very slow. What happens if you use gdb directly?

EDIT: Just tried it myself with Nemiver and it works properly (very small app though).

Last edited by Steef435 (2017-04-28 15:51:53)

Offline

#13 2017-04-29 15:32:37

l1nuxfr3@k
Member
From: Novi Sad, Serbia
Registered: 2016-06-12
Posts: 20

Re: [SOLVED] Problems with using webkit2gtk-4.0 and Vala

Well, it runs so slow that it freezes on my PC. I am working on a huge app though, and I believe it has something to do with multi-threading.
Good news.. I've managed to fix issues with Anjuta IDE! So I've switched to my debug configuration and edited this file: project_folder/Debug/src/Makefile. I've replaced this particular line:

VALACOMPILE = $(VALAC) $(AM_VALAFLAGS) $(VALAFLAGS)

with:

VALACOMPILE = $(VALAC) -g --save-temps $(AM_VALAFLAGS) $(VALAFLAGS)

then went back to the Debug directory ( cd ../ )  and ran make.

Now my debugger works, I can debug normally big_smile
The only problem is, when stepping to the next line of code, in some scenarios, I have to press F6 many times to actually "step" (I guess it actually steps through the underlying C code and sometimes, for complex "one liners", I had to press F6 around 20 times to actually highlight the next line of code big_smile)


Proud Arch user, programmer & guitarist.

Offline

Board footer

Powered by FluxBB