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 <string> | 9 #include <string> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/lazy_instance.h" |
12 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/synchronization/lock.h" |
13 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
| 17 #include "base/timer/timer.h" |
| 18 #include "content/public/browser/utility_process_host.h" |
14 #include "content/public/browser/utility_process_host_client.h" | 19 #include "content/public/browser/utility_process_host_client.h" |
15 | 20 |
16 class SkBitmap; | 21 class SkBitmap; |
17 | 22 |
18 // Decodes an image in a sandboxed process. | 23 // This is a helper class for decoding images safely in a utility process. To |
| 24 // use this, call ImageDecoder::Start(...) or |
| 25 // ImageDecoder::StartWithOptions(...) on any thread. |
| 26 // |
| 27 // Internally, most of the work happens on the IO thread, and then |
| 28 // the callback (ImageRequest::OnImageDecoded or |
| 29 // ImageRequest::OnDecodeImageFailed) is posted back to the |task_runner_| |
| 30 // associated with the ImageRequest. |
| 31 // The Cancel() method runs on whichever thread called it. |map_lock_| is used |
| 32 // to protect the data that is accessed from multiple threads. |
19 class ImageDecoder : public content::UtilityProcessHostClient { | 33 class ImageDecoder : public content::UtilityProcessHostClient { |
20 public: | 34 public: |
21 class Delegate { | 35 class ImageRequest { |
22 public: | 36 public: |
23 // Called when image is decoded. | 37 // Called when image is decoded. |
24 // |decoder| is used to identify the image in case of decoding several | 38 virtual void OnImageDecoded(const SkBitmap& decoded_image) = 0; |
25 // images simultaneously. | |
26 virtual void OnImageDecoded(const ImageDecoder* decoder, | |
27 const SkBitmap& decoded_image) = 0; | |
28 | 39 |
29 // Called when decoding image failed. Delegate can do some cleanup in | 40 // Called when decoding image failed. ImageRequest can do some cleanup in |
30 // this handler. | 41 // this handler. |
31 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) {} | 42 virtual void OnDecodeImageFailed() {} |
| 43 |
| 44 base::SequencedTaskRunner* task_runner() const { |
| 45 return task_runner_.get(); |
| 46 } |
32 | 47 |
33 protected: | 48 protected: |
34 virtual ~Delegate() {} | 49 explicit ImageRequest( |
| 50 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
| 51 virtual ~ImageRequest(); |
| 52 |
| 53 private: |
| 54 // The thread to post OnImageDecoded or OnDecodeImageFailed once the |
| 55 // the image has been decoded. |
| 56 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
35 }; | 57 }; |
36 | 58 |
37 enum ImageCodec { | 59 enum ImageCodec { |
38 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). | 60 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). |
39 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. | 61 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. |
40 }; | 62 }; |
41 | 63 |
42 ImageDecoder(Delegate* delegate, | 64 // Calls StartWithOptions with ImageCodec::DEFAULT_CODEC and |
43 const std::string& image_data, | 65 // shrink_to_fit = false. |
44 ImageCodec image_codec); | 66 static void Start(ImageRequest* image_request, |
45 | 67 const std::string& image_data); |
46 ImageDecoder(Delegate* delegate, | |
47 const std::vector<char>& image_data, | |
48 ImageCodec image_codec); | |
49 | 68 |
50 // Starts asynchronous image decoding. Once finished, the callback will be | 69 // Starts asynchronous image decoding. Once finished, the callback will be |
51 // posted back to |task_runner|. | 70 // posted back to image_request's |task_runner_|. |
52 void Start(scoped_refptr<base::SequencedTaskRunner> task_runner); | 71 static void StartWithOptions(ImageRequest* image_request, |
| 72 const std::string& image_data, |
| 73 ImageCodec image_codec, |
| 74 bool shrink_to_fit); |
53 | 75 |
54 const std::vector<unsigned char>& get_image_data() const { | 76 // Removes all instances of image_request from |image_request_id_map_|, |
55 return image_data_; | 77 // ensuring callbacks are not made to the image_request after it is destroyed. |
56 } | 78 static void Cancel(ImageRequest* image_request); |
57 | |
58 void set_delegate(Delegate* delegate) { delegate_ = delegate; } | |
59 void set_shrink_to_fit(bool shrink_to_fit) { shrink_to_fit_ = shrink_to_fit; } | |
60 | 79 |
61 private: | 80 private: |
| 81 friend struct base::DefaultLazyInstanceTraits<ImageDecoder>; |
| 82 |
| 83 ImageDecoder(); |
62 // It's a reference counted object, so destructor is private. | 84 // It's a reference counted object, so destructor is private. |
63 ~ImageDecoder() override; | 85 ~ImageDecoder() override; |
64 | 86 |
65 // Overidden from UtilityProcessHostClient: | 87 // Sends a request to the sandboxed process to decode the image. Starts |
| 88 // batch mode if necessary. |
| 89 void DecodeImageInSandbox(ImageRequest* image_request, |
| 90 const std::vector<unsigned char>& image_data, |
| 91 ImageCodec image_codec, |
| 92 bool shrink_to_fit); |
| 93 |
| 94 void CancelImpl(ImageRequest* image_request); |
| 95 |
| 96 using RequestMap = std::map<int, ImageRequest*>; |
| 97 |
| 98 // Starts UtilityProcessHost in batch mode and starts |batch_mode_timer_|. |
| 99 void StartBatchMode(); |
| 100 |
| 101 // Stops batch mode if no requests have come in since |
| 102 // kBatchModeTimeoutSeconds. |
| 103 void StopBatchMode(); |
| 104 |
| 105 // Overidden from UtilityProcessHostClient. |
66 bool OnMessageReceived(const IPC::Message& message) override; | 106 bool OnMessageReceived(const IPC::Message& message) override; |
67 | 107 |
68 // IPC message handlers. | 108 // IPC message handlers. |
69 void OnDecodeImageSucceeded(const SkBitmap& decoded_image); | 109 void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int request_id); |
70 void OnDecodeImageFailed(); | 110 void OnDecodeImageFailed(int request_id); |
71 | 111 |
72 // Launches sandboxed process that will decode the image. | 112 // id to use for the next Start request that comes in. |
73 void DecodeImageInSandbox(const std::vector<unsigned char>& image_data); | 113 int image_request_id_counter_; |
74 | 114 |
75 Delegate* delegate_; | 115 // Map of request id's to ImageRequests. |
76 std::vector<unsigned char> image_data_; | 116 RequestMap image_request_id_map_; |
77 const ImageCodec image_codec_; | 117 |
78 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 118 // Protects |image_request_id_map_| and |image_request_id_counter_|. |
79 bool shrink_to_fit_; // if needed for IPC msg size limit | 119 base::Lock map_lock_; |
| 120 |
| 121 // The UtilityProcessHost requests are sent to. |
| 122 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; |
| 123 |
| 124 // Calls StopBatchMode after kBatchModeTimeoutSeconds have elapsed. |
| 125 base::RepeatingTimer<ImageDecoder> batch_mode_timer_; |
| 126 |
| 127 // The time Start was last called. |
| 128 base::TimeTicks last_request_; |
80 | 129 |
81 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); | 130 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); |
82 }; | 131 }; |
83 | 132 |
84 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ | 133 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ |
OLD | NEW |