-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
[rust] Publish Selenium Manager as native shared library #15368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
nvborisenko
wants to merge
9
commits into
SeleniumHQ:trunk
Choose a base branch
from
nvborisenko:rust-shared-library
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4c944e3
Build shared library
nvborisenko 69e5405
Define cargo targets
nvborisenko 775ab44
Added example of Log Callback
nvborisenko 15559d0
Example how to free fn result
nvborisenko eb287c1
Verify memory leakage for log events
nvborisenko 1205f52
Handle panic
nvborisenko 5c06ba7
Intentional panic
nvborisenko 6d4be86
Move extern function to ffi.rs
nvborisenko 0bc4ab4
Simplify exporting of ffi module
nvborisenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use std::{ffi::{c_char, c_int, CStr, CString}, ptr::null_mut}; | ||
|
||
// this is callback function to be called each time when rust wants to send log data | ||
type LogCallback = extern "C" fn(level: c_int, message: *const std::os::raw::c_char); | ||
|
||
#[repr(C)] | ||
pub struct WebDriverPathResult { | ||
success: bool, | ||
driver_path: *mut c_char, | ||
error: *mut c_char, | ||
} | ||
|
||
// this is just an example how to expose function for external usage | ||
#[no_mangle] | ||
pub extern "C" fn get_dummy_webdriver_path(driver_name: *const c_char, log: LogCallback) -> WebDriverPathResult { | ||
let result = std::panic::catch_unwind(|| { | ||
|
||
for i in 1..6 { | ||
let message = CString::new("Hello, I am logging message").unwrap(); | ||
//let message = CString::new(String::from("A").repeat(10_000_000)).unwrap(); | ||
log(i, message.as_ptr()); | ||
} | ||
|
||
//panic!("Intentional panic for testing"); | ||
|
||
let driver = unsafe { CStr::from_ptr(driver_name).to_str().unwrap() }; | ||
|
||
return CString::new("This is dummy driver path for ".to_owned() + driver).unwrap().into_raw(); | ||
//return CString::new(String::from("A").repeat(10_000_000)).unwrap().into_raw(); | ||
}); | ||
|
||
match result { | ||
Ok(driver_path) => WebDriverPathResult { | ||
success: true, | ||
driver_path, | ||
error: null_mut(), | ||
}, | ||
Err(panic) => WebDriverPathResult { | ||
success: false, | ||
driver_path: null_mut(), | ||
error: CString::new(extract_panic_message(panic)).unwrap().into_raw(), | ||
} | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn free_webdriver_path_result(result: *mut WebDriverPathResult) { | ||
if result.is_null() { | ||
return; | ||
} | ||
unsafe { | ||
let ffi_result = &mut *result; | ||
if !ffi_result.driver_path.is_null() { | ||
// Reconstruct CString to drop it and free memory | ||
let _ = CString::from_raw(ffi_result.driver_path); | ||
} | ||
if !ffi_result.error.is_null() { | ||
// Reconstruct CString to drop it and free memory | ||
let _ = CString::from_raw(ffi_result.error); | ||
} | ||
} | ||
} | ||
|
||
/// Extract panic message from `Box<dyn Any>` | ||
fn extract_panic_message(panic: Box<dyn std::any::Any + Send>) -> String { | ||
// Try to downcast to common panic types | ||
if let Some(s) = panic.downcast_ref::<String>() { | ||
s.clone() | ||
} else if let Some(s) = panic.downcast_ref::<&str>() { | ||
s.to_string() | ||
} else { | ||
"Unknown panic (non-string payload)".to_string() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bonigarcia can you help me here? We want to handle all panics in this block and propagate it in returning result. Seems I do it incorrectly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I don't have bandwidth for the following weeks. In any case, this deserves to be discussed in the following Selenium Dev Summit in Valencia (24-25 March).