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

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

Powered by Google App Engine
This is Rietveld 408576698