blob: ca7604350e97ea63b229a339666faa49bd1a6002 [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
twellingtona71414d2015-03-26 15:09:4512#include "base/lazy_instance.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"
twellingtona71414d2015-03-26 15:09:4517#include "base/timer/timer.h"
18#include "content/public/browser/utility_process_host.h"
[email protected]c4f883a2012-02-03 17:02:0719#include "content/public/browser/utility_process_host_client.h"
[email protected]373c1062011-06-09 21:11:5120
21class SkBitmap;
[email protected]551707a2010-06-16 16:59:4722
twellingtona71414d2015-03-26 15:09:4523// This is a helper class for decoding images safely in a utility process. To
24// use this, call ImageDecoder::Start(...) or
25// ImageDecoder::StartWithOptions(...) on any thread.
26//
27// Internally, most of the work happens on the IO thread, and then
28// the callback (ImageRequest::OnImageDecoded or
29// ImageRequest::OnDecodeImageFailed) is posted back to the |task_runner_|
30// associated with the ImageRequest.
31// The Cancel() method runs on whichever thread called it. |map_lock_| is used
32// to protect the data that is accessed from multiple threads.
[email protected]c4f883a2012-02-03 17:02:0733class ImageDecoder : public content::UtilityProcessHostClient {
[email protected]551707a2010-06-16 16:59:4734 public:
thestigc5eeb9f362015-04-22 18:42:3235 // ImageRequest objects needs to be created and destroyed on the same
36 // SequencedTaskRunner.
twellingtona71414d2015-03-26 15:09:4537 class ImageRequest {
[email protected]551707a2010-06-16 16:59:4738 public:
39 // Called when image is decoded.
twellingtona71414d2015-03-26 15:09:4540 virtual void OnImageDecoded(const SkBitmap& decoded_image) = 0;
[email protected]928c0e712011-03-16 15:01:5741
twellingtona71414d2015-03-26 15:09:4542 // Called when decoding image failed. ImageRequest can do some cleanup in
[email protected]928c0e712011-03-16 15:01:5743 // this handler.
twellingtona71414d2015-03-26 15:09:4544 virtual void OnDecodeImageFailed() {}
45
46 base::SequencedTaskRunner* task_runner() const {
47 return task_runner_.get();
48 }
[email protected]551707a2010-06-16 16:59:4749
50 protected:
thestigc5eeb9f362015-04-22 18:42:3251 // Creates an ImageRequest that runs on the thread creating it.
52 ImageRequest();
53 // Explicitly pass in |task_runner| if the current thread is part of a
54 // thread pool.
twellingtona71414d2015-03-26 15:09:4555 explicit ImageRequest(
56 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
57 virtual ~ImageRequest();
58
59 private:
thestigc5eeb9f362015-04-22 18:42:3260 // The thread to post OnImageDecoded() or OnDecodeImageFailed() once the
twellingtona71414d2015-03-26 15:09:4561 // the image has been decoded.
62 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
thestigc5eeb9f362015-04-22 18:42:3263
64 base::SequenceChecker sequence_checker_;
[email protected]551707a2010-06-16 16:59:4765 };
66
[email protected]11f16d102012-08-29 23:12:1567 enum ImageCodec {
68 DEFAULT_CODEC = 0, // Uses WebKit image decoding (via WebImage).
69 ROBUST_JPEG_CODEC, // Restrict decoding to robust jpeg codec.
70 };
71
thestigc5eeb9f362015-04-22 18:42:3272 // Calls StartWithOptions() with ImageCodec::DEFAULT_CODEC and
twellingtona71414d2015-03-26 15:09:4573 // shrink_to_fit = false.
74 static void Start(ImageRequest* image_request,
75 const std::string& image_data);
pneubeck93871252015-01-20 11:26:3676
[email protected]cdc8a3e2013-06-12 04:18:1777 // Starts asynchronous image decoding. Once finished, the callback will be
twellingtona71414d2015-03-26 15:09:4578 // posted back to image_request's |task_runner_|.
79 static void StartWithOptions(ImageRequest* image_request,
80 const std::string& image_data,
81 ImageCodec image_codec,
82 bool shrink_to_fit);
[email protected]551707a2010-06-16 16:59:4783
thestigc5eeb9f362015-04-22 18:42:3284 // Removes all instances of |image_request| from |image_request_id_map_|,
twellingtona71414d2015-03-26 15:09:4585 // ensuring callbacks are not made to the image_request after it is destroyed.
86 static void Cancel(ImageRequest* image_request);
[email protected]11b0856d2012-06-13 19:21:5687
[email protected]551707a2010-06-16 16:59:4788 private:
twellingtona71414d2015-03-26 15:09:4589 friend struct base::DefaultLazyInstanceTraits<ImageDecoder>;
90
thestigc5eeb9f362015-04-22 18:42:3291 using RequestMap = std::map<int, ImageRequest*>;
92
twellingtona71414d2015-03-26 15:09:4593 ImageDecoder();
[email protected]551707a2010-06-16 16:59:4794 // It's a reference counted object, so destructor is private.
Daniel Chenga542fca2014-10-21 09:51:2995 ~ImageDecoder() override;
[email protected]551707a2010-06-16 16:59:4796
twellingtona71414d2015-03-26 15:09:4597 // Sends a request to the sandboxed process to decode the image. Starts
twellington612b9b62015-04-01 00:33:1298 // batch mode if necessary. If the utility process fails to start,
99 // an OnDecodeImageFailed task is posted to image_request's |task_runner_|.
thestigc5eeb9f362015-04-22 18:42:32100 void DecodeImageInSandbox(int request_id,
twellingtona71414d2015-03-26 15:09:45101 const std::vector<unsigned char>& image_data,
102 ImageCodec image_codec,
103 bool shrink_to_fit);
104
thestigc5eeb9f362015-04-22 18:42:32105 void StartWithOptionsImpl(ImageRequest* image_request,
106 const std::string& image_data,
107 ImageCodec image_codec,
108 bool shrink_to_fit);
twellingtona71414d2015-03-26 15:09:45109 void CancelImpl(ImageRequest* image_request);
110
twellingtona71414d2015-03-26 15:09:45111 // Starts UtilityProcessHost in batch mode and starts |batch_mode_timer_|.
twellington612b9b62015-04-01 00:33:12112 // If the utility process fails to start, the method resets
113 // |utility_process_host_| and returns.
twellingtona71414d2015-03-26 15:09:45114 void StartBatchMode();
115
116 // Stops batch mode if no requests have come in since
thestigc5eeb9f362015-04-22 18:42:32117 // |kBatchModeTimeoutSeconds|.
twellingtona71414d2015-03-26 15:09:45118 void StopBatchMode();
119
120 // Overidden from UtilityProcessHostClient.
Daniel Chenga542fca2014-10-21 09:51:29121 bool OnMessageReceived(const IPC::Message& message) override;
[email protected]373c1062011-06-09 21:11:51122
123 // IPC message handlers.
twellingtona71414d2015-03-26 15:09:45124 void OnDecodeImageSucceeded(const SkBitmap& decoded_image, int request_id);
125 void OnDecodeImageFailed(int request_id);
[email protected]551707a2010-06-16 16:59:47126
thestigc5eeb9f362015-04-22 18:42:32127 // For the ImageRequest identified by |request_id|, call its OnImageDecoded()
128 // or OnDecodeImageFailed() method on its task runner thread.
129 void RunOnImageDecoded(const SkBitmap& decoded_image, int request_id);
130 void RunOnDecodeImageFailed(int request_id);
131
132 // id to use for the next Start() request that comes in.
twellingtona71414d2015-03-26 15:09:45133 int image_request_id_counter_;
[email protected]551707a2010-06-16 16:59:47134
twellingtona71414d2015-03-26 15:09:45135 // Map of request id's to ImageRequests.
136 RequestMap image_request_id_map_;
137
138 // Protects |image_request_id_map_| and |image_request_id_counter_|.
139 base::Lock map_lock_;
140
141 // The UtilityProcessHost requests are sent to.
142 base::WeakPtr<content::UtilityProcessHost> utility_process_host_;
143
thestigc5eeb9f362015-04-22 18:42:32144 // Calls StopBatchMode() after |kBatchModeTimeoutSeconds| have elapsed.
twellingtona71414d2015-03-26 15:09:45145 base::RepeatingTimer<ImageDecoder> batch_mode_timer_;
146
thestigc5eeb9f362015-04-22 18:42:32147 // The time Start() was last called.
twellingtona71414d2015-03-26 15:09:45148 base::TimeTicks last_request_;
[email protected]551707a2010-06-16 16:59:47149
150 DISALLOW_COPY_AND_ASSIGN(ImageDecoder);
151};
152
[email protected]6d33da172011-11-22 03:56:09153#endif // CHROME_BROWSER_IMAGE_DECODER_H_