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/notifications/sync_notifier/synced_notification.h" |
| 6 |
| 7 #include "sync/protocol/sync.pb.h" |
| 8 #include "sync/protocol/synced_notification_specifics.pb.h" |
| 9 |
| 10 namespace { |
| 11 const char kExtensionScheme[] = "chrome-extension://"; |
| 12 } // namespace |
| 13 |
| 14 namespace notifier { |
| 15 |
| 16 // An important side effect of the constructor is that it keeps the |
| 17 // SyncNotificationSpecifics alive by putting it into the contained sync entity. |
| 18 // At construction time, copy the data we might need from the sync_data |
| 19 // protobuf object. |
| 20 SyncedNotification::SyncedNotification(const syncer::SyncData& sync_data) |
| 21 : sync_data_(sync_data), |
| 22 has_local_changes_(false), |
| 23 title_(ExtractTitle(sync_data)), |
| 24 app_id_(ExtractAppId(sync_data)), |
| 25 coalescing_key_(ExtractCoalescingKey(sync_data)), |
| 26 first_external_id_(ExtractFirstExternalId(sync_data)), |
| 27 notification_id_(ExtractNotificationId(sync_data)), |
| 28 body_(ExtractBody(sync_data)), |
| 29 origin_url_(ExtractOriginUrl(sync_data)), |
| 30 icon_url_(ExtractIconUrl(sync_data)), |
| 31 image_url_(ExtractImageUrl(sync_data)) { |
| 32 } |
| 33 |
| 34 SyncedNotification::~SyncedNotification() {} |
| 35 |
| 36 const std::string& SyncedNotification::title() const { |
| 37 return title_; |
| 38 } |
| 39 |
| 40 const std::string& SyncedNotification::app_id() const { |
| 41 return app_id_; |
| 42 } |
| 43 |
| 44 const std::string& SyncedNotification::coalescing_key() const { |
| 45 return coalescing_key_; |
| 46 } |
| 47 |
| 48 const GURL& SyncedNotification::origin_url() const { |
| 49 return origin_url_; |
| 50 } |
| 51 |
| 52 const GURL& SyncedNotification::icon_url() const { |
| 53 return icon_url_; |
| 54 } |
| 55 |
| 56 const GURL& SyncedNotification::image_url() const { |
| 57 return image_url_; |
| 58 } |
| 59 |
| 60 const std::string& SyncedNotification::first_external_id() const { |
| 61 return first_external_id_; |
| 62 } |
| 63 |
| 64 const std::string& SyncedNotification::notification_id() const { |
| 65 return notification_id_; |
| 66 } |
| 67 |
| 68 const std::string& SyncedNotification::body() const { |
| 69 return body_; |
| 70 } |
| 71 |
| 72 // TODO(petewil): Consider the timestamp too once it gets added to the protobuf. |
| 73 bool SyncedNotification::Equals(const SyncedNotification& other) const { |
| 74 // Two notifications are equal if the <appId/coalescingKey> pair matches. |
| 75 return (notification_id() == other.notification_id()); |
| 76 } |
| 77 |
| 78 // Set the read state on the notification, returns true for success. |
| 79 bool SyncedNotification::SetReadState( |
| 80 const sync_pb::CoalescedSyncedNotification_ReadState& readState) { |
| 81 |
| 82 // TODO(petewil): implement |
| 83 return true; |
| 84 } |
| 85 |
| 86 // Mark this notification as having been read locally. |
| 87 void SyncedNotification::NotificationHasBeenRead() { |
| 88 // We set the is_local_ flag to true since we modified it locally |
| 89 // then we create a sync change object to pass back up. |
| 90 has_local_changes_ = true; |
| 91 |
| 92 bool success = SetReadState( |
| 93 sync_pb::CoalescedSyncedNotification_ReadState_READ); |
| 94 DCHECK(success); |
| 95 } |
| 96 |
| 97 // mark this notification as having been dismissed locally |
| 98 void SyncedNotification::NotificationHasBeenDeleted() { |
| 99 // We set the is_deleted_ flag to true since we modified it locally |
| 100 // then we create a sync change object to pass back up. |
| 101 has_local_changes_ = true; |
| 102 |
| 103 bool success = SetReadState( |
| 104 sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED); |
| 105 DCHECK(success); |
| 106 } |
| 107 |
| 108 // TODO(petewil): Consider whether the repeated code below can be re-used. |
| 109 // A first attempt to do so failed. |
| 110 |
| 111 const sync_pb::SyncedNotificationSpecifics* |
| 112 SyncedNotification::GetSyncedNotificationSpecifics() { |
| 113 return &(sync_data_.GetSpecifics().synced_notification()); |
| 114 } |
| 115 |
| 116 std::string SyncedNotification::ExtractFirstExternalId( |
| 117 const syncer::SyncData& sync_data) const { |
| 118 if (!sync_data.GetSpecifics().synced_notification(). |
| 119 has_coalesced_notification()) |
| 120 return ""; |
| 121 if (sync_data.GetSpecifics().synced_notification(). |
| 122 coalesced_notification().notification_size() < 1) |
| 123 return ""; |
| 124 if (!sync_data.GetSpecifics().synced_notification(). |
| 125 coalesced_notification().notification(0).has_external_id()) |
| 126 return ""; |
| 127 |
| 128 return sync_data.GetSpecifics().synced_notification(). |
| 129 coalesced_notification().notification(0).external_id(); |
| 130 } |
| 131 |
| 132 std::string SyncedNotification::ExtractTitle( |
| 133 const syncer::SyncData& sync_data) const { |
| 134 if (!sync_data.GetSpecifics().synced_notification(). |
| 135 coalesced_notification().render_info().layout().has_layout_type()) |
| 136 return ""; |
| 137 |
| 138 const sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType layout_type = |
| 139 sync_data.GetSpecifics().synced_notification(). |
| 140 coalesced_notification().render_info().layout().layout_type(); |
| 141 |
| 142 // Depending on the layout type, get the proper title. |
| 143 switch (layout_type) { |
| 144 case sync_pb:: |
| 145 SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_SUBTEXT: { |
| 146 // If we have title and subtext, get that title. |
| 147 if (!sync_data.GetSpecifics().synced_notification(). |
| 148 coalesced_notification().render_info().layout(). |
| 149 title_and_subtext_data().has_title()) |
| 150 return ""; |
| 151 |
| 152 return sync_data.GetSpecifics().synced_notification(). |
| 153 coalesced_notification().render_info().layout(). |
| 154 title_and_subtext_data().title(); |
| 155 } |
| 156 |
| 157 case sync_pb:: |
| 158 SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_IMAGE: { |
| 159 // If we have title and image, get that title. |
| 160 if (!sync_data.GetSpecifics().synced_notification(). |
| 161 coalesced_notification().render_info().layout(). |
| 162 title_and_image_data().has_title()) |
| 163 return ""; |
| 164 |
| 165 return sync_data.GetSpecifics().synced_notification(). |
| 166 coalesced_notification().render_info().layout(). |
| 167 title_and_image_data().title(); |
| 168 } |
| 169 default: { |
| 170 // This is an error case, we should never get here unless the protobuf |
| 171 // is bad, or a new type is introduced and this code does not get updated. |
| 172 NOTREACHED(); |
| 173 return ""; |
| 174 } |
| 175 } |
| 176 } |
| 177 |
| 178 std::string SyncedNotification::ExtractAppId( |
| 179 const syncer::SyncData& sync_data) const { |
| 180 if (!sync_data.GetSpecifics().synced_notification(). |
| 181 coalesced_notification().id(). |
| 182 has_app_id()) |
| 183 return ""; |
| 184 return sync_data.GetSpecifics().synced_notification(). |
| 185 coalesced_notification().id().app_id(); |
| 186 } |
| 187 |
| 188 std::string SyncedNotification::ExtractCoalescingKey( |
| 189 const syncer::SyncData& sync_data) const { |
| 190 if (!sync_data.GetSpecifics().synced_notification(). |
| 191 coalesced_notification().id(). |
| 192 has_coalescing_key()) |
| 193 return ""; |
| 194 return sync_data.GetSpecifics().synced_notification(). |
| 195 coalesced_notification().id().coalescing_key(); |
| 196 } |
| 197 |
| 198 GURL SyncedNotification::ExtractOriginUrl( |
| 199 const syncer::SyncData& sync_data) const { |
| 200 std::string origin_url(kExtensionScheme); |
| 201 origin_url += app_id_; |
| 202 return GURL(origin_url); |
| 203 } |
| 204 |
| 205 GURL SyncedNotification::ExtractIconUrl(const syncer::SyncData& sync_data) |
| 206 const { |
| 207 if (!sync_data.GetSpecifics().synced_notification(). |
| 208 coalesced_notification().render_info().layout().has_layout_type()) |
| 209 return GURL(); |
| 210 |
| 211 const sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType layout_type = |
| 212 sync_data.GetSpecifics().synced_notification(). |
| 213 coalesced_notification().render_info().layout().layout_type(); |
| 214 |
| 215 // Depending on the layout type, get the icon. |
| 216 if (sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_SUBTEXT |
| 217 == layout_type) { |
| 218 // If we have title and subtext, get that icon. |
| 219 if (!sync_data.GetSpecifics().synced_notification(). |
| 220 coalesced_notification().render_info().layout(). |
| 221 title_and_subtext_data().icon().has_url()) |
| 222 return GURL(); |
| 223 |
| 224 return GURL(sync_data.GetSpecifics().synced_notification(). |
| 225 coalesced_notification().render_info().layout(). |
| 226 title_and_subtext_data().icon().url()); |
| 227 } |
| 228 return GURL(); |
| 229 } |
| 230 |
| 231 GURL SyncedNotification::ExtractImageUrl(const syncer::SyncData& sync_data) |
| 232 const { |
| 233 if (!sync_data.GetSpecifics().synced_notification(). |
| 234 coalesced_notification().render_info().layout().has_layout_type()) |
| 235 return GURL(); |
| 236 |
| 237 const sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType layout_type = |
| 238 sync_data.GetSpecifics().synced_notification(). |
| 239 coalesced_notification().render_info().layout().layout_type(); |
| 240 |
| 241 // Depending on the layout type, get the image. |
| 242 if (sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_IMAGE |
| 243 == layout_type) { |
| 244 // If we have title and subtext, get that image. |
| 245 if (!sync_data.GetSpecifics().synced_notification(). |
| 246 coalesced_notification().render_info().layout(). |
| 247 title_and_image_data().image().has_url()) |
| 248 return GURL(); |
| 249 |
| 250 return GURL(sync_data.GetSpecifics().synced_notification(). |
| 251 coalesced_notification().render_info().layout(). |
| 252 title_and_image_data().image().url()); |
| 253 } |
| 254 return GURL(); |
| 255 } |
| 256 |
| 257 std::string SyncedNotification::ExtractBody( |
| 258 const syncer::SyncData& sync_data) const { |
| 259 // If we have subtext data, concatenate the text lines and return it. |
| 260 if (!sync_data.GetSpecifics().synced_notification(). |
| 261 coalesced_notification().render_info().layout().has_layout_type()) |
| 262 return ""; |
| 263 |
| 264 const sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType layout_type = |
| 265 sync_data.GetSpecifics().synced_notification(). |
| 266 coalesced_notification().render_info().layout().layout_type(); |
| 267 |
| 268 // Check if this layout type includes body text. |
| 269 if (sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_SUBTEXT |
| 270 == layout_type) { |
| 271 // If we have title and subtext, get the text. |
| 272 if (!sync_data.GetSpecifics().synced_notification(). |
| 273 coalesced_notification().render_info().layout(). |
| 274 has_title_and_subtext_data()) |
| 275 return ""; |
| 276 int subtext_lines = sync_data.GetSpecifics().synced_notification(). |
| 277 coalesced_notification().render_info().layout(). |
| 278 title_and_subtext_data().subtext_size(); |
| 279 if (subtext_lines < 1) |
| 280 return ""; |
| 281 |
| 282 std::string subtext; |
| 283 for (int ii = 0; ii < subtext_lines; ++ii) { |
| 284 subtext += sync_data.GetSpecifics().synced_notification(). |
| 285 coalesced_notification().render_info().layout(). |
| 286 title_and_subtext_data().subtext(ii); |
| 287 if (ii < subtext_lines - 1) |
| 288 subtext += '\n'; |
| 289 } |
| 290 return subtext; |
| 291 } |
| 292 return ""; |
| 293 } |
| 294 |
| 295 std::string SyncedNotification::ExtractNotificationId( |
| 296 const syncer::SyncData& sync_data) const { |
| 297 // Append the coalescing key to the app id to get the unique id. |
| 298 std::string id = app_id_; |
| 299 id += "/"; |
| 300 id += coalescing_key_; |
| 301 |
| 302 return id; |
| 303 } |
| 304 |
| 305 } // namespace notifier |
OLD | NEW |