Chromium Code Reviews| 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_manager_impl.h" | 5 #include "chrome/browser/chromeos/login/users/avatar/user_image_manager_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 // image. | 232 // image. |
| 233 void SetToPath(const base::FilePath& path, | 233 void SetToPath(const base::FilePath& path, |
| 234 int image_index, | 234 int image_index, |
| 235 const GURL& image_url, | 235 const GURL& image_url, |
| 236 bool resize); | 236 bool resize); |
| 237 | 237 |
| 238 private: | 238 private: |
| 239 // Called back after an image has been loaded from disk. | 239 // Called back after an image has been loaded from disk. |
| 240 void OnLoadImageDone(bool save, const user_manager::UserImage& user_image); | 240 void OnLoadImageDone(bool save, const user_manager::UserImage& user_image); |
| 241 | 241 |
| 242 // Updates the user object with |user_image_|. | 242 // Updates the user object with |user_image|. |
| 243 void UpdateUser(); | 243 void UpdateUser(const user_manager::UserImage& user_image); |
| 244 | 244 |
| 245 // Saves |user_image_| to disk in JPEG format. Local state will be updated | 245 // Saves |user_image_| to disk in JPEG format. Local state will be updated |
| 246 // when a callback indicates that the image has been saved. | 246 // when a callback indicates that the image has been saved. |
| 247 void SaveImageAndUpdateLocalState(); | 247 void SaveImageAndUpdateLocalState(const user_manager::UserImage& user_image); |
| 248 | 248 |
| 249 // Called back after the |user_image_| has been saved to | 249 // Called back after the user image has been saved to |
| 250 // disk. Updates the user image information in local state. The | 250 // disk. Updates the user image information in local state. The |
| 251 // information is only updated if |success| is true (indicating that | 251 // information is only updated if |success| is true (indicating that |
| 252 // the image was saved successfully) or the user image is the | 252 // the image was saved successfully) or the user image is the |
| 253 // profile image (indicating that even if the image could not be | 253 // profile image (indicating that even if the image could not be |
| 254 // saved because it is not available right now, it will be | 254 // saved because it is not available right now, it will be |
| 255 // downloaded eventually). | 255 // downloaded eventually). |
| 256 void OnSaveImageDone(bool success); | 256 void OnSaveImageDone(bool success); |
| 257 | 257 |
| 258 // Updates the user image in local state, setting it to one of the | 258 // Updates the user image in local state, setting it to one of the |
| 259 // default images or the saved |user_image_|, depending on | 259 // default images or the saved user image, depending on |
| 260 // |image_index_|. | 260 // |image_index_|. |
| 261 void UpdateLocalState(); | 261 void UpdateLocalState(); |
| 262 | 262 |
| 263 // Notifies the |parent_| that the Job is done. | 263 // Notifies the |parent_| that the Job is done. |
| 264 void NotifyJobDone(); | 264 void NotifyJobDone(); |
| 265 | 265 |
| 266 const std::string& user_id() const { return parent_->user_id(); } | 266 const std::string& user_id() const { return parent_->user_id(); } |
| 267 | 267 |
| 268 UserImageManagerImpl* parent_; | 268 UserImageManagerImpl* parent_; |
| 269 | 269 |
| 270 // Whether one of the Load*() or Set*() methods has been run already. | 270 // Whether one of the Load*() or Set*() methods has been run already. |
| 271 bool run_; | 271 bool run_; |
| 272 | 272 |
| 273 int image_index_; | 273 int image_index_; |
| 274 GURL image_url_; | 274 GURL image_url_; |
| 275 base::FilePath image_path_; | 275 base::FilePath image_path_; |
| 276 | 276 |
| 277 user_manager::UserImage user_image_; | |
| 278 | |
| 279 base::WeakPtrFactory<Job> weak_factory_; | 277 base::WeakPtrFactory<Job> weak_factory_; |
| 280 | 278 |
| 281 DISALLOW_COPY_AND_ASSIGN(Job); | 279 DISALLOW_COPY_AND_ASSIGN(Job); |
| 282 }; | 280 }; |
| 283 | 281 |
| 284 UserImageManagerImpl::Job::Job(UserImageManagerImpl* parent) | 282 UserImageManagerImpl::Job::Job(UserImageManagerImpl* parent) |
| 285 : parent_(parent), | 283 : parent_(parent), |
| 286 run_(false), | 284 run_(false), |
| 287 weak_factory_(this) { | 285 weak_factory_(this) { |
| 288 } | 286 } |
| 289 | 287 |
| 290 UserImageManagerImpl::Job::~Job() { | 288 UserImageManagerImpl::Job::~Job() { |
| 291 } | 289 } |
| 292 | 290 |
| 293 void UserImageManagerImpl::Job::LoadImage(base::FilePath image_path, | 291 void UserImageManagerImpl::Job::LoadImage(base::FilePath image_path, |
| 294 const int image_index, | 292 const int image_index, |
| 295 const GURL& image_url) { | 293 const GURL& image_url) { |
| 296 DCHECK(!run_); | 294 DCHECK(!run_); |
| 297 run_ = true; | 295 run_ = true; |
| 298 | 296 |
| 299 image_index_ = image_index; | 297 image_index_ = image_index; |
| 300 image_url_ = image_url; | 298 image_url_ = image_url; |
| 301 image_path_ = image_path; | 299 image_path_ = image_path; |
| 302 | 300 |
| 303 if (image_index_ >= 0 && | 301 if (image_index_ >= 0 && |
| 304 image_index_ < default_user_image::kDefaultImagesCount) { | 302 image_index_ < default_user_image::kDefaultImagesCount) { |
| 305 // Load one of the default images. This happens synchronously. | 303 // Load one of the default images. This happens synchronously. |
| 306 user_image_ = user_manager::UserImage( | 304 const user_manager::UserImage user_image( |
| 307 default_user_image::GetDefaultImage(image_index_)); | 305 default_user_image::GetDefaultImage(image_index_)); |
| 308 UpdateUser(); | 306 UpdateUser(user_image); |
| 309 NotifyJobDone(); | 307 NotifyJobDone(); |
| 310 } else if (image_index_ == user_manager::User::USER_IMAGE_EXTERNAL || | 308 } else if (image_index_ == user_manager::User::USER_IMAGE_EXTERNAL || |
| 311 image_index_ == user_manager::User::USER_IMAGE_PROFILE) { | 309 image_index_ == user_manager::User::USER_IMAGE_PROFILE) { |
| 312 // Load the user image from a file referenced by |image_path|. This happens | 310 // Load the user image from a file referenced by |image_path|. This happens |
| 313 // asynchronously. ROBUST_JPEG_CODEC can be used here because | 311 // asynchronously. ROBUST_JPEG_CODEC can be used here because |
| 314 // LoadImage() is called only for users whose user image has previously | 312 // LoadImage() is called only for users whose user image has previously |
| 315 // been set by one of the Set*() methods, which transcode to JPEG format. | 313 // been set by one of the Set*() methods, which transcode to JPEG format. |
| 316 DCHECK(!image_path_.empty()); | 314 DCHECK(!image_path_.empty()); |
| 317 user_image_loader::StartWithFilePath( | 315 user_image_loader::StartWithFilePath( |
| 318 parent_->background_task_runner_, image_path_, | 316 parent_->background_task_runner_, image_path_, |
| 319 ImageDecoder::ROBUST_JPEG_CODEC, | 317 ImageDecoder::ROBUST_JPEG_CODEC, |
| 320 0, // Do not crop. | 318 0, // Do not crop. |
| 321 base::Bind(&Job::OnLoadImageDone, weak_factory_.GetWeakPtr(), false)); | 319 base::Bind(&Job::OnLoadImageDone, weak_factory_.GetWeakPtr(), false)); |
| 322 } else { | 320 } else { |
| 323 NOTREACHED(); | 321 NOTREACHED(); |
| 324 NotifyJobDone(); | 322 NotifyJobDone(); |
| 325 } | 323 } |
| 326 } | 324 } |
| 327 | 325 |
| 328 void UserImageManagerImpl::Job::SetToDefaultImage(int default_image_index) { | 326 void UserImageManagerImpl::Job::SetToDefaultImage(int default_image_index) { |
| 329 DCHECK(!run_); | 327 DCHECK(!run_); |
| 330 run_ = true; | 328 run_ = true; |
| 331 | 329 |
| 332 DCHECK_LE(0, default_image_index); | 330 DCHECK_LE(0, default_image_index); |
| 333 DCHECK_GT(default_user_image::kDefaultImagesCount, default_image_index); | 331 DCHECK_GT(default_user_image::kDefaultImagesCount, default_image_index); |
| 334 | 332 |
| 335 image_index_ = default_image_index; | 333 image_index_ = default_image_index; |
| 336 user_image_ = user_manager::UserImage( | 334 const user_manager::UserImage user_image( |
| 337 default_user_image::GetDefaultImage(image_index_)); | 335 default_user_image::GetDefaultImage(image_index_)); |
| 338 | 336 |
| 339 UpdateUser(); | 337 UpdateUser(user_image); |
| 340 UpdateLocalState(); | 338 UpdateLocalState(); |
| 341 NotifyJobDone(); | 339 NotifyJobDone(); |
| 342 } | 340 } |
| 343 | 341 |
| 344 void UserImageManagerImpl::Job::SetToImage( | 342 void UserImageManagerImpl::Job::SetToImage( |
| 345 int image_index, | 343 int image_index, |
| 346 const user_manager::UserImage& user_image) { | 344 const user_manager::UserImage& user_image) { |
| 347 DCHECK(!run_); | 345 DCHECK(!run_); |
| 348 run_ = true; | 346 run_ = true; |
| 349 | 347 |
| 350 DCHECK(image_index == user_manager::User::USER_IMAGE_EXTERNAL || | 348 DCHECK(image_index == user_manager::User::USER_IMAGE_EXTERNAL || |
| 351 image_index == user_manager::User::USER_IMAGE_PROFILE); | 349 image_index == user_manager::User::USER_IMAGE_PROFILE); |
| 352 | 350 |
| 353 image_index_ = image_index; | 351 image_index_ = image_index; |
| 354 user_image_ = user_image; | |
| 355 | 352 |
| 356 UpdateUser(); | 353 UpdateUser(user_image); |
| 357 SaveImageAndUpdateLocalState(); | 354 SaveImageAndUpdateLocalState(user_image); |
| 358 } | 355 } |
| 359 | 356 |
| 360 void UserImageManagerImpl::Job::SetToImageData(scoped_ptr<std::string> data) { | 357 void UserImageManagerImpl::Job::SetToImageData(scoped_ptr<std::string> data) { |
| 361 DCHECK(!run_); | 358 DCHECK(!run_); |
| 362 run_ = true; | 359 run_ = true; |
| 363 | 360 |
| 364 image_index_ = user_manager::User::USER_IMAGE_EXTERNAL; | 361 image_index_ = user_manager::User::USER_IMAGE_EXTERNAL; |
| 365 | 362 |
| 366 // This method uses ROBUST_JPEG_CODEC, not DEFAULT_CODEC: | 363 // This method uses ROBUST_JPEG_CODEC, not DEFAULT_CODEC: |
| 367 // * This is necessary because the method is used to update the user image | 364 // * This is necessary because the method is used to update the user image |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 392 DCHECK(!path.empty()); | 389 DCHECK(!path.empty()); |
| 393 user_image_loader::StartWithFilePath( | 390 user_image_loader::StartWithFilePath( |
| 394 parent_->background_task_runner_, path, ImageDecoder::DEFAULT_CODEC, | 391 parent_->background_task_runner_, path, ImageDecoder::DEFAULT_CODEC, |
| 395 resize ? login::kMaxUserImageSize : 0, | 392 resize ? login::kMaxUserImageSize : 0, |
| 396 base::Bind(&Job::OnLoadImageDone, weak_factory_.GetWeakPtr(), true)); | 393 base::Bind(&Job::OnLoadImageDone, weak_factory_.GetWeakPtr(), true)); |
| 397 } | 394 } |
| 398 | 395 |
| 399 void UserImageManagerImpl::Job::OnLoadImageDone( | 396 void UserImageManagerImpl::Job::OnLoadImageDone( |
| 400 bool save, | 397 bool save, |
| 401 const user_manager::UserImage& user_image) { | 398 const user_manager::UserImage& user_image) { |
| 402 user_image_ = user_image; | 399 UpdateUser(user_image); |
| 403 UpdateUser(); | |
| 404 if (save) | 400 if (save) |
| 405 SaveImageAndUpdateLocalState(); | 401 SaveImageAndUpdateLocalState(user_image); |
| 406 else | 402 else |
| 407 NotifyJobDone(); | 403 NotifyJobDone(); |
| 408 } | 404 } |
| 409 | 405 |
| 410 void UserImageManagerImpl::Job::UpdateUser() { | 406 void UserImageManagerImpl::Job::UpdateUser( |
| 407 const user_manager::UserImage& user_image) { | |
| 411 user_manager::User* user = parent_->GetUserAndModify(); | 408 user_manager::User* user = parent_->GetUserAndModify(); |
| 412 if (!user) | 409 if (!user) |
| 413 return; | 410 return; |
| 414 | 411 |
| 415 if (!user_image_.image().isNull()) { | 412 if (!user_image.image().isNull()) { |
| 416 user->SetImage(user_image_, image_index_); | 413 user->SetImage(user_image, image_index_); |
| 417 } else { | 414 } else { |
| 418 user->SetStubImage( | 415 user->SetStubImage( |
| 419 user_manager::UserImage( | 416 user_manager::UserImage( |
| 420 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 417 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 421 IDR_PROFILE_PICTURE_LOADING)), | 418 IDR_PROFILE_PICTURE_LOADING)), |
| 422 image_index_, | 419 image_index_, |
| 423 false); | 420 false); |
| 424 } | 421 } |
| 425 user->SetImageURL(image_url_); | 422 user->SetImageURL(image_url_); |
| 426 | 423 |
| 427 parent_->OnJobChangedUserImage(); | 424 parent_->OnJobChangedUserImage(); |
| 428 } | 425 } |
| 429 | 426 |
| 430 void UserImageManagerImpl::Job::SaveImageAndUpdateLocalState() { | 427 void UserImageManagerImpl::Job::SaveImageAndUpdateLocalState( |
| 428 const user_manager::UserImage& user_image) { | |
| 431 base::FilePath user_data_dir; | 429 base::FilePath user_data_dir; |
| 432 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | 430 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 433 image_path_ = user_data_dir.Append(user_id() + kSafeImagePathExtension); | 431 image_path_ = user_data_dir.Append(user_id() + kSafeImagePathExtension); |
| 434 | 432 |
| 435 base::PostTaskAndReplyWithResult( | 433 base::PostTaskAndReplyWithResult( |
| 436 parent_->background_task_runner_.get(), | 434 parent_->background_task_runner_.get(), FROM_HERE, |
| 437 FROM_HERE, | 435 base::Bind(&SaveImage, user_image, image_path_), |
| 438 base::Bind(&SaveImage, user_image_, image_path_), | |
| 439 base::Bind(&Job::OnSaveImageDone, weak_factory_.GetWeakPtr())); | 436 base::Bind(&Job::OnSaveImageDone, weak_factory_.GetWeakPtr())); |
| 440 } | 437 } |
| 441 | 438 |
| 442 void UserImageManagerImpl::Job::OnSaveImageDone(bool success) { | 439 void UserImageManagerImpl::Job::OnSaveImageDone(bool success) { |
| 443 if (success || image_index_ == user_manager::User::USER_IMAGE_PROFILE) | 440 if (success || image_index_ == user_manager::User::USER_IMAGE_PROFILE) |
| 444 UpdateLocalState(); | 441 UpdateLocalState(); |
| 445 NotifyJobDone(); | 442 NotifyJobDone(); |
| 446 } | 443 } |
| 447 | 444 |
| 448 void UserImageManagerImpl::Job::UpdateLocalState() { | 445 void UserImageManagerImpl::Job::UpdateLocalState() { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 538 image_index, | 535 image_index, |
| 539 true); | 536 true); |
| 540 DCHECK(!image_path.empty() || | 537 DCHECK(!image_path.empty() || |
| 541 image_index == user_manager::User::USER_IMAGE_PROFILE); | 538 image_index == user_manager::User::USER_IMAGE_PROFILE); |
| 542 if (image_path.empty()) { | 539 if (image_path.empty()) { |
| 543 // Return if the profile image is to be used but has not been downloaded | 540 // Return if the profile image is to be used but has not been downloaded |
| 544 // yet. The profile image will be downloaded after login. | 541 // yet. The profile image will be downloaded after login. |
| 545 return; | 542 return; |
| 546 } | 543 } |
| 547 | 544 |
| 548 job_.reset(new Job(this)); | 545 job_.reset(new Job(this)); |
|
satorux1
2016/03/14 03:54:40
BTW, the Job class looks rather awkward to me. I t
hashimoto
2016/03/14 05:59:16
I agree the design of the class is not pretty, but
satorux1
2016/03/14 06:10:26
That's a good point. I'll keep the code as is for
| |
| 549 job_->LoadImage(base::FilePath(image_path), image_index, image_url); | 546 job_->LoadImage(base::FilePath(image_path), image_index, image_url); |
| 550 } | 547 } |
| 551 | 548 |
| 552 void UserImageManagerImpl::UserLoggedIn(bool user_is_new, | 549 void UserImageManagerImpl::UserLoggedIn(bool user_is_new, |
| 553 bool user_is_local) { | 550 bool user_is_local) { |
| 554 const user_manager::User* user = GetUser(); | 551 const user_manager::User* user = GetUser(); |
| 555 if (user_is_new) { | 552 if (user_is_new) { |
| 556 if (!user_is_local) | 553 if (!user_is_local) |
| 557 SetInitialUserImage(); | 554 SetInitialUserImage(); |
| 558 } else { | 555 } else { |
| (...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 933 } | 930 } |
| 934 | 931 |
| 935 bool UserImageManagerImpl::IsUserLoggedInAndHasGaiaAccount() const { | 932 bool UserImageManagerImpl::IsUserLoggedInAndHasGaiaAccount() const { |
| 936 const user_manager::User* user = GetUser(); | 933 const user_manager::User* user = GetUser(); |
| 937 if (!user) | 934 if (!user) |
| 938 return false; | 935 return false; |
| 939 return user->is_logged_in() && user->HasGaiaAccount(); | 936 return user->is_logged_in() && user->HasGaiaAccount(); |
| 940 } | 937 } |
| 941 | 938 |
| 942 } // namespace chromeos | 939 } // namespace chromeos |
| OLD | NEW |