diff options
Diffstat (limited to 'components/net_traits/image_cache_task.rs')
-rw-r--r-- | components/net_traits/image_cache_task.rs | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/components/net_traits/image_cache_task.rs b/components/net_traits/image_cache_task.rs index dd6bb3c98ff..3614cf4d58a 100644 --- a/components/net_traits/image_cache_task.rs +++ b/components/net_traits/image_cache_task.rs @@ -12,7 +12,7 @@ use std::sync::mpsc::{channel, Sender}; /// image load completes. It is typically used to trigger a reflow /// and/or repaint. pub trait ImageResponder : Send { - fn respond(&self, Option<Arc<Image>>); + fn respond(&self, ImageResponse); } /// The current state of an image in the cache. @@ -23,6 +23,17 @@ pub enum ImageState { NotRequested, } +/// The returned image. +#[derive(Clone)] +pub enum ImageResponse { + /// The requested image was loaded. + Loaded(Arc<Image>), + /// The requested image failed to load, so a placeholder was loaded instead. + PlaceholderLoaded(Arc<Image>), + /// Neither the requested image nor the placeholder could be loaded. + None +} + /// Channel for sending commands to the image cache. #[derive(Clone)] pub struct ImageCacheChan(pub Sender<ImageCacheResult>); @@ -31,7 +42,7 @@ pub struct ImageCacheChan(pub Sender<ImageCacheResult>); /// caller. pub struct ImageCacheResult { pub responder: Option<Box<ImageResponder>>, - pub image: Option<Arc<Image>>, + pub image_response: ImageResponse, } /// Commands that the image cache understands. @@ -45,12 +56,18 @@ pub enum ImageCacheCommand { /// TODO(gw): Profile this on some real world sites and see /// if it's worth caching the results of this locally in each /// layout / paint task. - GetImageIfAvailable(Url, Sender<Result<Arc<Image>, ImageState>>), + GetImageIfAvailable(Url, UsePlaceholder, Sender<Result<Arc<Image>, ImageState>>), /// Clients must wait for a response before shutting down the ResourceTask Exit(Sender<()>), } +#[derive(Copy, Clone, PartialEq)] +pub enum UsePlaceholder { + No, + Yes, +} + /// The client side of the image cache task. This can be safely cloned /// and passed to different tasks. #[derive(Clone)] @@ -78,9 +95,10 @@ impl ImageCacheTask { } /// Get the current state of an image. See ImageCacheCommand::GetImageIfAvailable. - pub fn get_image_if_available(&self, url: Url) -> Result<Arc<Image>, ImageState> { + pub fn get_image_if_available(&self, url: Url, use_placeholder: UsePlaceholder) + -> Result<Arc<Image>, ImageState> { let (sender, receiver) = channel(); - let msg = ImageCacheCommand::GetImageIfAvailable(url, sender); + let msg = ImageCacheCommand::GetImageIfAvailable(url, use_placeholder, sender); self.chan.send(msg).unwrap(); receiver.recv().unwrap() } @@ -92,3 +110,4 @@ impl ImageCacheTask { response_port.recv().unwrap(); } } + |