Chromium Code Reviews| Index: athena/content/app_activity_registry.cc |
| diff --git a/athena/content/app_activity_registry.cc b/athena/content/app_activity_registry.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..89273130ef84f832cb036425033240d85d9af288 |
| --- /dev/null |
| +++ b/athena/content/app_activity_registry.cc |
| @@ -0,0 +1,131 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "athena/content/app_activity_registry.h" |
| + |
| +#include "athena/activity/public/activity_manager.h" |
| +#include "athena/content/app_activity.h" |
| +#include "athena/content/app_activity_proxy.h" |
| +#include "athena/content/public/app_content_delegate.h" |
| +#include "athena/content/public/app_registry.h" |
| +#include "ui/aura/window.h" |
| +#include "ui/views/view.h" |
| +#include "ui/views/widget/widget.h" |
| + |
| +namespace athena { |
| + |
| +AppActivityRegistry::AppActivityRegistry( |
| + const std::string& app_id, |
| + content::BrowserContext* browser_context) : |
| + app_id_(app_id), |
| + browser_context_(browser_context), |
| + unloaded_activity_proxy_(NULL) {} |
| + |
| +AppActivityRegistry::~AppActivityRegistry() { |
| + CHECK(activity_list_.empty()); |
| + if (unloaded_activity_proxy_) |
| + ActivityManager::Get()->RemoveActivity(unloaded_activity_proxy_); |
| + DCHECK(!unloaded_activity_proxy_); |
| +} |
| + |
| +void AppActivityRegistry::RegisterAppActivity(AppActivity* app_activity) { |
| + if (unloaded_activity_proxy_) { |
| + // Since we add an application window, the activity isn't unloaded anymore. |
| + ActivityManager::Get()->RemoveActivity(unloaded_activity_proxy_); |
| + // With the removal the object should have been deleted and we should have |
| + // been informed of the object's destruction. |
| + DCHECK(!unloaded_activity_proxy_); |
| + } |
| + // The same window should never be added twice. |
| + CHECK(std::find(activity_list_.begin(), |
| + activity_list_.end(), |
| + app_activity) == activity_list_.end()); |
| + activity_list_.push_back(app_activity); |
| +} |
| + |
| +void AppActivityRegistry::UnregisterAppActivity(AppActivity* app_activity) { |
| + // It is possible that a detach gets called without ever being attached. |
| + std::vector<AppActivity*>::iterator it = |
| + std::find(activity_list_.begin(), activity_list_.end(), app_activity); |
| + if (it == activity_list_.end()) |
| + return; |
| + |
| + activity_list_.erase(it); |
| + // When the last window gets destroyed and there is no proxy to restart, we |
| + // delete ourselves. |
| + if (activity_list_.empty() && !unloaded_activity_proxy_) { |
| + AppRegistry::Get()->RemoveAppActivityRegistry(this); |
| + // after this call this object should be gone. |
| + } |
| +} |
| + |
| +void AppActivityRegistry::Unload() { |
| + CHECK(!unloaded_activity_proxy_); |
| + DCHECK(!activity_list_.empty()); |
| + |
| + // In order to allow an entire application to unload we require that all of |
| + // its activities are marked as unloaded. |
| + for (std::vector<AppActivity*>::iterator it = activity_list_.begin(); |
| + it != activity_list_.end(); ++it) { |
| + if ((*it)->GetCurrentState() != Activity::ACTIVITY_UNLOADED) |
| + return; |
| + } |
| + |
| + // Create an activity proxy which can be used to re-activate the app. Insert |
| + // the proxy then into the activity stream at the location of the (newest) |
| + // current activity. |
| + unloaded_activity_proxy_ = |
| + new AppActivityProxy(activity_list_[0]->GetActivityViewModel(), this); |
| + ActivityManager::Get()->AddActivity(unloaded_activity_proxy_); |
| + // The new activity should be in the place of the most recently used app |
| + // window. To get it there, we get the most recently used application window |
| + // and place the proxy activities window in front or behind, so that when the |
| + // activity disappears it takes its place. |
| + // Note: Since the new app window is newer, it should be over the mru app |
| + // window and therefore we move it below it. |
| + aura::Window* window = unloaded_activity_proxy_->GetWindow(); |
| + aura::Window* mru_app_window = FindMruApplicationWindow(); |
| + DCHECK_EQ(window->parent(), mru_app_window->parent()); |
| + window->parent()->StackChildBelow(window, mru_app_window); |
| + |
| + // Unload the application. This operation will be asynchronous. |
| + if (!AppRegistry::Get()->GetDelegate()->UnloadApplication(app_id_, |
| + browser_context_)) { |
| + activity_list_.clear(); |
| + } |
| +} |
| + |
| +void AppActivityRegistry::ProxyDestroyed(AppActivityProxy* proxy) { |
| + DCHECK_EQ(unloaded_activity_proxy_, proxy); |
| + unloaded_activity_proxy_ = NULL; |
| +} |
| + |
| +void AppActivityRegistry::RestartApplication(AppActivityProxy* proxy) { |
| + DCHECK_EQ(unloaded_activity_proxy_, proxy); |
| + // First remove the activity from the Activity manager. |
| + ActivityManager::Get()->RemoveActivity(unloaded_activity_proxy_); |
| + // Restart the application. |
| + AppRegistry::Get()->GetDelegate()->RestartApplication(app_id_, |
| + browser_context_); |
| +} |
| + |
| +aura::Window* AppActivityRegistry::FindMruApplicationWindow() { |
| + DCHECK(activity_list_.size()); |
| + const aura::Window::Windows children = |
| + activity_list_[0]->GetWindow()->parent()->children(); |
|
oshima
2014/08/15 15:15:59
This should use WindowListProvider (which you prob
Mr4D (OOO till 08-26)
2014/08/18 16:09:32
Hmm... So the order of windows within the children
oshima
2014/08/18 17:51:43
The code above has too much knowledge/assumption t
Mr4D (OOO till 08-26)
2014/08/18 22:36:31
I added a comment to change this once we have a be
oshima
2014/08/18 23:14:01
It's ok to add athena/wm to content/DEPS. It doesn
|
| + // Find the first window in the container which is part of the application. |
| + for (aura::Window::Windows::const_iterator child_iterator = children.begin(); |
| + child_iterator != children.end(); ++child_iterator) { |
| + for (std::vector<AppActivity*>::iterator app_iterator = |
| + activity_list_.begin(); |
| + app_iterator != activity_list_.end(); ++app_iterator) { |
| + if (*child_iterator == (*app_iterator)->GetWindow()) |
| + return *child_iterator; |
| + } |
| + } |
| + NOTREACHED() << "The application does not have a window in this container"; |
| + return NULL; |
| +} |
| + |
| +} // namespace athena |