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 #include "content/browser/background_fetch/background_fetch_event_dispatcher.h" | 5 #include "content/browser/background_fetch/background_fetch_event_dispatcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/metrics/histogram_functions.h" | 9 #include "base/metrics/histogram_functions.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 11 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
12 #include "content/browser/service_worker/service_worker_registration.h" | 12 #include "content/browser/service_worker/service_worker_registration.h" |
13 #include "content/browser/service_worker/service_worker_version.h" | 13 #include "content/browser/service_worker/service_worker_version.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 | 15 |
16 namespace content { | 16 namespace content { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // Returns the histogram suffix for the given |event| type. | 20 // Returns the histogram suffix for the given |event| type. |
21 std::string HistogramSuffixForEventType(ServiceWorkerMetrics::EventType event) { | 21 std::string HistogramSuffixForEventType(ServiceWorkerMetrics::EventType event) { |
22 switch (event) { | 22 switch (event) { |
23 case ServiceWorkerMetrics::EventType::BACKGROUND_FETCH_ABORT: | 23 case ServiceWorkerMetrics::EventType::BACKGROUND_FETCH_ABORT: |
24 return "AbortEvent"; | 24 return "AbortEvent"; |
25 case ServiceWorkerMetrics::EventType::BACKGROUND_FETCH_CLICK: | 25 case ServiceWorkerMetrics::EventType::BACKGROUND_FETCH_CLICK: |
26 return "ClickEvent"; | 26 return "ClickEvent"; |
| 27 case ServiceWorkerMetrics::EventType::BACKGROUND_FETCH_FAIL: |
| 28 return "FailEvent"; |
| 29 case ServiceWorkerMetrics::EventType::BACKGROUND_FETCHED: |
| 30 return "FetchedEvent"; |
27 default: | 31 default: |
28 NOTREACHED(); | 32 NOTREACHED(); |
29 return std::string(); | 33 return std::string(); |
30 } | 34 } |
31 } | 35 } |
32 | 36 |
33 // Records the result of a dispatched Background Fetch event. | 37 // Records the result of a dispatched Background Fetch event. |
34 void RecordDispatchResult( | 38 void RecordDispatchResult( |
35 ServiceWorkerMetrics::EventType event, | 39 ServiceWorkerMetrics::EventType event, |
36 BackgroundFetchEventDispatcher::DispatchResult result) { | 40 BackgroundFetchEventDispatcher::DispatchResult result) { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 const std::string& tag, | 110 const std::string& tag, |
107 mojom::BackgroundFetchState state, | 111 mojom::BackgroundFetchState state, |
108 scoped_refptr<ServiceWorkerVersion> service_worker_version, | 112 scoped_refptr<ServiceWorkerVersion> service_worker_version, |
109 int request_id) { | 113 int request_id) { |
110 DCHECK(service_worker_version); | 114 DCHECK(service_worker_version); |
111 service_worker_version->event_dispatcher()->DispatchBackgroundFetchClickEvent( | 115 service_worker_version->event_dispatcher()->DispatchBackgroundFetchClickEvent( |
112 tag, state, | 116 tag, state, |
113 service_worker_version->CreateSimpleEventCallback(request_id)); | 117 service_worker_version->CreateSimpleEventCallback(request_id)); |
114 } | 118 } |
115 | 119 |
| 120 void BackgroundFetchEventDispatcher::DispatchBackgroundFetchFailEvent( |
| 121 int64_t service_worker_registration_id, |
| 122 const GURL& origin, |
| 123 const std::string& tag, |
| 124 const std::vector<BackgroundFetchSettledFetch>& fetches, |
| 125 base::Closure finished_closure) { |
| 126 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 127 LoadServiceWorkerRegistrationForDispatch( |
| 128 ServiceWorkerMetrics::EventType::BACKGROUND_FETCH_FAIL, |
| 129 service_worker_registration_id, origin, std::move(finished_closure), |
| 130 base::Bind( |
| 131 &BackgroundFetchEventDispatcher::DoDispatchBackgroundFetchFailEvent, |
| 132 tag, fetches)); |
| 133 } |
| 134 |
| 135 void BackgroundFetchEventDispatcher::DoDispatchBackgroundFetchFailEvent( |
| 136 const std::string& tag, |
| 137 const std::vector<BackgroundFetchSettledFetch>& fetches, |
| 138 scoped_refptr<ServiceWorkerVersion> service_worker_version, |
| 139 int request_id) { |
| 140 DCHECK(service_worker_version); |
| 141 service_worker_version->event_dispatcher()->DispatchBackgroundFetchFailEvent( |
| 142 tag, fetches, |
| 143 service_worker_version->CreateSimpleEventCallback(request_id)); |
| 144 } |
| 145 |
| 146 void BackgroundFetchEventDispatcher::DispatchBackgroundFetchedEvent( |
| 147 int64_t service_worker_registration_id, |
| 148 const GURL& origin, |
| 149 const std::string& tag, |
| 150 const std::vector<BackgroundFetchSettledFetch>& fetches, |
| 151 base::Closure finished_closure) { |
| 152 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 153 LoadServiceWorkerRegistrationForDispatch( |
| 154 ServiceWorkerMetrics::EventType::BACKGROUND_FETCHED, |
| 155 service_worker_registration_id, origin, std::move(finished_closure), |
| 156 base::Bind( |
| 157 &BackgroundFetchEventDispatcher::DoDispatchBackgroundFetchedEvent, |
| 158 tag, fetches)); |
| 159 } |
| 160 |
| 161 void BackgroundFetchEventDispatcher::DoDispatchBackgroundFetchedEvent( |
| 162 const std::string& tag, |
| 163 const std::vector<BackgroundFetchSettledFetch>& fetches, |
| 164 scoped_refptr<ServiceWorkerVersion> service_worker_version, |
| 165 int request_id) { |
| 166 DCHECK(service_worker_version); |
| 167 service_worker_version->event_dispatcher()->DispatchBackgroundFetchedEvent( |
| 168 tag, fetches, |
| 169 service_worker_version->CreateSimpleEventCallback(request_id)); |
| 170 } |
| 171 |
116 void BackgroundFetchEventDispatcher::LoadServiceWorkerRegistrationForDispatch( | 172 void BackgroundFetchEventDispatcher::LoadServiceWorkerRegistrationForDispatch( |
117 ServiceWorkerMetrics::EventType event, | 173 ServiceWorkerMetrics::EventType event, |
118 int64_t service_worker_registration_id, | 174 int64_t service_worker_registration_id, |
119 const GURL& origin, | 175 const GURL& origin, |
120 base::Closure finished_closure, | 176 base::Closure finished_closure, |
121 ServiceWorkerLoadedCallback loaded_callback) { | 177 ServiceWorkerLoadedCallback loaded_callback) { |
122 service_worker_context_->FindReadyRegistrationForId( | 178 service_worker_context_->FindReadyRegistrationForId( |
123 service_worker_registration_id, origin, | 179 service_worker_registration_id, origin, |
124 base::Bind(&BackgroundFetchEventDispatcher::StartActiveWorkerForDispatch, | 180 base::Bind(&BackgroundFetchEventDispatcher::StartActiveWorkerForDispatch, |
125 event, std::move(finished_closure), | 181 event, std::move(finished_closure), |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 } else { | 241 } else { |
186 RecordDispatchResult(event, DISPATCH_RESULT_SUCCESS); | 242 RecordDispatchResult(event, DISPATCH_RESULT_SUCCESS); |
187 } | 243 } |
188 break; | 244 break; |
189 } | 245 } |
190 | 246 |
191 finished_closure.Run(); | 247 finished_closure.Run(); |
192 } | 248 } |
193 | 249 |
194 } // namespace content | 250 } // namespace content |
OLD | NEW |