OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_IMAGE_DECODER_H_ | 5 #ifndef CHROME_BROWSER_IMAGE_DECODER_H_ |
6 #define CHROME_BROWSER_IMAGE_DECODER_H_ | 6 #define CHROME_BROWSER_IMAGE_DECODER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/threading/sequenced_worker_pool.h" | 13 #include "base/threading/sequenced_worker_pool.h" |
14 #include "content/public/browser/utility_process_host_client.h" | 14 #include "content/common/image_decoder.mojom.h" |
15 | 15 #include "third_party/skia/include/core/SkBitmap.h" |
16 class SkBitmap; | |
17 | 16 |
18 // Decodes an image in a sandboxed process. | 17 // Decodes an image in a sandboxed process. |
19 class ImageDecoder : public content::UtilityProcessHostClient { | 18 class ImageDecoder : public base::RefCountedThreadSafe<ImageDecoder> { |
20 public: | 19 public: |
21 class Delegate { | 20 class Delegate { |
22 public: | 21 public: |
23 // Called when image is decoded. | 22 // Called when image is decoded. |
24 // |decoder| is used to identify the image in case of decoding several | 23 // |decoder| is used to identify the image in case of decoding several |
25 // images simultaneously. | 24 // images simultaneously. |
26 virtual void OnImageDecoded(const ImageDecoder* decoder, | 25 virtual void OnImageDecoded(const ImageDecoder* decoder, |
27 const SkBitmap& decoded_image) = 0; | 26 const SkBitmap& decoded_image) = 0; |
28 | 27 |
29 // Called when decoding image failed. Delegate can do some cleanup in | 28 // Called when decoding image failed. Delegate can do some cleanup in |
(...skipping 22 matching lines...) Expand all Loading... |
52 void Start(scoped_refptr<base::SequencedTaskRunner> task_runner); | 51 void Start(scoped_refptr<base::SequencedTaskRunner> task_runner); |
53 | 52 |
54 const std::vector<unsigned char>& get_image_data() const { | 53 const std::vector<unsigned char>& get_image_data() const { |
55 return image_data_; | 54 return image_data_; |
56 } | 55 } |
57 | 56 |
58 void set_delegate(Delegate* delegate) { delegate_ = delegate; } | 57 void set_delegate(Delegate* delegate) { delegate_ = delegate; } |
59 void set_shrink_to_fit(bool shrink_to_fit) { shrink_to_fit_ = shrink_to_fit; } | 58 void set_shrink_to_fit(bool shrink_to_fit) { shrink_to_fit_ = shrink_to_fit; } |
60 | 59 |
61 private: | 60 private: |
| 61 friend class base::RefCountedThreadSafe<ImageDecoder>; |
62 // It's a reference counted object, so destructor is private. | 62 // It's a reference counted object, so destructor is private. |
63 ~ImageDecoder() override; | 63 ~ImageDecoder(); |
64 | |
65 // Overidden from UtilityProcessHostClient: | |
66 bool OnMessageReceived(const IPC::Message& message) override; | |
67 | |
68 // IPC message handlers. | |
69 void OnDecodeImageSucceeded(const SkBitmap& decoded_image); | |
70 void OnDecodeImageFailed(); | |
71 | 64 |
72 // Launches sandboxed process that will decode the image. | 65 // Launches sandboxed process that will decode the image. |
73 void DecodeImageInSandbox(const std::vector<unsigned char>& image_data); | 66 void DecodeImageInSandbox(const std::vector<unsigned char>& image_data); |
74 | 67 |
| 68 // Mojo RPC callback. |
| 69 void OnDecodeImageDone(bool success, content::ImageDataPtr image); |
| 70 |
75 Delegate* delegate_; | 71 Delegate* delegate_; |
76 std::vector<unsigned char> image_data_; | 72 std::vector<unsigned char> image_data_; |
77 const ImageCodec image_codec_; | 73 const ImageCodec image_codec_; |
78 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 74 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
79 bool shrink_to_fit_; // if needed for IPC msg size limit | 75 bool shrink_to_fit_; // if needed for IPC msg size limit |
| 76 content::ImageDecoderPtr decoder_; |
80 | 77 |
81 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); | 78 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); |
82 }; | 79 }; |
83 | 80 |
| 81 // TODO(amistry): Move somewhere else. |
| 82 namespace mojo { |
| 83 template <> |
| 84 struct TypeConverter<SkBitmap, content::ImageDataPtr> { |
| 85 static SkBitmap Convert(const content::ImageDataPtr& image) { |
| 86 SkBitmap bitmap; |
| 87 if (!bitmap.tryAllocPixels(SkImageInfo::Make( |
| 88 image->width, image->height, |
| 89 static_cast<SkColorType>(image->color_type), |
| 90 static_cast<SkAlphaType>(image->alpha_type)))) { |
| 91 LOG(ERROR) << "Unable to create SkBitmap"; |
| 92 return bitmap; |
| 93 } |
| 94 if (bitmap.getSize() != image->pixels.size()) { |
| 95 LOG(ERROR) << "Incorrect bitmap size, expected: " << image->pixels.size() |
| 96 << ", actual: " << bitmap.getSize(); |
| 97 return bitmap; |
| 98 } |
| 99 memcpy(bitmap.getPixels(), &image->pixels[0], bitmap.getSize()); |
| 100 return bitmap; |
| 101 } |
| 102 }; |
| 103 } // namespace mojo |
| 104 |
84 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ | 105 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ |
OLD | NEW |