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 "ash/shelf/shelf_util.h"
9 #include "ash/wm/window_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/ash/launcher/app_window_launcher_item_controller.h"
13 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
14 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
15 #include "chrome/browser/ui/host_desktop.h"
16 #include "extensions/browser/app_window/app_window.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 extensions::AppWindow
;
22 using extensions::AppWindowRegistry
;
26 std::string
GetAppShelfId(AppWindow
* app_window
) {
27 if (app_window
->window_type_is_panel())
28 return base::StringPrintf("panel:%d", app_window
->session_id().id());
29 return app_window
->extension_id();
32 bool ControlsWindow(aura::Window
* window
) {
33 return chrome::GetHostDesktopTypeForNativeWindow(window
) ==
34 chrome::HOST_DESKTOP_TYPE_ASH
;
39 AppWindowLauncherController::AppWindowLauncherController(
40 ChromeLauncherController
* owner
)
41 : owner_(owner
), activation_client_(NULL
) {
42 AppWindowRegistry
* registry
= 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
<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 AppWindowRegistry
* registry
= AppWindowRegistry::Get(profile
);
80 if (registry_
.find(registry
) != registry_
.end())
83 registry
->AddObserver(this);
84 registry_
.insert(registry
);
87 void AppWindowLauncherController::OnAppWindowIconChanged(
88 AppWindow
* app_window
) {
89 if (!ControlsWindow(app_window
->GetNativeWindow()))
92 const std::string app_shelf_id
= GetAppShelfId(app_window
);
93 AppControllerMap::iterator iter
= app_controller_map_
.find(app_shelf_id
);
94 if (iter
== app_controller_map_
.end())
96 AppWindowLauncherItemController
* controller
= iter
->second
;
97 controller
->set_image_set_by_controller(true);
98 owner_
->SetLauncherItemImage(controller
->shelf_id(),
99 app_window
->app_icon().AsImageSkia());
102 void AppWindowLauncherController::OnAppWindowShown(AppWindow
* app_window
,
104 aura::Window
* window
= app_window
->GetNativeWindow();
105 if (!ControlsWindow(window
))
108 if (!IsRegisteredApp(window
))
109 RegisterApp(app_window
);
112 void AppWindowLauncherController::OnAppWindowHidden(AppWindow
* app_window
) {
113 aura::Window
* window
= app_window
->GetNativeWindow();
114 if (!ControlsWindow(window
))
117 if (IsRegisteredApp(window
))
118 UnregisterApp(window
);
121 // Called from aura::Window::~Window(), before delegate_->OnWindowDestroyed()
122 // which destroys AppWindow, so both |window| and the associated AppWindow
124 void AppWindowLauncherController::OnWindowDestroying(aura::Window
* window
) {
125 if (!ControlsWindow(window
))
127 UnregisterApp(window
);
130 void AppWindowLauncherController::OnWindowActivated(aura::Window
* new_active
,
131 aura::Window
* old_active
) {
132 // Make the newly active window the active (first) entry in the controller.
133 AppWindowLauncherItemController
* new_controller
=
134 ControllerForWindow(new_active
);
135 if (new_controller
) {
136 new_controller
->SetActiveWindow(new_active
);
137 owner_
->SetItemStatus(new_controller
->shelf_id(), ash::STATUS_ACTIVE
);
140 // Mark the old active window's launcher item as running (if different).
141 AppWindowLauncherItemController
* old_controller
=
142 ControllerForWindow(old_active
);
143 if (old_controller
&& old_controller
!= new_controller
)
144 owner_
->SetItemStatus(old_controller
->shelf_id(), ash::STATUS_RUNNING
);
147 void AppWindowLauncherController::RegisterApp(AppWindow
* app_window
) {
148 // Windows created by IME extension should be treated the same way as the
149 // virtual keyboard window, which does not register itself in launcher.
150 if (app_window
->is_ime_window())
153 aura::Window
* window
= app_window
->GetNativeWindow();
154 // Get the app's shelf identifier and add an entry to the map.
155 DCHECK(window_to_app_shelf_id_map_
.find(window
) ==
156 window_to_app_shelf_id_map_
.end());
157 const std::string app_shelf_id
= GetAppShelfId(app_window
);
158 window_to_app_shelf_id_map_
[window
] = app_shelf_id
;
159 window
->AddObserver(this);
161 // Find or create an item controller and launcher item.
162 std::string app_id
= app_window
->extension_id();
163 ash::ShelfItemStatus status
= ash::wm::IsActiveWindow(window
)
165 : ash::STATUS_RUNNING
;
166 AppControllerMap::iterator iter
= app_controller_map_
.find(app_shelf_id
);
167 ash::ShelfID shelf_id
= 0;
168 if (iter
!= app_controller_map_
.end()) {
169 AppWindowLauncherItemController
* controller
= iter
->second
;
170 DCHECK(controller
->app_id() == app_id
);
171 shelf_id
= controller
->shelf_id();
172 controller
->AddAppWindow(app_window
, status
);
174 LauncherItemController::Type type
=
175 app_window
->window_type_is_panel()
176 ? LauncherItemController::TYPE_APP_PANEL
177 : LauncherItemController::TYPE_APP
;
178 AppWindowLauncherItemController
* controller
=
179 new AppWindowLauncherItemController(type
, app_shelf_id
, app_id
, owner_
);
180 controller
->AddAppWindow(app_window
, status
);
181 // If the app shelf id is not unique, and there is already a shelf
182 // item for this app id (e.g. pinned), use that shelf item.
183 if (app_shelf_id
== app_id
)
184 shelf_id
= owner_
->GetShelfIDForAppID(app_id
);
186 shelf_id
= owner_
->CreateAppLauncherItem(controller
, app_id
, status
);
187 // Restore any existing app icon and flag as set.
188 const gfx::Image
& app_icon
= app_window
->app_icon();
189 if (!app_icon
.IsEmpty()) {
190 owner_
->SetLauncherItemImage(shelf_id
, app_icon
.AsImageSkia());
191 controller
->set_image_set_by_controller(true);
194 owner_
->SetItemController(shelf_id
, controller
);
196 const std::string app_shelf_id
= GetAppShelfId(app_window
);
197 app_controller_map_
[app_shelf_id
] = controller
;
199 owner_
->SetItemStatus(shelf_id
, status
);
200 ash::SetShelfIDForWindow(shelf_id
, window
);
203 void AppWindowLauncherController::UnregisterApp(aura::Window
* window
) {
204 WindowToAppShelfIdMap::iterator iter1
=
205 window_to_app_shelf_id_map_
.find(window
);
206 DCHECK(iter1
!= window_to_app_shelf_id_map_
.end());
207 std::string app_shelf_id
= iter1
->second
;
208 window_to_app_shelf_id_map_
.erase(iter1
);
209 window
->RemoveObserver(this);
211 AppControllerMap::iterator iter2
= app_controller_map_
.find(app_shelf_id
);
212 DCHECK(iter2
!= app_controller_map_
.end());
213 AppWindowLauncherItemController
* controller
= iter2
->second
;
214 controller
->RemoveAppWindowForWindow(window
);
215 if (controller
->app_window_count() == 0) {
216 // If this is the last window associated with the app shelf id, close the
218 ash::ShelfID shelf_id
= controller
->shelf_id();
219 owner_
->CloseLauncherItem(shelf_id
);
220 app_controller_map_
.erase(iter2
);
224 bool AppWindowLauncherController::IsRegisteredApp(aura::Window
* window
) {
225 return window_to_app_shelf_id_map_
.find(window
) !=
226 window_to_app_shelf_id_map_
.end();
231 AppWindowLauncherItemController
*
232 AppWindowLauncherController::ControllerForWindow(aura::Window
* window
) {
233 WindowToAppShelfIdMap::iterator iter1
=
234 window_to_app_shelf_id_map_
.find(window
);
235 if (iter1
== window_to_app_shelf_id_map_
.end())
237 std::string app_shelf_id
= iter1
->second
;
238 AppControllerMap::iterator iter2
= app_controller_map_
.find(app_shelf_id
);
239 if (iter2
== app_controller_map_
.end())
241 return iter2
->second
;