Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / athena / activity / activity_view_manager_impl.cc
blob3dc8c7cf5b1f71b17a7ba03d7801203ac8246f68
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/public/activity_view_manager.h"
7 #include <algorithm>
8 #include <map>
10 #include "athena/activity/activity_widget_delegate.h"
11 #include "athena/activity/public/activity.h"
12 #include "athena/activity/public/activity_manager.h"
13 #include "athena/activity/public/activity_view_model.h"
14 #include "athena/screen/public/screen_manager.h"
15 #include "ui/aura/window.h"
16 #include "ui/views/view.h"
17 #include "ui/views/widget/widget.h"
18 #include "ui/views/widget/widget_observer.h"
20 namespace athena {
21 namespace {
23 typedef std::map<Activity*, views::Widget*> ActivityWidgetMap;
25 views::Widget* CreateWidget(Activity* activity) {
26 ActivityViewModel* view_model = activity->GetActivityViewModel();
27 views::Widget* widget = new views::Widget;
28 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
29 params.delegate = new ActivityWidgetDelegate(view_model);
30 params.activatable = views::Widget::InitParams::ACTIVATABLE_YES;
31 widget->Init(params);
32 activity->GetActivityViewModel()->Init();
33 return widget;
36 ActivityViewManager* instance = NULL;
38 class ActivityViewManagerImpl : public ActivityViewManager,
39 public views::WidgetObserver {
40 public:
41 ActivityViewManagerImpl() {
42 CHECK(!instance);
43 instance = this;
46 virtual ~ActivityViewManagerImpl() {
47 CHECK_EQ(this, instance);
48 instance = NULL;
51 // ActivityViewManager:
52 virtual void AddActivity(Activity* activity) OVERRIDE {
53 CHECK(activity_widgets_.end() == activity_widgets_.find(activity));
54 views::Widget* container = CreateWidget(activity);
55 container->AddObserver(this);
56 activity_widgets_[activity] = container;
57 container->UpdateWindowTitle();
58 container->Show();
59 container->Activate();
62 virtual void RemoveActivity(Activity* activity) OVERRIDE {
63 ActivityWidgetMap::iterator find = activity_widgets_.find(activity);
64 if (find != activity_widgets_.end()) {
65 views::Widget* widget = find->second;
66 widget->RemoveObserver(this);
67 widget->Close();
68 activity_widgets_.erase(activity);
72 virtual void UpdateActivity(Activity* activity) OVERRIDE {
73 ActivityWidgetMap::iterator find = activity_widgets_.find(activity);
74 if (find != activity_widgets_.end())
75 find->second->UpdateWindowTitle();
78 // views::WidgetObserver:
79 virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE {
80 for (ActivityWidgetMap::iterator iter = activity_widgets_.begin();
81 iter != activity_widgets_.end();
82 ++iter) {
83 if (iter->second == widget) {
84 delete iter->first;
85 break;
90 private:
91 ActivityWidgetMap activity_widgets_;
93 DISALLOW_COPY_AND_ASSIGN(ActivityViewManagerImpl);
96 } // namespace
98 // static
99 ActivityViewManager* ActivityViewManager::Create() {
100 new ActivityViewManagerImpl();
101 CHECK(instance);
102 return instance;
105 ActivityViewManager* ActivityViewManager::Get() {
106 return instance;
109 void ActivityViewManager::Shutdown() {
110 CHECK(instance);
111 delete instance;
114 } // namespace athena