Chromium Code Reviews| Index: ui/events/fraction_of_time_without_user_input_recorder.cc |
| diff --git a/ui/events/fraction_of_time_without_user_input_recorder.cc b/ui/events/fraction_of_time_without_user_input_recorder.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b12f145d3038e832e1a785e26bfb1c75af82f4e7 |
| --- /dev/null |
| +++ b/ui/events/fraction_of_time_without_user_input_recorder.cc |
| @@ -0,0 +1,79 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/events/fraction_of_time_without_user_input_recorder.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/metrics/histogram_macros.h" |
| + |
| +namespace { |
| + |
| +const base::TimeDelta DEFAULT_WINDOW_SIZE = base::TimeDelta::FromSecondsD(10); |
| +const base::TimeDelta DEFAULT_IDLE_TIMEOUT = |
|
sadrul
2017/03/19 00:38:51
constexpr
tdresser
2017/03/24 14:56:30
From https://chromium-cpp.appspot.com/
"Prefer to
sadrul
2017/03/24 17:29:33
I read that as 'Prefer constexpr to const'. Withou
tdresser
2017/03/27 18:59:26
Whoops, yeah, I repeatedly misread that. Thanks.
|
| + base::TimeDelta::FromSecondsD(0.05); |
| + |
| +} // namespace |
| + |
| +namespace ui { |
| + |
| +FractionOfTimeWithoutUserInputRecorder::FractionOfTimeWithoutUserInputRecorder() |
| + : window_size_(DEFAULT_WINDOW_SIZE), idle_timeout_(DEFAULT_IDLE_TIMEOUT) {} |
| + |
| +void FractionOfTimeWithoutUserInputRecorder::RecordEventAtTime( |
| + base::TimeTicks start_time) { |
| + base::TimeTicks event_end_time = start_time + idle_timeout_; |
| + |
| + if (active_duration_start_time_.is_null()) |
| + active_duration_start_time_ = start_time; |
| + if (previous_event_end_time_.is_null()) |
| + previous_event_end_time_ = start_time; |
| + |
| + // The user is no longer interacting with the page. Report the previous active |
| + // duration. |
|
sadrul
2017/03/19 00:38:51
s/with the page/<something more generic, like inte
tdresser
2017/03/24 14:56:30
Went with "browser".
|
| + if (previous_event_end_time_ < start_time) { |
| + RecordActiveInterval(active_duration_start_time_, previous_event_end_time_); |
| + active_duration_start_time_ = start_time; |
| + } |
| + |
| + previous_event_end_time_ = event_end_time; |
| +} |
| + |
| +void FractionOfTimeWithoutUserInputRecorder::RecordActiveInterval( |
| + base::TimeTicks start_time, |
| + base::TimeTicks end_time) { |
| + if (window_start_time_.is_null()) |
| + window_start_time_ = start_time; |
| + |
| + base::TimeTicks window_end_time; |
| + |
| + while (true) { |
| + window_end_time = window_start_time_ + window_size_; |
| + base::TimeDelta interval_in_window_duration = |
| + std::min(end_time, window_end_time) - |
| + std::max(start_time, window_start_time_); |
| + interval_in_window_duration = |
| + std::max(interval_in_window_duration, base::TimeDelta()); |
|
sadrul
2017/03/19 00:38:51
Is this needed? (i.e. can interval_in_window_durat
tdresser
2017/03/24 14:56:30
We're computing how much of the task is inside the
|
| + |
| + current_window_active_time_ += interval_in_window_duration; |
| + |
| + // If we haven't exceeded the window bounds, we're done. |
| + if (end_time < window_end_time) |
| + break; |
| + |
| + RecordToUma(current_window_active_time_.InMillisecondsF() / |
| + window_size_.InMillisecondsF()); |
| + |
| + current_window_active_time_ = base::TimeDelta(); |
| + window_start_time_ = window_end_time; |
| + } |
| +} |
| + |
| +void FractionOfTimeWithoutUserInputRecorder::RecordToUma( |
| + float fraction_active) { |
| + UMA_HISTOGRAM_PERCENTAGE("Event.FractionOfTimeWithoutUserInput", |
| + (1 - fraction_active) * 100); |
| +} |
| + |
| +} // namespace ui |