1 // Copyright 2014 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.
5 #include "athena/activity/activity_manager_impl.h"
9 #include "athena/activity/activity_widget_delegate.h"
10 #include "athena/activity/public/activity.h"
11 #include "athena/activity/public/activity_manager_observer.h"
12 #include "athena/activity/public/activity_view_model.h"
13 #include "athena/screen/public/screen_manager.h"
14 #include "base/logging.h"
15 #include "base/observer_list.h"
16 #include "ui/aura/window.h"
17 #include "ui/views/widget/widget.h"
18 #include "ui/wm/public/activation_client.h"
24 ActivityManager
* instance
= nullptr;
26 views::Widget
* CreateWidget(Activity
* activity
) {
27 aura::Window
* window
= activity
->GetWindow();
28 views::Widget
* widget
=
29 window
? views::Widget::GetWidgetForNativeView(window
) : nullptr;
32 widget
= new views::Widget
;
33 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_WINDOW
);
35 new ActivityWidgetDelegate(activity
->GetActivityViewModel());
40 views::Widget
* GetWidget(Activity
* activity
) {
42 CHECK(activity
->GetWindow());
43 return views::Widget::GetWidgetForNativeWindow(activity
->GetWindow());
48 ActivityManagerImpl::ActivityManagerImpl() {
52 aura::Window
* root_window
=
53 ScreenManager::Get()->GetContext()->GetRootWindow();
54 aura::client::GetActivationClient(root_window
)->AddObserver(this);
57 ActivityManagerImpl::~ActivityManagerImpl() {
58 aura::Window
* root_window
=
59 ScreenManager::Get()->GetContext()->GetRootWindow();
60 aura::client::GetActivationClient(root_window
)->RemoveObserver(this);
61 while (!activities_
.empty())
62 Activity::Delete(activities_
.front());
64 CHECK_EQ(this, instance
);
68 void ActivityManagerImpl::AddActivity(Activity
* activity
) {
69 CHECK(activities_
.end() ==
70 std::find(activities_
.begin(), activities_
.end(), activity
));
71 activities_
.insert(activities_
.begin(), activity
);
72 views::Widget
* widget
= CreateWidget(activity
);
73 widget
->GetNativeView()->AddObserver(this);
74 activity
->GetActivityViewModel()->Init();
76 FOR_EACH_OBSERVER(ActivityManagerObserver
,
78 OnActivityStarted(activity
));
81 void ActivityManagerImpl::RemoveActivity(Activity
* activity
) {
82 std::vector
<Activity
*>::iterator find
=
83 std::find(activities_
.begin(), activities_
.end(), activity
);
84 DCHECK(find
!= activities_
.end());
85 if (find
!= activities_
.end()) {
87 ActivityManagerObserver
, observers_
, OnActivityEnding(activity
));
88 activities_
.erase(find
);
89 views::Widget
* widget
= GetWidget(activity
);
90 widget
->GetNativeView()->RemoveObserver(this);
95 const ActivityList
& ActivityManagerImpl::GetActivityList() {
99 Activity
* ActivityManagerImpl::GetActivityForWindow(aura::Window
* window
) {
101 Matcher(aura::Window
* w
) : window(w
) {}
102 bool operator()(Activity
* activity
) {
103 return activity
->GetWindow() == window
;
105 aura::Window
* window
;
107 std::vector
<Activity
*>::iterator iter
=
108 std::find_if(activities_
.begin(), activities_
.end(), Matcher(window
));
109 return iter
!= activities_
.end() ? *iter
: nullptr;
112 void ActivityManagerImpl::AddObserver(ActivityManagerObserver
* observer
) {
113 observers_
.AddObserver(observer
);
116 void ActivityManagerImpl::RemoveObserver(ActivityManagerObserver
* observer
) {
117 observers_
.RemoveObserver(observer
);
120 void ActivityManagerImpl::OnWindowDestroying(aura::Window
* window
) {
121 Activity
* activity
= GetActivityForWindow(window
);
123 window
->RemoveObserver(this);
124 Activity::Delete(activity
);
128 void ActivityManagerImpl::OnWindowActivated(aura::Window
* gained_active
,
129 aura::Window
* lost_active
) {
130 Activity
* activity
= GetActivityForWindow(gained_active
);
133 CHECK(!activities_
.empty());
134 if (activity
== activities_
.front())
136 // Move the activity for |gained_active| at the front of the list.
137 ActivityList::reverse_iterator iter
= std::find(activities_
.rbegin(),
140 CHECK(iter
!= activities_
.rend());
141 std::rotate(iter
, iter
+ 1, activities_
.rend());
142 FOR_EACH_OBSERVER(ActivityManagerObserver
,
144 OnActivityOrderChanged());
148 ActivityManager
* ActivityManager::Create() {
149 new ActivityManagerImpl();
154 ActivityManager
* ActivityManager::Get() {
158 void ActivityManager::Shutdown() {
164 } // namespace athena