Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
ParityShell::open Return result (#8377)
Browse files Browse the repository at this point in the history
* start

* add error handling for winapi

* fix typo

* fix warnings and windows errors

* formatting

* Address review comments
  • Loading branch information
niklasad1 authored and 5chdn committed Apr 21, 2018
1 parent c983efe commit b0cc44a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
2 changes: 1 addition & 1 deletion parity/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ fn take_spec_name_override() -> Option<String> {
#[cfg(windows)]
fn global_cleanup() {
// We need to cleanup all sockets before spawning another Parity process. This makes shure everything is cleaned up.
// The loop is required because of internal refernce counter for winsock dll. We don't know how many crates we use do
// The loop is required because of internal reference counter for winsock dll. We don't know how many crates we use do
// initialize it. There's at least 2 now.
for _ in 0.. 10 {
unsafe { ::winapi::um::winsock2::WSACleanup(); }
Expand Down
5 changes: 2 additions & 3 deletions parity/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn open_ui(ws_conf: &rpc::WsConfiguration, ui_conf: &rpc::UiConfiguration, l

let token = signer::generate_token_and_url(ws_conf, ui_conf, logger_config)?;
// Open a browser
url::open(&token.url);
url::open(&token.url).map_err(|e| format!("{}", e))?;
// Print a message
println!("{}", token.message);
Ok(())
Expand All @@ -157,10 +157,9 @@ pub fn open_dapp(dapps_conf: &dapps::Configuration, rpc_conf: &rpc::HttpConfigur
}

let url = format!("http://{}:{}/{}/", rpc_conf.interface, rpc_conf.port, dapp);
url::open(&url);
url::open(&url).map_err(|e| format!("{}", e))?;
Ok(())
}

// node info fetcher for the local store.
struct FullNodeInfo {
miner: Option<Arc<Miner>>, // TODO: only TXQ needed, just use that after decoupling.
Expand Down
52 changes: 43 additions & 9 deletions parity/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,67 @@

//! Cross-platform open url in default browser

use std;
use std::os::raw::c_int;

#[allow(unused)]
pub enum Error {
ProcessError(std::io::Error),
WindowsShellExecute(c_int),
}

impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::ProcessError(err)
}
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
match *self {
Error::ProcessError(ref e) => write!(f, "{}", e),
Error::WindowsShellExecute(e) => write!(f, "WindowsShellExecute error: {}", e),
}
}
}

#[cfg(windows)]
pub fn open(url: &str) {
pub fn open(url: &str) -> Result<(), Error> {
use std::ffi::CString;
use std::ptr;
use winapi::um::shellapi::ShellExecuteA;
use winapi::um::winuser::SW_SHOWNORMAL as Normal;

unsafe {
const WINDOWS_SHELL_EXECUTE_SUCCESS: c_int = 32;

let h_instance = unsafe {
ShellExecuteA(ptr::null_mut(),
CString::new("open").unwrap().as_ptr(),
CString::new(url.to_owned().replace("\n", "%0A")).unwrap().as_ptr(),
ptr::null(),
ptr::null(),
Normal);
Normal) as c_int
};

// https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx
// `ShellExecute` returns a value greater than 32 on success
if h_instance > WINDOWS_SHELL_EXECUTE_SUCCESS {
Ok(())
} else {
Err(Error::WindowsShellExecute(h_instance))
}
}

#[cfg(any(target_os="macos", target_os="freebsd"))]
pub fn open(url: &str) {
use std;
let _ = std::process::Command::new("open").arg(url).spawn();
pub fn open(url: &str) -> Result<(), Error> {
let _ = std::process::Command::new("open").arg(url).spawn()?;
Ok(())
}

#[cfg(target_os="linux")]
pub fn open(url: &str) {
use std;
let _ = std::process::Command::new("xdg-open").arg(url).spawn();
pub fn open(url: &str) -> Result<(), Error> {
let _ = std::process::Command::new("xdg-open").arg(url).spawn()?;
Ok(())
}

#[cfg(target_os="android")]
Expand Down

0 comments on commit b0cc44a

Please sign in to comment.