Skip to content

Commit

Permalink
serious mode engaged: start of embedding crate using FFI
Browse files Browse the repository at this point in the history
current status

=============

[ ] Successfully crashing CEF

[X] Successfully not crashing CEF
  • Loading branch information
Mike Blumenkrantz committed May 26, 2014
1 parent 4d188e2 commit faa7f18
Show file tree
Hide file tree
Showing 12 changed files with 504 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Makefile.in
Expand Up @@ -346,6 +346,10 @@ servo: $(DEPS_servo)
$(Q)$(RUSTC) $(RFLAGS_servo) $< --crate-type dylib,rlib

RFLAGS_embedding = $(strip $(CFG_RUSTC_FLAGS)) $(addprefix -L $(B)src/,$(DEPS_SUBMODULES)) -L $(B)src/components/gfx -L $(B)src/components/util -L $(B)src/components/net -L $(B)src/components/script -L $(B)src/components/style -L $(B)src/components/msg -L $(B).. -L $(B)src/components/main -L $(B)src/components/macros -A non_camel_case_types -A unused_variable

ifeq ($(CFG_OSTYPE),apple-darwin)
RFLAGS_embedding += -C link-args="-Wl,-U,_tc_new -Wl,-U,_tc_newarray -Wl,-U,_tc_delete -Wl,-U,_tc_deletearray"
endif
SRC_embedding = $(call rwildcard,$(S)src/components/embedding/,*.rs)
CRATE_embedding = $(S)src/components/embedding/embedding.rs

Expand Down
29 changes: 29 additions & 0 deletions src/components/embedding/browser.rs
@@ -0,0 +1,29 @@
/* 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 libc::{calloc, size_t,c_int};
use std::mem;
use types::{cef_browser_settings_t, cef_browser_t, cef_client_t, cef_request_context_t, cef_string_t, cef_window_info_t};

#[no_mangle]
pub extern "C" fn cef_browser_host_create_browser(windowInfo: *cef_window_info_t,
client: *mut cef_client_t,
url: *cef_string_t,
settings: *cef_browser_settings_t,
request_context: *mut cef_request_context_t) -> c_int {
0
}

#[no_mangle]
pub extern "C" fn cef_browser_host_create_browser_sync(windowInfo: *cef_window_info_t,
client: *mut cef_client_t,
url: *cef_string_t,
settings: *cef_browser_settings_t,
request_context: *mut cef_request_context_t) -> *mut cef_browser_t {
unsafe {
let browser = calloc(1, mem::size_of::<cef_browser_t>() as size_t) as *mut cef_browser_t;
browser
}
}
93 changes: 93 additions & 0 deletions src/components/embedding/command_line.rs
@@ -0,0 +1,93 @@
/* 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 libc::{calloc, c_int, size_t};
use std::mem;
use std::str;
use std::c_vec::CVec;
use std::cast::transmute;
use string::{cef_string_userfree_utf16_alloc, cef_string_utf16_set};
use types::{cef_command_line_t, cef_string_t, cef_string_userfree_t, cef_string_utf16_t};

type command_line_t = command_line;
struct command_line {
pub cl: cef_command_line_t,
pub argc: c_int,
pub argv: Vec<~str>,
}

static mut GLOBAL_CMDLINE: Option<*mut command_line_t> = None;

fn command_line_new() -> *mut command_line_t {
unsafe {
let cl = calloc(1, mem::size_of::<command_line>() as size_t) as *mut command_line_t;
(*cl).cl.base.size = mem::size_of::<cef_command_line_t>() as u64;
cl
}
}

pub fn command_line_init(argc: c_int, argv: **u8) {
unsafe {
let mut a: Vec<~str> = vec!();
for i in range(0u, argc as uint) {
a.push(str::raw::from_c_str(*argv.offset(i as int) as *i8));
}
let cl = command_line_new();
(*cl).argc = argc;
(*cl).argv = a;
(*cl).cl.get_switch_value = command_line_get_switch_value;
GLOBAL_CMDLINE = Some(cl);
}
}

#[no_mangle]
pub extern "C" fn command_line_get_switch_value(cmd: *mut cef_command_line_t, name: *cef_string_t) -> *mut cef_string_userfree_t {
if cmd.is_null() || name.is_null() {
return 0 as *mut cef_string_userfree_t;
}
unsafe {
//technically cef_string_t can be any type of character size
//but the default cef callback uses utf16, so I'm jumping on board the SS Copy
let cl: *mut command_line_t = transmute(cmd);
let cs: *cef_string_utf16_t = transmute(name);
let opt = str::from_utf16(CVec::new((*cs).str, (*cs).length as uint).as_slice()).unwrap();
//debug!("opt: {}", opt);
for s in (*cl).argv.iter() {
let o = s.trim_left_chars('-');
//debug!("arg: {}", o);
if o.starts_with(opt) {
let string = cef_string_userfree_utf16_alloc() as *mut cef_string_utf16_t;
let arg = o.slice_from(opt.len() + 1).as_bytes();
arg.with_c_str(|c_str| {
cef_string_utf16_set(transmute(c_str), arg.len() as u64, string, 1);
});
return string as *mut cef_string_userfree_t
}
}
}
return 0 as *mut cef_string_userfree_t;
}

#[no_mangle]
pub extern "C" fn cef_command_line_create() -> *mut cef_command_line_t {
unsafe {
let cl = command_line_new();
(*cl).cl.get_switch_value = command_line_get_switch_value;
transmute(cl)
}
}

#[no_mangle]
pub extern "C" fn cef_command_line_get_global() -> *mut cef_command_line_t {
unsafe {
match GLOBAL_CMDLINE {
Some(scl) => {
transmute(scl)
},
None => {
0 as *mut cef_command_line_t
}
}
}
}
74 changes: 74 additions & 0 deletions src/components/embedding/core.rs
@@ -0,0 +1,74 @@
/* 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 azure;
use command_line::command_line_init;
use eutil::fptr_is_null;
use libc::{c_int, c_void};
use native;
use servo;
use servo_util::opts;
use std::cast::transmute;
use types::{cef_app_t, cef_main_args_t, cef_settings_t};


#[no_mangle]
pub extern "C" fn cef_initialize(args: *cef_main_args_t, settings: *mut cef_settings_t,
application: *mut cef_app_t, windows_sandbox_info: *c_void) -> c_int {
if args.is_null() {
return 0;
}
unsafe {
command_line_init((*args).argc, (*args).argv);
let cb = (*application).get_browser_process_handler;
if !fptr_is_null(transmute(cb)) {
let handler = cb(application);
if handler.is_not_null() {
let hcb = (*handler).on_context_initialized;
if !fptr_is_null(transmute(hcb)) {
hcb(handler);
}
}
}
}
return 1
}

#[no_mangle]
pub extern "C" fn cef_shutdown() {
}

#[no_mangle]
pub extern "C" fn cef_run_message_loop() {
let mut urls = Vec::new();
urls.push("http://www.w3c-test.org".to_owned());
let opts = opts::Opts {
urls: urls,
render_backend: azure::azure_hl::SkiaBackend,
n_render_threads: 1,
cpu_painting: false,
tile_size: 512,
profiler_period: None,
layout_threads: 1,
//layout_threads: cmp::max(rt::default_sched_threads() * 3 / 4, 1),
exit_after_load: false,
output_file: None,
headless: false,
hard_fail: false,
bubble_widths_separately: false,
};
native::start(0, 0 as **u8, proc() {
servo::run(opts);
});
}

#[no_mangle]
pub extern "C" fn cef_quit_message_loop() {
}

#[no_mangle]
pub extern "C" fn cef_execute_process(args: *cef_main_args_t, app: *mut cef_app_t, windows_sandbox_info: *mut c_void) -> c_int {
-1
}
12 changes: 11 additions & 1 deletion src/components/embedding/embedding.rs
Expand Up @@ -16,7 +16,6 @@ extern crate rustuv;
extern crate servo_macros = "macros";
extern crate servo;

extern crate alert;
extern crate azure;
extern crate geom;
extern crate gfx;
Expand Down Expand Up @@ -47,4 +46,15 @@ extern crate core_graphics;
#[cfg(target_os="macos")]
extern crate core_text;

pub mod browser;
pub mod command_line;
pub mod core;
pub mod eutil;
#[cfg(target_os="linux")] #[cfg(target_os="macos")]
pub mod mem;
pub mod request;
pub mod string;
pub mod task;
pub mod types;
pub mod urlrequest;

7 changes: 7 additions & 0 deletions src/components/embedding/eutil.rs
@@ -0,0 +1,7 @@
/* 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/. */

