Chromium Code Reviews| 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_MAX = DISPATCH_RESULT_CANNOT_DISPATCH_EVENT | |
|
Ilya Sherman
2017/03/16 03:58:50
Optional: I find that it's easier to work with DIS
Peter Beverloo
2017/03/16 16:05:58
We need MAX now that I've updated the .cc to use U
Ilya Sherman
2017/03/16 22:36:02
You should still use COUNT or MAX+1, even with Uma
Peter Beverloo
2017/03/17 13:43:28
Ack as discussed - changed to DISPATCH_RESULT_COUN
| |
| 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 std::string& tag, | |
| 49 base::Closure finished_closure); | |
| 50 | |
| 51 // Dispatches the `backgroundfetchclick` event, which indicates that the user | |
| 52 // interface displayed for an active background fetch was activated. | |
| 53 void DispatchBackgroundFetchClickEvent(int64_t service_worker_registration_id, | |
| 54 const std::string& tag, | |
| 55 mojom::BackgroundFetchState state, | |
| 56 base::Closure finished_closure); | |
| 57 | |
| 58 private: | |
| 59 using ServiceWorkerLoadedCallback = | |
| 60 base::Callback<void(scoped_refptr<ServiceWorkerVersion>, int)>; | |
| 61 | |
| 62 // Phase at which the dispatching process finished. Used for UMA. | |
| 63 enum class DispatchPhase { FINDING, STARTING, DISPATCHING }; | |
| 64 | |
| 65 // Loads the Service Worker identified by |service_worker_registration_id| and | |
| 66 // ensures that there is an activated version. Will invoke |finished_closure|, | |
| 67 // log UMA and abort on error, or invoke |loaded_callback| on success. | |
| 68 void LoadServiceWorkerRegistrationForDispatch( | |
| 69 ServiceWorkerMetrics::EventType event, | |
| 70 int64_t service_worker_registration_id, | |
| 71 base::Closure finished_closure, | |
|
harkness
2017/03/16 17:42:18
nit: error_closure? "finished" is harder to distin
Peter Beverloo
2017/03/17 13:43:28
We always invoke the |finished_closure|, so that's
| |
| 72 ServiceWorkerLoadedCallback loaded_callback); | |
| 73 | |
| 74 // Verifies that the |registration| has successfully been loaded, then starts | |
| 75 // the active Service Worker on the registration to dispatch |event|. Will | |
| 76 // invoke |finished_closure|, log UMA and abort on error, or invoke the | |
| 77 // |loaded_callback| on success. | |
| 78 static void StartActiveWorkerForDispatch( | |
| 79 ServiceWorkerMetrics::EventType event, | |
| 80 base::Closure finished_closure, | |
| 81 ServiceWorkerLoadedCallback loaded_callback, | |
| 82 ServiceWorkerStatusCode service_worker_status, | |
| 83 scoped_refptr<ServiceWorkerRegistration> registration); | |
| 84 | |
| 85 // Dispatches the actual event after the Service Worker has been started. | |
| 86 static void DispatchEvent( | |
| 87 ServiceWorkerMetrics::EventType event, | |
| 88 base::Closure finished_closure, | |
| 89 ServiceWorkerLoadedCallback loaded_callback, | |
| 90 scoped_refptr<ServiceWorkerVersion> service_worker_version); | |
| 91 | |
| 92 // Called when an event of type |event| has finished dispatching. | |
| 93 static void DidDispatchEvent(ServiceWorkerMetrics::EventType event, | |
| 94 base::Closure finished_closure, | |
| 95 DispatchPhase dispatch_phase, | |
| 96 ServiceWorkerStatusCode service_worker_status); | |
| 97 | |
| 98 // Methods that actually invoke the event on an activated Service Worker. | |
| 99 static void DoDispatchBackgroundFetchAbortEvent( | |
| 100 const std::string& tag, | |
| 101 scoped_refptr<ServiceWorkerVersion> service_worker_version, | |
| 102 int request_id); | |
| 103 static void DoDispatchBackgroundFetchClickEvent( | |
| 104 const std::string& tag, | |
| 105 mojom::BackgroundFetchState state, | |
| 106 scoped_refptr<ServiceWorkerVersion> service_worker_version, | |
| 107 int request_id); | |
| 108 | |
| 109 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | |
| 110 | |
| 111 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchEventDispatcher); | |
| 112 }; | |
| 113 | |
| 114 } // namespace content | |
| 115 | |
| 116 #endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_EVENT_DISPATCHER_H_ | |
| OLD | NEW |