Frame in on_load_error does not have a valid frame_id

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Frame in on_load_error does not have a valid frame_id

Postby aleitner » Thu Feb 22, 2024 9:49 am

This topic has been moved to the relevant board.

I have implemented code to utilize browser_host->execute_dev_tools_method using the id of the frame passed into cef_load_handler->on_load_error for the frameId parameter.

However my cef_dev_tools_message_observer->on_dev_tools_message method is being called with the following message:

Code: Select all
{"id":4,"error":{"code":-32000,"message":"No frame for given id found"}}


Why would the frame in on_load_error be giving me an id that is not found when used to setDocumentContent? Is the frame Id expected to be formatted differently? 38654705668

Example of the data being used to construct the method and parameters
Code: Select all
Method: "Page.setDocumentContent"

Parameters:
"frameId" "38654705668"
"html" "<html><body><h1>Oops, something went wrong...</h1></body></html>"


Function for setting the document content:
Code: Select all
int set_document_for_frame(cef_frame_t* frame, const char* html_content) {
    if (!frame || !html_content)
        return -1;

    /* Retrieve the browser host from the frame's browser */
    cef_browser_t* browser = frame->get_browser(frame);
    if (!browser)
        return -1;

    cef_browser_host_t* browser_host = browser->get_host(browser);
    if (!browser_host)
        return -1;

    guac_http_cef_dev_tools_message_observer_t* observer = guac_http_cef_dev_tools_message_observer();

    browser_host->add_dev_tools_message_observer(browser_host,
            (cef_dev_tools_message_observer_t*)observer);

    /* Construct the DevTools method name */
    cef_string_t method= {};
    cef_string_from_utf8("Page.setDocumentContent", 23, &method);

    /* Create the object for holding the DevTools method parameters */
    cef_dictionary_value_t* params = cef_dictionary_value_create();

    /* Retrieve the frame's ID as an int64_t */
    int64_t frame_id = frame->get_identifier(frame);

    /* Create the frameId parameter */
    cef_string_t frame_id_key = {};
    cef_string_from_utf8("frameId", 7, &frame_id_key);

    char frame_id_str[sizeof(int64_t)*8+1];
    sprintf(frame_id_str, "%lu", frame_id);
    cef_string_t frame_id_str_cef = {};
    cef_string_from_utf8(frame_id_str, strlen(frame_id_str), &frame_id_str_cef);

    /* Set the frameId in the parameters dictionary */
    params->set_string(params, &frame_id_key, &frame_id_str_cef);

    /* Create the html parameter */
    cef_string_t html_key = {};
    cef_string_from_utf8("html", 4, &html_key);

    cef_string_t html_content_str = {};
    cef_string_from_utf8(html_content, strlen(html_content), &html_content_str);

    /* Set the html in the parameters dictionary */
    params->set_string(params, &html_key, &html_content_str);

    /* Execute the DevTools method */
    int message_id = browser_host->execute_dev_tools_method(
        browser_host, 0, &method, params);

    /* Release resources */
    cef_string_clear(&method);
    cef_string_clear(&frame_id_key);
    cef_string_clear(&frame_id_str_cef);
    cef_string_clear(&html_key);
    cef_string_clear(&html_content_str);

    /* If message_id is 0, the command was not successful */
    return (message_id == 0) ? -1 : 0;
}

aleitner
Techie
 
Posts: 49
Joined: Fri Jun 16, 2023 12:05 pm

Re: Frame in on_load_error does not have a valid frame_id

Postby magreenblatt » Thu Feb 22, 2024 10:38 am

Is it the main frame? OnLoadError for an iframe may mean that the frame is not created.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Frame in on_load_error does not have a valid frame_id

Postby aleitner » Thu Feb 22, 2024 11:04 am

magreenblatt wrote:Is it the main frame? OnLoadError for an iframe may mean that the frame is not created.


It is the main frame

Code: Select all
    int64_t frame_id = frame->get_identifier(frame);
    int is_main  = frame->is_main(frame);
    cef_frame_t* main_frame = browser->get_main_frame(browser);
    int64_t main_frame_id = main_frame->get_identifier(main_frame);


Frame_id: 38654705668, is_main: 1, main_frame_id: 38654705668
aleitner
Techie
 
Posts: 49
Joined: Fri Jun 16, 2023 12:05 pm

Re: Frame in on_load_error does not have a valid frame_id

Postby magreenblatt » Thu Feb 22, 2024 11:20 am

The CEF frame ID is not the same as the browser-internal frame ID. You might need to use something like https://stackoverflow.com/questions/435 ... o-its-body
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Frame in on_load_error does not have a valid frame_id

Postby amaitland » Thu Feb 22, 2024 3:19 pm

Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am

Re: Frame in on_load_error does not have a valid frame_id

Postby aleitner » Fri Feb 23, 2024 10:07 am

amaitland wrote:Use https://chromedevtools.github.io/devtoo ... tFrameTree to get frame information.


I've noted that within the CSharp library, extracting the frame id is simplified, which stands in contrast to the C API. In the C API, fetching the result from the getFrameTree call requires the setup of a bespoke observer, adding to the complexity. A potential enhancement to the CAPI could involve the introduction of a more direct function. This new method could execute DevTools commands while populating the message and message_size parameters automatically:

Code: Select all
int(CEF_CALLBACK* execute_dev_tools_method)(
    struct _cef_browser_host_t* self,
    int message_id,
    const cef_string_t* method,
    struct _cef_dictionary_value_t* params,
    const void* message,
    size_t message_size);


Such an addition would streamline the process, mirroring the more user-friendly C# implementation, ultimately simplifying the DevTools interaction within the CEF C API.
aleitner
Techie
 
Posts: 49
Joined: Fri Jun 16, 2023 12:05 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 191 guests