Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ash / first_run / desktop_cleaner.cc
1 // Copyright 2013 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 "ash/first_run/desktop_cleaner.h"
6
7 #include "ash/shell.h"
8 #include "ash/shell_window_ids.h"
9 #include "ui/aura/window_event_dispatcher.h"
10 #include "ui/aura/window_observer.h"
11 #include "ui/compositor/layer_animation_observer.h"
12 #include "ui/compositor/scoped_layer_animation_settings.h"
13 #include "ui/message_center/message_center.h"
14 #include "ui/message_center/notification_blocker.h"
15
16 namespace ash {
17 namespace {
18
19 const int kContainerIdsToHide[] = {
20   kShellWindowId_DefaultContainer,
21   kShellWindowId_AlwaysOnTopContainer,
22   kShellWindowId_PanelContainer,
23   // TODO(dzhioev): uncomment this when issue with BrowserView::CanActivate
24   // will be fixed.
25   // kShellWindowId_SystemModalContainer
26 };
27
28 }  // namespace
29
30 class ContainerHider : public aura::WindowObserver,
31                        public ui::ImplicitAnimationObserver {
32  public:
33   explicit ContainerHider(aura::Window* container)
34       : container_was_hidden_(!container->IsVisible()),
35         container_(container) {
36     if (container_was_hidden_)
37       return;
38     ui::Layer* layer = container_->layer();
39     ui::ScopedLayerAnimationSettings animation_settings(layer->GetAnimator());
40     animation_settings.AddObserver(this);
41     layer->SetOpacity(0.0);
42   }
43
44   ~ContainerHider() override {
45     if (container_was_hidden_ || !container_)
46       return;
47     if (!WasAnimationCompletedForProperty(ui::LayerAnimationElement::OPACITY)) {
48       // We are in the middle of animation.
49       StopObservingImplicitAnimations();
50     } else {
51       container_->Show();
52     }
53     ui::Layer* layer = container_->layer();
54     ui::ScopedLayerAnimationSettings animation_settings(layer->GetAnimator());
55     layer->SetOpacity(1.0);
56   }
57
58  private:
59   // Overriden from ui::ImplicitAnimationObserver.
60   void OnImplicitAnimationsCompleted() override { container_->Hide(); }
61
62   // Overriden from aura::WindowObserver.
63   void OnWindowDestroying(aura::Window* window) override {
64     DCHECK(window == container_);
65     container_ = NULL;
66   }
67
68   const bool container_was_hidden_;
69   aura::Window* container_;
70
71   DISALLOW_COPY_AND_ASSIGN(ContainerHider);
72 };
73
74 class NotificationBlocker : public message_center::NotificationBlocker {
75  public:
76   NotificationBlocker()
77       : message_center::NotificationBlocker(
78             message_center::MessageCenter::Get()) {
79     NotifyBlockingStateChanged();
80   }
81
82   ~NotificationBlocker() override {}
83
84  private:
85   // Overriden from message_center::NotificationBlocker.
86   bool ShouldShowNotificationAsPopup(
87       const message_center::NotifierId& notifier_id) const override {
88     return false;
89   }
90
91   DISALLOW_COPY_AND_ASSIGN(NotificationBlocker);
92 };
93
94 DesktopCleaner::DesktopCleaner() {
95   // TODO(dzhioev): Add support for secondary displays.
96   aura::Window* root_window = Shell::GetInstance()->GetPrimaryRootWindow();
97   for (size_t i = 0; i < arraysize(kContainerIdsToHide); ++i) {
98     aura::Window* container =
99         Shell::GetContainer(root_window, kContainerIdsToHide[i]);
100     container_hiders_.push_back(make_linked_ptr(new ContainerHider(container)));
101   }
102   notification_blocker_.reset(new NotificationBlocker());
103 }
104
105 DesktopCleaner::~DesktopCleaner() {}
106
107 // static
108 std::vector<int> DesktopCleaner::GetContainersToHideForTest() {
109   return std::vector<int>(kContainerIdsToHide,
110                           kContainerIdsToHide + arraysize(kContainerIdsToHide));
111 }
112
113 }  // namespace ash
114