OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/users/avatar/user_image_sync_observer.h" | 5 #include "chrome/browser/chromeos/login/users/avatar/user_image_sync_observer.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/prefs/pref_change_registrar.h" | 8 #include "base/prefs/pref_change_registrar.h" |
9 #include "base/prefs/scoped_user_pref_update.h" | 9 #include "base/prefs/scoped_user_pref_update.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
12 #include "chrome/browser/chromeos/login/screens/user_image_screen.h" | 12 #include "chrome/browser/chromeos/login/screens/user_image_screen.h" |
13 #include "chrome/browser/chromeos/login/users/avatar/user_image_manager.h" | 13 #include "chrome/browser/chromeos/login/users/avatar/user_image_manager.h" |
14 #include "chrome/browser/chromeos/login/users/user.h" | |
15 #include "chrome/browser/chromeos/login/wizard_controller.h" | 14 #include "chrome/browser/chromeos/login/wizard_controller.h" |
16 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 15 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
17 #include "chrome/browser/prefs/pref_service_syncable.h" | 16 #include "chrome/browser/prefs/pref_service_syncable.h" |
18 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/profiles/profile_manager.h" | 18 #include "chrome/browser/profiles/profile_manager.h" |
20 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 20 #include "components/user_manager/user.h" |
21 #include "components/user_manager/user_image/default_user_images.h" | 21 #include "components/user_manager/user_image/default_user_images.h" |
22 #include "content/public/browser/notification_registrar.h" | 22 #include "content/public/browser/notification_registrar.h" |
23 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
24 | 24 |
25 namespace chromeos { | 25 namespace chromeos { |
26 namespace { | 26 namespace { |
27 | 27 |
28 // A dictionary containing info about user image. | 28 // A dictionary containing info about user image. |
29 const char kUserImageInfo[] = "user_image_info"; | 29 const char kUserImageInfo[] = "user_image_info"; |
30 // Path to value with image index. | 30 // Path to value with image index. |
31 const char kImageIndex[] = "image_index"; | 31 const char kImageIndex[] = "image_index"; |
32 | 32 |
33 bool IsIndexSupported(int index) { | 33 bool IsIndexSupported(int index) { |
34 return (index >= user_manager::kFirstDefaultImageIndex && | 34 return (index >= user_manager::kFirstDefaultImageIndex && |
35 index < user_manager::kDefaultImagesCount) || | 35 index < user_manager::kDefaultImagesCount) || |
36 (index == User::kProfileImageIndex); | 36 (index == user_manager::User::USER_IMAGE_PROFILE); |
37 } | 37 } |
38 | 38 |
39 Profile* GetUserProfile() { | 39 Profile* GetUserProfile() { |
40 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 40 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
41 if (!profile_manager || !profile_manager->IsLoggedIn()) | 41 if (!profile_manager || !profile_manager->IsLoggedIn()) |
42 return NULL; | 42 return NULL; |
43 base::FilePath profile_path = profile_manager->user_data_dir().Append( | 43 base::FilePath profile_path = profile_manager->user_data_dir().Append( |
44 profile_manager->GetInitialProfileDir()); | 44 profile_manager->GetInitialProfileDir()); |
45 return profile_manager->GetProfileByPath(profile_path); | 45 return profile_manager->GetProfileByPath(profile_path); |
46 } | 46 } |
47 | 47 |
48 } // anonymous namespace | 48 } // anonymous namespace |
49 | 49 |
50 UserImageSyncObserver::Observer::~Observer() {} | 50 UserImageSyncObserver::Observer::~Observer() {} |
51 | 51 |
52 UserImageSyncObserver::UserImageSyncObserver(const User* user) | 52 UserImageSyncObserver::UserImageSyncObserver(const user_manager::User* user) |
53 : user_(user), | 53 : user_(user), |
54 prefs_(NULL), | 54 prefs_(NULL), |
55 is_synced_(false), | 55 is_synced_(false), |
56 local_image_changed_(false) { | 56 local_image_changed_(false) { |
57 notification_registrar_.reset(new content::NotificationRegistrar); | 57 notification_registrar_.reset(new content::NotificationRegistrar); |
58 notification_registrar_->Add(this, | 58 notification_registrar_->Add(this, |
59 chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED, | 59 chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED, |
60 content::NotificationService::AllSources()); | 60 content::NotificationService::AllSources()); |
61 Profile* profile = GetUserProfile(); | 61 Profile* profile = GetUserProfile(); |
62 if (profile) { | 62 if (profile) { |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 is_synced_ = prefs_->IsPrioritySyncing(); | 157 is_synced_ = prefs_->IsPrioritySyncing(); |
158 if (is_synced_) { | 158 if (is_synced_) { |
159 prefs_->RemoveObserver(this); | 159 prefs_->RemoveObserver(this); |
160 OnInitialSync(); | 160 OnInitialSync(); |
161 } | 161 } |
162 } | 162 } |
163 | 163 |
164 void UserImageSyncObserver::UpdateSyncedImageFromLocal() { | 164 void UserImageSyncObserver::UpdateSyncedImageFromLocal() { |
165 int local_index = user_->image_index(); | 165 int local_index = user_->image_index(); |
166 if (!IsIndexSupported(local_index)) { | 166 if (!IsIndexSupported(local_index)) { |
167 local_index = User::kInvalidImageIndex; | 167 local_index = user_manager::User::USER_IMAGE_INVALID; |
168 } | 168 } |
169 int synced_index; | 169 int synced_index; |
170 if (GetSyncedImageIndex(&synced_index) && (synced_index == local_index)) | 170 if (GetSyncedImageIndex(&synced_index) && (synced_index == local_index)) |
171 return; | 171 return; |
172 DictionaryPrefUpdate update(prefs_, kUserImageInfo); | 172 DictionaryPrefUpdate update(prefs_, kUserImageInfo); |
173 base::DictionaryValue* dict = update.Get(); | 173 base::DictionaryValue* dict = update.Get(); |
174 dict->SetInteger(kImageIndex, local_index); | 174 dict->SetInteger(kImageIndex, local_index); |
175 VLOG(1) << "Saved avatar index " << local_index << " to sync."; | 175 VLOG(1) << "Saved avatar index " << local_index << " to sync."; |
176 } | 176 } |
177 | 177 |
178 void UserImageSyncObserver::UpdateLocalImageFromSynced() { | 178 void UserImageSyncObserver::UpdateLocalImageFromSynced() { |
179 int synced_index; | 179 int synced_index; |
180 GetSyncedImageIndex(&synced_index); | 180 GetSyncedImageIndex(&synced_index); |
181 int local_index = user_->image_index(); | 181 int local_index = user_->image_index(); |
182 if ((synced_index == local_index) || !IsIndexSupported(synced_index)) | 182 if ((synced_index == local_index) || !IsIndexSupported(synced_index)) |
183 return; | 183 return; |
184 UserImageManager* image_manager = | 184 UserImageManager* image_manager = |
185 UserManager::Get()->GetUserImageManager(user_->email()); | 185 UserManager::Get()->GetUserImageManager(user_->email()); |
186 if (synced_index == User::kProfileImageIndex) { | 186 if (synced_index == user_manager::User::USER_IMAGE_PROFILE) { |
187 image_manager->SaveUserImageFromProfileImage(); | 187 image_manager->SaveUserImageFromProfileImage(); |
188 } else { | 188 } else { |
189 image_manager->SaveUserDefaultImageIndex(synced_index); | 189 image_manager->SaveUserDefaultImageIndex(synced_index); |
190 } | 190 } |
191 VLOG(1) << "Loaded avatar index " << synced_index << " from sync."; | 191 VLOG(1) << "Loaded avatar index " << synced_index << " from sync."; |
192 } | 192 } |
193 | 193 |
194 bool UserImageSyncObserver::GetSyncedImageIndex(int* index) { | 194 bool UserImageSyncObserver::GetSyncedImageIndex(int* index) { |
195 *index = User::kInvalidImageIndex; | 195 *index = user_manager::User::USER_IMAGE_INVALID; |
196 const base::DictionaryValue* dict = prefs_->GetDictionary(kUserImageInfo); | 196 const base::DictionaryValue* dict = prefs_->GetDictionary(kUserImageInfo); |
197 return dict && dict->GetInteger(kImageIndex, index); | 197 return dict && dict->GetInteger(kImageIndex, index); |
198 } | 198 } |
199 | 199 |
200 bool UserImageSyncObserver::CanUpdateLocalImageNow() { | 200 bool UserImageSyncObserver::CanUpdateLocalImageNow() { |
201 if (WizardController* wizard_controller = | 201 if (WizardController* wizard_controller = |
202 WizardController::default_controller()) { | 202 WizardController::default_controller()) { |
203 UserImageScreen* screen = wizard_controller->GetUserImageScreen(); | 203 UserImageScreen* screen = wizard_controller->GetUserImageScreen(); |
204 if (wizard_controller->current_screen() == screen) { | 204 if (wizard_controller->current_screen() == screen) { |
205 if (screen->user_selected_image()) | 205 if (screen->user_selected_image()) |
206 return false; | 206 return false; |
207 } | 207 } |
208 } | 208 } |
209 return true; | 209 return true; |
210 } | 210 } |
211 | 211 |
212 } // namespace chromeos | 212 } // namespace chromeos |
213 | 213 |
OLD | NEW |