blob: d8bda97454fab4f214441aa5624a14423f0b330e [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
rockot2a0f4fb32016-11-11 18:44:1974 ImageDecoder();
75 ~ImageDecoder();
76
thestigc5eeb9f362015-04-22 18:42:3277 // Calls StartWithOptions() with ImageCodec::DEFAULT_CODEC and
twellingtona71414d2015-03-26 15:09:4578 // shrink_to_fit = false.
79 static void Start(ImageRequest* image_request,
nya601d9702016-07-26 18:39:2180 std::vector<uint8_t> image_data);
81 // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy.
82 static void Start(ImageRequest* image_request,
twellingtona71414d2015-03-26 15:09:4583 const std::string& image_data);
pneubeck93871252015-01-20 11:26:3684
[email protected]cdc8a3e2013-06-12 04:18:1785 // Starts asynchronous image decoding. Once finished, the callback will be
twellingtona71414d2015-03-26 15:09:4586 // posted back to image_request's |task_runner_|.
87 static void StartWithOptions(ImageRequest* image_request,
nya601d9702016-07-26 18:39:2188 std::vector<uint8_t> image_data,
89 ImageCodec image_codec,
90 bool shrink_to_fit);
91 // Deprecated. Use std::vector<uint8_t> version to avoid an extra copy.
92 static void StartWithOptions(ImageRequest* image_request,
twellingtona71414d2015-03-26 15:09:4593 const std::string& image_data,
94 ImageCodec image_codec,
95 bool shrink_to_fit);
[email protected]551707a2010-06-16 16:59:4796
thestigc5eeb9f362015-04-22 18:42:3297 // Removes all instances of |image_request| from |image_request_id_map_|,
twellingtona71414d2015-03-26 15:09:4598 // ensuring callbacks are not made to the image_request after it is destroyed.
99 static void Cancel(ImageRequest* image_request);
[email protected]11b0856d2012-06-13 19:21:56100
[email protected]551707a2010-06-16 16:59:47101 private:
thestigc5eeb9f362015-04-22 18:42:32102 using RequestMap = std::map<int, ImageRequest*>;
103
thestigc5eeb9f362015-04-22 18:42:32104 void StartWithOptionsImpl(ImageRequest* image_request,
nya601d9702016-07-26 18:39:21105 std::vector<uint8_t> image_data,
thestigc5eeb9f362015-04-22 18:42:32106 ImageCodec image_codec,
107 bool shrink_to_fit);
rockot2a0f4fb32016-11-11 18:44:19108
twellingtona71414d2015-03-26 15:09:45109 void CancelImpl(ImageRequest* image_request);
110
[email protected]373c1062011-06-09 21:11:51111 // IPC message handlers.
twellingtona71414d2015-03-26 15:09:45112 void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int request_id);
113 void OnDecodeImageFailed(int request_id);
[email protected]551707a2010-06-16 16:59:47114
thestigc5eeb9f362015-04-22 18:42:32115 // id to use for the next Start() request that comes in.
twellingtona71414d2015-03-26 15:09:45116 int image_request_id_counter_;
[email protected]551707a2010-06-16 16:59:47117
twellingtona71414d2015-03-26 15:09:45118 // Map of request id's to ImageRequests.
119 RequestMap image_request_id_map_;
120
121 // Protects |image_request_id_map_| and |image_request_id_counter_|.
122 base::Lock map_lock_;
123
[email protected]551707a2010-06-16 16:59:47124 DISALLOW_COPY_AND_ASSIGN(ImageDecoder);
125};
126
[email protected]6d33da172011-11-22 03:56:09127#endif // CHROME_BROWSER_IMAGE_DECODER_H_