blob: 1808eac85003d91af0413fc51a28f35168fb539c [file] [log] [blame]
[email protected]44013f682012-05-31 13:49:401// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]551707a2010-06-16 16:59:472// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]6d33da172011-11-22 03:56:095#ifndef CHROME_BROWSER_IMAGE_DECODER_H_
6#define CHROME_BROWSER_IMAGE_DECODER_H_
[email protected]551707a2010-06-16 16:59:477
twellingtona71414d2015-03-26 15:09:458#include <map>
amistry6f72f042016-04-05 20:42:079#include <memory>
[email protected]928c0e712011-03-16 15:01:5710#include <string>
[email protected]551707a2010-06-16 16:59:4711#include <vector>
12
twellingtona71414d2015-03-26 15:09:4513#include "base/lazy_instance.h"
avi6846aef2015-12-26 01:09:3814#include "base/macros.h"
[email protected]810dddf52013-01-25 19:37:2615#include "base/memory/ref_counted.h"
thestigc5eeb9f362015-04-22 18:42:3216#include "base/sequence_checker.h"
17#include "base/sequenced_task_runner.h"
twellingtona71414d2015-03-26 15:09:4518#include "base/synchronization/lock.h"
twellingtona71414d2015-03-26 15:09:4519#include "base/timer/timer.h"
avi6846aef2015-12-26 01:09:3820#include "build/build_config.h"
amistry6f72f042016-04-05 20:42:0721#include "chrome/common/image_decoder.mojom.h"
twellingtona71414d2015-03-26 15:09:4522#include "content/public/browser/utility_process_host.h"
[email protected]c4f883a2012-02-03 17:02:0723#include "content/public/browser/utility_process_host_client.h"
[email protected]373c1062011-06-09 21:11:5124
25class SkBitmap;
[email protected]551707a2010-06-16 16:59:4726
twellingtona71414d2015-03-26 15:09:4527// This is a helper class for decoding images safely in a utility process. To
28// use this, call ImageDecoder::Start(...) or
29// ImageDecoder::StartWithOptions(...) on any thread.
30//
31// Internally, most of the work happens on the IO thread, and then
32// the callback (ImageRequest::OnImageDecoded or
33// ImageRequest::OnDecodeImageFailed) is posted back to the |task_runner_|
34// associated with the ImageRequest.
35// The Cancel() method runs on whichever thread called it. |map_lock_| is used
36// to protect the data that is accessed from multiple threads.
[email protected]c4f883a2012-02-03 17:02:0737class ImageDecoder : public content::UtilityProcessHostClient {
[email protected]551707a2010-06-16 16:59:4738 public:
thestigc5eeb9f362015-04-22 18:42:3239 // ImageRequest objects needs to be created and destroyed on the same
40 // SequencedTaskRunner.
twellingtona71414d2015-03-26 15:09:4541 class ImageRequest {
[email protected]551707a2010-06-16 16:59:4742 public:
43 // Called when image is decoded.
twellingtona71414d2015-03-26 15:09:4544 virtual void OnImageDecoded(const SkBitmap& decoded_image) = 0;
[email protected]928c0e712011-03-16 15:01:5745
twellingtona71414d2015-03-26 15:09:4546 // Called when decoding image failed. ImageRequest can do some cleanup in
[email protected]928c0e712011-03-16 15:01:5747 // this handler.
twellingtona71414d2015-03-26 15:09:4548 virtual void OnDecodeImageFailed() {}
49
50 base::SequencedTaskRunner* task_runner() const {
51 return task_runner_.get();
52 }
[email protected]551707a2010-06-16 16:59:4753
54 protected:
thestigc5eeb9f362015-04-22 18:42:3255 // Creates an ImageRequest that runs on the thread creating it.
56 ImageRequest();
57 // Explicitly pass in |task_runner| if the current thread is part of a
58 // thread pool.
twellingtona71414d2015-03-26 15:09:4559 explicit ImageRequest(
60 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
61 virtual ~ImageRequest();
62
63 private:
thestigc5eeb9f362015-04-22 18:42:3264 // The thread to post OnImageDecoded() or OnDecodeImageFailed() once the
twellingtona71414d2015-03-26 15:09:4565 // the image has been decoded.
66 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
thestigc5eeb9f362015-04-22 18:42:3267
68 base::SequenceChecker sequence_checker_;
[email protected]551707a2010-06-16 16:59:4769 };
70
[email protected]11f16d102012-08-29 23:12:1571 enum ImageCodec {
72 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage).
edward.baker199f66b2015-05-07 19:13:3073#if defined(OS_CHROMEOS)
[email protected]11f16d102012-08-29 23:12:1574 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec.
satoruxf7e76f02016-03-09 06:30:4575 ROBUST_PNG_CODEC, // Restrict decoding to robust PNG codec.
edward.baker199f66b2015-05-07 19:13:3076#endif // defined(OS_CHROMEOS)
[email protected]11f16d102012-08-29 23:12:1577 };
78
thestigc5eeb9f362015-04-22 18:42:3279 // Calls StartWithOptions() with ImageCodec::DEFAULT_CODEC and
twellingtona71414d2015-03-26 15:09:4580 // shrink_to_fit = false.
81 static void Start(ImageRequest* image_request,
82 const std::string& image_data);
pneubeck93871252015-01-20 11:26:3683
[email protected]cdc8a3e2013-06-12 04:18:1784 // Starts asynchronous image decoding. Once finished, the callback will be
twellingtona71414d2015-03-26 15:09:4585 // posted back to image_request's |task_runner_|.
86 static void StartWithOptions(ImageRequest* image_request,
87 const std::string& image_data,
88 ImageCodec image_codec,
89 bool shrink_to_fit);
[email protected]551707a2010-06-16 16:59:4790
thestigc5eeb9f362015-04-22 18:42:3291 // Removes all instances of |image_request| from |image_request_id_map_|,
twellingtona71414d2015-03-26 15:09:4592 // ensuring callbacks are not made to the image_request after it is destroyed.
93 static void Cancel(ImageRequest* image_request);
[email protected]11b0856d2012-06-13 19:21:5694
[email protected]551707a2010-06-16 16:59:4795 private:
twellingtona71414d2015-03-26 15:09:4596 friend struct base::DefaultLazyInstanceTraits<ImageDecoder>;
97
thestigc5eeb9f362015-04-22 18:42:3298 using RequestMap = std::map<int, ImageRequest*>;
99
twellingtona71414d2015-03-26 15:09:45100 ImageDecoder();
[email protected]551707a2010-06-16 16:59:47101 // It's a reference counted object, so destructor is private.
Daniel Chenga542fca2014-10-21 09:51:29102 ~ImageDecoder() override;
[email protected]551707a2010-06-16 16:59:47103
twellingtona71414d2015-03-26 15:09:45104 // Sends a request to the sandboxed process to decode the image. Starts
twellington612b9b62015-04-01 00:33:12105 // batch mode if necessary. If the utility process fails to start,
106 // an OnDecodeImageFailed task is posted to image_request's |task_runner_|.
thestigc5eeb9f362015-04-22 18:42:32107 void DecodeImageInSandbox(int request_id,
twellingtona71414d2015-03-26 15:09:45108 const std::vector<unsigned char>& image_data,
109 ImageCodec image_codec,
110 bool shrink_to_fit);
111
thestigc5eeb9f362015-04-22 18:42:32112 void StartWithOptionsImpl(ImageRequest* image_request,
113 const std::string& image_data,
114 ImageCodec image_codec,
115 bool shrink_to_fit);
twellingtona71414d2015-03-26 15:09:45116 void CancelImpl(ImageRequest* image_request);
117
twellingtona71414d2015-03-26 15:09:45118 // Starts UtilityProcessHost in batch mode and starts |batch_mode_timer_|.
twellington612b9b62015-04-01 00:33:12119 // If the utility process fails to start, the method resets
120 // |utility_process_host_| and returns.
twellingtona71414d2015-03-26 15:09:45121 void StartBatchMode();
122
123 // Stops batch mode if no requests have come in since
thestigc5eeb9f362015-04-22 18:42:32124 // |kBatchModeTimeoutSeconds|.
twellingtona71414d2015-03-26 15:09:45125 void StopBatchMode();
126
amistryd59fe6b2015-04-30 23:50:30127 // Fails all outstanding requests.
128 void FailAllRequests();
129
twellingtona71414d2015-03-26 15:09:45130 // Overidden from UtilityProcessHostClient.
amistryd59fe6b2015-04-30 23:50:30131 void OnProcessCrashed(int exit_code) override;
wfh3adf87d2016-05-03 23:26:06132 void OnProcessLaunchFailed(int error_code) override;
Daniel Chenga542fca2014-10-21 09:51:29133 bool OnMessageReceived(const IPC::Message& message) override;
[email protected]373c1062011-06-09 21:11:51134
135 // IPC message handlers.
twellingtona71414d2015-03-26 15:09:45136 void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int request_id);
137 void OnDecodeImageFailed(int request_id);
[email protected]551707a2010-06-16 16:59:47138
thestigc5eeb9f362015-04-22 18:42:32139 // For the ImageRequest identified by |request_id|, call its OnImageDecoded()
140 // or OnDecodeImageFailed() method on its task runner thread.
141 void RunOnImageDecoded(const SkBitmap& decoded_image, int request_id);
142 void RunOnDecodeImageFailed(int request_id);
143
144 // id to use for the next Start() request that comes in.
twellingtona71414d2015-03-26 15:09:45145 int image_request_id_counter_;
[email protected]551707a2010-06-16 16:59:47146
twellingtona71414d2015-03-26 15:09:45147 // Map of request id's to ImageRequests.
148 RequestMap image_request_id_map_;
149
150 // Protects |image_request_id_map_| and |image_request_id_counter_|.
151 base::Lock map_lock_;
152
153 // The UtilityProcessHost requests are sent to.
154 base::WeakPtr<content::UtilityProcessHost> utility_process_host_;
155
thestigd3485b692015-04-30 09:47:32156 // Calls StopBatchMode() after |kBatchModeTimeoutSeconds| have elapsed,
157 // unless a new decoding request resets the timer.
amistry6f72f042016-04-05 20:42:07158 std::unique_ptr<base::DelayTimer> batch_mode_timer_;
159
160 // Mojo service connection. Must always be bound/reset and used on the IO
161 // thread.
162 mojom::ImageDecoderPtr decoder_;
[email protected]551707a2010-06-16 16:59:47163
164 DISALLOW_COPY_AND_ASSIGN(ImageDecoder);
165};
166
[email protected]6d33da172011-11-22 03:56:09167#endif // CHROME_BROWSER_IMAGE_DECODER_H_