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

Side by Side Diff: chrome/browser/chromeos/login/users/avatar/user_image_manager_impl.cc

Issue 2517053004: Use base::RefCountedBytes in user_manager::UserImage (Closed)
Patch Set: remove unnecessary base::move Created 4 years 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
OLDNEW
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 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 case user_manager::User::USER_IMAGE_EXTERNAL: 142 case user_manager::User::USER_IMAGE_EXTERNAL:
143 // TODO(ivankr): Distinguish this from selected from file. 143 // TODO(ivankr): Distinguish this from selected from file.
144 return default_user_image::kHistogramImageFromCamera; 144 return default_user_image::kHistogramImageFromCamera;
145 case user_manager::User::USER_IMAGE_PROFILE: 145 case user_manager::User::USER_IMAGE_PROFILE:
146 return default_user_image::kHistogramImageFromProfile; 146 return default_user_image::kHistogramImageFromProfile;
147 default: 147 default:
148 return image_index; 148 return image_index;
149 } 149 }
150 } 150 }
151 151
152 bool SaveImage(std::unique_ptr<user_manager::UserImage::Bytes> image_bytes, 152 bool SaveImage(scoped_refptr<base::RefCountedBytes> image_bytes,
153 const base::FilePath& image_path) { 153 const base::FilePath& image_path) {
154 if (image_bytes->empty() || 154 if (image_bytes->size() == 0 ||
155 base::WriteFile(image_path, 155 base::WriteFile(image_path,
156 reinterpret_cast<const char*>(image_bytes->data()), 156 reinterpret_cast<const char*>(image_bytes->front()),
157 image_bytes->size()) == -1) { 157 image_bytes->size()) == -1) {
158 LOG(ERROR) << "Failed to save image to file."; 158 LOG(ERROR) << "Failed to save image to file.";
159 return false; 159 return false;
160 } 160 }
161 161
162 return true; 162 return true;
163 } 163 }
164 164
165 } // namespace 165 } // namespace
166 166
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 230
231 // Updates the user object with |user_image|, and saves the image 231 // Updates the user object with |user_image|, and saves the image
232 // bytes. Local state will be updated as needed. 232 // bytes. Local state will be updated as needed.
233 void UpdateUserAndSaveImage( 233 void UpdateUserAndSaveImage(
234 std::unique_ptr<user_manager::UserImage> user_image); 234 std::unique_ptr<user_manager::UserImage> user_image);
235 235
236 // Saves |image_bytes| to disk in JPEG format if 236 // Saves |image_bytes| to disk in JPEG format if
237 // |image_is_safe_format|. Local state will be updated as needed. 237 // |image_is_safe_format|. Local state will be updated as needed.
238 void SaveImageAndUpdateLocalState( 238 void SaveImageAndUpdateLocalState(
239 bool image_is_safe_format, 239 bool image_is_safe_format,
240 std::unique_ptr<user_manager::UserImage::Bytes> image_bytes); 240 scoped_refptr<base::RefCountedBytes> image_bytes);
241 241
242 // Called back after the user image has been saved to 242 // Called back after the user image has been saved to
243 // disk. Updates the user image information in local state. The 243 // disk. Updates the user image information in local state. The
244 // information is only updated if |success| is true (indicating that 244 // information is only updated if |success| is true (indicating that
245 // the image was saved successfully) or the user image is the 245 // the image was saved successfully) or the user image is the
246 // profile image (indicating that even if the image could not be 246 // profile image (indicating that even if the image could not be
247 // saved because it is not available right now, it will be 247 // saved because it is not available right now, it will be
248 // downloaded eventually). 248 // downloaded eventually).
249 void OnSaveImageDone(bool success); 249 void OnSaveImageDone(bool success);
250 250
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 IDR_PROFILE_PICTURE_LOADING)), 414 IDR_PROFILE_PICTURE_LOADING)),
415 image_index_, false); 415 image_index_, false);
416 } 416 }
417 user->SetImageURL(image_url_); 417 user->SetImageURL(image_url_);
418 418
419 parent_->OnJobChangedUserImage(); 419 parent_->OnJobChangedUserImage();
420 } 420 }
421 421
422 void UserImageManagerImpl::Job::UpdateUserAndSaveImage( 422 void UserImageManagerImpl::Job::UpdateUserAndSaveImage(
423 std::unique_ptr<user_manager::UserImage> user_image) { 423 std::unique_ptr<user_manager::UserImage> user_image) {
424 // TODO(crbug.com/593251): Remove the data copy.
425 // Copy the image bytes, before the user image is passed to
426 // UpdateUser(). This is needed to safely save the data bytes to the disk
427 // in the blocking pool. Copying is not desirable but the user image is
428 // JPEG data of 512x512 pixels (usually <100KB) hence it's not enormous.
429 const bool image_is_safe_format = user_image->is_safe_format(); 424 const bool image_is_safe_format = user_image->is_safe_format();
430 std::unique_ptr<user_manager::UserImage::Bytes> copied_bytes; 425 // Create a reference before user_image is passed.
431 if (image_is_safe_format) { 426 scoped_refptr<base::RefCountedBytes> image_bytes;
432 copied_bytes.reset( 427 if (image_is_safe_format)
433 new user_manager::UserImage::Bytes(user_image->image_bytes())); 428 image_bytes = user_image->image_bytes();
434 }
435 429
436 UpdateUser(std::move(user_image)); 430 UpdateUser(std::move(user_image));
437 431
438 SaveImageAndUpdateLocalState(image_is_safe_format, std::move(copied_bytes)); 432 SaveImageAndUpdateLocalState(image_is_safe_format, image_bytes);
439 } 433 }
440 434
441 void UserImageManagerImpl::Job::SaveImageAndUpdateLocalState( 435 void UserImageManagerImpl::Job::SaveImageAndUpdateLocalState(
442 bool image_is_safe_format, 436 bool image_is_safe_format,
443 std::unique_ptr<user_manager::UserImage::Bytes> image_bytes) { 437 scoped_refptr<base::RefCountedBytes> image_bytes) {
444 base::FilePath user_data_dir; 438 base::FilePath user_data_dir;
445 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); 439 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
446 image_path_ = user_data_dir.Append(user_id() + kSafeImagePathExtension); 440 image_path_ = user_data_dir.Append(user_id() + kSafeImagePathExtension);
447 441
448 // This should always be true, because of the following reasons: 442 // This should always be true, because of the following reasons:
449 // 443 //
450 // 1) Profile image from Google account -> UserImage is created with 444 // 1) Profile image from Google account -> UserImage is created with
451 // CreateAndEncode() that generates safe bytes representation. 445 // CreateAndEncode() that generates safe bytes representation.
452 // 2) Profile image from user-specified image -> The bytes representation 446 // 2) Profile image from user-specified image -> The bytes representation
453 // is regenerated after the original image is decoded and cropped. 447 // is regenerated after the original image is decoded and cropped.
454 // 3) Profile image from policy (via OnExternalDataFetched()) -> JPEG is 448 // 3) Profile image from policy (via OnExternalDataFetched()) -> JPEG is
455 // only allowed and ROBUST_JPEG_CODEC is used. 449 // only allowed and ROBUST_JPEG_CODEC is used.
456 // 450 //
457 // However, check the value just in case because an unsafe image should 451 // However, check the value just in case because an unsafe image should
458 // never be saved. 452 // never be saved.
459 if (!image_is_safe_format) { 453 if (!image_is_safe_format) {
460 LOG(ERROR) << "User image is not in safe format"; 454 LOG(ERROR) << "User image is not in safe format";
461 OnSaveImageDone(false); 455 OnSaveImageDone(false);
462 return; 456 return;
463 } 457 }
464 458
465 base::PostTaskAndReplyWithResult( 459 base::PostTaskAndReplyWithResult(
466 parent_->background_task_runner_.get(), FROM_HERE, 460 parent_->background_task_runner_.get(), FROM_HERE,
467 base::Bind(&SaveImage, base::Passed(std::move(image_bytes)), image_path_), 461 base::Bind(&SaveImage, image_bytes, image_path_),
468 base::Bind(&Job::OnSaveImageDone, weak_factory_.GetWeakPtr())); 462 base::Bind(&Job::OnSaveImageDone, weak_factory_.GetWeakPtr()));
469 } 463 }
470 464
471 void UserImageManagerImpl::Job::OnSaveImageDone(bool success) { 465 void UserImageManagerImpl::Job::OnSaveImageDone(bool success) {
472 if (success || image_index_ == user_manager::User::USER_IMAGE_PROFILE) 466 if (success || image_index_ == user_manager::User::USER_IMAGE_PROFILE)
473 UpdateLocalState(); 467 UpdateLocalState();
474 NotifyJobDone(); 468 NotifyJobDone();
475 } 469 }
476 470
477 void UserImageManagerImpl::Job::UpdateLocalState() { 471 void UserImageManagerImpl::Job::UpdateLocalState() {
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after
963 } 957 }
964 958
965 bool UserImageManagerImpl::IsUserLoggedInAndHasGaiaAccount() const { 959 bool UserImageManagerImpl::IsUserLoggedInAndHasGaiaAccount() const {
966 const user_manager::User* user = GetUser(); 960 const user_manager::User* user = GetUser();
967 if (!user) 961 if (!user)
968 return false; 962 return false;
969 return user->is_logged_in() && user->HasGaiaAccount(); 963 return user->is_logged_in() && user->HasGaiaAccount();
970 } 964 }
971 965
972 } // namespace chromeos 966 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/login/users/avatar/user_image_loader.cc ('k') | chrome/browser/ui/webui/chromeos/image_source.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698