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/chromeos/app_mode/kiosk_app_manager.h" | 5 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/prefs/pref_registry_simple.h" | 11 #include "base/prefs/pref_registry_simple.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/chromeos/app_mode/kiosk_app_data.h" | 14 #include "chrome/browser/chromeos/app_mode/kiosk_app_data.h" |
| 15 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager_observer.h" | 15 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager_observer.h" |
| 16 #include "chrome/browser/chromeos/login/user_manager.h" | |
| 16 #include "chrome/browser/chromeos/settings/cros_settings.h" | 17 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 17 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
| 18 #include "chrome/common/chrome_paths.h" | 19 #include "chrome/common/chrome_paths.h" |
| 19 #include "chromeos/cryptohome/async_method_caller.h" | 20 #include "chromeos/cryptohome/async_method_caller.h" |
| 20 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
| 21 | 22 |
| 22 namespace chromeos { | 23 namespace chromeos { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 name(data.name()), | 64 name(data.name()), |
| 64 icon(data.icon()), | 65 icon(data.icon()), |
| 65 is_loading(data.IsLoading()) { | 66 is_loading(data.IsLoading()) { |
| 66 } | 67 } |
| 67 | 68 |
| 68 KioskAppManager::App::App() : is_loading(false) {} | 69 KioskAppManager::App::App() : is_loading(false) {} |
| 69 KioskAppManager::App::~App() {} | 70 KioskAppManager::App::~App() {} |
| 70 | 71 |
| 71 std::string KioskAppManager::GetAutoLaunchApp() const { | 72 std::string KioskAppManager::GetAutoLaunchApp() const { |
| 72 std::string app_id; | 73 std::string app_id; |
| 73 if (CrosSettings::Get()->GetString(kKioskAutoLaunch, &app_id)) | 74 std::string auto_login_id; |
| 75 if (CrosSettings::Get()->GetString(kAccountsPrefDeviceLocalAccountAutoLoginId, | |
| 76 &auto_login_id) && | |
| 77 UserManager::ParseKioskAppUserId(auto_login_id, &app_id)) { | |
| 74 return app_id; | 78 return app_id; |
| 79 } | |
| 75 | 80 |
| 76 return std::string(); | 81 return std::string(); |
| 77 } | 82 } |
| 78 | 83 |
| 79 void KioskAppManager::SetAutoLaunchApp(const std::string& app_id) { | 84 void KioskAppManager::SetAutoLaunchApp(const std::string& app_id) { |
| 80 CrosSettings::Get()->SetString(kKioskAutoLaunch, app_id); | 85 CrosSettings::Get()->SetString(kAccountsPrefDeviceLocalAccountAutoLoginId, |
| 86 UserManager::FormatKioskAppUserId(app_id)); | |
| 87 CrosSettings::Get()->SetInteger(kAccountsPrefDeviceLocalAccountAutoLoginId, | |
|
xiyuan
2013/04/23 06:36:56
Is this kAccountsPrefDeviceLocalAccountAutoLoginDe
Mattias Nissler (ping if slow)
2013/04/23 14:01:01
Yes :)
| |
| 88 0); | |
| 81 } | 89 } |
| 82 | 90 |
| 83 void KioskAppManager::AddApp(const std::string& app_id) { | 91 void KioskAppManager::AddApp(const std::string& app_id) { |
| 84 base::StringValue value(app_id); | 92 base::StringValue value(UserManager::FormatKioskAppUserId(app_id)); |
| 85 CrosSettings::Get()->AppendToList(kKioskApps, &value); | 93 CrosSettings::Get()->AppendToList(kAccountsPrefDeviceLocalAccounts, &value); |
| 86 } | 94 } |
| 87 | 95 |
| 88 void KioskAppManager::RemoveApp(const std::string& app_id) { | 96 void KioskAppManager::RemoveApp(const std::string& app_id) { |
| 89 base::StringValue value(app_id); | 97 base::StringValue value(UserManager::FormatKioskAppUserId(app_id)); |
| 90 CrosSettings::Get()->RemoveFromList(kKioskApps, &value); | 98 CrosSettings::Get()->RemoveFromList(kAccountsPrefDeviceLocalAccounts, &value); |
| 91 } | 99 } |
| 92 | 100 |
| 93 void KioskAppManager::GetApps(Apps* apps) const { | 101 void KioskAppManager::GetApps(Apps* apps) const { |
| 94 apps->reserve(apps_.size()); | 102 apps->reserve(apps_.size()); |
| 95 for (size_t i = 0; i < apps_.size(); ++i) | 103 for (size_t i = 0; i < apps_.size(); ++i) |
| 96 apps->push_back(App(*apps_[i])); | 104 apps->push_back(App(*apps_[i])); |
| 97 } | 105 } |
| 98 | 106 |
| 99 bool KioskAppManager::GetApp(const std::string& app_id, App* app) const { | 107 bool KioskAppManager::GetApp(const std::string& app_id, App* app) const { |
| 100 const KioskAppData* data = GetAppData(app_id); | 108 const KioskAppData* data = GetAppData(app_id); |
| 101 if (!data) | 109 if (!data) |
| 102 return false; | 110 return false; |
| 103 | 111 |
| 104 *app = App(*data); | 112 *app = App(*data); |
| 105 return true; | 113 return true; |
| 106 } | 114 } |
| 107 | 115 |
| 108 const base::RefCountedString* KioskAppManager::GetAppRawIcon( | 116 const base::RefCountedString* KioskAppManager::GetAppRawIcon( |
| 109 const std::string& app_id) const { | 117 const std::string& app_id) const { |
| 110 const KioskAppData* data = GetAppData(app_id); | 118 const KioskAppData* data = GetAppData(app_id); |
| 111 if (!data) | 119 if (!data) |
| 112 return NULL; | 120 return NULL; |
| 113 | 121 |
| 114 return data->raw_icon(); | 122 return data->raw_icon(); |
| 115 } | 123 } |
| 116 | 124 |
| 117 bool KioskAppManager::GetDisableBailoutShortcut() const { | 125 bool KioskAppManager::GetDisableBailoutShortcut() const { |
| 118 bool disable; | 126 bool enable; |
| 119 if (CrosSettings::Get()->GetBoolean(kKioskDisableBailoutShortcut, &disable)) | 127 if (CrosSettings::Get()->GetBoolean( |
| 120 return disable; | 128 kAccountsPrefDeviceLocalAccountAutoLoginBailoutEnabled, &enable)) { |
| 129 return !enable; | |
| 130 } | |
| 121 | 131 |
| 122 return false; | 132 return false; |
| 123 } | 133 } |
| 124 | 134 |
| 125 void KioskAppManager::AddObserver(KioskAppManagerObserver* observer) { | 135 void KioskAppManager::AddObserver(KioskAppManagerObserver* observer) { |
| 126 observers_.AddObserver(observer); | 136 observers_.AddObserver(observer); |
| 127 } | 137 } |
| 128 | 138 |
| 129 void KioskAppManager::RemoveObserver(KioskAppManagerObserver* observer) { | 139 void KioskAppManager::RemoveObserver(KioskAppManagerObserver* observer) { |
| 130 observers_.RemoveObserver(observer); | 140 observers_.RemoveObserver(observer); |
| 131 } | 141 } |
| 132 | 142 |
| 133 KioskAppManager::KioskAppManager() { | 143 KioskAppManager::KioskAppManager() { |
| 134 UpdateAppData(); | 144 UpdateAppData(); |
| 135 CrosSettings::Get()->AddSettingsObserver(kKioskApps, this); | 145 CrosSettings::Get()->AddSettingsObserver( |
| 146 kAccountsPrefDeviceLocalAccounts, this); | |
| 147 CrosSettings::Get()->AddSettingsObserver( | |
| 148 kAccountsPrefDeviceLocalAccountAutoLoginId, this); | |
| 149 CrosSettings::Get()->AddSettingsObserver( | |
| 150 kAccountsPrefDeviceLocalAccountAutoLoginDelay, this); | |
| 136 } | 151 } |
| 137 | 152 |
| 138 KioskAppManager::~KioskAppManager() {} | 153 KioskAppManager::~KioskAppManager() {} |
| 139 | 154 |
| 140 void KioskAppManager::CleanUp() { | 155 void KioskAppManager::CleanUp() { |
| 141 CrosSettings::Get()->RemoveSettingsObserver(kKioskApps, this); | 156 CrosSettings::Get()->RemoveSettingsObserver( |
| 157 kAccountsPrefDeviceLocalAccounts, this); | |
| 158 CrosSettings::Get()->RemoveSettingsObserver( | |
| 159 kAccountsPrefDeviceLocalAccountAutoLoginId, this); | |
| 160 CrosSettings::Get()->RemoveSettingsObserver( | |
| 161 kAccountsPrefDeviceLocalAccountAutoLoginDelay, this); | |
| 142 apps_.clear(); | 162 apps_.clear(); |
| 143 } | 163 } |
| 144 | 164 |
| 145 const KioskAppData* KioskAppManager::GetAppData( | 165 const KioskAppData* KioskAppManager::GetAppData( |
| 146 const std::string& app_id) const { | 166 const std::string& app_id) const { |
| 147 for (size_t i = 0; i < apps_.size(); ++i) { | 167 for (size_t i = 0; i < apps_.size(); ++i) { |
| 148 const KioskAppData* data = apps_[i]; | 168 const KioskAppData* data = apps_[i]; |
| 149 if (data->id() == app_id) | 169 if (data->id() == app_id) |
| 150 return data; | 170 return data; |
| 151 } | 171 } |
| 152 | 172 |
| 153 return NULL; | 173 return NULL; |
| 154 } | 174 } |
| 155 | 175 |
| 156 void KioskAppManager::UpdateAppData() { | 176 void KioskAppManager::UpdateAppData() { |
| 157 // Gets app id to data mapping for existing apps. | 177 // Gets app id to data mapping for existing apps. |
| 158 std::map<std::string, KioskAppData*> old_apps; | 178 std::map<std::string, KioskAppData*> old_apps; |
| 159 for (size_t i = 0; i < apps_.size(); ++i) | 179 for (size_t i = 0; i < apps_.size(); ++i) |
| 160 old_apps[apps_[i]->id()] = apps_[i]; | 180 old_apps[apps_[i]->id()] = apps_[i]; |
| 161 apps_.weak_clear(); // |old_apps| takes ownership | 181 apps_.weak_clear(); // |old_apps| takes ownership |
| 162 | 182 |
| 163 const base::ListValue* new_apps; | 183 const base::ListValue* local_accounts; |
| 164 CHECK(CrosSettings::Get()->GetList(kKioskApps, &new_apps)); | 184 if (CrosSettings::Get()->GetList(kAccountsPrefDeviceLocalAccounts, |
| 185 &local_accounts)) { | |
| 186 // Re-populates |apps_| and reuses existing KioskAppData when possible. | |
| 187 for (base::ListValue::const_iterator account = local_accounts->begin(); | |
| 188 account != local_accounts->end(); | |
| 189 ++account) { | |
| 190 std::string user_id; | |
| 191 if (!(*account)->GetAsString(&user_id)) { | |
| 192 NOTREACHED(); | |
| 193 continue; | |
| 194 } | |
| 165 | 195 |
| 166 // Re-populates |apps_| and reuses existing KioskAppData when possible. | 196 std::string kiosk_app_id; |
| 167 for (base::ListValue::const_iterator new_it = new_apps->begin(); | 197 if (!UserManager::ParseKioskAppUserId(user_id, &kiosk_app_id)) { |
| 168 new_it != new_apps->end(); | 198 // Not a kiosk app. |
| 169 ++new_it) { | 199 continue; |
| 170 std::string id; | 200 } |
| 171 CHECK((*new_it)->GetAsString(&id)); | |
| 172 | 201 |
| 173 std::map<std::string, KioskAppData*>::iterator old_it = old_apps.find(id); | 202 |
| 174 if (old_it != old_apps.end()) { | 203 std::map<std::string, KioskAppData*>::iterator old_it = |
| 175 apps_.push_back(old_it->second); | 204 old_apps.find(kiosk_app_id); |
| 176 old_apps.erase(old_it); | 205 if (old_it != old_apps.end()) { |
| 177 } else { | 206 apps_.push_back(old_it->second); |
| 178 KioskAppData* new_app = new KioskAppData(this, id); | 207 old_apps.erase(old_it); |
| 179 apps_.push_back(new_app); // Takes ownership of |new_app|. | 208 } else { |
| 180 new_app->Load(); | 209 KioskAppData* new_app = new KioskAppData(this, kiosk_app_id); |
| 210 apps_.push_back(new_app); // Takes ownership of |new_app|. | |
| 211 new_app->Load(); | |
| 212 } | |
| 181 } | 213 } |
| 182 } | 214 } |
| 183 | 215 |
| 184 // Clears cache and deletes the remaining old data. | 216 // Clears cache and deletes the remaining old data. |
| 185 for (std::map<std::string, KioskAppData*>::iterator it = old_apps.begin(); | 217 for (std::map<std::string, KioskAppData*>::iterator it = old_apps.begin(); |
| 186 it != old_apps.end(); ++it) { | 218 it != old_apps.end(); ++it) { |
| 187 it->second->ClearCache(); | 219 it->second->ClearCache(); |
| 188 cryptohome::AsyncMethodCaller::GetInstance()->AsyncRemove( | 220 cryptohome::AsyncMethodCaller::GetInstance()->AsyncRemove( |
| 189 it->first, base::Bind(&OnRemoveAppCryptohomeComplete, it->first)); | 221 it->first, base::Bind(&OnRemoveAppCryptohomeComplete, it->first)); |
| 190 } | 222 } |
| 191 STLDeleteValues(&old_apps); | 223 STLDeleteValues(&old_apps); |
| 224 | |
| 225 FOR_EACH_OBSERVER(KioskAppManagerObserver, observers_, | |
| 226 OnKioskAppsListChanged()); | |
| 192 } | 227 } |
| 193 | 228 |
| 194 void KioskAppManager::Observe(int type, | 229 void KioskAppManager::Observe(int type, |
| 195 const content::NotificationSource& source, | 230 const content::NotificationSource& source, |
| 196 const content::NotificationDetails& details) { | 231 const content::NotificationDetails& details) { |
| 197 DCHECK_EQ(chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED, type); | 232 DCHECK_EQ(chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED, type); |
| 198 DCHECK_EQ(kKioskApps, | |
| 199 *content::Details<const std::string>(details).ptr()); | |
| 200 | |
| 201 UpdateAppData(); | 233 UpdateAppData(); |
| 202 } | 234 } |
| 203 | 235 |
| 204 void KioskAppManager::GetKioskAppIconCacheDir(base::FilePath* cache_dir) { | 236 void KioskAppManager::GetKioskAppIconCacheDir(base::FilePath* cache_dir) { |
| 205 base::FilePath user_data_dir; | 237 base::FilePath user_data_dir; |
| 206 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)); | 238 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)); |
| 207 *cache_dir = user_data_dir.AppendASCII(kIconCacheDir); | 239 *cache_dir = user_data_dir.AppendASCII(kIconCacheDir); |
| 208 } | 240 } |
| 209 | 241 |
| 210 void KioskAppManager::OnKioskAppDataChanged(const std::string& app_id) { | 242 void KioskAppManager::OnKioskAppDataChanged(const std::string& app_id) { |
| 211 FOR_EACH_OBSERVER(KioskAppManagerObserver, | 243 FOR_EACH_OBSERVER(KioskAppManagerObserver, |
| 212 observers_, | 244 observers_, |
| 213 OnKioskAppDataChanged(app_id)); | 245 OnKioskAppDataChanged(app_id)); |
| 214 } | 246 } |
| 215 | 247 |
| 216 void KioskAppManager::OnKioskAppDataLoadFailure(const std::string& app_id) { | 248 void KioskAppManager::OnKioskAppDataLoadFailure(const std::string& app_id) { |
| 217 FOR_EACH_OBSERVER(KioskAppManagerObserver, | 249 FOR_EACH_OBSERVER(KioskAppManagerObserver, |
| 218 observers_, | 250 observers_, |
| 219 OnKioskAppDataLoadFailure(app_id)); | 251 OnKioskAppDataLoadFailure(app_id)); |
| 220 RemoveApp(app_id); | 252 RemoveApp(app_id); |
| 221 } | 253 } |
| 222 | 254 |
| 223 } // namespace chromeos | 255 } // namespace chromeos |
| OLD | NEW |