summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/ash/multi_user/multi_user_window_manager.cc
blob: 93b2af16b39cd99c48be4905b65e3b52a848ee66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"

#include "base/logging.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_stub.h"

#if defined(OS_CHROMEOS)
#include "ash/ash_switches.h"
#include "ash/multi_profile_uma.h"
#include "ash/session/session_state_delegate.h"
#include "ash/session/user_info.h"
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_chromeos.h"
#endif

namespace {
chrome::MultiUserWindowManager* g_instance = NULL;
}  // namespace

namespace chrome {

// Caching the current multi profile mode to avoid expensive detection
// operations.
chrome::MultiUserWindowManager::MultiProfileMode
    chrome::MultiUserWindowManager::multi_user_mode_ =
        chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_UNINITIALIZED;

// static
MultiUserWindowManager* MultiUserWindowManager::GetInstance() {
  return g_instance;
}

MultiUserWindowManager* MultiUserWindowManager::CreateInstance() {
  DCHECK(!g_instance);
  multi_user_mode_ = MULTI_PROFILE_MODE_OFF;
#if defined(OS_CHROMEOS)
  ash::MultiProfileUMA::SessionMode mode =
      ash::MultiProfileUMA::SESSION_SINGLE_USER_MODE;
  if (!g_instance &&
      ash::Shell::GetInstance()->delegate()->IsMultiProfilesEnabled()) {
    g_instance =
        new MultiUserWindowManagerChromeOS(ash::Shell::GetInstance()
                                               ->session_state_delegate()
                                               ->GetUserInfo(0)
                                               ->GetUserID());
    multi_user_mode_ = MULTI_PROFILE_MODE_SEPARATED;
    mode = ash::MultiProfileUMA::SESSION_SEPARATE_DESKTOP_MODE;
  } else if (ash::Shell::GetInstance()->delegate()->IsMultiProfilesEnabled()) {
    // The side by side mode is using the Single user window manager since all
    // windows are unmanaged side by side.
    multi_user_mode_ = MULTI_PROFILE_MODE_MIXED;
    mode = ash::MultiProfileUMA::SESSION_SIDE_BY_SIDE_MODE;
  }
  ash::MultiProfileUMA::RecordSessionMode(mode);
#endif
  // If there was no instance created yet we create a dummy instance.
  if (!g_instance)
    g_instance = new MultiUserWindowManagerStub();

  return g_instance;
}

// static
MultiUserWindowManager::MultiProfileMode
MultiUserWindowManager::GetMultiProfileMode() {
  return multi_user_mode_;
}

// satic
bool MultiUserWindowManager::ShouldShowAvatar(aura::Window* window) {
  // Note: In case of the M-31 mode the window manager won't exist.
  if (GetMultiProfileMode() == MULTI_PROFILE_MODE_SEPARATED) {
    // If the window is shown on a different desktop than the user, it should
    // have the avatar icon
    MultiUserWindowManager* instance = GetInstance();
    return !instance->IsWindowOnDesktopOfUser(window,
                                              instance->GetWindowOwner(window));
  }
  return false;
}

// static
void MultiUserWindowManager::DeleteInstance() {
  DCHECK(g_instance);
  delete g_instance;
  g_instance = NULL;
  multi_user_mode_ = MULTI_PROFILE_MODE_UNINITIALIZED;
}

void MultiUserWindowManager::SetInstanceForTest(
    MultiUserWindowManager* instance,
    MultiProfileMode mode) {
  if (g_instance)
    DeleteInstance();
  g_instance = instance;
  multi_user_mode_ = mode;
}

}  // namespace chrome