OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/login/wallpaper_loader.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "skia/ext/image_operations.h" |
| 14 #include "third_party/skia/include/core/SkBitmap.h" |
| 15 #include "ui/gfx/codec/png_codec.h" |
| 16 #include "ui/gfx/skbitmap_operations.h" |
| 17 |
| 18 using content::BrowserThread; |
| 19 |
| 20 namespace chromeos { |
| 21 |
| 22 WallpaperLoader::WallpaperInfo::WallpaperInfo(bool save_raw_image, |
| 23 const std::string& save_to_path, |
| 24 const LoadedCallback& loaded_cb) |
| 25 : save_raw_image(save_raw_image), |
| 26 save_to_path(save_to_path), |
| 27 loaded_cb(loaded_cb) { |
| 28 } |
| 29 |
| 30 WallpaperLoader::WallpaperInfo::~WallpaperInfo() { |
| 31 } |
| 32 |
| 33 WallpaperLoader::WallpaperLoader() |
| 34 : target_message_loop_(NULL) { |
| 35 } |
| 36 |
| 37 WallpaperLoader::~WallpaperLoader() { |
| 38 } |
| 39 |
| 40 void WallpaperLoader::Start(const std::string& filepath, |
| 41 bool save_raw_image, |
| 42 const std::string& save_to_path, |
| 43 const LoadedCallback& loaded_cb) { |
| 44 target_message_loop_ = MessageLoop::current(); |
| 45 |
| 46 WallpaperInfo info(save_raw_image, save_to_path, loaded_cb); |
| 47 |
| 48 BrowserThread::PostTask( |
| 49 BrowserThread::FILE, FROM_HERE, |
| 50 base::Bind(&WallpaperLoader::LoadImage, this, filepath, info)); |
| 51 } |
| 52 |
| 53 void WallpaperLoader::LoadImage(const std::string& filepath, |
| 54 const WallpaperInfo& wallpaper_info) { |
| 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 56 |
| 57 std::string image_data; |
| 58 file_util::ReadFileToString(FilePath(filepath), &image_data); |
| 59 |
| 60 scoped_refptr<ImageDecoder> image_decoder = |
| 61 new ImageDecoder(this, image_data); |
| 62 wallpaper_info_map_.insert(std::make_pair(image_decoder.get(), |
| 63 wallpaper_info)); |
| 64 image_decoder->Start(); |
| 65 } |
| 66 |
| 67 void WallpaperLoader::OnImageDecoded(const ImageDecoder* decoder, |
| 68 const SkBitmap& decoded_image) { |
| 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 70 |
| 71 WallpaperInfoMap::iterator wallpaper_info_it = |
| 72 wallpaper_info_map_.find(decoder); |
| 73 if (wallpaper_info_it == wallpaper_info_map_.end()) { |
| 74 NOTREACHED(); |
| 75 return; |
| 76 } |
| 77 |
| 78 const WallpaperInfo& info = wallpaper_info_it->second; |
| 79 |
| 80 if (info.save_raw_image) { |
| 81 std::vector<unsigned char> image_data = decoder->get_image_data(); |
| 82 int write_bytes = file_util::WriteFile(FilePath(info.save_to_path), |
| 83 reinterpret_cast<char*>(&*image_data.begin()), |
| 84 image_data.size()); |
| 85 DCHECK(write_bytes == static_cast<int>(image_data.size())); |
| 86 } |
| 87 |
| 88 const LoadedCallback& loaded_cb = info.loaded_cb; |
| 89 |
| 90 target_message_loop_->PostTask( |
| 91 FROM_HERE, |
| 92 base::Bind(loaded_cb, decoded_image)); |
| 93 |
| 94 wallpaper_info_map_.erase(wallpaper_info_it); |
| 95 } |
| 96 |
| 97 void WallpaperLoader::OnDecodeImageFailed(const ImageDecoder* decoder) { |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 99 wallpaper_info_map_.erase(decoder); |
| 100 } |
| 101 |
| 102 } // namespace chromeos |
OLD | NEW |