Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / ash / multi_user / multi_user_notification_blocker_chromeos.cc
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/multi_user/multi_user_notification_blocker_chromeos.h"
6
7 #include "ash/shell.h"
8 #include "ash/shell_window_ids.h"
9 #include "ash/system/system_notifier.h"
10 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
11 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
12 #include "ui/aura/window.h"
13 #include "ui/message_center/message_center.h"
14 #include "ui/message_center/notifier_settings.h"
15
16 MultiUserNotificationBlockerChromeOS::MultiUserNotificationBlockerChromeOS(
17     message_center::MessageCenter* message_center,
18     chrome::MultiUserWindowManager* multi_user_window_manager,
19     const std::string& initial_user_id)
20     : NotificationBlocker(message_center),
21       multi_user_window_manager_(multi_user_window_manager),
22       active_user_id_(initial_user_id) {
23   UpdateWindowOwners();
24   multi_user_window_manager_->AddObserver(this);
25 }
26
27 MultiUserNotificationBlockerChromeOS::~MultiUserNotificationBlockerChromeOS() {
28   multi_user_window_manager_->RemoveObserver(this);
29 }
30
31 void MultiUserNotificationBlockerChromeOS::UpdateWindowOwners() {
32   std::set<std::string> new_ids;
33   multi_user_window_manager_->GetOwnersOfVisibleWindows(&new_ids);
34
35   if (current_user_ids_ != new_ids) {
36     current_user_ids_.swap(new_ids);
37     NotifyBlockingStateChanged();
38   }
39 }
40
41 bool MultiUserNotificationBlockerChromeOS::ShouldShowNotification(
42     const message_center::NotifierId& notifier_id) const {
43   if (!IsActive())
44     return true;
45
46   if (ash::system_notifier::IsAshSystemNotifier(notifier_id))
47     return true;
48
49   return notifier_id.profile_id == active_user_id_;
50 }
51
52 bool MultiUserNotificationBlockerChromeOS::ShouldShowNotificationAsPopup(
53     const message_center::NotifierId& notifier_id) const {
54   return (current_user_ids_.find(notifier_id.profile_id) !=
55           current_user_ids_.end()) ||
56       ShouldShowNotification(notifier_id);
57 }
58
59 void MultiUserNotificationBlockerChromeOS::OnOwnerEntryAdded(
60     aura::Window* window) {
61   UpdateWindowOwners();
62 }
63
64 void MultiUserNotificationBlockerChromeOS::OnOwnerEntryChanged(
65     aura::Window* window) {
66   UpdateWindowOwners();
67 }
68
69 void MultiUserNotificationBlockerChromeOS::OnOwnerEntryRemoved(
70     aura::Window* window) {
71   UpdateWindowOwners();
72 }
73
74 void MultiUserNotificationBlockerChromeOS::ActiveUserChanged(
75     const std::string& user_id) {
76   if (active_user_id_ == user_id)
77     return;
78
79   quiet_modes_[active_user_id_] = message_center()->IsQuietMode();
80   active_user_id_ = user_id;
81   std::map<std::string, bool>::const_iterator iter =
82       quiet_modes_.find(active_user_id_);
83   if (iter != quiet_modes_.end() &&
84       iter->second != message_center()->IsQuietMode()) {
85     message_center()->SetQuietMode(iter->second);
86   }
87   UpdateWindowOwners();
88   NotifyBlockingStateChanged();
89 }
90
91 bool MultiUserNotificationBlockerChromeOS::IsActive() const {
92   return chrome::MultiUserWindowManager::GetMultiProfileMode() ==
93       chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED;
94 }