OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/login/wallpaper_manager.h" | 5 #include "chrome/browser/chromeos/login/wallpaper_manager.h" |
6 | 6 |
7 #include "ash/desktop_background/desktop_background_controller.h" | 7 #include "ash/desktop_background/desktop_background_controller.h" |
8 #include "ash/desktop_background/desktop_background_resources.h" | |
9 #include "ash/shell.h" | 8 #include "ash/shell.h" |
9 #include "base/command_line.h" | |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/file_path.h" | |
12 #include "base/file_util.h" | |
13 #include "base/path_service.h" | |
11 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
15 #include "base/string_util.h" | |
16 #include "base/stringprintf.h" | |
12 #include "base/time.h" | 17 #include "base/time.h" |
13 #include "base/values.h" | 18 #include "base/values.h" |
14 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
15 #include "chrome/browser/chromeos/login/user_manager.h" | 20 #include "chrome/browser/chromeos/login/user_manager.h" |
16 #include "chrome/browser/chromeos/login/user_manager_impl.h" | 21 #include "chrome/browser/chromeos/login/wizard_controller.h" |
17 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
18 #include "chrome/browser/chromeos/settings/cros_settings.h" | 22 #include "chrome/browser/chromeos/settings/cros_settings.h" |
19 #include "chrome/browser/prefs/pref_service.h" | 23 #include "chrome/browser/prefs/pref_service.h" |
20 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 24 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
25 #include "chrome/common/chrome_notification_types.h" | |
26 #include "chrome/common/chrome_paths.h" | |
27 #include "chrome/common/chrome_switches.h" | |
21 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
22 #include "chromeos/dbus/dbus_thread_manager.h" | 29 #include "chromeos/dbus/dbus_thread_manager.h" |
23 #include "chromeos/dbus/power_manager_client.h" | 30 #include "chromeos/dbus/power_manager_client.h" |
24 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
32 #include "content/public/browser/notification_service.h" | |
33 #include "skia/ext/image_operations.h" | |
34 #include "ui/gfx/codec/png_codec.h" | |
35 #include "ui/gfx/skia_util.h" | |
25 | 36 |
26 using content::BrowserThread; | 37 using content::BrowserThread; |
27 | 38 |
28 namespace { | 39 namespace { |
29 | 40 |
30 const int kWallpaperUpdateIntervalSec = 24 * 60 * 60; | 41 const int kWallpaperUpdateIntervalSec = 24 * 60 * 60; |
31 | 42 |
43 const char kWallpaperTypeNodeName[] = "type"; | |
44 const char kWallpaperIndexNodeName[] = "index"; | |
45 const char kWallpaperDateNodeName[] = "date"; | |
46 | |
47 const int kThumbnailWidth = 128; | |
48 const int kThumbnailHeight = 80; | |
49 | |
50 // Default wallpaper index used in OOBE (first boot). | |
51 // Defined here because Chromium default index differs. | |
52 // Also see ash::WallpaperInfo kDefaultWallpapers in | |
53 // desktop_background_resources.cc | |
54 #if defined(GOOGLE_CHROME_BUILD) | |
55 const int kDefaultOOBEWallpaperIndex = 16; // IDR_AURA_WALLPAPERS_3_URBAN0 | |
56 #else | |
57 const int kDefaultOOBEWallpaperIndex = 0; // IDR_AURA_WALLPAPERS_ROMAINGUY_0 | |
58 #endif | |
59 | |
32 // Names of nodes with info about wallpaper. | 60 // Names of nodes with info about wallpaper. |
33 const char kWallpaperDateNodeName[] = "date"; | 61 const char kNewWallpaperDateNodeName[] = "date"; |
34 const char kWallpaperLayoutNodeName[] = "layout"; | 62 const char kNewWallpaperLayoutNodeName[] = "layout"; |
35 const char kWallpaperFileNodeName[] = "file"; | 63 const char kNewWallpaperFileNodeName[] = "file"; |
36 const char kWallpaperTypeNodeName[] = "type"; | 64 const char kNewWallpaperTypeNodeName[] = "type"; |
37 | 65 |
38 } // namespace | 66 } // namespace |
39 | 67 |
40 namespace chromeos { | 68 namespace chromeos { |
41 | 69 |
42 static WallpaperManager* g_wallpaper_manager = NULL; | 70 static WallpaperManager* g_wallpaper_manager = NULL; |
43 | 71 |
44 // WallpaperManager, public: --------------------------------------------------- | 72 // WallpaperManager, public: --------------------------------------------------- |
45 | 73 |
46 // static | 74 // static |
47 WallpaperManager* WallpaperManager::Get() { | 75 WallpaperManager* WallpaperManager::Get() { |
48 if (!g_wallpaper_manager) | 76 if (!g_wallpaper_manager) |
49 g_wallpaper_manager = new WallpaperManager(); | 77 g_wallpaper_manager = new WallpaperManager(); |
50 return g_wallpaper_manager; | 78 return g_wallpaper_manager; |
51 } | 79 } |
52 | 80 |
81 WallpaperManager::WallpaperManager() | |
82 : ALLOW_THIS_IN_INITIALIZER_LIST(wallpaper_loader_(new UserImageLoader)), | |
83 current_user_wallpaper_type_(User::UNKNOWN), | |
84 ALLOW_THIS_IN_INITIALIZER_LIST(current_user_wallpaper_index_( | |
85 ash::GetInvalidWallpaperIndex())), | |
86 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
87 RestartTimer(); | |
88 registrar_.Add(this, | |
89 chrome::NOTIFICATION_LOGIN_USER_CHANGED, | |
90 content::NotificationService::AllSources()); | |
91 } | |
92 | |
53 // static | 93 // static |
54 void WallpaperManager::RegisterPrefs(PrefService* local_state) { | 94 void WallpaperManager::RegisterPrefs(PrefService* local_state) { |
55 local_state->RegisterDictionaryPref(prefs::kUsersWallpaperInfo, | 95 local_state->RegisterDictionaryPref(prefs::kUsersWallpaperInfo, |
56 PrefService::UNSYNCABLE_PREF); | 96 PrefService::UNSYNCABLE_PREF); |
57 } | 97 } |
58 | 98 |
59 WallpaperManager::WallpaperManager() : last_selected_user_("") { | 99 void WallpaperManager::AddObservers() { |
100 if (!DBusThreadManager::Get()->GetPowerManagerClient()->HasObserver(this)) | |
101 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this); | |
60 system::TimezoneSettings::GetInstance()->AddObserver(this); | 102 system::TimezoneSettings::GetInstance()->AddObserver(this); |
61 RestartTimer(); | |
62 } | 103 } |
63 | 104 |
64 void WallpaperManager::AddPowerManagerClientObserver() { | 105 void WallpaperManager::CacheIfCustomWallpaper(const std::string& email) { |
65 if (!DBusThreadManager::Get()->GetPowerManagerClient()->HasObserver(this)) | 106 User::WallpaperType type; |
66 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this); | 107 int index; |
108 base::Time date; | |
109 GetUserWallpaperProperties(email, &type, &index, &date); | |
110 if (type == User::CUSTOMIZED) { | |
111 std::string wallpaper_path = GetWallpaperPathForUser(email, false).value(); | |
112 | |
113 // Uses WeakPtr here to make the request cancelable. | |
114 wallpaper_loader_->Start(wallpaper_path, 0, true, | |
115 base::Bind(&WallpaperManager::CacheWallpaper, | |
116 weak_factory_.GetWeakPtr(), email)); | |
117 } | |
118 } | |
119 | |
120 void WallpaperManager::EnsureLoggedInUserWallpaperLoaded() { | |
121 User::WallpaperType type; | |
122 int index; | |
123 base::Time last_modification_date; | |
124 GetLoggedInUserWallpaperProperties(&type, &index, &last_modification_date); | |
125 | |
126 if (type != current_user_wallpaper_type_ || | |
127 index != current_user_wallpaper_index_) { | |
128 SetUserWallpaper(UserManager::Get()->GetLoggedInUser().email()); | |
129 } | |
130 } | |
131 | |
132 void WallpaperManager::FetchCustomWallpaper(const std::string& email) { | |
133 User::WallpaperType type; | |
134 int index; | |
135 base::Time date; | |
136 GetUserWallpaperProperties(email, &type, &index, &date); | |
137 ash::WallpaperLayout layout = static_cast<ash::WallpaperLayout>(index); | |
138 | |
139 std::string wallpaper_path = GetWallpaperPathForUser(email, false).value(); | |
140 | |
141 wallpaper_loader_->Start(wallpaper_path, 0, true, | |
142 base::Bind(&WallpaperManager::FetchWallpaper, | |
143 base::Unretained(this), email, layout)); | |
144 } | |
145 | |
146 bool WallpaperManager::GetCustomWallpaperFromCache(const std::string& email, | |
147 gfx::ImageSkia* wallpaper) { | |
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
149 CustomWallpaperMap::const_iterator it = custom_wallpaper_cache_.find(email); | |
150 if (it != custom_wallpaper_cache_.end()) { | |
151 *wallpaper = (*it).second; | |
152 return true; | |
153 } | |
154 return false; | |
155 } | |
156 | |
157 FilePath WallpaperManager::GetWallpaperPathForUser(const std::string& username, | |
158 bool is_thumbnail) { | |
159 const char* suffix = is_thumbnail ? "_thumb" : ""; | |
160 std::string filename = base::StringPrintf("%s%s%s%s", username.c_str(), | |
Nikita (slow)
2012/08/02 22:52:57
Format should be something like
"%s_wallpaper%s.pn
bshe
2012/08/03 00:23:35
Done.
| |
161 "_wallpaper", suffix, | |
162 ".png"); | |
163 FilePath user_data_dir; | |
164 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | |
165 return user_data_dir.AppendASCII(filename); | |
166 } | |
167 | |
168 gfx::ImageSkia WallpaperManager::GetCustomWallpaperThumbnail( | |
169 const std::string& email) { | |
170 CustomWallpaperMap::const_iterator it = | |
171 custom_wallpaper_thumbnail_cache_.find(email); | |
172 if (it != custom_wallpaper_cache_.end()) | |
173 return (*it).second; | |
174 else | |
175 return gfx::ImageSkia(); | |
176 } | |
177 | |
178 void WallpaperManager::GetLoggedInUserWallpaperProperties( | |
179 User::WallpaperType* type, | |
180 int* index, | |
181 base::Time* last_modification_date) { | |
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
183 | |
184 if (UserManager::Get()->IsLoggedInAsStub()) { | |
185 *type = current_user_wallpaper_type_ = User::DEFAULT; | |
186 *index = current_user_wallpaper_index_ = ash::GetInvalidWallpaperIndex(); | |
187 return; | |
188 } | |
189 | |
190 GetUserWallpaperProperties(UserManager::Get()->GetLoggedInUser().email(), | |
191 type, index, last_modification_date); | |
192 } | |
193 | |
194 void WallpaperManager::InitializeWallpaper() { | |
195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
196 UserManager* user_manager = UserManager::Get(); | |
197 | |
198 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) | |
Nikita (slow)
2012/08/02 22:52:57
You don't need this here as it will be done on hig
bshe
2012/08/03 00:23:35
I tried patch your cl and test it. Somehow the zer
| |
199 WizardController::SetZeroDelays(); | |
200 | |
201 if (!user_manager->IsUserLoggedIn()) { | |
202 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
203 switches::kDisableNewOobe)) { | |
204 if (!WizardController::IsDeviceRegistered() && | |
205 !WizardController::IsZeroDelayEnabled()) { | |
206 // TODO(nkostylev): Add switch to disable wallpaper transition on OOBE. | |
207 // Should be used on test images so that they are not slowed down. | |
208 ash::Shell::GetInstance()->desktop_background_controller()-> | |
209 SetDefaultWallpaper(kDefaultOOBEWallpaperIndex); | |
210 } else { | |
211 bool show_users = true; | |
212 bool result = CrosSettings::Get()->GetBoolean( | |
213 kAccountsPrefShowUserNamesOnSignIn, &show_users); | |
214 DCHECK(result) << "Unable to fetch setting " | |
215 << kAccountsPrefShowUserNamesOnSignIn; | |
216 if (!show_users) { | |
217 ash::Shell::GetInstance()->desktop_background_controller()-> | |
218 SetDefaultWallpaper(ash::GetSolidColorIndex()); | |
219 } | |
220 } | |
221 } | |
222 return; | |
223 } | |
224 SetUserWallpaper(user_manager->GetLoggedInUser().email()); | |
225 } | |
226 | |
227 void WallpaperManager::Observe(int type, | |
228 const content::NotificationSource& source, | |
229 const content::NotificationDetails& details) { | |
230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
231 if (type == chrome::NOTIFICATION_LOGIN_USER_CHANGED) { | |
232 // Cancel callback for previous cache requests. | |
233 weak_factory_.InvalidateWeakPtrs(); | |
234 custom_wallpaper_cache_.clear(); | |
235 } | |
67 } | 236 } |
68 | 237 |
69 void WallpaperManager::RestartTimer() { | 238 void WallpaperManager::RestartTimer() { |
70 timer_.Stop(); | 239 timer_.Stop(); |
71 base::Time midnight = base::Time::Now().LocalMidnight(); | 240 base::Time midnight = base::Time::Now().LocalMidnight(); |
72 base::TimeDelta interval = base::Time::Now() - midnight; | 241 base::TimeDelta interval = base::Time::Now() - midnight; |
73 int64 remaining_seconds = kWallpaperUpdateIntervalSec - interval.InSeconds(); | 242 int64 remaining_seconds = kWallpaperUpdateIntervalSec - interval.InSeconds(); |
74 if (remaining_seconds <= 0) { | 243 if (remaining_seconds <= 0) { |
75 BatchUpdateWallpaper(); | 244 BatchUpdateWallpaper(); |
76 } else { | 245 } else { |
77 // Set up a one shot timer which will batch update wallpaper at midnight. | 246 // Set up a one shot timer which will batch update wallpaper at midnight. |
78 timer_.Start(FROM_HERE, | 247 timer_.Start(FROM_HERE, |
79 base::TimeDelta::FromSeconds(remaining_seconds), | 248 base::TimeDelta::FromSeconds(remaining_seconds), |
80 this, | 249 this, |
81 &WallpaperManager::BatchUpdateWallpaper); | 250 &WallpaperManager::BatchUpdateWallpaper); |
82 } | 251 } |
83 } | 252 } |
84 | 253 |
254 void WallpaperManager::SaveUserWallpaperProperties(const std::string& email, | |
255 User::WallpaperType type, | |
256 int index) { | |
257 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
258 | |
259 current_user_wallpaper_type_ = type; | |
260 current_user_wallpaper_index_ = index; | |
261 // Ephemeral users can not save data to local state. We just cache the index | |
262 // in memory for them. | |
263 if (UserManager::Get()->IsCurrentUserEphemeral()) | |
264 return; | |
265 | |
266 PrefService* local_state = g_browser_process->local_state(); | |
267 DictionaryPrefUpdate wallpaper_update(local_state, | |
268 UserManager::kUserWallpapersProperties); | |
269 | |
270 base::DictionaryValue* wallpaper_properties = new base::DictionaryValue(); | |
271 wallpaper_properties->Set(kWallpaperTypeNodeName, | |
272 new base::FundamentalValue(type)); | |
273 wallpaper_properties->Set(kWallpaperIndexNodeName, | |
274 new base::FundamentalValue(index)); | |
275 wallpaper_properties->SetString(kWallpaperDateNodeName, | |
276 base::Int64ToString(base::Time::Now().LocalMidnight().ToInternalValue())); | |
277 wallpaper_update->SetWithoutPathExpansion(email, wallpaper_properties); | |
278 } | |
279 | |
280 void WallpaperManager::SetUserWallpaperFromFile( | |
281 const std::string& username, | |
282 const FilePath& path, | |
283 ash::WallpaperLayout layout, | |
284 base::WeakPtr<WallpaperDelegate> delegate) { | |
285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
286 | |
287 // For wallpapers, save the image without resizing. | |
288 wallpaper_loader_->Start( | |
289 path.value(), 0, true, | |
290 base::Bind(&WallpaperManager::SetWallpaper, | |
291 base::Unretained(this), username, layout, User::CUSTOMIZED, | |
292 delegate)); | |
293 } | |
294 | |
295 void WallpaperManager::SetInitialUserWallpaper(const std::string& username) { | |
296 current_user_wallpaper_type_ = User::DEFAULT; | |
297 // TODO(bshe): Ideally, wallpaper should start to load as soon as user click | |
298 // "Add user". | |
299 if (username == kGuestUser) | |
300 current_user_wallpaper_index_ = ash::GetGuestWallpaperIndex(); | |
301 else | |
302 current_user_wallpaper_index_ = ash::GetDefaultWallpaperIndex(); | |
303 SaveUserWallpaperProperties(username, | |
304 current_user_wallpaper_type_, | |
305 current_user_wallpaper_index_); | |
306 | |
307 // Some browser tests do not have shell instance. And it is not necessary to | |
308 // create a wallpaper for these tests. Add HasInstance check to prevent tests | |
309 // crash and speed up the tests by avoid loading wallpaper. | |
310 if (ash::Shell::HasInstance()) { | |
311 ash::Shell::GetInstance()->desktop_background_controller()-> | |
312 SetDefaultWallpaper(current_user_wallpaper_index_); | |
313 } | |
314 } | |
315 | |
85 void WallpaperManager::SaveUserWallpaperInfo(const std::string& username, | 316 void WallpaperManager::SaveUserWallpaperInfo(const std::string& username, |
86 const std::string& file_name, | 317 const std::string& file_name, |
87 ash::WallpaperLayout layout, | 318 ash::WallpaperLayout layout, |
88 User::WallpaperType type) { | 319 User::WallpaperType type) { |
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
90 | 321 |
91 // Ephemeral users can not save data to local state. We just cache the index | 322 // Ephemeral users can not save data to local state. We just cache the index |
92 // in memory for them. | 323 // in memory for them. |
93 if (UserManager::Get()->IsCurrentUserEphemeral()) | 324 if (UserManager::Get()->IsCurrentUserEphemeral()) |
94 return; | 325 return; |
95 | 326 |
96 PrefService* local_state = g_browser_process->local_state(); | 327 PrefService* local_state = g_browser_process->local_state(); |
97 DictionaryPrefUpdate wallpaper_update(local_state, | 328 DictionaryPrefUpdate wallpaper_update(local_state, |
98 prefs::kUsersWallpaperInfo); | 329 prefs::kUsersWallpaperInfo); |
99 | 330 |
100 base::DictionaryValue* wallpaper_properties = new base::DictionaryValue(); | 331 base::DictionaryValue* wallpaper_properties = new base::DictionaryValue(); |
101 wallpaper_properties->SetString(kWallpaperDateNodeName, | 332 wallpaper_properties->SetString(kNewWallpaperDateNodeName, |
102 base::Int64ToString(base::Time::Now().LocalMidnight().ToInternalValue())); | 333 base::Int64ToString(base::Time::Now().LocalMidnight().ToInternalValue())); |
103 wallpaper_properties->SetString(kWallpaperFileNodeName, file_name); | 334 wallpaper_properties->SetString(kNewWallpaperFileNodeName, file_name); |
104 wallpaper_properties->SetInteger(kWallpaperLayoutNodeName, layout); | 335 wallpaper_properties->SetInteger(kNewWallpaperLayoutNodeName, layout); |
105 wallpaper_properties->SetInteger(kWallpaperTypeNodeName, type); | 336 wallpaper_properties->SetInteger(kNewWallpaperTypeNodeName, type); |
106 wallpaper_update->SetWithoutPathExpansion(username, wallpaper_properties); | 337 wallpaper_update->SetWithoutPathExpansion(username, wallpaper_properties); |
107 } | 338 } |
108 | 339 |
109 void WallpaperManager::SetLastSelectedUser( | 340 void WallpaperManager::SetLastSelectedUser( |
110 const std::string& last_selected_user) { | 341 const std::string& last_selected_user) { |
111 last_selected_user_ = last_selected_user; | 342 last_selected_user_ = last_selected_user; |
112 } | 343 } |
113 | 344 |
345 void WallpaperManager::SetUserWallpaper(const std::string& email) { | |
346 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
347 if (!UserManager::Get()->IsKnownUser(email)) | |
348 return; | |
349 | |
350 User::WallpaperType type; | |
351 int index; | |
352 base::Time date; | |
353 GetUserWallpaperProperties(email, &type, &index, &date); | |
354 if (type == User::DAILY && date != base::Time::Now().LocalMidnight()) { | |
355 index = ash::GetNextWallpaperIndex(index); | |
356 SaveUserWallpaperProperties(email, User::DAILY, index); | |
357 } else if (type == User::CUSTOMIZED) { | |
358 gfx::ImageSkia custom_wallpaper; | |
359 if (GetCustomWallpaperFromCache(email, &custom_wallpaper)) { | |
360 // In customized mode, we use index pref to save the user selected | |
361 // wallpaper layout. See function SaveWallpaperToLocalState(). | |
362 ash::WallpaperLayout layout = static_cast<ash::WallpaperLayout>(index); | |
363 ash::Shell::GetInstance()->desktop_background_controller()-> | |
364 SetCustomWallpaper(custom_wallpaper, layout); | |
365 } else { | |
366 FetchCustomWallpaper(email); | |
367 } | |
368 return; | |
369 } | |
370 ash::Shell::GetInstance()->desktop_background_controller()-> | |
371 SetDefaultWallpaper(index); | |
372 SetLastSelectedUser(email); | |
373 } | |
374 | |
114 void WallpaperManager::SetWallpaperFromFilePath(const std::string& path, | 375 void WallpaperManager::SetWallpaperFromFilePath(const std::string& path, |
115 ash::WallpaperLayout layout) { | 376 ash::WallpaperLayout layout) { |
116 image_loader_->Start( | 377 image_loader_->Start( |
117 path, 0, false, | 378 path, 0, false, |
118 base::Bind(&WallpaperManager::OnWallpaperLoaded, | 379 base::Bind(&WallpaperManager::OnWallpaperLoaded, |
119 base::Unretained(this), layout)); | 380 base::Unretained(this), layout)); |
120 } | 381 } |
121 | 382 |
122 void WallpaperManager::SetWallpaperFromImageSkia( | 383 void WallpaperManager::SetWallpaperFromImageSkia( |
123 const gfx::ImageSkia& wallpaper, | 384 const gfx::ImageSkia& wallpaper, |
124 ash::WallpaperLayout layout) { | 385 ash::WallpaperLayout layout) { |
125 ash::Shell::GetInstance()->desktop_background_controller()-> | 386 ash::Shell::GetInstance()->desktop_background_controller()-> |
126 SetCustomWallpaper(wallpaper, layout); | 387 SetCustomWallpaper(wallpaper, layout); |
127 } | 388 } |
128 | 389 |
129 void WallpaperManager::UserDeselected() { | 390 void WallpaperManager::OnUserDeselected() { |
130 if (!UserManager::Get()->IsUserLoggedIn()) { | 391 if (!UserManager::Get()->IsUserLoggedIn()) { |
131 // This will set default login wallpaper (#fefefe). | 392 // This will set default login wallpaper (#fefefe). |
132 ash::Shell::GetInstance()->desktop_background_controller()-> | 393 ash::Shell::GetInstance()->desktop_background_controller()-> |
133 SetDefaultWallpaper(ash::GetSolidColorIndex()); | 394 SetDefaultWallpaper(ash::GetSolidColorIndex()); |
134 } | 395 } |
135 } | 396 } |
136 | 397 |
398 void WallpaperManager::OnUserSelected(const std::string& email) { | |
399 SetUserWallpaper(email); | |
400 } | |
401 | |
137 // WallpaperManager, private: -------------------------------------------------- | 402 // WallpaperManager, private: -------------------------------------------------- |
138 | 403 |
139 WallpaperManager::~WallpaperManager() { | 404 WallpaperManager::~WallpaperManager() { |
140 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this); | 405 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this); |
141 system::TimezoneSettings::GetInstance()->RemoveObserver(this); | 406 system::TimezoneSettings::GetInstance()->RemoveObserver(this); |
142 } | 407 } |
143 | 408 |
144 void WallpaperManager::OnCustomWallpaperLoaded(const std::string& email, | 409 void WallpaperManager::OnCustomWallpaperLoaded(const std::string& email, |
145 ash::WallpaperLayout layout, | 410 ash::WallpaperLayout layout, |
146 const UserImage& user_image) { | 411 const UserImage& user_image) { |
147 const SkBitmap& wallpaper = user_image.image(); | 412 const gfx::ImageSkia& wallpaper = user_image.image(); |
148 ash::Shell::GetInstance()->desktop_background_controller()-> | 413 ash::Shell::GetInstance()->desktop_background_controller()-> |
149 SetCustomWallpaper(wallpaper, layout); | 414 SetCustomWallpaper(wallpaper, layout); |
150 } | 415 } |
151 | 416 |
152 void WallpaperManager::BatchUpdateWallpaper() { | 417 void WallpaperManager::BatchUpdateWallpaper() { |
153 PrefService* local_state = g_browser_process->local_state(); | 418 PrefService* local_state = g_browser_process->local_state(); |
154 UserManager* user_manager = UserManager::Get(); | 419 UserManager* user_manager = UserManager::Get(); |
155 bool show_users = true; | 420 bool show_users = true; |
156 CrosSettings::Get()->GetBoolean( | 421 CrosSettings::Get()->GetBoolean( |
157 kAccountsPrefShowUserNamesOnSignIn, &show_users); | 422 kAccountsPrefShowUserNamesOnSignIn, &show_users); |
158 if (local_state) { | 423 if (local_state) { |
159 User::WallpaperType type; | 424 User::WallpaperType type; |
160 int index = 0; | 425 int index = 0; |
161 base::Time last_modification_date; | 426 base::Time last_modification_date; |
162 const UserList& users = user_manager->GetUsers(); | 427 const UserList& users = user_manager->GetUsers(); |
163 for (UserList::const_iterator it = users.begin(); | 428 for (UserList::const_iterator it = users.begin(); |
164 it != users.end(); ++it) { | 429 it != users.end(); ++it) { |
165 std::string email = (*it)->email(); | 430 std::string email = (*it)->email(); |
166 // TODO(bshe): Move GetUserWallpaperProperties() to this class. | 431 GetUserWallpaperProperties(email, &type, &index, &last_modification_date); |
167 static_cast<UserManagerImpl*>(user_manager)-> | |
168 GetUserWallpaperProperties(email, &type, &index, | |
169 &last_modification_date); | |
170 base::Time current_date = base::Time::Now().LocalMidnight(); | 432 base::Time current_date = base::Time::Now().LocalMidnight(); |
171 if (type == User::DAILY && current_date != last_modification_date) { | 433 if (type == User::DAILY && current_date != last_modification_date) { |
172 index = ash::GetNextWallpaperIndex(index); | 434 index = ash::GetNextWallpaperIndex(index); |
173 // TODO(bshe): Move SaveUserWallpaperProperties() to this class. | 435 SaveUserWallpaperProperties(email, type, index); |
174 static_cast<UserManagerImpl*>(user_manager)-> | |
175 SaveUserWallpaperProperties(email, type, index); | |
176 } | 436 } |
177 // Force a wallpaper update for logged in / last selected user. | 437 // Force a wallpaper update for logged in / last selected user. |
178 // TODO(bshe): Notify lock screen, wallpaper picker UI to update wallpaper | 438 // TODO(bshe): Notify lock screen, wallpaper picker UI to update wallpaper |
179 // as well. | 439 // as well. |
180 if (user_manager->IsUserLoggedIn() && | 440 if (user_manager->IsUserLoggedIn() && |
181 email == user_manager->GetLoggedInUser().email()) { | 441 email == user_manager->GetLoggedInUser().email()) { |
182 user_manager->UserSelected(email); | 442 SetUserWallpaper(email); |
183 } else if (show_users && | 443 } else if (show_users && |
184 email == last_selected_user_) { | 444 email == last_selected_user_) { |
185 user_manager->UserSelected(email); | 445 SetUserWallpaper(email); |
186 } | 446 } |
187 } | 447 } |
188 } | 448 } |
189 RestartTimer(); | 449 RestartTimer(); |
190 } | 450 } |
191 | 451 |
452 void WallpaperManager::CacheWallpaper(const std::string& email, | |
453 const UserImage& wallpaper) { | |
454 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
455 DCHECK(custom_wallpaper_cache_.find(email) == custom_wallpaper_cache_.end()); | |
456 | |
457 BrowserThread::PostTask( | |
458 BrowserThread::FILE, | |
459 FROM_HERE, | |
460 base::Bind(&WallpaperManager::CacheThumbnail, | |
461 base::Unretained(this), email, wallpaper.image())); | |
462 | |
463 custom_wallpaper_cache_.insert(std::make_pair(email, wallpaper.image())); | |
464 } | |
465 | |
466 void WallpaperManager::CacheThumbnail(const std::string& email, | |
467 const gfx::ImageSkia& wallpaper) { | |
468 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
469 gfx::ImageSkia thumbnail = | |
470 skia::ImageOperations::Resize(wallpaper, | |
471 skia::ImageOperations::RESIZE_LANCZOS3, | |
472 kThumbnailWidth, kThumbnailHeight); | |
473 custom_wallpaper_thumbnail_cache_[email] = thumbnail; | |
474 } | |
475 | |
476 void WallpaperManager::FetchWallpaper(const std::string& email, | |
477 ash::WallpaperLayout layout, | |
478 const UserImage& wallpaper) { | |
479 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
480 | |
481 BrowserThread::PostTask( | |
482 BrowserThread::FILE, | |
483 FROM_HERE, | |
484 base::Bind(&WallpaperManager::CacheThumbnail, | |
485 base::Unretained(this), email, wallpaper.image())); | |
486 | |
487 ash::Shell::GetInstance()->desktop_background_controller()-> | |
488 SetCustomWallpaper(wallpaper.image(), layout); | |
489 } | |
490 | |
491 void WallpaperManager::GetUserWallpaperProperties(const std::string& email, | |
492 User::WallpaperType* type, | |
493 int* index, | |
494 base::Time* last_modification_date) { | |
495 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
496 | |
497 // Default to the values cached in memory. | |
498 *type = current_user_wallpaper_type_; | |
499 *index = current_user_wallpaper_index_; | |
500 | |
501 // Override with values found in local store, if any. | |
502 if (!email.empty()) { | |
503 const DictionaryValue* user_wallpapers = g_browser_process->local_state()-> | |
504 GetDictionary(UserManager::kUserWallpapersProperties); | |
505 const base::DictionaryValue* wallpaper_properties; | |
506 if (user_wallpapers->GetDictionaryWithoutPathExpansion( | |
507 email, | |
508 &wallpaper_properties)) { | |
509 *type = User::UNKNOWN; | |
510 *index = ash::GetInvalidWallpaperIndex(); | |
511 wallpaper_properties->GetInteger(kWallpaperTypeNodeName, | |
512 reinterpret_cast<int*>(type)); | |
513 wallpaper_properties->GetInteger(kWallpaperIndexNodeName, index); | |
514 std::string date_string; | |
515 int64 val; | |
516 if (!(wallpaper_properties->GetString(kWallpaperDateNodeName, | |
517 &date_string) && | |
518 base::StringToInt64(date_string, &val))) | |
519 val = 0; | |
520 *last_modification_date = base::Time::FromInternalValue(val); | |
521 } | |
522 } | |
523 } | |
524 | |
525 void WallpaperManager::GenerateUserWallpaperThumbnail( | |
526 const std::string& email, | |
527 User::WallpaperType type, | |
528 base::WeakPtr<WallpaperDelegate> delegate, | |
529 const gfx::ImageSkia& wallpaper) { | |
530 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
531 gfx::ImageSkia thumbnail = | |
532 skia::ImageOperations::Resize(wallpaper, | |
533 skia::ImageOperations::RESIZE_LANCZOS3, | |
534 kThumbnailWidth, kThumbnailHeight); | |
535 | |
536 custom_wallpaper_thumbnail_cache_[email] = thumbnail; | |
537 | |
538 // Notify thumbnail is ready. | |
539 BrowserThread::PostTask( | |
540 BrowserThread::UI, | |
541 FROM_HERE, | |
542 base::Bind(&WallpaperManager::OnThumbnailUpdated, | |
543 base::Unretained(this), delegate)); | |
544 } | |
545 | |
546 void WallpaperManager::OnThumbnailUpdated( | |
547 base::WeakPtr<WallpaperDelegate> delegate) { | |
548 if (delegate) | |
549 delegate->SetCustomWallpaperThumbnail(); | |
550 } | |
551 | |
552 void WallpaperManager::SaveWallpaper(const std::string& path, | |
553 const UserImage& wallpaper) { | |
554 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
555 std::vector<unsigned char> image_data = wallpaper.raw_image(); | |
556 int write_bytes = file_util::WriteFile(FilePath(path), | |
557 reinterpret_cast<char*>(&*image_data.begin()), | |
558 image_data.size()); | |
559 DCHECK(write_bytes == static_cast<int>(image_data.size())); | |
560 } | |
561 | |
562 void WallpaperManager::SetWallpaper(const std::string& username, | |
563 ash::WallpaperLayout layout, | |
564 User::WallpaperType type, | |
565 base::WeakPtr<WallpaperDelegate> delegate, | |
566 const UserImage& wallpaper) { | |
567 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
568 | |
569 std::string wallpaper_path = | |
570 GetWallpaperPathForUser(username, false).value(); | |
571 | |
572 // TODO(bshe): Ephemeral user should not save custom wallpaper to disk. | |
573 BrowserThread::PostTask( | |
574 BrowserThread::FILE, | |
575 FROM_HERE, | |
576 base::Bind(&WallpaperManager::SaveWallpaper, | |
577 base::Unretained(this), wallpaper_path, wallpaper)); | |
578 | |
579 BrowserThread::PostTask( | |
580 BrowserThread::FILE, | |
581 FROM_HERE, | |
582 base::Bind(&WallpaperManager::GenerateUserWallpaperThumbnail, | |
583 base::Unretained(this), username, type, delegate, | |
584 wallpaper.image())); | |
585 | |
586 ash::Shell::GetInstance()->desktop_background_controller()-> | |
587 SetCustomWallpaper(wallpaper.image(), layout); | |
588 SaveUserWallpaperProperties(username, type, layout); | |
589 } | |
590 | |
192 void WallpaperManager::OnWallpaperLoaded(ash::WallpaperLayout layout, | 591 void WallpaperManager::OnWallpaperLoaded(ash::WallpaperLayout layout, |
193 const UserImage& user_image) { | 592 const UserImage& user_image) { |
194 const SkBitmap& wallpaper = user_image.image(); | 593 const SkBitmap& wallpaper = user_image.image(); |
195 SetWallpaperFromImageSkia(wallpaper, layout); | 594 SetWallpaperFromImageSkia(wallpaper, layout); |
196 } | 595 } |
197 | 596 |
198 void WallpaperManager::SystemResumed() { | 597 void WallpaperManager::SystemResumed() { |
199 BatchUpdateWallpaper(); | 598 BatchUpdateWallpaper(); |
200 } | 599 } |
201 | 600 |
202 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) { | 601 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) { |
203 RestartTimer(); | 602 RestartTimer(); |
204 } | 603 } |
205 | 604 |
206 } // chromeos | 605 } // chromeos |
OLD | NEW |