#ifndef CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_JOB_CONTROLLER_H_
#define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_JOB_CONTROLLER_H_
#include <stdint.h>
#include <memory>
#include <string>
#include <vector>
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "content/browser/background_fetch/background_fetch_delegate_proxy.h"
#include "content/browser/background_fetch/background_fetch_registration_id.h"
#include "content/browser/background_fetch/background_fetch_request_info.h"
#include "content/browser/background_fetch/background_fetch_scheduler.h"
#include "content/common/background_fetch/background_fetch_types.h"
#include "content/common/content_export.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace content {
class BackgroundFetchDataManager;
class CONTENT_EXPORT BackgroundFetchJobController
: public BackgroundFetchDelegateProxy::Controller {
public:
using ErrorCallback =
base::OnceCallback<void(blink::mojom::BackgroundFetchError)>;
using FinishedCallback =
base::OnceCallback<void(const BackgroundFetchRegistrationId&,
blink::mojom::BackgroundFetchFailureReason,
ErrorCallback)>;
using ProgressCallback = base::RepeatingCallback<void(
const std::string& unique_id,
const blink::mojom::BackgroundFetchRegistrationData&)>;
using RequestStartedCallback =
base::OnceCallback<void(const BackgroundFetchRegistrationId&,
const BackgroundFetchRequestInfo*)>;
using RequestFinishedCallback =
base::OnceCallback<void(const BackgroundFetchRegistrationId&,
scoped_refptr<BackgroundFetchRequestInfo>)>;
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);
BackgroundFetchJobController(const BackgroundFetchJobController&) = delete;
BackgroundFetchJobController& operator=(const BackgroundFetchJobController&) =
delete;
~BackgroundFetchJobController() override;
void InitializeRequestStatus(
int completed_downloads,
int total_downloads,
std::vector<scoped_refptr<BackgroundFetchRequestInfo>>
active_fetch_requests,
bool start_paused,
std::optional<net::IsolationInfo> isolation_info);
uint64_t GetInProgressDownloadedBytes();
uint64_t GetInProgressUploadedBytes();
blink::mojom::BackgroundFetchRegistrationDataPtr NewRegistrationData() const;
const BackgroundFetchRegistrationId& registration_id() const {
return registration_id_;
}
void DidStartRequest(
const std::string& guid,
std::unique_ptr<BackgroundFetchResponse> response) override;
void DidUpdateRequest(const std::string& guid,
uint64_t bytes_uploaded,
uint64_t bytes_downloaded) override;
void DidCompleteRequest(
const std::string& guid,
std::unique_ptr<BackgroundFetchResult> result) override;
void AbortFromDelegate(
blink::mojom::BackgroundFetchFailureReason failure_reason) override;
void GetUploadData(
const std::string& guid,
BackgroundFetchDelegate::GetUploadDataCallback callback) override;
void Abort(blink::mojom::BackgroundFetchFailureReason failure_reason,
ErrorCallback callback);
void PopNextRequest(RequestStartedCallback request_started_callback,
RequestFinishedCallback request_finished_callback);
void DidPopNextRequest(
RequestStartedCallback request_started_callback,
RequestFinishedCallback request_finished_callback,
blink::mojom::BackgroundFetchError error,
scoped_refptr<BackgroundFetchRequestInfo> request_info);
void StartRequest(scoped_refptr<BackgroundFetchRequestInfo> request,
RequestFinishedCallback request_finished_callback);
void MarkRequestAsComplete(scoped_refptr<BackgroundFetchRequestInfo> request);
bool HasMoreRequests();
int pending_downloads() const { return pending_downloads_; }
private:
struct InProgressRequestBytes {
uint64_t uploaded = 0u;
uint64_t downloaded = 0u;
};
void DidMarkRequestAsComplete(blink::mojom::BackgroundFetchError error);
void NotifyDownloadComplete(
scoped_refptr<BackgroundFetchRequestInfo> request);
void Finish(blink::mojom::BackgroundFetchFailureReason reason_to_abort,
ErrorCallback callback);
void DidGetUploadData(BackgroundFetchDelegate::GetUploadDataCallback callback,
blink::mojom::BackgroundFetchError error,
blink::mojom::SerializedBlobPtr blob);
raw_ptr<BackgroundFetchDataManager> data_manager_;
raw_ptr<BackgroundFetchDelegateProxy> delegate_proxy_;
std::map<std::string, scoped_refptr<BackgroundFetchRequestInfo>>
active_request_map_;
std::map<std::string, InProgressRequestBytes> active_bytes_map_;
BackgroundFetchRegistrationId registration_id_;
blink::mojom::BackgroundFetchOptionsPtr options_;
SkBitmap icon_;
std::map<std::string, RequestFinishedCallback>
active_request_finished_callbacks_;
uint64_t complete_requests_downloaded_bytes_cache_;
uint64_t complete_requests_uploaded_bytes_cache_;
uint64_t upload_total_;
ProgressCallback progress_callback_;
int total_downloads_ = 0;
int completed_downloads_ = 0;
int pending_downloads_ = 0;
blink::mojom::BackgroundFetchFailureReason failure_reason_ =
blink::mojom::BackgroundFetchFailureReason::NONE;
bool has_failed_cors_request_ = false;
FinishedCallback finished_callback_;
base::WeakPtrFactory<BackgroundFetchJobController> weak_ptr_factory_{this};
};
}
#endif