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/multi_profile_app_window_launcher_controller.h"
7 #include "apps/app_window.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/profiles/profile_manager.h"
10 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
11 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
12 #include "chrome/browser/ui/host_desktop.h"
13 #include "ui/aura/window.h"
17 bool ControlsWindow(aura::Window
* window
) {
18 return chrome::GetHostDesktopTypeForNativeWindow(window
) ==
19 chrome::HOST_DESKTOP_TYPE_ASH
;
24 MultiProfileAppWindowLauncherController::
25 MultiProfileAppWindowLauncherController(ChromeLauncherController
* owner
)
26 : AppWindowLauncherController(owner
) {}
28 MultiProfileAppWindowLauncherController::
29 ~MultiProfileAppWindowLauncherController() {
30 // We need to remove all Registry observers for added users.
31 for (AppWindowRegistryList::iterator it
= multi_user_registry_
.begin();
32 it
!= multi_user_registry_
.end();
34 (*it
)->RemoveObserver(this);
37 void MultiProfileAppWindowLauncherController::ActiveUserChanged(
38 const std::string
& user_email
) {
39 // The active user has changed and we need to traverse our list of items to
40 // show / hide them one by one. To avoid that a user dependent state
41 // "survives" in a launcher item, we first delete all items making sure that
42 // nothing remains and then re-create them again.
43 for (AppWindowList::iterator it
= app_window_list_
.begin();
44 it
!= app_window_list_
.end();
46 apps::AppWindow
* app_window
= *it
;
48 Profile::FromBrowserContext(app_window
->browser_context());
49 if (!multi_user_util::IsProfileFromActiveUser(profile
) &&
50 IsRegisteredApp(app_window
->GetNativeWindow()))
51 UnregisterApp(app_window
->GetNativeWindow());
53 for (AppWindowList::iterator it
= app_window_list_
.begin();
54 it
!= app_window_list_
.end();
56 apps::AppWindow
* app_window
= *it
;
58 Profile::FromBrowserContext(app_window
->browser_context());
59 if (multi_user_util::IsProfileFromActiveUser(profile
) &&
60 !IsRegisteredApp(app_window
->GetNativeWindow()))
65 void MultiProfileAppWindowLauncherController::AdditionalUserAddedToSession(
67 // Each users AppWindowRegistry needs to be observed.
68 apps::AppWindowRegistry
* registry
= apps::AppWindowRegistry::Get(profile
);
69 multi_user_registry_
.push_back(registry
);
70 registry
->AddObserver(this);
73 void MultiProfileAppWindowLauncherController::OnAppWindowAdded(
74 apps::AppWindow
* app_window
) {
75 if (!ControlsWindow(app_window
->GetNativeWindow()))
77 app_window_list_
.push_back(app_window
);
78 Profile
* profile
= Profile::FromBrowserContext(app_window
->browser_context());
79 if (multi_user_util::IsProfileFromActiveUser(profile
)) {
80 RegisterApp(app_window
);
82 // If the window got created for a non active user but the user allowed to
83 // teleport to the current user's desktop, we teleport it now.
84 if (UserHasAppOnActiveDesktop(app_window
)) {
85 chrome::MultiUserWindowManager::GetInstance()->ShowWindowForUser(
86 app_window
->GetNativeWindow(),
87 multi_user_util::GetCurrentUserId());
88 if (app_window
->GetNativeWindow()->type() == ui::wm::WINDOW_TYPE_PANEL
&&
89 !app_window
->GetNativeWindow()->layer()->GetTargetOpacity()) {
90 // The panel layout manager only manages windows which are anchored.
91 // Since this window did never had an anchor, it would stay hidden. We
92 // therefore make it visible now.
93 app_window
->GetNativeWindow()->layer()->SetOpacity(1.0f
);
99 void MultiProfileAppWindowLauncherController::OnAppWindowRemoved(
100 apps::AppWindow
* app_window
) {
101 if (!ControlsWindow(app_window
->GetNativeWindow()))
104 // If the application is registered with AppWindowLauncher (because the user
105 // is currently active), the OnWindowDestroying observer has already (or will
106 // soon) unregister it independently from the shelf. If it was not registered
107 // we don't need to do anything anyways. As such, all which is left to do here
108 // is to get rid of our own reference.
109 AppWindowList::iterator it
=
110 std::find(app_window_list_
.begin(), app_window_list_
.end(), app_window
);
111 DCHECK(it
!= app_window_list_
.end());
112 app_window_list_
.erase(it
);
115 #if defined(OS_CHROMEOS)
116 void MultiProfileAppWindowLauncherController::OnAppWindowShown(
117 apps::AppWindow
* app_window
) {
120 void MultiProfileAppWindowLauncherController::OnAppWindowHidden(
121 apps::AppWindow
* app_window
) {
125 bool MultiProfileAppWindowLauncherController::UserHasAppOnActiveDesktop(
126 apps::AppWindow
* app_window
) {
127 const std::string
& app_id
= app_window
->extension_id();
128 content::BrowserContext
* app_context
= app_window
->browser_context();
129 DCHECK(!app_context
->IsOffTheRecord());
130 const std::string
& current_user
= multi_user_util::GetCurrentUserId();
131 chrome::MultiUserWindowManager
* manager
=
132 chrome::MultiUserWindowManager::GetInstance();
133 for (AppWindowList::iterator it
= app_window_list_
.begin();
134 it
!= app_window_list_
.end();
136 apps::AppWindow
* other_window
= *it
;
137 DCHECK(!other_window
->browser_context()->IsOffTheRecord());
138 if (manager
->IsWindowOnDesktopOfUser(other_window
->GetNativeWindow(),
140 app_id
== other_window
->extension_id() &&
141 app_context
== other_window
->browser_context()) {