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

Side by Side Diff: ash/desktop_background/desktop_background_controller.cc

Issue 208273005: If customization includes default wallpaper, download and apply it. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update after-review. Created 6 years, 8 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
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 "ash/desktop_background/desktop_background_controller.h" 5 #include "ash/desktop_background/desktop_background_controller.h"
6 6
7 #include "ash/ash_switches.h" 7 #include "ash/ash_switches.h"
8 #include "ash/desktop_background/desktop_background_controller_observer.h" 8 #include "ash/desktop_background/desktop_background_controller_observer.h"
9 #include "ash/desktop_background/desktop_background_view.h" 9 #include "ash/desktop_background/desktop_background_view.h"
10 #include "ash/desktop_background/desktop_background_widget_controller.h" 10 #include "ash/desktop_background/desktop_background_widget_controller.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 45
46 } // namespace 46 } // namespace
47 47
48 const int kSmallWallpaperMaxWidth = 1366; 48 const int kSmallWallpaperMaxWidth = 1366;
49 const int kSmallWallpaperMaxHeight = 800; 49 const int kSmallWallpaperMaxHeight = 800;
50 const int kLargeWallpaperMaxWidth = 2560; 50 const int kLargeWallpaperMaxWidth = 2560;
51 const int kLargeWallpaperMaxHeight = 1700; 51 const int kLargeWallpaperMaxHeight = 1700;
52 const int kWallpaperThumbnailWidth = 108; 52 const int kWallpaperThumbnailWidth = 108;
53 const int kWallpaperThumbnailHeight = 68; 53 const int kWallpaperThumbnailHeight = 68;
54 54
55 // DesktopBackgroundController::WallpaperLoader wraps background wallpaper
56 // loading.
57 class DesktopBackgroundController::WallpaperLoader
58 : public base::RefCountedThreadSafe<
59 DesktopBackgroundController::WallpaperLoader> {
60 public:
61 // If set, |file_path| must be a trusted (i.e. read-only,
62 // non-user-controlled) file containing a JPEG image.
63 WallpaperLoader(const base::FilePath& file_path,
64 WallpaperLayout file_layout,
65 int resource_id,
66 WallpaperLayout resource_layout)
67 : file_path_(file_path),
68 file_layout_(file_layout),
69 resource_id_(resource_id),
70 resource_layout_(resource_layout) {
71 }
72
73 void LoadOnWorkerPoolThread() {
74 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
75 if (cancel_flag_.IsSet())
76 return;
77
78 if (!file_path_.empty()) {
79 VLOG(1) << "Loading " << file_path_.value();
80 file_bitmap_ = LoadSkBitmapFromJPEGFile(file_path_);
81 }
82
83 if (cancel_flag_.IsSet())
84 return;
85
86 if (file_bitmap_) {
87 gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(*file_bitmap_);
88 wallpaper_resizer_.reset(new WallpaperResizer(
89 image, GetMaxDisplaySizeInNative(), file_layout_));
90 } else {
91 wallpaper_resizer_.reset(new WallpaperResizer(
92 resource_id_, GetMaxDisplaySizeInNative(), resource_layout_));
93 }
94 }
95
96 const base::FilePath& file_path() const { return file_path_; }
97 int resource_id() const { return resource_id_; }
98
99 void Cancel() {
100 cancel_flag_.Set();
101 }
102
103 bool IsCanceled() {
104 return cancel_flag_.IsSet();
105 }
106
107 WallpaperResizer* ReleaseWallpaperResizer() {
108 return wallpaper_resizer_.release();
109 }
110
111 private:
112 friend class base::RefCountedThreadSafe<
113 DesktopBackgroundController::WallpaperLoader>;
114
115 // Loads a JPEG image from |path|, a trusted file -- note that the image
116 // is not loaded in a sandboxed process. Returns an empty pointer on
117 // error.
118 static scoped_ptr<SkBitmap> LoadSkBitmapFromJPEGFile(
119 const base::FilePath& path) {
120 std::string data;
121 if (!base::ReadFileToString(path, &data)) {
122 LOG(ERROR) << "Unable to read data from " << path.value();
123 return scoped_ptr<SkBitmap>();
124 }
125
126 scoped_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode(
127 reinterpret_cast<const unsigned char*>(data.data()), data.size()));
128 if (!bitmap)
129 LOG(ERROR) << "Unable to decode JPEG data from " << path.value();
130 return bitmap.Pass();
131 }
132
133 ~WallpaperLoader() {}
134
135 base::CancellationFlag cancel_flag_;
136
137 // Bitmap loaded from |file_path_|.
138 scoped_ptr<SkBitmap> file_bitmap_;
139
140 scoped_ptr<WallpaperResizer> wallpaper_resizer_;
141
142 // Path to a trusted JPEG file.
143 base::FilePath file_path_;
144
145 // Layout to be used when displaying the image from |file_path_|.
146 WallpaperLayout file_layout_;
147
148 // ID of an image resource to use if |file_path_| is empty or unloadable.
149 int resource_id_;
150
151 // Layout to be used when displaying |resource_id_|.
152 WallpaperLayout resource_layout_;
153
154 DISALLOW_COPY_AND_ASSIGN(WallpaperLoader);
155 };
156
157 DesktopBackgroundController::DesktopBackgroundController() 55 DesktopBackgroundController::DesktopBackgroundController()
158 : command_line_for_testing_(NULL), 56 : locked_(false),
159 locked_(false),
160 desktop_background_mode_(BACKGROUND_NONE), 57 desktop_background_mode_(BACKGROUND_NONE),
161 current_default_wallpaper_resource_id_(-1),
162 weak_ptr_factory_(this),
163 wallpaper_reload_delay_(kWallpaperReloadDelayMs) { 58 wallpaper_reload_delay_(kWallpaperReloadDelayMs) {
164 Shell::GetInstance()->display_controller()->AddObserver(this); 59 Shell::GetInstance()->display_controller()->AddObserver(this);
165 } 60 }
166 61
167 DesktopBackgroundController::~DesktopBackgroundController() { 62 DesktopBackgroundController::~DesktopBackgroundController() {
168 CancelDefaultWallpaperLoader();
169 Shell::GetInstance()->display_controller()->RemoveObserver(this); 63 Shell::GetInstance()->display_controller()->RemoveObserver(this);
170 } 64 }
171 65
172 gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const { 66 gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const {
173 if (current_wallpaper_) 67 if (current_wallpaper_)
174 return current_wallpaper_->image(); 68 return current_wallpaper_->image();
175 return gfx::ImageSkia(); 69 return gfx::ImageSkia();
176 } 70 }
177 71
178 void DesktopBackgroundController::AddObserver( 72 void DesktopBackgroundController::AddObserver(
(...skipping 22 matching lines...) Expand all
201 if (current_max_display_size_ != max_display_size) { 95 if (current_max_display_size_ != max_display_size) {
202 current_max_display_size_ = max_display_size; 96 current_max_display_size_ = max_display_size;
203 if (desktop_background_mode_ == BACKGROUND_IMAGE && 97 if (desktop_background_mode_ == BACKGROUND_IMAGE &&
204 current_wallpaper_.get()) 98 current_wallpaper_.get())
205 UpdateWallpaper(); 99 UpdateWallpaper();
206 } 100 }
207 101
208 InstallDesktopController(root_window); 102 InstallDesktopController(root_window);
209 } 103 }
210 104
211 bool DesktopBackgroundController::SetDefaultWallpaper(bool is_guest) { 105 void DesktopBackgroundController::SetWallpaper(const gfx::ImageSkia& image,
212 VLOG(1) << "SetDefaultWallpaper: is_guest=" << is_guest; 106 WallpaperLayout layout) {
213 const bool use_large = 107 VLOG(1) << "SetWallpaper: image_id=" << WallpaperResizer::GetImageId(image)
214 GetAppropriateResolution() == WALLPAPER_RESOLUTION_LARGE; 108 << " layout=" << layout;
215 109
216 base::FilePath file_path; 110 if (WallpaperIsAlreadyLoaded(image)) {
217 WallpaperLayout file_layout = use_large ? WALLPAPER_LAYOUT_CENTER_CROPPED : 111 VLOG(1) << "Wallpaper is already loaded";
218 WALLPAPER_LAYOUT_CENTER;
219 int resource_id = use_large ? IDR_AURA_WALLPAPER_DEFAULT_LARGE :
220 IDR_AURA_WALLPAPER_DEFAULT_SMALL;
221 WallpaperLayout resource_layout = WALLPAPER_LAYOUT_TILE;
222
223 CommandLine* command_line = command_line_for_testing_ ?
224 command_line_for_testing_ : CommandLine::ForCurrentProcess();
225 const char* switch_name = NULL;
226 if (is_guest) {
227 switch_name = use_large ? switches::kAshGuestWallpaperLarge :
228 switches::kAshGuestWallpaperSmall;
229 } else {
230 switch_name = use_large ? switches::kAshDefaultWallpaperLarge :
231 switches::kAshDefaultWallpaperSmall;
232 }
233 file_path = command_line->GetSwitchValuePath(switch_name);
234
235 if (DefaultWallpaperIsAlreadyLoadingOrLoaded(file_path, resource_id)) {
236 VLOG(1) << "Default wallpaper is already loading or loaded";
237 return false;
238 }
239
240 CancelDefaultWallpaperLoader();
241 default_wallpaper_loader_ = new WallpaperLoader(
242 file_path, file_layout, resource_id, resource_layout);
243 base::WorkerPool::PostTaskAndReply(
244 FROM_HERE,
245 base::Bind(&WallpaperLoader::LoadOnWorkerPoolThread,
246 default_wallpaper_loader_),
247 base::Bind(&DesktopBackgroundController::OnDefaultWallpaperLoadCompleted,
248 weak_ptr_factory_.GetWeakPtr(),
249 default_wallpaper_loader_),
250 true /* task_is_slow */);
251 return true;
252 }
253
254 void DesktopBackgroundController::SetCustomWallpaper(
255 const gfx::ImageSkia& image,
256 WallpaperLayout layout) {
257 VLOG(1) << "SetCustomWallpaper: image_id="
258 << WallpaperResizer::GetImageId(image) << " layout=" << layout;
259 CancelDefaultWallpaperLoader();
260
261 if (CustomWallpaperIsAlreadyLoaded(image)) {
262 VLOG(1) << "Custom wallpaper is already loaded";
263 return; 112 return;
264 } 113 }
265 114
266 current_wallpaper_.reset(new WallpaperResizer( 115 current_wallpaper_.reset(new WallpaperResizer(
267 image, GetMaxDisplaySizeInNative(), layout)); 116 image, GetMaxDisplaySizeInNative(), layout));
268 current_wallpaper_->StartResize(); 117 current_wallpaper_->StartResize();
269 118
270 current_default_wallpaper_path_ = base::FilePath();
271 current_default_wallpaper_resource_id_ = -1;
272
273 FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, observers_, 119 FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, observers_,
274 OnWallpaperDataChanged()); 120 OnWallpaperDataChanged());
275 SetDesktopBackgroundImageMode(); 121 SetDesktopBackgroundImageMode();
276 } 122 }
277 123
278 void DesktopBackgroundController::CancelDefaultWallpaperLoader() {
279 // Set canceled flag of previous request to skip unneeded loading.
280 if (default_wallpaper_loader_.get())
281 default_wallpaper_loader_->Cancel();
282
283 // Cancel reply callback for previous request.
284 weak_ptr_factory_.InvalidateWeakPtrs();
285 }
286
287 void DesktopBackgroundController::CreateEmptyWallpaper() { 124 void DesktopBackgroundController::CreateEmptyWallpaper() {
288 current_wallpaper_.reset(NULL); 125 current_wallpaper_.reset(NULL);
289 SetDesktopBackgroundImageMode(); 126 SetDesktopBackgroundImageMode();
290 } 127 }
291 128
292 WallpaperResolution DesktopBackgroundController::GetAppropriateResolution() { 129 WallpaperResolution DesktopBackgroundController::GetAppropriateResolution() {
293 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
294 gfx::Size size = GetMaxDisplaySizeInNative(); 131 gfx::Size size = GetMaxDisplaySizeInNative();
295 return (size.width() > kSmallWallpaperMaxWidth || 132 return (size.width() > kSmallWallpaperMaxWidth ||
296 size.height() > kSmallWallpaperMaxHeight) ? 133 size.height() > kSmallWallpaperMaxHeight) ?
(...skipping 24 matching lines...) Expand all
321 current_wallpaper_.get()) { 158 current_wallpaper_.get()) {
322 timer_.Stop(); 159 timer_.Stop();
323 timer_.Start(FROM_HERE, 160 timer_.Start(FROM_HERE,
324 base::TimeDelta::FromMilliseconds(wallpaper_reload_delay_), 161 base::TimeDelta::FromMilliseconds(wallpaper_reload_delay_),
325 this, 162 this,
326 &DesktopBackgroundController::UpdateWallpaper); 163 &DesktopBackgroundController::UpdateWallpaper);
327 } 164 }
328 } 165 }
329 } 166 }
330 167
331 bool DesktopBackgroundController::DefaultWallpaperIsAlreadyLoadingOrLoaded( 168 bool DesktopBackgroundController::WallpaperIsAlreadyLoaded(
332 const base::FilePath& image_file,
333 int image_resource_id) const {
334 return (default_wallpaper_loader_.get() &&
335 !default_wallpaper_loader_->IsCanceled() &&
336 default_wallpaper_loader_->file_path() == image_file &&
337 default_wallpaper_loader_->resource_id() == image_resource_id) ||
338 (current_wallpaper_.get() &&
339 current_default_wallpaper_path_ == image_file &&
340 current_default_wallpaper_resource_id_ == image_resource_id);
341 }
342
343 bool DesktopBackgroundController::CustomWallpaperIsAlreadyLoaded(
344 const gfx::ImageSkia& image) const { 169 const gfx::ImageSkia& image) const {
345 return current_wallpaper_.get() && 170 return current_wallpaper_.get() &&
346 (WallpaperResizer::GetImageId(image) == 171 (WallpaperResizer::GetImageId(image) ==
347 current_wallpaper_->original_image_id()); 172 current_wallpaper_->original_image_id());
348 } 173 }
349 174
350 void DesktopBackgroundController::SetDesktopBackgroundImageMode() { 175 void DesktopBackgroundController::SetDesktopBackgroundImageMode() {
351 desktop_background_mode_ = BACKGROUND_IMAGE; 176 desktop_background_mode_ = BACKGROUND_IMAGE;
352 InstallDesktopControllerForAllWindows(); 177 InstallDesktopControllerForAllWindows();
353 } 178 }
354 179
355 void DesktopBackgroundController::OnDefaultWallpaperLoadCompleted(
356 scoped_refptr<WallpaperLoader> loader) {
357 VLOG(1) << "OnDefaultWallpaperLoadCompleted";
358 current_wallpaper_.reset(loader->ReleaseWallpaperResizer());
359 current_wallpaper_->StartResize();
360 current_default_wallpaper_path_ = loader->file_path();
361 current_default_wallpaper_resource_id_ = loader->resource_id();
362 FOR_EACH_OBSERVER(DesktopBackgroundControllerObserver, observers_,
363 OnWallpaperDataChanged());
364
365 SetDesktopBackgroundImageMode();
366
367 DCHECK(loader.get() == default_wallpaper_loader_.get());
368 default_wallpaper_loader_ = NULL;
369 }
370
371 void DesktopBackgroundController::InstallDesktopController( 180 void DesktopBackgroundController::InstallDesktopController(
372 aura::Window* root_window) { 181 aura::Window* root_window) {
373 internal::DesktopBackgroundWidgetController* component = NULL; 182 internal::DesktopBackgroundWidgetController* component = NULL;
374 int container_id = GetBackgroundContainerId(locked_); 183 int container_id = GetBackgroundContainerId(locked_);
375 184
376 switch (desktop_background_mode_) { 185 switch (desktop_background_mode_) {
377 case BACKGROUND_IMAGE: { 186 case BACKGROUND_IMAGE: {
378 views::Widget* widget = internal::CreateDesktopBackground(root_window, 187 views::Widget* widget = internal::CreateDesktopBackground(root_window,
379 container_id); 188 container_id);
380 component = new internal::DesktopBackgroundWidgetController(widget); 189 component = new internal::DesktopBackgroundWidgetController(widget);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 return moved; 247 return moved;
439 } 248 }
440 249
441 int DesktopBackgroundController::GetBackgroundContainerId(bool locked) { 250 int DesktopBackgroundController::GetBackgroundContainerId(bool locked) {
442 return locked ? internal::kShellWindowId_LockScreenBackgroundContainer : 251 return locked ? internal::kShellWindowId_LockScreenBackgroundContainer :
443 internal::kShellWindowId_DesktopBackgroundContainer; 252 internal::kShellWindowId_DesktopBackgroundContainer;
444 } 253 }
445 254
446 void DesktopBackgroundController::UpdateWallpaper() { 255 void DesktopBackgroundController::UpdateWallpaper() {
447 current_wallpaper_.reset(NULL); 256 current_wallpaper_.reset(NULL);
448 current_default_wallpaper_path_ = base::FilePath();
449 current_default_wallpaper_resource_id_ = -1;
450 ash::Shell::GetInstance()->user_wallpaper_delegate()-> 257 ash::Shell::GetInstance()->user_wallpaper_delegate()->
451 UpdateWallpaper(true /* clear cache */); 258 UpdateWallpaper(true /* clear cache */);
452 } 259 }
453 260
454 // static 261 // static
455 gfx::Size DesktopBackgroundController::GetMaxDisplaySizeInNative() { 262 gfx::Size DesktopBackgroundController::GetMaxDisplaySizeInNative() {
456 int width = 0; 263 int width = 0;
457 int height = 0; 264 int height = 0;
458 std::vector<gfx::Display> displays = Shell::GetScreen()->GetAllDisplays(); 265 std::vector<gfx::Display> displays = Shell::GetScreen()->GetAllDisplays();
459 internal::DisplayManager* display_manager = 266 internal::DisplayManager* display_manager =
460 Shell::GetInstance()->display_manager(); 267 Shell::GetInstance()->display_manager();
461 268
462 for (std::vector<gfx::Display>::iterator iter = displays.begin(); 269 for (std::vector<gfx::Display>::iterator iter = displays.begin();
463 iter != displays.end(); ++iter) { 270 iter != displays.end(); ++iter) {
464 // Don't use size_in_pixel because we want to use the native pixel size. 271 // Don't use size_in_pixel because we want to use the native pixel size.
465 gfx::Size size_in_pixel = 272 gfx::Size size_in_pixel =
466 display_manager->GetDisplayInfo(iter->id()).bounds_in_native().size(); 273 display_manager->GetDisplayInfo(iter->id()).bounds_in_native().size();
467 if (iter->rotation() == gfx::Display::ROTATE_90 || 274 if (iter->rotation() == gfx::Display::ROTATE_90 ||
468 iter->rotation() == gfx::Display::ROTATE_270) { 275 iter->rotation() == gfx::Display::ROTATE_270) {
469 size_in_pixel = gfx::Size(size_in_pixel.height(), size_in_pixel.width()); 276 size_in_pixel = gfx::Size(size_in_pixel.height(), size_in_pixel.width());
470 } 277 }
471 width = std::max(size_in_pixel.width(), width); 278 width = std::max(size_in_pixel.width(), width);
472 height = std::max(size_in_pixel.height(), height); 279 height = std::max(size_in_pixel.height(), height);
473 } 280 }
474 return gfx::Size(width, height); 281 return gfx::Size(width, height);
475 } 282 }
476 283
477 } // namespace ash 284 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698