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 #include "content/browser/background_fetch/background_fetch_event_dispatcher.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/metrics/histogram_functions.h" | |
| 10 #include "base/strings/stringprintf.h" | |
| 11 #include "content/browser/service_worker/service_worker_context_wrapper.h" | |
| 12 #include "content/browser/service_worker/service_worker_registration.h" | |
| 13 #include "content/browser/service_worker/service_worker_version.h" | |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // Returns the histogram suffix for the given |event| type. | |
| 21 std::string HistogramSuffixForEventType(ServiceWorkerMetrics::EventType event) { | |
| 22 switch (event) { | |
| 23 case ServiceWorkerMetrics::EventType::BACKGROUND_FETCH_ABORT: | |
| 24 return "AbortEvent"; | |
| 25 case ServiceWorkerMetrics::EventType::BACKGROUND_FETCH_CLICK: | |
| 26 return "ClickEvent"; | |
| 27 default: | |
| 28 NOTREACHED(); | |
| 29 return std::string(); | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 // Records the result of a dispatched Background Fetch event. | |
| 34 void RecordDispatchResult( | |
| 35 ServiceWorkerMetrics::EventType event, | |
| 36 BackgroundFetchEventDispatcher::DispatchResult result) { | |
| 37 std::string histogram_name = "BackgroundFetch.EventDispatchResult." + | |
| 38 HistogramSuffixForEventType(event); | |
| 39 | |
| 40 // Used because the |histogram_name| is not a constant. | |
| 41 base::UmaHistogramEnumeration( | |
| 42 histogram_name, result, | |
| 43 BackgroundFetchEventDispatcher::DISPATCH_RESULT_MAX); | |
| 44 } | |
| 45 | |
| 46 // Records the failure reason of a failed dispatch for |metric_name|. | |
| 47 void RecordFailureResult(ServiceWorkerMetrics::EventType event, | |
| 48 const char* metric_name, | |
| 49 ServiceWorkerStatusCode service_worker_status) { | |
| 50 std::string histogram_name = base::StringPrintf( | |
| 51 "BackgroundFetch.EventDispatchFailure.%s.%s", metric_name, | |
| 52 HistogramSuffixForEventType(event).c_str()); | |
| 53 | |
| 54 // Used because the |histogram_name| is not a constant. | |
| 55 base::UmaHistogramEnumeration(histogram_name, service_worker_status, | |
| 56 SERVICE_WORKER_ERROR_MAX_VALUE); | |
|
Ilya Sherman
2017/03/16 22:36:02
Both here and above, you'll want to add "+1" to th
Peter Beverloo
2017/03/17 13:43:28
Done.
| |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 BackgroundFetchEventDispatcher::BackgroundFetchEventDispatcher( | |
| 62 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) | |
| 63 : service_worker_context_(service_worker_context) {} | |
| 64 | |
| 65 BackgroundFetchEventDispatcher::~BackgroundFetchEventDispatcher() = default; | |
| 66 | |
| 67 void BackgroundFetchEventDispatcher::DispatchBackgroundFetchAbortEvent( | |
| 68 int64_t service_worker_registration_id, | |
| 69 const GURL& origin, | |
| 70 const std::string& tag, | |
| 71 base::Closure finished_closure) { | |
| 72 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 73 LoadServiceWorkerRegistrationForDispatch( | |
| 74 ServiceWorkerMetrics::EventType::BACKGROUND_FETCH_ABORT, | |
| 75 service_worker_registration_id, origin, std::move(finished_closure), | |
| 76 base::Bind( | |
| 77 &BackgroundFetchEventDispatcher::DoDispatchBackgroundFetchAbortEvent, | |
| 78 tag)); | |
| 79 } | |
| 80 | |
| 81 void BackgroundFetchEventDispatcher::DoDispatchBackgroundFetchAbortEvent( | |
| 82 const std::string& tag, | |
| 83 scoped_refptr<ServiceWorkerVersion> service_worker_version, | |
| 84 int request_id) { | |
| 85 DCHECK(service_worker_version); | |
| 86 service_worker_version->event_dispatcher()->DispatchBackgroundFetchAbortEvent( | |
| 87 tag, service_worker_version->CreateSimpleEventCallback(request_id)); | |
| 88 } | |
| 89 | |
| 90 void BackgroundFetchEventDispatcher::DispatchBackgroundFetchClickEvent( | |
| 91 int64_t service_worker_registration_id, | |
| 92 const GURL& origin, | |
| 93 const std::string& tag, | |
| 94 mojom::BackgroundFetchState state, | |
| 95 base::Closure finished_closure) { | |
| 96 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 97 LoadServiceWorkerRegistrationForDispatch( | |
| 98 ServiceWorkerMetrics::EventType::BACKGROUND_FETCH_CLICK, | |
| 99 service_worker_registration_id, origin, std::move(finished_closure), | |
| 100 base::Bind( | |
| 101 &BackgroundFetchEventDispatcher::DoDispatchBackgroundFetchClickEvent, | |
| 102 tag, state)); | |
| 103 } | |
| 104 | |
| 105 void BackgroundFetchEventDispatcher::DoDispatchBackgroundFetchClickEvent( | |
| 106 const std::string& tag, | |
| 107 mojom::BackgroundFetchState state, | |
| 108 scoped_refptr<ServiceWorkerVersion> service_worker_version, | |
| 109 int request_id) { | |
| 110 DCHECK(service_worker_version); | |
| 111 service_worker_version->event_dispatcher()->DispatchBackgroundFetchClickEvent( | |
| 112 tag, state, | |
| 113 service_worker_version->CreateSimpleEventCallback(request_id)); | |
| 114 } | |
| 115 | |
| 116 void BackgroundFetchEventDispatcher::LoadServiceWorkerRegistrationForDispatch( | |
| 117 ServiceWorkerMetrics::EventType event, | |
| 118 int64_t service_worker_registration_id, | |
| 119 const GURL& origin, | |
| 120 base::Closure finished_closure, | |
| 121 ServiceWorkerLoadedCallback loaded_callback) { | |
| 122 service_worker_context_->FindReadyRegistrationForId( | |
| 123 service_worker_registration_id, origin, | |
| 124 base::Bind(&BackgroundFetchEventDispatcher::StartActiveWorkerForDispatch, | |
| 125 event, std::move(finished_closure), | |
| 126 std::move(loaded_callback))); | |
| 127 } | |
| 128 | |
| 129 void BackgroundFetchEventDispatcher::StartActiveWorkerForDispatch( | |
| 130 ServiceWorkerMetrics::EventType event, | |
| 131 base::Closure finished_closure, | |
| 132 ServiceWorkerLoadedCallback loaded_callback, | |
| 133 ServiceWorkerStatusCode service_worker_status, | |
| 134 scoped_refptr<ServiceWorkerRegistration> registration) { | |
| 135 if (service_worker_status != SERVICE_WORKER_OK) { | |
| 136 DidDispatchEvent(event, std::move(finished_closure), DispatchPhase::FINDING, | |
| 137 service_worker_status); | |
| 138 return; | |
| 139 } | |
| 140 | |
| 141 ServiceWorkerVersion* service_worker_version = registration->active_version(); | |
| 142 DCHECK(service_worker_version); | |
| 143 | |
| 144 service_worker_version->RunAfterStartWorker( | |
| 145 event, | |
| 146 base::Bind(&BackgroundFetchEventDispatcher::DispatchEvent, event, | |
| 147 finished_closure, loaded_callback, | |
| 148 make_scoped_refptr(service_worker_version)), | |
| 149 base::Bind(&BackgroundFetchEventDispatcher::DidDispatchEvent, event, | |
| 150 finished_closure, DispatchPhase::STARTING)); | |
| 151 } | |
| 152 | |
| 153 void BackgroundFetchEventDispatcher::DispatchEvent( | |
| 154 ServiceWorkerMetrics::EventType event, | |
| 155 base::Closure finished_closure, | |
| 156 ServiceWorkerLoadedCallback loaded_callback, | |
| 157 scoped_refptr<ServiceWorkerVersion> service_worker_version) { | |
| 158 int request_id = service_worker_version->StartRequest( | |
| 159 event, | |
| 160 base::Bind(&BackgroundFetchEventDispatcher::DidDispatchEvent, event, | |
| 161 std::move(finished_closure), DispatchPhase::DISPATCHING)); | |
| 162 | |
| 163 loaded_callback.Run(std::move(service_worker_version), request_id); | |
| 164 } | |
| 165 | |
| 166 void BackgroundFetchEventDispatcher::DidDispatchEvent( | |
| 167 ServiceWorkerMetrics::EventType event, | |
| 168 base::Closure finished_closure, | |
| 169 DispatchPhase dispatch_phase, | |
| 170 ServiceWorkerStatusCode service_worker_status) { | |
| 171 // Record the histograms tracking event dispatching success. | |
| 172 switch (dispatch_phase) { | |
| 173 case DispatchPhase::FINDING: | |
| 174 RecordDispatchResult(event, DISPATCH_RESULT_CANNOT_FIND_WORKER); | |
| 175 RecordFailureResult(event, "FindWorker", service_worker_status); | |
| 176 break; | |
| 177 case DispatchPhase::STARTING: | |
| 178 RecordDispatchResult(event, DISPATCH_RESULT_CANNOT_START_WORKER); | |
| 179 RecordFailureResult(event, "StartWorker", service_worker_status); | |
| 180 break; | |
| 181 case DispatchPhase::DISPATCHING: | |
| 182 if (service_worker_status != SERVICE_WORKER_OK) { | |
| 183 RecordDispatchResult(event, DISPATCH_RESULT_CANNOT_DISPATCH_EVENT); | |
| 184 RecordFailureResult(event, "Dispatch", service_worker_status); | |
| 185 } else { | |
| 186 RecordDispatchResult(event, DISPATCH_RESULT_SUCCESS); | |
| 187 } | |
| 188 break; | |
| 189 } | |
| 190 | |
| 191 finished_closure.Run(); | |
| 192 } | |
| 193 | |
| 194 } // namespace content | |
| OLD | NEW |