| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_EVENT_DISPATCHER_H_ | 5 #ifndef CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_EVENT_DISPATCHER_H_ |
| 6 #define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_EVENT_DISPATCHER_H_ | 6 #define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_EVENT_DISPATCHER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "content/browser/service_worker/service_worker_metrics.h" | 14 #include "content/browser/service_worker/service_worker_metrics.h" |
| 15 #include "content/common/content_export.h" | 15 #include "content/common/content_export.h" |
| 16 #include "content/common/service_worker/service_worker_event_dispatcher.mojom.h" | 16 #include "content/common/service_worker/service_worker_event_dispatcher.mojom.h" |
| 17 | 17 |
| 18 namespace content { | 18 namespace content { |
| 19 | 19 |
| 20 struct BackgroundFetchSettledFetch; |
| 20 class ServiceWorkerContextWrapper; | 21 class ServiceWorkerContextWrapper; |
| 21 class ServiceWorkerRegistration; | 22 class ServiceWorkerRegistration; |
| 22 class ServiceWorkerVersion; | 23 class ServiceWorkerVersion; |
| 23 | 24 |
| 24 // Responsible for dispatching the Background Fetch API events on a given | 25 // Responsible for dispatching the Background Fetch API events on a given |
| 25 // Service Worker. Must only be used on the IO thread. | 26 // Service Worker. Must only be used on the IO thread. |
| 26 class CONTENT_EXPORT BackgroundFetchEventDispatcher { | 27 class CONTENT_EXPORT BackgroundFetchEventDispatcher { |
| 27 public: | 28 public: |
| 28 // This enumeration is used for recording histograms. Treat as append-only. | 29 // This enumeration is used for recording histograms. Treat as append-only. |
| 29 enum DispatchResult { | 30 enum DispatchResult { |
| 30 DISPATCH_RESULT_SUCCESS = 0, | 31 DISPATCH_RESULT_SUCCESS = 0, |
| 31 DISPATCH_RESULT_CANNOT_FIND_WORKER = 1, | 32 DISPATCH_RESULT_CANNOT_FIND_WORKER = 1, |
| 32 DISPATCH_RESULT_CANNOT_START_WORKER = 2, | 33 DISPATCH_RESULT_CANNOT_START_WORKER = 2, |
| 33 DISPATCH_RESULT_CANNOT_DISPATCH_EVENT = 3, | 34 DISPATCH_RESULT_CANNOT_DISPATCH_EVENT = 3, |
| 34 | 35 |
| 35 DISPATCH_RESULT_COUNT | 36 DISPATCH_RESULT_COUNT |
| 36 }; | 37 }; |
| 37 | 38 |
| 38 explicit BackgroundFetchEventDispatcher( | 39 explicit BackgroundFetchEventDispatcher( |
| 39 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); | 40 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); |
| 40 ~BackgroundFetchEventDispatcher(); | 41 ~BackgroundFetchEventDispatcher(); |
| 41 | 42 |
| 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 | 43 // Dispatches the `backgroundfetchabort` event, which indicates that an active |
| 46 // background fetch was aborted by the user or another external event. | 44 // background fetch was aborted by the user or another external event. |
| 47 void DispatchBackgroundFetchAbortEvent(int64_t service_worker_registration_id, | 45 void DispatchBackgroundFetchAbortEvent(int64_t service_worker_registration_id, |
| 48 const GURL& origin, | 46 const GURL& origin, |
| 49 const std::string& tag, | 47 const std::string& tag, |
| 50 base::Closure finished_closure); | 48 base::Closure finished_closure); |
| 51 | 49 |
| 52 // Dispatches the `backgroundfetchclick` event, which indicates that the user | 50 // Dispatches the `backgroundfetchclick` event, which indicates that the user |
| 53 // interface displayed for an active background fetch was activated. | 51 // interface displayed for an active background fetch was activated. |
| 54 void DispatchBackgroundFetchClickEvent(int64_t service_worker_registration_id, | 52 void DispatchBackgroundFetchClickEvent(int64_t service_worker_registration_id, |
| 55 const GURL& origin, | 53 const GURL& origin, |
| 56 const std::string& tag, | 54 const std::string& tag, |
| 57 mojom::BackgroundFetchState state, | 55 mojom::BackgroundFetchState state, |
| 58 base::Closure finished_closure); | 56 base::Closure finished_closure); |
| 59 | 57 |
| 58 // Dispatches the `backgroundfetchfail` event, which indicates that a |
| 59 // background fetch has finished with one or more failed fetches. The request- |
| 60 // response pairs are included. |
| 61 void DispatchBackgroundFetchFailEvent( |
| 62 int64_t service_worker_registration_id, |
| 63 const GURL& origin, |
| 64 const std::string& tag, |
| 65 const std::vector<BackgroundFetchSettledFetch>& fetches, |
| 66 base::Closure finished_closure); |
| 67 |
| 68 // Dispatches the `backgroundfetched` event, which indicates that a background |
| 69 // fetch has successfully completed. The request-response pairs are included. |
| 70 void DispatchBackgroundFetchedEvent( |
| 71 int64_t service_worker_registration_id, |
| 72 const GURL& origin, |
| 73 const std::string& tag, |
| 74 const std::vector<BackgroundFetchSettledFetch>& fetches, |
| 75 base::Closure finished_closure); |
| 76 |
| 60 private: | 77 private: |
| 61 using ServiceWorkerLoadedCallback = | 78 using ServiceWorkerLoadedCallback = |
| 62 base::Callback<void(scoped_refptr<ServiceWorkerVersion>, int)>; | 79 base::Callback<void(scoped_refptr<ServiceWorkerVersion>, int)>; |
| 63 | 80 |
| 64 // Phase at which the dispatching process finished. Used for UMA. | 81 // Phase at which the dispatching process finished. Used for UMA. |
| 65 enum class DispatchPhase { FINDING, STARTING, DISPATCHING }; | 82 enum class DispatchPhase { FINDING, STARTING, DISPATCHING }; |
| 66 | 83 |
| 67 // Loads the Service Worker identified by |service_worker_registration_id| and | 84 // Loads the Service Worker identified by |service_worker_registration_id| and |
| 68 // ensures that there is an activated version. Will invoke |finished_closure|, | 85 // ensures that there is an activated version. Will invoke |finished_closure|, |
| 69 // log UMA and abort on error, or invoke |loaded_callback| on success. | 86 // log UMA and abort on error, or invoke |loaded_callback| on success. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 // Methods that actually invoke the event on an activated Service Worker. | 118 // Methods that actually invoke the event on an activated Service Worker. |
| 102 static void DoDispatchBackgroundFetchAbortEvent( | 119 static void DoDispatchBackgroundFetchAbortEvent( |
| 103 const std::string& tag, | 120 const std::string& tag, |
| 104 scoped_refptr<ServiceWorkerVersion> service_worker_version, | 121 scoped_refptr<ServiceWorkerVersion> service_worker_version, |
| 105 int request_id); | 122 int request_id); |
| 106 static void DoDispatchBackgroundFetchClickEvent( | 123 static void DoDispatchBackgroundFetchClickEvent( |
| 107 const std::string& tag, | 124 const std::string& tag, |
| 108 mojom::BackgroundFetchState state, | 125 mojom::BackgroundFetchState state, |
| 109 scoped_refptr<ServiceWorkerVersion> service_worker_version, | 126 scoped_refptr<ServiceWorkerVersion> service_worker_version, |
| 110 int request_id); | 127 int request_id); |
| 128 static void DoDispatchBackgroundFetchFailEvent( |
| 129 const std::string& tag, |
| 130 const std::vector<BackgroundFetchSettledFetch>& fetches, |
| 131 scoped_refptr<ServiceWorkerVersion> service_worker_version, |
| 132 int request_id); |
| 133 static void DoDispatchBackgroundFetchedEvent( |
| 134 const std::string& tag, |
| 135 const std::vector<BackgroundFetchSettledFetch>& fetches, |
| 136 scoped_refptr<ServiceWorkerVersion> service_worker_version, |
| 137 int request_id); |
| 111 | 138 |
| 112 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | 139 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |
| 113 | 140 |
| 114 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchEventDispatcher); | 141 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchEventDispatcher); |
| 115 }; | 142 }; |
| 116 | 143 |
| 117 } // namespace content | 144 } // namespace content |
| 118 | 145 |
| 119 #endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_EVENT_DISPATCHER_H_ | 146 #endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_EVENT_DISPATCHER_H_ |
| OLD | NEW |