From 6dd1eca677a58787f87aae076185a1e04dab1caf Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Fri, 7 Nov 2014 15:39:15 -0500 Subject: [PATCH 01/12] embedding: begin cef string_map api cef_string_map_alloc() --- ports/cef/lib.rs | 1 + ports/cef/string_map.rs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 ports/cef/string_map.rs diff --git a/ports/cef/lib.rs b/ports/cef/lib.rs index a13c489b790..ad2452ce60b 100644 --- a/ports/cef/lib.rs +++ b/ports/cef/lib.rs @@ -51,6 +51,7 @@ pub mod mem; pub mod request; pub mod string; pub mod string_list; +pub mod string_map; pub mod task; pub mod types; pub mod urlrequest; diff --git a/ports/cef/string_map.rs b/ports/cef/string_map.rs new file mode 100644 index 00000000000..ed3ca226060 --- /dev/null +++ b/ports/cef/string_map.rs @@ -0,0 +1,23 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use eutil::fptr_is_null; +use libc::{c_int}; +use std::collections::TreeMap; +use std::mem; +use std::slice; +use std::str; +use std::string::String; +use string::{cef_string_userfree_utf8_alloc,cef_string_userfree_utf8_free,cef_string_utf8_set}; +use types::{cef_string_map_t,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> = box TreeMap::new(); + mem::transmute(sm) + } +} From e486fa52d5a84ae4e8e1dcdb45486b1c3e956968 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Fri, 7 Nov 2014 15:40:09 -0500 Subject: [PATCH 02/12] embedding: cef_string_map_size() --- ports/cef/string_map.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ports/cef/string_map.rs b/ports/cef/string_map.rs index ed3ca226060..fe5c7d24cec 100644 --- a/ports/cef/string_map.rs +++ b/ports/cef/string_map.rs @@ -12,6 +12,10 @@ use std::string::String; use string::{cef_string_userfree_utf8_alloc,cef_string_userfree_utf8_free,cef_string_utf8_set}; use types::{cef_string_map_t,cef_string_t}; +fn string_map_to_treemap(sm: *mut cef_string_map_t) -> *mut TreeMap { + sm as *mut TreeMap +} + //cef_string_map #[no_mangle] @@ -21,3 +25,12 @@ pub extern "C" fn cef_string_map_alloc() -> *mut cef_string_map_t { mem::transmute(sm) } } + +#[no_mangle] +pub extern "C" fn cef_string_map_size(sm: *mut cef_string_map_t) -> c_int { + unsafe { + if fptr_is_null(mem::transmute(sm)) { return 0; } + let v = string_map_to_treemap(sm); + (*v).len() as c_int + } +} From 7199c6ea58541d5252e9afedb4f8fc8d24223623 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Fri, 7 Nov 2014 15:40:35 -0500 Subject: [PATCH 03/12] embedding: cef_string_map_append() --- ports/cef/string_map.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/ports/cef/string_map.rs b/ports/cef/string_map.rs index fe5c7d24cec..df9fcae2d88 100644 --- a/ports/cef/string_map.rs +++ b/ports/cef/string_map.rs @@ -34,3 +34,23 @@ pub extern "C" fn cef_string_map_size(sm: *mut cef_string_map_t) -> c_int { (*v).len() as c_int } } + +#[no_mangle] +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 fptr_is_null(mem::transmute(sm)) { return 0; } + let v = string_map_to_treemap(sm); + slice::raw::buf_as_slice(mem::transmute((*key).str), (*key).length as uint, |result| { + match str::from_utf8(result) { + Some(k) => { + let s = String::from_str(k); + let csv = cef_string_userfree_utf8_alloc(); + cef_string_utf8_set(mem::transmute((*value).str), (*value).length, csv, 1); + (*v).insert(s, csv); + 1 + }, + None => 0 + } + }) + } +} From 1a007e19e7682dd16fb213a70fe79fe1b57c220a Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Fri, 7 Nov 2014 15:40:55 -0500 Subject: [PATCH 04/12] embedding: cef_string_map_find() --- ports/cef/string_map.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/ports/cef/string_map.rs b/ports/cef/string_map.rs index df9fcae2d88..589d0c5189e 100644 --- a/ports/cef/string_map.rs +++ b/ports/cef/string_map.rs @@ -54,3 +54,25 @@ pub extern "C" fn cef_string_map_append(sm: *mut cef_string_map_t, key: *const c }) } } + +#[no_mangle] +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 fptr_is_null(mem::transmute(sm)) { return 0; } + let v = string_map_to_treemap(sm); + slice::raw::buf_as_slice(mem::transmute((*key).str), (*key).length as uint, |result| { + match str::from_utf8(result) { + Some(k) => { + match (*v).find(&String::from_str(k)) { + Some(s) => { + cef_string_utf8_set(mem::transmute((**s).str), (**s).length, value, 1); + 1 + } + None => 0 + } + }, + None => 0 + } + }) + } +} From 8fdb2286ae1e17b26d3d30aabf7e2977ab2906ba Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Fri, 7 Nov 2014 15:41:14 -0500 Subject: [PATCH 05/12] embedding: cef_string_map_key() --- ports/cef/string_map.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ports/cef/string_map.rs b/ports/cef/string_map.rs index 589d0c5189e..6e3a229eb4d 100644 --- a/ports/cef/string_map.rs +++ b/ports/cef/string_map.rs @@ -76,3 +76,20 @@ pub extern "C" fn cef_string_map_find(sm: *mut cef_string_map_t, key: *const cef }) } } + +#[no_mangle] +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 || fptr_is_null(mem::transmute(sm)) { return 0; } + let v = string_map_to_treemap(sm); + if index as uint > (*v).len() - 1 { return 0; } + + for (i, k) in (*v).keys().enumerate() { + if i == index as uint { + cef_string_utf8_set(k.as_bytes().as_ptr(), k.len() as u64, value, 1); + return 1; + } + } + } + 0 +} From 903c6f40f6233acbacf4f9f2edfe51f5eacc74d2 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Fri, 7 Nov 2014 15:41:33 -0500 Subject: [PATCH 06/12] embedding: cef_string_map_value() --- ports/cef/string_map.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ports/cef/string_map.rs b/ports/cef/string_map.rs index 6e3a229eb4d..ddc9f52117f 100644 --- a/ports/cef/string_map.rs +++ b/ports/cef/string_map.rs @@ -93,3 +93,20 @@ pub extern "C" fn cef_string_map_key(sm: *mut cef_string_map_t, index: c_int, va } 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 || fptr_is_null(mem::transmute(sm)) { return 0; } + let v = string_map_to_treemap(sm); + if index as uint > (*v).len() - 1 { return 0; } + + for (i, val) in (*v).values().enumerate() { + if i == index as uint { + cef_string_utf8_set(mem::transmute((**val).str), (**val).length, value, 1); + return 1; + } + } + } + 0 +} From 37edcdcddebfd38cdfdb7cc5aca3a8599dc99cdd Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Fri, 7 Nov 2014 15:41:48 -0500 Subject: [PATCH 07/12] embedding: cef_string_map_clear() --- ports/cef/string_map.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ports/cef/string_map.rs b/ports/cef/string_map.rs index ddc9f52117f..feb397d89de 100644 --- a/ports/cef/string_map.rs +++ b/ports/cef/string_map.rs @@ -110,3 +110,16 @@ pub extern "C" fn cef_string_map_value(sm: *mut cef_string_map_t, index: c_int, } 0 } + +#[no_mangle] +pub extern "C" fn cef_string_map_clear(sm: *mut cef_string_map_t) { + unsafe { + if fptr_is_null(mem::transmute(sm)) { return; } + let v = string_map_to_treemap(sm); + if (*v).len() == 0 { return; } + for val in (*v).values() { + cef_string_userfree_utf8_free((*val)); + } + (*v).clear(); + } +} From 0a82eebaa2cab3980b7fb76a457c3443a0ebf734 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Fri, 7 Nov 2014 15:41:58 -0500 Subject: [PATCH 08/12] embedding: cef_string_map_free() --- ports/cef/string_map.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ports/cef/string_map.rs b/ports/cef/string_map.rs index feb397d89de..4adf4b0762c 100644 --- a/ports/cef/string_map.rs +++ b/ports/cef/string_map.rs @@ -123,3 +123,13 @@ pub extern "C" fn cef_string_map_clear(sm: *mut cef_string_map_t) { (*v).clear(); } } + +#[no_mangle] +pub extern "C" fn cef_string_map_free(sm: *mut cef_string_map_t) { + unsafe { + if fptr_is_null(mem::transmute(sm)) { return; } + let v: Box> = mem::transmute(sm); + cef_string_map_clear(sm); + drop(v); + } +} From c813df777e343c752c1a9d669d90f9aa301a69b8 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Thu, 13 Nov 2014 14:37:56 -0500 Subject: [PATCH 09/12] embedding: address various review nitpicks for string_map --- ports/cef/string_map.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ports/cef/string_map.rs b/ports/cef/string_map.rs index 4adf4b0762c..91fb0459ded 100644 --- a/ports/cef/string_map.rs +++ b/ports/cef/string_map.rs @@ -10,7 +10,7 @@ use std::slice; use std::str; use std::string::String; use string::{cef_string_userfree_utf8_alloc,cef_string_userfree_utf8_free,cef_string_utf8_set}; -use types::{cef_string_map_t,cef_string_t}; +use types::{cef_string_map_t, cef_string_t}; fn string_map_to_treemap(sm: *mut cef_string_map_t) -> *mut TreeMap { sm as *mut TreeMap @@ -103,7 +103,7 @@ pub extern "C" fn cef_string_map_value(sm: *mut cef_string_map_t, index: c_int, for (i, val) in (*v).values().enumerate() { if i == index as uint { - cef_string_utf8_set(mem::transmute((**val).str), (**val).length, value, 1); + cef_string_utf8_set((**val).str as *const u8, (**val).length, value, 1); return 1; } } @@ -116,9 +116,8 @@ pub extern "C" fn cef_string_map_clear(sm: *mut cef_string_map_t) { unsafe { if fptr_is_null(mem::transmute(sm)) { return; } let v = string_map_to_treemap(sm); - if (*v).len() == 0 { return; } for val in (*v).values() { - cef_string_userfree_utf8_free((*val)); + cef_string_userfree_utf8_free(*val); } (*v).clear(); } @@ -128,8 +127,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 fptr_is_null(mem::transmute(sm)) { return; } - let v: Box> = mem::transmute(sm); + let _v: Box> = mem::transmute(sm); cef_string_map_clear(sm); - drop(v); } } From 7ba1150943b72f1da0cce2aa20fdd7662fa89449 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Thu, 13 Nov 2014 14:38:54 -0500 Subject: [PATCH 10/12] embedding: add eutil::slice_to_str() convenience function converts a *u8+length to &str and returns c_int --- ports/cef/eutil.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ports/cef/eutil.rs b/ports/cef/eutil.rs index b71421f8c40..30718f60e99 100644 --- a/ports/cef/eutil.rs +++ b/ports/cef/eutil.rs @@ -2,6 +2,23 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +use libc::c_int; +use std::slice; +use std::str; + pub fn fptr_is_null(fptr: *const u8) -> bool { fptr.is_null() } + +pub fn slice_to_str(s: *const u8, l: uint, f: |&str| -> c_int) -> c_int { + unsafe { + slice::raw::buf_as_slice(s, l, |result| { + match str::from_utf8(result) { + Some(ruststr) => { + f(ruststr) + }, + None => 0 + } + }) + } +} From 19c80b17410b7727c86455534405c6e2546bcb2b Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Thu, 13 Nov 2014 14:41:13 -0500 Subject: [PATCH 11/12] embedding: replace all occurrences of slicing to &str with slice_to_str() --- ports/cef/string.rs | 20 ++++++-------------- ports/cef/string_map.rs | 38 +++++++++++++------------------------- 2 files changed, 19 insertions(+), 39 deletions(-) diff --git a/ports/cef/string.rs b/ports/cef/string.rs index d8cf7d97531..81fd7087d9c 100644 --- a/ports/cef/string.rs +++ b/ports/cef/string.rs @@ -3,7 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use eutil::fptr_is_null; +use eutil::{fptr_is_null, slice_to_str}; use libc::{size_t, c_int, c_ushort,c_void}; use libc::types::os::arch::c95::wchar_t; use mem::{new0,newarray0,delete,deletearray}; @@ -11,7 +11,6 @@ use std::mem; use std::ptr; use std::slice; use std::string; -use std::str; use types::{cef_string_utf16_t, cef_string_utf8_t, cef_string_wide_t}; use types::{cef_string_userfree_utf16_t, cef_string_userfree_utf8_t, cef_string_userfree_wide_t}; @@ -95,18 +94,11 @@ pub extern "C" fn cef_string_utf8_set(src: *const u8, src_len: size_t, output: * #[no_mangle] pub extern "C" fn cef_string_utf8_to_utf16(src: *const u8, src_len: size_t, output: *mut cef_string_utf16_t) -> c_int { - unsafe { - slice::raw::buf_as_slice(src, src_len as uint, |result| { - match str::from_utf8(result) { - Some(enc) => { - let conv = enc.utf16_units().collect::>(); - cef_string_utf16_set(conv.as_ptr(), conv.len() as size_t, output, 1); - 1 - }, - None => 0 - } - }) - } + slice_to_str(src, src_len as uint, |result| { + let conv = result.utf16_units().collect::>(); + cef_string_utf16_set(conv.as_ptr(), conv.len() as size_t, output, 1); + 1 + }) } #[no_mangle] diff --git a/ports/cef/string_map.rs b/ports/cef/string_map.rs index 91fb0459ded..67469700957 100644 --- a/ports/cef/string_map.rs +++ b/ports/cef/string_map.rs @@ -2,12 +2,10 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use eutil::fptr_is_null; +use eutil::{fptr_is_null, slice_to_str}; use libc::{c_int}; use std::collections::TreeMap; use std::mem; -use std::slice; -use std::str; use std::string::String; use string::{cef_string_userfree_utf8_alloc,cef_string_userfree_utf8_free,cef_string_utf8_set}; use types::{cef_string_map_t, cef_string_t}; @@ -40,17 +38,12 @@ pub extern "C" fn cef_string_map_append(sm: *mut cef_string_map_t, key: *const c unsafe { if fptr_is_null(mem::transmute(sm)) { return 0; } let v = string_map_to_treemap(sm); - slice::raw::buf_as_slice(mem::transmute((*key).str), (*key).length as uint, |result| { - match str::from_utf8(result) { - Some(k) => { - let s = String::from_str(k); - let csv = cef_string_userfree_utf8_alloc(); - cef_string_utf8_set(mem::transmute((*value).str), (*value).length, csv, 1); - (*v).insert(s, csv); - 1 - }, - None => 0 - } + slice_to_str((*key).str as *const u8, (*key).length as uint, |result| { + let s = String::from_str(result); + let csv = cef_string_userfree_utf8_alloc(); + cef_string_utf8_set((*value).str as *const u8, (*value).length, csv, 1); + (*v).insert(s, csv); + 1 }) } } @@ -60,17 +53,12 @@ pub extern "C" fn cef_string_map_find(sm: *mut cef_string_map_t, key: *const cef unsafe { if fptr_is_null(mem::transmute(sm)) { return 0; } let v = string_map_to_treemap(sm); - slice::raw::buf_as_slice(mem::transmute((*key).str), (*key).length as uint, |result| { - match str::from_utf8(result) { - Some(k) => { - match (*v).find(&String::from_str(k)) { - Some(s) => { - cef_string_utf8_set(mem::transmute((**s).str), (**s).length, value, 1); - 1 - } - None => 0 - } - }, + slice_to_str((*key).str as *const u8, (*key).length as uint, |result| { + match (*v).find(&String::from_str(result)) { + Some(s) => { + cef_string_utf8_set((**s).str as *const u8, (**s).length, value, 1); + 1 + } None => 0 } }) From 2d4c6a1d1ca582973f839b1036f50d372492fb1b Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Thu, 13 Nov 2014 15:07:41 -0500 Subject: [PATCH 12/12] embedding: address most recent critic review --- ports/cef/eutil.rs | 7 +------ ports/cef/string_map.rs | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/ports/cef/eutil.rs b/ports/cef/eutil.rs index 30718f60e99..d5f9760f6bb 100644 --- a/ports/cef/eutil.rs +++ b/ports/cef/eutil.rs @@ -13,12 +13,7 @@ pub fn fptr_is_null(fptr: *const u8) -> bool { pub fn slice_to_str(s: *const u8, l: uint, f: |&str| -> c_int) -> c_int { unsafe { slice::raw::buf_as_slice(s, l, |result| { - match str::from_utf8(result) { - Some(ruststr) => { - f(ruststr) - }, - None => 0 - } + str::from_utf8(result).map(|s| f(s)).unwrap_or(0) }) } } diff --git a/ports/cef/string_map.rs b/ports/cef/string_map.rs index 67469700957..67753b3c5dc 100644 --- a/ports/cef/string_map.rs +++ b/ports/cef/string_map.rs @@ -7,7 +7,7 @@ use libc::{c_int}; use std::collections::TreeMap; use std::mem; use std::string::String; -use string::{cef_string_userfree_utf8_alloc,cef_string_userfree_utf8_free,cef_string_utf8_set}; +use string::{cef_string_userfree_utf8_alloc, cef_string_userfree_utf8_free, cef_string_utf8_set}; use types::{cef_string_map_t, cef_string_t}; fn string_map_to_treemap(sm: *mut cef_string_map_t) -> *mut TreeMap {