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