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

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

Issue 11968044: Fix login visual hitch on chromebook (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add settings observer and using unnamed sequence thread Created 7 years, 11 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/user_image_loader.h" 5 #include "chrome/browser/chromeos/login/user_image_loader.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_path.h" 8 #include "base/file_path.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/threading/worker_pool.h"
12 #include "chrome/browser/chromeos/login/helper.h" 13 #include "chrome/browser/chromeos/login/helper.h"
13 #include "chrome/browser/chromeos/login/user_image.h" 14 #include "chrome/browser/chromeos/login/user_image.h"
14 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
15 #include "skia/ext/image_operations.h" 16 #include "skia/ext/image_operations.h"
16 #include "third_party/skia/include/core/SkBitmap.h" 17 #include "third_party/skia/include/core/SkBitmap.h"
17 #include "ui/gfx/codec/png_codec.h" 18 #include "ui/gfx/codec/png_codec.h"
18 #include "ui/gfx/skbitmap_operations.h" 19 #include "ui/gfx/skbitmap_operations.h"
19 20
20 using content::BrowserThread; 21 using content::BrowserThread;
21 22
(...skipping 15 matching lines...) Expand all
37 38
38 UserImageLoader::~UserImageLoader() { 39 UserImageLoader::~UserImageLoader() {
39 } 40 }
40 41
41 void UserImageLoader::Start(const std::string& filepath, 42 void UserImageLoader::Start(const std::string& filepath,
42 int size, 43 int size,
43 const LoadedCallback& loaded_cb) { 44 const LoadedCallback& loaded_cb) {
44 target_message_loop_ = MessageLoop::current(); 45 target_message_loop_ = MessageLoop::current();
45 46
46 ImageInfo image_info(size, loaded_cb); 47 ImageInfo image_info(size, loaded_cb);
47 BrowserThread::PostTask( 48 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
48 BrowserThread::FILE, FROM_HERE, 49 SequenceToken sequence_token = pool->GetSequenceToken();
49 base::Bind(&UserImageLoader::LoadImage, this, filepath, image_info)); 50 scoped_refptr<base::SequencedTaskRunner> task_runner = pool->
51 GetSequencedTaskRunnerWithShutdownBehavior(sequence_token,
52 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
Joao da Silva 2013/01/21 08:07:39 If this is using a sequenced task runner then it w
bshe 2013/01/21 16:04:40 pool->GetSequenceToken will return a different tok
Joao da Silva 2013/01/21 16:46:58 Yes, you're right. Either call GetTaskRunnerWithSh
bshe 2013/01/21 22:53:07 It looks like UtilityProcessHost::Create require a
bshe 2013/01/22 03:48:57 Actually, the sequence task runner is running para
53 task_runner->PostTask(
54 FROM_HERE,
55 base::Bind(&UserImageLoader::LoadImage, this, filepath, image_info,
56 sequence_token));
50 } 57 }
51 58
52 void UserImageLoader::LoadImage(const std::string& filepath, 59 void UserImageLoader::LoadImage(const std::string& filepath,
53 const ImageInfo& image_info) { 60 const ImageInfo& image_info,
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 61 const SequenceToken& sequence_token) {
62 DCHECK(BrowserThread::GetBlockingPool()->
63 IsRunningSequenceOnCurrentThread(sequence_token));
55 64
56 std::string image_data; 65 std::string image_data;
57 file_util::ReadFileToString(FilePath(filepath), &image_data); 66 file_util::ReadFileToString(FilePath(filepath), &image_data);
58 67
59 scoped_refptr<ImageDecoder> image_decoder = 68 scoped_refptr<ImageDecoder> image_decoder =
60 new ImageDecoder(this, image_data, image_codec_); 69 new ImageDecoder(this, image_data, image_codec_);
61 image_info_map_.insert(std::make_pair(image_decoder.get(), image_info)); 70 image_info_map_.insert(std::make_pair(image_decoder.get(), image_info));
62 image_decoder->Start(); 71 image_decoder->Start(sequence_token);
63 } 72 }
64 73
65 void UserImageLoader::OnImageDecoded(const ImageDecoder* decoder, 74 void UserImageLoader::OnImageDecoded(const ImageDecoder* decoder,
66 const SkBitmap& decoded_image) { 75 const SkBitmap& decoded_image) {
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 76 ImageInfoMap::iterator info_it;
68 77 {
69 ImageInfoMap::iterator info_it = image_info_map_.find(decoder); 78 base::AutoLock lock(lock_);
70 if (info_it == image_info_map_.end()) { 79 info_it = image_info_map_.find(decoder);
71 NOTREACHED(); 80 if (info_it == image_info_map_.end()) {
72 return; 81 NOTREACHED();
82 return;
83 }
73 } 84 }
74 85
75 const ImageInfo& image_info = info_it->second; 86 const ImageInfo& image_info = info_it->second;
Joao da Silva 2013/01/21 08:07:39 info_it may be invalid at this point, because anot
bshe 2013/01/21 16:04:40 If post back to UI, I worry this may be blocked on
Joao da Silva 2013/01/21 16:46:58 I'd be very surprised if UI is stuck at any moment
bshe 2013/01/21 22:53:07 Thanks. I will try to use lock to unblock the task
76 SkBitmap final_image = decoded_image; 87 SkBitmap final_image = decoded_image;
77 88
78 if (image_info.size > 0) { 89 if (image_info.size > 0) {
79 // Auto crop the image, taking the largest square in the center. 90 // Auto crop the image, taking the largest square in the center.
80 int size = std::min(decoded_image.width(), decoded_image.height()); 91 int size = std::min(decoded_image.width(), decoded_image.height());
81 int x = (decoded_image.width() - size) / 2; 92 int x = (decoded_image.width() - size) / 2;
82 int y = (decoded_image.height() - size) / 2; 93 int y = (decoded_image.height() - size) / 2;
83 SkBitmap cropped_image = 94 SkBitmap cropped_image =
84 SkBitmapOperations::CreateTiledBitmap(decoded_image, x, y, size, size); 95 SkBitmapOperations::CreateTiledBitmap(decoded_image, x, y, size, size);
85 if (size > image_info.size) { 96 if (size > image_info.size) {
86 // Also downsize the image to save space and memory. 97 // Also downsize the image to save space and memory.
87 final_image = 98 final_image =
88 skia::ImageOperations::Resize(cropped_image, 99 skia::ImageOperations::Resize(cropped_image,
89 skia::ImageOperations::RESIZE_LANCZOS3, 100 skia::ImageOperations::RESIZE_LANCZOS3,
90 image_info.size, 101 image_info.size,
91 image_info.size); 102 image_info.size);
92 } else { 103 } else {
93 final_image = cropped_image; 104 final_image = cropped_image;
94 } 105 }
95 } 106 }
96 gfx::ImageSkia final_image_skia(final_image); 107 gfx::ImageSkia final_image_skia(final_image);
97 final_image_skia.MakeThreadSafe(); 108 final_image_skia.MakeThreadSafe();
98 UserImage user_image(final_image_skia, decoder->get_image_data()); 109 UserImage user_image(final_image_skia, decoder->get_image_data());
99 if (image_codec_ == ImageDecoder::ROBUST_JPEG_CODEC) 110 if (image_codec_ == ImageDecoder::ROBUST_JPEG_CODEC)
100 user_image.MarkAsSafe(); 111 user_image.MarkAsSafe();
101 target_message_loop_->PostTask( 112 target_message_loop_->PostTask(
102 FROM_HERE, 113 FROM_HERE,
103 base::Bind(image_info.loaded_cb, user_image)); 114 base::Bind(image_info.loaded_cb, user_image));
104 115
105 image_info_map_.erase(info_it); 116 {
117 base::AutoLock lock(lock_);
118 image_info_map_.erase(info_it);
119 }
106 } 120 }
107 121
108 void UserImageLoader::OnDecodeImageFailed(const ImageDecoder* decoder) { 122 void UserImageLoader::OnDecodeImageFailed(const ImageDecoder* decoder) {
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 123 ImageInfoMap::iterator info_it;
110 124 {
111 ImageInfoMap::iterator info_it = image_info_map_.find(decoder); 125 base::AutoLock lock(lock_);
112 if (info_it == image_info_map_.end()) { 126 info_it = image_info_map_.find(decoder);
113 NOTREACHED(); 127 if (info_it == image_info_map_.end()) {
114 return; 128 NOTREACHED();
129 return;
130 }
115 } 131 }
116 132
117 const ImageInfo& image_info = info_it->second; 133 const ImageInfo& image_info = info_it->second;
118 target_message_loop_->PostTask( 134 target_message_loop_->PostTask(
119 FROM_HERE, 135 FROM_HERE,
120 base::Bind(image_info.loaded_cb, UserImage())); 136 base::Bind(image_info.loaded_cb, UserImage()));
121 image_info_map_.erase(decoder); 137
138 {
139 base::AutoLock lock(lock_);
140 image_info_map_.erase(decoder);
141 }
122 } 142 }
123 143
124 } // namespace chromeos 144 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698