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/notifications/login_state_notification_blocker_chromeos.h"
6
7#include "ash/root_window_controller.h"
8#include "ash/shell.h"
9#include "ash/system/system_notifier.h"
10#include "ash/wm/window_properties.h"
11#include "chrome/browser/chrome_notification_types.h"
12#include "content/public/browser/notification_service.h"
13#include "ui/aura/window.h"
14#include "ui/aura/window_event_dispatcher.h"
15#include "ui/message_center/message_center.h"
16
17LoginStateNotificationBlockerChromeOS::LoginStateNotificationBlockerChromeOS(
18    message_center::MessageCenter* message_center)
19    : NotificationBlocker(message_center),
20      locked_(false),
21      observing_(true) {
22  // This class is created in the ctor of NotificationUIManager which is created
23  // when a notification is created, so ash::Shell should be initialized.
24  ash::Shell::GetInstance()->AddShellObserver(this);
25
26  // LoginState may not exist in some tests.
27  if (chromeos::LoginState::IsInitialized())
28    chromeos::LoginState::Get()->AddObserver(this);
29  chromeos::UserAddingScreen::Get()->AddObserver(this);
30}
31
32LoginStateNotificationBlockerChromeOS::
33    ~LoginStateNotificationBlockerChromeOS() {
34  // In some tests, the notification blockers may be removed without calling
35  // OnAppTerminating().
36  if (chromeos::LoginState::IsInitialized())
37    chromeos::LoginState::Get()->RemoveObserver(this);
38  if (observing_) {
39    if (ash::Shell::HasInstance())
40      ash::Shell::GetInstance()->RemoveShellObserver(this);
41    chromeos::UserAddingScreen::Get()->RemoveObserver(this);
42  }
43}
44
45bool LoginStateNotificationBlockerChromeOS::ShouldShowNotificationAsPopup(
46    const message_center::NotifierId& notifier_id) const {
47  if (ash::system_notifier::ShouldAlwaysShowPopups(notifier_id))
48    return true;
49
50  if (locked_)
51    return false;
52
53  if (chromeos::UserAddingScreen::Get()->IsRunning())
54    return false;
55
56  if (chromeos::LoginState::IsInitialized())
57    return chromeos::LoginState::Get()->IsUserLoggedIn();
58
59  return true;
60}
61
62void LoginStateNotificationBlockerChromeOS::OnLockStateChanged(bool locked) {
63  locked_ = locked;
64  NotifyBlockingStateChanged();
65}
66
67void LoginStateNotificationBlockerChromeOS::OnAppTerminating() {
68  ash::Shell::GetInstance()->RemoveShellObserver(this);
69  chromeos::UserAddingScreen::Get()->RemoveObserver(this);
70  observing_ = false;
71}
72
73void LoginStateNotificationBlockerChromeOS::LoggedInStateChanged() {
74  NotifyBlockingStateChanged();
75}
76
77void LoginStateNotificationBlockerChromeOS::OnUserAddingStarted() {
78  NotifyBlockingStateChanged();
79}
80
81void LoginStateNotificationBlockerChromeOS::OnUserAddingFinished() {
82  NotifyBlockingStateChanged();
83}
84