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

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: Small changes from last review 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/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
24 // use this, call ImageDecoder::Start(...) on any thread.
25 //
26 // Internally, all of the work happens on the IO thread, and then
Lei Zhang 2015/03/25 21:44:46 From just looking at the header file, it's not obv
Theresa 2015/03/25 23:13:15 Updated the comment. Cancel() doesn't run on the I
27 // the result (ImageRequest::OnImageDecoded or
Lei Zhang 2015/03/25 21:44:46 This part is a bit confusing - OnImageDecoded is n
Theresa 2015/03/25 23:13:15 I changed "result" to "callback"
28 // ImageRequest::OnDecodeImageFailed) is posted back to the task runner
29 // specified when Start(...) was called.
19 class ImageDecoder : public content::UtilityProcessHostClient { 30 class ImageDecoder : public content::UtilityProcessHostClient {
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 {
Lei Zhang 2015/03/25 21:44:46 Does this need to be public?
Theresa 2015/03/25 23:13:15 ImageDecoder needs to be able to call it, and it's
42 return task_runner_.get();
43 }
32 44
33 protected: 45 protected:
34 virtual ~Delegate() {} 46 explicit ImageRequest(
47 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
48 virtual ~ImageRequest();
49
50 private:
51 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
Lei Zhang 2015/03/25 21:44:46 Document this, mention it's the thread to post bac
Theresa 2015/03/25 23:13:15 Done.
35 }; 52 };
36 53
37 enum ImageCodec { 54 enum ImageCodec {
38 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage). 55 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage).
39 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec. 56 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec.
40 }; 57 };
41 58
42 ImageDecoder(Delegate* delegate, 59 // Starts asynchronous image decoding. Once finished, the callback will be
43 const std::string& image_data, 60 // posted back to image_request's task_runner_.
Lei Zhang 2015/03/25 21:44:46 nit: |task_runner|, ditto below.
Theresa 2015/03/25 23:13:15 Is the nit to rename task_runner_ to task_runner (
Lei Zhang 2015/03/25 23:28:38 Refer to variables as |variable|.
Theresa 2015/03/25 23:54:08 Done.
44 ImageCodec image_codec); 61 static void Start(ImageRequest* image_request,
Lei Zhang 2015/03/25 21:44:46 Since (ImageDecoder::DEFAULT_CODEC, false) are the
Theresa 2015/03/25 23:13:15 Done.
62 const std::string& image_data,
63 ImageCodec image_codec,
64 bool shrink_to_fit);
45 65
46 ImageDecoder(Delegate* delegate, 66 // Removes all instances of image_request from image_request_id_map_,
47 const std::vector<char>& image_data, 67 // ensuring callbacks are not made to the image_request after it is destroyed.
48 ImageCodec image_codec); 68 static void Cancel(ImageRequest* image_request);
49
50 // Starts asynchronous image decoding. Once finished, the callback will be
51 // posted back to |task_runner|.
52 void Start(scoped_refptr<base::SequencedTaskRunner> task_runner);
53
54 const std::vector<unsigned char>& get_image_data() const {
55 return image_data_;
56 }
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 69
61 private: 70 private:
71 friend struct base::DefaultLazyInstanceTraits<ImageDecoder>;
Lei Zhang 2015/03/25 21:44:46 nit: blank line after this
Theresa 2015/03/25 23:13:15 Done.
72 ImageDecoder();
62 // It's a reference counted object, so destructor is private. 73 // It's a reference counted object, so destructor is private.
63 ~ImageDecoder() override; 74 ~ImageDecoder() override;
64 75
65 // Overidden from UtilityProcessHostClient: 76 // Sends a request to the sandboxed process to decode the image. Starts
77 // batch mode if necessary.
78 void DecodeImageInSandbox(ImageRequest* image_request,
79 const std::vector<unsigned char>& image_data,
80 ImageCodec image_codec,
81 bool shrink_to_fit);
82
83 void CancelImpl(ImageRequest* image_request);
84
85 using RequestMap = std::map<int, ImageRequest*>;
Lei Zhang 2015/03/25 21:44:46 Is this the new preferred method vs. using a typed
Theresa 2015/03/25 23:13:15 Yes, unless the header needs to be compatible with
86
87 // Starts UtilityProcessHost in batch mode and starts batch_mode_timer_.
88 void StartBatchMode();
89
90 // Stops batch mode if no requests have come in since kBatchModeTimeout.
91 void StopBatchMode();
92
93 // Overidden from UtilityProcessHostClient.
66 bool OnMessageReceived(const IPC::Message& message) override; 94 bool OnMessageReceived(const IPC::Message& message) override;
67 95
68 // IPC message handlers. 96 // IPC message handlers.
69 void OnDecodeImageSucceeded(const SkBitmap& decoded_image); 97 void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int id);
Lei Zhang 2015/03/25 21:44:46 nit: id -> request_id?
Theresa 2015/03/25 23:13:15 Done.
70 void OnDecodeImageFailed(); 98 void OnDecodeImageFailed(int id);
71 99
72 // Launches sandboxed process that will decode the image. 100 // id to use for the next Start request that comes in.
73 void DecodeImageInSandbox(const std::vector<unsigned char>& image_data); 101 int image_request_id_counter_;
74 102
75 Delegate* delegate_; 103 // Map of request id's to ImageRequests.
76 std::vector<unsigned char> image_data_; 104 RequestMap image_request_id_map_;
77 const ImageCodec image_codec_; 105
78 scoped_refptr<base::SequencedTaskRunner> task_runner_; 106 // Protects image_request_id_map_ and image_request_id_counter_.
79 bool shrink_to_fit_; // if needed for IPC msg size limit 107 base::Lock map_lock_;
108
109 // The UtilityProcessHost requests are sent to.
110 base::WeakPtr<content::UtilityProcessHost> utility_process_host_;
111
112 // Calls StopBatchMode after kBatchModeTimeout has elapsed.
113 base::RepeatingTimer<ImageDecoder> batch_mode_timer_;
114
115 // The time Start was last called.
116 base::TimeTicks last_request_;
117
118 // How long to wait after the last request has been received before ending
119 // batch mode.
120 const int kBatchModeTimeoutSeconds = 5;
Lei Zhang 2015/03/25 21:44:46 Can this just go into the .cc file?
Theresa 2015/03/25 23:13:15 Done.
80 121
81 DISALLOW_COPY_AND_ASSIGN(ImageDecoder); 122 DISALLOW_COPY_AND_ASSIGN(ImageDecoder);
82 }; 123 };
83 124
84 #endif // CHROME_BROWSER_IMAGE_DECODER_H_ 125 #endif // CHROME_BROWSER_IMAGE_DECODER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698