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" | 8 #include "ash/desktop_background/desktop_background_resources.h" |
9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/string_number_conversions.h" |
11 #include "base/time.h" | 12 #include "base/time.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/chromeos/cros_settings.h" | 15 #include "chrome/browser/chromeos/cros_settings.h" |
15 #include "chrome/browser/chromeos/login/user.h" | |
16 #include "chrome/browser/chromeos/login/user_manager.h" | 16 #include "chrome/browser/chromeos/login/user_manager.h" |
17 #include "chrome/browser/chromeos/login/user_manager_impl.h" | 17 #include "chrome/browser/chromeos/login/user_manager_impl.h" |
18 #include "chrome/browser/prefs/pref_service.h" | 18 #include "chrome/browser/prefs/pref_service.h" |
| 19 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
19 #include "chromeos/dbus/dbus_thread_manager.h" | 20 #include "chromeos/dbus/dbus_thread_manager.h" |
20 #include "chromeos/dbus/power_manager_client.h" | 21 #include "chromeos/dbus/power_manager_client.h" |
| 22 #include "content/public/browser/browser_thread.h" |
| 23 |
| 24 using content::BrowserThread; |
21 | 25 |
22 namespace { | 26 namespace { |
| 27 |
23 const int kWallpaperUpdateIntervalSec = 24 * 60 * 60; | 28 const int kWallpaperUpdateIntervalSec = 24 * 60 * 60; |
| 29 |
| 30 // A dictionary that maps usernames to wallpaper properties. |
| 31 const char kUsersWallpaperInfo[] = "UsersWallpaperInfo"; |
| 32 |
| 33 // Names of nodes with info about wallpaper. |
| 34 const char kWallpaperDateNodeName[] = "date"; |
| 35 const char kWallpaperLayoutNodeName[] = "layout"; |
| 36 const char kWallpaperFileNodeName[] = "file"; |
| 37 const char kWallpaperTypeNodeName[] = "type"; |
| 38 |
24 } // namespace | 39 } // namespace |
25 | 40 |
26 namespace chromeos { | 41 namespace chromeos { |
27 | 42 |
28 static WallpaperManager* g_wallpaper_manager = NULL; | 43 static WallpaperManager* g_wallpaper_manager = NULL; |
29 | 44 |
30 // WallpaperManager, public: --------------------------------------------------- | 45 // WallpaperManager, public: --------------------------------------------------- |
31 | 46 |
32 // static | 47 // static |
33 WallpaperManager* WallpaperManager::Get() { | 48 WallpaperManager* WallpaperManager::Get() { |
34 if (!g_wallpaper_manager) | 49 if (!g_wallpaper_manager) |
35 g_wallpaper_manager = new WallpaperManager(); | 50 g_wallpaper_manager = new WallpaperManager(); |
36 return g_wallpaper_manager; | 51 return g_wallpaper_manager; |
37 } | 52 } |
38 | 53 |
| 54 // static |
| 55 void WallpaperManager::RegisterPrefs(PrefService* local_state) { |
| 56 local_state->RegisterDictionaryPref(kUsersWallpaperInfo, |
| 57 PrefService::UNSYNCABLE_PREF); |
| 58 } |
| 59 |
39 WallpaperManager::WallpaperManager() : last_selected_user_("") { | 60 WallpaperManager::WallpaperManager() : last_selected_user_("") { |
40 system::TimezoneSettings::GetInstance()->AddObserver(this); | 61 system::TimezoneSettings::GetInstance()->AddObserver(this); |
41 RestartTimer(); | 62 RestartTimer(); |
42 } | 63 } |
43 | 64 |
44 void WallpaperManager::AddPowerManagerClientObserver() { | 65 void WallpaperManager::AddPowerManagerClientObserver() { |
45 if (!DBusThreadManager::Get()->GetPowerManagerClient()->HasObserver(this)) | 66 if (!DBusThreadManager::Get()->GetPowerManagerClient()->HasObserver(this)) |
46 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this); | 67 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this); |
47 } | 68 } |
48 | 69 |
49 void WallpaperManager::RestartTimer() { | 70 void WallpaperManager::RestartTimer() { |
50 timer_.Stop(); | 71 timer_.Stop(); |
51 base::Time midnight = base::Time::Now().LocalMidnight(); | 72 base::Time midnight = base::Time::Now().LocalMidnight(); |
52 base::TimeDelta interval = base::Time::Now() - midnight; | 73 base::TimeDelta interval = base::Time::Now() - midnight; |
53 int64 remaining_seconds = kWallpaperUpdateIntervalSec - interval.InSeconds(); | 74 int64 remaining_seconds = kWallpaperUpdateIntervalSec - interval.InSeconds(); |
54 if (remaining_seconds <= 0) { | 75 if (remaining_seconds <= 0) { |
55 BatchUpdateWallpaper(); | 76 BatchUpdateWallpaper(); |
56 } else { | 77 } else { |
57 // Set up a one shot timer which will batch update wallpaper at midnight. | 78 // Set up a one shot timer which will batch update wallpaper at midnight. |
58 timer_.Start(FROM_HERE, | 79 timer_.Start(FROM_HERE, |
59 base::TimeDelta::FromSeconds(remaining_seconds), | 80 base::TimeDelta::FromSeconds(remaining_seconds), |
60 this, | 81 this, |
61 &WallpaperManager::BatchUpdateWallpaper); | 82 &WallpaperManager::BatchUpdateWallpaper); |
62 } | 83 } |
63 } | 84 } |
64 | 85 |
| 86 void WallpaperManager::SaveUserWallpaperInfo(const std::string& username, |
| 87 const std::string& file_name, |
| 88 ash::WallpaperLayout layout, |
| 89 User::WallpaperType type) { |
| 90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 91 |
| 92 // Ephemeral users can not save data to local state. We just cache the index |
| 93 // in memory for them. |
| 94 if (UserManager::Get()->IsCurrentUserEphemeral()) |
| 95 return; |
| 96 |
| 97 PrefService* local_state = g_browser_process->local_state(); |
| 98 DictionaryPrefUpdate wallpaper_update(local_state, |
| 99 kUsersWallpaperInfo); |
| 100 |
| 101 base::DictionaryValue* wallpaper_properties = new base::DictionaryValue(); |
| 102 wallpaper_properties->SetString(kWallpaperDateNodeName, |
| 103 base::Int64ToString(base::Time::Now().LocalMidnight().ToInternalValue())); |
| 104 wallpaper_properties->SetString(kWallpaperFileNodeName, |
| 105 file_name); |
| 106 wallpaper_properties->Set(kWallpaperLayoutNodeName, |
| 107 new base::FundamentalValue(layout)); |
| 108 wallpaper_properties->Set(kWallpaperTypeNodeName, |
| 109 new base::FundamentalValue(type)); |
| 110 wallpaper_update->SetWithoutPathExpansion(username, wallpaper_properties); |
| 111 } |
| 112 |
65 void WallpaperManager::SetLastSelectedUser( | 113 void WallpaperManager::SetLastSelectedUser( |
66 const std::string& last_selected_user) { | 114 const std::string& last_selected_user) { |
67 last_selected_user_ = last_selected_user; | 115 last_selected_user_ = last_selected_user; |
68 } | 116 } |
69 | 117 |
70 void WallpaperManager::SetWallpaperFromFile(std::string email, | 118 void WallpaperManager::SetWallpaperFromFilePath(const std::string& path, |
71 const std::string& path, | 119 ash::WallpaperLayout layout) { |
72 ash::WallpaperLayout layout) { | |
73 image_loader_->Start( | 120 image_loader_->Start( |
74 path, 0, false, | 121 path, 0, false, |
75 base::Bind(&WallpaperManager::OnCustomWallpaperLoaded, | 122 base::Bind(&WallpaperManager::OnWallpaperLoaded, |
76 base::Unretained(this), email, layout)); | 123 base::Unretained(this), layout)); |
| 124 } |
| 125 |
| 126 void WallpaperManager::SetWallpaperFromImageSkia( |
| 127 const gfx::ImageSkia& wallpaper, |
| 128 ash::WallpaperLayout layout) { |
| 129 ash::Shell::GetInstance()->desktop_background_controller()-> |
| 130 SetCustomWallpaper(wallpaper, layout); |
77 } | 131 } |
78 | 132 |
79 void WallpaperManager::UserDeselected() { | 133 void WallpaperManager::UserDeselected() { |
80 if (!UserManager::Get()->IsUserLoggedIn()) { | 134 if (!UserManager::Get()->IsUserLoggedIn()) { |
81 // This will set default login wallpaper (#fefefe). | 135 // This will set default login wallpaper (#fefefe). |
82 ash::Shell::GetInstance()->desktop_background_controller()-> | 136 ash::Shell::GetInstance()->desktop_background_controller()-> |
83 SetDefaultWallpaper(ash::GetSolidColorIndex()); | 137 SetDefaultWallpaper(ash::GetSolidColorIndex()); |
84 } | 138 } |
85 } | 139 } |
86 | 140 |
87 // WallpaperManager, private: -------------------------------------------------- | 141 // WallpaperManager, private: -------------------------------------------------- |
88 | 142 |
89 WallpaperManager::~WallpaperManager() { | 143 WallpaperManager::~WallpaperManager() { |
90 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this); | 144 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this); |
91 system::TimezoneSettings::GetInstance()->RemoveObserver(this); | 145 system::TimezoneSettings::GetInstance()->RemoveObserver(this); |
92 } | 146 } |
93 | 147 |
94 void WallpaperManager::OnCustomWallpaperLoaded(const std::string& email, | |
95 ash::WallpaperLayout layout, | |
96 const UserImage& user_image) { | |
97 const SkBitmap& wallpaper = user_image.image(); | |
98 ash::Shell::GetInstance()->desktop_background_controller()-> | |
99 SetCustomWallpaper(wallpaper, layout); | |
100 } | |
101 | |
102 void WallpaperManager::BatchUpdateWallpaper() { | 148 void WallpaperManager::BatchUpdateWallpaper() { |
103 PrefService* local_state = g_browser_process->local_state(); | 149 PrefService* local_state = g_browser_process->local_state(); |
104 UserManager* user_manager = UserManager::Get(); | 150 UserManager* user_manager = UserManager::Get(); |
105 bool show_users = true; | 151 bool show_users = true; |
106 CrosSettings::Get()->GetBoolean( | 152 CrosSettings::Get()->GetBoolean( |
107 kAccountsPrefShowUserNamesOnSignIn, &show_users); | 153 kAccountsPrefShowUserNamesOnSignIn, &show_users); |
108 if (local_state) { | 154 if (local_state) { |
109 User::WallpaperType type; | 155 User::WallpaperType type; |
110 int index = 0; | 156 int index = 0; |
111 base::Time last_modification_date; | 157 base::Time last_modification_date; |
(...skipping 20 matching lines...) Expand all Loading... |
132 user_manager->UserSelected(email); | 178 user_manager->UserSelected(email); |
133 } else if (show_users && | 179 } else if (show_users && |
134 email == last_selected_user_) { | 180 email == last_selected_user_) { |
135 user_manager->UserSelected(email); | 181 user_manager->UserSelected(email); |
136 } | 182 } |
137 } | 183 } |
138 } | 184 } |
139 RestartTimer(); | 185 RestartTimer(); |
140 } | 186 } |
141 | 187 |
| 188 void WallpaperManager::OnWallpaperLoaded(ash::WallpaperLayout layout, |
| 189 const UserImage& user_image) { |
| 190 const SkBitmap& wallpaper = user_image.image(); |
| 191 SetWallpaperFromImageSkia(wallpaper, layout); |
| 192 } |
| 193 |
142 void WallpaperManager::SystemResumed() { | 194 void WallpaperManager::SystemResumed() { |
143 BatchUpdateWallpaper(); | 195 BatchUpdateWallpaper(); |
144 } | 196 } |
145 | 197 |
146 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) { | 198 void WallpaperManager::TimezoneChanged(const icu::TimeZone& timezone) { |
147 RestartTimer(); | 199 RestartTimer(); |
148 } | 200 } |
149 | 201 |
150 } // chromeos | 202 } // chromeos |
OLD | NEW |