e12e8bb32d1b02a9c42379d32849f39bd41201cf
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / ash / launcher / app_window_launcher_controller.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 "chrome/browser/ui/ash/launcher/app_window_launcher_controller.h"
6
7 #include "apps/app_window.h"
8 #include "ash/shelf/shelf_util.h"
9 #include "ash/shell.h"
10 #include "ash/wm/window_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/ash/launcher/app_window_launcher_item_controller.h"
14 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
15 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
16 #include "chrome/browser/ui/host_desktop.h"
17 #include "extensions/common/extension.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #include "ui/wm/public/activation_client.h"
20
21 using apps::AppWindow;
22
23 namespace {
24
25 std::string GetAppShelfId(AppWindow* app_window) {
26   if (app_window->window_type_is_panel())
27     return base::StringPrintf("panel:%d", app_window->session_id().id());
28   return app_window->extension()->id();
29 }
30
31 bool ControlsWindow(aura::Window* window) {
32   return chrome::GetHostDesktopTypeForNativeWindow(window) ==
33          chrome::HOST_DESKTOP_TYPE_ASH;
34 }
35
36 }  // namespace
37
38 AppWindowLauncherController::AppWindowLauncherController(
39     ChromeLauncherController* owner)
40     : owner_(owner), activation_client_(NULL) {
41   apps::AppWindowRegistry* registry =
42       apps::AppWindowRegistry::Get(owner->profile());
43   registry_.insert(registry);
44   registry->AddObserver(this);
45   if (ash::Shell::HasInstance()) {
46     if (ash::Shell::GetInstance()->GetPrimaryRootWindow()) {
47       activation_client_ = aura::client::GetActivationClient(
48           ash::Shell::GetInstance()->GetPrimaryRootWindow());
49       if (activation_client_)
50         activation_client_->AddObserver(this);
51     }
52   }
53 }
54
55 AppWindowLauncherController::~AppWindowLauncherController() {
56   for (std::set<apps::AppWindowRegistry*>::iterator it = registry_.begin();
57        it != registry_.end();
58        ++it)
59     (*it)->RemoveObserver(this);
60
61   if (activation_client_)
62     activation_client_->RemoveObserver(this);
63   for (WindowToAppShelfIdMap::iterator iter =
64            window_to_app_shelf_id_map_.begin();
65        iter != window_to_app_shelf_id_map_.end();
66        ++iter) {
67     iter->first->RemoveObserver(this);
68   }
69 }
70
71 void AppWindowLauncherController::AdditionalUserAddedToSession(
72     Profile* profile) {
73   // TODO(skuhne): This was added for the legacy side by side mode in M32. If
74   // this mode gets no longer pursued this special case can be removed.
75   if (chrome::MultiUserWindowManager::GetMultiProfileMode() !=
76       chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_MIXED)
77     return;
78
79   apps::AppWindowRegistry* registry = apps::AppWindowRegistry::Get(profile);
80   if (registry_.find(registry) != registry_.end())
81     return;
82
83   registry->AddObserver(this);
84   registry_.insert(registry);
85 }
86
87 void AppWindowLauncherController::OnAppWindowAdded(AppWindow* app_window) {
88   if (!ControlsWindow(app_window->GetNativeWindow()))
89     return;
90   RegisterApp(app_window);
91 }
92
93 void AppWindowLauncherController::OnAppWindowIconChanged(
94     AppWindow* app_window) {
95   if (!ControlsWindow(app_window->GetNativeWindow()))
96     return;
97
98   const std::string app_shelf_id = GetAppShelfId(app_window);
99   AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id);
100   if (iter == app_controller_map_.end())
101     return;
102   AppWindowLauncherItemController* controller = iter->second;
103   controller->set_image_set_by_controller(true);
104   owner_->SetLauncherItemImage(controller->shelf_id(),
105                                app_window->app_icon().AsImageSkia());
106 }
107
108 void AppWindowLauncherController::OnAppWindowRemoved(AppWindow* app_window) {
109   // Do nothing here; app_window->window() has already been deleted and
110   // OnWindowDestroying() has been called, doing the removal.
111 }
112
113 // Called from aura::Window::~Window(), before delegate_->OnWindowDestroyed()
114 // which destroys AppWindow, so both |window| and the associated AppWindow
115 // are valid here.
116 void AppWindowLauncherController::OnWindowDestroying(aura::Window* window) {
117   if (!ControlsWindow(window))
118     return;
119   UnregisterApp(window);
120 }
121
122 void AppWindowLauncherController::OnWindowActivated(aura::Window* new_active,
123                                                     aura::Window* old_active) {
124   // Make the newly active window the active (first) entry in the controller.
125   AppWindowLauncherItemController* new_controller =
126       ControllerForWindow(new_active);
127   if (new_controller) {
128     new_controller->SetActiveWindow(new_active);
129     owner_->SetItemStatus(new_controller->shelf_id(), ash::STATUS_ACTIVE);
130   }
131
132   // Mark the old active window's launcher item as running (if different).
133   AppWindowLauncherItemController* old_controller =
134       ControllerForWindow(old_active);
135   if (old_controller && old_controller != new_controller)
136     owner_->SetItemStatus(old_controller->shelf_id(), ash::STATUS_RUNNING);
137 }
138
139 void AppWindowLauncherController::RegisterApp(AppWindow* app_window) {
140   aura::Window* window = app_window->GetNativeWindow();
141   // Get the app's shelf identifier and add an entry to the map.
142   DCHECK(window_to_app_shelf_id_map_.find(window) ==
143          window_to_app_shelf_id_map_.end());
144   const std::string app_shelf_id = GetAppShelfId(app_window);
145   window_to_app_shelf_id_map_[window] = app_shelf_id;
146   window->AddObserver(this);
147
148   // Find or create an item controller and launcher item.
149   std::string app_id = app_window->extension()->id();
150   ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window)
151                                     ? ash::STATUS_ACTIVE
152                                     : ash::STATUS_RUNNING;
153   AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id);
154   ash::ShelfID shelf_id = 0;
155   if (iter != app_controller_map_.end()) {
156     AppWindowLauncherItemController* controller = iter->second;
157     DCHECK(controller->app_id() == app_id);
158     shelf_id = controller->shelf_id();
159     controller->AddAppWindow(app_window, status);
160   } else {
161     LauncherItemController::Type type =
162         app_window->window_type_is_panel()
163             ? LauncherItemController::TYPE_APP_PANEL
164             : LauncherItemController::TYPE_APP;
165     AppWindowLauncherItemController* controller =
166         new AppWindowLauncherItemController(type, app_shelf_id, app_id, owner_);
167     controller->AddAppWindow(app_window, status);
168     // If the app shelf id is not unique, and there is already a shelf
169     // item for this app id (e.g. pinned), use that shelf item.
170     if (app_shelf_id == app_id)
171       shelf_id = owner_->GetShelfIDForAppID(app_id);
172     if (shelf_id == 0) {
173       shelf_id = owner_->CreateAppLauncherItem(controller, app_id, status);
174       // Restore any existing app icon and flag as set.
175       const gfx::Image& app_icon = app_window->app_icon();
176       if (!app_icon.IsEmpty()) {
177         owner_->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia());
178         controller->set_image_set_by_controller(true);
179       }
180     } else {
181       owner_->SetItemController(shelf_id, controller);
182     }
183     const std::string app_shelf_id = GetAppShelfId(app_window);
184     app_controller_map_[app_shelf_id] = controller;
185   }
186   owner_->SetItemStatus(shelf_id, status);
187   ash::SetShelfIDForWindow(shelf_id, window);
188 }
189
190 void AppWindowLauncherController::UnregisterApp(aura::Window* window) {
191   WindowToAppShelfIdMap::iterator iter1 =
192       window_to_app_shelf_id_map_.find(window);
193   DCHECK(iter1 != window_to_app_shelf_id_map_.end());
194   std::string app_shelf_id = iter1->second;
195   window_to_app_shelf_id_map_.erase(iter1);
196   window->RemoveObserver(this);
197
198   AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id);
199   DCHECK(iter2 != app_controller_map_.end());
200   AppWindowLauncherItemController* controller = iter2->second;
201   controller->RemoveAppWindowForWindow(window);
202   if (controller->app_window_count() == 0) {
203     // If this is the last window associated with the app shelf id, close the
204     // shelf item.
205     ash::ShelfID shelf_id = controller->shelf_id();
206     owner_->CloseLauncherItem(shelf_id);
207     app_controller_map_.erase(iter2);
208   }
209 }
210
211 bool AppWindowLauncherController::IsRegisteredApp(aura::Window* window) {
212   return window_to_app_shelf_id_map_.find(window) !=
213          window_to_app_shelf_id_map_.end();
214 }
215
216 // Private Methods
217
218 AppWindowLauncherItemController*
219 AppWindowLauncherController::ControllerForWindow(aura::Window* window) {
220   WindowToAppShelfIdMap::iterator iter1 =
221       window_to_app_shelf_id_map_.find(window);
222   if (iter1 == window_to_app_shelf_id_map_.end())
223     return NULL;
224   std::string app_shelf_id = iter1->second;
225   AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id);
226   if (iter2 == app_controller_map_.end())
227     return NULL;
228   return iter2->second;
229 }