#ifndef CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_DELEGATE_PROXY_H_
#define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_DELEGATE_PROXY_H_
#include <stdint.h>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "content/browser/background_fetch/background_fetch_request_info.h"
#include "content/common/content_export.h"
#include "content/public/browser/background_fetch_delegate.h"
#include "content/public/browser/background_fetch_description.h"
#include "content/public/browser/background_fetch_response.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/blink/public/mojom/background_fetch/background_fetch.mojom.h"
class SkBitmap;
namespace content {
class BrowserContext;
class PermissionController;
class RenderFrameHostImpl;
class RenderProcessHost;
class StoragePartitionImpl;
class CONTENT_EXPORT BackgroundFetchDelegateProxy
: public BackgroundFetchDelegate::Client {
public:
using DispatchClickEventCallback =
base::RepeatingCallback<void(const std::string& unique_id)>;
using GetPermissionForOriginCallback =
base::OnceCallback<void(BackgroundFetchPermission)>;
class Controller {
public:
virtual void DidStartRequest(
const std::string& guid,
std::unique_ptr<BackgroundFetchResponse> response) = 0;
virtual void DidUpdateRequest(const std::string& guid,
uint64_t bytes_uploaded,
uint64_t bytes_downloaded) = 0;
virtual void DidCompleteRequest(
const std::string& guid,
std::unique_ptr<BackgroundFetchResult> result) = 0;
virtual void AbortFromDelegate(
blink::mojom::BackgroundFetchFailureReason) = 0;
virtual void GetUploadData(
const std::string& guid,
BackgroundFetchDelegate::GetUploadDataCallback callback) = 0;
virtual ~Controller() = default;
};
explicit BackgroundFetchDelegateProxy(
base::WeakPtr<StoragePartitionImpl> storage_partition);
BackgroundFetchDelegateProxy(const BackgroundFetchDelegateProxy&) = delete;
BackgroundFetchDelegateProxy& operator=(const BackgroundFetchDelegateProxy&) =
delete;
~BackgroundFetchDelegateProxy() override;
void SetClickEventDispatcher(DispatchClickEventCallback click_event_callback);
void GetIconDisplaySize(
BackgroundFetchDelegate::GetIconDisplaySizeCallback callback);
void GetPermissionForOrigin(const url::Origin& origin,
RenderProcessHost* rph,
RenderFrameHostImpl* rfh,
GetPermissionForOriginCallback callback);
void CreateDownloadJob(
base::WeakPtr<Controller> controller,
std::unique_ptr<BackgroundFetchDescription> fetch_description);
void StartRequest(const std::string& job_unique_id,
const url::Origin& origin,
const scoped_refptr<BackgroundFetchRequestInfo>& request);
void UpdateUI(
const std::string& job_unique_id,
const std::optional<std::string>& title,
const std::optional<SkBitmap>& icon,
blink::mojom::BackgroundFetchRegistrationService::UpdateUICallback
update_ui_callback);
void Abort(const std::string& job_unique_id);
void MarkJobComplete(const std::string& job_unique_id);
void Shutdown();
private:
void OnJobCancelled(
const std::string& job_unique_id,
const std::string& download_guid,
blink::mojom::BackgroundFetchFailureReason reason_to_abort) override;
void OnDownloadComplete(
const std::string& job_unique_id,
const std::string& guid,
std::unique_ptr<BackgroundFetchResult> result) override;
void OnDownloadUpdated(const std::string& job_unique_id,
const std::string& guid,
uint64_t bytes_uploaded,
uint64_t bytes_downloaded) override;
void OnDownloadStarted(
const std::string& job_unique_id,
const std::string& guid,
std::unique_ptr<BackgroundFetchResponse> response) override;
void OnUIActivated(const std::string& job_unique_id) override;
void OnUIUpdated(const std::string& job_unique_id) override;
void GetUploadData(
const std::string& job_unique_id,
const std::string& download_guid,
BackgroundFetchDelegate::GetUploadDataCallback callback) override;
void DidGetPermissionFromDownloadRequestLimiter(
GetPermissionForOriginCallback callback,
bool has_permission);
BrowserContext* GetBrowserContext();
BackgroundFetchDelegate* GetDelegate();
PermissionController* GetPermissionController();
std::map<std::string, base::WeakPtr<Controller>> controller_map_;
std::map<std::string,
blink::mojom::BackgroundFetchRegistrationService::UpdateUICallback>
update_ui_callback_map_;
DispatchClickEventCallback click_event_dispatcher_callback_;
base::WeakPtr<StoragePartitionImpl> storage_partition_;
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<BackgroundFetchDelegateProxy> weak_ptr_factory_{this};
};
}
#endif