Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CEF Callbacks to execute Go Code (communication mechanism) #32

Open
z505 opened this issue Apr 15, 2017 · 5 comments
Open

CEF Callbacks to execute Go Code (communication mechanism) #32

z505 opened this issue Apr 15, 2017 · 5 comments

Comments

@z505
Copy link

z505 commented Apr 15, 2017

Do you know if any of the forks implement a way to call Go Code from html/javascript without having to use more convoluted solutions like an HTTP server/xml requests? i.e. something like a Javascript.Callback() which then sends some parameters into a go function which can be inspected there, and dealt with..

paperlesspost/cef2go fork seems mostly focused on Linux only?

If there is no code out there that offers this (prefer it to work on Windows/MacOsx, other OS's like bsd/linux are a bonus) then would you or anyone be interested in me setting up a bounty to get callbacks/ipc working? By bounty I mean money/payment for to get something working...

Other options to consider are usinggoogle protocol buffers for ipc, or a background http request that somehow communicates with go.

Would prefer to do something like this in my Go Code:

func CefCallback(params..)  {
  // inspect parameters here, which are sent from a javascript function
  // so that javascript can send messages back to GoLang code
}

Javascript:
CEF.CallBack(1,2,"string", "etc")

Obviously C++ and chromium embedded for delphi have something like this which Go lacks, as your go code right now requires and http server, which could work, but I would prefer just a simpler callback mechanism.

And I guess there also needs to be a way to send data to javascript, in addition to sending data back to go, so that it goes both ways

So do any of the forks have this functionality that you know of (hope they work on macosx and windows, not just Linux for example)... And if not, a bounty/payment for the work could be provided by myself? Any interest/knowledge on this? thanks

@cztomczak
Copy link
Owner

I wouldn't name it "callbacks", better to use "javascript bindings" for Go code. I will reserve "callback" name for functions passed as arguments that expect to be called back.

I am only aware of paperlesspost fork that implements javascript bindings. However it works only on Linux and only in single process mode. Single process mode is unstable and not suitable for production.

There is also fork by 24hours which had much development, but it doesn't support V8 bindings. I also don't see any such feature in fromkeith's fork.

Here are a few questions about what features you need:

  1. Do you also need to be able to pass javascript functions (callbacks) to Go code? As Go<>JS communication is asynchronous, the only way to return value is to pass a callback that can be later executed and passed a return value.
  2. Do you also need to be able to pass Go functions (callbacks) to javascript?
  3. It must be noted that current binaries are old and embed Chrome 33 beta. Are these fine or do you need updating to latest CEF?
  4. Do you need to pass big data between javascript/Go? For performance reasons in CEF big data shouldn't be passed using javascript bindings, it is recommended to pass it using requests either by implementing CEF ResourceHandler or by running an internal web-server.
  5. In CEF there is Browser.ExecuteFunction(string code) that allows to execute js code from native code. Would that be enough for you? (you can jsonize Go args data) The other way to communication from Go was mentioned in point 2), however that requires that communication is initiated first from javascript. With ExecuteFunction you can initiate communication from Go. This would require some refactoring in API, CreateBrowserSync would need to return a Browser object.

CEF Python project that I'm also maintaining has a complete implementation for javascript bindings with support for both python callbacks and javascript callbacks. I recommend reading how it works in Tutorial doc, so that you have a full understanding of the features available in CEF:
https://github.com/cztomczak/cefpython/blob/master/docs/Tutorial.md#javascript-integration

How much bounty would you like to set for this task?

@z505
Copy link
Author

z505 commented Apr 16, 2017

Executing Javascript (does this currently work?) would be a good way to send any data required over to the html/javascript side, but what's needed mostly is an ability to send data from Javascript/html back to Go.
One issue I see that will occur, is in multiprocess mode, the go code will not be able to access the code as things are in a separate process, as far as I understand:

Therefore SendProcessMessage() may also need to be implemented to send an IPC message from one of the chromium/go processes to the other go process where the actual logic of the application is. So a callback or handler has some args[] which then one can send to the go main program with the logic and all your (my) favourite code in it... By sendprocessmessage(args[1])

https://www.google.ca/search?q=cef%20sendprocessmessage

At least, this is how delphi/freepascal/lazarus works, which I'd like similar in GoLang.

Another potential way of sending data might be somehow use another chromium hidden tab and place data there (instead of a full blown web server, a tab doesn't require any ports open), but I haven't thought that through how that could work, and it may not even work... Or some kind of thing like a Magnet link that torrent uses, and send data using a custom URL like custom:// instead of magnet:// and then send data from the javascript to that url using url params, which then opens up a custom exe. But these are all just brain farts that may not work :-) ... A crude way to share data is also GetEnv/SetEnv but not sure again if that would work, and you'd never know when the data arrived there without some message notifying you a setenv was called to store the data for later getenv retrieval.

I do not know how much work it would take to make cef callbacks and SendProcessMessage() work and do not know what to put down as a bounty..... Do you have any ideas? Any idea how involved it could be? If go to CEF needs to be updated to a later version, another bounty may need to be added for that too

Generally, I don't need to pass BIG data between go/js, mostly just small integers and such.. But, maybe in the future I may send a 10KB text from an edit/textarea widget? or up to a few MB of data (a text editor/IDE similar to LIMEtext... but that's not something I've started yet.. I think Lime uses WebKit which is similar to cef. Not sure how WebKit callbacks work)

@z505
Copy link
Author

z505 commented Apr 16, 2017

And interestingly, I just found this:

https://github.com/lroc/cef/search?l=Go&q=callback&type=&utf8=%E2%9C%93

And this:

https://github.com/lroc/cef/search?l=Go&q=sendprocessmessage&type=&utf8=%E2%9C%93

Looks like there is a fork with some work being done...

So could possibly import this code into your repository even, if it works.. so maybe the work required is not as significant as I thought if someone else has already done half the work.

But, I am not sure which Operating Systems or platforms that code works on... And he says you must download Ming on windows. Hrmm.

https://github.com/lroc/cef

@cztomczak
Copy link
Owner

The lroc/cef fork looks like a fork of paperlesspost and possibly of other authors without acknowledgements. In the License file lroc attributed all copyright rights to himself, even my rights were removed. From the commits you can see that all commits history was cleared. Basically the code seems to be stolen and you don't know who owns copyrights.

The code you reference is far from complete. It doesn't work in multi-process mode which is the real deal. Javascript bindings can be implemented without the need to update CEF at this point.

It seems to me that you have a good understanding how things work, so you can try implement this yourself. You already have some code working in single process mode that you can treat as a reference. Pull request is welcome.

If you would like me to implement this I would be glad to take on this task for a 1,000 USD bounty.

@z505
Copy link
Author

z505 commented Apr 25, 2017

I will check in to lroc's code a bit to see what is required. That's really not right that he removed all the copyrights ! (in the Readme file he says "This is a modify version of https://github.com/CzarekTomczak/cef2go/" )

On github it appears you can't search forks, so there may be another fork of your project that has some code that would be useful. But without a search of forks, it's hard to find anything. The reason lroc's code is found using github search, is because it is not a fork but a new project pretending not to be a fork, even though it is. Not sure why github does not allow searching forks yet.

Okay I'll consider your bounty after looking through this. Will take a few days (weeks) for me to digest this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants