summaryrefslogtreecommitdiffstats
path: root/ash/desktop_background/desktop_background_controller.cc
blob: 2d49b4d18cd27d7c67b7d577080e3b74d7c976d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/desktop_background/desktop_background_controller.h"

#include "ash/desktop_background/desktop_background_view.h"
#include "ash/desktop_background/desktop_background_widget_controller.h"
#include "ash/shell.h"
#include "ash/shell_factory.h"
#include "ash/shell_window_ids.h"
#include "ash/wm/root_window_layout_manager.h"
#include "base/bind.h"
#include "base/logging.h"
#include "base/synchronization/cancellation_flag.h"
#include "base/threading/worker_pool.h"
#include "grit/ui_resources.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/image/image.h"
#include "ui/views/widget/widget.h"

namespace ash {
namespace {

const int kSmallWallpaperMaximalWidth = 1366;
const int kSmallWallpaperMaximalHeight = 800;
const SkColor kTransparentColor = SkColorSetARGB(0x00, 0x00, 0x00, 0x00);

internal::RootWindowLayoutManager* GetRootWindowLayoutManager(
    aura::RootWindow* root_window) {
  return static_cast<internal::RootWindowLayoutManager*>(
      root_window->layout_manager());
}
}  // namespace

// Stores the current wallpaper data.
struct DesktopBackgroundController::WallpaperData {
  WallpaperData(int index, WallpaperResolution resolution)
      : wallpaper_index(index),
        wallpaper_layout(GetWallpaperViewInfo(index, resolution).layout),
        wallpaper_image(*(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
            GetWallpaperViewInfo(index, resolution).id).ToImageSkia())) {
  }
  WallpaperData(WallpaperLayout layout, const gfx::ImageSkia& image)
      : wallpaper_index(-1),
        wallpaper_layout(layout),
        wallpaper_image(image) {
  }
  const int wallpaper_index;
  const WallpaperLayout wallpaper_layout;
  const gfx::ImageSkia wallpaper_image;
};

// DesktopBackgroundController::WallpaperOperation wraps background wallpaper
// loading.
class DesktopBackgroundController::WallpaperOperation
    : public base::RefCountedThreadSafe<
          DesktopBackgroundController::WallpaperOperation> {
 public:
  WallpaperOperation(int index, WallpaperResolution resolution)
      : index_(index),
        resolution_(resolution) {
  }

  static void Run(scoped_refptr<WallpaperOperation> wo) {
    wo->LoadingWallpaper();
  }

  void LoadingWallpaper() {
    if (cancel_flag_.IsSet())
      return;
    wallpaper_data_.reset(new WallpaperData(index_, resolution_));
  }

  void Cancel() {
    cancel_flag_.Set();
  }

  WallpaperData* ReleaseWallpaperData() {
    return wallpaper_data_.release();
  }

 private:
  friend class base::RefCountedThreadSafe<
      DesktopBackgroundController::WallpaperOperation>;

  ~WallpaperOperation() {}

  base::CancellationFlag cancel_flag_;

  scoped_ptr<WallpaperData> wallpaper_data_;

  const int index_;

  const WallpaperResolution resolution_;

  DISALLOW_COPY_AND_ASSIGN(WallpaperOperation);
};

DesktopBackgroundController::DesktopBackgroundController()
    : locked_(false),
      desktop_background_mode_(BACKGROUND_SOLID_COLOR),
      background_color_(kTransparentColor),
      weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
  InstallComponentForAllWindows();
}

DesktopBackgroundController::~DesktopBackgroundController() {
  CancelPendingWallpaperOperation();
}

gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const {
  if (current_wallpaper_.get())
    return current_wallpaper_->wallpaper_image;
  return gfx::ImageSkia();
}

WallpaperLayout DesktopBackgroundController::GetWallpaperLayout() const {
  if (current_wallpaper_.get())
    return current_wallpaper_->wallpaper_layout;
  return CENTER_CROPPED;
}

SkBitmap DesktopBackgroundController::GetCurrentWallpaperImage() {
  if (desktop_background_mode_ != BACKGROUND_IMAGE)
    return SkBitmap();
  return GetWallpaper();
}

void DesktopBackgroundController::OnRootWindowAdded(
    aura::RootWindow* root_window) {
  // Handle resolution change for "built-in" images."
  if (BACKGROUND_IMAGE == desktop_background_mode_) {
    if (current_wallpaper_.get()) {
      gfx::Size root_window_size = root_window->GetHostSize();
      int wallpaper_width = current_wallpaper_->wallpaper_image.width();
      int wallpaper_height = current_wallpaper_->wallpaper_image.height();
      // Loads a higher resolution wallpaper if needed.
      if ((wallpaper_width < root_window_size.width() ||
           wallpaper_height < root_window_size.height()) &&
          current_wallpaper_->wallpaper_index != -1 &&
          current_wallpaper_->wallpaper_layout != TILE)
        SetDefaultWallpaper(current_wallpaper_->wallpaper_index, true);
    }
  }

  InstallComponent(root_window);
}

void DesktopBackgroundController::CacheDefaultWallpaper(int index) {
  DCHECK(index >= 0);

  WallpaperResolution resolution = GetAppropriateResolution();
  scoped_refptr<WallpaperOperation> wallpaper_op =
      new WallpaperOperation(index, resolution);
  base::WorkerPool::PostTask(
      FROM_HERE,
      base::Bind(&WallpaperOperation::Run, wallpaper_op),
      true);
}

void DesktopBackgroundController::SetDefaultWallpaper(int index,
                                                      bool force_reload) {
  // We should not change background when index is invalid. For instance, at
  // login screen or stub_user login.
  if (index == GetInvalidWallpaperIndex()) {
    CreateEmptyWallpaper();
    return;
  } else if (index == GetSolidColorIndex()) {
    SetDesktopBackgroundSolidColorMode(kLoginWallpaperColor);
    return;
  }

  if (!force_reload && current_wallpaper_.get() &&
      current_wallpaper_->wallpaper_index == index)
    return;

  CancelPendingWallpaperOperation();

  WallpaperResolution resolution = GetAppropriateResolution();

  wallpaper_op_ = new WallpaperOperation(index, resolution);
  base::WorkerPool::PostTaskAndReply(
      FROM_HERE,
      base::Bind(&WallpaperOperation::Run, wallpaper_op_),
      base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted,
                 weak_ptr_factory_.GetWeakPtr(),
                 wallpaper_op_),
      true /* task_is_slow */);
}

void DesktopBackgroundController::SetCustomWallpaper(
    const gfx::ImageSkia& wallpaper,
    WallpaperLayout layout) {
  CancelPendingWallpaperOperation();
  current_wallpaper_.reset(new WallpaperData(layout, wallpaper));
  SetDesktopBackgroundImageMode();
}

void DesktopBackgroundController::CancelPendingWallpaperOperation() {
  // Set canceled flag of previous request to skip unneeded loading.
  if (wallpaper_op_.get())
    wallpaper_op_->Cancel();

  // Cancel reply callback for previous request.
  weak_ptr_factory_.InvalidateWeakPtrs();
}

void DesktopBackgroundController::SetDesktopBackgroundSolidColorMode(
    SkColor color) {
  background_color_ = color;
  if (desktop_background_mode_ != BACKGROUND_SOLID_COLOR) {
    desktop_background_mode_ = BACKGROUND_SOLID_COLOR;
    InstallComponentForAllWindows();
    return;
  }

  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
  for (Shell::RootWindowList::iterator iter = root_windows.begin();
       iter != root_windows.end(); ++iter) {
    aura::RootWindow* root_window = *iter;
    internal::DesktopBackgroundWidgetController* component = root_window->
        GetProperty(internal::kWindowDesktopComponent);
    DCHECK(component);
    DCHECK(component->layer());
    component->layer()->SetColor(background_color_ );
  }
}

void DesktopBackgroundController::CreateEmptyWallpaper() {
  current_wallpaper_.reset(NULL);
  SetDesktopBackgroundImageMode();
}

void DesktopBackgroundController::MoveDesktopToLockedContainer() {
  if (locked_)
    return;
  locked_ = true;
  ReparentBackgroundWidgets(GetBackgroundContainerId(false),
                            GetBackgroundContainerId(true));
}

void DesktopBackgroundController::MoveDesktopToUnlockedContainer() {
  if (!locked_)
    return;
  locked_ = false;
  ReparentBackgroundWidgets(GetBackgroundContainerId(true),
                            GetBackgroundContainerId(false));
}

