Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / athena / content / app_activity_registry.cc
blobda28386659bf29484b4ff6ab9439592254579435
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/content/app_activity_registry.h"
7 #include "athena/activity/public/activity_manager.h"
8 #include "athena/content/app_activity.h"
9 #include "athena/content/app_activity_proxy.h"
10 #include "athena/content/public/app_registry.h"
11 #include "athena/extensions/public/extensions_delegate.h"
12 #include "ui/aura/window.h"
13 #include "ui/views/view.h"
14 #include "ui/views/widget/widget.h"
16 namespace athena {
18 AppActivityRegistry::AppActivityRegistry(
19 const std::string& app_id,
20 content::BrowserContext* browser_context) :
21 app_id_(app_id),
22 browser_context_(browser_context),
23 unloaded_activity_proxy_(NULL) {}
25 AppActivityRegistry::~AppActivityRegistry() {
26 CHECK(activity_list_.empty());
27 if (unloaded_activity_proxy_)
28 ActivityManager::Get()->RemoveActivity(unloaded_activity_proxy_);
29 DCHECK(!unloaded_activity_proxy_);
32 void AppActivityRegistry::RegisterAppActivity(AppActivity* app_activity) {
33 if (unloaded_activity_proxy_) {
34 // Since we add an application window, the activity isn't unloaded anymore.
35 ActivityManager::Get()->RemoveActivity(unloaded_activity_proxy_);
36 // With the removal the object should have been deleted and we should have
37 // been informed of the object's destruction.
38 DCHECK(!unloaded_activity_proxy_);
40 // The same window should never be added twice.
41 CHECK(std::find(activity_list_.begin(),
42 activity_list_.end(),
43 app_activity) == activity_list_.end());
44 activity_list_.push_back(app_activity);
47 void AppActivityRegistry::UnregisterAppActivity(AppActivity* app_activity) {
48 // It is possible that a detach gets called without ever being attached.
49 std::vector<AppActivity*>::iterator it =
50 std::find(activity_list_.begin(), activity_list_.end(), app_activity);
51 if (it == activity_list_.end())
52 return;
54 activity_list_.erase(it);
55 // When the last window gets destroyed and there is no proxy to restart, we
56 // delete ourselves.
57 if (activity_list_.empty() && !unloaded_activity_proxy_) {
58 AppRegistry::Get()->RemoveAppActivityRegistry(this);
59 // after this call this object should be gone.
63 AppActivity* AppActivityRegistry::GetAppActivityAt(size_t index) {
64 if (index >= activity_list_.size())
65 return NULL;
66 return activity_list_[index];
69 void AppActivityRegistry::Unload() {
70 CHECK(!unloaded_activity_proxy_);
71 DCHECK(!activity_list_.empty());
73 // In order to allow an entire application to unload we require that all of
74 // its activities are marked as unloaded.
75 for (std::vector<AppActivity*>::iterator it = activity_list_.begin();
76 it != activity_list_.end(); ++it) {
77 if ((*it)->GetCurrentState() != Activity::ACTIVITY_UNLOADED)
78 return;
81 // Create an activity proxy which can be used to re-activate the app. Insert
82 // the proxy then into the activity stream at the location of the (newest)
83 // current activity.
84 unloaded_activity_proxy_ =
85 new AppActivityProxy(activity_list_[0]->GetActivityViewModel(), this);
86 ActivityManager::Get()->AddActivity(unloaded_activity_proxy_);
87 // The new activity should be in the place of the most recently used app
88 // window. To get it there, we get the most recently used application window
89 // and place the proxy activities window in front or behind, so that when the
90 // activity disappears it takes its place.
91 MoveBeforeMruApplicationWindow(unloaded_activity_proxy_->GetWindow());
93 // Unload the application. This operation will be asynchronous.
94 if (!ExtensionsDelegate::Get(browser_context_)->UnloadApp(app_id_)) {
95 while(!activity_list_.empty())
96 delete activity_list_.back();
100 void AppActivityRegistry::ProxyDestroyed(AppActivityProxy* proxy) {
101 DCHECK_EQ(unloaded_activity_proxy_, proxy);
102 unloaded_activity_proxy_ = NULL;
103 if (activity_list_.empty()) {
104 AppRegistry::Get()->RemoveAppActivityRegistry(this);
105 // |This| is gone now.
109 void AppActivityRegistry::RestartApplication(AppActivityProxy* proxy) {
110 DCHECK_EQ(unloaded_activity_proxy_, proxy);
111 // Restart the application.
112 ExtensionsDelegate::Get(browser_context_)->LaunchApp(app_id_);
113 // Delete the activity which will also remove the it from the ActivityManager.
114 delete unloaded_activity_proxy_; // Will call ProxyDestroyed.
115 // After this call |this| might be gone if the app did not open a window yet.
118 void AppActivityRegistry::MoveBeforeMruApplicationWindow(aura::Window* window) {
119 DCHECK(activity_list_.size());
120 // TODO(skuhne): This needs to be changed to some kind of delegate which
121 // resides in the window manager.
122 const aura::Window::Windows children =
123 activity_list_[0]->GetWindow()->parent()->children();;
124 // Find the first window in the container which is part of the application.
125 for (aura::Window::Windows::const_iterator child_iterator = children.begin();
126 child_iterator != children.end(); ++child_iterator) {
127 for (std::vector<AppActivity*>::iterator app_iterator =
128 activity_list_.begin();
129 app_iterator != activity_list_.end(); ++app_iterator) {
130 if (*child_iterator == (*app_iterator)->GetWindow()) {
131 // Since "StackChildBelow" does not change the order if the window
132 // if the window is below - but not immediately behind - the target
133 // window, we re-stack both ways.
134 window->parent()->StackChildBelow(window, *child_iterator);
135 window->parent()->StackChildBelow(*child_iterator, window);
136 return;
140 NOTREACHED() << "The application does not get tracked by the mru list";
143 } // namespace athena