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