app_list_service.cc revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
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/metrics/histogram.h"
8#include "base/prefs/pref_registry_simple.h"
9#include "base/prefs/pref_service.h"
10#include "chrome/browser/browser_process.h"
11#include "chrome/browser/extensions/extension_prefs.h"
12#include "chrome/common/chrome_constants.h"
13#include "chrome/common/pref_names.h"
14
15namespace {
16
17void SendAppListAppLaunch(int count) {
18  UMA_HISTOGRAM_CUSTOM_COUNTS(
19      "Apps.AppListDailyAppLaunches", count, 1, 1000, 50);
20  if (count > 0)
21    UMA_HISTOGRAM_ENUMERATION("Apps.AppListHasLaunchedAppToday", 1, 2);
22}
23
24void SendAppListLaunch(int count) {
25  UMA_HISTOGRAM_CUSTOM_COUNTS(
26      "Apps.AppListDailyLaunches", count, 1, 1000, 50);
27  if (count > 0)
28    UMA_HISTOGRAM_ENUMERATION("Apps.AppListHasLaunchedAppListToday", 1, 2);
29}
30
31bool SendDailyEventFrequency(
32    const char* last_ping_pref,
33    const char* count_pref,
34    void (*send_callback)(int count)) {
35  PrefService* local_state = g_browser_process->local_state();
36
37  base::Time now = base::Time::Now();
38  base::Time last = base::Time::FromInternalValue(local_state->GetInt64(
39      last_ping_pref));
40  int days = (now - last).InDays();
41  if (days > 0) {
42    send_callback(local_state->GetInteger(count_pref));
43    local_state->SetInt64(
44        last_ping_pref,
45        (last + base::TimeDelta::FromDays(days)).ToInternalValue());
46    local_state->SetInteger(count_pref, 0);
47    return true;
48  }
49  return false;
50}
51
52void RecordDailyEventFrequency(
53    const char* last_ping_pref,
54    const char* count_pref,
55    void (*send_callback)(int count)) {
56  PrefService* local_state = g_browser_process->local_state();
57
58  int count = local_state->GetInteger(count_pref);
59  local_state->SetInteger(count_pref, count + 1);
60  if (SendDailyEventFrequency(last_ping_pref, count_pref, send_callback)) {
61    local_state->SetInteger(count_pref, 1);
62  }
63}
64
65}  // namespace
66
67// static
68void AppListService::RegisterPrefs(PrefRegistrySimple* registry) {
69  registry->RegisterInt64Pref(prefs::kLastAppListLaunchPing, 0);
70  registry->RegisterIntegerPref(prefs::kAppListLaunchCount, 0);
71  registry->RegisterInt64Pref(prefs::kLastAppListAppLaunchPing, 0);
72  registry->RegisterIntegerPref(prefs::kAppListAppLaunchCount, 0);
73  registry->RegisterStringPref(prefs::kAppListProfile, std::string());
74#if defined(OS_WIN)
75  registry->RegisterBooleanPref(prefs::kRestartWithAppList, false);
76#endif
77}
78
79void AppListService::Init(Profile* initial_profile) {}
80
81base::FilePath AppListService::GetAppListProfilePath(
82    const base::FilePath& user_data_dir) {
83  PrefService* local_state = g_browser_process->local_state();
84  DCHECK(local_state);
85
86  std::string app_list_profile;
87  if (local_state->HasPrefPath(prefs::kAppListProfile))
88    app_list_profile = local_state->GetString(prefs::kAppListProfile);
89
90  // If the user has no profile preference for the app launcher, default to the
91  // last browser profile used.
92  if (app_list_profile.empty() &&
93      local_state->HasPrefPath(prefs::kProfileLastUsed))
94    app_list_profile = local_state->GetString(prefs::kProfileLastUsed);
95
96  std::string profile_path = app_list_profile.empty() ?
97      chrome::kInitialProfile :
98      app_list_profile;
99
100  return user_data_dir.AppendASCII(profile_path);
101}
102
103// static
104void AppListService::RecordAppListLaunch() {
105  RecordDailyEventFrequency(prefs::kLastAppListLaunchPing,
106                            prefs::kAppListLaunchCount,
107                            &SendAppListLaunch);
108}
109
110// static
111void AppListService::RecordAppListAppLaunch() {
112  RecordDailyEventFrequency(prefs::kLastAppListAppLaunchPing,
113                            prefs::kAppListAppLaunchCount,
114                            &SendAppListAppLaunch);
115}
116
117// static
118void AppListService::SendAppListStats() {
119  if (!g_browser_process || g_browser_process->IsShuttingDown())
120    return;
121  SendDailyEventFrequency(prefs::kLastAppListLaunchPing,
122                          prefs::kAppListLaunchCount,
123                          &SendAppListLaunch);
124  SendDailyEventFrequency(prefs::kLastAppListAppLaunchPing,
125                          prefs::kAppListAppLaunchCount,
126                          &SendAppListAppLaunch);
127}
128
129void AppListService::ShowAppList(Profile* profile) {}
130
131void AppListService::DismissAppList() {}
132
133void AppListService::SetAppListProfile(
134    const base::FilePath& profile_file_path) {}
135
136Profile* AppListService::GetCurrentAppListProfile() { return NULL; }
137
138bool AppListService::IsAppListVisible() const { return false; }
139
140void AppListService::EnableAppList() {}
141
142void AppListService::OnProfileAdded(const base::FilePath& profilePath) {}
143
144void AppListService::OnProfileWillBeRemoved(
145    const base::FilePath& profile_path) {}
146
147void AppListService::OnProfileWasRemoved(const base::FilePath& profile_path,
148                                         const string16& profile_name) {}
149
150void AppListService::OnProfileNameChanged(const base::FilePath& profile_path,
151                                          const string16& profile_name) {}
152
153void AppListService::OnProfileAvatarChanged(
154    const base::FilePath& profile_path) {}
155
156AppListControllerDelegate* AppListService::CreateControllerDelegate() {
157  return NULL;
158}
159