Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / ui / app_list / app_list_service.cc
blob210e8f41572dcc665bdea117f6f4208dffbe1d90
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.
5 #include "chrome/browser/ui/app_list/app_list_service.h"
7 #include "base/command_line.h"
8 #include "base/metrics/histogram.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/process/process_info.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/pref_names.h"
17 namespace {
19 enum StartupType {
20 COLD_START,
21 WARM_START,
22 WARM_START_FAST,
25 // For when an app list show request is received via CommandLine. Indicates
26 // whether the Profile the app list was previously showing was the SAME, OTHER
27 // or NONE with respect to the new Profile to show.
28 enum ProfileLoadState {
29 PROFILE_LOADED_SAME,
30 PROFILE_LOADED_OTHER,
31 PROFILE_LOADED_NONE,
34 base::Time GetOriginalProcessStartTime(const base::CommandLine& command_line) {
35 if (command_line.HasSwitch(switches::kOriginalProcessStartTime)) {
36 std::string start_time_string =
37 command_line.GetSwitchValueASCII(switches::kOriginalProcessStartTime);
38 int64 remote_start_time;
39 base::StringToInt64(start_time_string, &remote_start_time);
40 return base::Time::FromInternalValue(remote_start_time);
43 // base::CurrentProcessInfo::CreationTime() is only defined on some
44 // platforms.
45 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
46 return base::CurrentProcessInfo::CreationTime();
47 #else
48 return base::Time();
49 #endif
52 StartupType GetStartupType(const base::CommandLine& command_line) {
53 // The presence of kOriginalProcessStartTime implies that another process
54 // has sent us its command line to handle, ie: we are already running.
55 if (command_line.HasSwitch(switches::kOriginalProcessStartTime)) {
56 return command_line.HasSwitch(switches::kFastStart) ?
57 WARM_START_FAST : WARM_START;
59 return COLD_START;
62 // The time the process that caused the app list to be shown started. This isn't
63 // necessarily the currently executing process as we may be processing a command
64 // line given to a short-lived Chrome instance.
65 int64 g_original_process_start_time;
67 // The type of startup the the current app list show has gone through.
68 StartupType g_app_show_startup_type;
70 // The state of the active app list profile at the most recent launch.
71 ProfileLoadState g_profile_load_state;
73 void RecordFirstPaintTiming() {
74 base::Time start_time(
75 base::Time::FromInternalValue(g_original_process_start_time));
76 base::TimeDelta elapsed = base::Time::Now() - start_time;
77 switch (g_app_show_startup_type) {
78 case COLD_START:
79 DCHECK_EQ(PROFILE_LOADED_NONE, g_profile_load_state);
80 UMA_HISTOGRAM_LONG_TIMES("Startup.AppListFirstPaintColdStart", elapsed);
81 break;
82 case WARM_START:
83 // For warm starts, only record showing the same profile. "NONE" should
84 // only occur in the first 30 seconds after startup. "OTHER" only occurs
85 // for multi-profile cases. In these cases, timings are also affected by
86 // whether or not a profile has been loaded from disk, which makes the
87 // profile load asynchronous and skews results unpredictably.
88 if (g_profile_load_state == PROFILE_LOADED_SAME)
89 UMA_HISTOGRAM_LONG_TIMES("Startup.AppListFirstPaintWarmStart", elapsed);
90 break;
91 case WARM_START_FAST:
92 if (g_profile_load_state == PROFILE_LOADED_SAME) {
93 UMA_HISTOGRAM_LONG_TIMES("Startup.AppListFirstPaintWarmStartFast",
94 elapsed);
96 break;
100 void RecordStartupInfo(AppListService* service,
101 const base::CommandLine& command_line,
102 Profile* launch_profile) {
103 base::Time start_time = GetOriginalProcessStartTime(command_line);
104 if (start_time.is_null())
105 return;
107 base::TimeDelta elapsed = base::Time::Now() - start_time;
108 StartupType startup_type = GetStartupType(command_line);
109 switch (startup_type) {
110 case COLD_START:
111 UMA_HISTOGRAM_LONG_TIMES("Startup.ShowAppListColdStart", elapsed);
112 break;
113 case WARM_START:
114 UMA_HISTOGRAM_LONG_TIMES("Startup.ShowAppListWarmStart", elapsed);
115 break;
116 case WARM_START_FAST:
117 UMA_HISTOGRAM_LONG_TIMES("Startup.ShowAppListWarmStartFast", elapsed);
118 break;
121 g_original_process_start_time = start_time.ToInternalValue();
122 g_app_show_startup_type = startup_type;
124 Profile* current_profile = service->GetCurrentAppListProfile();
125 if (!current_profile)
126 g_profile_load_state = PROFILE_LOADED_NONE;
127 else if (current_profile == launch_profile)
128 g_profile_load_state = PROFILE_LOADED_SAME;
129 else
130 g_profile_load_state = PROFILE_LOADED_OTHER;
132 service->SetAppListNextPaintCallback(RecordFirstPaintTiming);
135 } // namespace
137 // static
138 void AppListService::RegisterPrefs(PrefRegistrySimple* registry) {
139 registry->RegisterInt64Pref(prefs::kLastAppListLaunchPing, 0);
140 registry->RegisterIntegerPref(prefs::kAppListLaunchCount, 0);
141 registry->RegisterInt64Pref(prefs::kLastAppListAppLaunchPing, 0);
142 registry->RegisterIntegerPref(prefs::kAppListAppLaunchCount, 0);
143 registry->RegisterStringPref(prefs::kAppListProfile, std::string());
144 registry->RegisterBooleanPref(prefs::kAppLauncherHasBeenEnabled, false);
145 registry->RegisterIntegerPref(prefs::kAppListEnableMethod,
146 ENABLE_NOT_RECORDED);
147 registry->RegisterInt64Pref(prefs::kAppListEnableTime, 0);
148 registry->RegisterInt64Pref(prefs::kAppListLastLaunchTime, 0);
150 #if defined(OS_MACOSX)
151 registry->RegisterIntegerPref(prefs::kAppLauncherShortcutVersion, 0);
152 #endif
154 // Identifies whether we should show the app launcher promo or not. This
155 // becomes false when the user dismisses the promo.
156 registry->RegisterBooleanPref(prefs::kShowAppLauncherPromo, true);
159 // static
160 bool AppListService::HandleLaunchCommandLine(
161 const base::CommandLine& command_line,
162 Profile* launch_profile) {
163 InitAll(launch_profile, launch_profile->GetPath());
164 if (!command_line.HasSwitch(switches::kShowAppList))
165 return false;
167 // The --show-app-list switch is used for shortcuts on the native desktop.
168 AppListService* service = Get(chrome::HOST_DESKTOP_TYPE_NATIVE);
169 DCHECK(service);
170 RecordStartupInfo(service, command_line, launch_profile);
171 service->ShowForProfile(launch_profile);
172 return true;