| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "athena/activity/activity_manager_impl.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 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" | |
| 19 | |
| 20 namespace athena { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 ActivityManager* instance = nullptr; | |
| 25 | |
| 26 views::Widget* CreateWidget(Activity* activity) { | |
| 27 aura::Window* window = activity->GetWindow(); | |
| 28 views::Widget* widget = | |
| 29 window ? views::Widget::GetWidgetForNativeView(window) : nullptr; | |
| 30 if (widget) | |
| 31 return widget; | |
| 32 widget = new views::Widget; | |
| 33 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW); | |
| 34 params.delegate = | |
| 35 new ActivityWidgetDelegate(activity->GetActivityViewModel()); | |
| 36 widget->Init(params); | |
| 37 return widget; | |
| 38 } | |
| 39 | |
| 40 views::Widget* GetWidget(Activity* activity) { | |
| 41 CHECK(activity); | |
| 42 CHECK(activity->GetWindow()); | |
| 43 return views::Widget::GetWidgetForNativeWindow(activity->GetWindow()); | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 ActivityManagerImpl::ActivityManagerImpl() { | |
| 49 CHECK(!instance); | |
| 50 instance = this; | |
| 51 | |
| 52 aura::Window* root_window = | |
| 53 ScreenManager::Get()->GetContext()->GetRootWindow(); | |
| 54 aura::client::GetActivationClient(root_window)->AddObserver(this); | |
| 55 } | |
| 56 | |
| 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()); | |
| 63 | |
| 64 CHECK_EQ(this, instance); | |
| 65 instance = nullptr; | |
| 66 } | |
| 67 | |
| 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(); | |
| 75 | |
| 76 FOR_EACH_OBSERVER(ActivityManagerObserver, | |
| 77 observers_, | |
| 78 OnActivityStarted(activity)); | |
| 79 } | |
| 80 | |
| 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()) { | |
| 86 FOR_EACH_OBSERVER( | |
| 87 ActivityManagerObserver, observers_, OnActivityEnding(activity)); | |
| 88 activities_.erase(find); | |
| 89 views::Widget* widget = GetWidget(activity); | |
| 90 widget->GetNativeView()->RemoveObserver(this); | |
| 91 widget->Close(); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 const ActivityList& ActivityManagerImpl::GetActivityList() { | |
| 96 return activities_; | |
| 97 } | |
| 98 | |
| 99 Activity* ActivityManagerImpl::GetActivityForWindow(aura::Window* window) { | |
| 100 struct Matcher { | |
| 101 Matcher(aura::Window* w) : window(w) {} | |
| 102 bool operator()(Activity* activity) { | |
| 103 return activity->GetWindow() == window; | |
| 104 } | |
| 105 aura::Window* window; | |
| 106 }; | |
| 107 std::vector<Activity*>::iterator iter = | |
| 108 std::find_if(activities_.begin(), activities_.end(), Matcher(window)); | |
| 109 return iter != activities_.end() ? *iter : nullptr; | |
| 110 } | |
| 111 | |
| 112 void ActivityManagerImpl::AddObserver(ActivityManagerObserver* observer) { | |
| 113 observers_.AddObserver(observer); | |
| 114 } | |
| 115 | |
| 116 void ActivityManagerImpl::RemoveObserver(ActivityManagerObserver* observer) { | |
| 117 observers_.RemoveObserver(observer); | |
| 118 } | |
| 119 | |
| 120 void ActivityManagerImpl::OnWindowDestroying(aura::Window* window) { | |
| 121 Activity* activity = GetActivityForWindow(window); | |
| 122 if (activity) { | |
| 123 window->RemoveObserver(this); | |
| 124 Activity::Delete(activity); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 void ActivityManagerImpl::OnWindowActivated(aura::Window* gained_active, | |
| 129 aura::Window* lost_active) { | |
| 130 Activity* activity = GetActivityForWindow(gained_active); | |
| 131 if (!activity) | |
| 132 return; | |
| 133 CHECK(!activities_.empty()); | |
| 134 if (activity == activities_.front()) | |
| 135 return; | |
| 136 // Move the activity for |gained_active| at the front of the list. | |
| 137 ActivityList::reverse_iterator iter = std::find(activities_.rbegin(), | |
| 138 activities_.rend(), | |
| 139 activity); | |
| 140 CHECK(iter != activities_.rend()); | |
| 141 std::rotate(iter, iter + 1, activities_.rend()); | |
| 142 FOR_EACH_OBSERVER(ActivityManagerObserver, | |
| 143 observers_, | |
| 144 OnActivityOrderChanged()); | |
| 145 } | |
| 146 | |
| 147 // static | |
| 148 ActivityManager* ActivityManager::Create() { | |
| 149 new ActivityManagerImpl(); | |
| 150 CHECK(instance); | |
| 151 return instance; | |
| 152 } | |
| 153 | |
| 154 ActivityManager* ActivityManager::Get() { | |
| 155 return instance; | |
| 156 } | |
| 157 | |
| 158 void ActivityManager::Shutdown() { | |
| 159 CHECK(instance); | |
| 160 delete instance; | |
| 161 } | |
| 162 | |
| 163 | |
| 164 } // namespace athena | |
| OLD | NEW |