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

ash / system / notification_center / inactive_user_notification_blocker.cc [blame]

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/notification_center/inactive_user_notification_blocker.h"

#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "components/account_id/account_id.h"
#include "ui/message_center/message_center.h"

namespace ash {

InactiveUserNotificationBlocker::InactiveUserNotificationBlocker(
    message_center::MessageCenter* message_center)
    : NotificationBlocker(message_center), scoped_observer_(this) {}

InactiveUserNotificationBlocker::~InactiveUserNotificationBlocker() = default;

bool InactiveUserNotificationBlocker::ShouldShowNotification(
    const message_center::Notification& notification) const {
  // Allow notifications before login or in a single user session.
  if (Shell::Get()->session_controller()->NumberOfLoggedInUsers() < 2)
    return true;

  // All non-system notifications should be tied to a user profile.
  if (notification.notifier_id().profile_id.empty())
    return true;

  return AccountId::FromUserEmail(notification.notifier_id().profile_id) ==
         active_account_id_;
}

bool InactiveUserNotificationBlocker::ShouldShowNotificationAsPopup(
    const message_center::Notification& notification) const {
  return ShouldShowNotification(notification);
}

void InactiveUserNotificationBlocker::OnActiveUserSessionChanged(
    const AccountId& account_id) {
  if (active_account_id_ == account_id)
    return;

  quiet_modes_[active_account_id_] = message_center()->IsQuietMode();
  active_account_id_ = account_id;
  std::map<AccountId, bool>::const_iterator iter =
      quiet_modes_.find(active_account_id_);
  if (iter != quiet_modes_.end() &&
      iter->second != message_center()->IsQuietMode()) {
    message_center()->SetQuietMode(iter->second);
  }
  NotifyBlockingStateChanged();
}

}  // namespace ash