| 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/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/weak_ptr.h" |
| 15 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 16 #include "base/threading/sequenced_worker_pool.h" | 17 #include "base/threading/sequenced_worker_pool.h" |
| 17 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
| 18 #include "content/public/browser/utility_process_host.h" | 19 #include "content/public/browser/utility_process_host.h" |
| 19 #include "content/public/browser/utility_process_host_client.h" | 20 #include "content/public/browser/utility_process_host_client.h" |
| 20 | 21 |
| 21 class SkBitmap; | 22 class SkBitmap; |
| 22 | 23 |
| 23 // This is a helper class for decoding images safely in a utility process. To | 24 // This is a helper class for decoding images safely in a utility process. To |
| 24 // use this, call ImageDecoder::Start(...) or | 25 // use this, call ImageDecoder::Start(...) or |
| (...skipping 13 matching lines...) Expand all Loading... |
| 38 virtual void OnImageDecoded(const SkBitmap& decoded_image) = 0; | 39 virtual void OnImageDecoded(const SkBitmap& decoded_image) = 0; |
| 39 | 40 |
| 40 // Called when decoding image failed. ImageRequest can do some cleanup in | 41 // Called when decoding image failed. ImageRequest can do some cleanup in |
| 41 // this handler. | 42 // this handler. |
| 42 virtual void OnDecodeImageFailed() {} | 43 virtual void OnDecodeImageFailed() {} |
| 43 | 44 |
| 44 base::SequencedTaskRunner* task_runner() const { | 45 base::SequencedTaskRunner* task_runner() const { |
| 45 return task_runner_.get(); | 46 return task_runner_.get(); |
| 46 } | 47 } |
| 47 | 48 |
| 49 base::WeakPtr<ImageRequest> GetWeakPtr(); |
| 50 |
| 48 protected: | 51 protected: |
| 49 explicit ImageRequest( | 52 explicit ImageRequest( |
| 50 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 53 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
| 51 virtual ~ImageRequest(); | 54 virtual ~ImageRequest(); |
| 52 | 55 |
| 53 private: | 56 private: |
| 54 // The thread to post OnImageDecoded or OnDecodeImageFailed once the | 57 // The thread to post OnImageDecoded or OnDecodeImageFailed once the |
| 55 // the image has been decoded. | 58 // the image has been decoded. |
| 56 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 59 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 60 |
| 61 base::WeakPtrFactory<ImageRequest> weak_factory_; |
| 57 }; | 62 }; |
| 58 | 63 |
| 59 enum ImageCodec { | 64 enum ImageCodec { |
| 60 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). | 65 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). |
| 61 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. | 66 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. |
| 62 }; | 67 }; |
| 63 | 68 |
| 64 // Calls StartWithOptions with ImageCodec::DEFAULT_CODEC and | 69 // Calls StartWithOptions with ImageCodec::DEFAULT_CODEC and |
| 65 // shrink_to_fit = false. | 70 // shrink_to_fit = false. |
| 66 static void Start(ImageRequest* image_request, | 71 static void Start(ImageRequest* image_request, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 80 private: | 85 private: |
| 81 friend struct base::DefaultLazyInstanceTraits<ImageDecoder>; | 86 friend struct base::DefaultLazyInstanceTraits<ImageDecoder>; |
| 82 | 87 |
| 83 ImageDecoder(); | 88 ImageDecoder(); |
| 84 // It's a reference counted object, so destructor is private. | 89 // It's a reference counted object, so destructor is private. |
| 85 ~ImageDecoder() override; | 90 ~ImageDecoder() override; |
| 86 | 91 |
| 87 // Sends a request to the sandboxed process to decode the image. Starts | 92 // Sends a request to the sandboxed process to decode the image. Starts |
| 88 // batch mode if necessary. If the utility process fails to start, | 93 // batch mode if necessary. If the utility process fails to start, |
| 89 // an OnDecodeImageFailed task is posted to image_request's |task_runner_|. | 94 // an OnDecodeImageFailed task is posted to image_request's |task_runner_|. |
| 90 void DecodeImageInSandbox(ImageRequest* image_request, | 95 void DecodeImageInSandbox(int request_id, |
| 91 const std::vector<unsigned char>& image_data, | 96 const std::vector<unsigned char>& image_data, |
| 92 ImageCodec image_codec, | 97 ImageCodec image_codec, |
| 93 bool shrink_to_fit); | 98 bool shrink_to_fit); |
| 94 | 99 |
| 100 void StartWithOptionsImpl(ImageRequest* image_request, |
| 101 const std::string& image_data, |
| 102 ImageCodec image_codec, |
| 103 bool shrink_to_fit); |
| 95 void CancelImpl(ImageRequest* image_request); | 104 void CancelImpl(ImageRequest* image_request); |
| 96 | 105 |
| 97 using RequestMap = std::map<int, ImageRequest*>; | 106 using RequestMap = |
| 107 std::map<int, |
| 108 std::pair<scoped_refptr<base::SequencedTaskRunner>, |
| 109 base::WeakPtr<ImageRequest>>>; |
| 98 | 110 |
| 99 // Starts UtilityProcessHost in batch mode and starts |batch_mode_timer_|. | 111 // Starts UtilityProcessHost in batch mode and starts |batch_mode_timer_|. |
| 100 // If the utility process fails to start, the method resets | 112 // If the utility process fails to start, the method resets |
| 101 // |utility_process_host_| and returns. | 113 // |utility_process_host_| and returns. |
| 102 void StartBatchMode(); | 114 void StartBatchMode(); |
| 103 | 115 |
| 104 // Stops batch mode if no requests have come in since | 116 // Stops batch mode if no requests have come in since |
| 105 // kBatchModeTimeoutSeconds. | 117 // kBatchModeTimeoutSeconds. |
| 106 void StopBatchMode(); | 118 void StopBatchMode(); |
| 107 | 119 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 127 // Calls StopBatchMode after kBatchModeTimeoutSeconds have elapsed. | 139 // Calls StopBatchMode after kBatchModeTimeoutSeconds have elapsed. |
| 128 base::RepeatingTimer<ImageDecoder> batch_mode_timer_; | 140 base::RepeatingTimer<ImageDecoder> batch_mode_timer_; |
| 129 | 141 |
| 130 // The time Start was last called. | 142 // The time Start was last called. |
| 131 base::TimeTicks last_request_; | 143 base::TimeTicks last_request_; |
| 132 | 144 |
| 133 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); | 145 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); |
| 134 }; | 146 }; |
| 135 | 147 |
| 136 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ | 148 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ |
| OLD | NEW |