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 <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/sequence_checker.h" | 14 #include "base/sequence_checker.h" |
15 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
17 | 17 |
| 18 namespace gfx { |
| 19 class Size; |
| 20 } // namespace gfx |
| 21 |
18 class SkBitmap; | 22 class SkBitmap; |
19 | 23 |
20 // This is a helper class for decoding images safely in a sandboxed service. To | 24 // This is a helper class for decoding images safely in a sandboxed service. To |
21 // use this, call ImageDecoder::Start(...) or | 25 // use this, call ImageDecoder::Start(...) or |
22 // ImageDecoder::StartWithOptions(...) on any thread. | 26 // ImageDecoder::StartWithOptions(...) on any thread. |
23 // | 27 // |
24 // ImageRequest::OnImageDecoded or ImageRequest::OnDecodeImageFailed is posted | 28 // ImageRequest::OnImageDecoded or ImageRequest::OnDecodeImageFailed is posted |
25 // back to the |task_runner_| associated with the ImageRequest. | 29 // back to the |task_runner_| associated with the ImageRequest. |
26 // | 30 // |
27 // The Cancel() method runs on whichever thread called it. | 31 // The Cancel() method runs on whichever thread called it. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 // Calls StartWithOptions() with ImageCodec::DEFAULT_CODEC and | 81 // Calls StartWithOptions() with ImageCodec::DEFAULT_CODEC and |
78 // shrink_to_fit = false. | 82 // shrink_to_fit = false. |
79 static void Start(ImageRequest* image_request, | 83 static void Start(ImageRequest* image_request, |
80 std::vector<uint8_t> image_data); | 84 std::vector<uint8_t> image_data); |
81 // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy. | 85 // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy. |
82 static void Start(ImageRequest* image_request, | 86 static void Start(ImageRequest* image_request, |
83 const std::string& image_data); | 87 const std::string& image_data); |
84 | 88 |
85 // Starts asynchronous image decoding. Once finished, the callback will be | 89 // Starts asynchronous image decoding. Once finished, the callback will be |
86 // posted back to image_request's |task_runner_|. | 90 // posted back to image_request's |task_runner_|. |
| 91 // For images with multiple frames (e.g. ico files), we choose a frame with a |
| 92 // size as close as possible to |desired_image_frame_size| (tries to take one |
| 93 // in larger size if there's no precise match). |
87 static void StartWithOptions(ImageRequest* image_request, | 94 static void StartWithOptions(ImageRequest* image_request, |
88 std::vector<uint8_t> image_data, | 95 std::vector<uint8_t> image_data, |
89 ImageCodec image_codec, | 96 ImageCodec image_codec, |
90 bool shrink_to_fit); | 97 bool shrink_to_fit, |
| 98 const gfx::Size& desired_image_frame_size); |
91 // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy. | 99 // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy. |
92 static void StartWithOptions(ImageRequest* image_request, | 100 static void StartWithOptions(ImageRequest* image_request, |
93 const std::string& image_data, | 101 const std::string& image_data, |
94 ImageCodec image_codec, | 102 ImageCodec image_codec, |
95 bool shrink_to_fit); | 103 bool shrink_to_fit); |
96 | 104 |
97 // Removes all instances of |image_request| from |image_request_id_map_|, | 105 // Removes all instances of |image_request| from |image_request_id_map_|, |
98 // ensuring callbacks are not made to the image_request after it is destroyed. | 106 // ensuring callbacks are not made to the image_request after it is destroyed. |
99 static void Cancel(ImageRequest* image_request); | 107 static void Cancel(ImageRequest* image_request); |
100 | 108 |
101 private: | 109 private: |
102 using RequestMap = std::map<int, ImageRequest*>; | 110 using RequestMap = std::map<int, ImageRequest*>; |
103 | 111 |
104 void StartWithOptionsImpl(ImageRequest* image_request, | 112 void StartWithOptionsImpl(ImageRequest* image_request, |
105 std::vector<uint8_t> image_data, | 113 std::vector<uint8_t> image_data, |
106 ImageCodec image_codec, | 114 ImageCodec image_codec, |
107 bool shrink_to_fit); | 115 bool shrink_to_fit, |
| 116 const gfx::Size& desired_image_frame_size); |
108 | 117 |
109 void CancelImpl(ImageRequest* image_request); | 118 void CancelImpl(ImageRequest* image_request); |
110 | 119 |
111 // IPC message handlers. | 120 // IPC message handlers. |
112 void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int request_id); | 121 void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int request_id); |
113 void OnDecodeImageFailed(int request_id); | 122 void OnDecodeImageFailed(int request_id); |
114 | 123 |
115 // id to use for the next Start() request that comes in. | 124 // id to use for the next Start() request that comes in. |
116 int image_request_id_counter_; | 125 int image_request_id_counter_; |
117 | 126 |
118 // Map of request id's to ImageRequests. | 127 // Map of request id's to ImageRequests. |
119 RequestMap image_request_id_map_; | 128 RequestMap image_request_id_map_; |
120 | 129 |
121 // Protects |image_request_id_map_| and |image_request_id_counter_|. | 130 // Protects |image_request_id_map_| and |image_request_id_counter_|. |
122 base::Lock map_lock_; | 131 base::Lock map_lock_; |
123 | 132 |
124 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); | 133 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); |
125 }; | 134 }; |
126 | 135 |
127 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ | 136 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ |
OLD | NEW |