Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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: | |
| 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 private: | |
| 128 ProfileManager* profile_manager_; | |
| 129 base::WeakPtrFactory<ProfileStoreImpl> weak_factory_; | |
| 130 }; | |
| 78 | 131 |
| 79 } // namespace | 132 } // namespace |
| 80 | 133 |
| 81 // static | 134 // static |
| 82 void AppListServiceImpl::RecordAppListLaunch() { | 135 void AppListServiceImpl::RecordAppListLaunch() { |
| 83 RecordDailyEventFrequency(prefs::kLastAppListLaunchPing, | 136 RecordDailyEventFrequency(prefs::kLastAppListLaunchPing, |
| 84 prefs::kAppListLaunchCount, | 137 prefs::kAppListLaunchCount, |
| 85 &SendAppListLaunch); | 138 &SendAppListLaunch); |
| 86 } | 139 } |
| 87 | 140 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 100 SendDailyEventFrequency(prefs::kLastAppListLaunchPing, | 153 SendDailyEventFrequency(prefs::kLastAppListLaunchPing, |
| 101 prefs::kAppListLaunchCount, | 154 prefs::kAppListLaunchCount, |
| 102 &SendAppListLaunch); | 155 &SendAppListLaunch); |
| 103 SendDailyEventFrequency(prefs::kLastAppListAppLaunchPing, | 156 SendDailyEventFrequency(prefs::kLastAppListAppLaunchPing, |
| 104 prefs::kAppListAppLaunchCount, | 157 prefs::kAppListAppLaunchCount, |
| 105 &SendAppListAppLaunch); | 158 &SendAppListAppLaunch); |
| 106 } | 159 } |
| 107 | 160 |
| 108 AppListServiceImpl::AppListServiceImpl() | 161 AppListServiceImpl::AppListServiceImpl() |
| 109 : profile_(NULL), | 162 : profile_(NULL), |
| 110 profile_load_sequence_id_(0), | |
| 111 pending_profile_loads_(0), | |
| 112 weak_factory_(this), | 163 weak_factory_(this), |
| 113 profile_loader_(g_browser_process->profile_manager()) { | 164 local_state_(g_browser_process->local_state()), |
| 114 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 165 profile_store_(new ProfileStoreImpl( |
| 115 profile_manager->GetProfileInfoCache().AddObserver(this); | 166 g_browser_process->profile_manager())), |
| 167 profile_loader_(new ProfileLoader( | |
| 168 profile_store_.get(), make_scoped_ptr(new KeepAliveServiceImpl))) { | |
| 169 profile_store_->AddProfileObserver(this); | |
| 170 } | |
| 171 | |
| 172 AppListServiceImpl::AppListServiceImpl( | |
| 173 PrefService* local_state, | |
| 174 scoped_ptr<ProfileStore> profile_store, | |
| 175 scoped_ptr<KeepAliveService> keep_alive_service) | |
| 176 : profile_(NULL), | |
| 177 weak_factory_(this), | |
| 178 local_state_(local_state), | |
| 179 profile_store_(profile_store.Pass()), | |
| 180 profile_loader_(new ProfileLoader( | |
| 181 profile_store_.get(), keep_alive_service.Pass())) { | |
| 182 profile_store_->AddProfileObserver(this); | |
| 116 } | 183 } |
| 117 | 184 |
| 118 AppListServiceImpl::~AppListServiceImpl() {} | 185 AppListServiceImpl::~AppListServiceImpl() {} |
| 119 | 186 |
| 120 void AppListServiceImpl::SetAppListNextPaintCallback( | 187 void AppListServiceImpl::SetAppListNextPaintCallback( |
| 121 const base::Closure& callback) {} | 188 const base::Closure& callback) {} |
| 122 | 189 |
| 123 void AppListServiceImpl::HandleFirstRun() {} | 190 void AppListServiceImpl::HandleFirstRun() {} |
| 124 | 191 |
| 125 void AppListServiceImpl::Init(Profile* initial_profile) {} | 192 void AppListServiceImpl::Init(Profile* initial_profile) {} |
| 126 | 193 |
| 127 base::FilePath AppListServiceImpl::GetProfilePath( | 194 base::FilePath AppListServiceImpl::GetProfilePath( |
| 128 const base::FilePath& user_data_dir) { | 195 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; | 196 std::string app_list_profile; |
| 133 if (local_state->HasPrefPath(prefs::kAppListProfile)) | 197 if (local_state_->HasPrefPath(prefs::kAppListProfile)) |
| 134 app_list_profile = local_state->GetString(prefs::kAppListProfile); | 198 app_list_profile = local_state_->GetString(prefs::kAppListProfile); |
| 135 | 199 |
| 136 // If the user has no profile preference for the app launcher, default to the | 200 // If the user has no profile preference for the app launcher, default to the |
| 137 // last browser profile used. | 201 // last browser profile used. |
| 138 if (app_list_profile.empty() && | 202 if (app_list_profile.empty() && |
| 139 local_state->HasPrefPath(prefs::kProfileLastUsed)) { | 203 local_state_->HasPrefPath(prefs::kProfileLastUsed)) { |
| 140 app_list_profile = local_state->GetString(prefs::kProfileLastUsed); | 204 app_list_profile = local_state_->GetString(prefs::kProfileLastUsed); |
| 141 } | 205 } |
| 142 | 206 |
| 143 // If there is no last used profile recorded, use the initial profile. | 207 // If there is no last used profile recorded, use the initial profile. |
| 144 if (app_list_profile.empty()) | 208 if (app_list_profile.empty()) |
| 145 app_list_profile = chrome::kInitialProfile; | 209 app_list_profile = chrome::kInitialProfile; |
| 146 | 210 |
| 147 return user_data_dir.AppendASCII(app_list_profile); | 211 return user_data_dir.AppendASCII(app_list_profile); |
| 148 } | 212 } |
| 149 | 213 |
| 150 void AppListServiceImpl::SetProfilePath(const base::FilePath& profile_path) { | 214 void AppListServiceImpl::SetProfilePath(const base::FilePath& profile_path) { |
| 151 // Ensure we don't set the pref to a managed user's profile path. | 215 local_state_->SetString( |
|
koz (OOO until 15th September)
2013/09/26 03:12:01
Note: this omission is intentional, as per my comm
koz (OOO until 15th September)
2013/09/26 05:47:20
Never mind that - as per the discussion I've re-ad
| |
| 152 ProfileInfoCache& profile_info = | |
| 153 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 154 size_t profile_index = profile_info.GetIndexOfProfileWithPath(profile_path); | |
| 155 if (profile_info.ProfileIsManagedAtIndex(profile_index)) | |
| 156 return; | |
| 157 | |
| 158 g_browser_process->local_state()->SetString( | |
| 159 prefs::kAppListProfile, | 216 prefs::kAppListProfile, |
| 160 profile_path.BaseName().MaybeAsASCII()); | 217 profile_path.BaseName().MaybeAsASCII()); |
| 161 } | 218 } |
| 162 | 219 |
| 163 void AppListServiceImpl::CreateShortcut() {} | 220 void AppListServiceImpl::CreateShortcut() {} |
| 164 | 221 |
| 165 // We need to watch for profile removal to keep kAppListProfile updated. | 222 // We need to watch for profile removal to keep kAppListProfile updated. |
| 166 void AppListServiceImpl::OnProfileWillBeRemoved( | 223 void AppListServiceImpl::OnProfileWillBeRemoved( |
| 167 const base::FilePath& profile_path) { | 224 const base::FilePath& profile_path) { |
| 168 // If the profile the app list uses just got deleted, reset it to the last | 225 // If the profile the app list uses just got deleted, reset it to the last |
| 169 // used profile. | 226 // used profile. |
| 170 PrefService* local_state = g_browser_process->local_state(); | 227 std::string app_list_last_profile = local_state_->GetString( |
| 171 std::string app_list_last_profile = local_state->GetString( | |
| 172 prefs::kAppListProfile); | 228 prefs::kAppListProfile); |
| 173 if (profile_path.BaseName().MaybeAsASCII() == app_list_last_profile) { | 229 if (profile_path.BaseName().MaybeAsASCII() == app_list_last_profile) { |
| 174 local_state->SetString(prefs::kAppListProfile, | 230 local_state_->SetString(prefs::kAppListProfile, |
| 175 local_state->GetString(prefs::kProfileLastUsed)); | 231 local_state_->GetString(prefs::kProfileLastUsed)); |
| 176 } | 232 } |
| 177 } | 233 } |
| 178 | 234 |
| 179 void AppListServiceImpl::Show() { | 235 void AppListServiceImpl::Show() { |
| 180 profile_loader_.LoadProfileInvalidatingOtherLoads( | 236 profile_loader_->LoadProfileInvalidatingOtherLoads( |
| 181 GetProfilePath(g_browser_process->profile_manager()->user_data_dir()), | 237 GetProfilePath(profile_store_->GetUserDataDir()), |
| 182 base::Bind(&AppListServiceImpl::ShowForProfile, | 238 base::Bind(&AppListServiceImpl::ShowForProfile, |
| 183 weak_factory_.GetWeakPtr())); | 239 weak_factory_.GetWeakPtr())); |
| 184 } | 240 } |
| 185 | 241 |
| 186 void AppListServiceImpl::EnableAppList(Profile* initial_profile) { | 242 void AppListServiceImpl::EnableAppList(Profile* initial_profile) { |
| 187 SetProfilePath(initial_profile->GetPath()); | 243 SetProfilePath(initial_profile->GetPath()); |
| 188 if (HasAppListEnabledPreference()) | 244 if (local_state_->GetBoolean(apps::prefs::kAppLauncherHasBeenEnabled)) |
| 189 return; | 245 return; |
| 190 | 246 |
| 191 SetAppListEnabledPreference(true); | 247 local_state_->SetBoolean(apps::prefs::kAppLauncherHasBeenEnabled, true); |
| 192 CreateShortcut(); | 248 CreateShortcut(); |
| 193 } | 249 } |
| 194 | 250 |
| 195 Profile* AppListServiceImpl::GetCurrentAppListProfile() { | 251 Profile* AppListServiceImpl::GetCurrentAppListProfile() { |
| 196 return profile(); | 252 return profile(); |
| 197 } | 253 } |
| 198 | 254 |
| 199 void AppListServiceImpl::SetProfile(Profile* new_profile) { | 255 void AppListServiceImpl::SetProfile(Profile* new_profile) { |
| 200 profile_ = new_profile; | 256 profile_ = new_profile; |
| 201 } | 257 } |
| 202 | 258 |
| 203 void AppListServiceImpl::InvalidatePendingProfileLoads() { | 259 void AppListServiceImpl::InvalidatePendingProfileLoads() { |
| 204 profile_loader_.InvalidatePendingProfileLoads(); | 260 profile_loader_->InvalidatePendingProfileLoads(); |
| 205 } | 261 } |
| 206 | 262 |
| 207 void AppListServiceImpl::HandleCommandLineFlags(Profile* initial_profile) { | 263 void AppListServiceImpl::HandleCommandLineFlags(Profile* initial_profile) { |
| 208 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableAppList)) | 264 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableAppList)) |
| 209 EnableAppList(initial_profile); | 265 EnableAppList(initial_profile); |
| 210 | 266 |
| 211 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableAppList)) | 267 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableAppList)) |
| 212 SetAppListEnabledPreference(false); | 268 local_state_->SetBoolean(apps::prefs::kAppLauncherHasBeenEnabled, false); |
| 213 | 269 |
| 214 // Send app list usage stats after a delay. | 270 // Send app list usage stats after a delay. |
| 215 const int kSendUsageStatsDelay = 5; | 271 const int kSendUsageStatsDelay = 5; |
| 216 base::MessageLoop::current()->PostDelayedTask( | 272 base::MessageLoop::current()->PostDelayedTask( |
| 217 FROM_HERE, | 273 FROM_HERE, |
| 218 base::Bind(&AppListServiceImpl::SendAppListStats), | 274 base::Bind(&AppListServiceImpl::SendAppListStats), |
| 219 base::TimeDelta::FromSeconds(kSendUsageStatsDelay)); | 275 base::TimeDelta::FromSeconds(kSendUsageStatsDelay)); |
| 220 } | 276 } |
| OLD | NEW |