task_switch_metrics_recorder.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2015 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. #ifndef ASH_METRICS_TASK_SWITCH_METRICS_RECORDER_H_
  5. #define ASH_METRICS_TASK_SWITCH_METRICS_RECORDER_H_
  6. #include <memory>
  7. #include <string>
  8. #include <unordered_map>
  9. #include "ash/ash_export.h"
  10. #include "ash/metrics/task_switch_source.h"
  11. namespace ash {
  12. class TaskSwitchTimeTracker;
  13. // The TaskSwitchMetricsRecorder class records UMA metrics related to task
  14. // switching. The main purpose of the TaskSwitchMetricsRecorder is to track time
  15. // deltas between task switches and record histograms of the deltas.
  16. class ASH_EXPORT TaskSwitchMetricsRecorder {
  17. public:
  18. TaskSwitchMetricsRecorder();
  19. TaskSwitchMetricsRecorder(const TaskSwitchMetricsRecorder&) = delete;
  20. TaskSwitchMetricsRecorder& operator=(const TaskSwitchMetricsRecorder&) =
  21. delete;
  22. virtual ~TaskSwitchMetricsRecorder();
  23. // Notifies |this| that a "navigate to" task switch has occurred from the
  24. // specified |task_switch_source|. The metrics associated with
  25. // TaskSwitchSource::ANY source will be updated as well.
  26. //
  27. // NOTE: A |task_switch_source| value of TaskSwitchSource::ANY should not be
  28. // used and behavior is undefined if it is.
  29. //
  30. // A "navigate to" operation is defined by a task switch where the specific
  31. // task that becomes active is user-predictable (e.g., Alt+Tab accelerator,
  32. // launching a new window via the shelf, etc). Contrast to a "navigate away"
  33. // operation which is defined as a user interaction that navigates away from a
  34. // specified task and the next task that becomes active is likely not
  35. // user-predictable (e.g., closing or minimizing a window, closing a tab,
  36. // etc).
  37. //
  38. // Will add an entry to |histogram_map_| when called for the first time for
  39. // each |task_switch_source| value.
  40. void OnTaskSwitch(TaskSwitchSource task_switch_source);
  41. private:
  42. // Internal implementation of OnTaskSwitch(TaskSwitchSource) that will accept
  43. // the TaskSwitchSource::ANY value.
  44. void OnTaskSwitchInternal(TaskSwitchSource task_switch_source);
  45. // Returns the TaskSwitchTimeTracker associated with the specified
  46. // |task_switch_source|. May return nullptr if mapping does not exist yet.
  47. TaskSwitchTimeTracker* FindTaskSwitchTimeTracker(
  48. TaskSwitchSource task_switch_source);
  49. // Adds a TaskSwitchTimeTracker to |histogram_map_| for the specified
  50. // |task_switch_source|. Behavior is undefined if a TaskSwitchTimeTracker
  51. // |histogram_map_| already has an entry for |task_switch_source|.
  52. void AddTaskSwitchTimeTracker(TaskSwitchSource task_switch_source);
  53. // Tracks TaskSwitchSource to TaskSwitchTimeTracker mappings. The
  54. // |histogram_map_| is populated on demand the first time a
  55. // TaskSwitchTimeTracker is needed for a given source.
  56. std::unordered_map<int, std::unique_ptr<TaskSwitchTimeTracker>>
  57. histogram_map_;
  58. };
  59. } // namespace ash
  60. #endif // ASH_METRICS_TASK_SWITCH_METRICS_RECORDER_H_