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
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405

content / browser / background_fetch / background_fetch_job_controller.cc [blame]

// Copyright 2017 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/browser/background_fetch/background_fetch_job_controller.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/not_fatal_until.h"
#include "content/browser/background_fetch/background_fetch_cross_origin_filter.h"
#include "content/browser/background_fetch/background_fetch_data_manager.h"
#include "content/browser/background_fetch/background_fetch_request_match_params.h"
#include "content/public/browser/browser_thread.h"
#include "services/network/public/cpp/cors/cors.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "third_party/blink/public/mojom/background_fetch/background_fetch.mojom.h"

namespace content {

namespace {

// Performs mixed content checks on the |request| for Background Fetch.
// Background Fetch depends on Service Workers, which are restricted for use
// on secure origins. We can therefore assume that the registration's origin
// is secure. This test ensures that the origin for the url of every
// request is also secure.
bool IsMixedContent(const BackgroundFetchRequestInfo& request) {
  // Empty request is valid, it shouldn't fail the mixed content check.
  if (request.fetch_request()->url.is_empty())
    return false;

  return !network::IsUrlPotentiallyTrustworthy(request.fetch_request()->url);
}

// Whether the |request| needs CORS preflight.
// Requests that require CORS preflights are temporarily blocked, because the
// browser side of Background Fetch doesn't yet support performing CORS
// checks. TODO(crbug.com/40515511): Remove this temporary block.
bool RequiresCorsPreflight(const BackgroundFetchRequestInfo& request,
                           const url::Origin& origin) {
  const blink::mojom::FetchAPIRequestPtr& fetch_request =
      request.fetch_request();

  // Same origin requests don't require a CORS preflight.
  // https://fetch.spec.whatwg.org/#main-fetch
  // TODO(crbug.com/40515511): Make sure that cross-origin redirects are
  // disabled.
  if (url::IsSameOriginWith(origin.GetURL(), fetch_request->url))
    return false;

  // Requests that are more involved than what is possible with HTML's form
  // element require a CORS-preflight request.
  // https://fetch.spec.whatwg.org/#main-fetch
  if (!fetch_request->method.empty() &&
      !network::cors::IsCorsSafelistedMethod(fetch_request->method)) {
    return true;
  }

  net::HttpRequestHeaders::HeaderVector headers;
  for (const auto& header : fetch_request->headers)
    headers.emplace_back(header.first, header.second);

  return !network::cors::CorsUnsafeRequestHeaderNames(headers).empty();
}

}  // namespace

using blink::mojom::BackgroundFetchError;
using blink::mojom::BackgroundFetchFailureReason;

BackgroundFetchJobController::BackgroundFetchJobController(
    BackgroundFetchDataManager* data_manager,
    BackgroundFetchDelegateProxy* delegate_proxy,
    const BackgroundFetchRegistrationId& registration_id,
    blink::mojom::BackgroundFetchOptionsPtr options,
    const SkBitmap& icon,
    uint64_t bytes_downloaded,
    uint64_t bytes_uploaded,
    uint64_t upload_total,
    ProgressCallback progress_callback,
    FinishedCallback finished_callback)
    : data_manager_(data_manager),
      delegate_proxy_(delegate_proxy),
      registration_id_(registration_id),
      options_(std::move(options)),
      icon_(icon),
      complete_requests_downloaded_bytes_cache_(bytes_downloaded),
      complete_requests_uploaded_bytes_cache_(bytes_uploaded),
      upload_total_(upload_total),
      progress_callback_(std::move(progress_callback)),
      finished_callback_(std::move(finished_callback)) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
}

void BackgroundFetchJobController::InitializeRequestStatus(
    int completed_downloads,
    int total_downloads,
    std::vector<scoped_refptr<BackgroundFetchRequestInfo>>
        active_fetch_requests,
    bool start_paused,
    std::optional<net::IsolationInfo> isolation_info) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  // Don't allow double initialization.
  DCHECK_GT(total_downloads, 0);
  DCHECK_EQ(total_downloads_, 0);

  completed_downloads_ = completed_downloads;
  total_downloads_ = total_downloads;
  pending_downloads_ = active_fetch_requests.size();

  std::vector<std::string> active_guids;
  active_guids.reserve(active_fetch_requests.size());
  for (const auto& request_info : active_fetch_requests)
    active_guids.push_back(request_info->download_guid());

  auto fetch_description = std::make_unique<BackgroundFetchDescription>(
      registration_id().unique_id(), registration_id().storage_key().origin(),
      options_->title, icon_, completed_downloads_, total_downloads_,
      complete_requests_downloaded_bytes_cache_,
      complete_requests_uploaded_bytes_cache_, options_->download_total,
      upload_total_, std::move(active_guids), start_paused,
      std::move(isolation_info));

  for (auto& active_request : active_fetch_requests)
    active_request_map_[active_request->download_guid()] = active_request;

  delegate_proxy_->CreateDownloadJob(weak_ptr_factory_.GetWeakPtr(),
                                     std::move(fetch_description));
}

