C API example - cefcapi project on Github

Do not post support requests, bug reports or feature requests. Discuss CEF here. Non-CEF related discussion goes in General Discussion!

C API example - cefcapi project on Github

Postby Czarek » Tue Feb 11, 2014 2:55 pm

Hello,

I've created an example on how to use C API in CEF. Currently only linux example is available. Example creates a window, embeds a browser and displays a local example.html file. Shows how to implement dummy reference counting, and how to initialize CEF structures with callbacks to implement CEF handlers like cef_app_t and cef_client_t.

Project is hosted on github:
https://github.com/CzarekTomczak/cefcapi

TODO in plans:
1. Reference counting
2. Python script that will automatically generate callback functions and initialize_struct_name() functions for all capi header files.
3. Examples for Windows and Mac.

Marshall, would you mind taking a quick look at the code to see if there aren't any blatant irregularities in the code? It would be appreciated. Others will use it, so if you point out problems soon, you could save yourself from questions on the CEF forum in the future :)

Best regards.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: C API example - cefcapi project on Github

Postby magreenblatt » Tue Feb 11, 2014 4:10 pm

Sure :). Some comments:

1. https://github.com/CzarekTomczak/cefcap ... inux.c#L16

struct _cef_main_args_t mainArgs = {};

Does it not compile if you use "cef_main_args_t" instead of "struct _cef_main_args_t"? Same in other places.

2. https://github.com/CzarekTomczak/cefcap ... base.h#L24

void initialize_cef_callbacks(void* ptr, const void* methods[]) {

The implementation using a for loop isn't very efficient, and usage is problematic because changes in structural order won't be caught at compile time (for example, if a new function pointer is added). It would be clearer and less error prone to just assign the function pointers directly in initialize_app_handler, etc.

3. https://github.com/CzarekTomczak/cefcap ... base.h#L75

void initialize_cef_base(void* ptr) {

The logic here is rather magical. Consider passing in a cef_base_t* instead.
magreenblatt
Site Admin
 
Posts: 12407
Joined: Fri May 29, 2009 6:57 pm

Re: C API example - cefcapi project on Github

Postby Czarek » Tue Feb 11, 2014 5:23 pm

magreenblatt wrote:1. https://github.com/CzarekTomczak/cefcap ... inux.c#L16
struct _cef_main_args_t mainArgs = {};
Does it not compile if you use "cef_main_args_t" instead of "struct _cef_main_args_t"? Same in other places.

There is problem in Sublime Text editor that I'm using, it doesn't see "cef_main_args_t" structure. When I click F12 "Go to definition" it works only on "_cef_main_args_t".

I've added a comment at the top of main_linux.c to explain the reasoning and that there is a shorter way to declare structures.

magreenblatt wrote:2. https://github.com/CzarekTomczak/cefcap ... base.h#L24
void initialize_cef_callbacks(void* ptr, const void* methods[]) {
The implementation using a for loop isn't very efficient, and usage is problematic because changes in structural order won't be caught at compile time (for example, if a new function pointer is added). It would be clearer and less error prone to just assign the function pointers directly in initialize_app_handler, etc.

What would be the syntax so that structural order is checked at compile time? Hmm it's a bit embarrasing as I don't know other way than memcpy() ;-) The members of the structures are unnamed, so I cannot refer to them using ".name".

The goal is to have this generated by a python script, and in that case loop wouldn't be needed. I was creating it manually so created this helper function.

magreenblatt wrote:3. https://github.com/CzarekTomczak/cefcap ... base.h#L75
void initialize_cef_base(void* ptr) {
The logic here is rather magical. Consider passing in a cef_base_t* instead.

Right. Fixed. Magic removed :)

Thank you for the comments.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: C API example - cefcapi project on Github

Postby magreenblatt » Tue Feb 11, 2014 5:34 pm

Czarek wrote:
magreenblatt wrote:2. https://github.com/CzarekTomczak/cefcap ... base.h#L24
void initialize_cef_callbacks(void* ptr, const void* methods[]) {
The implementation using a for loop isn't very efficient, and usage is problematic because changes in structural order won't be caught at compile time (for example, if a new function pointer is added). It would be clearer and less error prone to just assign the function pointers directly in initialize_app_handler, etc.

What would be the syntax so that structural order is checked at compile time? Hmm it's a bit embarrasing as I don't know other way than memcpy() ;-) The members of the structures are unnamed, so I cannot refer to them using ".name".

What do you mean by unnamed? You can do:

Code: Select all
cef_base_t obj;
obj.add_ref = my_add_ref;
obj.release = my_release;
magreenblatt
Site Admin
 
Posts: 12407
Joined: Fri May 29, 2009 6:57 pm

Re: C API example - cefcapi project on Github

Postby Czarek » Tue Feb 11, 2014 9:48 pm

Ahhhh....... That weird syntax misled me and I thought that you can access these members only through their memory address. Oh my. It is so simple now :)

Now I try to imagine what was your face when you were looking at the code ;-)

