app_list_service.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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/ui/app_list/app_list_service.h"
6
7#include "base/bind.h"
8#include "base/callback.h"
9#include "base/command_line.h"
10#include "base/metrics/histogram.h"
11#include "base/prefs/pref_registry_simple.h"
12#include "base/process/process_info.h"
13#include "base/strings/string_number_conversions.h"
14#include "base/time/time.h"
15#include "chrome/common/chrome_switches.h"
16#include "chrome/common/pref_names.h"
17
18namespace {
19
20enum StartupType {
21  COLD_START,
22  WARM_START,
23  WARM_START_FAST,
24};
25
26base::Time GetOriginalProcessStartTime(const CommandLine& command_line) {
27  if (command_line.HasSwitch(switches::kOriginalProcessStartTime)) {
28    std::string start_time_string =
29        command_line.GetSwitchValueASCII(switches::kOriginalProcessStartTime);
30    int64 remote_start_time;
31    base::StringToInt64(start_time_string, &remote_start_time);
32    return base::Time::FromInternalValue(remote_start_time);
33  }
34
35// base::CurrentProcessInfo::CreationTime() is only defined on some
36// platforms.
37#if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
38  return base::CurrentProcessInfo::CreationTime();
39#endif
40  return base::Time();
41}
42
43StartupType GetStartupType(const CommandLine& command_line) {
44  // The presence of kOriginalProcessStartTime implies that another process
45  // has sent us its command line to handle, ie: we are already running.
46  if (command_line.HasSwitch(switches::kOriginalProcessStartTime)) {
47    return command_line.HasSwitch(switches::kFastStart) ?
48        WARM_START_FAST : WARM_START;
49  }
50  return COLD_START;
51}
52
53void RecordFirstPaintTiming(StartupType startup_type,
54                            const base::Time& start_time) {
55  base::TimeDelta elapsed = base::Time::Now() - start_time;
56  switch (startup_type) {
57    case COLD_START:
58      UMA_HISTOGRAM_LONG_TIMES("Startup.AppListFirstPaintColdStart", elapsed);
59      break;
60    case WARM_START:
61      UMA_HISTOGRAM_LONG_TIMES("Startup.AppListFirstPaintWarmStart", elapsed);
62      break;
63    case WARM_START_FAST:
64      UMA_HISTOGRAM_LONG_TIMES("Startup.AppListFirstPaintWarmStartFast",
65                               elapsed);
66      break;
67  }
68}
69
70}  // namespace
71
72// static
73void AppListService::RegisterPrefs(PrefRegistrySimple* registry) {
74  registry->RegisterInt64Pref(prefs::kLastAppListLaunchPing, 0);
75  registry->RegisterIntegerPref(prefs::kAppListLaunchCount, 0);
76  registry->RegisterInt64Pref(prefs::kLastAppListAppLaunchPing, 0);
77  registry->RegisterIntegerPref(prefs::kAppListAppLaunchCount, 0);
78  registry->RegisterStringPref(prefs::kAppListProfile, std::string());
79  registry->RegisterBooleanPref(prefs::kRestartWithAppList, false);
80  registry->RegisterBooleanPref(prefs::kAppLauncherIsEnabled, false);
81  registry->RegisterBooleanPref(prefs::kAppLauncherHasBeenEnabled, false);
82
83#if defined(OS_MACOSX)
84  registry->RegisterIntegerPref(prefs::kAppLauncherShortcutVersion, 0);
85#endif
86
87  // Identifies whether we should show the app launcher promo or not.
88  // Note that a field trial also controls the showing, so the promo won't show
89  // unless the pref is set AND the field trial is set to a proper group.
90  registry->RegisterBooleanPref(prefs::kShowAppLauncherPromo, true);
91}
92
93// static
94void AppListService::RecordShowTimings(const CommandLine& command_line) {
95  base::Time start_time = GetOriginalProcessStartTime(command_line);
96  if (start_time.is_null())
97    return;
98
99  base::TimeDelta elapsed = base::Time::Now() - start_time;
100  StartupType startup = GetStartupType(command_line);
101  switch (startup) {
102    case COLD_START:
103      UMA_HISTOGRAM_LONG_TIMES("Startup.ShowAppListColdStart", elapsed);
104      break;
105    case WARM_START:
106      UMA_HISTOGRAM_LONG_TIMES("Startup.ShowAppListWarmStart", elapsed);
107      break;
108    case WARM_START_FAST:
109      UMA_HISTOGRAM_LONG_TIMES("Startup.ShowAppListWarmStartFast", elapsed);
110      break;
111  }
112
113  AppListService::Get()->SetAppListNextPaintCallback(
114      base::Bind(RecordFirstPaintTiming, startup, start_time));
115}
116