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 using namespace sync_pb; |
| 11 |
| 12 namespace { |
| 13 const char kExtensionScheme[] = "chrome-extension://"; |
| 14 } // namespace |
| 15 |
| 16 namespace notifier { |
| 17 |
| 18 // An important side effect of the constructor is that it keeps the |
| 19 // SyncNotificationSpecifics alive by putting it into the contained sync entity. |
| 20 SyncedNotification::SyncedNotification(const syncer::SyncData& sync_data) |
| 21 : sync_data_(sync_data), has_local_changes_(false) { |
| 22 } |
| 23 |
| 24 SyncedNotification::~SyncedNotification() {} |
| 25 |
| 26 // TODO(petewil): Consider the timestamp too once it gets added to the protobuf. |
| 27 bool SyncedNotification::Equals(const SyncedNotification& other) const { |
| 28 // Two notifications are equal if the <appId/coalescingKey> pair matches. |
| 29 return (GetNotificationId() == other.GetNotificationId()); |
| 30 } |
| 31 |
| 32 // Set the read state on the notification, returns true for success. |
| 33 bool SyncedNotification::SetReadState( |
| 34 sync_pb::SyncedNotificationCoalesced_ReadState readState) { |
| 35 if (!sync_data_.GetSpecifics().synced_notification(). |
| 36 has_coalesced_notification()) |
| 37 return false; |
| 38 |
| 39 // TODO(petewil): implement |
| 40 return true; |
| 41 } |
| 42 |
| 43 // Mark this notification as having been read locally. |
| 44 void SyncedNotification::NotificationHasBeenRead() { |
| 45 // We set the is_local_ flag to true since we modified it locally |
| 46 // then we create a sync change object to pass back up. |
| 47 has_local_changes_ = true; |
| 48 |
| 49 bool success = SetReadState( |
| 50 sync_pb::SyncedNotificationCoalesced_ReadState_READ); |
| 51 DCHECK(success); |
| 52 } |
| 53 |
| 54 // mark this notification as having been dismissed locally |
| 55 void SyncedNotification::NotificationHasBeenDeleted() { |
| 56 // We set the is_deleted_ flag to true since we modified it locally |
| 57 // then we create a sync change object to pass back up. |
| 58 has_local_changes_ = true; |
| 59 |
| 60 bool success = SetReadState( |
| 61 sync_pb::SyncedNotificationCoalesced_ReadState_DISMISSED); |
| 62 DCHECK(success); |
| 63 } |
| 64 |
| 65 // TODO(petewil): Consider whether the repeated code below can be re-used. |
| 66 // A first attempt to do so failed. |
| 67 |
| 68 const sync_pb::SyncedNotificationSpecifics* |
| 69 SyncedNotification::GetSyncedNotificationSpecifics() { |
| 70 return &(sync_data_.GetSpecifics().synced_notification()); |
| 71 } |
| 72 |
| 73 std::string SyncedNotification::GetFirstExternalId() const { |
| 74 if (!sync_data_.GetSpecifics().synced_notification(). |
| 75 has_coalesced_notification()) |
| 76 return ""; |
| 77 if (sync_data_.GetSpecifics().synced_notification(). |
| 78 coalesced_notification().notification_size() < 1) |
| 79 return ""; |
| 80 if (!sync_data_.GetSpecifics().synced_notification(). |
| 81 coalesced_notification().notification(0).has_external_id()) |
| 82 return ""; |
| 83 |
| 84 return sync_data_.GetSpecifics().synced_notification(). |
| 85 coalesced_notification().notification(0).external_id(); |
| 86 } |
| 87 |
| 88 std::string SyncedNotification::GetTitle() const { |
| 89 if (!sync_data_.GetSpecifics().synced_notification(). |
| 90 coalesced_notification().render_info().layout().has_layout_type()) |
| 91 return ""; |
| 92 |
| 93 const SyncedNotificationRenderInfo_Layout_LayoutType layout_type = |
| 94 sync_data_.GetSpecifics().synced_notification(). |
| 95 coalesced_notification().render_info().layout().layout_type(); |
| 96 |
| 97 // Depending on the layout type, get the proper title. |
| 98 switch (layout_type) { |
| 99 case SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_SUBTEXT: { |
| 100 // If we have title and subtext, get that title. |
| 101 if (!sync_data_.GetSpecifics().synced_notification(). |
| 102 coalesced_notification().render_info().layout(). |
| 103 title_and_subtext_data().has_title()) |
| 104 return ""; |
| 105 |
| 106 return sync_data_.GetSpecifics().synced_notification(). |
| 107 coalesced_notification().render_info().layout(). |
| 108 title_and_subtext_data().title(); |
| 109 } |
| 110 |
| 111 case SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_IMAGE: { |
| 112 // If we have title and image, get that title. |
| 113 if (!sync_data_.GetSpecifics().synced_notification(). |
| 114 coalesced_notification().render_info().layout(). |
| 115 title_and_image_data().has_title()) |
| 116 return ""; |
| 117 |
| 118 return sync_data_.GetSpecifics().synced_notification(). |
| 119 coalesced_notification().render_info().layout(). |
| 120 title_and_image_data().title(); |
| 121 } |
| 122 default: { |
| 123 // This is an error case, we should never get here unless the protobuf |
| 124 // is bad, or a new type is introduced and this code does not get updated. |
| 125 DCHECK(false); |
| 126 return ""; |
| 127 } |
| 128 } |
| 129 } |
| 130 |
| 131 std::string SyncedNotification::GetAppId() const { |
| 132 if (!sync_data_.GetSpecifics().synced_notification(). |
| 133 coalesced_notification().id(). |
| 134 has_app_id()) |
| 135 return ""; |
| 136 return sync_data_.GetSpecifics().synced_notification(). |
| 137 coalesced_notification().id().app_id(); |
| 138 } |
| 139 |
| 140 std::string SyncedNotification::GetCoalescingKey() const { |
| 141 if (!sync_data_.GetSpecifics().synced_notification(). |
| 142 coalesced_notification().id(). |
| 143 has_coalescing_key()) |
| 144 return ""; |
| 145 return sync_data_.GetSpecifics().synced_notification(). |
| 146 coalesced_notification().id().coalescing_key(); |
| 147 } |
| 148 |
| 149 GURL SyncedNotification::GetOriginUrl() const { |
| 150 std::string origin_url(kExtensionScheme); |
| 151 origin_url += GetAppId(); |
| 152 return GURL(origin_url); |
| 153 } |
| 154 |
| 155 GURL SyncedNotification::GetIconUrl() const { |
| 156 if (!sync_data_.GetSpecifics().synced_notification(). |
| 157 coalesced_notification().render_info().layout().has_layout_type()) |
| 158 return GURL(); |
| 159 |
| 160 const SyncedNotificationRenderInfo_Layout_LayoutType layout_type = |
| 161 sync_data_.GetSpecifics().synced_notification(). |
| 162 coalesced_notification().render_info().layout().layout_type(); |
| 163 |
| 164 // Depending on the layout type, get the icon. |
| 165 if (SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_SUBTEXT |
| 166 == layout_type) { |
| 167 |
| 168 // If we have title and subtext, get that icon. |
| 169 if (!sync_data_.GetSpecifics().synced_notification(). |
| 170 coalesced_notification().render_info().layout(). |
| 171 title_and_subtext_data().icon().has_url()) |
| 172 return GURL(); |
| 173 |
| 174 return GURL(sync_data_.GetSpecifics().synced_notification(). |
| 175 coalesced_notification().render_info().layout(). |
| 176 title_and_subtext_data().icon().url()); |
| 177 |
| 178 } |
| 179 return GURL(); |
| 180 } |
| 181 |
| 182 std::string SyncedNotification::GetBody() const { |
| 183 // If we have subtext data, concatenate the text lines and return it. |
| 184 if (!sync_data_.GetSpecifics().synced_notification(). |
| 185 coalesced_notification().render_info().layout().has_layout_type()) |
| 186 return ""; |
| 187 |
| 188 const SyncedNotificationRenderInfo_Layout_LayoutType layout_type = |
| 189 sync_data_.GetSpecifics().synced_notification(). |
| 190 coalesced_notification().render_info().layout().layout_type(); |
| 191 |
| 192 // Check if this layout type includes body text. |
| 193 if (SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_SUBTEXT |
| 194 == layout_type) { |
| 195 |
| 196 // If we have title and subtext, get the text. |
| 197 if (!sync_data_.GetSpecifics().synced_notification(). |
| 198 coalesced_notification().render_info().layout(). |
| 199 has_title_and_subtext_data()) |
| 200 return ""; |
| 201 int subtext_lines = sync_data_.GetSpecifics().synced_notification(). |
| 202 coalesced_notification().render_info().layout(). |
| 203 title_and_subtext_data().subtext_size(); |
| 204 if (subtext_lines < 1) |
| 205 return ""; |
| 206 |
| 207 std::string subtext; |
| 208 for (int ii = 0; ii < subtext_lines; ++ii) { |
| 209 subtext += sync_data_.GetSpecifics().synced_notification(). |
| 210 coalesced_notification().render_info().layout(). |
| 211 title_and_subtext_data().subtext(ii); |
| 212 if ( ii < subtext_lines - 1) |
| 213 subtext += '\n'; |
| 214 } |
| 215 return subtext; |
| 216 } |
| 217 return ""; |
| 218 } |
| 219 |
| 220 std::string SyncedNotification::GetNotificationId() const { |
| 221 // Append the coalescing key to the app id to get the unique id. |
| 222 std::string id = GetAppId(); |
| 223 id += "/"; |
| 224 id += GetCoalescingKey(); |
| 225 |
| 226 return id; |
| 227 } |
| 228 |
| 229 } // namespace notifier |
OLD | NEW |