| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_EVENT_DISPATCHER_H_ |
| 6 #define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_EVENT_DISPATCHER_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 #include <string> |
| 10 |
| 11 #include "base/callback_forward.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "content/browser/service_worker/service_worker_metrics.h" |
| 15 #include "content/common/content_export.h" |
| 16 #include "content/common/service_worker/service_worker_event_dispatcher.mojom.h" |
| 17 |
| 18 namespace content { |
| 19 |
| 20 class ServiceWorkerContextWrapper; |
| 21 class ServiceWorkerRegistration; |
| 22 class ServiceWorkerVersion; |
| 23 |
| 24 // Responsible for dispatching the Background Fetch API events on a given |
| 25 // Service Worker. Must only be used on the IO thread. |
| 26 class CONTENT_EXPORT BackgroundFetchEventDispatcher { |
| 27 public: |
| 28 // This enumeration is used for recording histograms. Treat as append-only. |
| 29 enum DispatchResult { |
| 30 DISPATCH_RESULT_SUCCESS = 0, |
| 31 DISPATCH_RESULT_CANNOT_FIND_WORKER = 1, |
| 32 DISPATCH_RESULT_CANNOT_START_WORKER = 2, |
| 33 DISPATCH_RESULT_CANNOT_DISPATCH_EVENT = 3, |
| 34 |
| 35 DISPATCH_RESULT_COUNT |
| 36 }; |
| 37 |
| 38 explicit BackgroundFetchEventDispatcher( |
| 39 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); |
| 40 ~BackgroundFetchEventDispatcher(); |
| 41 |
| 42 // TODO(peter): Support the `backgroundfetched` event. |
| 43 // TODO(peter): Support the `backgroundfetchfail` event. |
| 44 |
| 45 // Dispatches the `backgroundfetchabort` event, which indicates that an active |
| 46 // background fetch was aborted by the user or another external event. |
| 47 void DispatchBackgroundFetchAbortEvent(int64_t service_worker_registration_id, |
| 48 const GURL& origin, |
| 49 const std::string& tag, |
| 50 base::Closure finished_closure); |
| 51 |
| 52 // Dispatches the `backgroundfetchclick` event, which indicates that the user |
| 53 // interface displayed for an active background fetch was activated. |
| 54 void DispatchBackgroundFetchClickEvent(int64_t service_worker_registration_id, |
| 55 const GURL& origin, |
| 56 const std::string& tag, |
| 57 mojom::BackgroundFetchState state, |
| 58 base::Closure finished_closure); |
| 59 |
| 60 private: |
| 61 using ServiceWorkerLoadedCallback = |
| 62 base::Callback<void(scoped_refptr<ServiceWorkerVersion>, int)>; |
| 63 |
| 64 // Phase at which the dispatching process finished. Used for UMA. |
| 65 enum class DispatchPhase { FINDING, STARTING, DISPATCHING }; |
| 66 |
| 67 // Loads the Service Worker identified by |service_worker_registration_id| and |
| 68 // ensures that there is an activated version. Will invoke |finished_closure|, |
| 69 // log UMA and abort on error, or invoke |loaded_callback| on success. |
| 70 void LoadServiceWorkerRegistrationForDispatch( |
| 71 ServiceWorkerMetrics::EventType event, |
| 72 int64_t service_worker_registration_id, |
| 73 const GURL& origin, |
| 74 base::Closure finished_closure, |
| 75 ServiceWorkerLoadedCallback loaded_callback); |
| 76 |
| 77 // Verifies that the |registration| has successfully been loaded, then starts |
| 78 // the active Service Worker on the registration to dispatch |event|. Will |
| 79 // invoke |finished_closure|, log UMA and abort on error, or invoke the |
| 80 // |loaded_callback| on success. |
| 81 static void StartActiveWorkerForDispatch( |
| 82 ServiceWorkerMetrics::EventType event, |
| 83 base::Closure finished_closure, |
| 84 ServiceWorkerLoadedCallback loaded_callback, |
| 85 ServiceWorkerStatusCode service_worker_status, |
| 86 scoped_refptr<ServiceWorkerRegistration> registration); |
| 87 |
| 88 // Dispatches the actual event after the Service Worker has been started. |
| 89 static void DispatchEvent( |
| 90 ServiceWorkerMetrics::EventType event, |
| 91 base::Closure finished_closure, |
| 92 ServiceWorkerLoadedCallback loaded_callback, |
| 93 scoped_refptr<ServiceWorkerVersion> service_worker_version); |
| 94 |
| 95 // Called when an event of type |event| has finished dispatching. |
| 96 static void DidDispatchEvent(ServiceWorkerMetrics::EventType event, |
| 97 base::Closure finished_closure, |
| 98 DispatchPhase dispatch_phase, |
| 99 ServiceWorkerStatusCode service_worker_status); |
| 100 |
| 101 // Methods that actually invoke the event on an activated Service Worker. |
| 102 static void DoDispatchBackgroundFetchAbortEvent( |
| 103 const std::string& tag, |
| 104 scoped_refptr<ServiceWorkerVersion> service_worker_version, |
| 105 int request_id); |
| 106 static void DoDispatchBackgroundFetchClickEvent( |
| 107 const std::string& tag, |
| 108 mojom::BackgroundFetchState state, |
| 109 scoped_refptr<ServiceWorkerVersion> service_worker_version, |
| 110 int request_id); |
| 111 |
| 112 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |
| 113 |
| 114 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchEventDispatcher); |
| 115 }; |
| 116 |
| 117 } // namespace content |
| 118 |
| 119 #endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_EVENT_DISPATCHER_H_ |
| OLD | NEW |