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

Side by Side Diff: chrome/browser/chromeos/login/wallpaper_manager.cc

Issue 10832019: Speed up custom wallpaper switching time and wallpaper manager code refactor (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 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 (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/string_number_conversions.h"
14 #include "base/string_util.h"
11 #include "base/time.h" 15 #include "base/time.h"
12 #include "base/values.h" 16 #include "base/values.h"
13 #include "chrome/browser/browser_process.h" 17 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chromeos/cros_settings.h" 18 #include "chrome/browser/chromeos/cros_settings.h"
15 #include "chrome/browser/chromeos/login/user.h" 19 #include "chrome/browser/chromeos/login/user.h"
16 #include "chrome/browser/chromeos/login/user_manager.h" 20 #include "chrome/browser/chromeos/login/user_manager.h"
17 #include "chrome/browser/chromeos/login/user_manager_impl.h" 21 #include "chrome/browser/chromeos/login/user_manager_impl.h"
22 #include "chrome/browser/chromeos/login/wizard_controller.h"
18 #include "chrome/browser/prefs/pref_service.h" 23 #include "chrome/browser/prefs/pref_service.h"
24 #include "chrome/browser/prefs/scoped_user_pref_update.h"
25 #include "chrome/common/chrome_notification_types.h"
26 #include "chrome/common/chrome_switches.h"
19 #include "chromeos/dbus/dbus_thread_manager.h" 27 #include "chromeos/dbus/dbus_thread_manager.h"
20 #include "chromeos/dbus/power_manager_client.h" 28 #include "chromeos/dbus/power_manager_client.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/notification_service.h"
31 #include "skia/ext/image_operations.h"
32 #include "third_party/zlib/zlib.h"
33 #include "ui/gfx/codec/png_codec.h"
34 #include "ui/gfx/skia_util.h"
35
36 using content::BrowserThread;
21 37
22 namespace { 38 namespace {
39
23 const int kWallpaperUpdateIntervalSec = 24 * 60 * 60; 40 const int kWallpaperUpdateIntervalSec = 24 * 60 * 60;
41
42 const char kWallpaperTypeNodeName[] = "type";
43 const char kWallpaperIndexNodeName[] = "index";
44 const char kWallpaperDateNodeName[] = "date";
45
46 const int kThumbnailWidth = 128;
47 const int kThumbnailHeight = 80;
48
49 // Default wallpaper index used in OOBE (first boot).
50 // Defined here because Chromium default index differs.
51 // Also see ash::WallpaperInfo kDefaultWallpapers in
52 // desktop_background_resources.cc
53 #if defined(GOOGLE_CHROME_BUILD)
54 const int kDefaultOOBEWallpaperIndex = 16; // IDR_AURA_WALLPAPERS_3_URBAN0
55 #else
56 const int kDefaultOOBEWallpaperIndex = 0; // IDR_AURA_WALLPAPERS_ROMAINGUY_0
57 #endif
58
24 } // namespace 59 } // namespace
25 60
26 namespace chromeos { 61 namespace chromeos {
27 62
28 static WallpaperManager* g_wallpaper_manager = NULL; 63 static WallpaperManager* g_wallpaper_manager = NULL;
29 64
30 // WallpaperManager, public: --------------------------------------------------- 65 // WallpaperManager, public: ---------------------------------------------------
31 66
32 // static 67 // static
33 WallpaperManager* WallpaperManager::Get() { 68 WallpaperManager* WallpaperManager::Get() {
34 if (!g_wallpaper_manager) 69 if (!g_wallpaper_manager)
35 g_wallpaper_manager = new WallpaperManager(); 70 g_wallpaper_manager = new WallpaperManager();
36 return g_wallpaper_manager; 71 return g_wallpaper_manager;
37 } 72 }
38 73
39 WallpaperManager::WallpaperManager() : last_selected_user_("") { 74 WallpaperManager::WallpaperManager()
75 : ALLOW_THIS_IN_INITIALIZER_LIST(wallpaper_loader_(new WallpaperLoader)),
76 current_user_wallpaper_type_(User::UNKNOWN),
77 ALLOW_THIS_IN_INITIALIZER_LIST(current_user_wallpaper_index_(
78 ash::GetInvalidWallpaperIndex())),
79 last_selected_user_(""),
80 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
40 system::TimezoneSettings::GetInstance()->AddObserver(this); 81 system::TimezoneSettings::GetInstance()->AddObserver(this);
41 RestartTimer(); 82 RestartTimer();
83 registrar_.Add(this,
84 chrome::NOTIFICATION_LOGIN_USER_CHANGED,
85 content::NotificationService::AllSources());
42 } 86 }
43 87
44 void WallpaperManager::AddPowerManagerClientObserver() { 88 void WallpaperManager::AddPowerManagerClientObserver() {
45 if (!DBusThreadManager::Get()->GetPowerManagerClient()->HasObserver(this)) 89 if (!DBusThreadManager::Get()->GetPowerManagerClient()->HasObserver(this))
46 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this); 90 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this);
47 } 91 }
48 92
93 void WallpaperManager::CacheIfCustomWallpaper(const std::string& email) {
94 User::WallpaperType type;
95 int index;
96 base::Time date;
97 GetUserWallpaperProperties(email, &type, &index, &date);
98 if (type == User::CUSTOMIZED) {
99 // Uses WeakPtr here to make the request cancelable.
100 LoadedCallback loaded_cb = base::Bind(&WallpaperManager::CacheCallback,
Ivan Korotkov 2012/07/27 23:02:02 Here and below: there is no point in storing a cal
bshe 2012/07/31 14:27:54 Done.
101 weak_factory_.GetWeakPtr(), email);
102 LoadCustomWallpaperFromFile(email, loaded_cb);
103 }
104 }
105
106 void WallpaperManager::EnsureLoggedInUserWallpaperLoaded() {
107 User::WallpaperType type;
108 int index;
109 base::Time last_modification_date;
110 GetLoggedInUserWallpaperProperties(&type, &index, &last_modification_date);
111
112 if (type != current_user_wallpaper_type_ ||
113 index != current_user_wallpaper_index_) {
114 UserSelected(UserManager::Get()->GetLoggedInUser().email());
Ivan Korotkov 2012/07/27 23:02:02 Please don't make manual call of event handlers (l
bshe 2012/07/31 14:27:54 Done.
115 }
116 }
117
118 void WallpaperManager::FetchCustomWallpaper(const std::string& email) {
119 User::WallpaperType type;
120 int index;
121 base::Time date;
122 GetUserWallpaperProperties(email, &type, &index, &date);
123 ash::WallpaperLayout layout = static_cast<ash::WallpaperLayout>(index);
124 LoadedCallback loaded_cb = base::Bind(&WallpaperManager::FetchCallback,
125 base::Unretained(this), email, layout);
126 LoadCustomWallpaperFromFile(email, loaded_cb);
127 }
128
129 bool WallpaperManager::GetCustomWallpaperFromCache(const std::string& email,
130 SkBitmap* wallpaper) {
131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
132 CustomWallpaperMap::iterator it = custom_wallpaper_cache_.find(email);
Ivan Korotkov 2012/07/27 23:02:02 Here and below: const_iterator
bshe 2012/07/31 14:27:54 Done.
133 if (it != custom_wallpaper_cache_.end()) {
134 *wallpaper = (*it).second;
135 return true;
136 } else {
Ivan Korotkov 2012/07/27 23:02:02 Chrome's style is "no else after return"
bshe 2012/07/31 14:27:54 Done.
137 return false;
138 }
139 }
140
141 SkBitmap WallpaperManager::GetCustomWallpaperThumbnail(
142 const std::string& email) {
143 CustomWallpaperMap::iterator it =
144 custom_wallpaper_thumbnail_cache_.find(email);
145 if (it != custom_wallpaper_cache_.end())
146 return (*it).second;
147 else
148 return SkBitmap();
149 }
150
151 void WallpaperManager::GetLoggedInUserWallpaperProperties(
152 User::WallpaperType* type,
153 int* index,
154 base::Time* last_modification_date) {
155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
156
157 if (UserManager::Get()->IsLoggedInAsStub()) {
158 *type = current_user_wallpaper_type_ = User::DEFAULT;
159 *index = current_user_wallpaper_index_ = ash::GetInvalidWallpaperIndex();
160 return;
161 }
162
163 GetUserWallpaperProperties(UserManager::Get()->GetLoggedInUser().email(),
164 type, index, last_modification_date);
165 }
166
167 void WallpaperManager::InitializeWallpaper() {
168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
169 UserManager* user_manager = UserManager::Get();
170 if (!user_manager->IsUserLoggedIn()) {
171 if (!CommandLine::ForCurrentProcess()->HasSwitch(
172 switches::kDisableNewOobe)) {
173 if (!WizardController::IsDeviceRegistered() &&
174 !WizardController::IsZeroDelayEnabled()) {
175 // TODO(nkostylev): Add switch to disable wallpaper transition on OOBE.
176 // Should be used on test images so that they are not slowed down.
177 ash::Shell::GetInstance()->desktop_background_controller()->
178 SetDefaultWallpaper(kDefaultOOBEWallpaperIndex);
179 } else {
180 bool show_users = true;
181 bool result = CrosSettings::Get()->GetBoolean(
182 kAccountsPrefShowUserNamesOnSignIn, &show_users);
183 DCHECK(result) << "Unable to fetch setting "
184 << kAccountsPrefShowUserNamesOnSignIn;
185 if (!show_users) {
186 ash::Shell::GetInstance()->desktop_background_controller()->
187 SetDefaultWallpaper(ash::GetSolidColorIndex());
188 }
189 }
190 }
191 return;
192 }
193 UserSelected(user_manager->GetLoggedInUser().email());
Ivan Korotkov 2012/07/27 23:02:02 Same note about calling event handler.
bshe 2012/07/31 14:27:54 Done.
194 }
195
196 void WallpaperManager::Observe(int type,
197 const content::NotificationSource& source,
198 const content::NotificationDetails& details) {
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
200 if (type == chrome::NOTIFICATION_LOGIN_USER_CHANGED) {
201 // Cancel callback for previous cache requests.
202 weak_factory_.InvalidateWeakPtrs();
203 custom_wallpaper_cache_.clear();
204 }
205 }
206
49 void WallpaperManager::RestartTimer() { 207 void WallpaperManager::RestartTimer() {
50 timer_.Stop(); 208 timer_.Stop();
51 base::Time midnight = base::Time::Now().LocalMidnight(); 209 base::Time midnight = base::Time::Now().LocalMidnight();
52 base::TimeDelta interval = base::Time::Now() - midnight; 210 base::TimeDelta interval = base::Time::Now() - midnight;
53 int64 remaining_seconds = kWallpaperUpdateIntervalSec - interval.InSeconds(); 211 int64 remaining_seconds = kWallpaperUpdateIntervalSec - interval.InSeconds();
54 if (remaining_seconds <= 0) { 212 if (remaining_seconds <= 0) {
55 BatchUpdateWallpaper(); 213 BatchUpdateWallpaper();
56 } else { 214 } else {
57 // Set up a one shot timer which will batch update wallpaper at midnight. 215 // Set up a one shot timer which will batch update wallpaper at midnight.
58 timer_.Start(FROM_HERE, 216 timer_.Start(FROM_HERE,
59 base::TimeDelta::FromSeconds(remaining_seconds), 217 base::TimeDelta::FromSeconds(remaining_seconds),
60 this, 218 this,
61 &WallpaperManager::BatchUpdateWallpaper); 219 &WallpaperManager::BatchUpdateWallpaper);
62 } 220 }
63 } 221 }
64 222
223 void WallpaperManager::SaveUserWallpaperProperties(const std::string& email,
224 User::WallpaperType type,
225 int index) {
226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
227
228 current_user_wallpaper_type_ = type;
229 current_user_wallpaper_index_ = index;
230 // Ephemeral users can not save data to local state. We just cache the index
231 // in memory for them.
232 if (UserManager::Get()->IsCurrentUserEphemeral()) {
233 return;
234 }
235
236 PrefService* local_state = g_browser_process->local_state();
237 DictionaryPrefUpdate wallpaper_update(local_state,
238 UserManager::kUserWallpapersProperties);
239
240 base::DictionaryValue* wallpaper_properties = new base::DictionaryValue();
241 wallpaper_properties->Set(kWallpaperTypeNodeName,
242 new base::FundamentalValue(type));
243 wallpaper_properties->Set(kWallpaperIndexNodeName,
244 new base::FundamentalValue(index));
245 wallpaper_properties->SetString(kWallpaperDateNodeName,
246 base::Int64ToString(base::Time::Now().LocalMidnight().ToInternalValue()));
247 wallpaper_update->SetWithoutPathExpansion(email, wallpaper_properties);
248 }
249
250 void WallpaperManager::SetUserWallpaperFromFile(
251 const std::string& username,
252 const FilePath& path,
253 ash::WallpaperLayout layout,
254 base::WeakPtr<WallpaperDelegate> delegate) {
255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
256
257 // TODO(bshe): Refactor GetWallpaperPathForUser to this class.
258 std::string wallpaper_path =
259 static_cast<UserManagerImpl*>(UserManager::Get())->
260 GetWallpaperPathForUser(username, false).value();
261 // need to expose ephemeral user.
262 bool save_raw_file = true;
263
264 // For wallpapers, save the image without resizing.
265 wallpaper_loader_->Start(
266 path.value(), save_raw_file, wallpaper_path,
267 base::Bind(&WallpaperManager::SetCallback,
268 base::Unretained(this), username, layout, User::CUSTOMIZED,
269 delegate));
270 }
271
272 void WallpaperManager::SetInitialUserWallpaper(const std::string& username) {
273 current_user_wallpaper_type_ = User::DEFAULT;
274 // TODO(bshe): Ideally, wallpaper should start to load as soon as user click
275 // "Add user".
276 if (username == kGuestUser)
277 current_user_wallpaper_index_ = ash::GetGuestWallpaperIndex();
278 else
279 current_user_wallpaper_index_ = ash::GetDefaultWallpaperIndex();
280 SaveUserWallpaperProperties(username,
281 current_user_wallpaper_type_,
282 current_user_wallpaper_index_);
283
284 // Some browser tests do not have shell instance. And it is not necessary to
285 // create a wallpaper for these tests. Add HasInstance check to prevent tests
286 // crash and speed up the tests by avoid loading wallpaper.
287 if (ash::Shell::HasInstance()) {
288 ash::Shell::GetInstance()->desktop_background_controller()->
289 SetDefaultWallpaper(current_user_wallpaper_index_);
290 }
291 }
292
65 void WallpaperManager::SetLastSelectedUser( 293 void WallpaperManager::SetLastSelectedUser(
66 const std::string& last_selected_user) { 294 const std::string& last_selected_user) {
67 last_selected_user_ = last_selected_user; 295 last_selected_user_ = last_selected_user;
68 } 296 }
69 297
70 void WallpaperManager::UserDeselected() { 298 void WallpaperManager::UserDeselected() {
71 if (!UserManager::Get()->IsUserLoggedIn()) { 299 if (!UserManager::Get()->IsUserLoggedIn()) {
72 // This will set default login wallpaper (#fefefe). 300 // This will set default login wallpaper (#fefefe).
73 ash::Shell::GetInstance()->desktop_background_controller()-> 301 ash::Shell::GetInstance()->desktop_background_controller()->
74 SetDefaultWallpaper(ash::GetSolidColorIndex()); 302 SetDefaultWallpaper(ash::GetSolidColorIndex());
75 } 303 }
76 } 304 }
77 305
306 void WallpaperManager::UserSelected(const std::string& email) {
307 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
308 if (UserManager::Get()->IsKnownUser(email)) {
309 User::WallpaperType type;
310 int index;
311 base::Time date;
312 GetUserWallpaperProperties(email, &type, &index, &date);
313 if (type == User::DAILY && date != base::Time::Now().LocalMidnight()) {
314 index = ash::GetNextWallpaperIndex(index);
315 SaveUserWallpaperProperties(email, User::DAILY, index);
316 } else if (type == User::CUSTOMIZED) {
317 SkBitmap custom_wallpaper;
318 if (GetCustomWallpaperFromCache(email, &custom_wallpaper)) {
319 // In customized mode, we use index pref to save the user selected
320 // wallpaper layout. See function SaveWallpaperToLocalState().
321 ash::WallpaperLayout layout = static_cast<ash::WallpaperLayout>(index);
322 ash::Shell::GetInstance()->desktop_background_controller()->
323 SetCustomWallpaper(custom_wallpaper, layout);
324 } else {
325 FetchCustomWallpaper(email);
326 }
327 return;
328 }
329 ash::Shell::GetInstance()->desktop_background_controller()->
330 SetDefaultWallpaper(index);
331 SetLastSelectedUser(email);
332 }
333 }
334
78 // WallpaperManager, private: -------------------------------------------------- 335 // WallpaperManager, private: --------------------------------------------------
79 336
80 WallpaperManager::~WallpaperManager() { 337 WallpaperManager::~WallpaperManager() {
81 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this); 338 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this);
82 system::TimezoneSettings::GetInstance()->RemoveObserver(this); 339 system::TimezoneSettings::GetInstance()->RemoveObserver(this);
83 } 340 }
84 341
85 void WallpaperManager::BatchUpdateWallpaper() { 342 void WallpaperManager::BatchUpdateWallpaper() {
86 PrefService* local_state = g_browser_process->local_state(); 343 PrefService* local_state = g_browser_process->local_state();
87 UserManager* user_manager = UserManager::Get(); 344 UserManager* user_manager = UserManager::Get();
88 bool show_users = true; 345 bool show_users = true;
89 CrosSettings::Get()->GetBoolean( 346 CrosSettings::Get()->GetBoolean(
90 kAccountsPrefShowUserNamesOnSignIn, &show_users); 347 kAccountsPrefShowUserNamesOnSignIn, &show_users);
91 if (local_state) { 348 if (local_state) {
92 User::WallpaperType type; 349 User::WallpaperType type;
93 int index = 0; 350 int index = 0;
94 base::Time last_modification_date; 351 base::Time last_modification_date;
95 const UserList& users = user_manager->GetUsers(); 352 const UserList& users = user_manager->GetUsers();
96 for (UserList::const_iterator it = users.begin(); 353 for (UserList::const_iterator it = users.begin();
97 it != users.end(); ++it) { 354 it != users.end(); ++it) {
98 std::string email = (*it)->email(); 355 std::string email = (*it)->email();
99 // TODO(bshe): Move GetUserWallpaperProperties() to this class. 356 GetUserWallpaperProperties(email, &type, &index, &last_modification_date);
100 static_cast<UserManagerImpl*>(user_manager)->
101 GetUserWallpaperProperties(email, &type, &index,
102 &last_modification_date);
103 base::Time current_date = base::Time::Now().LocalMidnight(); 357 base::Time current_date = base::Time::Now().LocalMidnight();
104 if (type == User::DAILY && current_date != last_modification_date) { 358 if (type == User::DAILY && current_date != last_modification_date) {
105 index = ash::GetNextWallpaperIndex(index); 359 index = ash::GetNextWallpaperIndex(index);
106 // TODO(bshe): Move SaveUserWallpaperProperties() to this class. 360 SaveUserWallpaperProperties(email, type, index);
107 static_cast<UserManagerImpl*>(user_manager)->
108 SaveUserWallpaperProperties(email, type, index);
109 } 361 }
110 // Force a wallpaper update for logged in / last selected user. 362 // Force a wallpaper update for logged in / last selected user.
111 // TODO(bshe): Notify lock screen, wallpaper picker UI to update wallpaper 363 // TODO(bshe): Notify lock screen, wallpaper picker UI to update wallpaper
112 // as well. 364 // as well.
113 if (user_manager->IsUserLoggedIn() && 365 if (user_manager->IsUserLoggedIn() &&
114 email == user_manager->GetLoggedInUser().email()) { 366 email == user_manager->GetLoggedInUser().email()) {
115 user_manager->UserSelected(email); 367 UserSelected(email);
116 } else if (show_users && 368 } else if (show_users &&
117 email == last_selected_user_) { 369 email == last_selected_user_) {
118 user_manager->UserSelected(email); 370 UserSelected(email);
119 } 371 }
120 } 372 }
121 } 373 }
122 RestartTimer(); 374 RestartTimer();
123 } 375 }
124 376
377 void WallpaperManager::CacheCallback(const std::string& email,
378 const SkBitmap& wallpaper) {
379 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
380
381 if (custom_wallpaper_cache_.find(email) != custom_wallpaper_cache_.end()) {
382 LOG(ERROR) << "Try to cache a wallpaper for the same user twice.";
Ivan Korotkov 2012/07/27 23:02:02 No need in braces
Ivan Korotkov 2012/07/27 23:02:02 "Attemped to cache wallpaper for the same user twi
Ivan Korotkov 2012/07/27 23:02:02 If this can happen only due to incorrect programmi
bshe 2012/07/31 14:27:54 Done. Using a DCHECK instead. On 2012/07/27 23:02
383 }
384
385 BrowserThread::PostTask(
386 BrowserThread::FILE,
387 FROM_HERE,
388 base::Bind(&WallpaperManager::CacheThumbnail,
389 base::Unretained(this), email, wallpaper));
390
391 custom_wallpaper_cache_.insert(std::make_pair(email, wallpaper));
392 }
393
394 void WallpaperManager::CacheThumbnail(const std::string& email,
395 const SkBitmap& wallpaper) {
396 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
397 SkBitmap thumbnail =
398 skia::ImageOperations::Resize(wallpaper,
399 skia::ImageOperations::RESIZE_LANCZOS3,
400 kThumbnailWidth, kThumbnailHeight);
401 CustomWallpaperMap::iterator it =
Ivan Korotkov 2012/07/27 23:02:02 Just custom_wallpaper_thumbnail_cache_[email] = th
bshe 2012/07/31 14:27:54 Done.
402 custom_wallpaper_thumbnail_cache_.find(email);
403 if (it != custom_wallpaper_thumbnail_cache_.end())
404 it->second = thumbnail;
405 else
406 custom_wallpaper_thumbnail_cache_.insert(std::make_pair(email, thumbnail));
407 }
408
409 void WallpaperManager::FetchCallback(const std::string& email,
410 ash::WallpaperLayout layout,
411 const SkBitmap& wallpaper) {
412 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
413
414 BrowserThread::PostTask(
415 BrowserThread::FILE,
416 FROM_HERE,
417 base::Bind(&WallpaperManager::CacheThumbnail,
418 base::Unretained(this), email, wallpaper));
419
420 ash::Shell::GetInstance()->desktop_background_controller()->
421 SetCustomWallpaper(wallpaper, layout);
422 }
423
424 void WallpaperManager::GetUserWallpaperProperties(const std::string& email,
425 User::WallpaperType* type,
426 int* index,
427 base::Time* last_modification_date) {
428 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
429
430 // Default to the values cached in memory.
431 *type = current_user_wallpaper_type_;
432 *index = current_user_wallpaper_index_;
433
434 // Override with values found in local store, if any.
435 if (!email.empty()) {
436 const DictionaryValue* user_wallpapers = g_browser_process->local_state()->
437 GetDictionary(UserManager::kUserWallpapersProperties);
438 base::DictionaryValue* wallpaper_properties;
439 if (user_wallpapers->GetDictionaryWithoutPathExpansion(
440 email,
441 &wallpaper_properties)) {
442 *type = User::UNKNOWN;
443 *index = ash::GetInvalidWallpaperIndex();
444 wallpaper_properties->GetInteger(kWallpaperTypeNodeName,
445 reinterpret_cast<int*>(type));
446 wallpaper_properties->GetInteger(kWallpaperIndexNodeName, index);
447 std::string date_string;
448 int64 val;
449 if (!(wallpaper_properties->GetString(kWallpaperDateNodeName,
450 &date_string) &&
451 base::StringToInt64(date_string, &val)))
452 val = 0;
453 *last_modification_date = base::Time::FromInternalValue(val);
454 }
455 }
456 }
457
458 void WallpaperManager::GenerateUserWallpaperThumbnail(
459 const std::string& email,
460 User::WallpaperType type,
461 base::WeakPtr<WallpaperDelegate> delegate,
462 const SkBitmap& wallpaper) {
463 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
464 SkBitmap thumbnail =
465 skia::ImageOperations::Resize(wallpaper,
466 skia::ImageOperations::RESIZE_LANCZOS3,
467 kThumbnailWidth, kThumbnailHeight);
468
469 CustomWallpaperMap::iterator it =
Ivan Korotkov 2012/07/27 23:02:02 ditto
bshe 2012/07/31 14:27:54 Done.
470 custom_wallpaper_thumbnail_cache_.find(email);
471 if (it != custom_wallpaper_thumbnail_cache_.end())
472 it->second = thumbnail;
473 else
474 custom_wallpaper_thumbnail_cache_.insert(std::make_pair(email, thumbnail));
475
476 // Notify thumbnail is ready.
477 BrowserThread::PostTask(
478 BrowserThread::UI,
479 FROM_HERE,
480 base::Bind(&WallpaperManager::OnThumbnailUpdated,
481 base::Unretained(this), delegate));
482 }
483
484 void WallpaperManager::LoadCustomWallpaperFromFile(const std::string& email,
485 const LoadedCallback& loaded_cb) {
486 // TODO(bshe): Refactor GetWallpaperPathForUser to this class.
Ivan Korotkov 2012/07/27 23:02:02 Time to do it maybe? :) Or put it into UserManager
bshe 2012/07/31 14:27:54 Done.
487 std::string wallpaper_path =
488 static_cast<UserManagerImpl*>(UserManager::Get())->
489 GetWallpaperPathForUser(email, false).value();
490 // Load user wallpaper asynchronously.
491 wallpaper_loader_->Start(wallpaper_path, false, std::string(),
492 base::Bind(loaded_cb));
Ivan Korotkov 2012/07/27 23:02:02 Should be no need in empty Bind
bshe 2012/07/31 14:27:54 Done.
493 }
494
495 void WallpaperManager::OnThumbnailUpdated(
496 base::WeakPtr<WallpaperDelegate> delegate) {
497 if (delegate)
498 delegate->SetCustomWallpaperThumbnail();
499 }
500
501 void WallpaperManager::SetCallback(const std::string& username,
502 ash::WallpaperLayout layout,
503 User::WallpaperType type,
504 base::WeakPtr<WallpaperDelegate> delegate,
505 const SkBitmap& wallpaper) {
506 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
507
508 BrowserThread::PostTask(
509 BrowserThread::FILE,
510 FROM_HERE,
511 base::Bind(&WallpaperManager::GenerateUserWallpaperThumbnail,
512 base::Unretained(this), username, type, delegate, wallpaper));
513
514 ash::Shell::GetInstance()->desktop_background_controller()->
515 SetCustomWallpaper(wallpaper, layout);
516
517 BrowserThread::PostTask(
518 BrowserThread::UI,
519 FROM_HERE,
520 base::Bind(&WallpaperManager::SaveUserWallpaperProperties,
521 base::Unretained(this),
522 username, type, layout));
523 }
524
125 void WallpaperManager::SystemResumed() { 525 void WallpaperManager::SystemResumed() {
126 BatchUpdateWallpaper(); 526 BatchUpdateWallpaper();
127 } 527 }
128 528
129 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) { 529 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) {
130 RestartTimer(); 530 RestartTimer();
131 } 531 }
132 532
133 } // chromeos 533 } // chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698