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

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

Powered by Google App Engine
This is Rietveld 408576698