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 "chrome/browser/ui/ash/launcher/app_window_launcher_controller.h"
7 #include "apps/app_window.h"
8 #include "ash/shelf/shelf_util.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"
21 using apps::AppWindow;
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();
31 bool ControlsWindow(aura::Window* window) {
32 return chrome::GetHostDesktopTypeForNativeWindow(window) ==
33 chrome::HOST_DESKTOP_TYPE_ASH;
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);
55 AppWindowLauncherController::~AppWindowLauncherController() {
56 for (std::set<apps::AppWindowRegistry*>::iterator it = registry_.begin();
57 it != registry_.end();
59 (*it)->RemoveObserver(this);
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();
67 iter->first->RemoveObserver(this);
71 void AppWindowLauncherController::AdditionalUserAddedToSession(
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)
79 apps::AppWindowRegistry* registry = apps::AppWindowRegistry::Get(profile);
80 if (registry_.find(registry) != registry_.end())
83 registry->AddObserver(this);
84 registry_.insert(registry);
87 void AppWindowLauncherController::OnAppWindowAdded(AppWindow* app_window) {
88 #if !defined(OS_CHROMEOS)
89 if (!ControlsWindow(app_window->GetNativeWindow()))
91 RegisterApp(app_window);
95 void AppWindowLauncherController::OnAppWindowIconChanged(
96 AppWindow* app_window) {
97 if (!ControlsWindow(app_window->GetNativeWindow()))
100 const std::string app_shelf_id = GetAppShelfId(app_window);
101 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id);
102 if (iter == app_controller_map_.end())
104 AppWindowLauncherItemController* controller = iter->second;
105 controller->set_image_set_by_controller(true);
106 owner_->SetLauncherItemImage(controller->shelf_id(),
107 app_window->app_icon().AsImageSkia());
110 void AppWindowLauncherController::OnAppWindowRemoved(AppWindow* app_window) {
111 // Do nothing here; app_window->window() has already been deleted and
112 // OnWindowDestroying() has been called, doing the removal.
115 #if defined(OS_CHROMEOS)
116 void AppWindowLauncherController::OnAppWindowShown(AppWindow* app_window) {
117 aura::Window* window = app_window->GetNativeWindow();
118 if (!ControlsWindow(window))
121 if (!IsRegisteredApp(window))
122 RegisterApp(app_window);
125 void AppWindowLauncherController::OnAppWindowHidden(AppWindow* app_window) {
126 aura::Window* window = app_window->GetNativeWindow();
127 if (!ControlsWindow(window))
130 if (IsRegisteredApp(window))
131 UnregisterApp(window);
135 // Called from aura::Window::~Window(), before delegate_->OnWindowDestroyed()
136 // which destroys AppWindow, so both |window| and the associated AppWindow
138 void AppWindowLauncherController::OnWindowDestroying(aura::Window* window) {
139 if (!ControlsWindow(window))
141 UnregisterApp(window);
144 void AppWindowLauncherController::OnWindowActivated(aura::Window* new_active,
145 aura::Window* old_active) {
146 // Make the newly active window the active (first) entry in the controller.
147 AppWindowLauncherItemController* new_controller =
148 ControllerForWindow(new_active);
149 if (new_controller) {
150 new_controller->SetActiveWindow(new_active);
151 owner_->SetItemStatus(new_controller->shelf_id(), ash::STATUS_ACTIVE);
154 // Mark the old active window's launcher item as running (if different).
155 AppWindowLauncherItemController* old_controller =
156 ControllerForWindow(old_active);
157 if (old_controller && old_controller != new_controller)
158 owner_->SetItemStatus(old_controller->shelf_id(), ash::STATUS_RUNNING);
161 void AppWindowLauncherController::RegisterApp(AppWindow* app_window) {
162 aura::Window* window = app_window->GetNativeWindow();
163 // Get the app's shelf identifier and add an entry to the map.
164 DCHECK(window_to_app_shelf_id_map_.find(window) ==
165 window_to_app_shelf_id_map_.end());
166 const std::string app_shelf_id = GetAppShelfId(app_window);
167 window_to_app_shelf_id_map_[window] = app_shelf_id;
168 window->AddObserver(this);
170 // Find or create an item controller and launcher item.
171 std::string app_id = app_window->extension()->id();
172 ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window)
174 : ash::STATUS_RUNNING;
175 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id);
176 ash::ShelfID shelf_id = 0;
177 if (iter != app_controller_map_.end()) {
178 AppWindowLauncherItemController* controller = iter->second;
179 DCHECK(controller->app_id() == app_id);
180 shelf_id = controller->shelf_id();
181 controller->AddAppWindow(app_window, status);
183 LauncherItemController::Type type =
184 app_window->window_type_is_panel()
185 ? LauncherItemController::TYPE_APP_PANEL
186 : LauncherItemController::TYPE_APP;
187 AppWindowLauncherItemController* controller =
188 new AppWindowLauncherItemController(type, app_shelf_id, app_id, owner_);
189 controller->AddAppWindow(app_window, status);
190 // If the app shelf id is not unique, and there is already a shelf
191 // item for this app id (e.g. pinned), use that shelf item.
192 if (app_shelf_id == app_id)
193 shelf_id = owner_->GetShelfIDForAppID(app_id);
195 shelf_id = owner_->CreateAppLauncherItem(controller, app_id, status);
196 // Restore any existing app icon and flag as set.
197 const gfx::Image& app_icon = app_window->app_icon();
198 if (!app_icon.IsEmpty()) {
199 owner_->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia());
200 controller->set_image_set_by_controller(true);
203 owner_->SetItemController(shelf_id, controller);
205 const std::string app_shelf_id = GetAppShelfId(app_window);
206 app_controller_map_[app_shelf_id] = controller;
208 owner_->SetItemStatus(shelf_id, status);
209 ash::SetShelfIDForWindow(shelf_id, window);
212 void AppWindowLauncherController::UnregisterApp(aura::Window* window) {
213 WindowToAppShelfIdMap::iterator iter1 =
214 window_to_app_shelf_id_map_.find(window);
215 DCHECK(iter1 != window_to_app_shelf_id_map_.end());
216 std::string app_shelf_id = iter1->second;
217 window_to_app_shelf_id_map_.erase(iter1);
218 window->RemoveObserver(this);
220 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id);
221 DCHECK(iter2 != app_controller_map_.end());
222 AppWindowLauncherItemController* controller = iter2->second;
223 controller->RemoveAppWindowForWindow(window);
224 if (controller->app_window_count() == 0) {
225 // If this is the last window associated with the app shelf id, close the
227 ash::ShelfID shelf_id = controller->shelf_id();
228 owner_->CloseLauncherItem(shelf_id);
229 app_controller_map_.erase(iter2);
233 bool AppWindowLauncherController::IsRegisteredApp(aura::Window* window) {
234 return window_to_app_shelf_id_map_.find(window) !=
235 window_to_app_shelf_id_map_.end();
240 AppWindowLauncherItemController*
241 AppWindowLauncherController::ControllerForWindow(aura::Window* window) {
242 WindowToAppShelfIdMap::iterator iter1 =
243 window_to_app_shelf_id_map_.find(window);
244 if (iter1 == window_to_app_shelf_id_map_.end())
246 std::string app_shelf_id = iter1->second;
247 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id);
248 if (iter2 == app_controller_map_.end())
250 return iter2->second;