File: quick_insert_asset_fetcher_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (199 lines) | stat: -rw-r--r-- 7,225 bytes parent folder | download | duplicates (6)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/quick_insert/quick_insert_asset_fetcher_impl.h"

#include <memory>
#include <string>
#include <utility>

#include "ash/public/cpp/image_util.h"
#include "ash/quick_insert/quick_insert_asset_fetcher.h"
#include "ash/quick_insert/quick_insert_asset_fetcher_impl_delegate.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/rand_util.h"
#include "base/task/sequenced_task_runner.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/data_decoder/public/mojom/image_decoder.mojom-shared.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/fetch_api.mojom-shared.h"
#include "url/gurl.h"
#include "url/url_constants.h"

namespace ash {
namespace {

using DownloadGifMediaToStringCallback =
    base::OnceCallback<void(const std::string& gif_media_data)>;

bool IsValidGifMediaUrl(const GURL& url) {
  // TODO: b/323784358 - Check requirements for validating GIF urls.
  return url.DomainIs("media.tenor.com") && url.SchemeIs(url::kHttpsScheme);
}

void OnGifMediaDownloaded(base::WeakPtr<const network::SimpleURLLoader> loader,
                          DownloadGifMediaToStringCallback callback,
                          std::unique_ptr<std::string> response_body) {
  if (loader && loader->NetError() == net::OK && response_body) {
    std::move(callback).Run(*response_body);
    return;
  }
  // TODO: b/325368650 - Determine how network errors should be handled.
  std::move(callback).Run(std::string());
}

// Creates a network request for GIF without starting the request.
std::unique_ptr<network::SimpleURLLoader> CreateGifMediaRequest(
    const GURL& url) {
  if (!IsValidGifMediaUrl(url)) {
    return nullptr;
  }

  auto resource_request = std::make_unique<network::ResourceRequest>();
  resource_request->url = url;
  resource_request->method = "GET";
  resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;

  constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation =
      net::DefineNetworkTrafficAnnotation(
          "chromeos_quick_insert_gif_media_fetcher",
          R"(
      semantics {
        sender: "ChromeOS Quick Insert"
        description:
          "Fetches a GIF or GIF preview from Tenor for the specified url. This "
          "is used to show GIFs and GIF preview images in ChromeOS Quick "
          "Insert, which users can select to insert the GIF into supported "
          "textfields."
        trigger:
          "Triggered when the user opens ChromeOS Quick Insert."
        data:
          "A GIF ID to specify the GIF to fetch."
        destination: GOOGLE_OWNED_SERVICE
        internal {
          contacts {
              email: "e14s-eng@google.com"
          }
        }
        user_data {
          type: NONE
        }
        last_reviewed: "2024-10-15"
      }
      policy {
        cookies_allowed: NO
        setting:
          "No setting. Users must take explicit action to trigger the feature."
        policy_exception_justification:
          "Not implemented, not considered useful. This request is part of a "
          "flow which is user-initiated."
      }
  )");
  return network::SimpleURLLoader::Create(std::move(resource_request),
                                          kTrafficAnnotation);
}

// Downloads a gif or gif preview using `loader`. If the download is successful,
// the gif is passed to `callback` as a string of encoded bytes in gif or png
// format. Otherwise, `callback` is run with an empty string.
void DownloadGifMediaToString(
    base::WeakPtr<const network::SimpleURLLoader> loader,
    scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory,
    DownloadGifMediaToStringCallback callback) {
  if (!loader) {
    return;
  }

  // This is safe because `loader` is a WeakPtr to the non-const SimpleURLLoader
  // created by `CreateGifMediaRequest`.
  const_cast<network::SimpleURLLoader*>(loader.get())
      ->DownloadToString(
          shared_url_loader_factory.get(),
          base::BindOnce(&OnGifMediaDownloaded, loader, std::move(callback)),
          network::SimpleURLLoader::kMaxBoundedStringDownloadSize);
}

void RunImmediatelyOrWithDelay(base::OnceClosure closure,
                               base::TimeDelta delay) {
  if (delay.is_zero()) {
    std::move(closure).Run();
  } else {
    base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
        FROM_HERE, std::move(closure), delay);
  }
}

// Returns the delay for the request with `rank`.
// The delay should increase with `rank` to encourage lower rank requests to
// finish first and avoid congestion.
base::TimeDelta GetRequestDelay(size_t rank) {
  // The top 4 results should be fetched instantly, since they are likely to be
  // visible above the fold.
  if (rank < 4) {
    return base::Seconds(0);
  }
  // The remaining results can be fetched later since they are likely to be
  // below the fold.
  return base::Milliseconds(200) + rank * base::Milliseconds(100);
}

}  // namespace

QuickInsertAssetFetcherImpl::QuickInsertAssetFetcherImpl(
    QuickInsertAssetFetcherImplDelegate* delegate)
    : delegate_(delegate) {}

QuickInsertAssetFetcherImpl::~QuickInsertAssetFetcherImpl() = default;

std::unique_ptr<network::SimpleURLLoader>
QuickInsertAssetFetcherImpl::FetchGifFromUrl(
    const GURL& url,
    size_t rank,
    QuickInsertGifFetchedCallback callback) {
  std::unique_ptr<network::SimpleURLLoader> loader = CreateGifMediaRequest(url);
  if (loader == nullptr) {
    std::move(callback).Run({});
    return nullptr;
  }
  RunImmediatelyOrWithDelay(
      base::BindOnce(&DownloadGifMediaToString, loader->GetWeakPtr(),
                     delegate_->GetSharedURLLoaderFactory(),
                     base::BindOnce(&image_util::DecodeAnimationData,
                                    std::move(callback))),
      GetRequestDelay(rank) + base::Milliseconds(200));
  return loader;
}

std::unique_ptr<network::SimpleURLLoader>
QuickInsertAssetFetcherImpl::FetchGifPreviewImageFromUrl(
    const GURL& url,
    size_t rank,
    QuickInsertImageFetchedCallback callback) {
  std::unique_ptr<network::SimpleURLLoader> loader = CreateGifMediaRequest(url);
  if (loader == nullptr) {
    std::move(callback).Run({});
    return nullptr;
  }
  RunImmediatelyOrWithDelay(
      base::BindOnce(
          &DownloadGifMediaToString, loader->GetWeakPtr(),
          delegate_->GetSharedURLLoaderFactory(),
          base::BindOnce(&image_util::DecodeImageData, std::move(callback),
                         data_decoder::mojom::ImageCodec::kDefault)),
      GetRequestDelay(rank));
  return loader;
}

void QuickInsertAssetFetcherImpl::FetchFileThumbnail(
    const base::FilePath& path,
    const gfx::Size& size,
    FetchFileThumbnailCallback callback) {
  delegate_->FetchFileThumbnail(path, size, std::move(callback));
}

}  // namespace ash