summaryrefslogtreecommitdiffstats
path: root/ui/message_center/notification.cc
blob: 5a92c094c3855288378ef386eab2f702f1e77606 (plain)
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
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/message_center/notification.h"

#include "base/logging.h"
#include "ui/message_center/notification_types.h"

namespace {
unsigned g_next_serial_number_ = 0;
}

namespace message_center {

NotificationItem::NotificationItem(const string16& title,
                                   const string16& message)
 : title(title),
   message(message) {
}

ButtonInfo::ButtonInfo(const string16& title)
 : title(title) {
}

Notification::Notification(NotificationType type,
                           const std::string& id,
                           const string16& title,
                           const string16& message,
                           const string16& display_source,
                           const std::string& extension_id,
                           const DictionaryValue* optional_fields)
 : type_(type),
   id_(id),
   title_(title),
   message_(message),
   display_source_(display_source),
   extension_id_(extension_id),
   priority_(DEFAULT_PRIORITY),
   timestamp_(base::Time::Now()),
   serial_number_(g_next_serial_number_++),
   is_read_(false),
   shown_as_popup_(false) {
  // This can override some data members initialized to deafule values above.
  ApplyOptionalFields(optional_fields);
}

Notification::~Notification() {
}

bool Notification::SetButtonIcon(size_t index, const gfx::ImageSkia& icon) {
  if (index >= buttons_.size())
    return false;
  buttons_[index].icon = icon;
  return true;
}

void Notification::ApplyOptionalFields(const DictionaryValue* fields) {
  if (!fields)
    return;

  fields->GetInteger(kPriorityKey, &priority_);
  if (fields->HasKey(kTimestampKey)) {
    std::string time_string;
    fields->GetString(kTimestampKey, &time_string);
    base::Time::FromString(time_string.c_str(), &timestamp_);
  }
  if (fields->HasKey(kButtonOneTitleKey) ||
      fields->HasKey(kButtonOneIconUrlKey)) {
    string16 title;
    string16 icon;
    if (fields->GetString(kButtonOneTitleKey, &title) ||
        fields->GetString(kButtonOneIconUrlKey, &icon)) {
      buttons_.push_back(ButtonInfo(title));
      if (fields->GetString(kButtonTwoTitleKey, &title) ||
          fields->GetString(kButtonTwoIconUrlKey, &icon)) {
        buttons_.push_back(ButtonInfo(title));
      }
    }
  }
  fields->GetString(kExpandedMessageKey, &expanded_message_);
  if (fields->HasKey(kItemsKey)) {
    const ListValue* items;
    CHECK(fields->GetList(kItemsKey, &items));
    for (size_t i = 0; i < items->GetSize(); ++i) {
      string16 title;
      string16 message;
      const base::DictionaryValue* item;
      items->GetDictionary(i, &item);
      item->GetString(kItemTitleKey, &title);
      item->GetString(kItemMessageKey, &message);
      items_.push_back(NotificationItem(title, message));
    }
  }
}

}  // namespace message_center