#ifndef CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_EVENT_DISPATCHER_H_
#define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_EVENT_DISPATCHER_H_
#include <stdint.h>
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "content/browser/service_worker/service_worker_metrics.h"
#include "content/common/content_export.h"
#include "third_party/blink/public/mojom/background_fetch/background_fetch.mojom.h"
namespace content {
class BackgroundFetchContext;
class BackgroundFetchRegistrationId;
class DevToolsBackgroundServicesContextImpl;
class ServiceWorkerContextWrapper;
class ServiceWorkerRegistration;
class ServiceWorkerVersion;
class CONTENT_EXPORT BackgroundFetchEventDispatcher {
public:
enum DispatchResult {
DISPATCH_RESULT_SUCCESS = 0,
DISPATCH_RESULT_CANNOT_FIND_WORKER = 1,
DISPATCH_RESULT_CANNOT_START_WORKER = 2,
DISPATCH_RESULT_CANNOT_DISPATCH_EVENT = 3,
DISPATCH_RESULT_COUNT
};
BackgroundFetchEventDispatcher(
BackgroundFetchContext* background_fetch_context,
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context,
DevToolsBackgroundServicesContextImpl& devtools_context);
BackgroundFetchEventDispatcher(const BackgroundFetchEventDispatcher&) =
delete;
BackgroundFetchEventDispatcher& operator=(
const BackgroundFetchEventDispatcher&) = delete;
~BackgroundFetchEventDispatcher();
void DispatchBackgroundFetchCompletionEvent(
const BackgroundFetchRegistrationId& registration_id,
blink::mojom::BackgroundFetchRegistrationDataPtr registration_data,
base::OnceClosure finished_closure);
void DispatchBackgroundFetchClickEvent(
const BackgroundFetchRegistrationId& registration_id,
blink::mojom::BackgroundFetchRegistrationDataPtr registration_data,
base::OnceClosure finished_closure);
void Shutdown();
private:
using ServiceWorkerLoadedCallback =
base::OnceCallback<void(scoped_refptr<ServiceWorkerVersion>,
int request_id)>;
void DispatchBackgroundFetchAbortEvent(
const BackgroundFetchRegistrationId& registration_id,
blink::mojom::BackgroundFetchRegistrationPtr registration,
base::OnceClosure finished_closure);
void DispatchBackgroundFetchFailEvent(
const BackgroundFetchRegistrationId& registration_id,
blink::mojom::BackgroundFetchRegistrationPtr registration,
base::OnceClosure finished_closure);
void DispatchBackgroundFetchSuccessEvent(
const BackgroundFetchRegistrationId& registration_id,
blink::mojom::BackgroundFetchRegistrationPtr registration,
base::OnceClosure finished_closure);
enum class DispatchPhase { FINDING, STARTING, DISPATCHING };
void LoadServiceWorkerRegistrationForDispatch(
const BackgroundFetchRegistrationId& registration_id,
ServiceWorkerMetrics::EventType event,
base::OnceClosure finished_closure,
ServiceWorkerLoadedCallback loaded_callback);
static void StartActiveWorkerForDispatch(
ServiceWorkerMetrics::EventType event,
base::OnceClosure finished_closure,
ServiceWorkerLoadedCallback loaded_callback,
blink::ServiceWorkerStatusCode service_worker_status,
scoped_refptr<ServiceWorkerRegistration> registration);
static void DispatchEvent(
ServiceWorkerMetrics::EventType event,
base::OnceClosure finished_closure,
ServiceWorkerLoadedCallback loaded_callback,
scoped_refptr<ServiceWorkerVersion> service_worker_version,
blink::ServiceWorkerStatusCode start_worker_status);
static void DidDispatchEvent(
ServiceWorkerMetrics::EventType event,
base::OnceClosure finished_closure,
DispatchPhase dispatch_phase,
blink::ServiceWorkerStatusCode service_worker_status);
static void DoDispatchBackgroundFetchAbortEvent(
blink::mojom::BackgroundFetchRegistrationPtr registration,
scoped_refptr<ServiceWorkerVersion> service_worker_version,
int request_id);
static void DoDispatchBackgroundFetchClickEvent(
blink::mojom::BackgroundFetchRegistrationPtr registration,
scoped_refptr<ServiceWorkerVersion> service_worker_version,
int request_id);
static void DoDispatchBackgroundFetchFailEvent(
blink::mojom::BackgroundFetchRegistrationPtr registration,
scoped_refptr<ServiceWorkerVersion> service_worker_version,
int request_id);
static void DoDispatchBackgroundFetchSuccessEvent(
blink::mojom::BackgroundFetchRegistrationPtr registration,
scoped_refptr<ServiceWorkerVersion> service_worker_version,
int request_id);
void LogBackgroundFetchCompletionForDevTools(
const BackgroundFetchRegistrationId& registration_id,
ServiceWorkerMetrics::EventType event_type,
blink::mojom::BackgroundFetchFailureReason failure_reason);
raw_ptr<BackgroundFetchContext> background_fetch_context_;
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
raw_ptr<DevToolsBackgroundServicesContextImpl> devtools_context_;
};
}
#endif