void DesktopBackgroundController::OnWindowDestroying(aura::Window* window) {
   window->SetProperty(internal::kWindowDesktopComponent,
       static_cast<internal::DesktopBackgroundWidgetController*>(NULL));
}

void DesktopBackgroundController::SetDesktopBackgroundImageMode() {
  if (desktop_background_mode_ != BACKGROUND_IMAGE) {
    desktop_background_mode_ = BACKGROUND_IMAGE;
    InstallComponentForAllWindows();
    return;
  }

  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
  for (Shell::RootWindowList::iterator iter = root_windows.begin();
      iter != root_windows.end(); ++iter) {
    aura::RootWindow* root_window = *iter;
    internal::DesktopBackgroundWidgetController* component = root_window->
        GetProperty(internal::kWindowDesktopComponent);
    DCHECK(component);
    DCHECK(component->widget());
    aura::Window* window = component->widget()->GetNativeView();
    gfx::Rect bounds = window->bounds();
    window->SchedulePaintInRect(gfx::Rect(0, 0,
                                          bounds.width(), bounds.height()));
  }
}

void DesktopBackgroundController::OnWallpaperLoadCompleted(
    scoped_refptr<WallpaperOperation> wo) {
  current_wallpaper_.reset(wo->ReleaseWallpaperData());

  SetDesktopBackgroundImageMode();

  DCHECK(wo.get() == wallpaper_op_.get());
  wallpaper_op_ = NULL;
}

ui::Layer* DesktopBackgroundController::SetColorLayerForContainer(
    SkColor color,
    aura::RootWindow* root_window,
    int container_id) {
  ui::Layer* background_layer = new ui::Layer(ui::LAYER_SOLID_COLOR);
  background_layer->SetColor(color);

  Shell::GetContainer(root_window,container_id)->
      layer()->Add(background_layer);
  return background_layer;
}

void DesktopBackgroundController::InstallComponent(
    aura::RootWindow* root_window) {
  internal::DesktopBackgroundWidgetController* component = NULL;
  int container_id = GetBackgroundContainerId(locked_);

  switch (desktop_background_mode_) {
    case BACKGROUND_IMAGE: {
      views::Widget* widget = internal::CreateDesktopBackground(root_window,
                                                                container_id);
      component = new internal::DesktopBackgroundWidgetController(widget);
      break;
    }
    case BACKGROUND_SOLID_COLOR: {
      ui::Layer* layer = SetColorLayerForContainer(background_color_,
                                                   root_window,
                                                   container_id);
      component = new internal::DesktopBackgroundWidgetController(layer);
      break;
    }
    default: {
      NOTREACHED();
    }
  }
  if (NULL == root_window->GetProperty(internal::kWindowDesktopComponent)) {
    // First time for this root window
    root_window->AddObserver(this);
  }
  root_window->SetProperty(internal::kWindowDesktopComponent, component);
}

void DesktopBackgroundController::InstallComponentForAllWindows() {
  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
  for (Shell::RootWindowList::iterator iter = root_windows.begin();
       iter != root_windows.end(); ++iter) {
    InstallComponent(*iter);
  }
}

void DesktopBackgroundController::ReparentBackgroundWidgets(int src_container,
                                                            int dst_container) {
  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
  for (Shell::RootWindowList::iterator iter = root_windows.begin();
    iter != root_windows.end(); ++iter) {
    aura::RootWindow* root_window = *iter;
    internal::DesktopBackgroundWidgetController* component = root_window->
        GetProperty(internal::kWindowDesktopComponent);
    DCHECK(component);
    component->Reparent(root_window,
                        src_container,
                        dst_container);
  }
}

int DesktopBackgroundController::GetBackgroundContainerId(bool locked) {
  return locked ? internal::kShellWindowId_LockScreenBackgroundContainer :
                  internal::kShellWindowId_DesktopBackgroundContainer;
}

WallpaperResolution DesktopBackgroundController::GetAppropriateResolution() {
  WallpaperResolution resolution = SMALL;
  Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
  for (Shell::RootWindowList::iterator iter = root_windows.begin();
       iter != root_windows.end(); ++iter) {
    gfx::Size root_window_size = (*iter)->GetHostSize();
    if (root_window_size.width() > kSmallWallpaperMaximalWidth ||
        root_window_size.height() > kSmallWallpaperMaximalHeight)
      resolution = LARGE;
  }
  return resolution;
}

}  // namespace ash