Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: chrome/browser/search/suggestions/image_decoder_impl.cc

Issue 2763103002: Move ImageDecoder to components/image_fetcher/content (Closed)
Patch Set: Rebased. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <algorithm>
6 #include <vector>
7
8 #include "base/callback.h"
9 #include "chrome/browser/search/suggestions/image_decoder_impl.h"
10 #include "ui/gfx/geometry/size.h"
11 #include "ui/gfx/image/image.h"
12
13 namespace suggestions {
14
15 // A request for decoding an image.
16 class ImageDecoderImpl::DecodeImageRequest
17 : public ::ImageDecoder::ImageRequest {
18 public:
19 DecodeImageRequest(ImageDecoderImpl* decoder,
20 const image_fetcher::ImageDecodedCallback& callback)
21 : decoder_(decoder), callback_(callback) {}
22 ~DecodeImageRequest() override {}
23
24 private:
25 // Runs the callback and remove the request from the internal request queue.
26 void RunCallbackAndRemoveRequest(const gfx::Image& image);
27
28 // Methods inherited from ImageDecoder::ImageRequest
29
30 void OnImageDecoded(const SkBitmap& decoded_image) override;
31
32 void OnDecodeImageFailed() override;
33
34 ImageDecoderImpl* decoder_;
35
36 // The callback to call after the request completed.
37 image_fetcher::ImageDecodedCallback callback_;
38
39 DISALLOW_COPY_AND_ASSIGN(DecodeImageRequest);
40 };
41
42 void ImageDecoderImpl::DecodeImageRequest::OnImageDecoded(
43 const SkBitmap& decoded_bitmap) {
44 // TODO(markusheintz): Check whether high res displays that require 2x and 3x
45 // image versions need to be supported here.
46 gfx::Image image(gfx::Image::CreateFrom1xBitmap(decoded_bitmap));
47 RunCallbackAndRemoveRequest(image);
48 }
49
50 // Called when decoding image failed.
51 void ImageDecoderImpl::DecodeImageRequest::OnDecodeImageFailed() {
52 RunCallbackAndRemoveRequest(gfx::Image());
53 }
54
55 void ImageDecoderImpl::DecodeImageRequest::RunCallbackAndRemoveRequest(
56 const gfx::Image& image) {
57 callback_.Run(image);
58
59 // This must be the last line in the method body.
60 decoder_->RemoveDecodeImageRequest(this);
61 }
62
63 ImageDecoderImpl::ImageDecoderImpl() {}
64
65 ImageDecoderImpl::~ImageDecoderImpl() {}
66
67 void ImageDecoderImpl::DecodeImage(
68 const std::string& image_data,
69 const gfx::Size& desired_image_frame_size,
70 const image_fetcher::ImageDecodedCallback& callback) {
71 std::unique_ptr<DecodeImageRequest> decode_image_request(
72 new DecodeImageRequest(this, callback));
73
74 ::ImageDecoder::StartWithOptions(
75 decode_image_request.get(),
76 std::vector<uint8_t>(image_data.begin(), image_data.end()),
77 ::ImageDecoder::DEFAULT_CODEC,
78 /*shrink_to_fit=*/false, desired_image_frame_size);
79
80 decode_image_requests_.push_back(std::move(decode_image_request));
81 }
82
83 void ImageDecoderImpl::RemoveDecodeImageRequest(DecodeImageRequest* request) {
84 // Remove the finished request from the request queue.
85 auto request_it =
86 std::find_if(decode_image_requests_.begin(),
87 decode_image_requests_.end(),
88 [request](const std::unique_ptr<DecodeImageRequest>& r) {
89 return r.get() == request;
90 });
91 DCHECK(request_it != decode_image_requests_.end());
92 decode_image_requests_.erase(request_it);
93 }
94
95 } // namespace suggestions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698