pub fn fptr_is_null(fptr: *u8) -> bool {
fptr.is_null()
}
54 changes: 54 additions & 0 deletions src/components/embedding/mem.rs
@@ -0,0 +1,54 @@
/* 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 libc::{c_void, size_t};
use std::mem;
use std::ptr::set_memory;

extern "C" {
fn tc_new(size: size_t) -> *mut c_void;
fn tc_delete(mem: *mut c_void);
fn tc_newarray(size: size_t) -> *mut c_void;
fn tc_deletearray(mem: *mut c_void);
}

pub fn newarray0<T>(nmem: size_t) -> *mut T {
let mem = newarray::<T>(nmem) as *mut T;
unsafe {
set_memory(mem, 0 as u8, nmem as uint);
}
mem
}

pub fn newarray<T>(nmem: size_t) -> *mut T {
unsafe {
tc_newarray(nmem * mem::size_of::<T>() as u64) as *mut T
}
}

pub fn new0<T>(nmem: size_t) -> *mut T {
let mem = new(nmem * mem::size_of::<T>() as u64) as *mut T;
unsafe {
set_memory(mem, 0 as u8, nmem as uint);
}
mem
}

pub fn new(size: size_t) -> *mut c_void {
unsafe {
tc_new(size)
}
}

pub fn delete(mem: *mut c_void) {
unsafe {
tc_delete(mem)
}
}

pub fn deletearray(mem: *mut c_void) {
unsafe {
tc_deletearray(mem)
}
}
21 changes: 21 additions & 0 deletions src/components/embedding/request.rs
@@ -0,0 +1,21 @@
/* 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 types::{cef_post_data_element_t, cef_post_data_t, cef_request_t};

#[no_mangle]
pub extern "C" fn cef_request_create() -> *mut cef_request_t {
0 as *mut cef_request_t
}

#[no_mangle]
pub extern "C" fn cef_post_data_create() -> *mut cef_post_data_t {
0 as *mut cef_post_data_t
}

#[no_mangle]
pub extern "C" fn cef_post_data_element_create() -> *mut cef_post_data_element_t {
0 as *mut cef_post_data_element_t
}

0 comments on commit faa7f18

Please sign in to comment.