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

Unified 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, 3 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/app_list/app_list_service_impl.cc
diff --git a/chrome/browser/ui/app_list/app_list_service_impl.cc b/chrome/browser/ui/app_list/app_list_service_impl.cc
index a559132ee8711485e7dba4b86e68662bfbd5aeb9..006138fd5a42d4ae968088c0b95f813aae6b9102 100644
--- a/chrome/browser/ui/app_list/app_list_service_impl.cc
+++ b/chrome/browser/ui/app_list/app_list_service_impl.cc
@@ -4,12 +4,20 @@
#include "chrome/browser/ui/app_list/app_list_service_impl.h"
+#include <string>
+
+#include "apps/pref_names.h"
#include "base/command_line.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_service.h"
+#include "base/strings/string16.h"
#include "base/time/time.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_manager.h"
+#include "chrome/browser/ui/app_list/keep_alive_service.h"
+#include "chrome/browser/ui/app_list/keep_alive_service_impl.h"
+#include "chrome/browser/ui/app_list/profile_loader.h"
+#include "chrome/browser/ui/app_list/profile_store.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
@@ -65,15 +73,68 @@ void RecordDailyEventFrequency(
}
}
-bool HasAppListEnabledPreference() {
- PrefService* local_state = g_browser_process->local_state();
- return local_state->GetBoolean(prefs::kAppLauncherHasBeenEnabled);
-}
+class ProfileStoreImpl : public ProfileStore {
+ public:
+ explicit ProfileStoreImpl(ProfileManager* profile_manager)
+ : profile_manager_(profile_manager),
+ weak_factory_(this) {
+ }
-void SetAppListEnabledPreference(bool enabled) {
- PrefService* local_state = g_browser_process->local_state();
- local_state->SetBoolean(prefs::kAppLauncherHasBeenEnabled, enabled);
-}
+ virtual void AddProfileObserver(ProfileInfoCacheObserver* observer) OVERRIDE {
+ profile_manager_->GetProfileInfoCache().AddObserver(observer);
+ }
+
+ virtual void LoadProfileAsync(
+ const base::FilePath& path,
+ base::Callback<void(Profile*)> callback) OVERRIDE {
+ profile_manager_->CreateProfileAsync(
+ path,
+ base::Bind(&ProfileStoreImpl::OnProfileCreated,
+ weak_factory_.GetWeakPtr(),
+ callback),
+ base::string16(),
+ base::string16(),
+ std::string());
+ }
+
+ void OnProfileCreated(base::Callback<void(Profile*)> callback,
+ Profile* profile,
+ Profile::CreateStatus status) {
+ switch (status) {
+ case Profile::CREATE_STATUS_CREATED:
+ break;
+ case Profile::CREATE_STATUS_INITIALIZED:
+ callback.Run(profile);
+ break;
+ case Profile::CREATE_STATUS_LOCAL_FAIL:
+ case Profile::CREATE_STATUS_REMOTE_FAIL:
+ case Profile::CREATE_STATUS_CANCELED:
+ break;
+ case Profile::MAX_CREATE_STATUS:
+ NOTREACHED();
+ break;
+ }
+ }
+
+ virtual Profile* GetProfileByPath(const base::FilePath& path) OVERRIDE {
+ return profile_manager_->GetProfileByPath(path);
+ }
+
+ virtual base::FilePath GetUserDataDir() OVERRIDE {
+ return profile_manager_->user_data_dir();
+ }
+
+ virtual bool IsProfileManaged(const base::FilePath& profile_path) OVERRIDE {
+ ProfileInfoCache& profile_info =
+ g_browser_process->profile_manager()->GetProfileInfoCache();
+ size_t profile_index = profile_info.GetIndexOfProfileWithPath(profile_path);
+ return profile_info.ProfileIsManagedAtIndex(profile_index);
+ }
+
+ private:
+ ProfileManager* profile_manager_;
+ base::WeakPtrFactory<ProfileStoreImpl> weak_factory_;
+};
} // namespace
@@ -106,12 +167,30 @@ void AppListServiceImpl::SendAppListStats() {
AppListServiceImpl::AppListServiceImpl()
: profile_(NULL),
- profile_load_sequence_id_(0),
- pending_profile_loads_(0),
+ profile_store_(new ProfileStoreImpl(
+ g_browser_process->profile_manager())),
weak_factory_(this),
- profile_loader_(g_browser_process->profile_manager()) {
- ProfileManager* profile_manager = g_browser_process->profile_manager();
- profile_manager->GetProfileInfoCache().AddObserver(this);
+ command_line_(*CommandLine::ForCurrentProcess()),
+ local_state_(g_browser_process->local_state()),
+ profile_loader_(new ProfileLoader(
+ profile_store_.get(),
+ scoped_ptr<KeepAliveService>(new KeepAliveServiceImpl))) {
+ profile_store_->AddProfileObserver(this);
+}
+
+AppListServiceImpl::AppListServiceImpl(
+ const CommandLine& command_line,
+ PrefService* local_state,
+ scoped_ptr<ProfileStore> profile_store,
+ scoped_ptr<KeepAliveService> keep_alive_service)
+ : profile_(NULL),
+ profile_store_(profile_store.Pass()),
+ weak_factory_(this),
+ command_line_(command_line),
+ local_state_(local_state),
+ profile_loader_(new ProfileLoader(
+ profile_store_.get(), keep_alive_service.Pass())) {
+ profile_store_->AddProfileObserver(this);
}
AppListServiceImpl::~AppListServiceImpl() {}
@@ -125,18 +204,15 @@ void AppListServiceImpl::Init(Profile* initial_profile) {}
base::FilePath AppListServiceImpl::GetProfilePath(
const base::FilePath& user_data_dir) {
- PrefService* local_state = g_browser_process->local_state();
- DCHECK(local_state);
-
std::string app_list_profile;
- if (local_state->HasPrefPath(prefs::kAppListProfile))
- app_list_profile = local_state->GetString(prefs::kAppListProfile);
+ if (local_state_->HasPrefPath(prefs::kAppListProfile))
+ app_list_profile = local_state_->GetString(prefs::kAppListProfile);
// If the user has no profile preference for the app launcher, default to the
// last browser profile used.
if (app_list_profile.empty() &&
- local_state->HasPrefPath(prefs::kProfileLastUsed)) {
- app_list_profile = local_state->GetString(prefs::kProfileLastUsed);
+ local_state_->HasPrefPath(prefs::kProfileLastUsed)) {
+ app_list_profile = local_state_->GetString(prefs::kProfileLastUsed);
}
// If there is no last used profile recorded, use the initial profile.
@@ -148,13 +224,12 @@ base::FilePath AppListServiceImpl::GetProfilePath(
void AppListServiceImpl::SetProfilePath(const base::FilePath& profile_path) {
// Ensure we don't set the pref to a managed user's profile path.
- ProfileInfoCache& profile_info =
- g_browser_process->profile_manager()->GetProfileInfoCache();
- size_t profile_index = profile_info.GetIndexOfProfileWithPath(profile_path);
- if (profile_info.ProfileIsManagedAtIndex(profile_index))
+ // TODO(calamity): Filter out managed profiles from the settings app so this
+ // can't get hit, so we can remove it.
+ if (profile_store_->IsProfileManaged(profile_path))
return;
- g_browser_process->local_state()->SetString(
+ local_state_->SetString(
prefs::kAppListProfile,
profile_path.BaseName().MaybeAsASCII());
}
@@ -166,28 +241,27 @@ void AppListServiceImpl::OnProfileWillBeRemoved(
const base::FilePath& profile_path) {
// If the profile the app list uses just got deleted, reset it to the last
// used profile.
- PrefService* local_state = g_browser_process->local_state();
- std::string app_list_last_profile = local_state->GetString(
+ std::string app_list_last_profile = local_state_->GetString(
prefs::kAppListProfile);
if (profile_path.BaseName().MaybeAsASCII() == app_list_last_profile) {
- local_state->SetString(prefs::kAppListProfile,
- local_state->GetString(prefs::kProfileLastUsed));
+ local_state_->SetString(prefs::kAppListProfile,
+ local_state_->GetString(prefs::kProfileLastUsed));
}
}
void AppListServiceImpl::Show() {
- profile_loader_.LoadProfileInvalidatingOtherLoads(
- GetProfilePath(g_browser_process->profile_manager()->user_data_dir()),
+ profile_loader_->LoadProfileInvalidatingOtherLoads(
+ GetProfilePath(profile_store_->GetUserDataDir()),
base::Bind(&AppListServiceImpl::ShowForProfile,
weak_factory_.GetWeakPtr()));
}
void AppListServiceImpl::EnableAppList(Profile* initial_profile) {
SetProfilePath(initial_profile->GetPath());
- if (HasAppListEnabledPreference())
+ if (local_state_->GetBoolean(prefs::kAppLauncherHasBeenEnabled))
return;
- SetAppListEnabledPreference(true);
+ local_state_->SetBoolean(prefs::kAppLauncherHasBeenEnabled, true);
CreateShortcut();
}
@@ -200,16 +274,18 @@ void AppListServiceImpl::SetProfile(Profile* new_profile) {
}
void AppListServiceImpl::InvalidatePendingProfileLoads() {
- profile_loader_.InvalidatePendingProfileLoads();
+ profile_loader_->InvalidatePendingProfileLoads();
}
void AppListServiceImpl::HandleCommandLineFlags(Profile* initial_profile) {
- if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableAppList))
+ if (command_line_.HasSwitch(switches::kEnableAppList))
EnableAppList(initial_profile);
- if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableAppList))
- SetAppListEnabledPreference(false);
+ if (command_line_.HasSwitch(switches::kDisableAppList))
+ local_state_->SetBoolean(prefs::kAppLauncherHasBeenEnabled, false);
+}
+void AppListServiceImpl::SendUsageStats() {
// Send app list usage stats after a delay.
const int kSendUsageStatsDelay = 5;
base::MessageLoop::current()->PostDelayedTask(
« 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