BackgroundFetchJobController::~BackgroundFetchJobController() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
}

bool BackgroundFetchJobController::HasMoreRequests() {
  return completed_downloads_ + pending_downloads_ < total_downloads_;
}

void BackgroundFetchJobController::StartRequest(
    scoped_refptr<BackgroundFetchRequestInfo> request,
    RequestFinishedCallback request_finished_callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  DCHECK_LT(completed_downloads_, total_downloads_);
  DCHECK(request_finished_callback);
  DCHECK(request);

  active_request_finished_callbacks_.emplace(
      request->download_guid(), std::move(request_finished_callback));

  if (IsMixedContent(*request.get()) ||
      RequiresCorsPreflight(*request.get(),
                            registration_id_.storage_key().origin())) {
    request->SetEmptyResultWithFailureReason(
        BackgroundFetchResult::FailureReason::FETCH_ERROR);

    NotifyDownloadComplete(std::move(request));
    return;
  }

  active_request_map_[request->download_guid()] = request;
  delegate_proxy_->StartRequest(registration_id().unique_id(),
                                registration_id().storage_key().origin(),
                                request.get());
}

void BackgroundFetchJobController::DidStartRequest(
    const std::string& guid,
    std::unique_ptr<BackgroundFetchResponse> response) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  DCHECK(active_request_map_.count(guid));
  const auto& request = active_request_map_[guid];
  DCHECK(request);

  request->PopulateWithResponse(std::move(response));

  // TODO(crbug.com/40593934): Stop the fetch if the cross origin filter fails.
  BackgroundFetchCrossOriginFilter filter(
      registration_id_.storage_key().origin(), *request);
  request->set_can_populate_body(filter.CanPopulateBody());
  if (!request->can_populate_body())
    has_failed_cors_request_ = true;
}

void BackgroundFetchJobController::DidUpdateRequest(const std::string& guid,
                                                    uint64_t bytes_uploaded,
                                                    uint64_t bytes_downloaded) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  DCHECK(active_request_map_.count(guid));
  const auto& request = active_request_map_[guid];
  DCHECK(request);
  InProgressRequestBytes& in_progress_bytes = active_bytes_map_[guid];

  // Don't send download updates so the size is not leaked.
  // Upload updates are fine since that information is already available.
  if (!request->can_populate_body() && bytes_downloaded > 0u)
    return;

  if (in_progress_bytes.downloaded == bytes_downloaded &&
      in_progress_bytes.uploaded == bytes_uploaded) {
    return;
  }

  in_progress_bytes.downloaded = bytes_downloaded;
  in_progress_bytes.uploaded = bytes_uploaded;

  auto registration_data = NewRegistrationData();
  registration_data->downloaded += GetInProgressDownloadedBytes();
  registration_data->uploaded += GetInProgressUploadedBytes();
  progress_callback_.Run(registration_id_.unique_id(), *registration_data);
}

void BackgroundFetchJobController::DidCompleteRequest(
    const std::string& guid,
    std::unique_ptr<BackgroundFetchResult> result) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  DCHECK(active_request_map_.count(guid));
  const auto& request = active_request_map_[guid];
  DCHECK(request);

  request->SetResult(std::move(result));

  if (request->can_populate_body())
    complete_requests_downloaded_bytes_cache_ += request->GetResponseSize();
  complete_requests_uploaded_bytes_cache_ += request->request_body_size();

  NotifyDownloadComplete(request);
  active_bytes_map_.erase(guid);
  active_request_map_.erase(guid);
}

blink::mojom::BackgroundFetchRegistrationDataPtr
BackgroundFetchJobController::NewRegistrationData() const {
  return blink::mojom::BackgroundFetchRegistrationData::New(
      registration_id().developer_id(), upload_total_,
      complete_requests_uploaded_bytes_cache_, options_->download_total,
      complete_requests_downloaded_bytes_cache_,
      blink::mojom::BackgroundFetchResult::UNSET, failure_reason_);
}

uint64_t BackgroundFetchJobController::GetInProgressDownloadedBytes() {
  uint64_t bytes = 0u;
  for (const std::pair<const std::string, InProgressRequestBytes>&
           in_progress_bytes : active_bytes_map_) {
    bytes += in_progress_bytes.second.downloaded;
  }
  return bytes;
}

uint64_t BackgroundFetchJobController::GetInProgressUploadedBytes() {
  uint64_t bytes = 0u;
  for (const std::pair<const std::string, InProgressRequestBytes>&
           in_progress_bytes : active_bytes_map_) {
    bytes += in_progress_bytes.second.uploaded;
  }
  return bytes;
}

void BackgroundFetchJobController::AbortFromDelegate(
    BackgroundFetchFailureReason failure_reason) {
  if (failure_reason == BackgroundFetchFailureReason::DOWNLOAD_TOTAL_EXCEEDED &&
      has_failed_cors_request_) {
    // Don't expose that the download total has been exceeded. Use a less
    // specific error.
    failure_reason_ = BackgroundFetchFailureReason::FETCH_ERROR;
  } else {
    failure_reason_ = failure_reason;
  }

  Finish(failure_reason_, base::DoNothing());
}

