|
| 1 | +use cef::{ImplV8Handler, ImplV8Value, V8Value, WrapV8Handler, rc::Rc, v8_context_get_current_context}; |
| 2 | + |
| 3 | +use crate::cef::ipc::{MessageType, SendMessage}; |
| 4 | + |
| 5 | +pub struct BrowserProcessV8HandlerImpl { |
| 6 | + object: *mut cef::rc::RcImpl<cef::sys::_cef_v8_handler_t, Self>, |
| 7 | +} |
| 8 | + |
| 9 | +impl BrowserProcessV8HandlerImpl { |
| 10 | + pub(crate) fn new() -> Self { |
| 11 | + Self { object: std::ptr::null_mut() } |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +impl ImplV8Handler for BrowserProcessV8HandlerImpl { |
| 16 | + fn execute( |
| 17 | + &self, |
| 18 | + name: Option<&cef::CefString>, |
| 19 | + _object: Option<&mut V8Value>, |
| 20 | + arguments: Option<&[Option<V8Value>]>, |
| 21 | + _retval: Option<&mut Option<V8Value>>, |
| 22 | + _exception: Option<&mut cef::CefString>, |
| 23 | + ) -> ::std::os::raw::c_int { |
| 24 | + if let Some(name) = name { |
| 25 | + if name.to_string() == "sendNativeMessage" { |
| 26 | + let Some(args) = arguments else { |
| 27 | + tracing::error!("No arguments provided to sendNativeMessage"); |
| 28 | + return 0; |
| 29 | + }; |
| 30 | + let Some(arg1) = args.first() else { |
| 31 | + tracing::error!("No arguments provided to sendNativeMessage"); |
| 32 | + return 0; |
| 33 | + }; |
| 34 | + let Some(arg1) = arg1.as_ref() else { |
| 35 | + tracing::error!("First argument to sendNativeMessage is not an ArrayBuffer"); |
| 36 | + return 0; |
| 37 | + }; |
| 38 | + if arg1.is_array_buffer() == 0 { |
| 39 | + tracing::error!("First argument to sendNativeMessage is not an ArrayBuffer"); |
| 40 | + return 0; |
| 41 | + } |
| 42 | + |
| 43 | + let size = arg1.array_buffer_byte_length(); |
| 44 | + let ptr = arg1.array_buffer_data(); |
| 45 | + let data = unsafe { std::slice::from_raw_parts_mut(ptr as *mut u8, size) }; |
| 46 | + |
| 47 | + v8_context_get_current_context().send_message(MessageType::SendToNative, data); |
| 48 | + |
| 49 | + return 1; |
| 50 | + } |
| 51 | + } |
| 52 | + 1 |
| 53 | + } |
| 54 | + |
| 55 | + fn get_raw(&self) -> *mut cef::sys::_cef_v8_handler_t { |
| 56 | + self.object.cast() |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl Clone for BrowserProcessV8HandlerImpl { |
| 61 | + fn clone(&self) -> Self { |
| 62 | + unsafe { |
| 63 | + let rc_impl = &mut *self.object; |
| 64 | + rc_impl.interface.add_ref(); |
| 65 | + } |
| 66 | + Self { object: self.object } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl Rc for BrowserProcessV8HandlerImpl { |
| 71 | + fn as_base(&self) -> &cef::sys::cef_base_ref_counted_t { |
| 72 | + unsafe { |
| 73 | + let base = &*self.object; |
| 74 | + std::mem::transmute(&base.cef_object) |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl WrapV8Handler for BrowserProcessV8HandlerImpl { |
| 80 | + fn wrap_rc(&mut self, object: *mut cef::rc::RcImpl<cef::sys::_cef_v8_handler_t, Self>) { |
| 81 | + self.object = object; |
| 82 | + } |
| 83 | +} |
0 commit comments