For my excuse I was coding this through the night.

I always thought C was supposed to be hard, so I didn't even bother looking.

Done. Fixed. And let's forget this thing.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: C API example - cefcapi project on Github

Postby SamQ » Tue Dec 11, 2018 7:07 pm

Hello,

I've been trying to get this sample to run on macOS. Keep in mind, I am avoiding the c++ wrappers. Does a view need to be created for mac or what other settings are needed? When I package it all into an app (framework etc..), the window appears but no browser. I have a custom helper process which runs with what appears to be correct parameters along with the window. The helper and browser work fine on Windows and Linux.

All window_info members are 0/null/false except for:
window_info.width = 800;
window_info.height = 600;
window_info.window_name = make_cef_string(title);

Helper code:
cef_main_args_t main_args;
memset(&main_args, 0, sizeof(main_args));
cef_main_args_t main_args;
memset(&main_args, 0, sizeof(main_args));
cef_execute_process(&main_args, nullptr/*&app*/, nullptr/*windows_sandbox_info*/);

Helper command line:
--type=renderer --no-sandbox --service-pipe-token=9801674488663766557 --lang=en-US --log-file=/Users/Sam/1/debug.log --log-severity=verbose --num-raster-threads=4 --enable-zero-copy --enable-gpu-memory-buffer-compositor-resources --enable-main-frame-before-activation --service-request-channel-token=9801674488663766557 --renderer-client-id=3

debug.log output:
A few Adding CT log.....
[1211/154640.261298:ERROR:mach_port_broker.mm(43)] bootstrap_look_up: Unknown service name (1102)
[1211/154640.309780:VERBOSE1:network_delegate.cc(32)] NetworkDelegate::NotifyBeforeURLRequest: http://www.google.com/
[1211/154640.404081:ERROR:mach_port_broker.mm(43)] bootstrap_look_up: Unknown service name (1102)
[1211/154640.408579:INFO:cpu_info.cc(46)] Available number of cores: 8
[1211/154640.463516:VERBOSE1:network_delegate.cc(32)] NetworkDelegate::NotifyBeforeURLRequest: https://www.google.com/?gws_rd=ssl

-Thanks
SamQ
Newbie
 
Posts: 2
Joined: Tue Dec 11, 2018 6:42 pm

Re: C API example - cefcapi project on Github

Postby Czarek » Tue Dec 11, 2018 9:33 pm

This is a 4 year old topic and this is not a place for your issue.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: C API example - cefcapi project on Github

Postby SamQ » Wed Dec 12, 2018 1:13 pm

Great. Thanks for the help.
SamQ
Newbie
 
Posts: 2
Joined: Tue Dec 11, 2018 6:42 pm


Return to CEF Discussion

Who is online

Users browsing this forum: Google [Bot] and 16 guests