File: cached_image_fetcher.cc

package info (click to toggle)
chromium-browser 70.0.3538.110-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,619,476 kB
  • sloc: cpp: 13,024,755; ansic: 1,349,823; python: 916,672; xml: 314,489; java: 280,047; asm: 276,936; perl: 75,771; objc: 66,634; sh: 45,860; cs: 28,354; php: 11,064; makefile: 10,911; yacc: 9,109; tcl: 8,403; ruby: 4,065; lex: 1,779; pascal: 1,411; lisp: 1,055; awk: 41; jsp: 39; sed: 17; sql: 3
file content (173 lines) | stat: -rw-r--r-- 6,279 bytes parent folder | download | duplicates (2)
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
// Copyright 2017 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 "components/ntp_snippets/remote/cached_image_fetcher.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "components/image_fetcher/core/image_decoder.h"
#include "components/image_fetcher/core/image_fetcher.h"
#include "components/ntp_snippets/remote/remote_suggestions_database.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image.h"

namespace ntp_snippets {
namespace {
constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation =
    net::DefineNetworkTrafficAnnotation("remote_suggestions_provider", R"(
        semantics {
          sender: "Content Suggestion Thumbnail Fetch"
          description:
            "Retrieves thumbnails for content suggestions, for display on the "
            "New Tab page or Chrome Home."
          trigger:
            "Triggered when the user looks at a content suggestion (and its "
            "thumbnail isn't cached yet)."
          data: "None."
          destination: GOOGLE_OWNED_SERVICE
        }
        policy {
          cookies_allowed: NO
          setting: "Currently not available, but in progress: crbug.com/703684"
        chrome_policy {
          NTPContentSuggestionsEnabled {
            policy_options {mode: MANDATORY}
            NTPContentSuggestionsEnabled: false
          }
        }
      })");
}  // namespace

CachedImageFetcher::CachedImageFetcher(
    std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher,
    PrefService* pref_service,
    RemoteSuggestionsDatabase* database)
    : image_fetcher_(std::move(image_fetcher)),
      database_(database),
      thumbnail_requests_throttler_(
          pref_service,
          RequestThrottler::RequestType::CONTENT_SUGGESTION_THUMBNAIL) {
  // |image_fetcher_| can be null in tests.
  if (image_fetcher_) {
    image_fetcher_->SetDataUseServiceName(
        data_use_measurement::DataUseUserData::NTP_SNIPPETS_THUMBNAILS);
  }
}

CachedImageFetcher::~CachedImageFetcher() {}

void CachedImageFetcher::FetchSuggestionImage(
    const ContentSuggestion::ID& suggestion_id,
    const GURL& url,
    ImageDataFetchedCallback image_data_callback,
    ImageFetchedCallback image_callback) {
  database_->LoadImage(
      suggestion_id.id_within_category(),
      base::BindOnce(&CachedImageFetcher::OnImageFetchedFromDatabase,
                     base::Unretained(this), std::move(image_data_callback),
                     std::move(image_callback), suggestion_id, url));
}

void CachedImageFetcher::OnImageDecodingDone(
    ImageFetchedCallback callback,
    const std::string& id_within_category,
    const gfx::Image& image,
    const image_fetcher::RequestMetadata& metadata) {
  std::move(callback).Run(image);
}

void CachedImageFetcher::OnImageFetchedFromDatabase(
    ImageDataFetchedCallback image_data_callback,
    ImageFetchedCallback image_callback,
    const ContentSuggestion::ID& suggestion_id,
    const GURL& url,
    std::string data) {  // SnippetImageCallback requires by-value.
  if (data.empty()) {
    // Fetching from the DB failed; start a network fetch.
    FetchImageFromNetwork(suggestion_id, url, std::move(image_data_callback),
                          std::move(image_callback));
    return;
  }

  if (image_data_callback) {
    std::move(image_data_callback).Run(data);
  }

  if (image_callback) {
    image_fetcher_->GetImageDecoder()->DecodeImage(
        data,
        // We're not dealing with multi-frame images.
        /*desired_image_frame_size=*/gfx::Size(),
        base::Bind(&CachedImageFetcher::OnImageDecodedFromDatabase,
                   base::Unretained(this),
                   base::Passed(std::move(image_callback)), suggestion_id,
                   url));
  }
}

void CachedImageFetcher::OnImageDecodedFromDatabase(
    ImageFetchedCallback callback,
    const ContentSuggestion::ID& suggestion_id,
    const GURL& url,
    const gfx::Image& image) {
  if (!image.IsEmpty()) {
    std::move(callback).Run(image);
    return;
  }
  // If decoding the image failed, delete the DB entry.
  database_->DeleteImage(suggestion_id.id_within_category());
  FetchImageFromNetwork(suggestion_id, url, ImageDataFetchedCallback(),
                        std::move(callback));
}

void CachedImageFetcher::FetchImageFromNetwork(
    const ContentSuggestion::ID& suggestion_id,
    const GURL& url,
    ImageDataFetchedCallback image_data_callback,
    ImageFetchedCallback image_callback) {
  if (url.is_empty() || !thumbnail_requests_throttler_.DemandQuotaForRequest(
                            /*interactive_request=*/true)) {
    // Return an empty image. Directly, this is never synchronous with the
    // original FetchSuggestionImage() call - an asynchronous database query has
    // happened in the meantime.
    if (image_data_callback) {
      std::move(image_data_callback).Run(std::string());
    }
    if (image_callback) {
      std::move(image_callback).Run(gfx::Image());
    }
    return;
  }
  // Image decoding callback only set when requested.
  image_fetcher::ImageFetcherCallback decode_callback;
  if (image_callback) {
    decode_callback =
        base::BindOnce(&CachedImageFetcher::OnImageDecodingDone,
                       base::Unretained(this), std::move(image_callback));
  }

  image_fetcher_->FetchImageAndData(
      suggestion_id.id_within_category(), url,
      base::BindOnce(&CachedImageFetcher::SaveImageAndInvokeDataCallback,
                     base::Unretained(this), suggestion_id.id_within_category(),
                     std::move(image_data_callback)),
      std::move(decode_callback), kTrafficAnnotation);
}

void CachedImageFetcher::SaveImageAndInvokeDataCallback(
    const std::string& id_within_category,
    ImageDataFetchedCallback callback,
    const std::string& image_data,
    const image_fetcher::RequestMetadata& request_metadata) {
  if (!image_data.empty()) {
    database_->SaveImage(id_within_category, image_data);
  }
  if (callback) {
    std::move(callback).Run(image_data);
  }
}

}  // namespace ntp_snippets