1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
|
/* 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/. */
//! Off-screen windows.
//!
//! This is used for off-screen rendering mode only; on-screen windows (the default embedding mode)
//! are managed by a platform toolkit (Glutin).
#[cfg(target_os="linux")]
use core::CEF_APP;
use eutil::Downcast;
#[cfg(target_os="linux")]
use interfaces::CefApp;
use interfaces::CefBrowser;
use render_handler::CefRenderHandlerExtensions;
use rustc_unicode::str::Utf16Encoder;
use types::{cef_cursor_handle_t, cef_cursor_type_t, cef_rect_t};
use wrappers::CefWrap;
use compositing::compositor_thread::{self, CompositorProxy, CompositorReceiver};
use compositing::windowing::{WindowEvent, WindowMethods};
use euclid::point::Point2D;
use euclid::scale_factor::ScaleFactor;
use euclid::size::{Size2D, TypedSize2D};
use gfx_traits::DevicePixel;
use gleam::gl;
use msg::constellation_msg::{Key, KeyModifiers};
use net_traits::net_error_list::NetError;
use std::cell::RefCell;
use std::ffi::CString;
use std::os::raw::{c_char, c_void};
use std::ptr;
use std::rc::Rc;
use std::sync::mpsc::{Sender, channel};
use std_url::Url;
use style_traits::cursor::Cursor;
use util::geometry::ScreenPx;
#[cfg(target_os="linux")]
extern crate x11;
#[cfg(target_os="linux")]
use self::x11::xlib::{XInitThreads,XOpenDisplay};
#[cfg(target_os="linux")]
pub static mut DISPLAY: *mut c_void = 0 as *mut c_void;
/// The type of an off-screen window.
#[derive(Clone)]
pub struct Window {
cef_browser: RefCell<Option<CefBrowser>>,
size: TypedSize2D<u32, DevicePixel>
}
#[cfg(target_os="macos")]
fn load_gl() {
const RTLD_DEFAULT: *mut c_void = (-2isize) as usize as *mut c_void;
extern {
fn dlsym(handle: *mut c_void, symbol: *const c_char) -> *mut c_void;
}
gl::load_with(|s| {
unsafe {
let c_str = CString::new(s).unwrap();
dlsym(RTLD_DEFAULT, c_str.as_ptr()) as *const c_void
}
});
}
#[cfg(target_os="linux")]
fn load_gl() {
extern {
fn glXGetProcAddress(symbol: *const c_char) -> *mut c_void;
}
gl::load_with(|s| {
unsafe {
let c_str = CString::new(s).unwrap();
glXGetProcAddress(c_str.as_ptr()) as *const c_void
}
});
}
impl Window {
/// Creates a new window.
pub fn new(width: u32, height: u32) -> Rc<Window> {
load_gl();
Rc::new(Window {
cef_browser: RefCell::new(None),
size: TypedSize2D::new(width, height)
})
}
/// Sets the current browser.
pub fn set_browser(&self, browser: CefBrowser) {
*self.cef_browser.borrow_mut() = Some(browser)
}
/// Currently unimplemented.
pub fn wait_events(&self) -> Vec<WindowEvent> {
vec![WindowEvent::Idle]
}
fn cursor_type_for_cursor(&self, cursor: Cursor) -> cef_cursor_type_t {
match cursor {
Cursor::None => return cef_cursor_type_t::CT_NONE,
Cursor::ContextMenu => return cef_cursor_type_t::CT_CONTEXTMENU,
Cursor::Grabbing => return cef_cursor_type_t::CT_GRABBING,
Cursor::Crosshair => return cef_cursor_type_t::CT_CROSS,
Cursor::Copy => return cef_cursor_type_t::CT_COPY,
Cursor::Alias => return cef_cursor_type_t::CT_ALIAS,
Cursor::Text => return cef_cursor_type_t::CT_IBEAM,
Cursor::Grab | Cursor::AllScroll =>
return cef_cursor_type_t::CT_GRAB,
Cursor::NoDrop => return cef_cursor_type_t::CT_NODROP,
Cursor::NotAllowed => return cef_cursor_type_t::CT_NOTALLOWED,
Cursor::Pointer => return cef_cursor_type_t::CT_POINTER,
Cursor::SResize => return cef_cursor_type_t::CT_SOUTHRESIZE,
Cursor::WResize => return cef_cursor_type_t::CT_WESTRESIZE,
Cursor::EwResize => return cef_cursor_type_t::CT_EASTWESTRESIZE,
Cursor::ColResize => return cef_cursor_type_t::CT_COLUMNRESIZE,
Cursor::EResize => return cef_cursor_type_t::CT_EASTRESIZE,
Cursor::NResize => return cef_cursor_type_t::CT_NORTHRESIZE,
Cursor::NsResize => return cef_cursor_type_t::CT_NORTHSOUTHRESIZE,
Cursor::RowResize => return cef_cursor_type_t::CT_ROWRESIZE,
Cursor::VerticalText => return cef_cursor_type_t::CT_VERTICALTEXT,
_ => return cef_cursor_type_t::CT_POINTER,
}
}
/// Returns the Cocoa cursor for a CSS cursor. These match Firefox, except where Firefox
/// bundles custom resources (which we don't yet do).
#[cfg(target_os="macos")]
fn cursor_handle_for_cursor(&self, cursor: Cursor) -> cef_cursor_handle_t {
use cocoa::base::class;
unsafe {
match cursor {
Cursor::None => return 0 as cef_cursor_handle_t,
Cursor::ContextMenu => msg_send![class("NSCursor"), contextualMenuCursor],
Cursor::Grabbing => msg_send![class("NSCursor"), closedHandCursor],
Cursor::Crosshair => msg_send![class("NSCursor"), crosshairCursor],
Cursor::Copy => msg_send![class("NSCursor"), dragCopyCursor],
Cursor::Alias => msg_send![class("NSCursor"), dragLinkCursor],
Cursor::Text => msg_send![class("NSCursor"), IBeamCursor],
Cursor::Grab | Cursor::AllScroll =>
msg_send![class("NSCursor"), openHandCursor],
Cursor::NoDrop | Cursor::NotAllowed =>
msg_send![class("NSCursor"), operationNotAllowedCursor],
Cursor::Pointer => msg_send![class("NSCursor"), pointingHandCursor],
Cursor::SResize => msg_send![class("NSCursor"), resizeDownCursor],
Cursor::WResize => msg_send![class("NSCursor"), resizeLeftCursor],
Cursor::EwResize | Cursor::ColResize =>
msg_send![class("NSCursor"), resizeLeftRightCursor],
Cursor::EResize => msg_send![class("NSCursor"), resizeRightCursor],
Cursor::NResize => msg_send![class("NSCursor"), resizeUpCursor],
Cursor::NsResize | Cursor::RowResize =>
msg_send![class("NSCursor"), resizeUpDownCursor],
Cursor::VerticalText => msg_send![class("NSCursor"), IBeamCursorForVerticalLayout],
_ => msg_send![class("NSCursor"), arrowCursor],
}
}
}
#[cfg(not(target_os="macos"))]
fn cursor_handle_for_cursor(&self, _: Cursor) -> cef_cursor_handle_t {
0
}
}
impl WindowMethods for Window {
fn framebuffer_size(&self) -> TypedSize2D<u32, DevicePixel> {
let browser = self.cef_browser.borrow();
match *browser {
None => self.size,
Some(ref browser) => {
if browser.downcast().callback_executed.get() != true {
self.size
} else {
let mut rect = cef_rect_t::zero();
rect.width = self.size.width as i32;
rect.height = self.size.height as i32;
if cfg!(target_os="macos") {
// osx relies on virtual pixel scaling to provide sizes different from actual
// pixel size on screen. other platforms are just 1.0 unless the desktop/toolkit says otherwise
if check_ptr_exist!(browser.get_host().get_client(), get_render_handler) &&
check_ptr_exist!(browser.get_host().get_client().get_render_handler(), get_backing_rect) {
browser.get_host()
.get_client()
.get_render_handler()
.get_backing_rect((*browser).clone(), &mut rect);
}
} else {
if check_ptr_exist!(browser.get_host().get_client(), get_render_handler) &&
check_ptr_exist!(browser.get_host().get_client().get_render_handler(), get_view_rect) {
browser.get_host()
.get_client()
.get_render_handler()
.get_view_rect((*browser).clone(), &mut rect);
}
}
TypedSize2D::new(rect.width as u32, rect.height as u32)
}
}
}
}
fn size(&self) -> TypedSize2D<f32, ScreenPx> {
let browser = self.cef_browser.borrow();
match *browser {
None => TypedSize2D::new(400.0, 300.0),
Some(ref browser) => {
let mut rect = cef_rect_t::zero();
browser.get_host()
.get_client()
.get_render_handler()
.get_view_rect((*browser).clone(), &mut rect);
TypedSize2D::new(rect.width as f32, rect.height as f32)
}
}
}
fn client_window(&self) -> (Size2D<u32>, Point2D<i32>) {
let size = self.size().to_untyped();
let width = size.width as u32;
let height = size.height as u32;
//TODO get real window position
(Size2D::new(width, height), Point2D::zero())
}
fn set_inner_size(&self, _size: Size2D<u32>) {
}
fn set_position(&self, _point: Point2D<i32>) {
}
fn present(&self) {
let browser = self.cef_browser.borrow();
match *browser {
None => {}
Some(ref browser) => {
if check_ptr_exist!(browser.get_host().get_client(), get_render_handler) &&
check_ptr_exist!(browser.get_host().get_client().get_render_handler(), on_present) {
browser.get_host().get_client().get_render_handler().on_present(browser.clone());
}
}
}
}
fn scale_factor(&self) -> ScaleFactor<f32, ScreenPx, DevicePixel> {
if cfg!(target_os="macos") {
let browser = self.cef_browser.borrow();
match *browser {
None => ScaleFactor::new(1.0),
Some(ref browser) => {
let mut view_rect = cef_rect_t::zero();
if check_ptr_exist!(browser.get_host().get_client(), get_render_handler) &&
check_ptr_exist!(browser.get_host().get_client().get_render_handler(), get_view_rect) {
browser.get_host()
.get_client()
.get_render_handler()
.get_view_rect((*browser).clone(), &mut view_rect);
}
let mut backing_rect = cef_rect_t::zero();
if check_ptr_exist!(browser.get_host().get_client(), get_render_handler) &&
check_ptr_exist!(browser.get_host().get_client().get_render_handler(), get_backing_rect) {
browser.get_host()
.get_client()
.get_render_handler()
.get_backing_rect((*browser).clone(), &mut backing_rect);
}
ScaleFactor::new(backing_rect.width as f32 / view_rect.width as f32)
}
}
} else {
// FIXME(zmike)
// need to figure out a method for actually getting the scale factor instead of this nonsense
ScaleFactor::new(1.0 as f32)
}
}
fn create_compositor_channel(&self)
-> (Box<CompositorProxy+Send>, Box<CompositorReceiver>) {
let (sender, receiver) = channel();
(box CefCompositorProxy {
sender: sender,
} as Box<CompositorProxy+Send>,
box receiver as Box<CompositorReceiver>)
}
fn prepare_for_composite(&self, width: usize, height: usize) -> bool {
let browser = self.cef_browser.borrow();
match *browser {
None => {
panic!("No browser?!?");
}
Some(ref browser) => {
if browser.downcast().host.downcast().composite_ok.get() == true {
true
} else {
if check_ptr_exist!(browser.get_host().get_client(), get_render_handler) &&
check_ptr_exist!(browser.get_host().get_client().get_render_handler(), on_paint) {
browser.get_host().get_client().get_render_handler().paint(browser.clone(), width, height);
}
false
}
}
}
}
fn set_favicon(&self, url: Url) {
let browser = self.cef_browser.borrow();
let browser = match *browser {
None => return,
Some(ref browser) => browser,
};
browser.downcast().favicons.borrow_mut().push(url.to_string().clone());
}
fn status(&self, info: Option<String>) {
let browser = self.cef_browser.borrow();
let browser = match *browser {
None => return,
Some(ref browser) => browser,
};
let str = match info {
Some(s) => {
let utf16_chars: Vec<u16> = Utf16Encoder::new(s.chars()).collect();
utf16_chars
}
None => vec![]
};
if check_ptr_exist!(browser.get_host().get_client(), get_display_handler) &&
check_ptr_exist!(browser.get_host().get_client().get_display_handler(), on_status_message) {
browser.get_host().get_client().get_display_handler().on_status_message((*browser).clone(), str.as_slice());
}
}
fn load_start(&self, back: bool, forward: bool) {
let browser = self.cef_browser.borrow();
let browser = match *browser {
None => return,
Some(ref browser) => browser,
};
browser.downcast().loading.set(true);
browser.downcast().back.set(back);
browser.downcast().forward.set(forward);
browser.downcast().favicons.borrow_mut().clear();
if check_ptr_exist!(browser.get_host().get_client(), get_load_handler) &&
check_ptr_exist!(browser.get_host().get_client().get_load_handler(), on_loading_state_change) {
browser.get_host()
.get_client()
.get_load_handler()
.on_loading_state_change((*browser).clone(), 1i32, back as i32, forward as i32);
}
}
fn load_end(&self, back: bool, forward: bool, _: bool) {
// FIXME(pcwalton): The status code 200 is a lie.
let browser = self.cef_browser.borrow();
let browser = match *browser {
None => return,
Some(ref browser) => browser,
};
browser.downcast().loading.set(false);
browser.downcast().back.set(back);
browser.downcast().forward.set(forward);
if check_ptr_exist!(browser.get_host().get_client(), get_load_handler) &&
check_ptr_exist!(browser.get_host().get_client().get_load_handler(), on_loading_state_change) {
browser.get_host()
.get_client()
.get_load_handler()
.on_loading_state_change((*browser).clone(), 0i32, back as i32, forward as i32);
}
if check_ptr_exist!(browser.get_host().get_client(), get_load_handler) &&
check_ptr_exist!(browser.get_host().get_client().get_load_handler(), on_load_end) {
browser.get_host()
.get_client()
.get_load_handler()
.on_load_end((*browser).clone(), browser.get_main_frame(), 200);
}
}
fn load_error(&self, code: NetError, url: String) {
let browser = self.cef_browser.borrow();
let browser = match *browser {
None => return,
Some(ref browser) => browser,
};
if check_ptr_exist!(browser.get_host().get_client(), get_load_handler) &&
check_ptr_exist!(browser.get_host().get_client().get_load_handler(), on_load_error) {
let utf16_chars: Vec<u16> = Utf16Encoder::new((url).chars()).collect();
browser.get_host()
.get_client()
.get_load_handler()
.on_load_error((*browser).clone(), browser.get_main_frame(),
code, &[], utf16_chars.as_slice());
}
}
fn head_parsed(&self) {
let browser = self.cef_browser.borrow();
let browser = match *browser {
None => return,
Some(ref browser) => browser,
};
if check_ptr_exist!(browser.get_host().get_client(), get_display_handler) &&
check_ptr_exist!(browser.get_host().get_client().get_display_handler(), on_favicon_urlchange) {
browser.get_host().get_client().get_display_handler().on_favicon_urlchange((*browser).clone(), &browser.downcast().favicons.borrow());
}
}
fn set_page_title(&self, string: Option<String>) {
let browser = self.cef_browser.borrow();
let browser = match *browser {
None => return,
Some(ref browser) => browser,
};
let frame = browser.get_main_frame();
let frame = frame.downcast();
let mut title_visitor = frame.title_visitor.borrow_mut();
let str = match string {
Some(s) => {
let utf16_chars: Vec<u16> = Utf16Encoder::new(s.chars()).collect();
utf16_chars
}
None => vec![]
};
if check_ptr_exist!(browser.get_host().get_client(), get_display_handler) &&
check_ptr_exist!(browser.get_host().get_client().get_display_handler(), on_title_change) {
browser.get_host().get_client().get_display_handler().on_title_change((*browser).clone(), str.as_slice());
}
match &mut *title_visitor {
&mut None => {},
&mut Some(ref mut visitor) => {
visitor.visit(&str);
}
};
}
fn set_page_url(&self, url: Url) {
// it seems to be the case that load start is always called
// IMMEDIATELY before address change, so just stick it here
on_load_start(self);
let browser = self.cef_browser.borrow();
let browser = match *browser {
None => return,
Some(ref browser) => browser,
};
let frame = browser.get_main_frame();
let servoframe = frame.downcast();
// FIXME(https://github.com/rust-lang/rust/issues/23338)
let mut frame_url = servoframe.url.borrow_mut();
*frame_url = url.to_string();
let utf16_chars: Vec<u16> = Utf16Encoder::new((*frame_url).chars()).collect();
if check_ptr_exist!(browser.get_host().get_client(), get_display_handler) &&
check_ptr_exist!(browser.get_host().get_client().get_display_handler(), on_address_change) {
browser.get_host().get_client().get_display_handler().on_address_change((*browser).clone(), frame.clone(), utf16_chars.as_slice());
}
}
fn handle_key(&self, _: Option<char>, _: Key, _: KeyModifiers) {
// TODO(negge)
}
fn set_cursor(&self, cursor: Cursor) {
use types::{CefCursorInfo,cef_point_t,cef_size_t};
let browser = self.cef_browser.borrow();
match *browser {
None => {}
Some(ref browser) => {
let cursor_handle = self.cursor_handle_for_cursor(cursor);
let info = CefCursorInfo { hotspot: cef_point_t {x: 0, y: 0}, image_scale_factor: 0.0, buffer: 0 as *mut isize, size: cef_size_t { width: 0, height: 0 } };
if check_ptr_exist!(browser.get_host().get_client(), get_render_handler) &&
check_ptr_exist!(browser.get_host().get_client().get_render_handler(), on_cursor_change) {
browser.get_host()
.get_client()
.get_render_handler()
.on_cursor_change(browser.clone(), cursor_handle,
self.cursor_type_for_cursor(cursor), &info)
}
}
}
}
fn supports_clipboard(&self) -> bool {
false
}
}
struct CefCompositorProxy {
sender: Sender<compositor_thread::Msg>,
}
impl CompositorProxy for CefCompositorProxy {
fn send(&self, msg: compositor_thread::Msg) {
self.sender.send(msg).unwrap();
app_wakeup();
}
fn clone_compositor_proxy(&self) -> Box<CompositorProxy+Send> {
box CefCompositorProxy {
sender: self.sender.clone(),
} as Box<CompositorProxy+Send>
}
}
fn on_load_start(window: &Window) {
let browser = window.cef_browser.borrow();
let browser = match *browser {
None => return,
Some(ref browser) => browser,
};
if check_ptr_exist!(browser.get_host().get_client(), get_load_handler) &&
check_ptr_exist!(browser.get_host().get_client().get_load_handler(), on_load_start) {
browser.get_host()
.get_client()
.get_load_handler()
.on_load_start((*browser).clone(), browser.get_main_frame());
}
}
#[cfg(target_os="macos")]
pub fn app_wakeup() {
use cocoa::appkit::{NSApp, NSApplication, NSApplicationDefined};
use cocoa::appkit::{NSEvent, NSEventModifierFlags, NSEventSubtype};
use cocoa::base::nil;
use cocoa::foundation::{NSAutoreleasePool, NSPoint};
unsafe {
let pool = NSAutoreleasePool::new(nil);
let event =
NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_(
nil,
NSApplicationDefined,
NSPoint::new(0.0, 0.0),
NSEventModifierFlags::empty(),
0.0,
0,
nil,
NSEventSubtype::NSWindowExposedEventType,
0,
0);
NSApp().postEvent_atStart_(event, 0);
pool.drain();
}
}
#[cfg(target_os="linux")]
pub fn app_wakeup() {
unsafe { if CEF_APP.is_null() { return; } }
let capp = unsafe { CefApp::from_c_object_addref(CEF_APP) };
if unsafe { (*CEF_APP).get_browser_process_handler.is_some() } &&
check_ptr_exist!(capp.get_browser_process_handler(), on_work_available) {
capp.get_browser_process_handler().on_work_available();
}
}
#[cfg(target_os="linux")]
pub fn init_window() {
unsafe {
assert!(XInitThreads() != 0);
DISPLAY = XOpenDisplay(ptr::null()) as *mut c_void;
}
}
#[cfg(not(target_os="linux"))]
pub fn init_window() {}
#[cfg(target_os="linux")]
#[no_mangle]
pub extern "C" fn cef_get_xdisplay() -> *mut c_void {
unsafe { DISPLAY }
}
#[cfg(not(target_os="linux"))]
#[no_mangle]
pub extern "C" fn cef_get_xdisplay() -> *mut c_void {
ptr::null_mut()
}
|