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
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum ProtocolRegisterError {
|
||||
ForbiddenScheme,
|
||||
SchemeAlreadyRegistered,
|
||||
}
|
||||
|
||||
impl ProtocolRegistry {
|
||||
pub fn with_internal_protocols() -> Self {
|
||||
let mut registry = Self::default();
|
||||
registry.register("data", DataProtocolHander::default());
|
||||
registry.register("blob", BlobProtocolHander::default());
|
||||
registry.register("file", FileProtocolHander::default());
|
||||
// We just created a new registry, and know that we aren't using
|
||||
// any forbidden schemes, so this should never panic.
|
||||
registry
|
||||
.register("data", DataProtocolHander::default())
|
||||
.expect("Infallible");
|
||||
registry
|
||||
.register("blob", BlobProtocolHander::default())
|
||||
.expect("Infallible");
|
||||
registry
|
||||
.register("file", FileProtocolHander::default())
|
||||
.expect("Infallible");
|
||||
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) {
|
||||
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()) {
|
||||
entry.insert(Box::new(handler));
|
||||
true
|
||||
Ok(())
|
||||
} else {
|
||||
error!("Protocol handler for '{scheme}' is already registered.");
|
||||
false
|
||||
Err(ProtocolRegisterError::SchemeAlreadyRegistered)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -122,12 +122,13 @@ impl App {
|
|||
}
|
||||
|
||||
let mut protocol_registry = ProtocolRegistry::default();
|
||||
protocol_registry.register(
|
||||
let _ = protocol_registry.register(
|
||||
"urlinfo",
|
||||
protocols::urlinfo::UrlInfoProtocolHander::default(),
|
||||
);
|
||||
protocol_registry.register("servo", protocols::servo::ServoProtocolHandler::default());
|
||||
protocol_registry.register(
|
||||
let _ =
|
||||
protocol_registry.register("servo", protocols::servo::ServoProtocolHandler::default());
|
||||
let _ = protocol_registry.register(
|
||||
"resource",
|
||||
protocols::resource::ResourceProtocolHandler::default(),
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue