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 // |decoder| is used to identify the image in case of decoding several |
|
dcheng
2015/02/27 16:47:17
This comment needs to be updated.
Theresa
2015/03/04 03:10:07
Done.
| |
| 25 // images simultaneously. | 28 // images simultaneously. |
| 26 virtual void OnImageDecoded(const ImageDecoder* decoder, | 29 virtual void OnImageDecoded(const SkBitmap& decoded_image) = 0; |
| 27 const SkBitmap& decoded_image) = 0; | |
| 28 | 30 |
| 29 // Called when decoding image failed. Delegate can do some cleanup in | 31 // Called when decoding image failed. Delegate can do some cleanup in |
| 30 // this handler. | 32 // this handler. |
| 31 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) {} | 33 virtual void OnDecodeImageFailed() {} |
| 32 | 34 |
| 33 protected: | 35 protected: |
| 34 virtual ~Delegate() {} | 36 virtual ~Delegate() {} |
| 35 }; | 37 }; |
| 36 | 38 |
| 37 enum ImageCodec { | 39 enum ImageCodec { |
| 38 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). | 40 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). |
| 39 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. | 41 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. |
| 40 }; | 42 }; |
| 41 | 43 |
| 42 ImageDecoder(Delegate* delegate, | 44 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 | 45 |
| 50 // Starts asynchronous image decoding. Once finished, the callback will be | 46 // Starts asynchronous image decoding. Once finished, the callback will be |
| 51 // posted back to |task_runner|. | 47 // posted back to |task_runner|. |
| 52 void Start(scoped_refptr<base::SequencedTaskRunner> task_runner); | 48 void Start(Delegate* delegate, |
| 53 | 49 const std::string& image_data, |
| 54 const std::vector<unsigned char>& get_image_data() const { | 50 ImageCodec image_codec, |
| 55 return image_data_; | 51 scoped_refptr<base::SequencedTaskRunner> task_runner, |
|
dcheng
2015/02/27 16:47:18
Just an aside, it's slightly better to pass a scop
Theresa
2015/03/04 03:10:06
Acknowledged.
| |
| 56 } | 52 bool shrink_to_fit = false); |
|
dcheng
2015/02/27 16:47:17
The Google C++ style guide forbids default args =(
Theresa
2015/03/04 03:10:07
Done.
| |
| 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 | 53 |
| 61 private: | 54 private: |
| 62 // It's a reference counted object, so destructor is private. | 55 friend struct base::DefaultLazyInstanceTraits<ImageDecoder>; |
| 63 ~ImageDecoder() override; | 56 ImageDecoder(); |
| 57 ~ImageDecoder(); | |
| 64 | 58 |
| 65 // Overidden from UtilityProcessHostClient: | 59 class ImageDecoderImpl : public content::UtilityProcessHostClient { |
|
dcheng
2015/02/27 16:47:17
I can't say I'm enthused about this delegate. I do
Theresa
2015/03/04 03:10:06
Acknowledged. I agree, but I can't think of any go
| |
| 66 bool OnMessageReceived(const IPC::Message& message) override; | 60 public: |
| 61 ImageDecoderImpl(); | |
| 62 void Start(Delegate* delegate, | |
| 63 const std::string& image_data, | |
| 64 ImageCodec image_codec, | |
| 65 scoped_refptr<base::SequencedTaskRunner> task_runner, | |
| 66 bool shrink_to_fit = false); | |
| 67 | 67 |
| 68 // IPC message handlers. | 68 private: |
| 69 void OnDecodeImageSucceeded(const SkBitmap& decoded_image); | 69 // It's a reference counted object, so destructor is private. |
| 70 void OnDecodeImageFailed(); | 70 ~ImageDecoderImpl() override; |
| 71 | 71 |
| 72 // Launches sandboxed process that will decode the image. | 72 // Starts UtilityProcessHost in batch mode and calls |
| 73 void DecodeImageInSandbox(const std::vector<unsigned char>& image_data); | 73 // PostDelayedStopBatchModeTask. |
| 74 void StartBatchMode(Delegate* delegate, | |
| 75 const std::string& image_data, | |
| 76 ImageCodec image_codec, | |
| 77 bool shrink_to_fit); | |
| 74 | 78 |
| 75 Delegate* delegate_; | 79 // Posts a delayed task to stop the UtilityProcessHost running in |
| 76 std::vector<unsigned char> image_data_; | 80 // batch mode. |
| 77 const ImageCodec image_codec_; | 81 void PostDelayedStopBatchModeTask(); |
| 78 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 82 |
| 79 bool shrink_to_fit_; // if needed for IPC msg size limit | 83 // Stops batch mode if no requests have come in since |
| 84 // |batch_mode_timeout_seconds_|; else calls PostDelayedStopBatchModeTask. | |
| 85 void StopBatchMode(); | |
| 86 | |
| 87 // Overidden from UtilityProcessHostClient. | |
| 88 bool OnMessageReceived(const IPC::Message& message) override; | |
| 89 | |
| 90 // IPC message handlers. | |
| 91 void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int id); | |
| 92 void OnDecodeImageFailed(int id); | |
| 93 | |
| 94 void NotifyDelegateDecodeSucceeded(const SkBitmap& decoded_image, | |
| 95 Delegate* delegate); | |
| 96 void NotifyDelegateDecodeFailed(Delegate* delegate); | |
| 97 | |
| 98 // Sends a request to the sandboxed process to decode the image. | |
| 99 void DecodeImageInSandbox(Delegate* delegate, | |
| 100 const std::vector<unsigned char>& image_data, | |
| 101 ImageCodec image_codec, | |
| 102 bool shrink_to_fit, | |
| 103 int delegate_id); | |
| 104 | |
| 105 // id to use for the next Start request that comes in. | |
| 106 int delegate_id_counter_ = 0; | |
|
dcheng
2015/02/27 16:47:17
Warning: personal preference. I prefer not to use
Theresa
2015/03/04 03:10:07
Done.
| |
| 107 | |
| 108 // Map of request id -> Delegate* | |
| 109 std::map<int, Delegate*> delegate_map_; | |
| 110 | |
| 111 // Map of request id -> SequencedTaskRunner | |
| 112 std::map<int, scoped_refptr<base::SequencedTaskRunner>> task_runner_map_; | |
| 113 | |
| 114 // The SequencedTaskRunner provided when Start is called before batch mode | |
| 115 // is started. base::Bind does not accept scoped_refptr objects, so the | |
| 116 // value must be saved instead. | |
| 117 scoped_refptr<base::SequencedTaskRunner> task_runner_at_start_; | |
| 118 | |
| 119 // The UtilityProcessHost requests are sent to. | |
| 120 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; | |
| 121 | |
| 122 // The time Start was last called. | |
| 123 time_t last_request_ = 0; | |
|
dcheng
2015/02/27 16:47:17
This should probably be base::TimeTicks.
Theresa
2015/03/04 03:10:07
Done.
| |
| 124 | |
| 125 // How many seconds to wait after the last request has been received | |
| 126 // before ending batch mode. | |
| 127 const int batch_mode_timeout_seconds_ = 5; | |
|
dcheng
2015/02/27 16:47:18
kBatchModeTimeoutSeconds
Theresa
2015/03/04 03:10:07
Done.
| |
| 128 | |
| 129 // True iff utility_process_host_ has been created and started in | |
| 130 // batch bode. | |
| 131 bool batch_mode_started_ = false; | |
| 132 }; | |
| 133 | |
| 134 scoped_refptr<ImageDecoderImpl> image_decoder_impl_; | |
| 80 | 135 |
| 81 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); | 136 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); |
| 82 }; | 137 }; |
| 83 | 138 |
| 84 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ | 139 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ |
| OLD | NEW |