Auto merge of #6435 - Ms2ger:cef, r=zmike

Various CEF refactoring.



<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/6435)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2015-06-20 11:17:49 -06:00
commit 6e04c12bce
6 changed files with 77 additions and 110 deletions

View file

@ -32,12 +32,11 @@ fn command_line_new() -> *mut command_line_t {
pub fn command_line_init(argc: c_int, argv: *const *const u8) {
unsafe {
let mut a: Vec<String> = vec!();
for i in 0..(argc as usize) {
let slice = ffi::CStr::from_ptr(*argv.offset(i as isize) as *const c_char);
let s = str::from_utf8(slice.to_bytes()).unwrap();
a.push(s.to_owned());
}
let args = slice::from_raw_parts(argv, argc as usize);
let a = args.iter().map(|&arg| {
let slice = ffi::CStr::from_ptr(arg as *const c_char);
str::from_utf8(slice.to_bytes()).unwrap().to_owned()
}).collect();
let cl = command_line_new();
(*cl).argc = argc;
(*cl).argv = a;

View file

@ -71,7 +71,7 @@ pub extern "C" fn cef_string_utf8_clear(cs: *mut cef_string_utf8_t) {
(*cs).dtor.map(|dtor| dtor((*cs).str));
(*cs).length = 0;
(*cs).str = 0 as *mut u8;
(*cs).dtor = mem::transmute(0 as *const u8);
(*cs).dtor = None;
}
}
@ -89,19 +89,19 @@ pub extern "C" fn cef_string_utf8_set(src: *const u8, src_len: size_t, output: *
unsafe {
if copy != 0 {
if !src.is_null() && src_len > 0 {
(*output).str = libc::calloc(1, src_len + 1) as *mut u8;
if (*output).str.is_null() {
return 0;
}
(*output).str = libc::calloc(1, src_len + 1) as *mut u8;
if (*output).str.is_null() {
return 0;
}
ptr::copy(src, (*output).str, src_len as usize);
(*output).length = src_len;
(*output).dtor = Some(string_utf8_dtor as extern "C" fn(*mut u8));
ptr::copy(src, (*output).str, src_len as usize);
(*output).length = src_len;
(*output).dtor = Some(string_utf8_dtor as extern "C" fn(*mut u8));
}
} else {
(*output).str = mem::transmute(src);
(*output).length = src_len;
(*output).dtor = mem::transmute(0 as *const u8);
(*output).str = src as *mut _;
(*output).length = src_len;
(*output).dtor = None;
}
}
return 1;
@ -151,7 +151,7 @@ pub extern "C" fn cef_string_utf16_clear(cs: *mut cef_string_utf16_t) {
(*cs).dtor.map(|dtor| dtor((*cs).str));
(*cs).length = 0;
(*cs).str = 0 as *mut c_ushort;
(*cs).dtor = mem::transmute(0 as *const u8);
(*cs).dtor = None;
}
}
@ -169,20 +169,20 @@ pub extern "C" fn cef_string_utf16_set(src: *const c_ushort, src_len: size_t, ou
unsafe {
if copy != 0 {
if !src.is_null() && src_len > 0 {
(*output).str = libc::calloc(1, (src_len + 1) * mem::size_of::<c_ushort>() as u64) as
*mut u16;
if (*output).str.is_null() {
return 0;
}
(*output).str = libc::calloc(1, (src_len + 1) * mem::size_of::<c_ushort>() as u64) as
*mut u16;
if (*output).str.is_null() {
return 0;
}
ptr::copy(src, (*output).str, src_len as usize);
(*output).length = src_len;
(*output).dtor = Some(string_utf16_dtor as extern "C" fn(*mut c_ushort));
ptr::copy(src, (*output).str, src_len as usize);
(*output).length = src_len;
(*output).dtor = Some(string_utf16_dtor as extern "C" fn(*mut c_ushort));
}
} else {
(*output).str = mem::transmute(src);
(*output).length = src_len;
(*output).dtor = mem::transmute(0 as *const u8);
(*output).str = src as *mut _;
(*output).length = src_len;
(*output).dtor = None;
}
}
return 1;
@ -209,7 +209,7 @@ pub extern "C" fn cef_string_wide_clear(cs: *mut cef_string_wide_t) {
(*cs).dtor.map(|dtor| dtor((*cs).str));
(*cs).length = 0;
(*cs).str = 0 as *mut wchar_t;
(*cs).dtor = mem::transmute(0 as *const u8);
(*cs).dtor = None;
}
}
@ -227,20 +227,20 @@ pub extern "C" fn cef_string_wide_set(src: *const wchar_t, src_len: size_t, outp
unsafe {
if copy != 0 {
if !src.is_null() && src_len > 0 {
(*output).str = libc::calloc(1, (src_len + 1) * mem::size_of::<wchar_t>() as u64) as
*mut wchar_t;
if (*output).str.is_null() {
return 0;
}
(*output).str = libc::calloc(1, (src_len + 1) * mem::size_of::<wchar_t>() as u64) as
*mut wchar_t;
if (*output).str.is_null() {
return 0;
}
ptr::copy(src, (*output).str, src_len as usize);
(*output).length = src_len;
(*output).dtor = Some(string_wide_dtor as extern "C" fn(*mut wchar_t));
ptr::copy(src, (*output).str, src_len as usize);
(*output).length = src_len;
(*output).dtor = Some(string_wide_dtor as extern "C" fn(*mut wchar_t));
}
} else {
(*output).str = mem::transmute(src);
(*output).length = src_len;
(*output).dtor = mem::transmute(0 as *const u8);
(*output).str = src as *mut _;
(*output).length = src_len;
(*output).dtor = None;
}
}
return 1;
@ -311,8 +311,8 @@ pub fn empty_utf16_string() -> cef_string_utf16_t {
pub fn string_to_userfree_string(string: cef_string_utf16_t) -> cef_string_userfree_utf16_t {
unsafe {
let allocation: cef_string_userfree_utf16_t =
mem::transmute(libc::malloc(mem::size_of::<cef_string_utf16_t>() as size_t));
let allocation = libc::malloc(mem::size_of::<cef_string_utf16_t>() as size_t)
as cef_string_userfree_utf16_t;
ptr::write(allocation, string);
allocation
}

View file

@ -3,33 +3,25 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use libc::{c_int};
use std::mem;
use std::boxed;
use std::slice;
use string::cef_string_utf16_set;
use types::{cef_string_list_t,cef_string_t};
use rustc_unicode::str::Utf16Encoder;
fn string_list_to_vec(lt: *mut cef_string_list_t) -> *mut Vec<String> {
lt as *mut Vec<String>
}
//cef_string_list
#[no_mangle]
pub extern "C" fn cef_string_list_alloc() -> *mut cef_string_list_t {
unsafe {
let lt: Box<Vec<String>> = box vec!();
mem::transmute(lt)
}
boxed::into_raw(box vec!())
}
#[no_mangle]
pub extern "C" fn cef_string_list_size(lt: *mut cef_string_list_t) -> c_int {
unsafe {
if lt.is_null() { return 0; }
let v = string_list_to_vec(lt);
(*v).len() as c_int
(*lt).len() as c_int
}
}
@ -37,8 +29,7 @@ pub extern "C" fn cef_string_list_size(lt: *mut cef_string_list_t) -> c_int {
pub extern "C" fn cef_string_list_append(lt: *mut cef_string_list_t, value: *const cef_string_t) {
unsafe {
if lt.is_null() { return; }
let v = string_list_to_vec(lt);
(*v).push(String::from_utf16(slice::from_raw_parts((*value).str, (*value).length as usize)).unwrap());
(*lt).push(String::from_utf16(slice::from_raw_parts((*value).str, (*value).length as usize)).unwrap());
}
}
@ -46,11 +37,10 @@ pub extern "C" fn cef_string_list_append(lt: *mut cef_string_list_t, value: *con
pub extern "C" fn cef_string_list_value(lt: *mut cef_string_list_t, index: c_int, value: *mut cef_string_t) -> c_int {
unsafe {
if index < 0 || lt.is_null() { return 0; }
let v = string_list_to_vec(lt);
if index as usize > (*v).len() - 1 { return 0; }
let ref string = (*v)[index as usize];
if index as usize > (*lt).len() - 1 { return 0; }
let ref string = (*lt)[index as usize];
let utf16_chars: Vec<u16> = Utf16Encoder::new(string.chars()).collect();
cef_string_utf16_set(mem::transmute(utf16_chars.as_ptr()), utf16_chars.len() as u64, value, 1)
cef_string_utf16_set(utf16_chars.as_ptr(), utf16_chars.len() as u64, value, 1)
}
}
@ -58,8 +48,7 @@ pub extern "C" fn cef_string_list_value(lt: *mut cef_string_list_t, index: c_int
pub extern "C" fn cef_string_list_clear(lt: *mut cef_string_list_t) {
unsafe {
if lt.is_null() { return; }
let v = string_list_to_vec(lt);
(*v).clear();
(*lt).clear();
}
}
@ -67,9 +56,8 @@ pub extern "C" fn cef_string_list_clear(lt: *mut cef_string_list_t) {
pub extern "C" fn cef_string_list_free(lt: *mut cef_string_list_t) {
unsafe {
if lt.is_null() { return; }
let v: Box<Vec<String>> = mem::transmute(lt);
cef_string_list_clear(lt);
drop(v);
drop(Box::from_raw(lt));
}
}
@ -77,8 +65,7 @@ pub extern "C" fn cef_string_list_free(lt: *mut cef_string_list_t) {
pub extern "C" fn cef_string_list_copy(lt: *mut cef_string_list_t) -> *mut cef_string_list_t {
unsafe {
if lt.is_null() { return 0 as *mut cef_string_list_t; }
let v = string_list_to_vec(lt);
let copy = (*v).clone();
mem::transmute(box copy)
let copy = (*lt).clone();
boxed::into_raw(box copy)
}
}

View file

@ -4,33 +4,24 @@
use eutil::slice_to_str;
use libc::{c_int};
use std::boxed;
use std::collections::BTreeMap;
use std::mem;
use std::string::String;
use string::{cef_string_userfree_utf16_alloc, cef_string_userfree_utf16_free};
use string::{cef_string_utf16_set};
use types::{cef_string_map_t, cef_string_t};
fn string_map_to_treemap(sm: *mut cef_string_map_t) -> *mut BTreeMap<String, *mut cef_string_t> {
sm as *mut BTreeMap<String, *mut cef_string_t>
}
//cef_string_map
#[no_mangle]
pub extern "C" fn cef_string_map_alloc() -> *mut cef_string_map_t {
unsafe {
let sm: Box<BTreeMap<String, *mut cef_string_t>> = box BTreeMap::new();
mem::transmute(sm)
}
boxed::into_raw(box BTreeMap::new())
}
#[no_mangle]
pub extern "C" fn cef_string_map_size(sm: *mut cef_string_map_t) -> c_int {
unsafe {
if sm.is_null() { return 0; }
let v = string_map_to_treemap(sm);
(*v).len() as c_int
(*sm).len() as c_int
}
}
@ -38,11 +29,10 @@ pub extern "C" fn cef_string_map_size(sm: *mut cef_string_map_t) -> c_int {
pub extern "C" fn cef_string_map_append(sm: *mut cef_string_map_t, key: *const cef_string_t, value: *const cef_string_t) -> c_int {
unsafe {
if sm.is_null() { return 0; }
let v = string_map_to_treemap(sm);
slice_to_str((*key).str as *const u8, (*key).length as usize, |result| {
let csv = cef_string_userfree_utf16_alloc();
cef_string_utf16_set((*value).str as *const u16, (*value).length, csv, 1);
(*v).insert(result.to_owned(), csv);
(*sm).insert(result.to_owned(), csv);
1
})
}
@ -52,12 +42,10 @@ pub extern "C" fn cef_string_map_append(sm: *mut cef_string_map_t, key: *const c
pub extern "C" fn cef_string_map_find(sm: *mut cef_string_map_t, key: *const cef_string_t, value: *mut cef_string_t) -> c_int {
unsafe {
if sm.is_null() { return 0; }
let v = string_map_to_treemap(sm);
slice_to_str((*key).str as *const u8, (*key).length as usize, |result| {
match (*v).get(result) {
match (*sm).get(result) {
Some(s) => {
cef_string_utf16_set((**s).str as *const u16, (**s).length, value, 1);
1
cef_string_utf16_set((**s).str as *const u16, (**s).length, value, 1)
}
None => 0
}
@ -69,48 +57,44 @@ pub extern "C" fn cef_string_map_find(sm: *mut cef_string_map_t, key: *const cef
pub extern "C" fn cef_string_map_key(sm: *mut cef_string_map_t, index: c_int, value: *mut cef_string_t) -> c_int {
unsafe {
if index < 0 || sm.is_null() { return 0; }
let v = string_map_to_treemap(sm);
if index as usize > (*v).len() - 1 { return 0; }
if index as usize > (*sm).len() - 1 { return 0; }
for (i, k) in (*v).keys().enumerate() {
if i == index as usize {
match (*sm).keys().nth(index as usize) {
Some(k) => {
cef_string_utf16_set(k.as_bytes().as_ptr() as *const u16,
k.len() as u64,
value,
1);
return 1;
}
1)
},
None => 0,
}
}
0
}
#[no_mangle]
pub extern "C" fn cef_string_map_value(sm: *mut cef_string_map_t, index: c_int, value: *mut cef_string_t) -> c_int {
unsafe {
if index < 0 || sm.is_null() { return 0; }
let v = string_map_to_treemap(sm);
if index as usize > (*v).len() - 1 { return 0; }
if index as usize > (*sm).len() - 1 { return 0; }
for (i, val) in (*v).values().enumerate() {
if i == index as usize {
match (*sm).values().nth(index as usize) {
Some(val) => {
cef_string_utf16_set((**val).str as *const u16, (**val).length, value, 1);
return 1;
}
1
},
None => 0,
}
}
0
}
#[no_mangle]
pub extern "C" fn cef_string_map_clear(sm: *mut cef_string_map_t) {
unsafe {
if sm.is_null() { return; }
let v = string_map_to_treemap(sm);
for val in (*v).values() {
for val in (*sm).values() {
cef_string_userfree_utf16_free(*val);
}
(*v).clear();
(*sm).clear();
}
}
@ -118,7 +102,7 @@ pub extern "C" fn cef_string_map_clear(sm: *mut cef_string_map_t) {
pub extern "C" fn cef_string_map_free(sm: *mut cef_string_map_t) {
unsafe {
if sm.is_null() { return; }
let _v: Box<BTreeMap<String, *mut cef_string_t>> = mem::transmute(sm);
cef_string_map_clear(sm);
drop(Box::from_raw(sm));
}
}

View file

@ -15,7 +15,7 @@ pub use self::cef_rect as cef_rect_t;
use std::collections::BTreeMap;
pub enum cef_string_map_t {}
pub type cef_string_map_t = BTreeMap<String, *mut cef_string_t>;
pub type cef_string_multimap_t = BTreeMap<String, Vec<*mut cef_string_t>>;
pub type cef_string_list_t = Vec<String>;
pub enum cef_text_input_context_t {}

View file

@ -38,7 +38,7 @@ use std::boxed;
use std::collections::HashMap;
use std::mem;
use std::ptr;
use std::raw;
use std::slice;
pub trait CefWrap<CObject> {
fn to_c(rust_object: Self) -> CObject;
@ -209,10 +209,7 @@ impl<'a> CefWrap<*const cef_string_t> for &'a [u16] {
}
}
unsafe fn to_rust(cef_string: *const cef_string_t) -> &'a [u16] {
mem::transmute(raw::Slice {
data: (*cef_string).str,
len: (*cef_string).length as usize,
})
slice::from_raw_parts((*cef_string).str, (*cef_string).length as usize)
}
}