Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Side by Side Diff: chrome/browser/image_decoder.h

Issue 931993002: Make image_decoder a Leaky LazyInstance (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactor webstore_install_helper to use ImageDecoder, remove DecodeBase64 support Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/id_map.h"
14 #include "base/lazy_instance.h"
12 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.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::GetInstance()->Start(...) on any thread.
25 //
26 // Internally, all of the work happens on the IO thread, and then
27 // the result (Delegate::OnImageDecoded or Delegate::OnDecodeImageFailed) is
28 // posted back to the task runner specified when Start(...) was called.
29 class ImageDecoder {
20 public: 30 public:
21 class Delegate { 31 class Delegate {
22 public: 32 public:
23 // Called when image is decoded. 33 // Called when image is decoded.
24 // |decoder| is used to identify the image in case of decoding several 34 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 35
29 // Called when decoding image failed. Delegate can do some cleanup in 36 // Called when decoding image failed. Delegate can do some cleanup in
30 // this handler. 37 // this handler.
31 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) {} 38 virtual void OnDecodeImageFailed() {}
32 39
33 protected: 40 protected:
34 virtual ~Delegate() {} 41 virtual ~Delegate() {}
35 }; 42 };
36 43
37 enum ImageCodec { 44 enum ImageCodec {
38 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). 45 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage).
39 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. 46 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec.
40 }; 47 };
41 48
42 ImageDecoder(Delegate* delegate, 49 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 50
50 // Starts asynchronous image decoding. Once finished, the callback will be 51 // Starts asynchronous image decoding. Once finished, the callback will be
51 // posted back to |task_runner|. 52 // posted back to |task_runner|.
52 void Start(scoped_refptr<base::SequencedTaskRunner> task_runner); 53 void Start(Delegate* delegate,
53 54 const std::string& image_data,
54 const std::vector<unsigned char>& get_image_data() const { 55 ImageCodec image_codec,
55 return image_data_; 56 scoped_refptr<base::SequencedTaskRunner> task_runner,
56 } 57 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 58
61 private: 59 private:
62 // It's a reference counted object, so destructor is private. 60 friend struct base::DefaultLazyInstanceTraits<ImageDecoder>;
63 ~ImageDecoder() override; 61 ImageDecoder();
62 ~ImageDecoder();
64 63
65 // Overidden from UtilityProcessHostClient: 64 class ImageDecoderImpl : public content::UtilityProcessHostClient {
66 bool OnMessageReceived(const IPC::Message& message) override; 65 public:
66 ImageDecoderImpl();
67 67
68 // IPC message handlers. 68 // Sends a request to the sandboxed process to decode the image. Starts
69 void OnDecodeImageSucceeded(const SkBitmap& decoded_image); 69 // batch mode if necessary.
70 void OnDecodeImageFailed(); 70 void DecodeImageInSandbox(
71 Delegate* delegate,
72 const std::vector<unsigned char>& image_data,
73 ImageCodec image_codec,
74 scoped_refptr<base::SequencedTaskRunner> task_runner,
75 bool shrink_to_fit);
71 76
72 // Launches sandboxed process that will decode the image. 77 private:
73 void DecodeImageInSandbox(const std::vector<unsigned char>& image_data); 78 // It's a reference counted object, so destructor is private.
79 ~ImageDecoderImpl() override;
74 80
75 Delegate* delegate_; 81 // Contains attributes we need to know about each image decode
76 std::vector<unsigned char> image_data_; 82 // request we send.
77 const ImageCodec image_codec_; 83 struct RequestInfo {
78 scoped_refptr<base::SequencedTaskRunner> task_runner_; 84 RequestInfo(Delegate* delegate,
79 bool shrink_to_fit_; // if needed for IPC msg size limit 85 scoped_refptr<base::SequencedTaskRunner> task_runner);
86 ~RequestInfo();
87
88 Delegate* delegate;
89 scoped_refptr<base::SequencedTaskRunner> task_runner;
90 };
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 delegate_id_counter_;
107
108 // Map of request id's to RequestInfo.
109 IDMap<RequestInfo> request_info_id_map_;
110
111 // The UtilityProcessHost requests are sent to.
112 base::WeakPtr<content::UtilityProcessHost> utility_process_host_;
113
114 // Calls StopBatchMode after kBatchModeTimeout has elapsed.
115 base::RepeatingTimer<ImageDecoderImpl> batch_mode_timer_;
116
117 // The time Start was last called.
118 base::TimeTicks last_request_;
119
120 // How long to wait after the last request has been received before ending
121 // batch mode.
122 const base::TimeDelta kBatchModeTimeout = base::TimeDelta::FromSeconds(5);
123
124 // True iff utility_process_host_ has been created and started in
125 // batch mode.
126 bool batch_mode_started_;
127 };
128
129 const scoped_refptr<ImageDecoderImpl> image_decoder_impl_;
80 130
81 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); 131 DISALLOW_COPY_AND_ASSIGN(ImageDecoder);
82 }; 132 };
83 133
84 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ 134 #endif // CHROME_BROWSER_IMAGE_DECODER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698