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/background/ash_user_wallpaper_delegate.h"
6
7#include "ash/desktop_background/user_wallpaper_delegate.h"
8#include "ash/shell.h"
9#include "ash/wm/window_animations.h"
10#include "base/command_line.h"
11#include "base/logging.h"
12#include "chrome/browser/chrome_notification_types.h"
13#include "chrome/browser/chromeos/extensions/wallpaper_manager_util.h"
14#include "chrome/browser/chromeos/login/startup_utils.h"
15#include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
16#include "chrome/browser/chromeos/login/wizard_controller.h"
17#include "chromeos/chromeos_switches.h"
18#include "chromeos/login/login_state.h"
19#include "components/user_manager/user.h"
20#include "components/user_manager/user_manager.h"
21#include "content/public/browser/notification_service.h"
22
23namespace chromeos {
24
25namespace {
26
27bool IsNormalWallpaperChange() {
28  if (chromeos::LoginState::Get()->IsUserLoggedIn() ||
29      !CommandLine::ForCurrentProcess()->HasSwitch(
30          switches::kFirstExecAfterBoot) ||
31      WizardController::IsZeroDelayEnabled() ||
32      !CommandLine::ForCurrentProcess()->HasSwitch(switches::kLoginManager)) {
33    return true;
34  }
35
36  return false;
37}
38
39class UserWallpaperDelegate : public ash::UserWallpaperDelegate {
40 public:
41  UserWallpaperDelegate()
42      : boot_animation_finished_(false),
43        animation_duration_override_in_ms_(0) {
44  }
45
46  virtual ~UserWallpaperDelegate() {
47  }
48
49  virtual int GetAnimationType() OVERRIDE {
50    return ShouldShowInitialAnimation() ?
51        ash::WINDOW_VISIBILITY_ANIMATION_TYPE_BRIGHTNESS_GRAYSCALE :
52        static_cast<int>(wm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE);
53  }
54
55  virtual int GetAnimationDurationOverride() OVERRIDE {
56    return animation_duration_override_in_ms_;
57  }
58
59  virtual void SetAnimationDurationOverride(
60      int animation_duration_in_ms) OVERRIDE {
61    animation_duration_override_in_ms_ = animation_duration_in_ms;
62  }
63
64  virtual bool ShouldShowInitialAnimation() OVERRIDE {
65    if (IsNormalWallpaperChange() || boot_animation_finished_)
66      return false;
67
68    // It is a first boot case now. If kDisableBootAnimation flag
69    // is passed, it only disables any transition after OOBE.
70    bool is_registered = StartupUtils::IsDeviceRegistered();
71    const CommandLine* command_line = CommandLine::ForCurrentProcess();
72    bool disable_boot_animation = command_line->
73        HasSwitch(switches::kDisableBootAnimation);
74    if (is_registered && disable_boot_animation)
75      return false;
76
77    return true;
78  }
79
80  virtual void UpdateWallpaper(bool clear_cache) OVERRIDE {
81    chromeos::WallpaperManager::Get()->UpdateWallpaper(clear_cache);
82  }
83
84  virtual void InitializeWallpaper() OVERRIDE {
85    chromeos::WallpaperManager::Get()->InitializeWallpaper();
86  }
87
88  virtual void OpenSetWallpaperPage() OVERRIDE {
89    if (CanOpenSetWallpaperPage())
90      wallpaper_manager_util::OpenWallpaperManager();
91  }
92
93  virtual bool CanOpenSetWallpaperPage() OVERRIDE {
94    const LoginState* login_state = LoginState::Get();
95    const LoginState::LoggedInUserType user_type =
96        login_state->GetLoggedInUserType();
97    if (!login_state->IsUserLoggedIn())
98      return false;
99
100    // Whitelist user types that are allowed to change their wallpaper.  (Guest
101    // users are not, see crosbug 26900.)
102    if (user_type != LoginState::LOGGED_IN_USER_REGULAR &&
103        user_type != LoginState::LOGGED_IN_USER_OWNER &&
104        user_type != LoginState::LOGGED_IN_USER_PUBLIC_ACCOUNT &&
105        user_type != LoginState::LOGGED_IN_USER_SUPERVISED) {
106      return false;
107    }
108    const user_manager::User* user =
109        user_manager::UserManager::Get()->GetActiveUser();
110    if (!user)
111      return false;
112    if (chromeos::WallpaperManager::Get()->IsPolicyControlled(user->email()))
113      return false;
114    return true;
115  }
116
117  virtual void OnWallpaperAnimationFinished() OVERRIDE {
118    content::NotificationService::current()->Notify(
119        chrome::NOTIFICATION_WALLPAPER_ANIMATION_FINISHED,
120        content::NotificationService::AllSources(),
121        content::NotificationService::NoDetails());
122  }
123
124  virtual void OnWallpaperBootAnimationFinished() OVERRIDE {
125    // Make sure that boot animation type is used only once.
126    boot_animation_finished_ = true;
127  }
128
129 private:
130  bool boot_animation_finished_;
131
132  // The animation duration to show a new wallpaper if an animation is required.
133  int animation_duration_override_in_ms_;
134
135  DISALLOW_COPY_AND_ASSIGN(UserWallpaperDelegate);
136};
137
138}  // namespace
139
140ash::UserWallpaperDelegate* CreateUserWallpaperDelegate() {
141  return new chromeos::UserWallpaperDelegate();
142}
143
144}  // namespace chromeos
145