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