servo: Merge #10767 - Added Store Command to ImageCache Task (from akhan7:command); r=jdm
authorSagar Muchhal <muchhalsagar88@gmail.com>
Tue, 26 Apr 2016 07:40:18 -0700
changeset 476797 b515299f42bfa9c6fe1c304688554253875c96f8
parent 476796 70ebd9dedc1246fc8a5318bf439d75fdc1186f52
child 476798 15d63d4ea03037e4a67fb2597a7d57a9452ff0f6
push id44079
push userbmo:gps@mozilla.com
push dateSat, 04 Feb 2017 00:14:49 +0000
reviewersjdm
servo: Merge #10767 - Added Store Command to ImageCache Task (from akhan7:command); r=jdm Implemented last bullet of [Image conformance student project: Initial Steps](https://github.com/servo/servo/wiki/Image-load-conformance-student-project) Source-Repo: https://github.com/servo/servo Source-Revision: 47efbea666ed60e09804d57f42a8348c30815907
servo/components/net/image_cache_thread.rs
servo/components/net_traits/image_cache_thread.rs
--- a/servo/components/net/image_cache_thread.rs
+++ b/servo/components/net/image_cache_thread.rs
@@ -389,16 +389,19 @@ impl ImageCache {
             ImageCacheCommand::GetImageIfAvailable(url, use_placeholder, consumer) => {
                 let result = self.get_image_if_available(url, use_placeholder);
                 consumer.send(result).unwrap();
             }
             ImageCacheCommand::GetImageOrMetadataIfAvailable(url, use_placeholder, consumer) => {
                 let result = self.get_image_or_meta_if_available(url, use_placeholder);
                 consumer.send(result).unwrap();
             }
+            ImageCacheCommand::StoreDecodeImage(url, image_vector) => {
+                self.store_decode_image(url, image_vector);
+            }
         };
 
         None
     }
 
     // Handle progress messages from the resource thread
     fn handle_progress(&mut self, msg: ResourceLoadInfo) {
         match (msg.action, msg.key) {
@@ -583,16 +586,33 @@ impl ImageCache {
                     Some(ref meta) => meta,
                     None => return Err(ImageState::Pending),
                 };
 
                 Ok(ImageOrMetadataAvailable::MetadataAvailable(meta.clone()))
             }
         }
     }
+
+    fn store_decode_image(&mut self,
+                          ref_url: Url,
+                          loaded_bytes: Vec<u8>) {
+        let (cache_result, load_key, _) = self.pending_loads.get_cached(Arc::new(ref_url));
+        assert!(cache_result == CacheResult::Miss);
+        let action = ResponseAction::DataAvailable(loaded_bytes);
+        let _ = self.progress_sender.send(ResourceLoadInfo {
+            action: action,
+            key: load_key,
+        });
+        let action = ResponseAction::ResponseComplete(Ok(()));
+        let _ = self.progress_sender.send(ResourceLoadInfo {
+            action: action,
+            key: load_key,
+        });
+    }
 }
 
 /// Create a new image cache.
 pub fn new_image_cache_thread(resource_thread: ResourceThread,
                               webrender_api: Option<webrender_traits::RenderApi>) -> ImageCacheThread {
     let (ipc_command_sender, ipc_command_receiver) = ipc::channel().unwrap();
 
     spawn_named("ImageCacheThread".to_owned(), move || {
--- a/servo/components/net_traits/image_cache_thread.rs
+++ b/servo/components/net_traits/image_cache_thread.rs
@@ -87,16 +87,20 @@ pub enum ImageCacheCommand {
     /// if it's worth caching the results of this locally in each
     /// layout / paint thread.
     GetImageIfAvailable(Url, UsePlaceholder, IpcSender<Result<Arc<Image>, ImageState>>),
 
     /// Synchronously check the state of an image in the cache. If the image is in a loading
     /// state and but its metadata has been made available, it will be sent as a response.
     GetImageOrMetadataIfAvailable(Url, UsePlaceholder, IpcSender<Result<ImageOrMetadataAvailable, ImageState>>),
 
+    /// Instruct the cache to store this data as a newly-complete network request and continue
+    /// decoding the result into pixel data
+    StoreDecodeImage(Url, Vec<u8>),
+
     /// Clients must wait for a response before shutting down the ResourceThread
     Exit(IpcSender<()>),
 }
 
 #[derive(Copy, Clone, PartialEq, Hash, Eq, Deserialize, Serialize)]
 pub enum UsePlaceholder {
     No,
     Yes,
@@ -152,16 +156,23 @@ impl ImageCacheThread {
     pub fn find_image_or_metadata(&self, url: Url, use_placeholder: UsePlaceholder)
                                   -> Result<ImageOrMetadataAvailable, ImageState> {
         let (sender, receiver) = ipc::channel().unwrap();
         let msg = ImageCacheCommand::GetImageOrMetadataIfAvailable(url, use_placeholder, sender);
         self.chan.send(msg).unwrap();
         receiver.recv().unwrap()
     }
 
+    /// Decode the given image bytes and cache the result for the given URL.
+    pub fn store_complete_image_bytes(&self,
+                                   url: Url,
+                                   image_data: Vec<u8>) {
+        let msg = ImageCacheCommand::StoreDecodeImage(url, image_data);
+        self.chan.send(msg).unwrap();
+    }
+
     /// Shutdown the image cache thread.
     pub fn exit(&self) {
         let (response_chan, response_port) = ipc::channel().unwrap();
         self.chan.send(ImageCacheCommand::Exit(response_chan)).unwrap();
         response_port.recv().unwrap();
     }
 }
-