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