#ifndef CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_CONTEXT_H_
#define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_CONTEXT_H_
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "base/gtest_prod_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/ref_counted_delete_on_sequence.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "content/browser/background_fetch/background_fetch_delegate_proxy.h"
#include "content/browser/background_fetch/background_fetch_event_dispatcher.h"
#include "content/browser/background_fetch/storage/get_initialization_data_task.h"
#include "content/browser/devtools/devtools_background_services_context_impl.h"
#include "content/common/content_export.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/background_fetch/background_fetch.mojom.h"
namespace storage {
class QuotaManagerProxy;
}
namespace content {
class BackgroundFetchDataManager;
class BackgroundFetchRegistrationId;
class BackgroundFetchRegistrationNotifier;
class BackgroundFetchRequestMatchParams;
class BackgroundFetchScheduler;
class RenderFrameHostImpl;
class ServiceWorkerContextWrapper;
class StoragePartitionImpl;
class CONTENT_EXPORT BackgroundFetchContext
: public base::RefCounted<BackgroundFetchContext> {
public:
BackgroundFetchContext(
base::WeakPtr<StoragePartitionImpl> storage_partition,
const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context,
scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
DevToolsBackgroundServicesContextImpl& devtools_context);
BackgroundFetchContext(const BackgroundFetchContext&) = delete;
BackgroundFetchContext& operator=(const BackgroundFetchContext&) = delete;
void Initialize();
void Shutdown();
void GetRegistration(
int64_t service_worker_registration_id,
const blink::StorageKey& storage_key,
const std::string& developer_id,
blink::mojom::BackgroundFetchService::GetRegistrationCallback callback);
void GetDeveloperIdsForServiceWorker(
int64_t service_worker_registration_id,
const blink::StorageKey& storage_key,
blink::mojom::BackgroundFetchService::GetDeveloperIdsCallback callback);
void StartFetch(const BackgroundFetchRegistrationId& registration_id,
std::vector<blink::mojom::FetchAPIRequestPtr> requests,
blink::mojom::BackgroundFetchOptionsPtr options,
const SkBitmap& icon,
blink::mojom::BackgroundFetchUkmDataPtr ukm_data,
RenderProcessHost* rph,
RenderFrameHostImpl* rfh,
const net::IsolationInfo& isolation_info,
blink::mojom::BackgroundFetchService::FetchCallback callback);
void GetIconDisplaySize(
blink::mojom::BackgroundFetchService::GetIconDisplaySizeCallback
callback);
void MatchRequests(
const BackgroundFetchRegistrationId& registration_id,
std::unique_ptr<BackgroundFetchRequestMatchParams> match_params,
blink::mojom::BackgroundFetchRegistrationService::MatchRequestsCallback
callback);
void Abort(
const BackgroundFetchRegistrationId& registration_id,
blink::mojom::BackgroundFetchRegistrationService::AbortCallback callback);
void AddRegistrationObserver(
const std::string& unique_id,
mojo::PendingRemote<blink::mojom::BackgroundFetchRegistrationObserver>
observer);
void UpdateUI(
const BackgroundFetchRegistrationId& registration_id,
const std::optional<std::string>& title,
const std::optional<SkBitmap>& icon,
blink::mojom::BackgroundFetchRegistrationService::UpdateUICallback
callback);
BackgroundFetchRegistrationNotifier* registration_notifier() const {
return registration_notifier_.get();
}
base::WeakPtr<BackgroundFetchContext> GetWeakPtr();
private:
using GetPermissionCallback =
base::OnceCallback<void(BackgroundFetchPermission)>;
FRIEND_TEST_ALL_PREFIXES(BackgroundFetchServiceTest,
JobsInitializedOnBrowserRestart);
friend class BackgroundFetchServiceTest;
friend class BackgroundFetchJobControllerTest;
friend class base::RefCounted<BackgroundFetchContext>;
~BackgroundFetchContext();
void DidGetRegistration(
blink::mojom::BackgroundFetchService::GetRegistrationCallback callback,
blink::mojom::BackgroundFetchError error,
BackgroundFetchRegistrationId registration_id,
blink::mojom::BackgroundFetchRegistrationDataPtr registration_data);
void DidCreateRegistration(
const BackgroundFetchRegistrationId& registration_id,
blink::mojom::BackgroundFetchError error,
blink::mojom::BackgroundFetchRegistrationDataPtr registration_data);
void DidGetMatchingRequests(
const std::string& unique_id,
blink::mojom::BackgroundFetchRegistrationService::MatchRequestsCallback
callback,
blink::mojom::BackgroundFetchError error,
std::vector<blink::mojom::BackgroundFetchSettledFetchPtr>
settled_fetches);
void DidGetInitializationData(
blink::mojom::BackgroundFetchError error,
std::vector<background_fetch::BackgroundFetchInitializationData>
initialization_data);
void SetDataManagerForTesting(
std::unique_ptr<BackgroundFetchDataManager> data_manager);
void DidGetPermission(const BackgroundFetchRegistrationId& registration_id,
std::vector<blink::mojom::FetchAPIRequestPtr> requests,
blink::mojom::BackgroundFetchOptionsPtr options,
const SkBitmap& icon,
blink::mojom::BackgroundFetchUkmDataPtr ukm_data,
const GlobalRenderFrameHostId& rfh_id,
const net::IsolationInfo& isolation_info,
BackgroundFetchPermission permission);
std::unique_ptr<BackgroundFetchDataManager> data_manager_;
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
raw_ptr<DevToolsBackgroundServicesContextImpl> devtools_context_;
std::unique_ptr<BackgroundFetchRegistrationNotifier> registration_notifier_;
BackgroundFetchDelegateProxy delegate_proxy_;
std::unique_ptr<BackgroundFetchScheduler> scheduler_;
std::map<BackgroundFetchRegistrationId,
blink::mojom::BackgroundFetchService::FetchCallback>
fetch_callbacks_;
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<BackgroundFetchContext> weak_factory_{
this};
};
}
#endif