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

View file

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