Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / athena / content / app_activity_registry.cc
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/content/app_activity_registry.h"
6
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 "athena/resource_manager/public/resource_manager.h"
13 #include "athena/wm/public/window_list_provider.h"
14 #include "athena/wm/public/window_manager.h"
15 #include "base/bind.h"
16 #include "base/location.h"
17 #include "base/single_thread_task_runner.h"
18 #include "base/thread_task_runner_handle.h"
19 #include "ui/aura/window.h"
20 #include "ui/views/view.h"
21 #include "ui/views/widget/widget.h"
22
23 namespace athena {
24
25 AppActivityRegistry::AppActivityRegistry(
26     const std::string& app_id,
27     content::BrowserContext* browser_context)
28     : app_id_(app_id),
29       browser_context_(browser_context),
30       unloaded_activity_proxy_(nullptr) {
31 }
32
33 AppActivityRegistry::~AppActivityRegistry() {
34   CHECK(activity_list_.empty());
35   if (unloaded_activity_proxy_)
36     ActivityManager::Get()->RemoveActivity(unloaded_activity_proxy_);
37   DCHECK(!unloaded_activity_proxy_);
38 }
39
40 void AppActivityRegistry::RegisterAppActivity(AppActivity* app_activity) {
41   // The same window should never be added twice.
42   CHECK(std::find(activity_list_.begin(),
43                   activity_list_.end(),
44                   app_activity) == activity_list_.end());
45   activity_list_.push_back(app_activity);
46 }
47
48 void AppActivityRegistry::UnregisterAppActivity(AppActivity* app_activity) {
49   // It is possible that a detach gets called without ever being attached.
50   std::vector<AppActivity*>::iterator it =
51       std::find(activity_list_.begin(), activity_list_.end(), app_activity);
52   if (it == activity_list_.end())
53     return;
54
55   activity_list_.erase(it);
56   // When the last window gets destroyed and there is no proxy to restart, we
57   // delete ourselves.
58   if (activity_list_.empty() && !unloaded_activity_proxy_) {
59     AppRegistry::Get()->RemoveAppActivityRegistry(this);
60     // after this call this object should be gone.
61   }
62 }
63
64 AppActivity* AppActivityRegistry::GetAppActivityAt(size_t index) {
65   if (index >= activity_list_.size())
66     return nullptr;
67   return activity_list_[index];
68 }
69
70 void AppActivityRegistry::Unload() {
71   CHECK(!unloaded_activity_proxy_);
72   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;
79   }
80
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_ = new AppActivityProxy(GetMruActivity(), this);
85   ActivityManager::Get()->AddActivity(unloaded_activity_proxy_);
86   unloaded_activity_proxy_->GetWindow()->SetName("AppActivityProxy");
87
88   // This function can be called through an observer call. When that happens,
89   // several activities will be closed / started. That can then cause a crash.
90   // We postpone therefore the activity destruction till after the observer is
91   // done.
92   base::ThreadTaskRunnerHandle::Get()->PostTask(
93       FROM_HERE,
94       base::Bind(&AppActivityRegistry::DelayedUnload, base::Unretained(this)));
95 }
96
97 void AppActivityRegistry::ProxyDestroyed(AppActivityProxy* proxy) {
98   DCHECK_EQ(unloaded_activity_proxy_, proxy);
99   unloaded_activity_proxy_ = nullptr;
100   if (activity_list_.empty()) {
101     AppRegistry::Get()->RemoveAppActivityRegistry(this);
102     // |This| is gone now.
103   }
104 }
105
106 void AppActivityRegistry::RestartApplication(AppActivityProxy* proxy) {
107   DCHECK_EQ(unloaded_activity_proxy_, proxy);
108   // Restart the application. Note that the first created app window will make
109   // sure that the proxy gets deleted - after - the new activity got moved
110   // to the proxies activity location.
111   ExtensionsDelegate::Get(browser_context_)->LaunchApp(app_id_);
112 }
113
114 void AppActivityRegistry::DelayedUnload() {
115   if (!ExtensionsDelegate::Get(browser_context_)->UnloadApp(app_id_)) {
116     while(!activity_list_.empty())
117       Activity::Delete(activity_list_.back());
118   }
119 }
120
121 AppActivity* AppActivityRegistry::GetMruActivity() {
122   DCHECK(activity_list_.size());
123   WindowListProvider* window_list_provider =
124       WindowManager::Get()->GetWindowListProvider();
125   const aura::Window::Windows& children =
126       window_list_provider->GetWindowList();
127   // Find the first window in the container which is part of the application.
128   for (aura::Window::Windows::const_iterator child_iterator = children.begin();
129       child_iterator != children.end(); ++child_iterator) {
130     for (std::vector<AppActivity*>::iterator app_iterator =
131              activity_list_.begin();
132          app_iterator != activity_list_.end(); ++app_iterator) {
133       if (*child_iterator == (*app_iterator)->GetWindow())
134         return *app_iterator;
135     }
136   }
137   NOTREACHED() << "The application does not get tracked by the mru list";
138   return nullptr;
139 }
140
141 }  // namespace athena