blob: 2770662f3657eee796a61fa2407fd2592130fd2f [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>
[email protected]928c0e712011-03-16 15:01:579#include <string>
[email protected]551707a2010-06-16 16:59:4710#include <vector>
11
avi6846aef2015-12-26 01:09:3812#include "base/macros.h"
[email protected]810dddf52013-01-25 19:37:2613#include "base/memory/ref_counted.h"
thestigc5eeb9f362015-04-22 18:42:3214#include "base/sequence_checker.h"
15#include "base/sequenced_task_runner.h"
twellingtona71414d2015-03-26 15:09:4516#include "base/synchronization/lock.h"
[email protected]373c1062011-06-09 21:11:5117
18class SkBitmap;
[email protected]551707a2010-06-16 16:59:4719
rockot2a0f4fb32016-11-11 18:44:1920// This is a helper class for decoding images safely in a sandboxed service. To
twellingtona71414d2015-03-26 15:09:4521// use this, call ImageDecoder::Start(...) or
22// ImageDecoder::StartWithOptions(...) on any thread.
23//
rockot2a0f4fb32016-11-11 18:44:1924// ImageRequest::OnImageDecoded or ImageRequest::OnDecodeImageFailed is posted
25// back to the |task_runner_| associated with the ImageRequest.
26//
27// The Cancel() method runs on whichever thread called it.
28//
29// TODO(rockot): Use of this class should be replaced with direct image_decoder
30// client library usage.
31class ImageDecoder {
[email protected]551707a2010-06-16 16:59:4732 public:
thestigc5eeb9f362015-04-22 18:42:3233 // ImageRequest objects needs to be created and destroyed on the same
34 // SequencedTaskRunner.
twellingtona71414d2015-03-26 15:09:4535 class ImageRequest {
[email protected]551707a2010-06-16 16:59:4736 public:
37 // Called when image is decoded.
twellingtona71414d2015-03-26 15:09:4538 virtual void OnImageDecoded(const SkBitmap& decoded_image) = 0;
[email protected]928c0e712011-03-16 15:01:5739
twellingtona71414d2015-03-26 15:09:4540 // Called when decoding image failed. ImageRequest can do some cleanup in
[email protected]928c0e712011-03-16 15:01:5741 // this handler.
twellingtona71414d2015-03-26 15:09:4542 virtual void OnDecodeImageFailed() {}
43
44 base::SequencedTaskRunner* task_runner() const {
45 return task_runner_.get();
46 }
[email protected]551707a2010-06-16 16:59:4747
48 protected:
rockot2a0f4fb32016-11-11 18:44:1949 // Creates an ImageRequest that runs on the thread which created it.
thestigc5eeb9f362015-04-22 18:42:3250 ImageRequest();
rockot2a0f4fb32016-11-11 18:44:1951
thestigc5eeb9f362015-04-22 18:42:3252 // Explicitly pass in |task_runner| if the current thread is part of a
53 // thread pool.
twellingtona71414d2015-03-26 15:09:4554 explicit ImageRequest(
55 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
56 virtual ~ImageRequest();
57
58 private:
thestigc5eeb9f362015-04-22 18:42:3259 // The thread to post OnImageDecoded() or OnDecodeImageFailed() once the
twellingtona71414d2015-03-26 15:09:4560 // the image has been decoded.
61 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
thestigc5eeb9f362015-04-22 18:42:3262
63 base::SequenceChecker sequence_checker_;
[email protected]551707a2010-06-16 16:59:4764 };
65
[email protected]11f16d102012-08-29 23:12:1566 enum ImageCodec {
67 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage).
edward.baker199f66b2015-05-07 19:13:3068#if defined(OS_CHROMEOS)
[email protected]11f16d102012-08-29 23:12:1569 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec.
satoruxf7e76f02016-03-09 06:30:4570 ROBUST_PNG_CODEC, // Restrict decoding to robust PNG codec.
edward.baker199f66b2015-05-07 19:13:3071#endif // defined(OS_CHROMEOS)
[email protected]11f16d102012-08-29 23:12:1572 };
73
scottmgd69ee9d2017-02-06 20:55:3674 static ImageDecoder* GetInstance();
rockot2a0f4fb32016-11-11 18:44:1975
thestigc5eeb9f362015-04-22 18:42:3276 // Calls StartWithOptions() with ImageCodec::DEFAULT_CODEC and
twellingtona71414d2015-03-26 15:09:4577 // shrink_to_fit = false.
78 static void Start(ImageRequest* image_request,
nya601d9702016-07-26 18:39:2179 std::vector<uint8_t> image_data);
80 // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy.
81 static void Start(ImageRequest* image_request,
twellingtona71414d2015-03-26 15:09:4582 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,
nya601d9702016-07-26 18:39:2187 std::vector<uint8_t> image_data,
88 ImageCodec image_codec,
89 bool shrink_to_fit);
90 // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy.
91 static void StartWithOptions(ImageRequest* image_request,
twellingtona71414d2015-03-26 15:09:4592 const std::string& image_data,
93 ImageCodec image_codec,
94 bool shrink_to_fit);
[email protected]551707a2010-06-16 16:59:4795
thestigc5eeb9f362015-04-22 18:42:3296 // Removes all instances of |image_request| from |image_request_id_map_|,
twellingtona71414d2015-03-26 15:09:4597 // ensuring callbacks are not made to the image_request after it is destroyed.
98 static void Cancel(ImageRequest* image_request);
[email protected]11b0856d2012-06-13 19:21:5699
[email protected]551707a2010-06-16 16:59:47100 private:
thestigc5eeb9f362015-04-22 18:42:32101 using RequestMap = std::map<int, ImageRequest*>;
102
scottmgd69ee9d2017-02-06 20:55:36103 ImageDecoder();
104 ~ImageDecoder() = delete;
105
thestigc5eeb9f362015-04-22 18:42:32106 void StartWithOptionsImpl(ImageRequest* image_request,
nya601d9702016-07-26 18:39:21107 std::vector<uint8_t> image_data,
thestigc5eeb9f362015-04-22 18:42:32108 ImageCodec image_codec,
109 bool shrink_to_fit);
rockot2a0f4fb32016-11-11 18:44:19110
twellingtona71414d2015-03-26 15:09:45111 void CancelImpl(ImageRequest* image_request);
112
[email protected]373c1062011-06-09 21:11:51113 // IPC message handlers.
twellingtona71414d2015-03-26 15:09:45114 void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int request_id);
115 void OnDecodeImageFailed(int request_id);
[email protected]551707a2010-06-16 16:59:47116
thestigc5eeb9f362015-04-22 18:42:32117 // id to use for the next Start() request that comes in.
twellingtona71414d2015-03-26 15:09:45118 int image_request_id_counter_;
[email protected]551707a2010-06-16 16:59:47119
twellingtona71414d2015-03-26 15:09:45120 // Map of request id's to ImageRequests.
121 RequestMap image_request_id_map_;
122
123 // Protects |image_request_id_map_| and |image_request_id_counter_|.
124 base::Lock map_lock_;
125
[email protected]551707a2010-06-16 16:59:47126 DISALLOW_COPY_AND_ASSIGN(ImageDecoder);
127};
128
[email protected]6d33da172011-11-22 03:56:09129#endif // CHROME_BROWSER_IMAGE_DECODER_H_