Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_IMAGE_DECODER_H_
      6 #define CHROME_BROWSER_IMAGE_DECODER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/threading/sequenced_worker_pool.h"
     14 #include "content/public/browser/utility_process_host_client.h"
     15 
     16 class SkBitmap;
     17 
     18 // Decodes an image in a sandboxed process.
     19 class ImageDecoder : public content::UtilityProcessHostClient {
     20  public:
     21   class Delegate {
     22    public:
     23     // Called when image is decoded.
     24     // |decoder| is used to identify the image in case of decoding several
     25     // images simultaneously.
     26     virtual void OnImageDecoded(const ImageDecoder* decoder,
     27                                 const SkBitmap& decoded_image) = 0;
     28 
     29     // Called when decoding image failed. Delegate can do some cleanup in
     30     // this handler.
     31     virtual void OnDecodeImageFailed(const ImageDecoder* decoder) {}
     32 
     33    protected:
     34     virtual ~Delegate() {}
     35   };
     36 
     37   enum ImageCodec {
     38     DEFAULT_CODEC = 0,  // Uses WebKit image decoding (via WebImage).
     39     ROBUST_JPEG_CODEC,  // Restrict decoding to robust jpeg codec.
     40   };
     41 
     42   ImageDecoder(Delegate* delegate,
     43                const std::string& image_data,
     44                ImageCodec image_codec);
     45 
     46   // Starts asynchronous image decoding. Once finished, the callback will be
     47   // posted back to |task_runner|.
     48   void Start(scoped_refptr<base::SequencedTaskRunner> task_runner);
     49 
     50   const std::vector<unsigned char>& get_image_data() const {
     51     return image_data_;
     52   }
     53 
     54   void set_delegate(Delegate* delegate) { delegate_ = delegate; }
     55 
     56  private:
     57   // It's a reference counted object, so destructor is private.
     58   virtual ~ImageDecoder();
     59 
     60   // Overidden from UtilityProcessHostClient:
     61   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     62 
     63   // IPC message handlers.
     64   void OnDecodeImageSucceeded(const SkBitmap& decoded_image);
     65   void OnDecodeImageFailed();
     66 
     67   // Launches sandboxed process that will decode the image.
     68   void DecodeImageInSandbox(const std::vector<unsigned char>& image_data);
     69 
     70   Delegate* delegate_;
     71   std::vector<unsigned char> image_data_;
     72   const ImageCodec image_codec_;
     73   scoped_refptr<base::SequencedTaskRunner> task_runner_;
     74 
     75   DISALLOW_COPY_AND_ASSIGN(ImageDecoder);
     76 };
     77 
     78 #endif  // CHROME_BROWSER_IMAGE_DECODER_H_
     79