- add sources.
[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/shell_window.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/profiles/profile_manager.h"
10 #include "chrome/browser/ui/host_desktop.h"
11
12 #if defined(OS_CHROMEOS)
13 #include "chrome/browser/chromeos/login/user_manager.h"
14 #include "chrome/browser/ui/ash/multi_user_window_manager.h"
15 #endif
16
17 namespace {
18
19 bool ControlsWindow(aura::Window* window) {
20   return chrome::GetHostDesktopTypeForNativeWindow(window) ==
21       chrome::HOST_DESKTOP_TYPE_ASH;
22 }
23
24 }  // namespace
25
26
27 MultiProfileShellWindowLauncherController::
28     MultiProfileShellWindowLauncherController(
29     ChromeLauncherController* owner)
30     : ShellWindowLauncherController(owner) {
31 }
32
33 MultiProfileShellWindowLauncherController::
34     ~MultiProfileShellWindowLauncherController() {
35   // We need to remove all Registry observers for added users.
36   for (ShellWindowRegistryList::iterator it = multi_user_registry_.begin();
37        it != multi_user_registry_.end(); ++it)
38     (*it)->RemoveObserver(this);
39 }
40
41 void MultiProfileShellWindowLauncherController::ActiveUserChanged(
42     const std::string& user_email) {
43   // The active user has changed and we need to traverse our list of items to
44   // show / hide them one by one. To avoid that a user dependent state
45   // "survives" in a launcher item, we first delete all items making sure that
46   // nothing remains and then re-create them again.
47   for (ShellWindowList::iterator it = shell_window_list_.begin();
48        it != shell_window_list_.end(); ++it) {
49     aura::Window* window = (*it)->GetNativeWindow();
50     if (!IsShellWindowFromActiveUser(*it) && IsRegisteredApp(window))
51       UnregisterApp(window);
52   }
53   for (ShellWindowList::iterator it = shell_window_list_.begin();
54        it != shell_window_list_.end(); ++it) {
55     aura::Window* window = (*it)->GetNativeWindow();
56     if (IsShellWindowFromActiveUser(*it) && !IsRegisteredApp(window))
57       RegisterApp(*it);
58   }
59 }
60
61 void MultiProfileShellWindowLauncherController::AdditionalUserAddedToSession(
62     Profile* profile) {
63   // Each users ShellRegistry needs to be observed.
64   apps::ShellWindowRegistry* registry = apps::ShellWindowRegistry::Get(profile);
65   multi_user_registry_.push_back(registry);
66   registry->AddObserver(this);
67 }
68
69 void MultiProfileShellWindowLauncherController::OnShellWindowAdded(
70     apps::ShellWindow* shell_window) {
71   if (!ControlsWindow(shell_window->GetNativeWindow()))
72     return;
73   shell_window_list_.push_back(shell_window);
74   if (IsShellWindowFromActiveUser(shell_window))
75     RegisterApp(shell_window);
76 }
77
78 void MultiProfileShellWindowLauncherController::OnShellWindowRemoved(
79     apps::ShellWindow* shell_window) {
80   if (!ControlsWindow(shell_window->GetNativeWindow()))
81     return;
82
83   // If the application is registered with ShellWindowLauncher (because the user
84   // is currently active), the OnWindowDestroying observer has already (or will
85   // soon) unregister it independently from the shelf. If it was not registered
86   // we don't need to do anything anyways. As such, all which is left to do here
87   // is to get rid of our own reference.
88   ShellWindowList::iterator it = std::find(shell_window_list_.begin(),
89                                            shell_window_list_.end(),
90                                            shell_window);
91   DCHECK(it != shell_window_list_.end());
92   shell_window_list_.erase(it);
93 }
94
95 bool MultiProfileShellWindowLauncherController::IsShellWindowFromActiveUser(
96     apps::ShellWindow* shell_window) {
97   Profile* profile = shell_window->profile();
98 #if defined(OS_CHROMEOS)
99   return chrome::MultiUserWindowManager::ProfileIsFromActiveUser(profile);
100 #else
101   return profile->GetOriginalProfile() == ProfileManager::GetDefaultProfile();
102 #endif
103 }