SendProcessMessage changes data

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.

SendProcessMessage changes data

Postby Staxcelrom » Fri Aug 19, 2022 1:50 pm

Hello,

I observe very strange behavior:

Code: Select all
class my_callback_Cpp___ : public CefV8Handler
{

public:


   bool Execute(const CefString& name, CefRefPtr<CefV8Value> object, const CefV8ValueList& arguments, CefRefPtr<CefV8Value>& retval, CefString& exception) override
   {
                std::string my_string_data;    //inside my data


                                if ((*object).IsString() == true)
                  {
            my_string_data= (*my_CefV8Value).GetStringValue();
                                 }



                std::cout<<"my_string_data:" << my_string_data<<std::endl;
                std::cout<<"my_string_data_SIZE:" << my_string_data<<std::endl;

                CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create(*my_string_data);                 
      browser_->GetMainFrame()->SendProcessMessage(PID_BROWSER, msg);             
        }

}


the following @my_string_data@ is output to the console:

Code: Select all
my_string_data:   29744672   999   0   noneю RIFFпї?пї?  WE
my_string_data_SIZE: 50


Next I get my_string_data in OnProcessMessageReceived in Browser procces:

Code: Select all
bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefProcessId source_process, CefRefPtr<CefProcessMessage> message) override
   {
                std::string CefProcessMessage_message_name;

                 CefProcessMessage_message_name_temp = message->GetName();

                std::cout << "CefProcessMessage_message_name:"<< CefProcessMessage_message_name <<std::endl;
      std::cout << "CefProcessMessage_message_name_SIZE:" << CefProcessMessage_message_name.size() << std::endl;

        }



Code: Select all
CefProcessMessage_message_name:   29308512   999   0   noneпї? RIFFпї?пї?  WE
CefProcessMessage_message_name_SIZE: 52


That is, during the transfer of the string - 2 bytes were added ??


Code: Select all
 29744672   999   0   noneю RIFFпї?пї?  WE
 29308512   999   0   noneпї? RIFFпї?пї?  WE


Or am I misunderstanding something?
Staxcelrom
Expert
 
Posts: 206
Joined: Wed Jan 26, 2022 8:20 am

Re: SendProcessMessage changes data

Postby magreenblatt » Fri Aug 19, 2022 2:19 pm

Looks like a string encoding/decoding issue. What is the character encoding of the source string from JavaScript?

More generally, process message strings should be ASCII or UTF-8 encoded. Process message contents can contain a blob type (CefBinaryValue) if you need to send arbitrary binary data.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: SendProcessMessage changes data

Postby Staxcelrom » Fri Aug 19, 2022 2:55 pm

magreenblatt wrote:Looks like a string encoding/decoding issue. What is the character encoding of the source string from JavaScript?

More generally, process message strings should be ASCII or UTF-8 encoded. Process message contents can contain a blob type (CefBinaryValue) if you need to send arbitrary binary data.


inside the javascript I put the binary data (image buffer) in a String and passed it to C++.
Apparently it's wrong to do so.

Although the buffer change happened after the call to SendProcessMessage, theoretically it's not about encoding / decoding.
Staxcelrom
Expert
 
Posts: 206
Joined: Wed Jan 26, 2022 8:20 am

Re: SendProcessMessage changes data

Postby magreenblatt » Fri Aug 19, 2022 4:10 pm

Strings in JavaScript are usually UTF-16. Assigning to CefString then implicitly converts it to UTF-8. When your JavaScript string instead contains arbitrary binary data (when it’s supposed to be UTF-16) you will get unexpected/incorrect results.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: SendProcessMessage changes data

Postby magreenblatt » Fri Aug 19, 2022 4:17 pm

Actually, CefString is still UTF-16 by default so the conversion to UTF-8 is happening when you assign to std::string (and maybe somewhere else internally).
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: SendProcessMessage changes data

Postby Staxcelrom » Sat Aug 20, 2022 3:49 am

Is it possible to pass a std::string from the Render process to the Browser process - without transformations, just as data in memory, as a copy?
Staxcelrom
Expert
 
Posts: 206
Joined: Wed Jan 26, 2022 8:20 am

Re: SendProcessMessage changes data

Postby Staxcelrom » Sat Aug 20, 2022 6:15 am

Maybe it will be useful for someone - this is how you can pass binary data through the process:

From the render process:
Code: Select all
bool Execute(...)  override
{
std::string my_string = "my_binary_data";

CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("msg_cross_process");

CefRefPtr<CefBinaryValue> CefBinaryValue_binary = CefBinaryValue::Create(&my_string[0], my_stringos.size());
msg->GetArgumentList()->SetBinary(0, CefBinaryValue_binary);

browser_->GetMainFrame()->SendProcessMessage(PID_BROWSER, msg);
}



In the browser process:
Code: Select all
bool OnProcessMessageReceived(...) override
   {
CefRefPtr<CefBinaryValue>CefBinaryValue_binary = message->GetArgumentList()->GetBinary(0);
size_t my_size = (*CefBinaryValue_binary).GetSize();

std::string my_binary_string;
my_binary_string.resize(my_size);

(*CefBinaryValue_binary).GetData(&my_binary_string[0], my_size , 0);

std::cout << my_binary_string<< std::endl;
        }
Staxcelrom
Expert
 
Posts: 206
Joined: Wed Jan 26, 2022 8:20 am


Return to Support Forum

Who is online

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