├── .gitignore ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── cef-sys ├── Cargo.toml ├── build.rs ├── gen-bindings.sh ├── src │ ├── bindings.rs │ ├── bindings_msvc.rs │ └── lib.rs └── wrapper.h └── cef ├── Cargo.toml ├── examples ├── demo.rs ├── worker.rs └── worker_child.rs └── src ├── app ├── browser_process_handler.rs ├── command_line.rs └── mod.rs ├── browser.rs ├── browser_host.rs ├── client ├── display_handler.rs ├── life_span_handler.rs ├── mod.rs └── render_handler.rs ├── frame.rs ├── lib.rs ├── platform ├── linux.rs ├── mod.rs └── windows.rs ├── ptr.rs ├── settings.rs ├── thread.rs ├── types ├── mod.rs ├── point.rs ├── range.rs ├── rect.rs ├── size.rs └── string.rs └── window.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | /.idea 5 | /*.iml 6 | /cef-sys/lib 7 | /GPUCache -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | 3 | members = [ 4 | "cef-sys", 5 | "cef" 6 | ] 7 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | MIT License (MIT) 2 | 3 | Copyright (c) 2018 Julian Waller 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rust CEF SDK 2 | 3 | Bindings to the [Chromium Embedded Framework](https://bitbucket.org/chromiumembedded/cef/wiki/Home) for Rust. 4 | This aims to be an easy to use and safe wrapper around the CEF capi. 5 | It aims to follow the C++ api wherever possible, to make porting code or learning from other documentation easy. 6 | 7 | Note: This is a work in progress. Many areas are not yet implemented, and it is not very well tested yet. 8 | 9 | ## Installation 10 | 11 | You will need to download the minimal distribution of CEF from http://opensource.spotify.com/cefbuilds/index.html. 12 | Currently this has only been tested with `CEF 75.0.13+g2c92fcd+chromium-75.0.3770.100` other versions may work, but may also unexpectedly crash. 13 | 14 | Unzip the downloaded distribution, and inform cargo where it is located through the CEF_ROOT environment variable 15 | 16 | eg for linux: `export CEF_ROOT=/home/julus/Projects/cef_binary_75.0.13+g2c92fcd+chromium-75.0.3770.100_linux64_minimal` 17 | 18 | You can then run cargo commands like usual and it should work as expected. 19 | 20 | ## Usage 21 | 22 | See the examples, or the official C++ docs for more information. 23 | 24 | ## License 25 | 26 | Licensed under either of 27 | 28 | * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) 29 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) 30 | 31 | at your option. 32 | 33 | ### Contribution 34 | 35 | Unless you explicitly state otherwise, any contribution intentionally submitted 36 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any 37 | additional terms or conditions. -------------------------------------------------------------------------------- /cef-sys/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "cef-sys" 3 | version = "0.1.0" 4 | license = "MIT/Apache-2.0" 5 | authors = ["Julian Waller "] 6 | edition = "2018" 7 | build="build.rs" 8 | links = "cef" 9 | 10 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 11 | 12 | [dependencies] 13 | 14 | [build-dependencies] 15 | fs_extra = "1.1.0" 16 | -------------------------------------------------------------------------------- /cef-sys/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | use std::path::Path; 3 | 4 | enum Platform { 5 | Windows, 6 | // Mac, // TODO - enable once mac has been tested 7 | Linux, 8 | } 9 | 10 | fn get_platform() -> Platform { 11 | match std::env::var("TARGET").unwrap().split('-').nth(2).unwrap() { 12 | "win32" | "windows" => Platform::Windows, 13 | // "darwin" => Platform::Mac, 14 | "linux" => Platform::Linux, 15 | other => panic!("Sorry, platform \"{}\" is not supported by CEF.", other), 16 | } 17 | } 18 | 19 | fn choose_source_dir() -> Option { 20 | if let Ok(path) = env::var("CEF_ROOT") { 21 | if Path::new(&path).exists() { 22 | return Some(path); 23 | } 24 | } 25 | None 26 | } 27 | 28 | fn main() { 29 | let source_dir = choose_source_dir().expect("Failed to locate CEF lib path"); 30 | 31 | // Inform rust what to link 32 | match get_platform() { 33 | Platform::Windows => println!("cargo:rustc-link-lib=libcef"), 34 | Platform::Linux => println!("cargo:rustc-link-lib=cef"), 35 | }; 36 | 37 | println!("Path: {:?}", source_dir); 38 | 39 | let release_dir = Path::new(&source_dir).join("Release"); 40 | let resources_dir = Path::new(&source_dir).join("Resources"); 41 | 42 | if !release_dir.exists() { 43 | panic!( 44 | "CEF Release directory ({}) does not exist", 45 | release_dir.to_str().unwrap_or_else(|| "") 46 | ); 47 | } 48 | if !resources_dir.exists() { 49 | panic!( 50 | "CEF Resources directory ({}) does not exist", 51 | resources_dir.to_str().unwrap_or_else(|| "") 52 | ); 53 | } 54 | 55 | if let Some(release_dir) = release_dir.to_str() { 56 | println!("cargo:rustc-link-search=native={}", release_dir); 57 | } 58 | 59 | // Copy the required Resources & Release contents to OUT_DIR so that a cargo run works 60 | let dest_path = Path::new(&env::var("OUT_DIR").unwrap()).join("../../.."); 61 | 62 | let opts = fs_extra::dir::CopyOptions { 63 | overwrite: true, 64 | skip_exist: false, 65 | buffer_size: 64000, // Default 66 | copy_inside: true, 67 | depth: 0, 68 | }; 69 | 70 | let mut release_items = fs_extra::dir::get_dir_content(&release_dir).unwrap(); 71 | let mut resources_items = fs_extra::dir::get_dir_content(&resources_dir).unwrap(); 72 | 73 | let mut all_items = Vec::new(); 74 | all_items.append(&mut release_items.directories); 75 | all_items.append(&mut release_items.files); 76 | all_items.append(&mut resources_items.directories); 77 | all_items.append(&mut resources_items.files); 78 | 79 | fs_extra::copy_items(&all_items, &dest_path, &opts).unwrap(); 80 | } 81 | -------------------------------------------------------------------------------- /cef-sys/gen-bindings.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | unameOut="$(uname -s)" 4 | case "${unameOut}" in 5 | Linux*) outfile=src/bindings.rs;; 6 | # Darwin*) machine=Mac;; 7 | CYGWIN*) outfile=src/bindings_msvc.rs;; 8 | MINGW*) outfile=src/bindings_msvc.rs;; 9 | *) echo "Unknown platform"; exit 1 10 | esac 11 | 12 | echo "Generating to: ${outfile}" 13 | 14 | bindgen wrapper.h -o $outfile \ 15 | --rust-target nightly \ 16 | --default-enum-style=rust_non_exhaustive \ 17 | --whitelist-type cef_.* \ 18 | --whitelist-function cef_.* \ 19 | --bitfield-enum .*_mask_t \ 20 | -- -I $CEF_ROOT 21 | -------------------------------------------------------------------------------- /cef-sys/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![feature(non_exhaustive)] 2 | 3 | #[allow( 4 | non_snake_case, 5 | non_camel_case_types, 6 | non_upper_case_globals, 7 | dead_code, 8 | clippy::all 9 | )] 10 | #[cfg(not(target_os = "windows"))] 11 | #[link(name = "cef", kind = "dynamic")] 12 | mod bindings; 13 | 14 | #[cfg(not(target_os = "windows"))] 15 | pub use bindings::*; 16 | 17 | #[allow( 18 | non_snake_case, 19 | non_camel_case_types, 20 | non_upper_case_globals, 21 | dead_code, 22 | clippy::all 23 | )] 24 | #[cfg(target_os = "windows")] 25 | #[link(name = "cef", kind = "dynamic")] 26 | mod bindings_msvc; 27 | 28 | #[cfg(target_os = "windows")] 29 | pub use bindings_msvc::*; 30 | -------------------------------------------------------------------------------- /cef-sys/wrapper.h: -------------------------------------------------------------------------------- 1 | 2 | #include "include/capi/cef_base_capi.h" 3 | #include "include/capi/cef_app_capi.h" 4 | #include "include/capi/cef_client_capi.h" 5 | -------------------------------------------------------------------------------- /cef/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "cef" 3 | version = "0.1.0" 4 | license = "MIT/Apache-2.0" 5 | authors = ["Julian Waller "] 6 | edition = "2018" 7 | 8 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 9 | 10 | [dependencies] 11 | cef-sys = { path="../cef-sys" } 12 | widestring = "0.4.0" 13 | -------------------------------------------------------------------------------- /cef/examples/demo.rs: -------------------------------------------------------------------------------- 1 | #![feature(async_await)] 2 | 3 | use cef::{ 4 | create_browser_sync, Browser, BrowserSettings, CefRect, PaintElementType, RenderHandler, 5 | Settings, WindowInfo, 6 | }; 7 | use std::sync::Arc; 8 | use std::time::Duration; 9 | 10 | struct MyApp {} 11 | impl cef::App for MyApp { 12 | type OutBrowserProcessHandler = (); 13 | } 14 | 15 | struct MyRenderHandler {} 16 | impl RenderHandler for MyRenderHandler { 17 | fn get_root_screen_rect(&self, _browser: &Browser) -> Option { 18 | None 19 | } 20 | 21 | fn get_view_rect(&self, _browser: &Browser) -> CefRect { 22 | CefRect { 23 | x: 0, 24 | y: 0, 25 | width: 800, 26 | height: 800, 27 | } 28 | } 29 | 30 | fn on_paint( 31 | &self, 32 | _browser: &Browser, 33 | _type_: PaintElementType, 34 | _dirty_rects: &[CefRect], 35 | _bytes: &[u8], 36 | _width: i32, 37 | _height: i32, 38 | ) { 39 | println!("Got frame"); 40 | } 41 | } 42 | 43 | struct MyClient { 44 | render_handler: Arc, 45 | } 46 | impl cef::Client for MyClient { 47 | type OutAudioHandler = (); 48 | type OutDisplayHandler = (); 49 | type OutLifeSpanHandler = (); 50 | type OutRenderHandler = MyRenderHandler; 51 | 52 | fn get_render_handler(&self) -> Option> { 53 | Some(self.render_handler.clone()) 54 | } 55 | } 56 | 57 | fn main() { 58 | let app = Arc::new(MyApp {}); 59 | 60 | { 61 | let result_code = cef::execute_process(&app); 62 | if result_code >= 0 { 63 | // It was a worker process, so stop here 64 | std::process::exit(result_code as i32); 65 | } 66 | } 67 | 68 | let mut settings = Settings::default(); 69 | // settings.log_severity = cef_log_severity_t_LOGSEVERITY_VERBOSE; 70 | settings.remote_debugging_port = Some(9876); 71 | settings.windowless_rendering_enabled = true; 72 | settings.multi_threaded_message_loop = true; 73 | 74 | cef::initialize(settings, &app); 75 | 76 | println!("ready"); 77 | 78 | // std::thread::spawn(|| { 79 | // std::thread::sleep(Duration::from_secs(2)); 80 | 81 | // let client2 = client.clone(); 82 | cef::post_task(cef::ThreadId::TID_UI, move || { 83 | let mut window_info = WindowInfo::default(); 84 | window_info.width = 1280; 85 | window_info.height = 720; 86 | window_info.windowless_rendering_enabled = true; 87 | 88 | let mut browser_settings = BrowserSettings::default(); 89 | browser_settings.windowless_frame_rate = 30; // TODO - not necessary here? 90 | 91 | let client = Arc::new(MyClient { 92 | render_handler: Arc::new(MyRenderHandler {}), 93 | }); 94 | 95 | // Open a window 96 | create_browser_sync(window_info, &client, "http://google.com", browser_settings); 97 | }) 98 | .unwrap(); 99 | 100 | println!("waiting"); 101 | std::thread::sleep(Duration::from_secs(600)); 102 | 103 | println!("quit"); 104 | cef::quit_message_loop(); 105 | // TODO - this doesnt appear to be stopping the loop.. 106 | // }); 107 | 108 | // cef::run_message_loop(); 109 | 110 | println!("shutting fown"); 111 | 112 | cef::shutdown(); 113 | } 114 | -------------------------------------------------------------------------------- /cef/examples/worker.rs: -------------------------------------------------------------------------------- 1 | #![feature(async_await)] 2 | 3 | use cef::{ 4 | create_browser_sync, Browser, BrowserSettings, CefRect, PaintElementType, RenderHandler, 5 | Settings, WindowInfo, 6 | }; 7 | use std::path::Path; 8 | use std::sync::Arc; 9 | use std::time::Duration; 10 | 11 | struct MyApp {} 12 | impl cef::App for MyApp { 13 | type OutBrowserProcessHandler = (); 14 | } 15 | 16 | struct MyRenderHandler {} 17 | impl RenderHandler for MyRenderHandler { 18 | fn get_root_screen_rect(&self, _browser: &Browser) -> Option { 19 | None 20 | } 21 | 22 | fn get_view_rect(&self, _browser: &Browser) -> CefRect { 23 | CefRect { 24 | x: 0, 25 | y: 0, 26 | width: 800, 27 | height: 800, 28 | } 29 | } 30 | 31 | fn on_paint( 32 | &self, 33 | _browser: &Browser, 34 | _type_: PaintElementType, 35 | _dirty_rects: &[CefRect], 36 | _bytes: &[u8], 37 | _width: i32, 38 | _height: i32, 39 | ) { 40 | println!("Got frame"); 41 | } 42 | } 43 | 44 | struct MyClient { 45 | render_handler: Arc, 46 | } 47 | impl cef::Client for MyClient { 48 | type OutAudioHandler = (); 49 | type OutDisplayHandler = (); 50 | type OutLifeSpanHandler = (); 51 | type OutRenderHandler = MyRenderHandler; 52 | 53 | fn get_render_handler(&self) -> Option> { 54 | Some(self.render_handler.clone()) 55 | } 56 | } 57 | 58 | fn main() { 59 | let app = Arc::new(MyApp {}); 60 | 61 | let exe_path = std::env::current_exe().unwrap(); 62 | let dir = Path::parent(&exe_path).unwrap(); 63 | let subprocess_path = dir.join("worker_child"); 64 | 65 | let mut settings = Settings::default(); 66 | // settings.log_severity = cef_log_severity_t_LOGSEVERITY_VERBOSE; 67 | settings.remote_debugging_port = Some(9876); 68 | // settings.windowless_rendering_enabled = true; 69 | settings.browser_subprocess_path = Some(subprocess_path.to_str().unwrap()); 70 | 71 | cef::initialize(settings, &app); 72 | 73 | println!("ready"); 74 | 75 | std::thread::spawn(|| { 76 | std::thread::sleep(Duration::from_secs(2)); 77 | 78 | let mut window_info = WindowInfo::default(); 79 | window_info.width = 1280; 80 | window_info.height = 720; 81 | // window_info.windowless_rendering_enabled = true; 82 | 83 | let mut browser_settings = BrowserSettings::default(); 84 | browser_settings.windowless_frame_rate = 30; // TODO - not necessary here? 85 | 86 | let client = Arc::new(MyClient { 87 | render_handler: Arc::new(MyRenderHandler {}), 88 | }); 89 | 90 | let client2 = client.clone(); 91 | cef::post_task(cef::ThreadId::TID_UI, move || { 92 | // Open a window 93 | create_browser_sync(window_info, &client2, "http://google.com", browser_settings); 94 | }) 95 | .unwrap(); 96 | 97 | println!("waiting"); 98 | std::thread::sleep(Duration::from_secs(600)); 99 | 100 | println!("quit"); 101 | cef::quit_message_loop(); 102 | // TODO - this doesnt appear to be stopping the loop.. 103 | }); 104 | 105 | cef::run_message_loop(); 106 | 107 | println!("shutting fown"); 108 | 109 | cef::shutdown(); 110 | } 111 | -------------------------------------------------------------------------------- /cef/examples/worker_child.rs: -------------------------------------------------------------------------------- 1 | use std::sync::Arc; 2 | 3 | struct MyApp {} 4 | impl cef::App for MyApp { 5 | type OutBrowserProcessHandler = (); 6 | } 7 | 8 | // This example will never be run directly, but is the child subprocess of the worker example 9 | fn main() { 10 | let app = Arc::new(MyApp {}); 11 | 12 | std::process::exit(cef::execute_process(&app)) 13 | } 14 | -------------------------------------------------------------------------------- /cef/src/app/browser_process_handler.rs: -------------------------------------------------------------------------------- 1 | use crate::ptr::{wrap_ptr, BaseRefCountedExt, WrapperFor}; 2 | use crate::{CommandLine, ToCef}; 3 | use cef_sys::{ 4 | cef_browser_process_handler_t, cef_command_line_t, cef_list_value_t, cef_print_handler_t, 5 | }; 6 | use std::ptr::null_mut; 7 | use std::sync::Arc; 8 | 9 | pub trait BrowserProcessHandler { 10 | fn on_context_initialized(&self) {} 11 | fn on_before_child_process_launch(&self, _command_line: &CommandLine) {} 12 | fn on_render_process_thread_created(&self, _extra_info: *mut cef_list_value_t) {} 13 | // fn get_print_handler(&self, extra_info) -> *mut cef_print_handler_t {} 14 | fn on_schedule_message_pump_work(&self, _delay_ms: i64) {} 15 | } 16 | impl BrowserProcessHandler for () {} 17 | 18 | struct BrowserProcessHandlerWrapper { 19 | _base: cef_browser_process_handler_t, 20 | internal: Arc, 21 | } 22 | unsafe impl WrapperFor 23 | for BrowserProcessHandlerWrapper 24 | { 25 | } 26 | impl BrowserProcessHandlerWrapper { 27 | fn from_ptr<'a>( 28 | ptr: *mut cef_browser_process_handler_t, 29 | ) -> &'a mut BaseRefCountedExt> 30 | { 31 | unsafe { &mut *(ptr as *mut _) } 32 | } 33 | 34 | unsafe extern "C" fn on_context_initialized(handler: *mut cef_browser_process_handler_t) { 35 | let handler = Self::from_ptr(handler); 36 | 37 | handler.internal.on_context_initialized() 38 | } 39 | 40 | unsafe extern "C" fn on_before_child_process_launch( 41 | handler: *mut cef_browser_process_handler_t, 42 | command_line: *mut cef_command_line_t, 43 | ) { 44 | let handler = Self::from_ptr(handler); 45 | let command_line = CommandLine::from(command_line, false); 46 | 47 | handler 48 | .internal 49 | .on_before_child_process_launch(&command_line); 50 | } 51 | unsafe extern "C" fn on_render_process_thread_created( 52 | handler: *mut cef_browser_process_handler_t, 53 | extra_info: *mut cef_list_value_t, 54 | ) { 55 | let handler = Self::from_ptr(handler); 56 | 57 | handler 58 | .internal 59 | .on_render_process_thread_created(extra_info) 60 | } 61 | 62 | unsafe extern "C" fn get_print_handler( 63 | handler: *mut cef_browser_process_handler_t, 64 | ) -> *mut cef_print_handler_t { 65 | let handler = Self::from_ptr(handler); 66 | 67 | // handler.internal.get_print_handler() 68 | null_mut() 69 | } 70 | 71 | unsafe extern "C" fn on_schedule_message_pump_work( 72 | handler: *mut cef_browser_process_handler_t, 73 | delay_ms: i64, 74 | ) { 75 | let handler = Self::from_ptr(handler); 76 | 77 | handler.internal.on_schedule_message_pump_work(delay_ms) 78 | } 79 | } 80 | impl ToCef for Arc { 81 | fn to_cef(&self) -> *mut cef_browser_process_handler_t { 82 | wrap_ptr(|base| BrowserProcessHandlerWrapper { 83 | _base: cef_browser_process_handler_t { 84 | base, 85 | on_context_initialized: Some( 86 | BrowserProcessHandlerWrapper::::on_context_initialized, 87 | ), 88 | on_before_child_process_launch: Some( 89 | BrowserProcessHandlerWrapper::::on_before_child_process_launch, 90 | ), 91 | on_render_process_thread_created: Some( 92 | BrowserProcessHandlerWrapper::::on_render_process_thread_created, 93 | ), 94 | get_print_handler: Some(BrowserProcessHandlerWrapper::::get_print_handler), 95 | on_schedule_message_pump_work: Some( 96 | BrowserProcessHandlerWrapper::::on_schedule_message_pump_work, 97 | ), 98 | }, 99 | internal: self.clone(), 100 | }) 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /cef/src/app/command_line.rs: -------------------------------------------------------------------------------- 1 | use crate::ptr::RefCounterGuard; 2 | use crate::types::string::CefString; 3 | use cef_sys::cef_command_line_t; 4 | use std::collections::HashMap; 5 | 6 | #[derive(Clone)] 7 | pub struct CommandLine { 8 | ptr: RefCounterGuard, 9 | } 10 | impl CommandLine { 11 | pub(crate) fn from(ptr: *mut cef_command_line_t, track_ref: bool) -> CommandLine { 12 | unsafe { 13 | CommandLine { 14 | ptr: RefCounterGuard::from(&mut (*ptr).base, ptr, track_ref), 15 | } 16 | } 17 | } 18 | 19 | pub fn is_valid(&self) -> bool { 20 | if let Some(func) = self.ptr.as_ref().is_valid { 21 | unsafe { func(self.ptr.get()) > 0 } 22 | } else { 23 | false 24 | } 25 | } 26 | 27 | pub fn is_read_only(&self) -> bool { 28 | if let Some(func) = self.ptr.as_ref().is_read_only { 29 | unsafe { func(self.ptr.get()) > 0 } 30 | } else { 31 | false 32 | } 33 | } 34 | 35 | pub fn copy(&self) -> CommandLine { 36 | self.clone() 37 | } 38 | 39 | pub fn init_from_argv( 40 | &self, 41 | _argc: ::std::os::raw::c_int, 42 | _argv: *const *const ::std::os::raw::c_char, 43 | ) { 44 | // TODO 45 | } 46 | 47 | pub fn init_from_string(&self, command_line: &str) { 48 | if let Some(func) = self.ptr.as_ref().init_from_string { 49 | let command_line = CefString::from_str(command_line); 50 | unsafe { func(self.ptr.get(), &command_line.into_cef()) } 51 | } 52 | } 53 | 54 | pub fn reset(&self) { 55 | if let Some(func) = self.ptr.as_ref().reset { 56 | unsafe { func(self.ptr.get()) } 57 | } 58 | } 59 | 60 | pub fn get_argv(&self) -> Vec { 61 | if let Some(func) = self.ptr.as_ref().get_argv { 62 | unsafe { 63 | let arguments = cef_sys::cef_string_list_alloc(); 64 | func(self.ptr.get(), arguments); 65 | 66 | let res = CefString::parse_string_list(arguments); 67 | cef_sys::cef_string_list_free(arguments); 68 | 69 | res 70 | } 71 | } else { 72 | Vec::new() 73 | } 74 | } 75 | 76 | pub fn get_command_line_string(&self) -> String { 77 | if let Some(func) = self.ptr.as_ref().get_command_line_string { 78 | CefString::from_userfree_cef(unsafe { func(self.ptr.get()) }).to_string() 79 | } else { 80 | "".to_string() 81 | } 82 | } 83 | 84 | pub fn get_program(&self) -> String { 85 | if let Some(func) = self.ptr.as_ref().get_program { 86 | CefString::from_userfree_cef(unsafe { func(self.ptr.get()) }).to_string() 87 | } else { 88 | "".to_string() 89 | } 90 | } 91 | 92 | pub fn set_program(&self, program: &str) { 93 | if let Some(func) = self.ptr.as_ref().set_program { 94 | let program = CefString::from_str(program); 95 | unsafe { func(self.ptr.get(), &program.into_cef()) } 96 | } 97 | } 98 | 99 | pub fn has_switches(&self) -> bool { 100 | if let Some(func) = self.ptr.as_ref().has_switches { 101 | unsafe { func(self.ptr.get()) > 0 } 102 | } else { 103 | false 104 | } 105 | } 106 | 107 | pub fn has_switch(&self, name: &str) -> bool { 108 | if let Some(func) = self.ptr.as_ref().has_switch { 109 | let name = CefString::from_str(name); 110 | unsafe { func(self.ptr.get(), &name.into_cef()) > 0 } 111 | } else { 112 | false 113 | } 114 | } 115 | 116 | pub fn get_switch_value(&self, name: &str) -> String { 117 | if let Some(func) = self.ptr.as_ref().get_switch_value { 118 | let name = CefString::from_str(name); 119 | CefString::from_userfree_cef(unsafe { func(self.ptr.get(), &name.into_cef()) }) 120 | .to_string() 121 | } else { 122 | "".to_string() 123 | } 124 | } 125 | 126 | pub fn get_switches(&self) -> HashMap { 127 | if let Some(func) = self.ptr.as_ref().get_switches { 128 | unsafe { 129 | let switches = cef_sys::cef_string_map_alloc(); 130 | func(self.ptr.get(), switches); 131 | 132 | let res = CefString::parse_string_map(switches); 133 | cef_sys::cef_string_map_free(switches); 134 | 135 | res 136 | } 137 | } else { 138 | HashMap::new() 139 | } 140 | } 141 | 142 | pub fn append_switch(&self, name: &str) { 143 | if let Some(func) = self.ptr.as_ref().append_switch { 144 | let name = CefString::from_str(name); 145 | unsafe { func(self.ptr.get(), &name.into_cef()) } 146 | } 147 | } 148 | 149 | pub fn append_switch_with_value(&self, name: &str, value: &str) { 150 | if let Some(func) = self.ptr.as_ref().append_switch_with_value { 151 | let name = CefString::from_str(name); 152 | let value = CefString::from_str(value); 153 | unsafe { func(self.ptr.get(), &name.into_cef(), &value.into_cef()) } 154 | } 155 | } 156 | 157 | pub fn has_arguments(&self) -> bool { 158 | if let Some(func) = self.ptr.as_ref().has_arguments { 159 | unsafe { func(self.ptr.get()) > 0 } 160 | } else { 161 | false 162 | } 163 | } 164 | 165 | pub fn get_arguments(&self) -> Vec { 166 | if let Some(func) = self.ptr.as_ref().get_arguments { 167 | unsafe { 168 | let arguments = cef_sys::cef_string_list_alloc(); 169 | func(self.ptr.get(), arguments); 170 | 171 | let res = CefString::parse_string_list(arguments); 172 | cef_sys::cef_string_list_free(arguments); 173 | 174 | res 175 | } 176 | } else { 177 | Vec::new() 178 | } 179 | } 180 | 181 | pub fn append_argument(&self, argument: &str) { 182 | if let Some(func) = self.ptr.as_ref().append_argument { 183 | let argument = CefString::from_str(argument); 184 | unsafe { func(self.ptr.get(), &argument.into_cef()) } 185 | } 186 | } 187 | 188 | pub fn prepend_wrapper(&self, wrapper: &str) { 189 | if let Some(func) = self.ptr.as_ref().prepend_wrapper { 190 | let wrapper = CefString::from_str(wrapper); 191 | unsafe { func(self.ptr.get(), &wrapper.into_cef()) } 192 | } 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /cef/src/app/mod.rs: -------------------------------------------------------------------------------- 1 | mod browser_process_handler; 2 | mod command_line; 3 | 4 | use crate::ptr::{wrap_ptr, BaseRefCountedExt, WrapperFor}; 5 | use crate::string::CefString; 6 | use crate::ToCef; 7 | pub use browser_process_handler::*; 8 | use cef_sys::{ 9 | cef_app_t, cef_browser_process_handler_t, cef_command_line_t, cef_render_process_handler_t, 10 | cef_resource_bundle_handler_t, cef_scheme_registrar_t, cef_string_t, 11 | }; 12 | pub use command_line::*; 13 | use std::ptr::null_mut; 14 | use std::sync::Arc; 15 | 16 | pub trait App { 17 | type OutBrowserProcessHandler: BrowserProcessHandler; 18 | 19 | fn on_before_command_line_processing(&self, _process_type: &str, _command_line: &CommandLine) {} 20 | // TODO - finish implementing 21 | // fn on_register_custom_schemes(&self) {} 22 | // fn get_resource_bundle_handler(&self) {} 23 | fn get_browser_process_handler(&self) -> Option> { 24 | None 25 | } 26 | // fn get_render_process_handler(&self) {} 27 | } 28 | 29 | #[repr(C)] 30 | struct AppWrapper { 31 | base: cef_app_t, 32 | internal: Arc, 33 | } 34 | unsafe impl WrapperFor for AppWrapper {} 35 | impl AppWrapper { 36 | fn from_ptr<'a>(ptr: *mut cef_app_t) -> &'a mut BaseRefCountedExt> { 37 | unsafe { &mut *(ptr as *mut _) } 38 | } 39 | extern "C" fn on_before_command_line_processing( 40 | app: *mut cef_app_t, 41 | process_type: *const cef_string_t, 42 | command_line: *mut cef_command_line_t, 43 | ) { 44 | let app = Self::from_ptr(app); 45 | let process_type = CefString::from_cef(process_type); 46 | let command_line = CommandLine::from(command_line, false); 47 | app.internal 48 | .on_before_command_line_processing(&process_type.to_string(), &command_line); 49 | } 50 | 51 | extern "C" fn on_register_custom_schemes( 52 | _app: *mut cef_app_t, 53 | _registrar: *mut cef_scheme_registrar_t, 54 | ) { 55 | // let app = Self::from_ptr(app); 56 | // TODO registrar 57 | // app.internal.on_register_custom_schemes(); 58 | } 59 | extern "C" fn get_resource_bundle_handler( 60 | _app: *mut cef_app_t, 61 | ) -> *mut cef_resource_bundle_handler_t { 62 | // let app = Self::from_ptr(app); 63 | 64 | // app.internal.get_resource_bundle_handler(); 65 | null_mut() 66 | } 67 | extern "C" fn get_browser_process_handler( 68 | app: *mut cef_app_t, 69 | ) -> *mut cef_browser_process_handler_t { 70 | let app = Self::from_ptr(app); 71 | if let Some(handler) = app.internal.get_browser_process_handler() { 72 | handler.to_cef() 73 | } else { 74 | null_mut() 75 | } 76 | } 77 | extern "C" fn get_render_process_handler( 78 | _app: *mut cef_app_t, 79 | ) -> *mut cef_render_process_handler_t { 80 | // let app = Self::from_ptr(app); 81 | 82 | // app.internal.get_render_process_handler(); 83 | null_mut() 84 | } 85 | } 86 | impl ToCef for Arc { 87 | fn to_cef(&self) -> *mut cef_app_t { 88 | wrap_ptr(|base| AppWrapper { 89 | base: cef_app_t { 90 | base, 91 | on_before_command_line_processing: Some( 92 | AppWrapper::::on_before_command_line_processing, 93 | ), 94 | on_register_custom_schemes: Some(AppWrapper::::on_register_custom_schemes), 95 | get_resource_bundle_handler: Some(AppWrapper::::get_resource_bundle_handler), 96 | get_browser_process_handler: Some(AppWrapper::::get_browser_process_handler), 97 | get_render_process_handler: Some(AppWrapper::::get_render_process_handler), 98 | }, 99 | internal: self.clone(), 100 | }) 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /cef/src/browser.rs: -------------------------------------------------------------------------------- 1 | use crate::ptr::RefCounterGuard; 2 | use crate::types::string::CefString; 3 | use crate::{BrowserHost, Frame}; 4 | use cef_sys::{cef_browser_host_t, cef_browser_t}; 5 | use std::ptr::null_mut; 6 | 7 | pub struct Browser { 8 | ptr: RefCounterGuard, 9 | } 10 | impl Browser { 11 | pub(crate) fn from(ptr: *mut cef_browser_t, track_ref: bool) -> Browser { 12 | unsafe { 13 | Browser { 14 | ptr: RefCounterGuard::from(&mut (*ptr).base, ptr, track_ref), 15 | } 16 | } 17 | } 18 | 19 | pub fn get_host(&self) -> Option { 20 | if let Some(func) = self.ptr.as_ref().get_host { 21 | Some(BrowserHost::from(unsafe { func(self.ptr.get()) }, true)) 22 | } else { 23 | None 24 | } 25 | } 26 | 27 | pub fn can_go_back(&self) -> bool { 28 | if let Some(func) = self.ptr.as_ref().can_go_back { 29 | unsafe { func(self.ptr.get()) > 0 } 30 | } else { 31 | false 32 | } 33 | } 34 | 35 | pub fn go_back(&self) { 36 | if let Some(func) = self.ptr.as_ref().go_back { 37 | unsafe { func(self.ptr.get()) } 38 | } 39 | } 40 | 41 | pub fn can_go_forward(&self) -> bool { 42 | if let Some(func) = self.ptr.as_ref().can_go_forward { 43 | unsafe { func(self.ptr.get()) > 0 } 44 | } else { 45 | false 46 | } 47 | } 48 | 49 | pub fn go_forward(&self) { 50 | if let Some(func) = self.ptr.as_ref().go_forward { 51 | unsafe { func(self.ptr.get()) } 52 | } 53 | } 54 | 55 | pub fn is_loading(&self) -> bool { 56 | if let Some(func) = self.ptr.as_ref().is_loading { 57 | unsafe { func(self.ptr.get()) > 0 } 58 | } else { 59 | false 60 | } 61 | } 62 | 63 | pub fn reload(&self) { 64 | if let Some(func) = self.ptr.as_ref().reload { 65 | unsafe { func(self.ptr.get()) } 66 | } 67 | } 68 | 69 | pub fn reload_ignore_cache(&self) { 70 | if let Some(func) = self.ptr.as_ref().reload_ignore_cache { 71 | unsafe { func(self.ptr.get()) } 72 | } 73 | } 74 | 75 | pub fn stop_load(&self) { 76 | if let Some(func) = self.ptr.as_ref().stop_load { 77 | unsafe { func(self.ptr.get()) } 78 | } 79 | } 80 | 81 | pub fn get_identifier(&self) -> i32 { 82 | if let Some(func) = self.ptr.as_ref().get_identifier { 83 | unsafe { func(self.ptr.get()) } 84 | } else { 85 | 0 86 | } 87 | } 88 | 89 | pub fn is_same(&self, that: &Self) -> bool { 90 | if let Some(func) = self.ptr.as_ref().is_same { 91 | unsafe { func(self.ptr.get(), that.ptr.get()) > 0 } 92 | } else { 93 | false 94 | } 95 | } 96 | 97 | pub fn is_popup(&self) -> bool { 98 | if let Some(func) = self.ptr.as_ref().is_popup { 99 | unsafe { func(self.ptr.get()) > 0 } 100 | } else { 101 | false 102 | } 103 | } 104 | 105 | pub fn has_document(&self) -> bool { 106 | if let Some(func) = self.ptr.as_ref().has_document { 107 | unsafe { func(self.ptr.get()) > 0 } 108 | } else { 109 | false 110 | } 111 | } 112 | 113 | pub fn get_main_frame(&self) -> Option { 114 | if let Some(func) = self.ptr.as_ref().get_main_frame { 115 | let ptr = unsafe { func(self.ptr.get()) }; 116 | if !ptr.is_null() { 117 | Some(Frame::from(ptr, true)) 118 | } else { 119 | None 120 | } 121 | } else { 122 | None 123 | } 124 | } 125 | 126 | pub fn get_focused_frame(&self) -> Option { 127 | if let Some(func) = self.ptr.as_ref().get_focused_frame { 128 | let ptr = unsafe { func(self.ptr.get()) }; 129 | if !ptr.is_null() { 130 | Some(Frame::from(ptr, true)) 131 | } else { 132 | None 133 | } 134 | } else { 135 | None 136 | } 137 | } 138 | 139 | pub fn get_frame_byident(&self, identifier: i64) -> Option { 140 | if let Some(func) = self.ptr.as_ref().get_frame_byident { 141 | let ptr = unsafe { func(self.ptr.get(), identifier) }; 142 | if !ptr.is_null() { 143 | Some(Frame::from(ptr, true)) 144 | } else { 145 | None 146 | } 147 | } else { 148 | None 149 | } 150 | } 151 | 152 | pub fn get_frame(&self, name: &str) -> Option { 153 | if let Some(func) = self.ptr.as_ref().get_frame { 154 | let name = CefString::from_str(name); 155 | let ptr = unsafe { func(self.ptr.get(), &name.into_cef()) }; 156 | if !ptr.is_null() { 157 | Some(Frame::from(ptr, true)) 158 | } else { 159 | None 160 | } 161 | } else { 162 | None 163 | } 164 | } 165 | 166 | pub fn get_frame_count(&self) -> usize { 167 | if let Some(func) = self.ptr.as_ref().get_frame_count { 168 | unsafe { func(self.ptr.get()) } 169 | } else { 170 | 0 171 | } 172 | } 173 | 174 | pub fn get_frame_identifiers(&self) -> Vec { 175 | if let Some(func) = self.ptr.as_ref().get_frame_identifiers { 176 | unsafe { 177 | let mut identifiers = vec![0; self.get_frame_count()]; 178 | 179 | let mut count = 0; 180 | func(self.ptr.get(), &mut count, identifiers.as_mut_ptr()); 181 | 182 | identifiers.resize(count, 0); 183 | identifiers 184 | } 185 | } else { 186 | Vec::new() 187 | } 188 | } 189 | 190 | pub fn get_frame_names(&self) -> Vec { 191 | if let Some(func) = self.ptr.as_ref().get_frame_names { 192 | unsafe { 193 | let names = cef_sys::cef_string_list_alloc(); 194 | func(self.ptr.get(), names); 195 | 196 | let res = CefString::parse_string_list(names); 197 | cef_sys::cef_string_list_free(names); 198 | 199 | res 200 | } 201 | } else { 202 | Vec::new() 203 | } 204 | } 205 | } 206 | -------------------------------------------------------------------------------- /cef/src/browser_host.rs: -------------------------------------------------------------------------------- 1 | use crate::ptr::RefCounterGuard; 2 | use crate::types::string::CefString; 3 | use crate::{Browser, CefWindowHandle, Frame, PaintElementType}; 4 | use cef_sys::{ 5 | cef_browser_host_t, cef_browser_settings_t, cef_client_t, cef_composition_underline_t, 6 | cef_download_image_callback_t, cef_drag_data_t, cef_drag_operations_mask_t, cef_extension_t, 7 | cef_file_dialog_mode_t, cef_key_event_t, cef_mouse_button_type_t, cef_mouse_event_t, 8 | cef_navigation_entry_t, cef_navigation_entry_visitor_t, cef_paint_element_type_t, 9 | cef_pdf_print_callback_t, cef_pdf_print_settings_t, cef_point_t, cef_range_t, 10 | cef_request_context_t, cef_run_file_dialog_callback_t, cef_size_t, cef_state_t, 11 | cef_string_list_t, cef_string_t, cef_touch_event_t, cef_window_info_t, 12 | }; 13 | use std::ptr::null_mut; 14 | 15 | pub type FileDialogMode = cef_file_dialog_mode_t; 16 | 17 | pub struct BrowserHost { 18 | ptr: RefCounterGuard, 19 | } 20 | impl BrowserHost { 21 | pub(crate) fn from(ptr: *mut cef_browser_host_t, track_ref: bool) -> BrowserHost { 22 | unsafe { 23 | BrowserHost { 24 | ptr: RefCounterGuard::from(&mut (*ptr).base, ptr, track_ref), 25 | } 26 | } 27 | } 28 | 29 | pub fn get_browser(&self) -> Option { 30 | if let Some(func) = self.ptr.as_ref().get_browser { 31 | Some(Browser::from(unsafe { func(self.ptr.get()) }, true)) 32 | } else { 33 | None 34 | } 35 | } 36 | 37 | pub fn close_browser(&self, force_close: bool) { 38 | if let Some(func) = self.ptr.as_ref().close_browser { 39 | unsafe { func(self.ptr.get(), force_close as i32) } 40 | } 41 | } 42 | 43 | pub fn try_close_browser(&self) -> bool { 44 | if let Some(func) = self.ptr.as_ref().try_close_browser { 45 | unsafe { func(self.ptr.get()) != 0 } 46 | } else { 47 | false 48 | } 49 | } 50 | 51 | pub fn set_focus(&self, focus: bool) { 52 | if let Some(func) = self.ptr.as_ref().set_focus { 53 | unsafe { func(self.ptr.get(), focus as i32) } 54 | } 55 | } 56 | 57 | pub fn get_window_handle(&self) -> CefWindowHandle { 58 | if let Some(func) = self.ptr.as_ref().get_window_handle { 59 | unsafe { func(self.ptr.get()) } 60 | } else { 61 | CefWindowHandle::default() 62 | } 63 | } 64 | 65 | pub fn get_opener_window_handle(&self) -> CefWindowHandle { 66 | if let Some(func) = self.ptr.as_ref().get_opener_window_handle { 67 | unsafe { func(self.ptr.get()) } 68 | } else { 69 | CefWindowHandle::default() 70 | } 71 | } 72 | 73 | pub fn has_view(&self) -> bool { 74 | if let Some(func) = self.ptr.as_ref().has_view { 75 | unsafe { func(self.ptr.get()) != 0 } 76 | } else { 77 | false 78 | } 79 | } 80 | 81 | // pub fn get_client(&self) -> *mut cef_client_t {} 82 | 83 | // pub fn get_request_context(&self) -> *mut cef_request_context_t {} 84 | 85 | pub fn get_zoom_level(&self) -> f64 { 86 | if let Some(func) = self.ptr.as_ref().get_zoom_level { 87 | unsafe { func(self.ptr.get()) } 88 | } else { 89 | 0.0 90 | } 91 | } 92 | 93 | pub fn set_zoom_level(&self, zoom_level: f64) { 94 | if let Some(func) = self.ptr.as_ref().set_zoom_level { 95 | unsafe { func(self.ptr.get(), zoom_level) } 96 | } 97 | } 98 | 99 | // pub fn run_file_dialog( 100 | // &self, 101 | // mode: FileDialogMode, 102 | // title: &str, 103 | // default_file_path: &str, 104 | // accept_filters: cef_string_list_t, 105 | // selected_accept_filter: i32, 106 | // callback: *mut cef_run_file_dialog_callback_t, 107 | // ) { 108 | // } 109 | 110 | pub fn start_download(&self, url: &str) { 111 | if let Some(func) = self.ptr.as_ref().start_download { 112 | let url = CefString::from_str(url); 113 | unsafe { func(self.ptr.get(), &url.into_cef()) } 114 | } 115 | } 116 | 117 | // pub fn download_image( 118 | // &self, 119 | // image_url: *const cef_string_t, 120 | // is_favicon: ::std::os::raw::c_int, 121 | // max_image_size: u32, 122 | // bypass_cache: ::std::os::raw::c_int, 123 | // callback: *mut cef_download_image_callback_t, 124 | // ) { 125 | // } 126 | 127 | pub fn print(&self) { 128 | if let Some(func) = self.ptr.as_ref().print { 129 | unsafe { func(self.ptr.get()) } 130 | } 131 | } 132 | 133 | // pub fn print_to_pdf( 134 | // &self, 135 | // path: *const cef_string_t, 136 | // settings: *const cef_pdf_print_settings_t, 137 | // callback: *mut cef_pdf_print_callback_t, 138 | // ) { 139 | // } 140 | 141 | pub fn find( 142 | &self, 143 | identifier: i32, 144 | search_text: &str, 145 | forward: bool, 146 | match_case: bool, 147 | find_next: bool, 148 | ) { 149 | if let Some(func) = self.ptr.as_ref().find { 150 | let search_text = CefString::from_str(search_text); 151 | unsafe { 152 | func( 153 | self.ptr.get(), 154 | identifier, 155 | &search_text.into_cef(), 156 | forward as i32, 157 | match_case as i32, 158 | find_next as i32, 159 | ) 160 | } 161 | } 162 | } 163 | 164 | pub fn stop_finding(&self, clear_selection: bool) { 165 | if let Some(func) = self.ptr.as_ref().stop_finding { 166 | unsafe { func(self.ptr.get(), clear_selection as i32) } 167 | } 168 | } 169 | 170 | // pub fn show_dev_tools( 171 | // &self, 172 | // windowInfo: *const cef_window_info_t, 173 | // client: *mut cef_client_t, 174 | // settings: *const cef_browser_settings_t, 175 | // inspect_element_at: *const cef_point_t, 176 | // ) { 177 | // } 178 | 179 | pub fn close_dev_tools(&self) { 180 | if let Some(func) = self.ptr.as_ref().close_dev_tools { 181 | unsafe { func(self.ptr.get()) } 182 | } 183 | } 184 | 185 | pub fn has_dev_tools(&self) -> bool { 186 | if let Some(func) = self.ptr.as_ref().has_dev_tools { 187 | unsafe { func(self.ptr.get()) != 0 } 188 | } else { 189 | false 190 | } 191 | } 192 | 193 | // pub fn get_navigation_entries( 194 | // &self, 195 | // visitor: *mut cef_navigation_entry_visitor_t, 196 | // current_only: ::std::os::raw::c_int, 197 | // ) { 198 | // } 199 | 200 | pub fn set_mouse_cursor_change_disabled(&self, disabled: bool) { 201 | if let Some(func) = self.ptr.as_ref().set_mouse_cursor_change_disabled { 202 | unsafe { func(self.ptr.get(), disabled as i32) } 203 | } 204 | } 205 | 206 | pub fn is_mouse_cursor_change_disabled(&self) -> bool { 207 | if let Some(func) = self.ptr.as_ref().is_mouse_cursor_change_disabled { 208 | unsafe { func(self.ptr.get()) != 0 } 209 | } else { 210 | false 211 | } 212 | } 213 | 214 | pub fn replace_misspelling(&self, word: &str) { 215 | if let Some(func) = self.ptr.as_ref().replace_misspelling { 216 | let word = CefString::from_str(word); 217 | unsafe { func(self.ptr.get(), &word.into_cef()) } 218 | } 219 | } 220 | 221 | pub fn add_word_to_dictionary(&self, word: &str) { 222 | if let Some(func) = self.ptr.as_ref().add_word_to_dictionary { 223 | let word = CefString::from_str(word); 224 | unsafe { func(self.ptr.get(), &word.into_cef()) } 225 | } 226 | } 227 | 228 | pub fn is_window_rendering_disabled(&self) -> bool { 229 | if let Some(func) = self.ptr.as_ref().is_window_rendering_disabled { 230 | unsafe { func(self.ptr.get()) != 0 } 231 | } else { 232 | false 233 | } 234 | } 235 | 236 | pub fn was_resized(&self) { 237 | if let Some(func) = self.ptr.as_ref().was_resized { 238 | unsafe { func(self.ptr.get()) } 239 | } 240 | } 241 | 242 | pub fn was_hidden(&self, hidden: bool) { 243 | if let Some(func) = self.ptr.as_ref().was_hidden { 244 | unsafe { func(self.ptr.get(), hidden as i32) } 245 | } 246 | } 247 | 248 | pub fn notify_screen_info_changed(&self) { 249 | if let Some(func) = self.ptr.as_ref().notify_screen_info_changed { 250 | unsafe { func(self.ptr.get()) } 251 | } 252 | } 253 | 254 | pub fn invalidate(&self, type_: PaintElementType) { 255 | if let Some(func) = self.ptr.as_ref().invalidate { 256 | unsafe { func(self.ptr.get(), type_) } 257 | } 258 | } 259 | 260 | pub fn send_external_begin_frame(&self) { 261 | if let Some(func) = self.ptr.as_ref().send_external_begin_frame { 262 | unsafe { func(self.ptr.get()) } 263 | } 264 | } 265 | 266 | // pub fn send_key_event(&self, event: *const cef_key_event_t) {} 267 | // 268 | // pub fn send_mouse_click_event( 269 | // &self, 270 | // event: *const cef_mouse_event_t, 271 | // type_: cef_mouse_button_type_t, 272 | // mouseUp: ::std::os::raw::c_int, 273 | // clickCount: ::std::os::raw::c_int, 274 | // ) { 275 | // } 276 | // 277 | // pub fn send_mouse_move_event( 278 | // &self, 279 | // event: *const cef_mouse_event_t, 280 | // mouseLeave: ::std::os::raw::c_int, 281 | // ) { 282 | // } 283 | // 284 | // pub fn send_mouse_wheel_event( 285 | // &self, 286 | // event: *const cef_mouse_event_t, 287 | // deltaX: ::std::os::raw::c_int, 288 | // deltaY: ::std::os::raw::c_int, 289 | // ) { 290 | // } 291 | // 292 | // pub fn send_touch_event(&self, event: *const cef_touch_event_t) {} 293 | // 294 | // pub fn send_focus_event(&self, setFocus: ::std::os::raw::c_int) {} 295 | // 296 | // pub fn send_capture_lost_event(&self) {} 297 | // 298 | // pub fn notify_move_or_resize_started(&self) {} 299 | // 300 | // pub fn get_windowless_frame_rate(&self) -> ::std::os::raw::c_int {} 301 | // 302 | // pub fn set_windowless_frame_rate(&self, frame_rate: ::std::os::raw::c_int) {} 303 | // 304 | // pub fn ime_set_composition( 305 | // &self, 306 | // text: *const cef_string_t, 307 | // underlinesCount: usize, 308 | // underlines: *const cef_composition_underline_t, 309 | // replacement_range: *const cef_range_t, 310 | // selection_range: *const cef_range_t, 311 | // ) { 312 | // } 313 | // 314 | // pub fn ime_commit_text( 315 | // &self, 316 | // text: *const cef_string_t, 317 | // replacement_range: *const cef_range_t, 318 | // relative_cursor_pos: ::std::os::raw::c_int, 319 | // ) { 320 | // } 321 | // 322 | // pub fn ime_finish_composing_text(&self, keep_selection: ::std::os::raw::c_int) {} 323 | // 324 | // pub fn ime_cancel_composition(&self) {} 325 | // 326 | // pub fn drag_target_drag_enter( 327 | // &self, 328 | // drag_data: *mut cef_drag_data_t, 329 | // event: *const cef_mouse_event_t, 330 | // allowed_ops: cef_drag_operations_mask_t, 331 | // ) { 332 | // } 333 | // 334 | // pub fn drag_target_drag_over( 335 | // &self, 336 | // event: *const cef_mouse_event_t, 337 | // allowed_ops: cef_drag_operations_mask_t, 338 | // ) { 339 | // } 340 | // 341 | // pub fn drag_target_drag_leave(&self) {} 342 | // 343 | // pub fn drag_target_drop(&self, event: *const cef_mouse_event_t) {} 344 | // 345 | // pub fn drag_source_ended_at( 346 | // &self, 347 | // x: ::std::os::raw::c_int, 348 | // y: ::std::os::raw::c_int, 349 | // op: cef_drag_operations_mask_t, 350 | // ) { 351 | // } 352 | // 353 | // pub fn drag_source_system_drag_ended(&self) {} 354 | // 355 | // pub fn get_visible_navigation_entry(&self) -> *mut cef_navigation_entry_t {} 356 | // 357 | // pub fn set_accessibility_state(&self, accessibility_state: cef_state_t) {} 358 | // 359 | // pub fn set_auto_resize_enabled( 360 | // &self, 361 | // enabled: ::std::os::raw::c_int, 362 | // min_size: *const cef_size_t, 363 | // max_size: *const cef_size_t, 364 | // ) { 365 | // } 366 | // 367 | // pub fn get_extension(&self) -> *mut cef_extension_t {} 368 | // 369 | // pub fn is_background_host(&self) -> ::std::os::raw::c_int {} 370 | // 371 | // pub fn set_audio_muted(&self, mute: ::std::os::raw::c_int) {} 372 | // 373 | // pub fn is_audio_muted(&self) -> ::std::os::raw::c_int {} 374 | } 375 | -------------------------------------------------------------------------------- /cef/src/client/display_handler.rs: -------------------------------------------------------------------------------- 1 | use crate::ptr::{wrap_ptr, BaseRefCountedExt, WrapperFor}; 2 | use crate::types::string::CefString; 3 | use crate::{Browser, CefSize, Frame, LogSeverity, ToCef}; 4 | use cef_sys::{ 5 | cef_browser_t, cef_display_handler_t, cef_frame_t, cef_log_severity_t, cef_size_t, 6 | cef_string_list_t, cef_string_t, 7 | }; 8 | use std::sync::Arc; 9 | 10 | pub trait DisplayHandler { 11 | fn on_address_change(&self, _browser: &Browser, _frame: &Frame, _url: String) {} 12 | fn on_title_change(&self, _browser: &Browser, _title: String) {} 13 | fn on_favicon_url_change(&self, _browser: &Browser, _icon_urls: Vec) {} 14 | fn on_fullscreen_mode_change(&self, _browser: &Browser, _fullscreen: bool) {} 15 | fn on_tooltip(&self, _browser: &Browser, _text: String) -> bool { 16 | false 17 | } 18 | fn on_status_message(&self, _browser: &Browser, _value: String) {} 19 | fn on_console_message( 20 | &self, 21 | _browser: &Browser, 22 | _level: LogSeverity, 23 | _message: String, 24 | _source: String, 25 | _line: i32, 26 | ) -> bool { 27 | false 28 | } 29 | fn on_auto_resize(&self, _browser: &Browser, _size: CefSize) -> bool { 30 | false 31 | } 32 | fn on_loading_progress_change(&self, _browser: &Browser, _progress: f64) {} 33 | } 34 | impl DisplayHandler for () {} 35 | 36 | struct DisplayHandlerWrapper { 37 | _base: cef_display_handler_t, 38 | internal: Arc, 39 | } 40 | unsafe impl WrapperFor for DisplayHandlerWrapper {} 41 | impl DisplayHandlerWrapper { 42 | fn from_ptr<'a>( 43 | ptr: *mut cef_display_handler_t, 44 | ) -> &'a mut BaseRefCountedExt> { 45 | unsafe { &mut *(ptr as *mut _) } 46 | } 47 | 48 | unsafe extern "C" fn on_address_change( 49 | handler: *mut cef_display_handler_t, 50 | browser: *mut cef_browser_t, 51 | frame: *mut cef_frame_t, 52 | url: *const cef_string_t, 53 | ) { 54 | let handler = Self::from_ptr(handler); 55 | let browser = Browser::from(browser, false); 56 | let frame = Frame::from(frame, false); 57 | let url = CefString::from_cef(url); 58 | 59 | handler 60 | .internal 61 | .on_address_change(&browser, &frame, url.to_string()) 62 | } 63 | 64 | unsafe extern "C" fn on_title_change( 65 | handler: *mut cef_display_handler_t, 66 | browser: *mut cef_browser_t, 67 | title: *const cef_string_t, 68 | ) { 69 | let handler = Self::from_ptr(handler); 70 | let browser = Browser::from(browser, false); 71 | let title = CefString::from_cef(title); 72 | 73 | handler 74 | .internal 75 | .on_title_change(&browser, title.to_string()) 76 | } 77 | 78 | unsafe extern "C" fn on_favicon_urlchange( 79 | handler: *mut cef_display_handler_t, 80 | browser: *mut cef_browser_t, 81 | icon_urls: cef_string_list_t, 82 | ) { 83 | let handler = Self::from_ptr(handler); 84 | let browser = Browser::from(browser, false); 85 | let icon_urls = CefString::parse_string_list(icon_urls); 86 | 87 | handler.internal.on_favicon_url_change(&browser, icon_urls) 88 | } 89 | 90 | unsafe extern "C" fn on_fullscreen_mode_change( 91 | handler: *mut cef_display_handler_t, 92 | browser: *mut cef_browser_t, 93 | fullscreen: ::std::os::raw::c_int, 94 | ) { 95 | let handler = Self::from_ptr(handler); 96 | let browser = Browser::from(browser, false); 97 | 98 | handler 99 | .internal 100 | .on_fullscreen_mode_change(&browser, fullscreen != 0) 101 | } 102 | 103 | unsafe extern "C" fn on_tooltip( 104 | handler: *mut cef_display_handler_t, 105 | browser: *mut cef_browser_t, 106 | text: *mut cef_string_t, 107 | ) -> ::std::os::raw::c_int { 108 | let handler = Self::from_ptr(handler); 109 | let browser = Browser::from(browser, false); 110 | let text = CefString::from_cef(text); 111 | 112 | handler.internal.on_tooltip(&browser, text.to_string()) as i32 113 | } 114 | 115 | unsafe extern "C" fn on_status_message( 116 | handler: *mut cef_display_handler_t, 117 | browser: *mut cef_browser_t, 118 | value: *const cef_string_t, 119 | ) { 120 | let handler = Self::from_ptr(handler); 121 | let browser = Browser::from(browser, false); 122 | let value = CefString::from_cef(value); 123 | 124 | handler 125 | .internal 126 | .on_status_message(&browser, value.to_string()) 127 | } 128 | 129 | unsafe extern "C" fn on_console_message( 130 | handler: *mut cef_display_handler_t, 131 | browser: *mut cef_browser_t, 132 | level: cef_log_severity_t, 133 | message: *const cef_string_t, 134 | source: *const cef_string_t, 135 | line: ::std::os::raw::c_int, 136 | ) -> ::std::os::raw::c_int { 137 | let handler = Self::from_ptr(handler); 138 | let browser = Browser::from(browser, false); 139 | let level = std::mem::transmute(level); 140 | let message = CefString::from_cef(message); 141 | let source = CefString::from_cef(source); 142 | 143 | handler.internal.on_console_message( 144 | &browser, 145 | level, 146 | message.to_string(), 147 | source.to_string(), 148 | line, 149 | ) as i32 150 | } 151 | 152 | unsafe extern "C" fn on_auto_resize( 153 | handler: *mut cef_display_handler_t, 154 | browser: *mut cef_browser_t, 155 | new_size: *const cef_size_t, 156 | ) -> ::std::os::raw::c_int { 157 | let handler = Self::from_ptr(handler); 158 | let browser = Browser::from(browser, false); 159 | let new_size = CefSize::from_ptr(new_size); 160 | 161 | handler.internal.on_auto_resize(&browser, new_size) as i32 162 | } 163 | 164 | unsafe extern "C" fn on_loading_progress_change( 165 | handler: *mut cef_display_handler_t, 166 | browser: *mut cef_browser_t, 167 | progress: f64, 168 | ) { 169 | let handler = Self::from_ptr(handler); 170 | let browser = Browser::from(browser, false); 171 | 172 | handler 173 | .internal 174 | .on_loading_progress_change(&browser, progress) 175 | } 176 | } 177 | impl ToCef for Arc { 178 | fn to_cef(&self) -> *mut cef_display_handler_t { 179 | wrap_ptr(|base| DisplayHandlerWrapper { 180 | _base: cef_display_handler_t { 181 | base, 182 | on_address_change: Some(DisplayHandlerWrapper::::on_address_change), 183 | on_title_change: Some(DisplayHandlerWrapper::::on_title_change), 184 | on_favicon_urlchange: Some(DisplayHandlerWrapper::::on_favicon_urlchange), 185 | on_fullscreen_mode_change: Some( 186 | DisplayHandlerWrapper::::on_fullscreen_mode_change, 187 | ), 188 | on_tooltip: Some(DisplayHandlerWrapper::::on_tooltip), 189 | on_status_message: Some(DisplayHandlerWrapper::::on_status_message), 190 | on_console_message: Some(DisplayHandlerWrapper::::on_console_message), 191 | on_auto_resize: Some(DisplayHandlerWrapper::::on_auto_resize), 192 | on_loading_progress_change: Some( 193 | DisplayHandlerWrapper::::on_loading_progress_change, 194 | ), 195 | }, 196 | internal: self.clone(), 197 | }) 198 | } 199 | } 200 | -------------------------------------------------------------------------------- /cef/src/client/life_span_handler.rs: -------------------------------------------------------------------------------- 1 | use crate::ptr::{wrap_ptr, BaseRefCountedExt, WrapperFor}; 2 | use crate::types::string::CefString; 3 | use crate::{Browser, Frame, ToCef}; 4 | use cef_sys::{ 5 | _cef_popup_features_t, cef_browser_settings_t, cef_browser_t, cef_client_t, cef_frame_t, 6 | cef_life_span_handler_t, cef_string_t, cef_window_info_t, cef_window_open_disposition_t, 7 | }; 8 | use std::sync::Arc; 9 | 10 | pub trait LifeSpanHandler { 11 | fn on_before_popup( 12 | &self, 13 | _browser: &Browser, 14 | _frame: &Frame, 15 | _target_url: String, 16 | _target_frame_name: String, 17 | _target_disposition: (), 18 | _user_gesture: bool, 19 | ) -> bool { 20 | false 21 | } 22 | fn on_after_created(&self, _browser: &Browser) {} 23 | fn do_close(&self, _browser: &Browser) -> bool { 24 | false 25 | } 26 | fn on_before_close(&self, _browser: &Browser) {} 27 | } 28 | impl LifeSpanHandler for () {} 29 | 30 | struct LifeSpanHandlerWrapper { 31 | _base: cef_life_span_handler_t, 32 | internal: Arc, 33 | } 34 | unsafe impl WrapperFor for LifeSpanHandlerWrapper {} 35 | impl LifeSpanHandlerWrapper { 36 | fn from_ptr<'a>( 37 | ptr: *mut cef_life_span_handler_t, 38 | ) -> &'a mut BaseRefCountedExt> { 39 | unsafe { &mut *(ptr as *mut _) } 40 | } 41 | 42 | unsafe extern "C" fn on_before_popup( 43 | handler: *mut cef_life_span_handler_t, 44 | browser: *mut cef_browser_t, 45 | frame: *mut cef_frame_t, 46 | target_url: *const cef_string_t, 47 | target_frame_name: *const cef_string_t, 48 | _target_disposition: cef_window_open_disposition_t, 49 | user_gesture: ::std::os::raw::c_int, 50 | _popup_features: *const _cef_popup_features_t, 51 | _window_info: *mut cef_window_info_t, 52 | _client: *mut *mut cef_client_t, 53 | _settings: *mut cef_browser_settings_t, 54 | _no_javascript_access: *mut ::std::os::raw::c_int, 55 | ) -> ::std::os::raw::c_int { 56 | let handler = Self::from_ptr(handler); 57 | let browser = Browser::from(browser, false); 58 | let frame = Frame::from(frame, false); 59 | let target_url = CefString::from_cef(target_url); 60 | let target_frame_name = CefString::from_cef(target_frame_name); 61 | // target_disposition 62 | 63 | // TODO - finish 64 | handler.internal.on_before_popup( 65 | &browser, 66 | &frame, 67 | target_url.to_string(), 68 | target_frame_name.to_string(), 69 | (), 70 | user_gesture != 0, 71 | ) as i32 72 | } 73 | 74 | unsafe extern "C" fn on_after_created( 75 | handler: *mut cef_life_span_handler_t, 76 | browser: *mut cef_browser_t, 77 | ) { 78 | let handler = Self::from_ptr(handler); 79 | let browser = Browser::from(browser, false); 80 | 81 | handler.internal.on_after_created(&browser) 82 | } 83 | 84 | unsafe extern "C" fn do_close( 85 | handler: *mut cef_life_span_handler_t, 86 | browser: *mut cef_browser_t, 87 | ) -> ::std::os::raw::c_int { 88 | let handler = Self::from_ptr(handler); 89 | let browser = Browser::from(browser, false); 90 | 91 | handler.internal.do_close(&browser) as i32 92 | } 93 | 94 | unsafe extern "C" fn on_before_close( 95 | handler: *mut cef_life_span_handler_t, 96 | browser: *mut cef_browser_t, 97 | ) { 98 | let handler = Self::from_ptr(handler); 99 | let browser = Browser::from(browser, false); 100 | 101 | handler.internal.on_before_close(&browser) 102 | } 103 | } 104 | impl ToCef for Arc { 105 | fn to_cef(&self) -> *mut cef_life_span_handler_t { 106 | wrap_ptr(|base| LifeSpanHandlerWrapper { 107 | _base: cef_life_span_handler_t { 108 | base, 109 | on_before_popup: Some(LifeSpanHandlerWrapper::::on_before_popup), 110 | on_after_created: Some(LifeSpanHandlerWrapper::::on_after_created), 111 | do_close: Some(LifeSpanHandlerWrapper::::do_close), 112 | on_before_close: Some(LifeSpanHandlerWrapper::::on_before_close), 113 | }, 114 | internal: self.clone(), 115 | }) 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /cef/src/client/mod.rs: -------------------------------------------------------------------------------- 1 | mod display_handler; 2 | mod life_span_handler; 3 | mod render_handler; 4 | 5 | pub use display_handler::*; 6 | pub use life_span_handler::*; 7 | pub use render_handler::*; 8 | 9 | use crate::ptr::{wrap_ptr, BaseRefCountedExt, WrapperFor}; 10 | use crate::ToCef; 11 | use cef_sys::{ 12 | cef_audio_handler_t, cef_browser_t, cef_client_t, cef_context_menu_handler_t, 13 | cef_dialog_handler_t, cef_display_handler_t, cef_download_handler_t, cef_drag_handler_t, 14 | cef_find_handler_t, cef_focus_handler_t, cef_jsdialog_handler_t, cef_keyboard_handler_t, 15 | cef_life_span_handler_t, cef_load_handler_t, cef_process_id_t, cef_process_message_t, 16 | cef_render_handler_t, cef_request_handler_t, 17 | }; 18 | use std::ptr::null_mut; 19 | use std::sync::Arc; 20 | 21 | pub trait AudioHandler { 22 | // 23 | } 24 | impl AudioHandler for () {} 25 | 26 | pub trait Client { 27 | type OutAudioHandler: AudioHandler; 28 | type OutDisplayHandler: DisplayHandler; 29 | type OutLifeSpanHandler: LifeSpanHandler; 30 | type OutRenderHandler: RenderHandler; 31 | 32 | // TODO - fill out 33 | fn get_audio_handler(&self) -> Option> { 34 | None 35 | } 36 | fn get_display_handler(&self) -> Option> { 37 | None 38 | } 39 | fn get_life_span_handler(&self) -> Option> { 40 | None 41 | } 42 | fn get_render_handler(&self) -> Option> { 43 | None 44 | } 45 | } 46 | 47 | struct ClientWrapper { 48 | _base: cef_client_t, 49 | internal: Arc, 50 | } 51 | unsafe impl WrapperFor for ClientWrapper {} 52 | impl ClientWrapper { 53 | fn from_ptr<'a>( 54 | ptr: *mut cef_client_t, 55 | ) -> &'a mut BaseRefCountedExt> { 56 | unsafe { &mut *(ptr as *mut _) } 57 | } 58 | 59 | extern "C" fn get_audio_handler(_client: *mut cef_client_t) -> *mut cef_audio_handler_t { 60 | null_mut() 61 | } 62 | 63 | extern "C" fn get_context_menu_handler( 64 | _client: *mut cef_client_t, 65 | ) -> *mut cef_context_menu_handler_t { 66 | null_mut() 67 | } 68 | 69 | extern "C" fn get_dialog_handler(_client: *mut cef_client_t) -> *mut cef_dialog_handler_t { 70 | null_mut() 71 | } 72 | 73 | extern "C" fn get_display_handler(client: *mut cef_client_t) -> *mut cef_display_handler_t { 74 | let client = Self::from_ptr(client); 75 | if let Some(handler) = client.internal.get_display_handler() { 76 | handler.to_cef() 77 | } else { 78 | null_mut() 79 | } 80 | } 81 | 82 | extern "C" fn get_download_handler(_client: *mut cef_client_t) -> *mut cef_download_handler_t { 83 | null_mut() 84 | } 85 | 86 | extern "C" fn get_drag_handler(_client: *mut cef_client_t) -> *mut cef_drag_handler_t { 87 | null_mut() 88 | } 89 | 90 | extern "C" fn get_find_handler(_client: *mut cef_client_t) -> *mut cef_find_handler_t { 91 | null_mut() 92 | } 93 | 94 | extern "C" fn get_focus_handler(_client: *mut cef_client_t) -> *mut cef_focus_handler_t { 95 | null_mut() 96 | } 97 | 98 | extern "C" fn get_jsdialog_handler(_client: *mut cef_client_t) -> *mut cef_jsdialog_handler_t { 99 | null_mut() 100 | } 101 | 102 | extern "C" fn get_keyboard_handler(_client: *mut cef_client_t) -> *mut cef_keyboard_handler_t { 103 | null_mut() 104 | } 105 | 106 | extern "C" fn get_life_span_handler(client: *mut cef_client_t) -> *mut cef_life_span_handler_t { 107 | let client = Self::from_ptr(client); 108 | if let Some(handler) = client.internal.get_life_span_handler() { 109 | handler.to_cef() 110 | } else { 111 | null_mut() 112 | } 113 | } 114 | 115 | extern "C" fn get_load_handler(_client: *mut cef_client_t) -> *mut cef_load_handler_t { 116 | null_mut() 117 | } 118 | 119 | extern "C" fn get_render_handler(client: *mut cef_client_t) -> *mut cef_render_handler_t { 120 | let client = Self::from_ptr(client); 121 | if let Some(handler) = client.internal.get_render_handler() { 122 | handler.to_cef() 123 | } else { 124 | null_mut() 125 | } 126 | } 127 | 128 | extern "C" fn get_request_handler(_client: *mut cef_client_t) -> *mut cef_request_handler_t { 129 | null_mut() 130 | } 131 | 132 | extern "C" fn on_process_message_received( 133 | _client: *mut cef_client_t, 134 | _browser: *mut cef_browser_t, 135 | _source_process: cef_process_id_t, 136 | _message: *mut cef_process_message_t, 137 | ) -> ::std::os::raw::c_int { 138 | 0 139 | } 140 | } 141 | impl ToCef for Arc { 142 | fn to_cef(&self) -> *mut cef_client_t { 143 | wrap_ptr(|base| ClientWrapper { 144 | _base: cef_client_t { 145 | base, 146 | get_audio_handler: Some(ClientWrapper::::get_audio_handler), 147 | get_context_menu_handler: Some(ClientWrapper::::get_context_menu_handler), 148 | get_dialog_handler: Some(ClientWrapper::::get_dialog_handler), 149 | get_display_handler: Some(ClientWrapper::::get_display_handler), 150 | get_download_handler: Some(ClientWrapper::::get_download_handler), 151 | get_drag_handler: Some(ClientWrapper::::get_drag_handler), 152 | get_find_handler: Some(ClientWrapper::::get_find_handler), 153 | get_focus_handler: Some(ClientWrapper::::get_focus_handler), 154 | get_jsdialog_handler: Some(ClientWrapper::::get_jsdialog_handler), 155 | get_keyboard_handler: Some(ClientWrapper::::get_keyboard_handler), 156 | get_life_span_handler: Some(ClientWrapper::::get_life_span_handler), 157 | get_load_handler: Some(ClientWrapper::::get_load_handler), 158 | get_render_handler: Some(ClientWrapper::::get_render_handler), 159 | get_request_handler: Some(ClientWrapper::::get_request_handler), 160 | on_process_message_received: Some(ClientWrapper::::on_process_message_received), 161 | }, 162 | internal: self.clone(), 163 | }) 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /cef/src/client/render_handler.rs: -------------------------------------------------------------------------------- 1 | use crate::ptr::{wrap_ptr, BaseRefCountedExt, WrapperFor}; 2 | use crate::types::string::CefString; 3 | use crate::{ 4 | Browser, CefCursorInternal, CefPoint, CefRange, CefRect, DragOperationsMask, PaintElementType, 5 | TextInputMode, ToCef, 6 | }; 7 | use cef_sys::{ 8 | cef_accessibility_handler_t, cef_browser_t, cef_cursor_info_t, cef_cursor_type_t, 9 | cef_drag_data_t, cef_drag_operations_mask_t, cef_paint_element_type_t, cef_range_t, cef_rect_t, 10 | cef_render_handler_t, cef_screen_info_t, cef_string_t, cef_text_input_mode_t, 11 | }; 12 | use std::ptr::null_mut; 13 | use std::slice::from_raw_parts; 14 | use std::sync::Arc; 15 | 16 | pub trait RenderHandler { 17 | //get_accessibility_handler 18 | fn get_root_screen_rect(&self, browser: &Browser) -> Option; 19 | fn get_view_rect(&self, browser: &Browser) -> CefRect; 20 | fn get_screen_point(&self, _browser: &Browser, _view: CefPoint) -> Option { 21 | None 22 | } 23 | //get_screen_info: 24 | fn on_popup_show(&self, _browser: &Browser, _show: bool) {} 25 | fn on_popup_size(&self, _browser: &Browser, _rect: CefRect) {} 26 | fn on_paint( 27 | &self, 28 | browser: &Browser, 29 | type_: PaintElementType, 30 | dirty_rects: &[CefRect], 31 | bytes: &[u8], 32 | width: i32, 33 | height: i32, 34 | ); 35 | fn on_accelerated_paint( 36 | &self, 37 | _browser: &Browser, 38 | _type_: PaintElementType, 39 | _dirty_rects: &[CefRect], 40 | _shader_handle: *mut std::os::raw::c_void, 41 | ) { 42 | } 43 | //on_cursor_change: 44 | //start_dragging: 45 | fn update_drag_cursor(&self, _browser: &Browser, _operation: DragOperationsMask) {} 46 | fn on_scroll_offset_changed(&self, _browser: &Browser, _x: f64, _y: f64) {} 47 | fn on_ime_composition_range_changed( 48 | &self, 49 | _browser: &Browser, 50 | _selected_range: CefRange, 51 | _character_bounds: &[CefRect], 52 | ) { 53 | } 54 | fn on_text_selection_changed( 55 | &self, 56 | _browser: &Browser, 57 | _selected_text: String, 58 | _selected_range: CefRange, 59 | ) { 60 | } 61 | fn on_virtual_keyboard_requested(&self, _browser: &Browser, _input_mode: TextInputMode) {} 62 | } 63 | 64 | impl RenderHandler for () { 65 | fn get_root_screen_rect(&self, _browser: &Browser) -> Option { 66 | None 67 | } 68 | 69 | fn get_view_rect(&self, _browser: &Browser) -> CefRect { 70 | CefRect::default() 71 | } 72 | 73 | fn on_paint( 74 | &self, 75 | _browser: &Browser, 76 | _type_: PaintElementType, 77 | _dirty_rects: &[CefRect], 78 | _bytes: &[u8], 79 | _width: i32, 80 | _height: i32, 81 | ) { 82 | } 83 | } 84 | 85 | struct RenderHandlerWrapper { 86 | _base: cef_render_handler_t, 87 | internal: Arc, 88 | } 89 | unsafe impl WrapperFor for RenderHandlerWrapper {} 90 | impl RenderHandlerWrapper { 91 | fn from_ptr<'a>( 92 | ptr: *mut cef_render_handler_t, 93 | ) -> &'a mut BaseRefCountedExt> { 94 | unsafe { &mut *(ptr as *mut _) } 95 | } 96 | 97 | extern "C" fn get_accessibility_handler( 98 | _client: *mut cef_render_handler_t, 99 | ) -> *mut cef_accessibility_handler_t { 100 | // TODO 101 | null_mut() 102 | } 103 | 104 | extern "C" fn get_root_screen_rect( 105 | client: *mut cef_render_handler_t, 106 | browser: *mut cef_browser_t, 107 | rect: *mut cef_rect_t, 108 | ) -> ::std::os::raw::c_int { 109 | let client = Self::from_ptr(client); 110 | let browser = Browser::from(browser, false); 111 | if let Some(res) = client.internal.get_root_screen_rect(&browser) { 112 | if !rect.is_null() { 113 | unsafe { 114 | (*rect).x = res.x; 115 | (*rect).y = res.y; 116 | (*rect).width = res.width; 117 | (*rect).height = res.height; 118 | } 119 | } 120 | 1 121 | } else { 122 | 0 123 | } 124 | } 125 | extern "C" fn get_view_rect( 126 | client: *mut cef_render_handler_t, 127 | browser: *mut cef_browser_t, 128 | rect: *mut cef_rect_t, 129 | ) { 130 | let client = Self::from_ptr(client); 131 | let browser = Browser::from(browser, false); 132 | let res = client.internal.get_view_rect(&browser); 133 | if !rect.is_null() { 134 | unsafe { 135 | (*rect).x = res.x; 136 | (*rect).y = res.y; 137 | (*rect).width = res.width; 138 | (*rect).height = res.height; 139 | } 140 | } 141 | } 142 | extern "C" fn get_screen_point( 143 | client: *mut cef_render_handler_t, 144 | browser: *mut cef_browser_t, 145 | view_x: ::std::os::raw::c_int, 146 | view_y: ::std::os::raw::c_int, 147 | screen_x: *mut ::std::os::raw::c_int, 148 | screen_y: *mut ::std::os::raw::c_int, 149 | ) -> ::std::os::raw::c_int { 150 | let client = Self::from_ptr(client); 151 | let browser = Browser::from(browser, false); 152 | let view_point = CefPoint { 153 | x: view_x, 154 | y: view_y, 155 | }; 156 | 157 | let screen_point = client.internal.get_screen_point(&browser, view_point); 158 | if let Some(screen_point) = screen_point { 159 | unsafe { 160 | *screen_x = screen_point.x; 161 | *screen_y = screen_point.y; 162 | } 163 | 1 164 | } else { 165 | 0 166 | } 167 | } 168 | 169 | extern "C" fn get_screen_info( 170 | _client: *mut cef_render_handler_t, 171 | _browser: *mut cef_browser_t, 172 | _screen_info: *mut cef_screen_info_t, 173 | ) -> ::std::os::raw::c_int { 174 | // TODO 175 | 0 176 | } 177 | 178 | extern "C" fn on_popup_show( 179 | client: *mut cef_render_handler_t, 180 | browser: *mut cef_browser_t, 181 | show: ::std::os::raw::c_int, 182 | ) { 183 | let client = Self::from_ptr(client); 184 | let browser = Browser::from(browser, false); 185 | client.internal.on_popup_show(&browser, show > 0); 186 | } 187 | 188 | extern "C" fn on_popup_size( 189 | client: *mut cef_render_handler_t, 190 | browser: *mut cef_browser_t, 191 | rect: *const cef_rect_t, 192 | ) { 193 | let client = Self::from_ptr(client); 194 | let browser = Browser::from(browser, false); 195 | let rect = CefRect::from_ptr(rect); 196 | client.internal.on_popup_size(&browser, rect); 197 | } 198 | 199 | extern "C" fn on_paint( 200 | client: *mut cef_render_handler_t, 201 | browser: *mut cef_browser_t, 202 | type_: cef_paint_element_type_t, 203 | dirty_rects_count: usize, 204 | dirty_rects: *const cef_rect_t, 205 | buffer: *const ::std::os::raw::c_void, 206 | width: ::std::os::raw::c_int, 207 | height: ::std::os::raw::c_int, 208 | ) { 209 | let client = Self::from_ptr(client); 210 | let browser = Browser::from(browser, false); 211 | let element_type = unsafe { std::mem::transmute(type_) }; 212 | let dirty_rects = CefRect::from_array(dirty_rects_count, dirty_rects); 213 | let bytes = unsafe { from_raw_parts(buffer as *const u8, (width * height * 4) as usize) }; 214 | 215 | client 216 | .internal 217 | .on_paint(&browser, element_type, &dirty_rects, bytes, width, height); 218 | } 219 | 220 | extern "C" fn on_accelerated_paint( 221 | client: *mut cef_render_handler_t, 222 | browser: *mut cef_browser_t, 223 | type_: cef_paint_element_type_t, 224 | dirty_rects_count: usize, 225 | dirty_rects: *const cef_rect_t, 226 | shared_handle: *mut ::std::os::raw::c_void, 227 | ) { 228 | let client = Self::from_ptr(client); 229 | let browser = Browser::from(browser, false); 230 | let element_type = unsafe { std::mem::transmute(type_) }; 231 | let dirty_rects = CefRect::from_array(dirty_rects_count, dirty_rects); 232 | 233 | client 234 | .internal 235 | .on_accelerated_paint(&browser, element_type, &dirty_rects, shared_handle); 236 | } 237 | 238 | extern "C" fn on_cursor_change( 239 | _client: *mut cef_render_handler_t, 240 | _browser: *mut cef_browser_t, 241 | _cursor: CefCursorInternal, 242 | _type_: cef_cursor_type_t, 243 | _custom_cursor_info: *const cef_cursor_info_t, 244 | ) { 245 | // TODO 246 | } 247 | 248 | extern "C" fn start_dragging( 249 | _client: *mut cef_render_handler_t, 250 | _browser: *mut cef_browser_t, 251 | _drag_data: *mut cef_drag_data_t, 252 | _allowed_ops: cef_drag_operations_mask_t, 253 | _x: ::std::os::raw::c_int, 254 | _y: ::std::os::raw::c_int, 255 | ) -> ::std::os::raw::c_int { 256 | // TODO 257 | 0 258 | } 259 | 260 | extern "C" fn update_drag_cursor( 261 | client: *mut cef_render_handler_t, 262 | browser: *mut cef_browser_t, 263 | operation: cef_drag_operations_mask_t, 264 | ) { 265 | let client = Self::from_ptr(client); 266 | let browser = Browser::from(browser, false); 267 | let operation = DragOperationsMask::from(operation); 268 | 269 | client.internal.update_drag_cursor(&browser, operation); 270 | } 271 | 272 | extern "C" fn on_scroll_offset_changed( 273 | client: *mut cef_render_handler_t, 274 | browser: *mut cef_browser_t, 275 | x: f64, 276 | y: f64, 277 | ) { 278 | let client = Self::from_ptr(client); 279 | let browser = Browser::from(browser, false); 280 | 281 | client.internal.on_scroll_offset_changed(&browser, x, y); 282 | } 283 | 284 | extern "C" fn on_ime_composition_range_changed( 285 | client: *mut cef_render_handler_t, 286 | browser: *mut cef_browser_t, 287 | selected_range: *const cef_range_t, 288 | character_bounds_count: usize, 289 | character_bounds: *const cef_rect_t, 290 | ) { 291 | let client = Self::from_ptr(client); 292 | let browser = Browser::from(browser, false); 293 | let selected_range = CefRange::from_ptr(selected_range); 294 | let character_bounds = CefRect::from_array(character_bounds_count, character_bounds); 295 | 296 | client.internal.on_ime_composition_range_changed( 297 | &browser, 298 | selected_range, 299 | &character_bounds, 300 | ); 301 | } 302 | 303 | extern "C" fn on_text_selection_changed( 304 | client: *mut cef_render_handler_t, 305 | browser: *mut cef_browser_t, 306 | selected_text: *const cef_string_t, 307 | selected_range: *const cef_range_t, 308 | ) { 309 | let client = Self::from_ptr(client); 310 | let browser = Browser::from(browser, false); 311 | let selected_text = CefString::from_cef(selected_text); 312 | let selected_range = CefRange::from_ptr(selected_range); 313 | 314 | client.internal.on_text_selection_changed( 315 | &browser, 316 | selected_text.to_string(), 317 | selected_range, 318 | ); 319 | } 320 | 321 | extern "C" fn on_virtual_keyboard_requested( 322 | client: *mut cef_render_handler_t, 323 | browser: *mut cef_browser_t, 324 | input_mode: cef_text_input_mode_t, 325 | ) { 326 | let client = Self::from_ptr(client); 327 | let browser = Browser::from(browser, false); 328 | let input_mode = unsafe { std::mem::transmute(input_mode) }; 329 | client 330 | .internal 331 | .on_virtual_keyboard_requested(&browser, input_mode); 332 | } 333 | } 334 | impl ToCef for Arc { 335 | fn to_cef(&self) -> *mut cef_render_handler_t { 336 | wrap_ptr(|base| RenderHandlerWrapper { 337 | _base: cef_render_handler_t { 338 | base, 339 | get_accessibility_handler: Some( 340 | RenderHandlerWrapper::::get_accessibility_handler, 341 | ), 342 | get_root_screen_rect: Some(RenderHandlerWrapper::::get_root_screen_rect), 343 | get_view_rect: Some(RenderHandlerWrapper::::get_view_rect), 344 | get_screen_point: Some(RenderHandlerWrapper::::get_screen_point), 345 | get_screen_info: Some(RenderHandlerWrapper::::get_screen_info), 346 | on_popup_show: Some(RenderHandlerWrapper::::on_popup_show), 347 | on_popup_size: Some(RenderHandlerWrapper::::on_popup_size), 348 | on_paint: Some(RenderHandlerWrapper::::on_paint), 349 | on_accelerated_paint: Some(RenderHandlerWrapper::::on_accelerated_paint), 350 | on_cursor_change: Some(RenderHandlerWrapper::::on_cursor_change), 351 | start_dragging: Some(RenderHandlerWrapper::::start_dragging), 352 | update_drag_cursor: Some(RenderHandlerWrapper::::update_drag_cursor), 353 | on_scroll_offset_changed: Some(RenderHandlerWrapper::::on_scroll_offset_changed), 354 | on_ime_composition_range_changed: Some( 355 | RenderHandlerWrapper::::on_ime_composition_range_changed, 356 | ), 357 | on_text_selection_changed: Some( 358 | RenderHandlerWrapper::::on_text_selection_changed, 359 | ), 360 | on_virtual_keyboard_requested: Some( 361 | RenderHandlerWrapper::::on_virtual_keyboard_requested, 362 | ), 363 | }, 364 | internal: self.clone(), 365 | }) 366 | } 367 | } 368 | -------------------------------------------------------------------------------- /cef/src/frame.rs: -------------------------------------------------------------------------------- 1 | use crate::ptr::RefCounterGuard; 2 | use crate::types::string::CefString; 3 | use crate::Browser; 4 | use cef_sys::{ 5 | cef_domvisitor_t, cef_frame_t, cef_process_id_t, cef_process_message_t, cef_request_t, 6 | cef_string_visitor_t, cef_v8context_t, 7 | }; 8 | use std::ptr::null_mut; 9 | 10 | pub struct Frame { 11 | ptr: RefCounterGuard, 12 | } 13 | impl Frame { 14 | pub(crate) fn from(ptr: *mut cef_frame_t, track_ref: bool) -> Self { 15 | unsafe { 16 | Self { 17 | ptr: RefCounterGuard::from(&mut (*ptr).base, ptr, track_ref), 18 | } 19 | } 20 | } 21 | 22 | pub fn is_valid(&self) -> bool { 23 | if let Some(func) = self.ptr.as_ref().is_valid { 24 | unsafe { func(self.ptr.get()) > 0 } 25 | } else { 26 | false 27 | } 28 | } 29 | 30 | pub fn undo(&self) { 31 | if let Some(func) = self.ptr.as_ref().undo { 32 | unsafe { func(self.ptr.get()) } 33 | } 34 | } 35 | 36 | pub fn redo(&self) { 37 | if let Some(func) = self.ptr.as_ref().redo { 38 | unsafe { func(self.ptr.get()) } 39 | } 40 | } 41 | 42 | pub fn cut(&self) { 43 | if let Some(func) = self.ptr.as_ref().cut { 44 | unsafe { func(self.ptr.get()) } 45 | } 46 | } 47 | 48 | pub fn copy(&self) { 49 | if let Some(func) = self.ptr.as_ref().copy { 50 | unsafe { func(self.ptr.get()) } 51 | } 52 | } 53 | 54 | pub fn paste(&self) { 55 | if let Some(func) = self.ptr.as_ref().paste { 56 | unsafe { func(self.ptr.get()) } 57 | } 58 | } 59 | 60 | pub fn del(&self) { 61 | if let Some(func) = self.ptr.as_ref().del { 62 | unsafe { func(self.ptr.get()) } 63 | } 64 | } 65 | 66 | pub fn select_all(&self) { 67 | if let Some(func) = self.ptr.as_ref().select_all { 68 | unsafe { func(self.ptr.get()) } 69 | } 70 | } 71 | 72 | pub fn view_source(&self) { 73 | if let Some(func) = self.ptr.as_ref().view_source { 74 | unsafe { func(self.ptr.get()) } 75 | } 76 | } 77 | 78 | pub fn get_source(&self, _visitor: *mut cef_string_visitor_t) { 79 | // TODO 80 | } 81 | 82 | pub fn get_text(&self, _visitor: *mut cef_string_visitor_t) { 83 | // TODO 84 | } 85 | 86 | pub fn load_request(&self, _request: *mut cef_request_t) { 87 | // TODO 88 | } 89 | 90 | pub fn load_url(&self, url: &str) { 91 | if let Some(func) = self.ptr.as_ref().load_url { 92 | let url = CefString::from_str(url); 93 | unsafe { func(self.ptr.get(), &url.into_cef()) } 94 | } 95 | } 96 | 97 | pub fn load_string(&self, string_val: &str, url: &str) { 98 | if let Some(func) = self.ptr.as_ref().load_string { 99 | let string_val = CefString::from_str(string_val); 100 | let url = CefString::from_str(url); 101 | unsafe { func(self.ptr.get(), &string_val.into_cef(), &url.into_cef()) } 102 | } 103 | } 104 | 105 | pub fn execute_javascript(&self, code: &str, script_url: &str, start_line: i32) { 106 | if let Some(func) = self.ptr.as_ref().execute_java_script { 107 | let code = CefString::from_str(code); 108 | let script_url = CefString::from_str(script_url); 109 | unsafe { 110 | func( 111 | self.ptr.get(), 112 | &code.into_cef(), 113 | &script_url.into_cef(), 114 | start_line, 115 | ) 116 | } 117 | } 118 | } 119 | 120 | pub fn is_main(&self) -> bool { 121 | if let Some(func) = self.ptr.as_ref().is_main { 122 | unsafe { func(self.ptr.get()) > 0 } 123 | } else { 124 | false 125 | } 126 | } 127 | 128 | pub fn is_focused(&self) -> bool { 129 | if let Some(func) = self.ptr.as_ref().is_focused { 130 | unsafe { func(self.ptr.get()) > 0 } 131 | } else { 132 | false 133 | } 134 | } 135 | 136 | pub fn get_name(&self) -> String { 137 | if let Some(func) = self.ptr.as_ref().get_name { 138 | CefString::from_userfree_cef(unsafe { func(self.ptr.get()) }).to_string() 139 | } else { 140 | "".to_string() 141 | } 142 | } 143 | 144 | pub fn get_identifier(&self) -> i64 { 145 | if let Some(func) = self.ptr.as_ref().get_identifier { 146 | unsafe { func(self.ptr.get()) } 147 | } else { 148 | 0 149 | } 150 | } 151 | 152 | pub fn get_parent(&self) -> Option { 153 | if let Some(func) = self.ptr.as_ref().get_parent { 154 | unsafe { Some(Self::from(func(self.ptr.get()), true)) } 155 | } else { 156 | None 157 | } 158 | } 159 | 160 | pub fn get_url(&self) -> String { 161 | if let Some(func) = self.ptr.as_ref().get_url { 162 | unsafe { 163 | let ptr = func(self.ptr.get()); 164 | let res = CefString::from_cef(ptr); 165 | 166 | cef_sys::cef_string_userfree_utf16_free(ptr); 167 | 168 | res.to_string() 169 | } 170 | } else { 171 | "".to_string() 172 | } 173 | } 174 | 175 | pub fn get_browser(&self) -> Option { 176 | if let Some(func) = self.ptr.as_ref().get_browser { 177 | unsafe { Some(Browser::from(func(self.ptr.get()), true)) } 178 | } else { 179 | None 180 | } 181 | } 182 | 183 | pub fn get_v8context(&self) -> *mut cef_v8context_t { 184 | if let Some(func) = self.ptr.as_ref().get_v8context { 185 | // TODO 186 | unsafe { func(self.ptr.get()) } 187 | } else { 188 | null_mut() 189 | } 190 | } 191 | 192 | pub fn visit_dom(&self, _visitor: *mut cef_domvisitor_t) { 193 | // TODO 194 | } 195 | 196 | pub fn send_process_message( 197 | &self, 198 | _target_process: cef_process_id_t, 199 | _message: *mut cef_process_message_t, 200 | ) { 201 | // TODO 202 | } 203 | } 204 | -------------------------------------------------------------------------------- /cef/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![feature(non_exhaustive)] 2 | 3 | extern crate cef_sys; 4 | 5 | mod app; 6 | mod browser; 7 | mod browser_host; 8 | mod client; 9 | mod frame; 10 | mod platform; 11 | mod ptr; 12 | mod settings; 13 | mod thread; 14 | mod types; 15 | mod window; 16 | 17 | use crate::types::string::CefString; 18 | pub use app::*; 19 | pub use browser::*; 20 | pub use browser_host::*; 21 | pub use client::*; 22 | pub use frame::*; 23 | pub use platform::*; 24 | pub use settings::*; 25 | use std::ptr::null_mut; 26 | use std::sync::Arc; 27 | pub use thread::*; 28 | pub use types::*; 29 | pub use window::*; 30 | 31 | pub(crate) trait ToCef { 32 | fn to_cef(&self) -> *mut T; 33 | } 34 | 35 | pub fn execute_process_with_args( 36 | args: CefArgs, 37 | application: &Arc, 38 | // windows_sandbox_info: *mut ::std::os::raw::c_void, 39 | ) -> i32 { 40 | let args = platform::args_to_cef(args); 41 | unsafe { cef_sys::cef_execute_process(&args.cef, application.to_cef(), null_mut()) } 42 | } 43 | pub fn execute_process( 44 | application: &Arc, 45 | // windows_sandbox_info: *mut ::std::os::raw::c_void, 46 | ) -> i32 { 47 | let args = platform::default_args(); 48 | unsafe { cef_sys::cef_execute_process(&args.cef, application.to_cef(), null_mut()) } 49 | } 50 | 51 | pub fn initialize_with_args( 52 | args: CefArgs, 53 | settings: Settings, 54 | application: &Arc, 55 | // application: *mut cef_sys::cef_app_t, 56 | // windows_sandbox_info: *mut ::std::os::raw::c_void, 57 | ) -> ::std::os::raw::c_int { 58 | let args = platform::args_to_cef(args); 59 | unsafe { 60 | cef_sys::cef_initialize( 61 | &args.cef, 62 | &settings.to_cef(), 63 | application.to_cef(), 64 | null_mut(), 65 | ) 66 | } 67 | } 68 | 69 | pub fn initialize( 70 | settings: Settings, 71 | application: &Arc, 72 | // application: *mut cef_sys::cef_app_t, 73 | // windows_sandbox_info: *mut ::std::os::raw::c_void, 74 | ) -> ::std::os::raw::c_int { 75 | let args = platform::default_args(); 76 | unsafe { 77 | cef_sys::cef_initialize( 78 | &args.cef, 79 | &settings.to_cef(), 80 | application.to_cef(), 81 | null_mut(), 82 | ) 83 | } 84 | } 85 | 86 | pub fn create_browser_sync( 87 | info: WindowInfo, 88 | client: &Arc, 89 | url: &str, 90 | settings: BrowserSettings, 91 | ) /*-> Browser */ 92 | { 93 | // TODO - calling this appears to leak a ref somewhere 94 | let res = unsafe { 95 | cef_sys::cef_browser_host_create_browser_sync( 96 | &info.to_cef(), 97 | client.to_cef(), 98 | &CefString::from_str(url).into_cef(), 99 | &settings.to_cef(), 100 | null_mut(), 101 | ) 102 | }; 103 | // Browser::from(res, true) 104 | } 105 | 106 | pub fn shutdown() { 107 | unsafe { cef_sys::cef_shutdown() } 108 | } 109 | 110 | pub fn do_message_loop_work() { 111 | unsafe { cef_sys::cef_do_message_loop_work() } 112 | } 113 | 114 | pub fn run_message_loop() { 115 | unsafe { cef_sys::cef_run_message_loop() } 116 | } 117 | 118 | pub fn quit_message_loop() { 119 | unsafe { cef_sys::cef_quit_message_loop() } 120 | } 121 | 122 | #[cfg(test)] 123 | mod tests { 124 | #[test] 125 | fn it_works() { 126 | assert_eq!(2 + 2, 4); 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /cef/src/platform/linux.rs: -------------------------------------------------------------------------------- 1 | use crate::types::string::CefString; 2 | use crate::WindowInfo; 3 | use cef_sys::cef_window_info_t; 4 | use std::ffi::CString; 5 | use std::os::raw::c_char; 6 | use std::ptr::null_mut; 7 | 8 | pub type CefArgs<'a> = &'a [String]; 9 | 10 | pub(crate) struct CefMainArgsWrapper { 11 | pub cef: cef_sys::_cef_main_args_t, 12 | pub keepalive: Vec, 13 | pub keepalive2: Vec<*mut c_char>, 14 | } 15 | 16 | pub(crate) fn args_to_cef(raw: CefArgs) -> CefMainArgsWrapper { 17 | // TODO - won't this cause the types to be freed before the pointers? 18 | let args = raw 19 | .iter() 20 | .map(|x| CString::new(x.as_str()).unwrap()) 21 | .collect::>(); 22 | let mut res = CefMainArgsWrapper { 23 | cef: cef_sys::_cef_main_args_t { 24 | argc: 0, 25 | argv: null_mut(), 26 | }, 27 | keepalive: args, 28 | keepalive2: Vec::new(), 29 | }; 30 | 31 | res.keepalive2 = res.keepalive.iter().map(|x| x.as_ptr() as *mut _).collect(); 32 | res.cef.argc = res.keepalive2.len() as i32; 33 | res.cef.argv = res.keepalive2.as_mut_ptr(); 34 | 35 | res 36 | } 37 | 38 | pub(crate) fn default_args() -> CefMainArgsWrapper { 39 | args_to_cef(&std::env::args().collect::>()) 40 | } 41 | 42 | pub(crate) type CefCursorInternal = ::std::os::raw::c_ulong; 43 | pub(crate) type CefWindowHandle = ::std::os::raw::c_ulong; 44 | 45 | impl<'a> WindowInfo<'a> { 46 | pub(crate) fn to_cef(&self) -> cef_window_info_t { 47 | cef_window_info_t { 48 | window_name: CefString::convert_str_to_cef(self.window_name), 49 | x: self.x, 50 | y: self.y, 51 | width: self.width, 52 | height: self.height, 53 | parent_window: self.parent_window, 54 | windowless_rendering_enabled: self.windowless_rendering_enabled as i32, 55 | shared_texture_enabled: self.shared_texture_enabled as i32, 56 | external_begin_frame_enabled: self.external_begin_frame_enabled as i32, 57 | window: self.window, 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /cef/src/platform/mod.rs: -------------------------------------------------------------------------------- 1 | #[cfg(not(target_os = "windows"))] 2 | mod linux; 3 | #[cfg(not(target_os = "windows"))] 4 | pub use linux::*; 5 | 6 | #[cfg(target_os = "windows")] 7 | mod windows; 8 | #[cfg(target_os = "windows")] 9 | pub use windows::*; 10 | -------------------------------------------------------------------------------- /cef/src/platform/windows.rs: -------------------------------------------------------------------------------- 1 | use crate::types::string::CefString; 2 | use crate::WindowInfo; 3 | use cef_sys::cef_window_info_t; 4 | use std::ptr::null_mut; 5 | 6 | pub type CefArgs<'a> = cef_sys::HINSTANCE; 7 | 8 | pub(crate) struct CefMainArgsWrapper { 9 | pub cef: cef_sys::_cef_main_args_t, 10 | } 11 | 12 | pub(crate) fn args_to_cef(raw: CefArgs) -> CefMainArgsWrapper { 13 | CefMainArgsWrapper { 14 | cef: cef_sys::_cef_main_args_t { instance: raw }, 15 | } 16 | } 17 | 18 | pub(crate) fn default_args() -> CefMainArgsWrapper { 19 | args_to_cef(null_mut()) 20 | } 21 | 22 | pub(crate) type CefCursorInternal = cef_sys::HCURSOR; 23 | 24 | impl<'a> WindowInfo<'a> { 25 | pub(crate) fn to_cef(&self) -> cef_window_info_t { 26 | cef_window_info_t { 27 | window_name: CefString::convert_str_to_cef(self.window_name), 28 | x: self.x as i32, 29 | y: self.y as i32, 30 | width: self.width as i32, 31 | height: self.height as i32, 32 | parent_window: self.parent_window, 33 | windowless_rendering_enabled: self.windowless_rendering_enabled as i32, 34 | shared_texture_enabled: self.shared_texture_enabled as i32, 35 | external_begin_frame_enabled: self.external_begin_frame_enabled as i32, 36 | window: self.window, 37 | 38 | // Windows only values 39 | ex_style: 0, 40 | style: 0, 41 | menu: null_mut(), 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /cef/src/ptr.rs: -------------------------------------------------------------------------------- 1 | use cef_sys::{_cef_base_ref_counted_t, cef_base_ref_counted_t}; 2 | use std::marker::PhantomData; 3 | use std::ops::{Deref, DerefMut}; 4 | use std::sync::atomic::{AtomicUsize, Ordering}; 5 | 6 | pub(crate) unsafe trait WrapperFor {} 7 | 8 | unsafe impl Send for RefCounterGuard {} 9 | unsafe impl Sync for RefCounterGuard {} 10 | pub(crate) struct RefCounterGuard { 11 | // TODO - test this type 12 | base: *mut cef_base_ref_counted_t, 13 | val: *mut T, 14 | track_ref: bool, 15 | } 16 | impl Deref for RefCounterGuard { 17 | type Target = *mut T; 18 | 19 | fn deref(&self) -> &Self::Target { 20 | &self.val 21 | } 22 | } 23 | impl DerefMut for RefCounterGuard { 24 | fn deref_mut(&mut self) -> &mut Self::Target { 25 | &mut self.val 26 | } 27 | } 28 | impl Drop for RefCounterGuard { 29 | fn drop(&mut self) { 30 | if self.track_ref && !self.base.is_null() { 31 | unsafe { 32 | let base = &mut *self.base; 33 | if let Some(release) = base.release { 34 | release(base); 35 | } 36 | } 37 | } 38 | } 39 | } 40 | impl Clone for RefCounterGuard { 41 | fn clone(&self) -> Self { 42 | let res = Self { 43 | base: self.base, 44 | val: self.val, 45 | track_ref: true, 46 | }; 47 | unsafe { res.add_ref() }; 48 | res 49 | } 50 | } 51 | impl RefCounterGuard { 52 | pub(crate) fn from( 53 | base: *mut cef_base_ref_counted_t, 54 | val: *mut T, 55 | track_ref: bool, 56 | ) -> RefCounterGuard { 57 | // if track_ref && !base.is_null() { 58 | // unsafe { 59 | // let base = &mut *base; 60 | // // Let CEF know we are passing it around 61 | // if let Some(add_ref) = base.add_ref { 62 | // add_ref(base); 63 | // } 64 | // } 65 | // } 66 | 67 | RefCounterGuard { 68 | base, 69 | val, 70 | track_ref, 71 | } 72 | } 73 | pub fn get(&self) -> *mut T { 74 | self.val 75 | } 76 | pub fn as_ref(&self) -> &mut T { 77 | unsafe { &mut *self.val } 78 | } 79 | 80 | pub unsafe fn add_ref(&self) { 81 | if !self.base.is_null() { 82 | let base = &mut *self.base; 83 | if let Some(add_ref) = base.add_ref { 84 | add_ref(base); 85 | } 86 | } 87 | } 88 | } 89 | 90 | // This relies on the c storage structure to allow casting a *_cef_base_ref_counted_t into a pointer of this type which starts with _cef_base_ref_counted_t 91 | #[repr(C)] 92 | pub(crate) struct BaseRefCountedExt { 93 | v: TWrapper, 94 | count: AtomicUsize, 95 | phantom: PhantomData, 96 | } 97 | impl Deref for BaseRefCountedExt { 98 | type Target = TWrapper; 99 | 100 | fn deref(&self) -> &Self::Target { 101 | &self.v 102 | } 103 | } 104 | impl DerefMut for BaseRefCountedExt { 105 | fn deref_mut(&mut self) -> &mut Self::Target { 106 | &mut self.v 107 | } 108 | } 109 | impl> BaseRefCountedExt { 110 | fn wrap_ptr(wrapper: F) -> *mut TCef 111 | where 112 | F: FnOnce(cef_base_ref_counted_t) -> TWrapper, 113 | { 114 | let base = BaseRefCountedExt:: { 115 | v: wrapper(_cef_base_ref_counted_t { 116 | size: std::mem::size_of::(), 117 | add_ref: Some(Self::add_ref), 118 | release: Some(Self::release), 119 | has_one_ref: Some(Self::has_one_ref), 120 | has_at_least_one_ref: Some(Self::has_at_least_one_ref), 121 | }), 122 | count: AtomicUsize::new(1), 123 | phantom: PhantomData, 124 | }; 125 | Box::into_raw(Box::new(base)) as *mut TCef 126 | } 127 | 128 | fn from_ptr<'a>(ptr: *mut cef_base_ref_counted_t) -> &'a mut BaseRefCountedExt { 129 | unsafe { &mut *(ptr as *mut _) } 130 | } 131 | extern "C" fn add_ref(ptr: *mut cef_base_ref_counted_t) { 132 | let base = Self::from_ptr(ptr); 133 | base.count.fetch_add(1, Ordering::Relaxed); 134 | } 135 | extern "C" fn release(ptr: *mut cef_base_ref_counted_t) -> i32 { 136 | let base = Self::from_ptr(ptr); 137 | let old_count = base.count.fetch_sub(1, Ordering::Release); 138 | if old_count == 1 { 139 | // reclaim and release 140 | unsafe { Box::from_raw(base) }; 141 | 142 | 1 // true 143 | } else { 144 | 0 // false 145 | } 146 | } 147 | extern "C" fn has_one_ref(ptr: *mut cef_base_ref_counted_t) -> i32 { 148 | let base = Self::from_ptr(ptr); 149 | if base.count.load(Ordering::SeqCst) == 1 { 150 | 1 // true 151 | } else { 152 | 0 // false 153 | } 154 | } 155 | extern "C" fn has_at_least_one_ref(ptr: *mut cef_base_ref_counted_t) -> i32 { 156 | let base = Self::from_ptr(ptr); 157 | if base.count.load(Ordering::SeqCst) >= 1 { 158 | 1 // true 159 | } else { 160 | 0 // false 161 | } 162 | } 163 | } 164 | 165 | pub(crate) fn wrap_ptr(wrapper: F) -> *mut TCef 166 | where 167 | F: FnOnce(cef_base_ref_counted_t) -> TWrapper, 168 | TWrapper: WrapperFor, 169 | { 170 | BaseRefCountedExt::wrap_ptr(wrapper) 171 | } 172 | -------------------------------------------------------------------------------- /cef/src/settings.rs: -------------------------------------------------------------------------------- 1 | use crate::string::CefString; 2 | use crate::LogSeverity; 3 | use cef_sys::cef_settings_t; 4 | use std::os::raw::c_int; 5 | 6 | #[derive(Debug, Copy, Clone)] 7 | pub struct Settings<'a> { 8 | pub no_sandbox: bool, 9 | pub browser_subprocess_path: Option<&'a str>, 10 | pub framework_dir_path: Option<&'a str>, 11 | pub multi_threaded_message_loop: bool, 12 | pub external_message_pump: bool, 13 | pub windowless_rendering_enabled: bool, 14 | pub command_line_args_disabled: bool, 15 | pub cache_path: Option<&'a str>, 16 | pub user_data_path: Option<&'a str>, 17 | pub persist_session_cookies: bool, 18 | pub persist_user_preferences: bool, 19 | pub user_agent: Option<&'a str>, 20 | pub product_version: Option<&'a str>, 21 | pub locale: Option<&'a str>, 22 | pub log_file: Option<&'a str>, 23 | pub log_severity: LogSeverity, 24 | pub javascript_flags: Option<&'a str>, 25 | pub resources_dir_path: Option<&'a str>, 26 | pub locales_dir_path: Option<&'a str>, 27 | pub pack_loading_disabled: bool, 28 | pub remote_debugging_port: Option, 29 | pub uncaught_exception_stack_size: Option, 30 | pub ignore_certificate_errors: bool, 31 | pub enable_net_security_expiration: bool, 32 | pub background_color: u32, 33 | pub accept_language_list: Option<&'a str>, 34 | } 35 | impl<'a> Default for Settings<'a> { 36 | fn default() -> Settings<'a> { 37 | Settings { 38 | no_sandbox: true, 39 | browser_subprocess_path: None, 40 | framework_dir_path: None, 41 | multi_threaded_message_loop: false, 42 | external_message_pump: false, 43 | windowless_rendering_enabled: false, 44 | command_line_args_disabled: false, 45 | cache_path: None, 46 | user_data_path: None, 47 | persist_session_cookies: false, 48 | persist_user_preferences: false, 49 | user_agent: None, 50 | product_version: None, 51 | locale: None, 52 | log_file: None, 53 | log_severity: LogSeverity::LOGSEVERITY_DEFAULT, 54 | javascript_flags: None, 55 | resources_dir_path: None, 56 | locales_dir_path: None, 57 | pack_loading_disabled: false, 58 | remote_debugging_port: None, 59 | uncaught_exception_stack_size: None, 60 | ignore_certificate_errors: false, 61 | enable_net_security_expiration: false, 62 | background_color: 0x0000_0000, 63 | accept_language_list: None, 64 | } 65 | } 66 | } 67 | impl<'a> Settings<'a> { 68 | pub(crate) fn to_cef(&self) -> cef_settings_t { 69 | cef_settings_t { 70 | size: std::mem::size_of::(), 71 | no_sandbox: self.no_sandbox as c_int, 72 | browser_subprocess_path: CefString::convert_str_to_cef(self.browser_subprocess_path), 73 | framework_dir_path: CefString::convert_str_to_cef(self.framework_dir_path), 74 | multi_threaded_message_loop: self.multi_threaded_message_loop as c_int, 75 | external_message_pump: self.external_message_pump as c_int, 76 | windowless_rendering_enabled: self.windowless_rendering_enabled as c_int, 77 | command_line_args_disabled: self.command_line_args_disabled as c_int, 78 | cache_path: CefString::convert_str_to_cef(self.cache_path), 79 | user_data_path: CefString::convert_str_to_cef(self.user_data_path), 80 | persist_session_cookies: self.persist_session_cookies as c_int, 81 | persist_user_preferences: self.persist_user_preferences as c_int, 82 | user_agent: CefString::convert_str_to_cef(self.user_agent), 83 | product_version: CefString::convert_str_to_cef(self.product_version), 84 | locale: CefString::convert_str_to_cef(self.locale), 85 | log_file: CefString::convert_str_to_cef(self.log_file), 86 | log_severity: self.log_severity, 87 | javascript_flags: CefString::convert_str_to_cef(self.javascript_flags), 88 | resources_dir_path: CefString::convert_str_to_cef(self.resources_dir_path), 89 | locales_dir_path: CefString::convert_str_to_cef(self.locales_dir_path), 90 | pack_loading_disabled: self.pack_loading_disabled as c_int, 91 | remote_debugging_port: self.remote_debugging_port.unwrap_or(0), 92 | uncaught_exception_stack_size: self.uncaught_exception_stack_size.unwrap_or(0), 93 | ignore_certificate_errors: self.ignore_certificate_errors as c_int, 94 | enable_net_security_expiration: self.enable_net_security_expiration as c_int, 95 | background_color: self.background_color, 96 | accept_language_list: CefString::convert_str_to_cef(self.accept_language_list), 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /cef/src/thread.rs: -------------------------------------------------------------------------------- 1 | use crate::ptr::{wrap_ptr, BaseRefCountedExt, WrapperFor}; 2 | use crate::ThreadId; 3 | use cef_sys::{cef_currently_on, cef_post_task, cef_task_t}; 4 | 5 | pub fn currently_on(id: ThreadId) -> bool { 6 | unsafe { cef_currently_on(id) > 0 } 7 | } 8 | 9 | pub fn post_task ()>(id: ThreadId, func: F) -> Result<(), bool> { 10 | if currently_on(id) { 11 | // Execute it now 12 | func(); 13 | return Ok(()); 14 | } 15 | 16 | let task = wrap_ptr(move |base| TaskWrapper { 17 | _base: cef_task_t { 18 | base, 19 | execute: Some(TaskWrapper::::execute), 20 | }, 21 | func: Some(func), 22 | }); 23 | 24 | let ok = unsafe { cef_post_task(id, task) }; 25 | if ok > 0 { 26 | Ok(()) 27 | } else { 28 | Err(false) 29 | } 30 | } 31 | 32 | pub struct TaskWrapper ()> { 33 | _base: cef_task_t, 34 | func: Option, 35 | } 36 | unsafe impl ()> WrapperFor for TaskWrapper {} 37 | impl ()> TaskWrapper { 38 | fn from_ptr<'a>(ptr: *mut cef_task_t) -> &'a mut BaseRefCountedExt> { 39 | unsafe { &mut *(ptr as *mut _) } 40 | } 41 | 42 | extern "C" fn execute(task: *mut cef_task_t) { 43 | let task = Self::from_ptr(task); 44 | 45 | if let Some(func) = task.func.take() { 46 | (func)(); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /cef/src/types/mod.rs: -------------------------------------------------------------------------------- 1 | mod point; 2 | mod range; 3 | mod rect; 4 | mod size; 5 | pub mod string; 6 | 7 | pub use point::*; 8 | pub use range::*; 9 | pub use rect::*; 10 | pub use size::*; 11 | 12 | pub type LogSeverity = cef_sys::cef_log_severity_t; 13 | pub type PaintElementType = cef_sys::cef_paint_element_type_t; 14 | pub type TextInputMode = cef_sys::cef_text_input_mode_t; 15 | pub type DragOperationsMask = cef_sys::cef_drag_operations_mask_t; 16 | pub type ThreadId = cef_sys::cef_thread_id_t; 17 | -------------------------------------------------------------------------------- /cef/src/types/point.rs: -------------------------------------------------------------------------------- 1 | use cef_sys::cef_point_t; 2 | 3 | #[derive(Clone, Debug)] 4 | pub struct CefPoint { 5 | pub x: i32, 6 | pub y: i32, 7 | } 8 | impl CefPoint { 9 | pub(crate) fn from_ptr(raw: *const cef_point_t) -> Self { 10 | Self::from(unsafe { &*raw }) 11 | } 12 | pub(crate) fn from(raw: &cef_point_t) -> Self { 13 | Self { x: raw.x, y: raw.y } 14 | } 15 | } 16 | impl Default for CefPoint { 17 | fn default() -> Self { 18 | Self { x: 0, y: 0 } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /cef/src/types/range.rs: -------------------------------------------------------------------------------- 1 | use cef_sys::cef_range_t; 2 | 3 | #[derive(Clone, Debug)] 4 | pub struct CefRange { 5 | pub from: i32, 6 | pub to: i32, 7 | } 8 | impl CefRange { 9 | pub(crate) fn from_ptr(raw: *const cef_range_t) -> Self { 10 | Self::from(unsafe { &*raw }) 11 | } 12 | pub(crate) fn from(raw: &cef_range_t) -> Self { 13 | Self { 14 | from: raw.from, 15 | to: raw.to, 16 | } 17 | } 18 | } 19 | impl Default for CefRange { 20 | fn default() -> Self { 21 | Self { from: 0, to: 0 } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /cef/src/types/rect.rs: -------------------------------------------------------------------------------- 1 | use cef_sys::cef_rect_t; 2 | use std::slice::from_raw_parts; 3 | 4 | #[derive(Clone, Debug)] 5 | pub struct CefRect { 6 | pub x: i32, 7 | pub y: i32, 8 | pub width: i32, 9 | pub height: i32, 10 | } 11 | impl CefRect { 12 | pub(crate) fn from_ptr(raw: *const cef_rect_t) -> Self { 13 | Self::from(unsafe { &*raw }) 14 | } 15 | pub(crate) fn from(raw: &cef_rect_t) -> Self { 16 | CefRect { 17 | x: raw.x, 18 | y: raw.y, 19 | width: raw.width, 20 | height: raw.height, 21 | } 22 | } 23 | pub(crate) fn from_array(count: usize, rects: *const cef_rect_t) -> Vec { 24 | let raw_rects = unsafe { from_raw_parts(rects, count) }; 25 | raw_rects.iter().map(|r| Self::from(r)).collect() 26 | } 27 | } 28 | impl Default for CefRect { 29 | fn default() -> Self { 30 | Self { 31 | x: 0, 32 | y: 0, 33 | width: 0, 34 | height: 0, 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /cef/src/types/size.rs: -------------------------------------------------------------------------------- 1 | use cef_sys::cef_size_t; 2 | 3 | #[derive(Clone, Debug)] 4 | pub struct CefSize { 5 | pub width: i32, 6 | pub height: i32, 7 | } 8 | impl CefSize { 9 | pub(crate) fn from_ptr(raw: *const cef_size_t) -> Self { 10 | Self::from(unsafe { &*raw }) 11 | } 12 | pub(crate) fn from(raw: &cef_size_t) -> Self { 13 | CefSize { 14 | width: raw.width, 15 | height: raw.height, 16 | } 17 | } 18 | } 19 | impl Default for CefSize { 20 | fn default() -> Self { 21 | Self { 22 | width: 0, 23 | height: 0, 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /cef/src/types/string.rs: -------------------------------------------------------------------------------- 1 | use cef_sys::{cef_string_list_t, cef_string_map_t, cef_string_utf16_t}; 2 | use std::collections::HashMap; 3 | use std::ptr::null_mut; 4 | use widestring::U16CString; 5 | 6 | pub(crate) type CefString = CefStringUTF16; 7 | pub(crate) struct CefStringUTF16 { 8 | str: U16CString, 9 | } 10 | impl CefStringUTF16 { 11 | pub fn from_str(s: &str) -> Self { 12 | Self { 13 | str: U16CString::from_str(s).unwrap(), // TODO error safety 14 | } 15 | } 16 | pub fn from_cef(ptr: *const cef_string_utf16_t) -> CefStringUTF16 { 17 | if ptr.is_null() { 18 | CefStringUTF16 { 19 | str: U16CString::from_str("").unwrap(), 20 | } 21 | } else { 22 | // It's a pointer, so CEF retains ownership and will call the dtor 23 | 24 | unsafe { 25 | CefStringUTF16 { 26 | str: U16CString::from_ptr((*ptr).str, (*ptr).length).unwrap(), // TODO error safety 27 | } 28 | } 29 | } 30 | } 31 | pub fn from_userfree_cef(ptr: *mut cef_string_utf16_t) -> CefStringUTF16 { 32 | let res = Self::from_cef(ptr); 33 | unsafe { 34 | cef_sys::cef_string_userfree_utf16_free(ptr); 35 | } 36 | res 37 | } 38 | pub fn into_cef(self) -> cef_string_utf16_t { 39 | extern "C" fn free_str(ptr: *mut u16) { 40 | if ptr.is_null() { 41 | return; 42 | } 43 | // TODO - what about the cef_string_utf16_t wrapper? 44 | unsafe { 45 | // Restore and drop 46 | U16CString::from_raw(ptr); 47 | } 48 | } 49 | 50 | cef_string_utf16_t { 51 | length: self.str.len(), 52 | str: self.str.into_raw(), 53 | dtor: Some(free_str), 54 | } 55 | } 56 | pub fn convert_str_to_cef(s: Option<&str>) -> cef_string_utf16_t { 57 | s.map(|x| CefString::from_str(x).into_cef()) 58 | .unwrap_or_else(|| unsafe { std::mem::zeroed() }) 59 | } 60 | pub unsafe fn parse_string_list(ptr: cef_string_list_t) -> Vec { 61 | let count = cef_sys::cef_string_list_size(ptr); 62 | let mut res = Vec::with_capacity(count); 63 | for i in 0..count { 64 | let value = null_mut(); 65 | if cef_sys::cef_string_list_value(ptr, i, value) > 0 { 66 | res.push(CefString::from_cef(value).to_string()); 67 | } 68 | } 69 | res 70 | } 71 | pub unsafe fn parse_string_map(ptr: cef_string_map_t) -> HashMap { 72 | let count = cef_sys::cef_string_map_size(ptr); 73 | let mut res = HashMap::with_capacity(count); 74 | for i in 0..count { 75 | let key = null_mut(); 76 | let value = null_mut(); 77 | cef_sys::cef_string_map_key(ptr, i, key); 78 | cef_sys::cef_string_map_value(ptr, i, value); 79 | 80 | res.insert( 81 | CefString::from_cef(key).to_string(), 82 | CefString::from_cef(value).to_string(), 83 | ); 84 | } 85 | res 86 | } 87 | } 88 | impl ToString for CefStringUTF16 { 89 | fn to_string(&self) -> String { 90 | self.str.to_string_lossy() 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /cef/src/window.rs: -------------------------------------------------------------------------------- 1 | use crate::string::CefString; 2 | use cef_sys::{cef_browser_settings_t, cef_state_t}; 3 | 4 | #[cfg(target_os = "windows")] 5 | mod platform { 6 | use std::ptr::null_mut; 7 | 8 | pub type WindowHandle = cef_sys::HWND; 9 | 10 | pub(crate) fn window_handle_default() -> WindowHandle { 11 | null_mut() 12 | } 13 | } 14 | 15 | #[cfg(not(target_os = "windows"))] 16 | mod platform { 17 | pub type WindowHandle = u64; 18 | 19 | pub(crate) fn window_handle_default() -> WindowHandle { 20 | 0 21 | } 22 | } 23 | pub use platform::*; 24 | 25 | #[derive(Debug, Copy, Clone)] 26 | pub struct WindowInfo<'a> { 27 | pub window_name: Option<&'a str>, 28 | pub x: u32, 29 | pub y: u32, 30 | pub width: u32, 31 | pub height: u32, 32 | 33 | pub windowless_rendering_enabled: bool, 34 | pub shared_texture_enabled: bool, 35 | pub external_begin_frame_enabled: bool, 36 | 37 | pub parent_window: WindowHandle, 38 | pub window: WindowHandle, 39 | } 40 | impl<'a> Default for WindowInfo<'a> { 41 | fn default() -> WindowInfo<'a> { 42 | WindowInfo { 43 | window_name: None, 44 | x: 0, 45 | y: 0, 46 | width: 640, 47 | height: 480, 48 | 49 | windowless_rendering_enabled: false, 50 | shared_texture_enabled: false, 51 | external_begin_frame_enabled: false, 52 | 53 | parent_window: window_handle_default(), 54 | window: window_handle_default(), 55 | } 56 | } 57 | } 58 | 59 | fn optional_bool_to_cef_state(val: Option) -> cef_state_t { 60 | match val { 61 | None => cef_sys::cef_state_t::STATE_DEFAULT, 62 | Some(false) => cef_sys::cef_state_t::STATE_DISABLED, 63 | Some(true) => cef_sys::cef_state_t::STATE_ENABLED, 64 | } 65 | } 66 | 67 | #[derive(Debug, Copy, Clone)] 68 | pub struct BrowserSettings<'a> { 69 | pub windowless_frame_rate: i32, 70 | pub standard_font_family: Option<&'a str>, 71 | pub fixed_font_family: Option<&'a str>, 72 | pub serif_font_family: Option<&'a str>, 73 | pub sans_serif_font_family: Option<&'a str>, 74 | pub cursive_font_family: Option<&'a str>, 75 | pub fantasy_font_family: Option<&'a str>, 76 | pub default_font_size: i32, 77 | pub default_fixed_font_size: i32, 78 | pub minimum_font_size: i32, 79 | pub minimum_logical_font_size: i32, 80 | pub default_encoding: Option<&'a str>, 81 | pub remote_fonts: Option, 82 | pub javascript: Option, 83 | pub javascript_close_windows: Option, 84 | pub javascript_access_clipboard: Option, 85 | pub javascript_dom_paste: Option, 86 | pub plugins: Option, 87 | pub universal_access_from_file_urls: Option, 88 | pub file_access_from_file_urls: Option, 89 | pub web_security: Option, 90 | pub image_loading: Option, 91 | pub image_shrink_standalone_to_fit: Option, 92 | pub text_area_resize: Option, 93 | pub tab_to_links: Option, 94 | pub local_storage: Option, 95 | pub databases: Option, 96 | pub application_cache: Option, 97 | pub webgl: Option, 98 | pub background_color: u32, 99 | pub accept_language_list: Option<&'a str>, 100 | } 101 | impl<'a> Default for BrowserSettings<'a> { 102 | fn default() -> BrowserSettings<'a> { 103 | BrowserSettings { 104 | windowless_frame_rate: 0, 105 | standard_font_family: None, 106 | fixed_font_family: None, 107 | serif_font_family: None, 108 | sans_serif_font_family: None, 109 | cursive_font_family: None, 110 | fantasy_font_family: None, 111 | default_font_size: 0, 112 | default_fixed_font_size: 0, 113 | minimum_font_size: 0, 114 | minimum_logical_font_size: 0, 115 | default_encoding: None, 116 | remote_fonts: None, 117 | javascript: None, 118 | javascript_close_windows: None, 119 | javascript_access_clipboard: None, 120 | javascript_dom_paste: None, 121 | plugins: None, 122 | universal_access_from_file_urls: None, 123 | file_access_from_file_urls: None, 124 | web_security: None, 125 | image_loading: None, 126 | image_shrink_standalone_to_fit: None, 127 | text_area_resize: None, 128 | tab_to_links: None, 129 | local_storage: None, 130 | databases: None, 131 | application_cache: None, 132 | webgl: None, 133 | background_color: 0x0000_0000, 134 | accept_language_list: None, 135 | } 136 | } 137 | } 138 | impl<'a> BrowserSettings<'a> { 139 | pub(crate) fn to_cef(&self) -> cef_browser_settings_t { 140 | cef_browser_settings_t { 141 | size: std::mem::size_of::(), 142 | windowless_frame_rate: self.windowless_frame_rate, 143 | standard_font_family: CefString::convert_str_to_cef(self.standard_font_family), 144 | fixed_font_family: CefString::convert_str_to_cef(self.fixed_font_family), 145 | serif_font_family: CefString::convert_str_to_cef(self.serif_font_family), 146 | sans_serif_font_family: CefString::convert_str_to_cef(self.sans_serif_font_family), 147 | cursive_font_family: CefString::convert_str_to_cef(self.cursive_font_family), 148 | fantasy_font_family: CefString::convert_str_to_cef(self.fantasy_font_family), 149 | default_font_size: self.default_font_size, 150 | default_fixed_font_size: self.default_fixed_font_size, 151 | minimum_font_size: self.minimum_font_size, 152 | minimum_logical_font_size: self.minimum_logical_font_size, 153 | default_encoding: CefString::convert_str_to_cef(self.default_encoding), 154 | remote_fonts: optional_bool_to_cef_state(self.remote_fonts), 155 | javascript: optional_bool_to_cef_state(self.javascript), 156 | javascript_close_windows: optional_bool_to_cef_state(self.javascript_close_windows), 157 | javascript_access_clipboard: optional_bool_to_cef_state( 158 | self.javascript_access_clipboard, 159 | ), 160 | javascript_dom_paste: optional_bool_to_cef_state(self.javascript_dom_paste), 161 | plugins: optional_bool_to_cef_state(self.plugins), 162 | universal_access_from_file_urls: optional_bool_to_cef_state( 163 | self.universal_access_from_file_urls, 164 | ), 165 | file_access_from_file_urls: optional_bool_to_cef_state(self.file_access_from_file_urls), 166 | web_security: optional_bool_to_cef_state(self.web_security), 167 | image_loading: optional_bool_to_cef_state(self.image_loading), 168 | image_shrink_standalone_to_fit: optional_bool_to_cef_state( 169 | self.image_shrink_standalone_to_fit, 170 | ), 171 | text_area_resize: optional_bool_to_cef_state(self.text_area_resize), 172 | tab_to_links: optional_bool_to_cef_state(self.tab_to_links), 173 | local_storage: optional_bool_to_cef_state(self.local_storage), 174 | databases: optional_bool_to_cef_state(self.databases), 175 | application_cache: optional_bool_to_cef_state(self.application_cache), 176 | webgl: optional_bool_to_cef_state(self.webgl), 177 | background_color: self.background_color, 178 | accept_language_list: CefString::convert_str_to_cef(self.accept_language_list), 179 | } 180 | } 181 | } 182 | --------------------------------------------------------------------------------