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

content / public / browser / background_fetch_response.h [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.

#ifndef CONTENT_PUBLIC_BROWSER_BACKGROUND_FETCH_RESPONSE_H_
#define CONTENT_PUBLIC_BROWSER_BACKGROUND_FETCH_RESPONSE_H_

#include <optional>
#include <vector>

#include "base/files/file_path.h"
#include "base/time/time.h"
#include "content/common/content_export.h"
#include "net/http/http_response_headers.h"
#include "storage/browser/blob/blob_data_handle.h"
#include "url/gurl.h"

namespace content {

// Contains the response after a background fetch has started.
struct CONTENT_EXPORT BackgroundFetchResponse {
  BackgroundFetchResponse(
      const std::vector<GURL>& url_chain,
      const scoped_refptr<const net::HttpResponseHeaders>& headers);

  BackgroundFetchResponse(const BackgroundFetchResponse&) = delete;
  BackgroundFetchResponse& operator=(const BackgroundFetchResponse&) = delete;

  ~BackgroundFetchResponse();

  const std::vector<GURL> url_chain;
  const scoped_refptr<const net::HttpResponseHeaders> headers;  // May be null.
};

struct CONTENT_EXPORT BackgroundFetchResult {
  // Failures that happen after the download has already started and are
  // reported via |BackgroundFetchDelegate::Client::OnDownloadComplete|.
  enum class FailureReason {
    // None of below failures occurred, although the fetch could still have
    // failed with an error code such as 404.
    NONE,

    // Used when the download has been aborted after reaching a threshold where
    // it was decided it is not worth attempting to start again. This could be
    // either due to a specific number of failed retry attempts or a specific
    // number of wasted bytes due to the download restarting.
    NETWORK,

    // Used when the download was not completed before the timeout.
    TIMEDOUT,

    // Used when the download was cancelled by the user.
    CANCELLED,

    // Catch-all error. Used when the failure reason is unknown or not exposed
    // to the developer.
    FETCH_ERROR,
  };

  // Constructor for failed downloads.
  BackgroundFetchResult(std::unique_ptr<BackgroundFetchResponse> response,
                        base::Time response_time,
                        FailureReason failure_reason);

  // Constructor for successful downloads.
  BackgroundFetchResult(std::unique_ptr<BackgroundFetchResponse> response,
                        base::Time response_time,
                        const base::FilePath& path,
                        std::optional<storage::BlobDataHandle> blob_handle,
                        uint64_t file_size);

  BackgroundFetchResult(const BackgroundFetchResult&) = delete;
  BackgroundFetchResult& operator=(const BackgroundFetchResult&) = delete;

  ~BackgroundFetchResult();

  std::unique_ptr<BackgroundFetchResponse> response;
  const base::Time response_time;
  const base::FilePath file_path;
  std::optional<storage::BlobDataHandle> blob_handle;
  const uint64_t file_size = 0;
  FailureReason failure_reason;
};

}  // namespace content

#endif  // CONTENT_PUBLIC_BROWSER_BACKGROUND_FETCH_RESPONSE_H_