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 "ui/events/fraction_of_time_without_user_input_recorder.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/metrics/histogram_macros.h" |
| 10 |
| 11 namespace { |
| 12 |
| 13 constexpr base::TimeDelta DEFAULT_WINDOW_SIZE = |
| 14 base::TimeDelta::FromSecondsD(10); |
| 15 constexpr base::TimeDelta DEFAULT_IDLE_TIMEOUT = |
| 16 base::TimeDelta::FromSecondsD(0.05); |
| 17 |
| 18 } // namespace |
| 19 |
| 20 namespace ui { |
| 21 |
| 22 FractionOfTimeWithoutUserInputRecorder::FractionOfTimeWithoutUserInputRecorder() |
| 23 : window_size_(DEFAULT_WINDOW_SIZE), idle_timeout_(DEFAULT_IDLE_TIMEOUT) {} |
| 24 |
| 25 void FractionOfTimeWithoutUserInputRecorder::RecordEventAtTime( |
| 26 base::TimeTicks start_time) { |
| 27 base::TimeTicks event_end_time = start_time + idle_timeout_; |
| 28 |
| 29 if (active_duration_start_time_.is_null()) |
| 30 active_duration_start_time_ = start_time; |
| 31 if (previous_event_end_time_.is_null()) |
| 32 previous_event_end_time_ = start_time; |
| 33 |
| 34 // The user is no longer interacting with the browser. Report the previous |
| 35 // active duration. |
| 36 if (previous_event_end_time_ < start_time) { |
| 37 RecordActiveInterval(active_duration_start_time_, previous_event_end_time_); |
| 38 active_duration_start_time_ = start_time; |
| 39 } |
| 40 |
| 41 previous_event_end_time_ = event_end_time; |
| 42 } |
| 43 |
| 44 void FractionOfTimeWithoutUserInputRecorder::RecordActiveInterval( |
| 45 base::TimeTicks start_time, |
| 46 base::TimeTicks end_time) { |
| 47 if (window_start_time_.is_null()) |
| 48 window_start_time_ = start_time; |
| 49 |
| 50 base::TimeTicks window_end_time; |
| 51 |
| 52 while (true) { |
| 53 window_end_time = window_start_time_ + window_size_; |
| 54 base::TimeDelta interval_in_window_duration = |
| 55 std::min(end_time, window_end_time) - |
| 56 std::max(start_time, window_start_time_); |
| 57 interval_in_window_duration = |
| 58 std::max(interval_in_window_duration, base::TimeDelta()); |
| 59 |
| 60 current_window_active_time_ += interval_in_window_duration; |
| 61 |
| 62 // If we haven't exceeded the window bounds, we're done. |
| 63 if (end_time < window_end_time) |
| 64 break; |
| 65 |
| 66 RecordToUma(current_window_active_time_.InMillisecondsF() / |
| 67 window_size_.InMillisecondsF()); |
| 68 |
| 69 current_window_active_time_ = base::TimeDelta(); |
| 70 window_start_time_ = window_end_time; |
| 71 } |
| 72 } |
| 73 |
| 74 void FractionOfTimeWithoutUserInputRecorder::RecordToUma( |
| 75 float fraction_active) const { |
| 76 UMA_HISTOGRAM_PERCENTAGE("Event.FractionOfTimeWithoutUserInput", |
| 77 std::round((1 - fraction_active) * 100)); |
| 78 } |
| 79 |
| 80 } // namespace ui |
OLD | NEW |