| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/extensions/app_notification.h" | |
| 6 | |
| 7 #include "base/guid.h" | |
| 8 #include "base/json/json_writer.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 | |
| 12 namespace extensions { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 const char* kIsLocalKey = "is_local"; | |
| 17 const char* kCreationTime= "creation_time"; | |
| 18 const char* kGuidKey = "guid"; | |
| 19 const char* kExtensionIdKey = "extension_id"; | |
| 20 const char* kTitleKey = "title"; | |
| 21 const char* kBodyKey = "body"; | |
| 22 const char* kLinkUrlKey = "link_url"; | |
| 23 const char* kLinkTextKey = "link_text"; | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 AppNotification::AppNotification(bool is_local, | |
| 28 const base::Time& creation_time, | |
| 29 const std::string& guid, | |
| 30 const std::string& extension_id, | |
| 31 const std::string& title, | |
| 32 const std::string& body) | |
| 33 : is_local_(is_local), | |
| 34 creation_time_(creation_time), | |
| 35 extension_id_(extension_id), | |
| 36 title_(title), | |
| 37 body_(body) { | |
| 38 guid_ = guid.empty() ? base::GenerateGUID() : guid; | |
| 39 } | |
| 40 | |
| 41 AppNotification::~AppNotification() {} | |
| 42 | |
| 43 AppNotification* AppNotification::Copy() { | |
| 44 AppNotification* copy = new AppNotification( | |
| 45 this->is_local(), this->creation_time(), | |
| 46 this->guid(), this->extension_id(), | |
| 47 this->title(), this->body()); | |
| 48 copy->set_link_url(this->link_url()); | |
| 49 copy->set_link_text(this->link_text()); | |
| 50 return copy; | |
| 51 } | |
| 52 | |
| 53 void AppNotification::ToDictionaryValue(DictionaryValue* result) const { | |
| 54 CHECK(result); | |
| 55 result->SetBoolean(kIsLocalKey, is_local_); | |
| 56 if (!creation_time_.is_null()) | |
| 57 result->SetString(kCreationTime, | |
| 58 base::Int64ToString(creation_time_.ToInternalValue())); | |
| 59 if (!guid_.empty()) | |
| 60 result->SetString(kGuidKey, guid_); | |
| 61 if (!extension_id_.empty()) | |
| 62 result->SetString(kExtensionIdKey, extension_id_); | |
| 63 if (!title_.empty()) | |
| 64 result->SetString(kTitleKey, title_); | |
| 65 if (!body_.empty()) | |
| 66 result->SetString(kBodyKey, body_); | |
| 67 if (!link_url_.is_empty()) | |
| 68 result->SetString(kLinkUrlKey, link_url_.possibly_invalid_spec()); | |
| 69 if (!link_text_.empty()) | |
| 70 result->SetString(kLinkTextKey, link_text_); | |
| 71 } | |
| 72 | |
| 73 // static | |
| 74 AppNotification* AppNotification::FromDictionaryValue( | |
| 75 const DictionaryValue& value) { | |
| 76 scoped_ptr<AppNotification> result( | |
| 77 new AppNotification(true, | |
| 78 base::Time::FromInternalValue(0), "", "", "", "")); | |
| 79 | |
| 80 if (value.HasKey(kIsLocalKey) && !value.GetBoolean( | |
| 81 kIsLocalKey, &result->is_local_)) { | |
| 82 return NULL; | |
| 83 } | |
| 84 if (value.HasKey(kCreationTime)) { | |
| 85 std::string time_string; | |
| 86 if (!value.GetString(kCreationTime, &time_string)) | |
| 87 return NULL; | |
| 88 int64 time_internal; | |
| 89 if (!base::StringToInt64(time_string, &time_internal)) { | |
| 90 return NULL; | |
| 91 } | |
| 92 base::Time time = base::Time::FromInternalValue(time_internal); | |
| 93 if (time.is_null()) { | |
| 94 return NULL; | |
| 95 } | |
| 96 result->set_creation_time(time); | |
| 97 } else { | |
| 98 return NULL; | |
| 99 } | |
| 100 | |
| 101 if (value.HasKey(kGuidKey) && !value.GetString(kGuidKey, &result->guid_)) | |
| 102 return NULL; | |
| 103 if (value.HasKey(kExtensionIdKey) && | |
| 104 !value.GetString(kExtensionIdKey, &result->extension_id_)) | |
| 105 return NULL; | |
| 106 if (value.HasKey(kTitleKey) && !value.GetString(kTitleKey, &result->title_)) | |
| 107 return NULL; | |
| 108 if (value.HasKey(kBodyKey) && !value.GetString(kBodyKey, &result->body_)) | |
| 109 return NULL; | |
| 110 if (value.HasKey(kLinkUrlKey)) { | |
| 111 std::string url; | |
| 112 if (!value.GetString(kLinkUrlKey, &url)) | |
| 113 return NULL; | |
| 114 GURL gurl(url); | |
| 115 if (!gurl.is_valid()) | |
| 116 return NULL; | |
| 117 result->set_link_url(gurl); | |
| 118 } | |
| 119 if (value.HasKey(kLinkTextKey) && | |
| 120 !value.GetString(kLinkTextKey, &result->link_text_)) { | |
| 121 return NULL; | |
| 122 } | |
| 123 | |
| 124 return result.release(); | |
| 125 } | |
| 126 | |
| 127 bool AppNotification::Equals(const AppNotification& other) const { | |
| 128 return (is_local_ == other.is_local_ && | |
| 129 creation_time_ == other.creation_time_ && | |
| 130 guid_ == other.guid_ && | |
| 131 extension_id_ == other.extension_id_ && | |
| 132 title_ == other.title_ && | |
| 133 body_ == other.body_ && | |
| 134 link_url_ == other.link_url_ && | |
| 135 link_text_ == other.link_text_); | |
| 136 } | |
| 137 | |
| 138 std::string AppNotification::ToString() const { | |
| 139 DictionaryValue value; | |
| 140 ToDictionaryValue(&value); | |
| 141 std::string result; | |
| 142 base::JSONWriter::WriteWithOptions(&value, | |
| 143 base::JSONWriter::OPTIONS_PRETTY_PRINT, | |
| 144 &result); | |
| 145 return result; | |
| 146 } | |
| 147 | |
| 148 AppNotificationList* CopyAppNotificationList( | |
| 149 const AppNotificationList& source) { | |
| 150 AppNotificationList* copy = new AppNotificationList(); | |
| 151 | |
| 152 for (AppNotificationList::const_iterator iter = source.begin(); | |
| 153 iter != source.end(); ++iter) { | |
| 154 copy->push_back(linked_ptr<AppNotification>(iter->get()->Copy())); | |
| 155 } | |
| 156 return copy; | |
| 157 } | |
| 158 | |
| 159 } // namespace extensions | |
| OLD | NEW |