- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / notifications / login_state_notification_blocker_chromeos.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/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/root_window.h"
14 #include "ui/aura/window.h"
15 #include "ui/message_center/message_center.h"
16
17 LoginStateNotificationBlockerChromeOS::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 }
30
31 LoginStateNotificationBlockerChromeOS::
32     ~LoginStateNotificationBlockerChromeOS() {
33   // In some tests, the notification blockers may be removed without calling
34   // OnAppTerminating().
35   if (chromeos::LoginState::IsInitialized())
36     chromeos::LoginState::Get()->RemoveObserver(this);
37   if (observing_ && ash::Shell::HasInstance())
38     ash::Shell::GetInstance()->RemoveShellObserver(this);
39 }
40
41 bool LoginStateNotificationBlockerChromeOS::ShouldShowNotificationAsPopup(
42     const message_center::NotifierId& notifier_id) const {
43   if (ash::system_notifier::ShouldAlwaysShowPopups(notifier_id))
44     return true;
45
46   if (locked_)
47     return false;
48
49   if (chromeos::LoginState::IsInitialized())
50     return chromeos::LoginState::Get()->IsUserLoggedIn();
51
52   return true;
53 }
54
55 void LoginStateNotificationBlockerChromeOS::OnLockStateChanged(bool locked) {
56   locked_ = locked;
57   FOR_EACH_OBSERVER(
58       NotificationBlocker::Observer, observers(), OnBlockingStateChanged());
59 }
60
61 void LoginStateNotificationBlockerChromeOS::OnAppTerminating() {
62   ash::Shell::GetInstance()->RemoveShellObserver(this);
63   observing_ = false;
64 }
65
66 void LoginStateNotificationBlockerChromeOS::LoggedInStateChanged() {
67   FOR_EACH_OBSERVER(
68       NotificationBlocker::Observer, observers(), OnBlockingStateChanged());
69 }