| OLD | NEW |
| (Empty) |
| 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 <map> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/macros.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/sequence_checker.h" | |
| 15 #include "base/sequenced_task_runner.h" | |
| 16 #include "base/synchronization/lock.h" | |
| 17 | |
| 18 namespace gfx { | |
| 19 class Size; | |
| 20 } // namespace gfx | |
| 21 | |
| 22 class SkBitmap; | |
| 23 | |
| 24 // This is a helper class for decoding images safely in a sandboxed service. To | |
| 25 // use this, call ImageDecoder::Start(...) or | |
| 26 // ImageDecoder::StartWithOptions(...) on any thread. | |
| 27 // | |
| 28 // ImageRequest::OnImageDecoded or ImageRequest::OnDecodeImageFailed is posted | |
| 29 // back to the |task_runner_| associated with the ImageRequest. | |
| 30 // | |
| 31 // The Cancel() method runs on whichever thread called it. | |
| 32 // | |
| 33 // TODO(rockot): Use of this class should be replaced with direct image_decoder | |
| 34 // client library usage. | |
| 35 class ImageDecoder { | |
| 36 public: | |
| 37 // ImageRequest objects needs to be created and destroyed on the same | |
| 38 // SequencedTaskRunner. | |
| 39 class ImageRequest { | |
| 40 public: | |
| 41 // Called when image is decoded. | |
| 42 virtual void OnImageDecoded(const SkBitmap& decoded_image) = 0; | |
| 43 | |
| 44 // Called when decoding image failed. ImageRequest can do some cleanup in | |
| 45 // this handler. | |
| 46 virtual void OnDecodeImageFailed() {} | |
| 47 | |
| 48 base::SequencedTaskRunner* task_runner() const { | |
| 49 return task_runner_.get(); | |
| 50 } | |
| 51 | |
| 52 protected: | |
| 53 // Creates an ImageRequest that runs on the thread which created it. | |
| 54 ImageRequest(); | |
| 55 | |
| 56 // Explicitly pass in |task_runner| if the current thread is part of a | |
| 57 // thread pool. | |
| 58 explicit ImageRequest( | |
| 59 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | |
| 60 virtual ~ImageRequest(); | |
| 61 | |
| 62 private: | |
| 63 // The thread to post OnImageDecoded() or OnDecodeImageFailed() once the | |
| 64 // the image has been decoded. | |
| 65 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 66 | |
| 67 base::SequenceChecker sequence_checker_; | |
| 68 }; | |
| 69 | |
| 70 enum ImageCodec { | |
| 71 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). | |
| 72 #if defined(OS_CHROMEOS) | |
| 73 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. | |
| 74 ROBUST_PNG_CODEC, // Restrict decoding to robust PNG codec. | |
| 75 #endif // defined(OS_CHROMEOS) | |
| 76 }; | |
| 77 | |
| 78 static ImageDecoder* GetInstance(); | |
| 79 | |
| 80 // Calls StartWithOptions() with ImageCodec::DEFAULT_CODEC and | |
| 81 // shrink_to_fit = false. | |
| 82 static void Start(ImageRequest* image_request, | |
| 83 std::vector<uint8_t> image_data); | |
| 84 // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy. | |
| 85 static void Start(ImageRequest* image_request, | |
| 86 const std::string& image_data); | |
| 87 | |
| 88 // Starts asynchronous image decoding. Once finished, the callback will be | |
| 89 // posted back to image_request's |task_runner_|. | |
| 90 // For images with multiple frames (e.g. ico files), a frame with a size as | |
| 91 // close as possible to |desired_image_frame_size| is chosen (tries to take | |
| 92 // one in larger size if there's no precise match). Passing gfx::Size() as | |
| 93 // |desired_image_frame_size| is also supported and will result in chosing the | |
| 94 // smallest available size. | |
| 95 static void StartWithOptions(ImageRequest* image_request, | |
| 96 std::vector<uint8_t> image_data, | |
| 97 ImageCodec image_codec, | |
| 98 bool shrink_to_fit, | |
| 99 const gfx::Size& desired_image_frame_size); | |
| 100 // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy. | |
| 101 static void StartWithOptions(ImageRequest* image_request, | |
| 102 const std::string& image_data, | |
| 103 ImageCodec image_codec, | |
| 104 bool shrink_to_fit); | |
| 105 | |
| 106 // Removes all instances of |image_request| from |image_request_id_map_|, | |
| 107 // ensuring callbacks are not made to the image_request after it is destroyed. | |
| 108 static void Cancel(ImageRequest* image_request); | |
| 109 | |
| 110 private: | |
| 111 using RequestMap = std::map<int, ImageRequest*>; | |
| 112 | |
| 113 ImageDecoder(); | |
| 114 ~ImageDecoder() = delete; | |
| 115 | |
| 116 void StartWithOptionsImpl(ImageRequest* image_request, | |
| 117 std::vector<uint8_t> image_data, | |
| 118 ImageCodec image_codec, | |
| 119 bool shrink_to_fit, | |
| 120 const gfx::Size& desired_image_frame_size); | |
| 121 | |
| 122 void CancelImpl(ImageRequest* image_request); | |
| 123 | |
| 124 // IPC message handlers. | |
| 125 void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int request_id); | |
| 126 void OnDecodeImageFailed(int request_id); | |
| 127 | |
| 128 // id to use for the next Start() request that comes in. | |
| 129 int image_request_id_counter_; | |
| 130 | |
| 131 // Map of request id's to ImageRequests. | |
| 132 RequestMap image_request_id_map_; | |
| 133 | |
| 134 // Protects |image_request_id_map_| and |image_request_id_counter_|. | |
| 135 base::Lock map_lock_; | |
| 136 | |
| 137 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); | |
| 138 }; | |
| 139 | |
| 140 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ | |
| OLD | NEW |