Skip to content

Commit 287c999

Browse files
quark-zjufacebook-github-bot
authored andcommitted
webview-sys: vendor the macOS part of the crate
Summary: Imported from https://github.com/Boscop/web-view/ commit 82d7cbce6228b1a964673cc0f22944ad808eab42. Non-mac version is dropped (on Windows `edge.exe --app=url` can be a decent alternative, on Linux it can be challenging to get dependencies ready). We didn't import it to the repo-wide third-party library because `reindeer` uses `cargo vendor` and `cargo vendor` cannot skip (hard-to-build) Linux dependencies even if the crate `webview-sys` is only referred for macOS. See rust-lang/cargo#7058 Reviewed By: sggutier Differential Revision: D48531141 fbshipit-source-id: 4a087e83deae3764edcad5725d1a93d28d8ff6a8
1 parent bbb1119 commit 287c999

File tree

5 files changed

+907
-0
lines changed

5 files changed

+907
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Serge Zaitsev
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
extern crate cc;
2+
3+
use std::env;
4+
5+
fn main() {
6+
let target = env::var("TARGET").unwrap();
7+
8+
let mut build = cc::Build::new();
9+
10+
build
11+
.include("webview.h")
12+
.flag_if_supported("-std=c11")
13+
.flag_if_supported("-w");
14+
15+
if env::var("DEBUG").is_err() {
16+
build.define("NDEBUG", None);
17+
} else {
18+
build.define("DEBUG", None);
19+
}
20+
21+
if target.contains("apple") {
22+
build
23+
.file("webview_cocoa.c")
24+
.flag("-x")
25+
.flag("objective-c");
26+
println!("cargo:rustc-link-lib=framework=Cocoa");
27+
println!("cargo:rustc-link-lib=framework=WebKit");
28+
} else {
29+
panic!("unsupported target (only macos is supported)");
30+
}
31+
32+
build.compile("webview");
33+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//! Raw FFI bindings to [webview].
2+
//!
3+
//! To use a custom version of webview, define an environment variable `WEBVIEW_DIR` with the path
4+
//! to its source directory.
5+
//!
6+
//! [webview]: https://github.com/zserge/webview
7+
8+
use std::os::raw::*;
9+
10+
pub enum CWebView {} // opaque type, only used in ffi pointers
11+
12+
type ErasedExternalInvokeFn = extern "C" fn(webview: *mut CWebView, arg: *const c_char);
13+
type ErasedDispatchFn = extern "C" fn(webview: *mut CWebView, arg: *mut c_void);
14+
15+
extern "C" {
16+
pub fn webview_free(this: *mut CWebView);
17+
pub fn webview_new(
18+
title: *const c_char,
19+
url: *const c_char,
20+
width: c_int,
21+
height: c_int,
22+
resizable: c_int,
23+
debug: c_int,
24+
frameless: c_int,
25+
visible: c_int,
26+
min_width: c_int,
27+
min_height: c_int,
28+
hide_instead_of_close: c_int,
29+
external_invoke_cb: Option<ErasedExternalInvokeFn>,
30+
userdata: *mut c_void,
31+
) -> *mut CWebView;
32+
pub fn webview_loop(this: *mut CWebView, blocking: c_int) -> c_int;
33+
pub fn webview_exit(this: *mut CWebView);
34+
pub fn webview_get_user_data(this: *mut CWebView) -> *mut c_void;
35+
pub fn webview_get_window_handle(this: *mut CWebView) -> *mut c_void;
36+
pub fn webview_dispatch(this: *mut CWebView, f: Option<ErasedDispatchFn>, arg: *mut c_void);
37+
pub fn webview_eval(this: *mut CWebView, js: *const c_char) -> c_int;
38+
pub fn webview_set_title(this: *mut CWebView, title: *const c_char);
39+
pub fn webview_set_fullscreen(this: *mut CWebView, fullscreen: c_int);
40+
pub fn webview_set_maximized(this: *mut CWebView, maximize: c_int);
41+
pub fn webview_set_minimized(this: *mut CWebView, minimize: c_int);
42+
pub fn webview_set_visible(this: *mut CWebView, visible: c_int);
43+
pub fn webview_set_color(this: *mut CWebView, red: u8, green: u8, blue: u8, alpha: u8);
44+
pub fn webview_set_zoom_level(this: *mut CWebView, percentage: c_double);
45+
pub fn webview_set_html(this: *mut CWebView, html: *const c_char);
46+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#ifndef WEBVIEW_H
2+
#define WEBVIEW_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#ifdef WEBVIEW_STATIC
9+
#define WEBVIEW_API static
10+
#else
11+
#define WEBVIEW_API extern
12+
#endif
13+
14+
#include <stdint.h>
15+
#include <string.h>
16+
#include <stdlib.h>
17+
#include <stdio.h>
18+
19+
typedef void* webview_t;
20+
typedef void (*webview_external_invoke_cb_t)(webview_t w, const char *arg);
21+
typedef void (*webview_dispatch_fn)(webview_t w, void *arg);
22+
23+
WEBVIEW_API void webview_run(webview_t w);
24+
WEBVIEW_API int webview_loop(webview_t w, int blocking);
25+
WEBVIEW_API int webview_eval(webview_t w, const char *js);
26+
WEBVIEW_API void webview_set_title(webview_t w, const char *title);
27+
WEBVIEW_API void webview_set_fullscreen(webview_t w, int fullscreen);
28+
WEBVIEW_API void webview_set_maximized(webview_t w, int maximize);
29+
WEBVIEW_API void webview_set_minimized(webview_t w, int minimize);
30+
WEBVIEW_API void webview_set_visible(webview_t w, int minimize);
31+
WEBVIEW_API void webview_set_color(webview_t w, uint8_t r, uint8_t g,
32+
uint8_t b, uint8_t a);
33+
WEBVIEW_API void webview_set_zoom_level(webview_t w, const double percentage);
34+
WEBVIEW_API void webview_set_html(webview_t w, const char *html);
35+
WEBVIEW_API void webview_dispatch(webview_t w, webview_dispatch_fn fn,
36+
void *arg);
37+
WEBVIEW_API void webview_exit(webview_t w);
38+
WEBVIEW_API void webview_debug(const char *format, ...);
39+
WEBVIEW_API void webview_print_log(const char *s);
40+
41+
WEBVIEW_API void* webview_get_user_data(webview_t w);
42+
WEBVIEW_API void* webview_get_window_handle(webview_t w);
43+
WEBVIEW_API webview_t webview_new(const char* title, const char* url, int width, int height, int resizable, int debug, int frameless, int visible, int min_width, int min_height, int hide_instead_of_close, webview_external_invoke_cb_t external_invoke_cb, void* userdata);
44+
WEBVIEW_API void webview_free(webview_t w);
45+
WEBVIEW_API void webview_destroy(webview_t w);
46+
47+
// TODO WEBVIEW_API void webview_navigate(webview_t w, const char* url);
48+
49+
struct webview_dispatch_arg {
50+
webview_dispatch_fn fn;
51+
webview_t w;
52+
void *arg;
53+
};
54+
55+
// Convert ASCII hex digit to a nibble (four bits, 0 - 15).
56+
//
57+
// Use unsigned to avoid signed overflow UB.
58+
static inline unsigned char hex2nibble(unsigned char c) {
59+
if (c >= '0' && c <= '9') {
60+
return c - '0';
61+
} else if (c >= 'a' && c <= 'f') {
62+
return 10 + (c - 'a');
63+
} else if (c >= 'A' && c <= 'F') {
64+
return 10 + (c - 'A');
65+
}
66+
return 0;
67+
}
68+
69+
// Convert ASCII hex string (two characters) to byte.
70+
//
71+
// E.g., "0B" => 0x0B, "af" => 0xAF.
72+
static inline char hex2char(const char* p) {
73+
return hex2nibble(p[0]) * 16 + hex2nibble(p[1]);
74+
}
75+
76+
#ifdef __cplusplus
77+
}
78+
#endif
79+
80+
#endif // WEBVIEW_H

0 commit comments

Comments
 (0)