summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/app_notification.cc
blob: 26893213745d4d197eb9ba4f43af38f49b0cb7d9 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2011 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 "chrome/browser/extensions/app_notification.h"

#include "base/memory/scoped_ptr.h"
#include "chrome/common/guid.h"

namespace {

const char* kIsLocalKey = "is_local";
const char* kGuidKey = "guid";
const char* kExtensionIdKey = "extension_id";
const char* kTitleKey = "title";
const char* kBodyKey = "body";
const char* kLinkUrlKey = "link_url";
const char* kLinkTextKey = "link_text";

}  // namespace

AppNotification::AppNotification(bool is_local,
                                 const std::string& guid,
                                 const std::string& extension_id,
                                 const std::string& title,
                                 const std::string& body)
    : is_local_(is_local),
      extension_id_(extension_id),
      title_(title),
      body_(body) {
  guid_ = guid.empty() ? guid::GenerateGUID() : guid;
}

AppNotification::~AppNotification() {}

AppNotification* AppNotification::Copy() {
  AppNotification* copy = new AppNotification(
      this->is_local(), this->guid(), this->extension_id(),
      this->title(), this->body());
  copy->set_link_url(this->link_url());
  copy->set_link_text(this->link_text());
  return copy;
}

void AppNotification::ToDictionaryValue(DictionaryValue* result) {
  CHECK(result);
  result->SetBoolean(kIsLocalKey, is_local_);
  if (!guid_.empty())
    result->SetString(kGuidKey, guid_);
  if (!extension_id_.empty())
    result->SetString(kExtensionIdKey, extension_id_);
  if (!title_.empty())
    result->SetString(kTitleKey, title_);
  if (!body_.empty())
    result->SetString(kBodyKey, body_);
  if (!link_url_.is_empty()) {
    result->SetString(kLinkUrlKey, link_url_.possibly_invalid_spec());
    result->SetString(kLinkTextKey, link_text_);
  }
}

// static
AppNotification* AppNotification::FromDictionaryValue(
    const DictionaryValue& value) {
  scoped_ptr<AppNotification> result(new AppNotification(true, "", "", "", ""));

  if (value.HasKey(kIsLocalKey) && !value.GetBoolean(
      kIsLocalKey, &result->is_local_)) {
    return NULL;
  }
  if (value.HasKey(kGuidKey) && !value.GetString(kGuidKey, &result->guid_))
    return NULL;
  if (value.HasKey(kExtensionIdKey) &&
      !value.GetString(kExtensionIdKey, &result->extension_id_))
    return NULL;
  if (value.HasKey(kTitleKey) && !value.GetString(kTitleKey, &result->title_))
    return NULL;
  if (value.HasKey(kBodyKey) && !value.GetString(kBodyKey, &result->body_))
    return NULL;
  if (value.HasKey(kLinkUrlKey)) {
    if (!value.HasKey(kLinkTextKey))
      return NULL;
    std::string url;
    if (!value.GetString(kLinkUrlKey, &url) ||
        !value.GetString(kLinkTextKey, &result->link_text_))
      return NULL;
    GURL gurl(url);
    if (!gurl.is_valid())
      return NULL;
    result->set_link_url(gurl);
  }

  return result.release();
}

bool AppNotification::Equals(const AppNotification& other) const {
  return (is_local_ == other.is_local_ &&
          guid_ == other.guid_ &&
          extension_id_ == other.extension_id_ &&
          title_ == other.title_ &&
          body_ == other.body_ &&
          link_url_ == other.link_url_ &&
          link_text_ == other.link_text_);
}

AppNotificationList* CopyAppNotificationList(
    const AppNotificationList& source) {
  AppNotificationList* copy = new AppNotificationList();

  for (AppNotificationList::const_iterator iter = source.begin();
      iter != source.end(); ++iter) {
    copy->push_back(linked_ptr<AppNotification>(iter->get()->Copy()));
  }
  return copy;
}