Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/notifications/sync_notifier/synced_notification.h" | 5 #include "chrome/browser/notifications/sync_notifier/synced_notification.h" |
| 6 | 6 |
| 7 #include "sync/protocol/sync.pb.h" | 7 #include "sync/protocol/sync.pb.h" |
| 8 #include "sync/protocol/synced_notification_specifics.pb.h" | 8 #include "sync/protocol/synced_notification_specifics.pb.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED, | 26 sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED, |
| 27 local_enum_must_match_protobuf_enum); | 27 local_enum_must_match_protobuf_enum); |
| 28 | 28 |
| 29 SyncedNotification::SyncedNotification(const syncer::SyncData& sync_data) { | 29 SyncedNotification::SyncedNotification(const syncer::SyncData& sync_data) { |
| 30 Update(sync_data); | 30 Update(sync_data); |
| 31 } | 31 } |
| 32 | 32 |
| 33 SyncedNotification::~SyncedNotification() {} | 33 SyncedNotification::~SyncedNotification() {} |
| 34 | 34 |
| 35 void SyncedNotification::Update(const syncer::SyncData& sync_data) { | 35 void SyncedNotification::Update(const syncer::SyncData& sync_data) { |
| 36 // TODO(petewil): Let's add checking that the notification looks valid. | |
| 36 specifics_.CopyFrom(sync_data.GetSpecifics().synced_notification()); | 37 specifics_.CopyFrom(sync_data.GetSpecifics().synced_notification()); |
| 37 } | 38 } |
| 38 | 39 |
| 39 sync_pb::EntitySpecifics SyncedNotification::GetEntitySpecifics() const { | 40 sync_pb::EntitySpecifics SyncedNotification::GetEntitySpecifics() const { |
| 40 sync_pb::EntitySpecifics entity_specifics; | 41 sync_pb::EntitySpecifics entity_specifics; |
| 41 entity_specifics.mutable_synced_notification()->CopyFrom(specifics_); | 42 entity_specifics.mutable_synced_notification()->CopyFrom(specifics_); |
| 42 return entity_specifics; | 43 return entity_specifics; |
| 43 } | 44 } |
| 44 | 45 |
| 45 std::string SyncedNotification::title() const { | |
| 46 return ExtractTitle(); | |
| 47 } | |
| 48 | |
| 49 std::string SyncedNotification::heading() const { | |
| 50 return ExtractHeading(); | |
| 51 } | |
| 52 | |
| 53 std::string SyncedNotification::description() const { | |
| 54 return ExtractDescription(); | |
| 55 } | |
| 56 | |
| 57 std::string SyncedNotification::app_id() const { | |
| 58 return ExtractAppId(); | |
| 59 } | |
| 60 | |
| 61 std::string SyncedNotification::key() const { | |
| 62 return ExtractKey(); | |
| 63 } | |
| 64 | |
| 65 GURL SyncedNotification::origin_url() const { | |
| 66 return ExtractOriginUrl(); | |
| 67 } | |
| 68 | |
| 69 GURL SyncedNotification::app_icon_url() const { | |
| 70 return ExtractAppIconUrl(); | |
| 71 } | |
| 72 | |
| 73 GURL SyncedNotification::image_url() const { | |
| 74 return ExtractImageUrl(); | |
| 75 } | |
| 76 | |
| 77 std::string SyncedNotification::first_external_id() const { | |
| 78 return ExtractFirstExternalId(); | |
| 79 } | |
| 80 | |
| 81 std::string SyncedNotification::notification_id() const { | |
| 82 return ExtractNotificationId(); | |
| 83 } | |
| 84 | |
| 85 std::string SyncedNotification::text() const { | |
| 86 return ExtractText(); | |
| 87 } | |
| 88 | |
| 89 SyncedNotification::ReadState SyncedNotification::read_state() const { | |
| 90 return ExtractReadState(); | |
| 91 } | |
| 92 | |
| 93 // TODO(petewil): Consider the timestamp too once it gets added to the protobuf. | 46 // TODO(petewil): Consider the timestamp too once it gets added to the protobuf. |
| 47 // TODO: add more fields in here | |
| 94 bool SyncedNotification::EqualsIgnoringReadState( | 48 bool SyncedNotification::EqualsIgnoringReadState( |
| 95 const SyncedNotification& other) const { | 49 const SyncedNotification& other) const { |
| 96 return (title() == other.title() && | 50 return (GetTitle() == other.GetTitle() && |
| 97 app_id() == other.app_id() && | 51 GetAppId() == other.GetAppId() && |
| 98 key() == other.key() && | 52 GetKey() == other.GetKey() && |
| 99 text() == other.text() && | 53 GetText() == other.GetText() && |
| 100 origin_url() == other.origin_url() && | 54 GetOriginUrl() == other.GetOriginUrl() && |
| 101 app_icon_url() == other.app_icon_url() && | 55 GetAppIconUrl() == other.GetAppIconUrl() && |
| 102 image_url() == other.image_url() ); | 56 GetImageUrl() == other.GetImageUrl() ); |
| 103 } | |
| 104 | |
| 105 bool SyncedNotification::IdMatches(const SyncedNotification& other) const { | |
| 106 // Two notifications have the same ID if the <appId/coalescingKey> pair | |
| 107 // matches. | |
| 108 return (notification_id() == other.notification_id()); | |
| 109 } | 57 } |
| 110 | 58 |
| 111 // Set the read state on the notification, returns true for success. | 59 // Set the read state on the notification, returns true for success. |
| 112 void SyncedNotification::SetReadState(const ReadState& read_state) { | 60 void SyncedNotification::SetReadState(const ReadState& read_state) { |
| 113 | 61 |
| 114 // convert the read state to the protobuf type for read state | 62 // convert the read state to the protobuf type for read state |
| 115 if (kDismissed == read_state) | 63 if (kDismissed == read_state) |
| 116 specifics_.mutable_coalesced_notification()->set_read_state( | 64 specifics_.mutable_coalesced_notification()->set_read_state( |
| 117 sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED); | 65 sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED); |
| 118 else if (kUnread == read_state) | 66 else if (kUnread == read_state) |
| 119 specifics_.mutable_coalesced_notification()->set_read_state( | 67 specifics_.mutable_coalesced_notification()->set_read_state( |
| 120 sync_pb::CoalescedSyncedNotification_ReadState_UNREAD); | 68 sync_pb::CoalescedSyncedNotification_ReadState_UNREAD); |
| 121 else if (kRead == read_state) | 69 else if (kRead == read_state) |
| 122 specifics_.mutable_coalesced_notification()->set_read_state( | 70 specifics_.mutable_coalesced_notification()->set_read_state( |
| 123 sync_pb::CoalescedSyncedNotification_ReadState_READ); | 71 sync_pb::CoalescedSyncedNotification_ReadState_READ); |
| 124 else | 72 else |
| 125 NOTREACHED(); | 73 NOTREACHED(); |
| 126 } | 74 } |
| 127 | 75 |
| 128 void SyncedNotification::NotificationHasBeenRead() { | |
| 129 SetReadState(kRead); | |
| 130 } | |
| 131 | |
| 132 void SyncedNotification::NotificationHasBeenDismissed() { | 76 void SyncedNotification::NotificationHasBeenDismissed() { |
| 133 SetReadState(kDismissed); | 77 SetReadState(kDismissed); |
| 134 } | 78 } |
| 135 | 79 |
| 136 std::string SyncedNotification::ExtractFirstExternalId() const { | 80 std::string SyncedNotification::GetTitle() const { |
| 137 if (!specifics_.has_coalesced_notification() || | |
| 138 specifics_.coalesced_notification().notification_size() < 1 || | |
| 139 !specifics_.coalesced_notification().notification(0).has_external_id()) | |
| 140 return std::string(); | |
| 141 | |
| 142 return specifics_.coalesced_notification().notification(0).external_id(); | |
| 143 } | |
| 144 | |
| 145 std::string SyncedNotification::ExtractTitle() const { | |
| 146 if (!specifics_.coalesced_notification().render_info().expanded_info(). | 81 if (!specifics_.coalesced_notification().render_info().expanded_info(). |
| 147 simple_expanded_layout().has_title()) | 82 simple_expanded_layout().has_title()) |
| 148 return ""; | 83 return std::string(); |
| 149 | 84 |
| 150 return specifics_.coalesced_notification().render_info().expanded_info(). | 85 return specifics_.coalesced_notification().render_info().expanded_info(). |
| 151 simple_expanded_layout().title(); | 86 simple_expanded_layout().title(); |
| 152 } | 87 } |
| 153 | 88 |
| 154 std::string SyncedNotification::ExtractHeading() const { | 89 std::string SyncedNotification::GetHeading() const { |
| 155 if (!specifics_.coalesced_notification().render_info().collapsed_info(). | 90 if (!specifics_.coalesced_notification().render_info().collapsed_info(). |
| 156 simple_collapsed_layout().has_heading()) | 91 simple_collapsed_layout().has_heading()) |
| 157 return ""; | 92 return std::string(); |
| 158 | 93 |
| 159 return specifics_.coalesced_notification().render_info().collapsed_info(). | 94 return specifics_.coalesced_notification().render_info().collapsed_info(). |
| 160 simple_collapsed_layout().heading(); | 95 simple_collapsed_layout().heading(); |
| 161 } | 96 } |
| 162 | 97 |
| 163 std::string SyncedNotification::ExtractDescription() const { | 98 std::string SyncedNotification::GetDescription() const { |
| 164 if (!specifics_.coalesced_notification().render_info().collapsed_info(). | 99 if (!specifics_.coalesced_notification().render_info().collapsed_info(). |
| 165 simple_collapsed_layout().has_description()) | 100 simple_collapsed_layout().has_description()) |
| 166 return ""; | 101 return std::string(); |
| 167 | 102 |
| 168 return specifics_.coalesced_notification().render_info().collapsed_info(). | 103 return specifics_.coalesced_notification().render_info().collapsed_info(). |
| 169 simple_collapsed_layout().description(); | 104 simple_collapsed_layout().description(); |
| 170 } | 105 } |
| 171 | 106 |
| 172 std::string SyncedNotification::ExtractAppId() const { | 107 std::string SyncedNotification::GetAppId() const { |
| 173 if (!specifics_.coalesced_notification().has_app_id()) | 108 if (!specifics_.coalesced_notification().has_app_id()) |
| 174 return ""; | 109 return std::string(); |
| 175 return specifics_.coalesced_notification().app_id(); | 110 return specifics_.coalesced_notification().app_id(); |
| 176 } | 111 } |
| 177 | 112 |
| 178 std::string SyncedNotification::ExtractKey() const { | 113 std::string SyncedNotification::GetKey() const { |
| 179 if (!specifics_.coalesced_notification().has_key()) | 114 if (!specifics_.coalesced_notification().has_key()) |
| 180 return ""; | 115 return std::string(); |
| 181 return specifics_.coalesced_notification().key(); | 116 return specifics_.coalesced_notification().key(); |
| 182 } | 117 } |
| 183 | 118 |
| 184 GURL SyncedNotification::ExtractOriginUrl() const { | 119 GURL SyncedNotification::GetOriginUrl() const { |
| 185 std::string origin_url(kExtensionScheme); | 120 std::string origin_url(kExtensionScheme); |
| 186 origin_url += app_id(); | 121 origin_url += GetAppId(); |
| 187 return GURL(origin_url); | 122 return GURL(origin_url); |
| 188 } | 123 } |
| 189 | 124 |
| 190 GURL SyncedNotification::ExtractAppIconUrl() const { | 125 // TODO(petewil): This only returns the first icon. We should make all the |
| 126 // icons available. | |
| 127 GURL SyncedNotification::GetAppIconUrl() const { | |
| 191 if (specifics_.coalesced_notification().render_info().expanded_info(). | 128 if (specifics_.coalesced_notification().render_info().expanded_info(). |
| 192 collapsed_info_size() == 0) | 129 collapsed_info_size() == 0) |
| 193 return GURL(); | 130 return GURL(); |
| 194 | 131 |
| 195 if (!specifics_.coalesced_notification().render_info().expanded_info(). | 132 if (!specifics_.coalesced_notification().render_info().expanded_info(). |
| 196 collapsed_info(0).simple_collapsed_layout().has_app_icon()) | 133 collapsed_info(0).simple_collapsed_layout().has_app_icon()) |
| 197 return GURL(); | 134 return GURL(); |
| 198 | 135 |
| 199 return GURL(specifics_.coalesced_notification().render_info(). | 136 return GURL(specifics_.coalesced_notification().render_info(). |
| 200 expanded_info().collapsed_info(0).simple_collapsed_layout(). | 137 expanded_info().collapsed_info(0).simple_collapsed_layout(). |
| 201 app_icon().url()); | 138 app_icon().url()); |
| 202 } | 139 } |
| 203 | 140 |
| 204 // TODO(petewil): This currenly only handles the first image from the first | 141 // TODO(petewil): This currenly only handles the first image from the first |
| 205 // collapsed item, someday return all images. | 142 // collapsed item, someday return all images. |
| 206 GURL SyncedNotification::ExtractImageUrl() const { | 143 GURL SyncedNotification::GetImageUrl() const { |
| 207 if (specifics_.coalesced_notification().render_info().expanded_info(). | 144 if (specifics_.coalesced_notification().render_info().expanded_info(). |
| 208 simple_expanded_layout().media_size() == 0) | 145 simple_expanded_layout().media_size() == 0) |
| 209 return GURL(); | 146 return GURL(); |
| 210 | 147 |
| 211 if (!specifics_.coalesced_notification().render_info().expanded_info(). | 148 if (!specifics_.coalesced_notification().render_info().expanded_info(). |
| 212 simple_expanded_layout().media(0).image().has_url()) | 149 simple_expanded_layout().media(0).image().has_url()) |
| 213 return GURL(); | 150 return GURL(); |
| 214 | 151 |
| 215 return GURL(specifics_.coalesced_notification().render_info(). | 152 return GURL(specifics_.coalesced_notification().render_info(). |
| 216 expanded_info().simple_expanded_layout().media(0).image().url()); | 153 expanded_info().simple_expanded_layout().media(0).image().url()); |
| 217 } | 154 } |
| 218 | 155 |
| 219 std::string SyncedNotification::ExtractText() const { | 156 std::string SyncedNotification::GetText() const { |
| 220 if (!specifics_.coalesced_notification().render_info().expanded_info(). | 157 if (!specifics_.coalesced_notification().render_info().expanded_info(). |
| 221 simple_expanded_layout().has_text()) | 158 simple_expanded_layout().has_text()) |
| 222 return ""; | 159 return std::string(); |
| 223 | 160 |
| 224 return specifics_.coalesced_notification().render_info().expanded_info(). | 161 return specifics_.coalesced_notification().render_info().expanded_info(). |
| 225 simple_expanded_layout().text(); | 162 simple_expanded_layout().text(); |
| 226 } | 163 } |
| 227 | 164 |
| 228 SyncedNotification::ReadState SyncedNotification::ExtractReadState() const { | 165 SyncedNotification::ReadState SyncedNotification::GetReadState() const { |
| 229 DCHECK(specifics_.coalesced_notification().has_read_state()); | 166 DCHECK(specifics_.coalesced_notification().has_read_state()); |
| 230 | 167 |
| 231 sync_pb::CoalescedSyncedNotification_ReadState found_read_state = | 168 sync_pb::CoalescedSyncedNotification_ReadState found_read_state = |
| 232 specifics_.coalesced_notification().read_state(); | 169 specifics_.coalesced_notification().read_state(); |
| 233 | 170 |
| 234 if (found_read_state == | 171 if (found_read_state == |
| 235 sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED) { | 172 sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED) { |
| 236 return kDismissed; | 173 return kDismissed; |
| 237 } else if (found_read_state == | 174 } else if (found_read_state == |
| 238 sync_pb::CoalescedSyncedNotification_ReadState_UNREAD) { | 175 sync_pb::CoalescedSyncedNotification_ReadState_UNREAD) { |
| 239 return kUnread; | 176 return kUnread; |
| 240 } else if (found_read_state == | 177 } else if (found_read_state == |
| 241 sync_pb::CoalescedSyncedNotification_ReadState_READ) { | 178 sync_pb::CoalescedSyncedNotification_ReadState_READ) { |
| 242 return kRead; | 179 return kRead; |
| 243 } else { | 180 } else { |
| 244 NOTREACHED(); | 181 NOTREACHED(); |
| 245 return static_cast<SyncedNotification::ReadState>(found_read_state); | 182 return static_cast<SyncedNotification::ReadState>(found_read_state); |
| 246 } | 183 } |
| 247 } | 184 } |
| 248 | 185 |
| 249 std::string SyncedNotification::ExtractNotificationId() const { | 186 // Time in milliseconds since the unix epoch, or 0 if not available. |
| 250 return key(); | 187 uint64 SyncedNotification::GetCreationTime() const { |
| 188 if (!specifics_.coalesced_notification().has_creation_time_msec()) | |
| 189 return 0; | |
| 190 | |
| 191 return specifics_.coalesced_notification().creation_time_msec(); | |
| 192 } | |
| 193 | |
| 194 int SyncedNotification::GetPriority() const { | |
| 195 if (!specifics_.coalesced_notification().has_priority()) | |
| 196 return 0; | |
|
miket_OOO
2013/03/28 01:03:52
See https://code.google.com/p/chromium/codesearch#
Pete Williamson
2013/03/28 17:16:12
Actually that brings up a bigger problem, the enum
| |
| 197 return static_cast<int>(specifics_.coalesced_notification(). | |
| 198 priority()); | |
| 199 } | |
| 200 | |
| 201 int SyncedNotification::GetNotificationCount() const { | |
| 202 return specifics_.coalesced_notification().render_info(). | |
| 203 expanded_info().collapsed_info_size(); | |
| 204 } | |
| 205 | |
| 206 int SyncedNotification::GetButtonCount() const { | |
| 207 return specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 208 target_size(); | |
| 209 } | |
| 210 | |
| 211 std::string SyncedNotification::GetDefaultDestinationTitle() const { | |
| 212 if (!specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 213 default_destination().icon().has_alt_text()) { | |
| 214 return std::string(); | |
| 215 } | |
| 216 return specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 217 default_destination().icon().alt_text(); | |
| 218 } | |
| 219 | |
| 220 std::string SyncedNotification::GetDefaultDestinationIconUrl() const { | |
| 221 if (!specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 222 default_destination().icon().has_url()) { | |
| 223 return std::string(); | |
| 224 } | |
| 225 return specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 226 default_destination().icon().url(); | |
| 227 } | |
| 228 | |
| 229 std::string SyncedNotification::GetDefaultDestinationUrl() const { | |
| 230 if (!specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 231 default_destination().has_url()) { | |
| 232 return std::string(); | |
| 233 } | |
| 234 return specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 235 default_destination().url(); | |
| 236 } | |
| 237 | |
| 238 std::string SyncedNotification::GetButtonOneTitle() const { | |
| 239 // Must ensure that we have a target before trying to access it. | |
| 240 if (GetButtonCount() < 1) | |
| 241 return std::string(); | |
| 242 if (!specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 243 target(0).action().icon().has_alt_text()) { | |
| 244 return std::string(); | |
| 245 } | |
| 246 return specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 247 target(0).action().icon().alt_text(); | |
| 248 } | |
| 249 | |
| 250 std::string SyncedNotification::GetButtonOneIconUrl() const { | |
| 251 // Must ensure that we have a target before trying to access it. | |
| 252 if (GetButtonCount() < 1) | |
| 253 return std::string(); | |
| 254 if (!specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 255 target(0).action().icon().has_url()) { | |
| 256 return std::string(); | |
| 257 } | |
| 258 return specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 259 target(0).action().icon().url(); | |
| 260 } | |
| 261 | |
| 262 std::string SyncedNotification::GetButtonOneUrl() const { | |
| 263 // Must ensure that we have a target before trying to access it. | |
| 264 if (GetButtonCount() < 1) | |
| 265 return std::string(); | |
| 266 if (!specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 267 target(0).action().has_url()) { | |
| 268 return std::string(); | |
| 269 } | |
| 270 return specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 271 target(0).action().url(); | |
| 272 } | |
| 273 | |
| 274 std::string SyncedNotification::GetButtonTwoTitle() const { | |
| 275 // Must ensure that we have a target before trying to access it. | |
| 276 if (GetButtonCount() < 2) | |
| 277 return std::string(); | |
| 278 if (!specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 279 target(1).action().icon().has_alt_text()) { | |
| 280 return std::string(); | |
| 281 } | |
| 282 return specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 283 target(1).action().icon().alt_text(); | |
| 284 } | |
| 285 | |
| 286 std::string SyncedNotification::GetButtonTwoIconUrl() const { | |
| 287 // Must ensure that we have a target before trying to access it. | |
| 288 if (GetButtonCount() < 2) | |
| 289 return std::string(); | |
| 290 if (!specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 291 target(1).action().icon().has_url()) { | |
| 292 return std::string(); | |
| 293 } | |
| 294 return specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 295 target(1).action().icon().url(); | |
| 296 } | |
| 297 | |
| 298 std::string SyncedNotification::GetButtonTwoUrl() const { | |
| 299 // Must ensure that we have a target before trying to access it. | |
| 300 if (GetButtonCount() < 2) | |
| 301 return std::string(); | |
| 302 if (!specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 303 target(1).action().has_url()) { | |
| 304 return std::string(); | |
| 305 } | |
| 306 return specifics_.coalesced_notification().render_info().collapsed_info(). | |
| 307 target(1).action().url(); | |
| 308 } | |
| 309 | |
| 310 std::string SyncedNotification::GetContainedNotificationTitle( | |
| 311 int index) const { | |
| 312 if (specifics_.coalesced_notification().render_info().expanded_info(). | |
| 313 collapsed_info_size() < index + 1) | |
| 314 return std::string(); | |
| 315 | |
| 316 return specifics_.coalesced_notification().render_info().expanded_info(). | |
| 317 collapsed_info(index).simple_collapsed_layout().heading(); | |
| 318 } | |
| 319 | |
| 320 std::string SyncedNotification::GetContainedNotificationMessage( | |
| 321 int index) const { | |
| 322 if (specifics_.coalesced_notification().render_info().expanded_info(). | |
| 323 collapsed_info_size() < index + 1) | |
| 324 return std::string(); | |
| 325 | |
| 326 return specifics_.coalesced_notification().render_info().expanded_info(). | |
| 327 collapsed_info(index).simple_collapsed_layout().description(); | |
| 251 } | 328 } |
| 252 | 329 |
| 253 } // namespace notifier | 330 } // namespace notifier |
| OLD | NEW |