void BackgroundFetchJobController::Abort(
    BackgroundFetchFailureReason failure_reason,
    ErrorCallback callback) {
  failure_reason_ = failure_reason;

  // Cancel any in-flight downloads and UI through the BGFetchDelegate.
  delegate_proxy_->Abort(registration_id().unique_id());

  Finish(failure_reason_, std::move(callback));
}

void BackgroundFetchJobController::Finish(
    BackgroundFetchFailureReason reason_to_abort,
    ErrorCallback callback) {
  DCHECK(reason_to_abort != BackgroundFetchFailureReason::NONE ||
         !HasMoreRequests());

  // Race conditions make it possible for a controller to finish twice. This
  // should be removed when the scheduler starts owning the controllers.
  if (!finished_callback_) {
    std::move(callback).Run(BackgroundFetchError::INVALID_ID);
    return;
  }

  std::move(finished_callback_)
      .Run(registration_id_, reason_to_abort, std::move(callback));
}

void BackgroundFetchJobController::PopNextRequest(
    RequestStartedCallback request_started_callback,
    RequestFinishedCallback request_finished_callback) {
  DCHECK(HasMoreRequests());

  ++pending_downloads_;
  data_manager_->PopNextRequest(
      registration_id(),
      base::BindOnce(&BackgroundFetchJobController::DidPopNextRequest,
                     weak_ptr_factory_.GetWeakPtr(),
                     std::move(request_started_callback),
                     std::move(request_finished_callback)));
}

void BackgroundFetchJobController::DidPopNextRequest(
    RequestStartedCallback request_started_callback,
    RequestFinishedCallback request_finished_callback,
    BackgroundFetchError error,
    scoped_refptr<BackgroundFetchRequestInfo> request_info) {
  if (error != BackgroundFetchError::NONE) {
    Abort(BackgroundFetchFailureReason::SERVICE_WORKER_UNAVAILABLE,
          base::DoNothing());
    return;
  }

  std::move(request_started_callback)
      .Run(registration_id(), request_info.get());
  StartRequest(std::move(request_info), std::move(request_finished_callback));
}

void BackgroundFetchJobController::MarkRequestAsComplete(
    scoped_refptr<BackgroundFetchRequestInfo> request_info) {
  data_manager_->MarkRequestAsComplete(
      registration_id(), std::move(request_info),
      base::BindOnce(&BackgroundFetchJobController::DidMarkRequestAsComplete,
                     weak_ptr_factory_.GetWeakPtr()));
}

void BackgroundFetchJobController::DidMarkRequestAsComplete(
    BackgroundFetchError error) {
  switch (error) {
    case BackgroundFetchError::NONE:
      break;
    case BackgroundFetchError::STORAGE_ERROR:
      Abort(BackgroundFetchFailureReason::SERVICE_WORKER_UNAVAILABLE,
            base::DoNothing());
      return;
    case BackgroundFetchError::QUOTA_EXCEEDED:
      Abort(BackgroundFetchFailureReason::QUOTA_EXCEEDED, base::DoNothing());
      return;
    default:
      NOTREACHED_IN_MIGRATION();
  }

  if (completed_downloads_ == total_downloads_) {
    Finish(BackgroundFetchFailureReason::NONE, base::DoNothing());
    return;
  }
}

void BackgroundFetchJobController::NotifyDownloadComplete(
    scoped_refptr<BackgroundFetchRequestInfo> request) {
  --pending_downloads_;
  ++completed_downloads_;
  auto it = active_request_finished_callbacks_.find(request->download_guid());
  CHECK(it != active_request_finished_callbacks_.end(),
        base::NotFatalUntil::M130);
  std::move(it->second).Run(registration_id(), std::move(request));
  active_request_finished_callbacks_.erase(it);
}

void BackgroundFetchJobController::GetUploadData(
    const std::string& guid,
    BackgroundFetchDelegate::GetUploadDataCallback callback) {
  DCHECK(active_request_map_.count(guid));
  const auto& request = active_request_map_[guid];
  DCHECK(request);

  data_manager_->GetRequestBlob(
      registration_id(), request,
      base::BindOnce(&BackgroundFetchJobController::DidGetUploadData,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void BackgroundFetchJobController::DidGetUploadData(
    BackgroundFetchDelegate::GetUploadDataCallback callback,
    BackgroundFetchError error,
    blink::mojom::SerializedBlobPtr blob) {
  if (error != BackgroundFetchError::NONE) {
    Abort(BackgroundFetchFailureReason::SERVICE_WORKER_UNAVAILABLE,
          base::DoNothing());
    std::move(callback).Run(nullptr);
    return;
  }

  DCHECK(blob);
  std::move(callback).Run(std::move(blob));
}

}  // namespace content