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

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

Issue 2064793004: Reland: Split the code for fetching images and for decoding images into two separate classes. This w (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Only start image requests once (Fixes a bug). Created 4 years, 6 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 "chrome/browser/search/suggestions/image_decoder_impl.h"
9 #include "ui/gfx/image/image.h"
10
11 namespace suggestions {
12
13 // A request for decoding an image.
14 class ImageDecoderImpl::DecodeImageRequest
15 : public ::ImageDecoder::ImageRequest {
16 public:
17 DecodeImageRequest(ImageDecoderImpl* decoder,
18 const image_fetcher::ImageDecodedCallback& callback)
19 : decoder_(decoder), callback_(callback) {}
20 ~DecodeImageRequest() override {}
21
22 private:
23 // Runs the callback and remove the request from the internal request queue.
24 void RunCallbackAndRemoveRequest(const gfx::Image& image);
25
26 // Methods inherited from ImageDecoder::ImageRequest
27
28 void OnImageDecoded(const SkBitmap& decoded_image) override;
29
30 void OnDecodeImageFailed() override;
31
32 ImageDecoderImpl* decoder_;
33
34 // The callback to call after the request completed.
35 image_fetcher::ImageDecodedCallback callback_;
36
37 DISALLOW_COPY_AND_ASSIGN(DecodeImageRequest);
38 };
39
40 void ImageDecoderImpl::DecodeImageRequest::OnImageDecoded(
41 const SkBitmap& decoded_bitmap) {
42 // TODO(markusheintz): Check whether high res displays that require 2x and 3x
43 // image versions need to be supported here.
44 gfx::Image image(gfx::Image::CreateFrom1xBitmap(decoded_bitmap));
45 RunCallbackAndRemoveRequest(image);
46 }
47
48 // Called when decoding image failed.
49 void ImageDecoderImpl::DecodeImageRequest::OnDecodeImageFailed() {
50 RunCallbackAndRemoveRequest(gfx::Image());
51 }
52
53 void ImageDecoderImpl::DecodeImageRequest::RunCallbackAndRemoveRequest(
54 const gfx::Image& image) {
55 callback_.Run(image);
56
57 // This must be the last line in the method body.
58 decoder_->RemoveDecodeImageRequest(this);
59 }
60
61 ImageDecoderImpl::ImageDecoderImpl() {}
62
63 ImageDecoderImpl::~ImageDecoderImpl() {}
64
65 void ImageDecoderImpl::DecodeImage(
66 const std::string& image_data,
67 const image_fetcher::ImageDecodedCallback& callback) {
68 std::unique_ptr<DecodeImageRequest> decode_image_request(
69 new DecodeImageRequest(this, callback));
70
71 ::ImageDecoder::Start(decode_image_request.get(), image_data);
72
73 decode_image_requests_.push_back(std::move(decode_image_request));
74 }
75
76 void ImageDecoderImpl::RemoveDecodeImageRequest(DecodeImageRequest* request) {
77 // Remove the finished request from the request queue.
78 auto request_it =
79 std::find_if(decode_image_requests_.begin(),
80 decode_image_requests_.end(),
81 [request](const std::unique_ptr<DecodeImageRequest>& r) {
82 return r.get() == request;
83 });
84 DCHECK(request_it != decode_image_requests_.end());
85 decode_image_requests_.erase(request_it);
86 }
87
88 } // namespace suggestions
OLDNEW
« no previous file with comments | « chrome/browser/search/suggestions/image_decoder_impl.h ('k') | chrome/browser/search/suggestions/image_fetcher_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698