Chromium Code Reviews| 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" |
| 13 #include "base/threading/sequenced_worker_pool.h" | 15 #include "base/threading/sequenced_worker_pool.h" |
| 16 #include "content/public/browser/utility_process_host.h" | |
| 14 #include "content/public/browser/utility_process_host_client.h" | 17 #include "content/public/browser/utility_process_host_client.h" |
| 15 | 18 |
| 16 class SkBitmap; | 19 class SkBitmap; |
| 17 | 20 |
| 18 // Decodes an image in a sandboxed process. | 21 // Singleton to decode images in a sandboxed process. |
| 19 class ImageDecoder : public content::UtilityProcessHostClient { | 22 class ImageDecoder { |
| 20 public: | 23 public: |
| 21 class Delegate { | 24 class Delegate { |
| 22 public: | 25 public: |
| 23 // Called when image is decoded. | 26 // Called when image is decoded. |
| 24 // |decoder| is used to identify the image in case of decoding several | 27 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 | 28 |
| 29 // Called when decoding image failed. Delegate can do some cleanup in | 29 // Called when decoding image failed. Delegate can do some cleanup in |
| 30 // this handler. | 30 // this handler. |
| 31 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) {} | 31 virtual void OnDecodeImageFailed() {} |
| 32 | 32 |
| 33 protected: | 33 protected: |
| 34 virtual ~Delegate() {} | 34 virtual ~Delegate() {} |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 enum ImageCodec { | 37 enum ImageCodec { |
| 38 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). | 38 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). |
| 39 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. | 39 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 ImageDecoder(Delegate* delegate, | 42 static ImageDecoder* GetInstance(); |
| 43 const std::string& image_data, | |
| 44 ImageCodec image_codec); | |
| 45 | |
| 46 ImageDecoder(Delegate* delegate, | |
| 47 const std::vector<char>& image_data, | |
| 48 ImageCodec image_codec); | |
| 49 | 43 |
| 50 // Starts asynchronous image decoding. Once finished, the callback will be | 44 // Starts asynchronous image decoding. Once finished, the callback will be |
| 51 // posted back to |task_runner|. | 45 // posted back to |task_runner|. |
| 52 void Start(scoped_refptr<base::SequencedTaskRunner> task_runner); | 46 void Start(Delegate* delegate, |
| 53 | 47 const std::string& image_data, |
| 54 const std::vector<unsigned char>& get_image_data() const { | 48 ImageCodec image_codec, |
| 55 return image_data_; | 49 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 56 } | 50 bool shrink_to_fit); |
| 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 | 51 |
| 61 private: | 52 private: |
| 62 // It's a reference counted object, so destructor is private. | 53 friend struct base::DefaultLazyInstanceTraits<ImageDecoder>; |
| 63 ~ImageDecoder() override; | 54 ImageDecoder(); |
| 55 ~ImageDecoder(); | |
| 64 | 56 |
| 65 // Overidden from UtilityProcessHostClient: | 57 class ImageDecoderImpl : public content::UtilityProcessHostClient { |
| 66 bool OnMessageReceived(const IPC::Message& message) override; | 58 public: |
| 59 ImageDecoderImpl(); | |
| 60 void Start(Delegate* delegate, | |
| 61 const std::string& image_data, | |
| 62 ImageCodec image_codec, | |
| 63 scoped_refptr<base::SequencedTaskRunner> task_runner, | |
| 64 bool shrink_to_fit); | |
| 67 | 65 |
| 68 // IPC message handlers. | 66 private: |
| 69 void OnDecodeImageSucceeded(const SkBitmap& decoded_image); | 67 // It's a reference counted object, so destructor is private. |
| 70 void OnDecodeImageFailed(); | 68 ~ImageDecoderImpl() override; |
| 71 | 69 |
| 72 // Launches sandboxed process that will decode the image. | 70 // Contains attributes we need to know about each image decode |
| 73 void DecodeImageInSandbox(const std::vector<unsigned char>& image_data); | 71 // request we send. |
| 72 struct RequestInfo { | |
| 73 RequestInfo(Delegate* delegate, | |
| 74 scoped_refptr<base::SequencedTaskRunner> task_runner); | |
| 75 ~RequestInfo(); | |
| 74 | 76 |
| 75 Delegate* delegate_; | 77 Delegate* delegate; |
| 76 std::vector<unsigned char> image_data_; | 78 scoped_refptr<base::SequencedTaskRunner> task_runner; |
| 77 const ImageCodec image_codec_; | 79 }; |
| 78 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 80 |
| 79 bool shrink_to_fit_; // if needed for IPC msg size limit | 81 typedef std::map<int, RequestInfo*> RequestMap; |
|
dcheng
2015/03/10 15:49:12
I have the vague impression that using foo = bar;
Theresa
2015/03/10 23:50:54
Acknowledged.
| |
| 82 | |
| 83 // Starts UtilityProcessHost in batch mode and calls | |
| 84 // PostDelayedStopBatchModeTask. | |
| 85 void StartBatchMode(Delegate* delegate, | |
| 86 const std::string& image_data, | |
| 87 ImageCodec image_codec, | |
| 88 bool shrink_to_fit); | |
| 89 | |
| 90 // Posts a delayed task to stop the UtilityProcessHost running in | |
| 91 // batch mode. | |
| 92 void PostDelayedStopBatchModeTask(); | |
| 93 | |
| 94 // Stops batch mode if no requests have come in since | |
| 95 // |batch_mode_timeout_seconds_|; else calls PostDelayedStopBatchModeTask. | |
| 96 void StopBatchMode(); | |
| 97 | |
| 98 // Overidden from UtilityProcessHostClient. | |
| 99 bool OnMessageReceived(const IPC::Message& message) override; | |
| 100 | |
| 101 // IPC message handlers. | |
| 102 void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int id); | |
| 103 void OnDecodeImageFailed(int id); | |
| 104 | |
| 105 void NotifyDelegateDecodeSucceeded(const SkBitmap& decoded_image, | |
| 106 Delegate* delegate); | |
| 107 void NotifyDelegateDecodeFailed(Delegate* delegate); | |
| 108 | |
| 109 // Sends a request to the sandboxed process to decode the image. | |
| 110 void DecodeImageInSandbox(Delegate* delegate, | |
| 111 const std::vector<unsigned char>& image_data, | |
| 112 ImageCodec image_codec, | |
| 113 bool shrink_to_fit, | |
| 114 int delegate_id); | |
| 115 | |
| 116 // id to use for the next Start request that comes in. | |
| 117 int delegate_id_counter_; | |
| 118 | |
| 119 // Map of request id's to RequestInfo | |
| 120 RequestMap request_info_id_map_; | |
| 121 | |
| 122 // The SequencedTaskRunner provided when Start is called before batch mode | |
| 123 // is started. base::Bind does not accept scoped_refptr objects, so the | |
| 124 // value must be saved instead. | |
|
dcheng
2015/03/10 15:49:12
I don't quite follow this comment. base::Bind() do
Theresa
2015/03/10 23:50:54
If I recall correctly, I was seeing this message (
| |
| 125 scoped_refptr<base::SequencedTaskRunner> task_runner_at_start_; | |
| 126 | |
| 127 // The UtilityProcessHost requests are sent to. | |
| 128 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; | |
| 129 | |
| 130 // The time Start was last called. | |
| 131 base::TimeTicks last_request_; | |
| 132 | |
| 133 // How many seconds to wait after the last request has been received | |
| 134 // before ending batch mode. | |
| 135 const int kBatchModeTimeoutSeconds = 5; | |
| 136 | |
| 137 // True iff utility_process_host_ has been created and started in | |
| 138 // batch bode. | |
| 139 bool batch_mode_started_; | |
| 140 }; | |
| 141 | |
| 142 const scoped_refptr<ImageDecoderImpl> image_decoder_impl_; | |
| 80 | 143 |
| 81 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); | 144 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); |
| 82 }; | 145 }; |
| 83 | 146 |
| 84 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ | 147 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ |
| OLD | NEW |