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

content / browser / manifest / manifest_icon_downloader.cc [blame]

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/public/browser/manifest_icon_downloader.h"

#include <stddef.h>

#include <limits>
#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/numerics/safe_conversions.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "skia/ext/image_operations.h"
#include "third_party/blink/public/mojom/devtools/console_message.mojom.h"

namespace content {

bool ManifestIconDownloader::Download(
    WebContents* web_contents,
    const GURL& icon_url,
    int ideal_icon_size_in_px,
    int minimum_icon_size_in_px,
    int maximum_icon_size_in_px,
    IconFetchCallback callback,
    bool square_only,
    const GlobalRenderFrameHostId& initiator_frame_routing_id) {
  DCHECK(minimum_icon_size_in_px <= ideal_icon_size_in_px);
  if (!web_contents || !icon_url.is_valid())
    return false;

  const gfx::Size preferred_size(ideal_icon_size_in_px, ideal_icon_size_in_px);
  web_contents->DownloadImageInFrame(
      initiator_frame_routing_id, icon_url,
      false,                    // is_favicon
      preferred_size,           // preferred_size
      maximum_icon_size_in_px,  // max_bitmap_size - 0 means no maximum size.
      false,                    // bypass_cache
      base::BindOnce(&ManifestIconDownloader::OnIconFetched,
                     ideal_icon_size_in_px, minimum_icon_size_in_px,
                     square_only, web_contents->GetWeakPtr(),
                     std::move(callback)));
  return true;
}

void ManifestIconDownloader::OnIconFetched(
    int ideal_icon_size_in_px,
    int minimum_icon_size_in_px,
    bool square_only,
    base::WeakPtr<WebContents> web_contents,
    IconFetchCallback callback,
    int id,
    int http_status_code,
    const GURL& url,
    const std::vector<SkBitmap>& bitmaps,
    const std::vector<gfx::Size>& sizes) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  if (bitmaps.empty()) {
    if (web_contents) {
      web_contents->GetPrimaryMainFrame()->AddMessageToConsole(
          blink::mojom::ConsoleMessageLevel::kError,
          "Error while trying to use the following icon from the Manifest: " +
              url.spec() + " (Download error or resource isn't a valid image)");
    }

    std::move(callback).Run(SkBitmap());
    return;
  }

  const int closest_index = FindClosestBitmapIndex(
      ideal_icon_size_in_px, minimum_icon_size_in_px, square_only, bitmaps);

  if (closest_index == -1) {
    if (web_contents) {
      web_contents->GetPrimaryMainFrame()->AddMessageToConsole(
          blink::mojom::ConsoleMessageLevel::kError,
          "Error while trying to use the following icon from the Manifest: " +
              url.spec() +
              " (Resource size is not correct - typo in the Manifest?)");
    }

    std::move(callback).Run(SkBitmap());
    return;
  }

  const SkBitmap& chosen = bitmaps[closest_index];
  float ratio = 1.0;
  // Preserve width/height ratio if non-square icons allowed.
  if (!square_only && !chosen.empty()) {
    ratio = base::checked_cast<float>(chosen.width()) /
            base::checked_cast<float>(chosen.height());
  }
  float ideal_icon_width_in_px = ratio * ideal_icon_size_in_px;
  // Only scale if we need to scale down. For scaling up we will let the system
  // handle that when it is required to display it. This saves space in the
  // webapp storage system as well.
  if (chosen.height() > ideal_icon_size_in_px ||
      chosen.width() > ideal_icon_width_in_px) {
    GetIOThreadTaskRunner({})->PostTask(
        FROM_HERE, base::BindOnce(&ManifestIconDownloader::ScaleIcon,
                                  ideal_icon_width_in_px, ideal_icon_size_in_px,
                                  chosen, std::move(callback)));
    return;
  }

  std::move(callback).Run(chosen);
}

void ManifestIconDownloader::ScaleIcon(int ideal_icon_width_in_px,
                                       int ideal_icon_height_in_px,
                                       const SkBitmap& bitmap,
                                       IconFetchCallback callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  const SkBitmap& scaled = skia::ImageOperations::Resize(
      bitmap, skia::ImageOperations::RESIZE_BEST, ideal_icon_width_in_px,
      ideal_icon_height_in_px);

  GetUIThreadTaskRunner({})->PostTask(
      FROM_HERE, base::BindOnce(std::move(callback), scaled));
}

int ManifestIconDownloader::FindClosestBitmapIndex(
    int ideal_icon_size_in_px,
    int minimum_icon_size_in_px,
    bool square_only,
    const std::vector<SkBitmap>& bitmaps) {
  int best_index = -1;
  int best_delta = std::numeric_limits<int>::min();
  const int max_negative_delta =
      minimum_icon_size_in_px - ideal_icon_size_in_px;

  for (size_t i = 0; i < bitmaps.size(); ++i) {
    if (bitmaps[i].empty())
      continue;

    // Check for valid width/height ratio.
    float width = base::checked_cast<float>(bitmaps[i].width());
    float height = base::checked_cast<float>(bitmaps[i].height());
    float ratio = width / height;
    if (ratio < 1 || ratio > kMaxWidthToHeightRatio)
      continue;
    if (square_only && ratio != 1)
      continue;

    int delta = bitmaps[i].height() - ideal_icon_size_in_px;
    if (delta == 0)
      return i;

    if (best_delta > 0 && delta < 0)
      continue;

    if ((best_delta > 0 && delta < best_delta) ||
        (best_delta < 0 && delta > best_delta && delta >= max_negative_delta)) {
      best_index = i;
      best_delta = delta;
    }
  }

  if (best_index != -1)
    return best_index;

  // There was no square/landscape icon of a correct size found. Try to find the
  // most square-like icon which has both dimensions greater than the minimum
  // size.
  float best_ratio_difference = std::numeric_limits<float>::infinity();
  for (size_t i = 0; i < bitmaps.size(); ++i) {
    if (bitmaps[i].height() < minimum_icon_size_in_px ||
        bitmaps[i].width() < minimum_icon_size_in_px) {
      continue;
    }

    float height = static_cast<float>(bitmaps[i].height());
    float width = static_cast<float>(bitmaps[i].width());
    float ratio = width / height;
    if (!square_only && ratio > kMaxWidthToHeightRatio)
      continue;
    float ratio_difference = fabs(ratio - 1);
    if (ratio_difference < best_ratio_difference) {
      best_index = i;
      best_ratio_difference = ratio_difference;
    }
  }

  return best_index;
}

}  // namespace content