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

ash / metrics / task_switch_metrics_recorder.cc [blame]

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/metrics/task_switch_metrics_recorder.h"

#include <memory>

#include "ash/metrics/task_switch_time_tracker.h"
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/notreached.h"

namespace ash {

namespace {

const char kAcceleratorWindowCycleHistogramName[] =
    "Ash.WindowCycleController.TimeBetweenTaskSwitches";

const char kOverviewModeHistogramName[] =
    "Ash.Overview.TimeBetweenActiveWindowChanges";

// Returns the histogram name for the given |task_switch_source|.
const char* GetHistogramName(TaskSwitchSource task_switch_source) {
  switch (task_switch_source) {
    case TaskSwitchSource::OVERVIEW_MODE:
      return kOverviewModeHistogramName;
    case TaskSwitchSource::WINDOW_CYCLE_CONTROLLER:
      return kAcceleratorWindowCycleHistogramName;
    case TaskSwitchSource::ANY:
    case TaskSwitchSource::DESKTOP:
    case TaskSwitchSource::SHELF:
      return nullptr;
  }
  NOTREACHED();
}

}  // namespace

TaskSwitchMetricsRecorder::TaskSwitchMetricsRecorder() = default;

TaskSwitchMetricsRecorder::~TaskSwitchMetricsRecorder() = default;

void TaskSwitchMetricsRecorder::OnTaskSwitch(
    TaskSwitchSource task_switch_source) {
  DCHECK_NE(task_switch_source, TaskSwitchSource::ANY);
  if (task_switch_source != TaskSwitchSource::ANY) {
    OnTaskSwitchInternal(task_switch_source);
    OnTaskSwitchInternal(TaskSwitchSource::ANY);
  }
}

void TaskSwitchMetricsRecorder::OnTaskSwitchInternal(
    TaskSwitchSource task_switch_source) {
  TaskSwitchTimeTracker* task_switch_time_tracker =
      FindTaskSwitchTimeTracker(task_switch_source);
  if (!task_switch_time_tracker)
    AddTaskSwitchTimeTracker(task_switch_source);

  task_switch_time_tracker = FindTaskSwitchTimeTracker(task_switch_source);
  if (task_switch_time_tracker) {
    task_switch_time_tracker->OnTaskSwitch();
  }
}

TaskSwitchTimeTracker* TaskSwitchMetricsRecorder::FindTaskSwitchTimeTracker(
    TaskSwitchSource task_switch_source) {
  auto it = histogram_map_.find(static_cast<int>(task_switch_source));
  if (it == histogram_map_.end())
    return nullptr;

  return it->second.get();
}

void TaskSwitchMetricsRecorder::AddTaskSwitchTimeTracker(
    TaskSwitchSource task_switch_source) {
  CHECK(!base::Contains(histogram_map_, static_cast<int>(task_switch_source)));

  const char* histogram_name = GetHistogramName(task_switch_source);
  if (histogram_name) {
    histogram_map_[static_cast<int>(task_switch_source)] =
        std::make_unique<TaskSwitchTimeTracker>(histogram_name);
  }
}

}  // namespace ash