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
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159

ash / public / cpp / system_notification_builder.cc [blame]

// Copyright 2022 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/public/cpp/system_notification_builder.h"

#include <memory>
#include <string>
#include <utility>

#include "ash/public/cpp/notification_utils.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_delegate.h"

namespace ash {

SystemNotificationBuilder::SystemNotificationBuilder() = default;
SystemNotificationBuilder::SystemNotificationBuilder(
    SystemNotificationBuilder&&) = default;
SystemNotificationBuilder& SystemNotificationBuilder::operator=(
    SystemNotificationBuilder&&) = default;
SystemNotificationBuilder::~SystemNotificationBuilder() = default;

SystemNotificationBuilder& SystemNotificationBuilder::SetType(
    message_center::NotificationType type) {
  type_ = type;
  return *this;
}

SystemNotificationBuilder& SystemNotificationBuilder::SetId(
    const std::string& id) {
  id_ = id;
  return *this;
}

SystemNotificationBuilder& SystemNotificationBuilder::SetTitle(
    const std::u16string& title) {
  title_ = title;
  return *this;
}

SystemNotificationBuilder& SystemNotificationBuilder::SetTitleId(
    const int title_id) {
  SetTitle(l10n_util::GetStringUTF16(title_id));
  return *this;
}

SystemNotificationBuilder& SystemNotificationBuilder::SetTitleWithArgs(
    const int title_id,
    const std::vector<std::u16string>& args) {
  SetTitle(l10n_util::GetStringFUTF16(title_id, args, /*offsets=*/nullptr));
  return *this;
}

SystemNotificationBuilder& SystemNotificationBuilder::SetMessage(
    const std::u16string& message) {
  message_ = message;
  return *this;
}

SystemNotificationBuilder& SystemNotificationBuilder::SetMessageId(
    const int message_id) {
  SetMessage(l10n_util::GetStringUTF16(message_id));
  return *this;
}

SystemNotificationBuilder& SystemNotificationBuilder::SetMessageWithArgs(
    const int message_id,
    const std::vector<std::u16string>& args) {
  SetMessage(l10n_util::GetStringFUTF16(message_id, args, /*offsets=*/nullptr));
  return *this;
}

SystemNotificationBuilder& SystemNotificationBuilder::SetDisplaySource(
    const std::u16string& display_source) {
  display_source_ = display_source;
  return *this;
}

SystemNotificationBuilder& SystemNotificationBuilder::SetOriginUrl(
    const GURL& origin_url) {
  origin_url_ = origin_url;
  return *this;
}

SystemNotificationBuilder& SystemNotificationBuilder::SetNotifierId(
    const message_center::NotifierId& notifier_id) {
  notifier_id_ = notifier_id;
  return *this;
}

SystemNotificationBuilder& SystemNotificationBuilder::SetCatalogName(
    const NotificationCatalogName catalog_name) {
  catalog_name_ = catalog_name;
  return *this;
}

SystemNotificationBuilder& SystemNotificationBuilder::SetDelegate(
    scoped_refptr<message_center::NotificationDelegate> delegate) {
  delegate_ = std::move(delegate);
  return *this;
}

SystemNotificationBuilder& SystemNotificationBuilder::SetSmallImage(
    const gfx::VectorIcon& small_image) {
  small_image_ = &small_image;
  return *this;
}

SystemNotificationBuilder& SystemNotificationBuilder::SetOptionalFields(
    const message_center::RichNotificationData& optional_fields) {
  optional_fields_ = optional_fields;
  return *this;
}

const message_center::RichNotificationData&
SystemNotificationBuilder::GetOptionalFields() {
  return optional_fields_;
}

SystemNotificationBuilder& SystemNotificationBuilder::SetWarningLevel(
    message_center::SystemNotificationWarningLevel warning_level) {
  warning_level_ = warning_level;
  return *this;
}

message_center::Notification SystemNotificationBuilder::Build(
    bool keep_timestamp) {
  DCHECK(!id_.empty());
  const message_center::NotifierId notifier_id = GetNotifierId();
  DCHECK(notifier_id.type == message_center::NotifierType::SYSTEM_COMPONENT);
  if (!keep_timestamp) {
    optional_fields_.timestamp = base::Time::Now();
  }
  return CreateSystemNotification(type_, id_, title_, message_, display_source_,
                                  origin_url_, notifier_id, optional_fields_,
                                  delegate_, *small_image_, warning_level_);
}

std::unique_ptr<message_center::Notification>
SystemNotificationBuilder::BuildPtr(bool keep_timestamp) {
  return std::make_unique<message_center::Notification>(Build(keep_timestamp));
}

message_center::NotifierId SystemNotificationBuilder::GetNotifierId() const {
  // value_or doesn't work here as it eagerly constructs the potential
  // replacement value and the default `catalog_name_` is set to invalid
  // resulting in a failed DCHECK. This could be solved with the monadic
  // operations for optional but they won't be added until C++23.
  if (notifier_id_.has_value())
    return notifier_id_.value();

  return message_center::NotifierId(
      message_center::NotifierType::SYSTEM_COMPONENT, id_, catalog_name_);
}

}  // namespace ash