summaryrefslogtreecommitdiffstats
path: root/chrome/browser/image_decoder.cc
blob: c587695faba823e156b22b6e4711710aca4ce451 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/image_decoder.h"

#include "base/bind.h"
#include "base/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/common/chrome_utility_messages.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/utility_process_host.h"
#include "ui/base/l10n/l10n_util.h"

using content::BrowserThread;
using content::UtilityProcessHost;

namespace {

// static, Leaky to allow access from any thread.
base::LazyInstance<ImageDecoder>::Leaky g_decoder = LAZY_INSTANCE_INITIALIZER;

// How long to wait after the last request has been received before ending
// batch mode.
const int kBatchModeTimeoutSeconds = 5;

}  // namespace

ImageDecoder::ImageDecoder()
    : image_request_id_counter_(0) {
  // A single ImageDecoder instance should live for the life of the program.
  // Explicitly add a reference so the object isn't deleted.
  AddRef();
}

ImageDecoder::~ImageDecoder() {
}

ImageDecoder::ImageRequest::ImageRequest()
    : task_runner_(base::ThreadTaskRunnerHandle::Get()) {
  DCHECK(sequence_checker_.CalledOnValidSequencedThread());
}

ImageDecoder::ImageRequest::ImageRequest(
    const scoped_refptr<base::SequencedTaskRunner>& task_runner)
    : task_runner_(task_runner) {
  DCHECK(sequence_checker_.CalledOnValidSequencedThread());
}

ImageDecoder::ImageRequest::~ImageRequest() {
  DCHECK(sequence_checker_.CalledOnValidSequencedThread());
  ImageDecoder::Cancel(this);
}

// static
void ImageDecoder::Start(ImageRequest* image_request,
                         const std::string& image_data) {
  StartWithOptions(image_request, image_data, DEFAULT_CODEC, false);
}

// static
void ImageDecoder::StartWithOptions(ImageRequest* image_request,
                                    const std::string& image_data,
                                    ImageCodec image_codec,
                                    bool shrink_to_fit) {
  g_decoder.Pointer()->StartWithOptionsImpl(image_request, image_data,
                                            image_codec, shrink_to_fit);
}

void ImageDecoder::StartWithOptionsImpl(ImageRequest* image_request,
                                        const std::string& image_data,
                                        ImageCodec image_codec,
                                        bool shrink_to_fit) {
  DCHECK(image_request);
  DCHECK(image_request->task_runner());

  int request_id;
  {
    base::AutoLock lock(map_lock_);
    request_id = image_request_id_counter_++;
    image_request_id_map_.insert(std::make_pair(request_id, image_request));
  }

  BrowserThread::PostTask(
      BrowserThread::IO, FROM_HERE,
      base::Bind(
          &ImageDecoder::DecodeImageInSandbox,
          g_decoder.Pointer(), request_id,
          std::vector<unsigned char>(image_data.begin(), image_data.end()),
          image_codec, shrink_to_fit));
}

// static
void ImageDecoder::Cancel(ImageRequest* image_request) {
  DCHECK(image_request);
  g_decoder.Pointer()->CancelImpl(image_request);
}

void ImageDecoder::DecodeImageInSandbox(
    int request_id,
    const std::vector<unsigned char>& image_data,
    ImageCodec image_codec,
    bool shrink_to_fit) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  base::AutoLock lock(map_lock_);
  const auto it = image_request_id_map_.find(request_id);
  if (it == image_request_id_map_.end())
    return;

  ImageRequest* image_request = it->second;
  if (!utility_process_host_) {
    StartBatchMode();
  }
  if (!utility_process_host_) {
    // Utility process failed to start; notify delegate and return.
    // Without this check, we were seeing crashes on startup. Further
    // investigation is needed to determine why the utility process
    // is failing to start. See crbug.com/472272
    image_request->task_runner()->PostTask(
        FROM_HERE,
        base::Bind(&ImageDecoder::RunOnDecodeImageFailed, this, request_id));
    return;
  }

  if (!batch_mode_timer_) {
    // Created here so it will call StopBatchMode() on the right thread.
    batch_mode_timer_.reset(new base::DelayTimer(
        FROM_HERE, base::TimeDelta::FromSeconds(kBatchModeTimeoutSeconds), this,
        &ImageDecoder::StopBatchMode));
  }
  batch_mode_timer_->Reset();

  switch (image_codec) {
#if defined(OS_CHROMEOS)
    case ROBUST_JPEG_CODEC:
      utility_process_host_->Send(new ChromeUtilityMsg_RobustJPEGDecodeImage(
          image_data, request_id));
      break;
#endif  // defined(OS_CHROMEOS)
    case DEFAULT_CODEC:
      utility_process_host_->Send(new ChromeUtilityMsg_DecodeImage(
          image_data, shrink_to_fit, request_id));
      break;
  }
}

