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:
Jonathan Schwender 2025-04-25 12:02:46 +02:00 committed by GitHub
parent 281d942981
commit cf59aa1948
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 10 deletions

View file

@ -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)
}
}

View file

@ -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(),
);