Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / ash / launcher / multi_profile_shell_window_launcher_controller.cc
1 // Copyright 2013 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/multi_profile_shell_window_launcher_controller.h"
6
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/host_desktop.h"
12
13 namespace {
14
15 bool ControlsWindow(aura::Window* window) {
16   return chrome::GetHostDesktopTypeForNativeWindow(window) ==
17       chrome::HOST_DESKTOP_TYPE_ASH;
18 }
19
20 }  // namespace
21
22
23 MultiProfileShellWindowLauncherController::
24     MultiProfileShellWindowLauncherController(
25     ChromeLauncherController* owner)
26     : ShellWindowLauncherController(owner) {
27 }
28
29 MultiProfileShellWindowLauncherController::
30     ~MultiProfileShellWindowLauncherController() {
31   // We need to remove all Registry observers for added users.
32   for (AppWindowRegistryList::iterator it = multi_user_registry_.begin();
33        it != multi_user_registry_.end();
34        ++it)
35     (*it)->RemoveObserver(this);
36 }
37
38 void MultiProfileShellWindowLauncherController::ActiveUserChanged(
39     const std::string& user_email) {
40   // The active user has changed and we need to traverse our list of items to
41   // show / hide them one by one. To avoid that a user dependent state
42   // "survives" in a launcher item, we first delete all items making sure that
43   // nothing remains and then re-create them again.
44   for (AppWindowList::iterator it = app_window_list_.begin();
45        it != app_window_list_.end();
46        ++it) {
47     apps::AppWindow* app_window = *it;
48     Profile* profile =
49         Profile::FromBrowserContext(app_window->browser_context());
50     if (!multi_user_util::IsProfileFromActiveUser(profile) &&
51         IsRegisteredApp(app_window->GetNativeWindow()))
52       UnregisterApp(app_window->GetNativeWindow());
53   }
54   for (AppWindowList::iterator it = app_window_list_.begin();
55        it != app_window_list_.end();
56        ++it) {
57     apps::AppWindow* app_window = *it;
58     Profile* profile =
59         Profile::FromBrowserContext(app_window->browser_context());
60     if (multi_user_util::IsProfileFromActiveUser(profile) &&
61         !IsRegisteredApp(app_window->GetNativeWindow()))
62       RegisterApp(*it);
63   }
64 }
65
66 void MultiProfileShellWindowLauncherController::AdditionalUserAddedToSession(
67     Profile* profile) {
68   // Each users AppWindowRegistry needs to be observed.
69   apps::AppWindowRegistry* registry = apps::AppWindowRegistry::Get(profile);
70   multi_user_registry_.push_back(registry);
71   registry->AddObserver(this);
72 }
73
74 void MultiProfileShellWindowLauncherController::OnAppWindowAdded(
75     apps::AppWindow* app_window) {
76   if (!ControlsWindow(app_window->GetNativeWindow()))
77     return;
78   app_window_list_.push_back(app_window);
79   Profile* profile = Profile::FromBrowserContext(app_window->browser_context());
80   if (multi_user_util::IsProfileFromActiveUser(profile))
81     RegisterApp(app_window);
82 }
83
84 void MultiProfileShellWindowLauncherController::OnAppWindowRemoved(
85     apps::AppWindow* app_window) {
86   if (!ControlsWindow(app_window->GetNativeWindow()))
87     return;
88
89   // If the application is registered with ShellWindowLauncher (because the user
90   // is currently active), the OnWindowDestroying observer has already (or will
91   // soon) unregister it independently from the shelf. If it was not registered
92   // we don't need to do anything anyways. As such, all which is left to do here
93   // is to get rid of our own reference.
94   AppWindowList::iterator it =
95       std::find(app_window_list_.begin(), app_window_list_.end(), app_window);
96   DCHECK(it != app_window_list_.end());
97   app_window_list_.erase(it);
98 }