1// Copyright 2013 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 "chrome/browser/chromeos/first_run/step.h"
6
7#include <cctype>
8
9#include "ash/first_run/first_run_helper.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/metrics/histogram.h"
12#include "chrome/browser/ui/webui/chromeos/first_run/first_run_actor.h"
13#include "ui/gfx/size.h"
14#include "ui/views/widget/widget.h"
15
16namespace {
17
18// Converts from "with-dashes-names" to "WithDashesNames".
19std::string ToCamelCase(const std::string& name) {
20  std::string result;
21  bool next_to_upper = true;
22  for (size_t i = 0; i < name.length(); ++i) {
23    if (name[i] == '-') {
24      next_to_upper = true;
25    } else if (next_to_upper) {
26      result.push_back(std::toupper(name[i]));
27      next_to_upper = false;
28    } else {
29      result.push_back(name[i]);
30    }
31  }
32  return result;
33}
34
35}  // namespace
36
37namespace chromeos {
38namespace first_run {
39
40Step::Step(const std::string& name,
41           ash::FirstRunHelper* shell_helper,
42           FirstRunActor* actor)
43    : name_(name),
44      shell_helper_(shell_helper),
45      actor_(actor) {
46}
47
48Step::~Step() { RecordCompletion(); }
49
50void Step::Show() {
51  show_time_ = base::Time::Now();
52  DoShow();
53}
54
55void Step::OnBeforeHide() {
56  actor()->RemoveBackgroundHoles();
57  DoOnBeforeHide();
58}
59
60void Step::OnAfterHide() {
61  RecordCompletion();
62  DoOnAfterHide();
63}
64
65gfx::Size Step::GetOverlaySize() const {
66  return shell_helper()->GetOverlayWidget()->GetWindowBoundsInScreen().size();
67}
68
69void Step::RecordCompletion() {
70  if (show_time_.is_null())
71    return;
72  std::string histogram_name =
73      "CrosFirstRun.TimeSpentOnStep" + ToCamelCase(name());
74  // Equivalent to using UMA_HISTOGRAM_CUSTOM_TIMES with 50 buckets on range
75  // [100ms, 3 min.]. UMA_HISTOGRAM_CUSTOM_TIMES can not be used here, because
76  // |histogram_name| is calculated dynamically and changes from call to call.
77  base::HistogramBase* histogram = base::Histogram::FactoryTimeGet(
78      histogram_name,
79      base::TimeDelta::FromMilliseconds(100),
80      base::TimeDelta::FromMinutes(3),
81      50,
82      base::HistogramBase::kUmaTargetedHistogramFlag);
83  histogram->AddTime(base::Time::Now() - show_time_);
84  show_time_ = base::Time();
85}
86
87}  // namespace first_run
88}  // namespace chromeos
89
90