void ImageDecoder::CancelImpl(ImageRequest* image_request) {
  base::AutoLock lock(map_lock_);
  for (auto it = image_request_id_map_.begin();
       it != image_request_id_map_.end();) {
    if (it->second == image_request) {
      image_request_id_map_.erase(it++);
    } else {
      ++it;
    }
  }
}

void ImageDecoder::StartBatchMode() {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  utility_process_host_ =
      UtilityProcessHost::Create(
          this, base::ThreadTaskRunnerHandle::Get().get())->AsWeakPtr();
  utility_process_host_->SetName(l10n_util::GetStringUTF16(
      IDS_UTILITY_PROCESS_IMAGE_DECODER_NAME));
  if (!utility_process_host_->StartBatchMode()) {
     utility_process_host_.reset();
     return;
  }
}

void ImageDecoder::StopBatchMode() {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  {
    // Check for outstanding requests and wait for them to finish.
    base::AutoLock lock(map_lock_);
    if (!image_request_id_map_.empty()) {
      batch_mode_timer_->Reset();
      return;
    }
  }

  if (utility_process_host_) {
    utility_process_host_->EndBatchMode();
    utility_process_host_.reset();
  }
}

void ImageDecoder::FailAllRequests() {
  RequestMap requests;
  {
    base::AutoLock lock(map_lock_);
    requests = image_request_id_map_;
  }

  // Since |OnProcessCrashed| and |OnProcessLaunchFailed| are run asynchronously
  // from the actual event, it's possible for a new utility process to have been
  // created and sent requests by the time these functions are run. This results
  // in failing requests that are unaffected by the crash. Although not ideal,
  // this is valid and simpler than tracking which request is sent to which
  // utility process, and whether the request has been sent at all.
  for (const auto& request : requests)
    OnDecodeImageFailed(request.first);
}

void ImageDecoder::OnProcessCrashed(int exit_code) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  FailAllRequests();
}

void ImageDecoder::OnProcessLaunchFailed() {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  FailAllRequests();
}

bool ImageDecoder::OnMessageReceived(
    const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(ImageDecoder, message)
    IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded,
                        OnDecodeImageSucceeded)
    IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed,
                        OnDecodeImageFailed)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void ImageDecoder::OnDecodeImageSucceeded(
    const SkBitmap& decoded_image,
    int request_id) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  base::AutoLock lock(map_lock_);
  auto it = image_request_id_map_.find(request_id);
  if (it == image_request_id_map_.end())
    return;

  ImageRequest* image_request = it->second;
  image_request->task_runner()->PostTask(
      FROM_HERE,
      base::Bind(&ImageDecoder::RunOnImageDecoded,
                 this,
                 decoded_image,
                 request_id));
}

void ImageDecoder::OnDecodeImageFailed(int request_id) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  base::AutoLock lock(map_lock_);
  auto it = image_request_id_map_.find(request_id);
  if (it == image_request_id_map_.end())
    return;

  ImageRequest* image_request = it->second;
  image_request->task_runner()->PostTask(
      FROM_HERE,
      base::Bind(&ImageDecoder::RunOnDecodeImageFailed, this, request_id));
}

void ImageDecoder::RunOnImageDecoded(const SkBitmap& decoded_image,
                                     int request_id) {
  ImageRequest* image_request;
  {
    base::AutoLock lock(map_lock_);
    auto it = image_request_id_map_.find(request_id);
    if (it == image_request_id_map_.end())
      return;
    image_request = it->second;
    image_request_id_map_.erase(it);
  }

  DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread());
  image_request->OnImageDecoded(decoded_image);
}

void ImageDecoder::RunOnDecodeImageFailed(int request_id) {
  ImageRequest* image_request;
  {
    base::AutoLock lock(map_lock_);
    auto it = image_request_id_map_.find(request_id);
    if (it == image_request_id_map_.end())
      return;
    image_request = it->second;
    image_request_id_map_.erase(it);
  }

  DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread());
  image_request->OnDecodeImageFailed();
}