Introduction
The c api of CEF is a C-based interface exposed by libcef DLL. The interface defined in the cef_capi.h header file is a C ++ API image automatically generated by CEF translator tool.
Reference count
Understanding the reference count may be the most difficult part to use the cef c api. The reference count concept of CEF is similar to that of COM, there are some basic rules that can help you reduce the difficulty of using the reference count.
1. When you pass a structure to its own member functions, do not add or subtract reference counts:
Struct -> Call_func ( Struct ,...); // No reference counting change on 'struct'
2. Add reference count before passing the structure as a parameter to other structures:
// Shocould have already added a reference to 'some _ other_struct' Struct -> Call_func (..., Some_other_struct ,...);
3. Reduce the reference count after you use the structure passed by parameters from other sources:
Void My_func (..., Some_other_struct ,...) { // Remove a reference from 'some _ other_struct 'after you' re done using it }
4. Input a processingProgramAdd its reference, for example, cef_create_browser (). When the API no longer needs to use a processing program, it will remove its reference.
5. Use atomic reference count implementation, because add_ref and release may be called across threads, The winapi interlockedincrement () and interlockeddecrement () functions can be used for this purpose.
6. If the reference computation turns to 0, the handler should delete itself in the releae () callback function assigned to the structure:
// Custom data structure that extends cef_handler_t
Typedef Struct _ My_handler_t {
Cef_handler_t Handler ; // Cef_handler_t member comes first
// Custom Members here, including the reference count variable
} My_handler_t ;
// Allocate the custom data structure (already initialized to zero)
My_handler_t * My_handler = ( My_handler_t *) Calloc ( 1 , Sizeof ( My_handler_t ));
// Set the size member of cef_base_t appropriately
My_handler -> Handler . Base . Size = Sizeof ( Cef_handler_t );
// Assign the release callback function (and the other callback functions)
My_handler -> Handler . Base = My_release ;
...
// Release callback function implementation for my_handler_t
Int Cef_callback my_release ( Struct _ Cef_base_t * Base ) {
// This cast works because cef_base_t is the first member
// Cef_handler_t and cef_handler_t is the first member of my_handler_t
My_handler_t * My_handler = ( My_handler_t *) Base ;
// Decrement the reference count (stored as a custom member of my_handler_t)
// Free the my_handler_t structure when the reference count reaches zero
If ( Reference_count_is_zero )
Free ( My_handler);
}
7. ClearCodeAll additional references added to the structure (for example, the reference of the cef_broswer_t pointer is maintained in the handler Implementation). The chance to release the reference is cef_handler_t: handle_before_window_closed ().
8. if no crash is generated and the debugobjct asserted is hit during shutdown, the reference count is correctly processed.