mirror of
https://github.com/servo/servo.git
synced 2025-07-23 15:23:42 +01:00
Return Result from ProtocolRegistry::register() (#36688)
Instead of returning true / false it's better to return a Result (even if we continue ignoring possible error). Testing: Doesn't change any behavior Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
This commit is contained in:
parent
281d942981
commit
cf59aa1948
2 changed files with 29 additions and 10 deletions
|
@ -54,27 +54,45 @@ pub struct ProtocolRegistry {
|
||||||
pub(crate) handlers: HashMap<String, Box<dyn ProtocolHandler>>, // Maps scheme -> handler
|
pub(crate) handlers: HashMap<String, Box<dyn ProtocolHandler>>, // Maps scheme -> handler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub enum ProtocolRegisterError {
|
||||||
|
ForbiddenScheme,
|
||||||
|
SchemeAlreadyRegistered,
|
||||||
|
}
|
||||||
|
|
||||||
impl ProtocolRegistry {
|
impl ProtocolRegistry {
|
||||||
pub fn with_internal_protocols() -> Self {
|
pub fn with_internal_protocols() -> Self {
|
||||||
let mut registry = Self::default();
|
let mut registry = Self::default();
|
||||||
registry.register("data", DataProtocolHander::default());
|
// We just created a new registry, and know that we aren't using
|
||||||
registry.register("blob", BlobProtocolHander::default());
|
// any forbidden schemes, so this should never panic.
|
||||||
registry.register("file", FileProtocolHander::default());
|
registry
|
||||||
|
.register("data", DataProtocolHander::default())
|
||||||
|
.expect("Infallible");
|
||||||
|
registry
|
||||||
|
.register("blob", BlobProtocolHander::default())
|
||||||
|
.expect("Infallible");
|
||||||
|
registry
|
||||||
|
.register("file", FileProtocolHander::default())
|
||||||
|
.expect("Infallible");
|
||||||
registry
|
registry
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn register(&mut self, scheme: &str, handler: impl ProtocolHandler + 'static) -> bool {
|
pub fn register(
|
||||||
|
&mut self,
|
||||||
|
scheme: &str,
|
||||||
|
handler: impl ProtocolHandler + 'static,
|
||||||
|
) -> Result<(), ProtocolRegisterError> {
|
||||||
if FORBIDDEN_SCHEMES.contains(&scheme) {
|
if FORBIDDEN_SCHEMES.contains(&scheme) {
|
||||||
error!("Protocol handler for '{scheme}' is not allowed to be registered.");
|
error!("Protocol handler for '{scheme}' is not allowed to be registered.");
|
||||||
return false;
|
return Err(ProtocolRegisterError::ForbiddenScheme);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Entry::Vacant(entry) = self.handlers.entry(scheme.into()) {
|
if let Entry::Vacant(entry) = self.handlers.entry(scheme.into()) {
|
||||||
entry.insert(Box::new(handler));
|
entry.insert(Box::new(handler));
|
||||||
true
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
error!("Protocol handler for '{scheme}' is already registered.");
|
error!("Protocol handler for '{scheme}' is already registered.");
|
||||||
false
|
Err(ProtocolRegisterError::SchemeAlreadyRegistered)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -122,12 +122,13 @@ impl App {
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut protocol_registry = ProtocolRegistry::default();
|
let mut protocol_registry = ProtocolRegistry::default();
|
||||||
protocol_registry.register(
|
let _ = protocol_registry.register(
|
||||||
"urlinfo",
|
"urlinfo",
|
||||||
protocols::urlinfo::UrlInfoProtocolHander::default(),
|
protocols::urlinfo::UrlInfoProtocolHander::default(),
|
||||||
);
|
);
|
||||||
protocol_registry.register("servo", protocols::servo::ServoProtocolHandler::default());
|
let _ =
|
||||||
protocol_registry.register(
|
protocol_registry.register("servo", protocols::servo::ServoProtocolHandler::default());
|
||||||
|
let _ = protocol_registry.register(
|
||||||
"resource",
|
"resource",
|
||||||
protocols::resource::ResourceProtocolHandler::default(),
|
protocols::resource::ResourceProtocolHandler::default(),
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue