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.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 const char* 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 ""; | |
| 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 = | |
| 38 base::StringPrintf("BackgroundFetch.EventDispatchResult.%s", | |
|
Ilya Sherman
2017/03/16 03:58:50
nit: I'm pretty sure that returning an std::string
Peter Beverloo
2017/03/16 16:05:58
Done.
| |
| 39 HistogramSuffixForEventType(event)); | |
| 40 | |
| 41 // Used because the |histogram_name| is not a constant. | |
| 42 base::LinearHistogram::FactoryGet( | |
|
Ilya Sherman
2017/03/16 03:58:50
Could you use a function from histogram_functions.
Peter Beverloo
2017/03/16 16:05:58
Aaaaah. With pleasure - done :)
| |
| 43 histogram_name, | |
| 44 BackgroundFetchEventDispatcher::DISPATCH_RESULT_SUCCESS /* minimum */, | |
| 45 BackgroundFetchEventDispatcher::DISPATCH_RESULT_MAX /* maximum */, | |
| 46 BackgroundFetchEventDispatcher::DISPATCH_RESULT_MAX + 1 /* buckets */, | |
| 47 base::Histogram::kUmaTargetedHistogramFlag) | |
| 48 ->Add(result); | |
| 49 } | |
| 50 | |
| 51 // Records the failure reason of a failed dispatch for |metric_name|. | |
| 52 void RecordFailureResult(ServiceWorkerMetrics::EventType event, | |
| 53 const char* metric_name, | |
| 54 ServiceWorkerStatusCode service_worker_status) { | |
| 55 std::string histogram_name = | |
| 56 base::StringPrintf("BackgroundFetch.EventDispatchFailure.%s.%s", | |
| 57 metric_name, HistogramSuffixForEventType(event)); | |
| 58 | |
| 59 // Used because the |histogram_name| is not a constant. | |
| 60 base::LinearHistogram::FactoryGet( | |
| 61 histogram_name, SERVICE_WORKER_OK /* minimum */, | |
| 62 SERVICE_WORKER_ERROR_MAX_VALUE /* maximum */, | |
| 63 SERVICE_WORKER_ERROR_MAX_VALUE + 1 /* buckets */, | |
| 64 base::Histogram::kUmaTargetedHistogramFlag) | |
| 65 ->Add(service_worker_status); | |
| 66 } | |
| 67 | |
| 68 } // namespace | |
| 69 | |
| 70 BackgroundFetchEventDispatcher::BackgroundFetchEventDispatcher( | |
| 71 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) | |
| 72 : service_worker_context_(service_worker_context) {} | |
| 73 | |
| 74 BackgroundFetchEventDispatcher::~BackgroundFetchEventDispatcher() = default; | |
| 75 | |
| 76 void BackgroundFetchEventDispatcher::DispatchBackgroundFetchAbortEvent( | |
| 77 int64_t service_worker_registration_id, | |
| 78 const std::string& tag, | |
| 79 base::Closure finished_closure) { | |
| 80 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 81 LoadServiceWorkerRegistrationForDispatch( | |
| 82 ServiceWorkerMetrics::EventType::BACKGROUND_FETCH_ABORT, | |
| 83 service_worker_registration_id, std::move(finished_closure), | |
| 84 base::Bind( | |
| 85 &BackgroundFetchEventDispatcher::DoDispatchBackgroundFetchAbortEvent, | |
| 86 tag)); | |
| 87 } | |
| 88 | |
| 89 void BackgroundFetchEventDispatcher::DoDispatchBackgroundFetchAbortEvent( | |
| 90 const std::string& tag, | |
| 91 scoped_refptr<ServiceWorkerVersion> service_worker_version, | |
| 92 int request_id) { | |
| 93 DCHECK(service_worker_version); | |
| 94 service_worker_version->event_dispatcher()->DispatchBackgroundFetchAbortEvent( | |
| 95 tag, service_worker_version->CreateSimpleEventCallback(request_id)); | |
| 96 } | |
| 97 | |
| 98 void BackgroundFetchEventDispatcher::DispatchBackgroundFetchClickEvent( | |
| 99 int64_t service_worker_registration_id, | |
| 100 const std::string& tag, | |
| 101 mojom::BackgroundFetchState state, | |
| 102 base::Closure finished_closure) { | |
| 103 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 104 LoadServiceWorkerRegistrationForDispatch( | |
| 105 ServiceWorkerMetrics::EventType::BACKGROUND_FETCH_CLICK, | |
| 106 service_worker_registration_id, std::move(finished_closure), | |
| 107 base::Bind( | |
| 108 &BackgroundFetchEventDispatcher::DoDispatchBackgroundFetchClickEvent, | |
| 109 tag, state)); | |
| 110 } | |
| 111 | |
| 112 void BackgroundFetchEventDispatcher::DoDispatchBackgroundFetchClickEvent( | |
| 113 const std::string& tag, | |
| 114 mojom::BackgroundFetchState state, | |
| 115 scoped_refptr<ServiceWorkerVersion> service_worker_version, | |
| 116 int request_id) { | |
| 117 DCHECK(service_worker_version); | |
| 118 service_worker_version->event_dispatcher()->DispatchBackgroundFetchClickEvent( | |
| 119 tag, state, | |
| 120 service_worker_version->CreateSimpleEventCallback(request_id)); | |
| 121 } | |
| 122 | |
| 123 void BackgroundFetchEventDispatcher::LoadServiceWorkerRegistrationForDispatch( | |
| 124 ServiceWorkerMetrics::EventType event, | |
| 125 int64_t service_worker_registration_id, | |
| 126 base::Closure finished_closure, | |
| 127 ServiceWorkerLoadedCallback loaded_callback) { | |
| 128 service_worker_context_->FindReadyRegistrationForIdOnly( | |
|
horo
2017/03/16 07:08:22
Can you use FindReadyRegistrationForId instead?
Fi
Peter Beverloo
2017/03/16 16:05:58
We'll use the Service Worker database's user data
Peter Beverloo
2017/03/16 16:05:58
Done.
| |
| 129 service_worker_registration_id, | |
| 130 base::Bind(&BackgroundFetchEventDispatcher::StartActiveWorkerForDispatch, | |
| 131 event, std::move(finished_closure), | |
| 132 std::move(loaded_callback))); | |
| 133 } | |
| 134 | |
| 135 void BackgroundFetchEventDispatcher::StartActiveWorkerForDispatch( | |
| 136 ServiceWorkerMetrics::EventType event, | |
| 137 base::Closure finished_closure, | |
| 138 ServiceWorkerLoadedCallback loaded_callback, | |
| 139 ServiceWorkerStatusCode service_worker_status, | |
| 140 scoped_refptr<ServiceWorkerRegistration> registration) { | |
| 141 if (service_worker_status != SERVICE_WORKER_OK) { | |
| 142 DidDispatchEvent(event, std::move(finished_closure), DispatchPhase::FINDING, | |
| 143 service_worker_status); | |
| 144 return; | |
| 145 } | |
| 146 | |
| 147 ServiceWorkerVersion* service_worker_version = registration->active_version(); | |
| 148 DCHECK(service_worker_version); | |
| 149 | |
| 150 service_worker_version->RunAfterStartWorker( | |
| 151 event, | |
| 152 base::Bind(&BackgroundFetchEventDispatcher::DispatchEvent, event, | |
| 153 finished_closure, loaded_callback, | |
| 154 make_scoped_refptr(service_worker_version)), | |
| 155 base::Bind(&BackgroundFetchEventDispatcher::DidDispatchEvent, event, | |
| 156 finished_closure, DispatchPhase::STARTING)); | |
| 157 } | |
| 158 | |
| 159 void BackgroundFetchEventDispatcher::DispatchEvent( | |
| 160 ServiceWorkerMetrics::EventType event, | |
| 161 base::Closure finished_closure, | |
| 162 ServiceWorkerLoadedCallback loaded_callback, | |
| 163 scoped_refptr<ServiceWorkerVersion> service_worker_version) { | |
| 164 int request_id = service_worker_version->StartRequest( | |
| 165 event, | |
| 166 base::Bind(&BackgroundFetchEventDispatcher::DidDispatchEvent, event, | |
| 167 std::move(finished_closure), DispatchPhase::DISPATCHING)); | |
| 168 | |
| 169 loaded_callback.Run(std::move(service_worker_version), request_id); | |
| 170 } | |
| 171 | |
| 172 void BackgroundFetchEventDispatcher::DidDispatchEvent( | |
| 173 ServiceWorkerMetrics::EventType event, | |
| 174 base::Closure finished_closure, | |
| 175 DispatchPhase dispatch_phase, | |
| 176 ServiceWorkerStatusCode service_worker_status) { | |
| 177 // Record the histograms tracking event dispatching success. | |
| 178 switch (dispatch_phase) { | |
| 179 case DispatchPhase::FINDING: | |
| 180 RecordDispatchResult(event, DISPATCH_RESULT_CANNOT_FIND_WORKER); | |
| 181 RecordFailureResult(event, "FindWorker", service_worker_status); | |
| 182 break; | |
| 183 case DispatchPhase::STARTING: | |
| 184 RecordDispatchResult(event, DISPATCH_RESULT_CANNOT_START_WORKER); | |
| 185 RecordFailureResult(event, "StartWorker", service_worker_status); | |
| 186 break; | |
| 187 case DispatchPhase::DISPATCHING: | |
| 188 if (service_worker_status != SERVICE_WORKER_OK) { | |
| 189 RecordDispatchResult(event, DISPATCH_RESULT_CANNOT_DISPATCH_EVENT); | |
| 190 RecordFailureResult(event, "Dispatch", service_worker_status); | |
| 191 } else { | |
| 192 RecordDispatchResult(event, DISPATCH_RESULT_SUCCESS); | |
| 193 } | |
| 194 break; | |
| 195 } | |
| 196 | |
| 197 finished_closure.Run(); | |
| 198 } | |
| 199 | |
| 200 } // namespace content | |
| OLD | NEW |