| OLD | NEW |
| 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/ui/webui/options/chromeos/wallpaper_source.h" | 5 #include "chrome/browser/ui/webui/options/chromeos/wallpaper_source.h" |
| 6 | 6 |
| 7 #include "ash/desktop_background/desktop_background_controller.h" | 7 #include "ash/desktop_background/desktop_background_controller.h" |
| 8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
| 9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
| 10 #include "base/synchronization/cancellation_flag.h" | 10 #include "base/synchronization/cancellation_flag.h" |
| 11 #include "base/threading/worker_pool.h" | 11 #include "base/threading/worker_pool.h" |
| 12 #include "chrome/browser/chromeos/login/user_manager.h" | 12 #include "chrome/browser/chromeos/login/user_manager.h" |
| 13 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
| 13 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 15 #include "grit/ui_resources.h" | 16 #include "grit/ui_resources.h" |
| 16 #include "net/base/mime_util.h" | 17 #include "net/base/mime_util.h" |
| 17 #include "ui/gfx/codec/png_codec.h" | 18 #include "ui/gfx/codec/png_codec.h" |
| 18 #include "ui/gfx/size.h" | 19 #include "ui/gfx/size.h" |
| 19 #include "ui/gfx/skia_util.h" | 20 #include "ui/gfx/skia_util.h" |
| 20 | 21 |
| 21 extern "C" { | 22 extern "C" { |
| 22 #include "third_party/zlib/zlib.h" | 23 #include "third_party/zlib/zlib.h" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 int request_id_; | 88 int request_id_; |
| 88 // Buffer to store encoded image. | 89 // Buffer to store encoded image. |
| 89 scoped_refptr<base::RefCountedBytes> data_; | 90 scoped_refptr<base::RefCountedBytes> data_; |
| 90 // Original image to encode. | 91 // Original image to encode. |
| 91 SkBitmap image_; | 92 SkBitmap image_; |
| 92 | 93 |
| 93 DISALLOW_COPY_AND_ASSIGN(WallpaperEncodingOperation); | 94 DISALLOW_COPY_AND_ASSIGN(WallpaperEncodingOperation); |
| 94 }; | 95 }; |
| 95 | 96 |
| 96 WallpaperImageSource::WallpaperImageSource() | 97 WallpaperImageSource::WallpaperImageSource() |
| 97 : DataSource(chrome::kChromeUIWallpaperImageHost, NULL), | 98 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 98 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 99 } | 99 } |
| 100 | 100 |
| 101 WallpaperImageSource::~WallpaperImageSource() { | 101 WallpaperImageSource::~WallpaperImageSource() { |
| 102 } | 102 } |
| 103 | 103 |
| 104 std::string WallpaperImageSource::GetSource() { |
| 105 return chrome::kChromeUIWallpaperImageHost; |
| 106 } |
| 107 |
| 104 void WallpaperImageSource::StartDataRequest(const std::string& email, | 108 void WallpaperImageSource::StartDataRequest(const std::string& email, |
| 105 bool is_incognito, | 109 bool is_incognito, |
| 106 int request_id) { | 110 int request_id) { |
| 107 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 111 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 108 TRACE_EVENT_ASYNC_BEGIN0("SCREEN_LOCK", "GetUserWallpaperDataRequest", | 112 TRACE_EVENT_ASYNC_BEGIN0("SCREEN_LOCK", "GetUserWallpaperDataRequest", |
| 109 request_id); | 113 request_id); |
| 110 CancelPendingEncodingOperation(); | 114 CancelPendingEncodingOperation(); |
| 111 content::BrowserThread::PostTask( | 115 content::BrowserThread::PostTask( |
| 112 content::BrowserThread::UI, | 116 content::BrowserThread::UI, |
| 113 FROM_HERE, | 117 FROM_HERE, |
| 114 base::Bind(&WallpaperImageSource::GetCurrentUserWallpaper, this, | 118 base::Bind(&WallpaperImageSource::GetCurrentUserWallpaper, |
| 115 request_id)); | 119 weak_ptr_factory_.GetWeakPtr(), request_id)); |
| 116 } | 120 } |
| 117 | 121 |
| 118 std::string WallpaperImageSource::GetMimeType(const std::string&) const { | 122 std::string WallpaperImageSource::GetMimeType(const std::string&) const { |
| 119 return "images/png"; | 123 return "images/png"; |
| 120 } | 124 } |
| 121 | 125 |
| 122 // Get current background image and store it to |data|. | 126 // Get current background image and store it to |data|. |
| 123 void WallpaperImageSource::GetCurrentUserWallpaper(int request_id) { | 127 void WallpaperImageSource::GetCurrentUserWallpaper( |
| 128 const base::WeakPtr<WallpaperImageSource>& this_object, int request_id) { |
| 124 SkBitmap image; | 129 SkBitmap image; |
| 125 TRACE_EVENT0("LOCK_SCREEN", "GetCurrentUserWallpaper"); | 130 TRACE_EVENT0("LOCK_SCREEN", "GetCurrentUserWallpaper"); |
| 126 if (chromeos::UserManager::Get()->IsUserLoggedIn()) { | 131 if (chromeos::UserManager::Get()->IsUserLoggedIn()) { |
| 127 // TODO(sad|bshe): It maybe necessary to include the scale factor in the | 132 // TODO(sad|bshe): It maybe necessary to include the scale factor in the |
| 128 // request (as is done for user-image and wallpaper-thumbnails). | 133 // request (as is done for user-image and wallpaper-thumbnails). |
| 129 SkBitmap wallpaper; | 134 SkBitmap wallpaper; |
| 130 gfx::ImageSkia wallpaper_skia = ash::Shell::GetInstance()-> | 135 gfx::ImageSkia wallpaper_skia = ash::Shell::GetInstance()-> |
| 131 desktop_background_controller()->GetCurrentWallpaperImage(); | 136 desktop_background_controller()->GetCurrentWallpaperImage(); |
| 132 if (!wallpaper_skia.isNull()) | 137 if (!wallpaper_skia.isNull()) |
| 133 wallpaper = *wallpaper_skia.bitmap(); | 138 wallpaper = *wallpaper_skia.bitmap(); |
| 134 SkBitmap copy; | 139 SkBitmap copy; |
| 135 if (wallpaper.deepCopyTo(©, wallpaper.config())) | 140 if (wallpaper.deepCopyTo(©, wallpaper.config())) |
| 136 image = copy; | 141 image = copy; |
| 137 } | 142 } |
| 138 content::BrowserThread::PostTask( | 143 content::BrowserThread::PostTask( |
| 139 content::BrowserThread::IO, | 144 content::BrowserThread::IO, |
| 140 FROM_HERE, | 145 FROM_HERE, |
| 141 base::Bind(&WallpaperImageSource::ImageAcquired, this, image, request_id)); | 146 base::Bind(&WallpaperImageSource::ImageAcquired, this_object, |
| 147 image, request_id)); |
| 142 } | 148 } |
| 143 | 149 |
| 144 | 150 |
| 145 void WallpaperImageSource::ImageAcquired(SkBitmap image, | 151 void WallpaperImageSource::ImageAcquired(SkBitmap image, int request_id) { |
| 146 int request_id) { | |
| 147 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 152 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 148 CancelPendingEncodingOperation(); | 153 CancelPendingEncodingOperation(); |
| 149 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); | 154 scoped_refptr<base::RefCountedBytes> data = new base::RefCountedBytes(); |
| 150 wallpaper_encoding_op_ = new WallpaperEncodingOperation(request_id, | 155 wallpaper_encoding_op_ = new WallpaperEncodingOperation(request_id, |
| 151 data, | 156 data, |
| 152 image); | 157 image); |
| 153 base::WorkerPool::PostTaskAndReply( | 158 base::WorkerPool::PostTaskAndReply( |
| 154 FROM_HERE, | 159 FROM_HERE, |
| 155 base::Bind(&WallpaperEncodingOperation::Run, wallpaper_encoding_op_), | 160 base::Bind(&WallpaperEncodingOperation::Run, wallpaper_encoding_op_), |
| 156 base::Bind(&WallpaperImageSource::SendCurrentUserWallpaper, | 161 base::Bind(&WallpaperImageSource::SendCurrentUserWallpaper, |
| 157 weak_ptr_factory_.GetWeakPtr(), request_id, data), | 162 weak_ptr_factory_.GetWeakPtr(), request_id, data), |
| 158 true /* task_is_slow */); | 163 true /* task_is_slow */); |
| 159 }; | 164 }; |
| 160 | 165 |
| 161 void WallpaperImageSource::CancelPendingEncodingOperation() { | 166 void WallpaperImageSource::CancelPendingEncodingOperation() { |
| 162 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 167 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 163 // Set canceled flag of previous request to skip unneeded encoding. | 168 // Set canceled flag of previous request to skip unneeded encoding. |
| 164 if (wallpaper_encoding_op_.get()) { | 169 if (wallpaper_encoding_op_.get()) { |
| 165 wallpaper_encoding_op_->Cancel(); | 170 wallpaper_encoding_op_->Cancel(); |
| 166 SendResponse(wallpaper_encoding_op_->request_id(), NULL); | 171 url_data_source()->SendResponse(wallpaper_encoding_op_->request_id(), NULL); |
| 167 TRACE_EVENT_ASYNC_END0("SCREEN_LOCK", "GetUserWallpaper", | 172 TRACE_EVENT_ASYNC_END0("SCREEN_LOCK", "GetUserWallpaper", |
| 168 wallpaper_encoding_op_->request_id()); | 173 wallpaper_encoding_op_->request_id()); |
| 169 } | 174 } |
| 170 | 175 |
| 171 // Cancel reply callback for previous request. | 176 // Cancel reply callback for previous request. |
| 172 weak_ptr_factory_.InvalidateWeakPtrs(); | 177 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 173 } | 178 } |
| 174 | 179 |
| 175 void WallpaperImageSource::SendCurrentUserWallpaper(int request_id, | 180 void WallpaperImageSource::SendCurrentUserWallpaper(int request_id, |
| 176 scoped_refptr<base::RefCountedBytes> data) { | 181 scoped_refptr<base::RefCountedBytes> data) { |
| 177 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 182 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 178 SendResponse(request_id, data); | 183 url_data_source()->SendResponse(request_id, data); |
| 179 TRACE_EVENT_ASYNC_END0("SCREEN_LOCK", "GetUserWallpaper", request_id); | 184 TRACE_EVENT_ASYNC_END0("SCREEN_LOCK", "GetUserWallpaper", request_id); |
| 180 } | 185 } |
| 181 | 186 |
| 182 } // namespace options | 187 } // namespace options |
| 183 } // namespace chromeos | 188 } // namespace chromeos |
| OLD | NEW |