1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
android_webview / browser / metrics / visibility_metrics_logger.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "android_webview/browser/metrics/visibility_metrics_logger.h"
#include "base/containers/contains.h"
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_base.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/typed_macros.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/url_constants.h"
using content::BrowserThread;
namespace android_webview {
namespace {
const char* SchemeEnumToString(VisibilityMetricsLogger::Scheme scheme) {
switch (scheme) {
case VisibilityMetricsLogger::Scheme::kEmpty:
return "empty";
case VisibilityMetricsLogger::Scheme::kUnknown:
return "unknown";
case VisibilityMetricsLogger::Scheme::kHttp:
return url::kHttpScheme;
case VisibilityMetricsLogger::Scheme::kHttps:
return url::kHttpsScheme;
case VisibilityMetricsLogger::Scheme::kFile:
return url::kFileScheme;
case VisibilityMetricsLogger::Scheme::kFtp:
return url::kFtpScheme;
case VisibilityMetricsLogger::Scheme::kData:
return url::kDataScheme;
case VisibilityMetricsLogger::Scheme::kJavaScript:
return url::kJavaScriptScheme;
case VisibilityMetricsLogger::Scheme::kAbout:
return url::kAboutScheme;
case VisibilityMetricsLogger::Scheme::kChrome:
return content::kChromeUIScheme;
case VisibilityMetricsLogger::Scheme::kBlob:
return url::kBlobScheme;
case VisibilityMetricsLogger::Scheme::kContent:
return url::kContentScheme;
case VisibilityMetricsLogger::Scheme::kIntent:
return "intent";
default:
NOTREACHED();
}
}
// Have bypassed the usual macros here because they do not support a
// means to increment counters by more than 1 per call.
base::HistogramBase* GetOrCreateHistogramForDurationTracking(
const std::string& name,
int max_value) {
return base::Histogram::FactoryGet(
name, 1, max_value + 1, max_value + 2,
base::HistogramBase::kUmaTargetedHistogramFlag);
}
// base::Histogram::FactoryGet would internally convert to std::string anyway,
// this overload is for convenience.
base::HistogramBase* GetOrCreateHistogramForDurationTracking(const char* name,
int max_value) {
return GetOrCreateHistogramForDurationTracking(std::string(name), max_value);
}
base::HistogramBase* GetGlobalVisibilityHistogram() {
static base::HistogramBase* histogram(GetOrCreateHistogramForDurationTracking(
"Android.WebView.Visibility.Global",
static_cast<int>(VisibilityMetricsLogger::Visibility::kMaxValue)));
return histogram;
}
base::HistogramBase* GetPerWebViewVisibilityHistogram() {
static base::HistogramBase* histogram(GetOrCreateHistogramForDurationTracking(
"Android.WebView.Visibility.PerWebView",
static_cast<int>(VisibilityMetricsLogger::Visibility::kMaxValue)));
return histogram;
}
void LogGlobalVisibleScheme(VisibilityMetricsLogger::Scheme scheme,
int32_t seconds) {
static base::HistogramBase* histogram(GetOrCreateHistogramForDurationTracking(
"Android.WebView.VisibleScheme.Global",
static_cast<int>(VisibilityMetricsLogger::Scheme::kMaxValue)));
histogram->AddCount(static_cast<int32_t>(scheme), seconds);
}
void LogPerWebViewVisibleScheme(VisibilityMetricsLogger::Scheme scheme,
int32_t seconds) {
static base::HistogramBase* histogram(GetOrCreateHistogramForDurationTracking(
"Android.WebView.VisibleScheme.PerWebView",
static_cast<int>(VisibilityMetricsLogger::Scheme::kMaxValue)));
histogram->AddCount(static_cast<int32_t>(scheme), seconds);
}
void LogGlobalVisibleScreenCoverage(int percentage, int32_t seconds) {
static base::HistogramBase* histogram(GetOrCreateHistogramForDurationTracking(
"Android.WebView.VisibleScreenCoverage.Global", 100));
histogram->AddCount(percentage, seconds);
}
void LogPerWebViewVisibleScreenCoverage(int percentage, int32_t seconds) {
static base::HistogramBase* histogram(GetOrCreateHistogramForDurationTracking(
"Android.WebView.VisibleScreenCoverage.PerWebView", 100));
histogram->AddCount(percentage, seconds);
}
void LogPerSchemeVisibleScreenCoverage(VisibilityMetricsLogger::Scheme scheme,
int percentage,
int32_t seconds) {
GetOrCreateHistogramForDurationTracking(
std::string("Android.WebView.VisibleScreenCoverage.PerWebView.") +
SchemeEnumToString(scheme),
100)
->AddCount(percentage, seconds);
}
} // anonymous namespace
// static
VisibilityMetricsLogger::Scheme VisibilityMetricsLogger::SchemeStringToEnum(
const std::string& scheme) {
if (scheme.empty())
return Scheme::kEmpty;
if (scheme == url::kHttpScheme)
return Scheme::kHttp;
if (scheme == url::kHttpsScheme)
return Scheme::kHttps;
if (scheme == url::kFileScheme)
return Scheme::kFile;
if (scheme == url::kFtpScheme)
return Scheme::kFtp;
if (scheme == url::kDataScheme)
return Scheme::kData;
if (scheme == url::kJavaScriptScheme)
return Scheme::kJavaScript;
if (scheme == url::kAboutScheme)
return Scheme::kAbout;
if (scheme == content::kChromeUIScheme)
return Scheme::kChrome;
if (scheme == url::kBlobScheme)
return Scheme::kBlob;
if (scheme == url::kContentScheme)
return Scheme::kContent;
if (scheme == "intent")
return Scheme::kIntent;
return Scheme::kUnknown;
}
VisibilityMetricsLogger::VisibilityMetricsLogger() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
last_update_time_ = base::TimeTicks::Now();
}
VisibilityMetricsLogger::~VisibilityMetricsLogger() = default;
void VisibilityMetricsLogger::AddClient(Client* client) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(!base::Contains(client_visibility_, client));
UpdateDurations();
client_visibility_[client] = VisibilityInfo();
ProcessClientUpdate(client, client->GetVisibilityInfo(),
ClientAction::kAdded);
}
void VisibilityMetricsLogger::RemoveClient(Client* client) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(base::Contains(client_visibility_, client));
UpdateDurations();
ProcessClientUpdate(client, VisibilityInfo(), ClientAction::kRemoved);
client_visibility_.erase(client);
}
void VisibilityMetricsLogger::ClientVisibilityChanged(Client* client) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(base::Contains(client_visibility_, client));
UpdateDurations();
ProcessClientUpdate(client, client->GetVisibilityInfo(),
ClientAction::kVisibilityChanged);
}
void VisibilityMetricsLogger::UpdateScreenCoverage(
int global_percentage,
const std::vector<Scheme>& schemes,
const std::vector<int>& scheme_percentages) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(schemes.size() == scheme_percentages.size());
UpdateDurations();
DCHECK(global_percentage >= 0);
DCHECK(global_percentage <= 100);
global_coverage_percentage_ = global_percentage;
schemes_to_coverage_percentages_.clear();
for (size_t i = 0; i < schemes.size(); i++) {
DCHECK(scheme_percentages[i] >= 0);
DCHECK(scheme_percentages[i] <= 100);
schemes_to_coverage_percentages_.emplace(schemes[i], scheme_percentages[i]);
}
}
void VisibilityMetricsLogger::UpdateDurations() {
base::TimeTicks update_time = base::TimeTicks::Now();
base::TimeDelta delta = update_time - last_update_time_;
if (all_clients_visible_count_ > 0) {
all_clients_tracker_.any_webview_tracked_duration_ += delta;
} else {
all_clients_tracker_.no_webview_tracked_duration_ += delta;
}
all_clients_tracker_.per_webview_duration_ +=
delta * all_clients_visible_count_;
all_clients_tracker_.per_webview_untracked_duration_ +=
delta * (client_visibility_.size() - all_clients_visible_count_);
for (size_t i = 0; i < std::size(per_scheme_visible_counts_); i++) {
if (!per_scheme_visible_counts_[i])
continue;
per_scheme_trackers_[i].any_webview_tracked_duration_ += delta;
per_scheme_trackers_[i].per_webview_duration_ +=
delta * per_scheme_visible_counts_[i];
}
if (all_clients_visible_count_ > 0) {
global_coverage_percentage_durations_[global_coverage_percentage_] += delta;
for (auto& scheme_and_percentage : schemes_to_coverage_percentages_) {
schemes_to_percentages_to_durations_[scheme_and_percentage.first]
[scheme_and_percentage.second] +=
delta;
}
}
last_update_time_ = update_time;
}
bool VisibilityMetricsLogger::VisibilityInfo::IsVisible() const {
return view_attached && view_visible && window_visible;
}
void VisibilityMetricsLogger::ProcessClientUpdate(Client* client,
const VisibilityInfo& info,
ClientAction action) {
VisibilityInfo curr_info = client_visibility_[client];
bool was_visible = curr_info.IsVisible();
bool is_visible = info.IsVisible();
Scheme old_scheme = curr_info.scheme;
Scheme new_scheme = info.scheme;
client_visibility_[client] = info;
DCHECK(!was_visible || all_clients_visible_count_ > 0);
bool any_client_was_visible = all_clients_visible_count_ > 0;
if (action == ClientAction::kAdded) {
// Only emit the event if the WebView is visible so that the track gets the
// appropriate name.
// TODO(b/280334022): set the track name explicitly after the Perfetto SDK
// migration is finished (crbug/1006541).
if (is_visible) {
TRACE_EVENT_BEGIN("android_webview.timeline", "WebViewVisible",
perfetto::Track::FromPointer(client));
}
}
// If visibility changes or the client is removed, close the event
// corresponding to the previous visibility state.
if (action == ClientAction::kRemoved || was_visible != is_visible) {
TRACE_EVENT_END("android_webview.timeline",
perfetto::Track::FromPointer(client));
}
if (!was_visible && is_visible) {
if (action != ClientAction::kRemoved) {
TRACE_EVENT_BEGIN("android_webview.timeline", "WebViewVisible",
perfetto::Track::FromPointer(client));
}
++all_clients_visible_count_;
} else if (was_visible && !is_visible) {
if (action != ClientAction::kRemoved) {
TRACE_EVENT_BEGIN("android_webview.timeline", "WebViewInvisible",
perfetto::Track::FromPointer(client));
}
--all_clients_visible_count_;
}
if (was_visible)
per_scheme_visible_counts_[static_cast<size_t>(old_scheme)]--;
if (is_visible)
per_scheme_visible_counts_[static_cast<size_t>(new_scheme)]++;
bool any_client_is_visible = all_clients_visible_count_ > 0;
if (on_visibility_changed_callback_ &&
any_client_was_visible != any_client_is_visible) {
on_visibility_changed_callback_.Run(any_client_is_visible);
}
}
void VisibilityMetricsLogger::SetOnVisibilityChangedCallback(
OnVisibilityChangedCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
on_visibility_changed_callback_ = std::move(callback);
}
void VisibilityMetricsLogger::RecordMetrics() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
UpdateDurations();
RecordVisibilityMetrics();
RecordVisibleSchemeMetrics();
RecordScreenCoverageMetrics();
}
void VisibilityMetricsLogger::RecordVisibilityMetrics() {
int32_t any_webview_visible_seconds;
int32_t no_webview_visible_seconds;
int32_t total_webview_visible_seconds;
int32_t total_no_webview_visible_seconds;
any_webview_visible_seconds =
all_clients_tracker_.any_webview_tracked_duration_.InSeconds();
all_clients_tracker_.any_webview_tracked_duration_ -=
base::Seconds(any_webview_visible_seconds);
no_webview_visible_seconds =
all_clients_tracker_.no_webview_tracked_duration_.InSeconds();
all_clients_tracker_.no_webview_tracked_duration_ -=
base::Seconds(no_webview_visible_seconds);
total_webview_visible_seconds =
all_clients_tracker_.per_webview_duration_.InSeconds();
all_clients_tracker_.per_webview_duration_ -=
base::Seconds(total_webview_visible_seconds);
total_no_webview_visible_seconds =
all_clients_tracker_.per_webview_untracked_duration_.InSeconds();
all_clients_tracker_.per_webview_untracked_duration_ -=
base::Seconds(total_no_webview_visible_seconds);
if (any_webview_visible_seconds) {
GetGlobalVisibilityHistogram()->AddCount(
static_cast<int>(Visibility::kVisible), any_webview_visible_seconds);
}
if (no_webview_visible_seconds) {
GetGlobalVisibilityHistogram()->AddCount(
static_cast<int>(Visibility::kNotVisible), no_webview_visible_seconds);
}
if (total_webview_visible_seconds) {
GetPerWebViewVisibilityHistogram()->AddCount(
static_cast<int>(Visibility::kVisible), total_webview_visible_seconds);
}
if (total_no_webview_visible_seconds) {
GetPerWebViewVisibilityHistogram()->AddCount(
static_cast<int>(Visibility::kNotVisible),
total_no_webview_visible_seconds);
}
}
void VisibilityMetricsLogger::RecordVisibleSchemeMetrics() {
for (size_t i = 0; i < std::size(per_scheme_trackers_); i++) {
Scheme scheme = static_cast<Scheme>(i);
auto& tracker = per_scheme_trackers_[i];
int32_t any_webview_seconds =
tracker.any_webview_tracked_duration_.InSeconds();
if (any_webview_seconds) {
tracker.any_webview_tracked_duration_ -=
base::Seconds(any_webview_seconds);
LogGlobalVisibleScheme(scheme, any_webview_seconds);
}
int32_t per_webview_seconds = tracker.per_webview_duration_.InSeconds();
if (per_webview_seconds) {
tracker.per_webview_duration_ -= base::Seconds(per_webview_seconds);
LogPerWebViewVisibleScheme(scheme, per_webview_seconds);
}
}
}
void VisibilityMetricsLogger::RecordScreenCoverageMetrics() {
for (size_t i = 0; i < std::size(global_coverage_percentage_durations_);
i++) {
int32_t seconds = global_coverage_percentage_durations_[i].InSeconds();
if (seconds == 0)
continue;
global_coverage_percentage_durations_[i] -= base::Seconds(seconds);
LogGlobalVisibleScreenCoverage(i, seconds);
}
for (auto& scheme_and_map : schemes_to_percentages_to_durations_) {
for (auto& percentage_and_duration : scheme_and_map.second) {
int32_t seconds = percentage_and_duration.second.InSeconds();
if (seconds == 0)
continue;
percentage_and_duration.second -= base::Seconds(seconds);
LogPerWebViewVisibleScreenCoverage(percentage_and_duration.first,
seconds);
LogPerSchemeVisibleScreenCoverage(scheme_and_map.first,
percentage_and_duration.first, seconds);
}
}
}
} // namespace android_webview