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 |
19 class ImageDecoder : public content::UtilityProcessHostClient { | 24 // use this, call ImageDecoder::Start(...) on any thread. |
25 // | |
26 // Internally, all of the work happens on the IO thread, and then | |
27 // the result (ImageRequest::OnImageDecoded or | |
28 // ImageRequest::OnDecodeImageFailed) is posted back to the task runner | |
29 // specified when Start(...) was called. | |
30 class ImageDecoder { | |
20 public: | 31 public: |
21 class Delegate { | 32 class ImageRequest { |
22 public: | 33 public: |
23 // Called when image is decoded. | 34 // Called when image is decoded. |
24 // |decoder| is used to identify the image in case of decoding several | 35 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 | 36 |
29 // Called when decoding image failed. Delegate can do some cleanup in | 37 // Called when decoding image failed. ImageRequest can do some cleanup in |
30 // this handler. | 38 // this handler. |
31 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) {} | 39 virtual void OnDecodeImageFailed() {} |
40 | |
41 base::SequencedTaskRunner* task_runner() const { return t.get(); } | |
32 | 42 |
33 protected: | 43 protected: |
34 virtual ~Delegate() {} | 44 explicit ImageRequest(const scoped_refptr<base::SequencedTaskRunner>& t); |
45 virtual ~ImageRequest(); | |
46 | |
47 private: | |
48 const scoped_refptr<base::SequencedTaskRunner> t; | |
dcheng
2015/03/23 11:52:09
Members should definitely have a descriptive name
Theresa
2015/03/23 17:33:10
Done.
| |
35 }; | 49 }; |
36 | 50 |
37 enum ImageCodec { | 51 enum ImageCodec { |
38 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). | 52 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). |
39 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. | 53 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. |
40 }; | 54 }; |
41 | 55 |
42 ImageDecoder(Delegate* delegate, | |
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 | |
50 // Starts asynchronous image decoding. Once finished, the callback will be | 56 // Starts asynchronous image decoding. Once finished, the callback will be |
51 // posted back to |task_runner|. | 57 // posted back to |task_runner|. |
52 void Start(scoped_refptr<base::SequencedTaskRunner> task_runner); | 58 static void Start(ImageRequest* image_request, |
59 const std::string& image_data, | |
60 ImageCodec image_codec, | |
61 bool shrink_to_fit); | |
53 | 62 |
54 const std::vector<unsigned char>& get_image_data() const { | 63 // Removes all instances of image_request from the IDMap, ensuring callbacks |
Theresa
2015/03/20 19:09:01
Note for self: update this comment to no longer re
Theresa
2015/03/23 17:33:10
Done.
| |
55 return image_data_; | 64 // are not made to the image_request after it is destroyed. |
56 } | 65 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 | 66 |
61 private: | 67 private: |
62 // It's a reference counted object, so destructor is private. | 68 friend struct base::DefaultLazyInstanceTraits<ImageDecoder>; |
63 ~ImageDecoder() override; | 69 static ImageDecoder* GetInstance(); |
dcheng
2015/03/23 11:52:09
This should be unnecessary to expose in the header
Theresa
2015/03/23 17:33:10
I think it still needs to be declared here before
| |
70 ImageDecoder(); | |
71 ~ImageDecoder(); | |
64 | 72 |
65 // Overidden from UtilityProcessHostClient: | 73 class ImageDecoderImpl : public content::UtilityProcessHostClient { |
66 bool OnMessageReceived(const IPC::Message& message) override; | 74 public: |
75 ImageDecoderImpl(); | |
67 | 76 |
68 // IPC message handlers. | 77 // Sends a request to the sandboxed process to decode the image. Starts |
69 void OnDecodeImageSucceeded(const SkBitmap& decoded_image); | 78 // batch mode if necessary. |
70 void OnDecodeImageFailed(); | 79 void DecodeImageInSandbox(ImageRequest* image_request, |
80 const std::vector<unsigned char>& image_data, | |
81 ImageCodec image_codec, | |
82 bool shrink_to_fit); | |
71 | 83 |
72 // Launches sandboxed process that will decode the image. | 84 void Cancel(ImageRequest* image_request); |
73 void DecodeImageInSandbox(const std::vector<unsigned char>& image_data); | |
74 | 85 |
75 Delegate* delegate_; | 86 private: |
76 std::vector<unsigned char> image_data_; | 87 using RequestMap = std::map<int, ImageRequest*>; |
77 const ImageCodec image_codec_; | 88 |
78 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 89 // It's a reference counted object, so destructor is private. |
79 bool shrink_to_fit_; // if needed for IPC msg size limit | 90 ~ImageDecoderImpl() override; |
91 | |
92 // Starts UtilityProcessHost in batch mode and starts batch_mode_timer_. | |
93 void StartBatchMode(); | |
94 | |
95 // Stops batch mode if no requests have come in since kBatchModeTimeout. | |
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 // id to use for the next Start request that comes in. | |
106 int image_request_id_counter_; | |
107 | |
108 // Map of request id's to ImageRequests. | |
109 RequestMap image_request_id_map_; | |
110 | |
111 // Protects image_request_id_map_; | |
112 base::Lock map_lock_; | |
dcheng
2015/03/23 11:52:09
This needs to protect the counter as well, since S
Theresa
2015/03/23 17:33:10
Done.
| |
113 | |
114 // The UtilityProcessHost requests are sent to. | |
115 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; | |
116 | |
117 // Calls StopBatchMode after kBatchModeTimeout has elapsed. | |
118 base::RepeatingTimer<ImageDecoderImpl> batch_mode_timer_; | |
119 | |
120 // The time Start was last called. | |
121 base::TimeTicks last_request_; | |
122 | |
123 // How long to wait after the last request has been received before ending | |
124 // batch mode. | |
125 const base::TimeDelta kBatchModeTimeout = base::TimeDelta::FromSeconds(5); | |
dcheng
2015/03/23 11:52:09
Just move this constant into the .cc, since it's a
Theresa
2015/03/23 17:33:10
Moved. I don't think it introduced a static initia
dcheng
2015/03/24 05:18:18
I've confirmed locally that this does introduce a
Theresa
2015/03/24 23:51:42
If it's in image_decoder.cc, do the static intiali
dcheng
2015/03/25 14:10:09
Yeah, it will still create a static initializer =/
Theresa
2015/03/25 17:13:45
Done. Changed back to a const int.
| |
126 | |
127 // True iff utility_process_host_ has been created and started in | |
128 // batch mode. | |
129 bool batch_mode_started_; | |
130 }; | |
131 | |
132 const scoped_refptr<ImageDecoderImpl> image_decoder_impl_; | |
80 | 133 |
81 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); | 134 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); |
82 }; | 135 }; |
83 | 136 |
84 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ | 137 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ |
OLD | NEW |