Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: chrome/browser/ui/app_list/app_list_service_impl.cc

Issue 24707002: Add unit tests for AppListServiceImpl. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: respond to comments Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/app_list/app_list_service_impl.h" 5 #include "chrome/browser/ui/app_list/app_list_service_impl.h"
6 6
7 #include <string>
8
7 #include "apps/pref_names.h" 9 #include "apps/pref_names.h"
8 #include "base/command_line.h"
9 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
10 #include "base/prefs/pref_service.h" 11 #include "base/prefs/pref_service.h"
12 #include "base/strings/string16.h"
11 #include "base/time/time.h" 13 #include "base/time/time.h"
12 #include "chrome/browser/browser_process.h" 14 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/profiles/profile_manager.h" 15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/ui/app_list/keep_alive_service.h"
17 #include "chrome/browser/ui/app_list/keep_alive_service_impl.h"
18 #include "chrome/browser/ui/app_list/profile_loader.h"
19 #include "chrome/browser/ui/app_list/profile_store.h"
14 #include "chrome/common/chrome_constants.h" 20 #include "chrome/common/chrome_constants.h"
15 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h" 22 #include "chrome/common/pref_names.h"
17 #include "content/public/browser/browser_thread.h" 23 #include "content/public/browser/browser_thread.h"
18 24
19 namespace { 25 namespace {
20 26
21 void SendAppListAppLaunch(int count) { 27 void SendAppListAppLaunch(int count) {
22 UMA_HISTOGRAM_CUSTOM_COUNTS( 28 UMA_HISTOGRAM_CUSTOM_COUNTS(
23 "Apps.AppListDailyAppLaunches", count, 1, 1000, 50); 29 "Apps.AppListDailyAppLaunches", count, 1, 1000, 50);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 void (*send_callback)(int count)) { 65 void (*send_callback)(int count)) {
60 PrefService* local_state = g_browser_process->local_state(); 66 PrefService* local_state = g_browser_process->local_state();
61 67
62 int count = local_state->GetInteger(count_pref); 68 int count = local_state->GetInteger(count_pref);
63 local_state->SetInteger(count_pref, count + 1); 69 local_state->SetInteger(count_pref, count + 1);
64 if (SendDailyEventFrequency(last_ping_pref, count_pref, send_callback)) { 70 if (SendDailyEventFrequency(last_ping_pref, count_pref, send_callback)) {
65 local_state->SetInteger(count_pref, 1); 71 local_state->SetInteger(count_pref, 1);
66 } 72 }
67 } 73 }
68 74
69 bool HasAppListEnabledPreference() { 75 class ProfileStoreImpl : public ProfileStore {
70 PrefService* local_state = g_browser_process->local_state(); 76 public:
71 return local_state->GetBoolean(apps::prefs::kAppLauncherHasBeenEnabled); 77 explicit ProfileStoreImpl(ProfileManager* profile_manager)
72 } 78 : profile_manager_(profile_manager),
79 weak_factory_(this) {
80 }
73 81
74 void SetAppListEnabledPreference(bool enabled) { 82 virtual void AddProfileObserver(ProfileInfoCacheObserver* observer) OVERRIDE {
75 PrefService* local_state = g_browser_process->local_state(); 83 profile_manager_->GetProfileInfoCache().AddObserver(observer);
76 local_state->SetBoolean(apps::prefs::kAppLauncherHasBeenEnabled, enabled); 84 }
77 } 85
86 virtual void LoadProfileAsync(
87 const base::FilePath& path,
88 base::Callback<void(Profile*)> callback) OVERRIDE {
89 profile_manager_->CreateProfileAsync(
90 path,
91 base::Bind(&ProfileStoreImpl::OnProfileCreated,
92 weak_factory_.GetWeakPtr(),
93 callback),
94 base::string16(),
95 base::string16(),
96 std::string());
97 }
98
99 void OnProfileCreated(base::Callback<void(Profile*)> callback,
100 Profile* profile,
101 Profile::CreateStatus status) {
102 switch (status) {
103 case Profile::CREATE_STATUS_CREATED:
104 break;
105 case Profile::CREATE_STATUS_INITIALIZED:
106 callback.Run(profile);
107 break;
108 case Profile::CREATE_STATUS_LOCAL_FAIL:
109 case Profile::CREATE_STATUS_REMOTE_FAIL:
110 case Profile::CREATE_STATUS_CANCELED:
111 break;
112 case Profile::MAX_CREATE_STATUS:
113 NOTREACHED();
114 break;
115 }
116 }
117
118 virtual Profile* GetProfileByPath(const base::FilePath& path) OVERRIDE {
119 return profile_manager_->GetProfileByPath(path);
120 }
121
122 virtual base::FilePath GetUserDataDir() OVERRIDE {
123 return profile_manager_->user_data_dir();
124 }
125
126 virtual bool IsProfileManaged(const base::FilePath& profile_path) {
127 ProfileInfoCache& profile_info =
128 g_browser_process->profile_manager()->GetProfileInfoCache();
129 size_t profile_index = profile_info.GetIndexOfProfileWithPath(profile_path);
130 return profile_info.ProfileIsManagedAtIndex(profile_index);
131 }
132
133 private:
134 ProfileManager* profile_manager_;
135 base::WeakPtrFactory<ProfileStoreImpl> weak_factory_;
136 };
78 137
79 } // namespace 138 } // namespace
80 139
81 // static 140 // static
82 void AppListServiceImpl::RecordAppListLaunch() { 141 void AppListServiceImpl::RecordAppListLaunch() {
83 RecordDailyEventFrequency(prefs::kLastAppListLaunchPing, 142 RecordDailyEventFrequency(prefs::kLastAppListLaunchPing,
84 prefs::kAppListLaunchCount, 143 prefs::kAppListLaunchCount,
85 &SendAppListLaunch); 144 &SendAppListLaunch);
86 } 145 }
87 146
(...skipping 12 matching lines...) Expand all
100 SendDailyEventFrequency(prefs::kLastAppListLaunchPing, 159 SendDailyEventFrequency(prefs::kLastAppListLaunchPing,
101 prefs::kAppListLaunchCount, 160 prefs::kAppListLaunchCount,
102 &SendAppListLaunch); 161 &SendAppListLaunch);
103 SendDailyEventFrequency(prefs::kLastAppListAppLaunchPing, 162 SendDailyEventFrequency(prefs::kLastAppListAppLaunchPing,
104 prefs::kAppListAppLaunchCount, 163 prefs::kAppListAppLaunchCount,
105 &SendAppListAppLaunch); 164 &SendAppListAppLaunch);
106 } 165 }
107 166
108 AppListServiceImpl::AppListServiceImpl() 167 AppListServiceImpl::AppListServiceImpl()
109 : profile_(NULL), 168 : profile_(NULL),
110 profile_load_sequence_id_(0),
111 pending_profile_loads_(0),
112 weak_factory_(this), 169 weak_factory_(this),
113 profile_loader_(g_browser_process->profile_manager()) { 170 command_line_(*CommandLine::ForCurrentProcess()),
114 ProfileManager* profile_manager = g_browser_process->profile_manager(); 171 local_state_(g_browser_process->local_state()),
115 profile_manager->GetProfileInfoCache().AddObserver(this); 172 profile_store_(new ProfileStoreImpl(
173 g_browser_process->profile_manager())),
174 profile_loader_(new ProfileLoader(
175 profile_store_.get(), make_scoped_ptr(new KeepAliveServiceImpl))) {
176 profile_store_->AddProfileObserver(this);
177 }
178
179 AppListServiceImpl::AppListServiceImpl(
180 const CommandLine& command_line,
181 PrefService* local_state,
182 scoped_ptr<ProfileStore> profile_store,
183 scoped_ptr<KeepAliveService> keep_alive_service)
184 : profile_(NULL),
185 weak_factory_(this),
186 command_line_(command_line),
187 local_state_(local_state),
188 profile_store_(profile_store.Pass()),
189 profile_loader_(new ProfileLoader(
190 profile_store_.get(), keep_alive_service.Pass())) {
191 profile_store_->AddProfileObserver(this);
116 } 192 }
117 193
118 AppListServiceImpl::~AppListServiceImpl() {} 194 AppListServiceImpl::~AppListServiceImpl() {}
119 195
120 void AppListServiceImpl::SetAppListNextPaintCallback( 196 void AppListServiceImpl::SetAppListNextPaintCallback(
121 const base::Closure& callback) {} 197 const base::Closure& callback) {}
122 198
123 void AppListServiceImpl::HandleFirstRun() {} 199 void AppListServiceImpl::HandleFirstRun() {}
124 200
125 void AppListServiceImpl::Init(Profile* initial_profile) {} 201 void AppListServiceImpl::Init(Profile* initial_profile) {}
126 202
127 base::FilePath AppListServiceImpl::GetProfilePath( 203 base::FilePath AppListServiceImpl::GetProfilePath(
128 const base::FilePath& user_data_dir) { 204 const base::FilePath& user_data_dir) {
129 PrefService* local_state = g_browser_process->local_state();
130 DCHECK(local_state);
131
132 std::string app_list_profile; 205 std::string app_list_profile;
133 if (local_state->HasPrefPath(prefs::kAppListProfile)) 206 if (local_state_->HasPrefPath(prefs::kAppListProfile))
134 app_list_profile = local_state->GetString(prefs::kAppListProfile); 207 app_list_profile = local_state_->GetString(prefs::kAppListProfile);
135 208
136 // If the user has no profile preference for the app launcher, default to the 209 // If the user has no profile preference for the app launcher, default to the
137 // last browser profile used. 210 // last browser profile used.
138 if (app_list_profile.empty() && 211 if (app_list_profile.empty() &&
139 local_state->HasPrefPath(prefs::kProfileLastUsed)) { 212 local_state_->HasPrefPath(prefs::kProfileLastUsed)) {
140 app_list_profile = local_state->GetString(prefs::kProfileLastUsed); 213 app_list_profile = local_state_->GetString(prefs::kProfileLastUsed);
141 } 214 }
142 215
143 // If there is no last used profile recorded, use the initial profile. 216 // If there is no last used profile recorded, use the initial profile.
144 if (app_list_profile.empty()) 217 if (app_list_profile.empty())
145 app_list_profile = chrome::kInitialProfile; 218 app_list_profile = chrome::kInitialProfile;
146 219
147 return user_data_dir.AppendASCII(app_list_profile); 220 return user_data_dir.AppendASCII(app_list_profile);
148 } 221 }
149 222
150 void AppListServiceImpl::SetProfilePath(const base::FilePath& profile_path) { 223 void AppListServiceImpl::SetProfilePath(const base::FilePath& profile_path) {
151 // Ensure we don't set the pref to a managed user's profile path. 224 // Ensure we don't set the pref to a managed user's profile path.
152 ProfileInfoCache& profile_info = 225 // TODO(calamity): Filter out managed profiles from the settings app so this
153 g_browser_process->profile_manager()->GetProfileInfoCache(); 226 // can't get hit, so we can remove it.
154 size_t profile_index = profile_info.GetIndexOfProfileWithPath(profile_path); 227 if (profile_store_->IsProfileManaged(profile_path))
155 if (profile_info.ProfileIsManagedAtIndex(profile_index))
156 return; 228 return;
157 229
158 g_browser_process->local_state()->SetString( 230 local_state_->SetString(
159 prefs::kAppListProfile, 231 prefs::kAppListProfile,
160 profile_path.BaseName().MaybeAsASCII()); 232 profile_path.BaseName().MaybeAsASCII());
161 } 233 }
162 234
163 void AppListServiceImpl::CreateShortcut() {} 235 void AppListServiceImpl::CreateShortcut() {}
164 236
165 // We need to watch for profile removal to keep kAppListProfile updated. 237 // We need to watch for profile removal to keep kAppListProfile updated.
166 void AppListServiceImpl::OnProfileWillBeRemoved( 238 void AppListServiceImpl::OnProfileWillBeRemoved(
167 const base::FilePath& profile_path) { 239 const base::FilePath& profile_path) {
168 // If the profile the app list uses just got deleted, reset it to the last 240 // If the profile the app list uses just got deleted, reset it to the last
169 // used profile. 241 // used profile.
170 PrefService* local_state = g_browser_process->local_state(); 242 std::string app_list_last_profile = local_state_->GetString(
171 std::string app_list_last_profile = local_state->GetString(
172 prefs::kAppListProfile); 243 prefs::kAppListProfile);
173 if (profile_path.BaseName().MaybeAsASCII() == app_list_last_profile) { 244 if (profile_path.BaseName().MaybeAsASCII() == app_list_last_profile) {
174 local_state->SetString(prefs::kAppListProfile, 245 local_state_->SetString(prefs::kAppListProfile,
175 local_state->GetString(prefs::kProfileLastUsed)); 246 local_state_->GetString(prefs::kProfileLastUsed));
176 } 247 }
177 } 248 }
178 249
179 void AppListServiceImpl::Show() { 250 void AppListServiceImpl::Show() {
180 profile_loader_.LoadProfileInvalidatingOtherLoads( 251 profile_loader_->LoadProfileInvalidatingOtherLoads(
181 GetProfilePath(g_browser_process->profile_manager()->user_data_dir()), 252 GetProfilePath(profile_store_->GetUserDataDir()),
182 base::Bind(&AppListServiceImpl::ShowForProfile, 253 base::Bind(&AppListServiceImpl::ShowForProfile,
183 weak_factory_.GetWeakPtr())); 254 weak_factory_.GetWeakPtr()));
184 } 255 }
185 256
186 void AppListServiceImpl::EnableAppList(Profile* initial_profile) { 257 void AppListServiceImpl::EnableAppList(Profile* initial_profile) {
187 SetProfilePath(initial_profile->GetPath()); 258 SetProfilePath(initial_profile->GetPath());
188 if (HasAppListEnabledPreference()) 259 if (local_state_->GetBoolean(apps::prefs::kAppLauncherHasBeenEnabled))
189 return; 260 return;
190 261
191 SetAppListEnabledPreference(true); 262 local_state_->SetBoolean(apps::prefs::kAppLauncherHasBeenEnabled, true);
192 CreateShortcut(); 263 CreateShortcut();
193 } 264 }
194 265
195 Profile* AppListServiceImpl::GetCurrentAppListProfile() { 266 Profile* AppListServiceImpl::GetCurrentAppListProfile() {
196 return profile(); 267 return profile();
197 } 268 }
198 269
199 void AppListServiceImpl::SetProfile(Profile* new_profile) { 270 void AppListServiceImpl::SetProfile(Profile* new_profile) {
200 profile_ = new_profile; 271 profile_ = new_profile;
201 } 272 }
202 273
203 void AppListServiceImpl::InvalidatePendingProfileLoads() { 274 void AppListServiceImpl::InvalidatePendingProfileLoads() {
204 profile_loader_.InvalidatePendingProfileLoads(); 275 profile_loader_->InvalidatePendingProfileLoads();
205 } 276 }
206 277
207 void AppListServiceImpl::HandleCommandLineFlags(Profile* initial_profile) { 278 void AppListServiceImpl::HandleCommandLineFlags(Profile* initial_profile) {
208 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableAppList)) 279 if (command_line_.HasSwitch(switches::kEnableAppList))
209 EnableAppList(initial_profile); 280 EnableAppList(initial_profile);
210 281
211 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableAppList)) 282 if (command_line_.HasSwitch(switches::kDisableAppList))
212 SetAppListEnabledPreference(false); 283 local_state_->SetBoolean(apps::prefs::kAppLauncherHasBeenEnabled, false);
284 }
213 285
286 void AppListServiceImpl::SendUsageStats() {
214 // Send app list usage stats after a delay. 287 // Send app list usage stats after a delay.
215 const int kSendUsageStatsDelay = 5; 288 const int kSendUsageStatsDelay = 5;
216 base::MessageLoop::current()->PostDelayedTask( 289 base::MessageLoop::current()->PostDelayedTask(
217 FROM_HERE, 290 FROM_HERE,
218 base::Bind(&AppListServiceImpl::SendAppListStats), 291 base::Bind(&AppListServiceImpl::SendAppListStats),
219 base::TimeDelta::FromSeconds(kSendUsageStatsDelay)); 292 base::TimeDelta::FromSeconds(kSendUsageStatsDelay));
220 } 293 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698