| 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> |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 static void Start(ImageRequest* image_request, | 66 static void Start(ImageRequest* image_request, |
| 67 const std::string& image_data); | 67 const std::string& image_data); |
| 68 | 68 |
| 69 // Starts asynchronous image decoding. Once finished, the callback will be | 69 // Starts asynchronous image decoding. Once finished, the callback will be |
| 70 // posted back to image_request's |task_runner_|. | 70 // posted back to image_request's |task_runner_|. |
| 71 static void StartWithOptions(ImageRequest* image_request, | 71 static void StartWithOptions(ImageRequest* image_request, |
| 72 const std::string& image_data, | 72 const std::string& image_data, |
| 73 ImageCodec image_codec, | 73 ImageCodec image_codec, |
| 74 bool shrink_to_fit); | 74 bool shrink_to_fit); |
| 75 | 75 |
| 76 // Removes all instances of image_request from |image_request_id_map_|, | 76 // Removes all instances of image_request from |job_id_map_|, |
| 77 // ensuring callbacks are not made to the image_request after it is destroyed. | 77 // ensuring callbacks are not made to the image_request after it is destroyed. |
| 78 // May be called from any thread. |
| 79 // NOTE: This may block if called from a different thread than the |
| 80 // ImageRequest's task runner. |
| 78 static void Cancel(ImageRequest* image_request); | 81 static void Cancel(ImageRequest* image_request); |
| 79 | 82 |
| 80 private: | 83 private: |
| 84 // Container for a single decode request. Used to ensure the ImageRequest's |
| 85 // functions are called on the correct task runner, and only when the |
| 86 // ImageRequest still exists. |
| 87 class Job; |
| 88 |
| 81 friend struct base::DefaultLazyInstanceTraits<ImageDecoder>; | 89 friend struct base::DefaultLazyInstanceTraits<ImageDecoder>; |
| 82 | 90 |
| 83 ImageDecoder(); | 91 ImageDecoder(); |
| 84 // It's a reference counted object, so destructor is private. | 92 // It's a reference counted object, so destructor is private. |
| 85 ~ImageDecoder() override; | 93 ~ImageDecoder() override; |
| 86 | 94 |
| 87 // Sends a request to the sandboxed process to decode the image. Starts | 95 // Sends a request to the sandboxed process to decode the image. Starts |
| 88 // batch mode if necessary. If the utility process fails to start, | 96 // batch mode if necessary. If the utility process fails to start, |
| 89 // an OnDecodeImageFailed task is posted to image_request's |task_runner_|. | 97 // an OnDecodeImageFailed task is posted to image_request's |task_runner_|. |
| 90 void DecodeImageInSandbox(ImageRequest* image_request, | 98 void DecodeImageInSandbox(int request_id, |
| 91 const std::vector<unsigned char>& image_data, | 99 const std::vector<unsigned char>& image_data, |
| 92 ImageCodec image_codec, | 100 ImageCodec image_codec, |
| 93 bool shrink_to_fit); | 101 bool shrink_to_fit); |
| 94 | 102 |
| 103 void StartWithOptionsImpl(ImageRequest* image_request, |
| 104 const std::string& image_data, |
| 105 ImageCodec image_codec, |
| 106 bool shrink_to_fit); |
| 95 void CancelImpl(ImageRequest* image_request); | 107 void CancelImpl(ImageRequest* image_request); |
| 96 | 108 |
| 97 using RequestMap = std::map<int, ImageRequest*>; | 109 using JobMap = std::map<int, scoped_refptr<Job>>; |
| 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 |
| 108 // Overidden from UtilityProcessHostClient. | 120 // Overidden from UtilityProcessHostClient. |
| 109 bool OnMessageReceived(const IPC::Message& message) override; | 121 bool OnMessageReceived(const IPC::Message& message) override; |
| 110 | 122 |
| 111 // IPC message handlers. | 123 // IPC message handlers. |
| 112 void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int request_id); | 124 void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int request_id); |
| 113 void OnDecodeImageFailed(int request_id); | 125 void OnDecodeImageFailed(int request_id); |
| 114 | 126 |
| 127 // Removes the job from the map of requests. |
| 128 void RemoveJob(const scoped_refptr<Job>& job); |
| 129 |
| 115 // id to use for the next Start request that comes in. | 130 // id to use for the next Start request that comes in. |
| 116 int image_request_id_counter_; | 131 int image_request_id_counter_; |
| 117 | 132 |
| 118 // Map of request id's to ImageRequests. | 133 // Map of request id's to Jobs. |
| 119 RequestMap image_request_id_map_; | 134 JobMap job_id_map_; |
| 120 | 135 |
| 121 // Protects |image_request_id_map_| and |image_request_id_counter_|. | 136 // Protects |job_id_map_| and |image_request_id_counter_|. |
| 122 base::Lock map_lock_; | 137 base::Lock map_lock_; |
| 123 | 138 |
| 124 // The UtilityProcessHost requests are sent to. | 139 // The UtilityProcessHost requests are sent to. |
| 125 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; | 140 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; |
| 126 | 141 |
| 127 // Calls StopBatchMode after kBatchModeTimeoutSeconds have elapsed. | 142 // Calls StopBatchMode after kBatchModeTimeoutSeconds have elapsed. |
| 128 base::RepeatingTimer<ImageDecoder> batch_mode_timer_; | 143 base::RepeatingTimer<ImageDecoder> batch_mode_timer_; |
| 129 | 144 |
| 130 // The time Start was last called. | 145 // The time Start was last called. |
| 131 base::TimeTicks last_request_; | 146 base::TimeTicks last_request_; |
| 132 | 147 |
| 133 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); | 148 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); |
| 134 }; | 149 }; |
| 135 | 150 |
| 136 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ | 151 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ |
| OLD | NEW |