blob: f2cca72f4946340bcaa865c93b9ef3d9aeac338c [file] [log] [blame]
[email protected]44013f682012-05-31 13:49:401// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]551707a2010-06-16 16:59:472// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]6d33da172011-11-22 03:56:095#ifndef CHROME_BROWSER_IMAGE_DECODER_H_
6#define CHROME_BROWSER_IMAGE_DECODER_H_
[email protected]551707a2010-06-16 16:59:477
[email protected]928c0e712011-03-16 15:01:578#include <string>
[email protected]551707a2010-06-16 16:59:479#include <vector>
10
[email protected]dfd50642011-11-28 20:00:4411#include "base/compiler_specific.h"
[email protected]810dddf52013-01-25 19:37:2612#include "base/memory/ref_counted.h"
13#include "base/threading/sequenced_worker_pool.h"
[email protected]c4f883a2012-02-03 17:02:0714#include "content/public/browser/utility_process_host_client.h"
[email protected]373c1062011-06-09 21:11:5115
16class SkBitmap;
[email protected]551707a2010-06-16 16:59:4717
[email protected]551707a2010-06-16 16:59:4718// Decodes an image in a sandboxed process.
[email protected]c4f883a2012-02-03 17:02:0719class ImageDecoder : public content::UtilityProcessHostClient {
[email protected]551707a2010-06-16 16:59:4720 public:
21 class Delegate {
22 public:
23 // Called when image is decoded.
[email protected]928c0e712011-03-16 15:01:5724 // |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) {}
[email protected]551707a2010-06-16 16:59:4732
33 protected:
34 virtual ~Delegate() {}
35 };
36
[email protected]11f16d102012-08-29 23:12:1537 enum ImageCodec {
38 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage).
39 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec.
40 };
41
[email protected]551707a2010-06-16 16:59:4742 ImageDecoder(Delegate* delegate,
[email protected]11f16d102012-08-29 23:12:1543 const std::string& image_data,
44 ImageCodec image_codec);
[email protected]551707a2010-06-16 16:59:4745
[email protected]cdc8a3e2013-06-12 04:18:1746 // Starts asynchronous image decoding. Once finished, the callback will be
47 // posted back to |task_runner|.
[email protected]810dddf52013-01-25 19:37:2648 void Start(scoped_refptr<base::SequencedTaskRunner> task_runner);
[email protected]551707a2010-06-16 16:59:4749
[email protected]bebc8e122012-06-12 19:52:3450 const std::vector<unsigned char>& get_image_data() const {
51 return image_data_;
52 }
53
[email protected]11b0856d2012-06-13 19:21:5654 void set_delegate(Delegate* delegate) { delegate_ = delegate; }
55
[email protected]551707a2010-06-16 16:59:4756 private:
57 // It's a reference counted object, so destructor is private.
[email protected]863e6d962011-05-15 19:39:3558 virtual ~ImageDecoder();
[email protected]551707a2010-06-16 16:59:4759
[email protected]c4f883a2012-02-03 17:02:0760 // Overidden from UtilityProcessHostClient:
[email protected]dfd50642011-11-28 20:00:4461 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
[email protected]373c1062011-06-09 21:11:5162
63 // IPC message handlers.
64 void OnDecodeImageSucceeded(const SkBitmap& decoded_image);
65 void OnDecodeImageFailed();
[email protected]551707a2010-06-16 16:59:4766
67 // Launches sandboxed process that will decode the image.
[email protected]4d9dcff2011-04-06 22:49:0868 void DecodeImageInSandbox(const std::vector<unsigned char>& image_data);
[email protected]551707a2010-06-16 16:59:4769
70 Delegate* delegate_;
71 std::vector<unsigned char> image_data_;
[email protected]11f16d102012-08-29 23:12:1572 const ImageCodec image_codec_;
[email protected]810dddf52013-01-25 19:37:2673 scoped_refptr<base::SequencedTaskRunner> task_runner_;
[email protected]551707a2010-06-16 16:59:4774
75 DISALLOW_COPY_AND_ASSIGN(ImageDecoder);
76};
77
[email protected]6d33da172011-11-22 03:56:0978#endif // CHROME_BROWSER_IMAGE_DECODER_H_