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 SyncedNotification::SyncedNotification(const syncer::SyncData sync_data) | |
17 : sync_data_(sync_data), has_local_changes_(false) { | |
18 } | |
19 | |
20 SyncedNotification::~SyncedNotification() {} | |
21 | |
22 // TODO(petewil): Consider the timestamp too once it gets added to the protobuf. | |
23 bool SyncedNotification::Equals(const SyncedNotification& other) const { | |
24 // Two notifications are equal if the <appId/coalescingKey> pair matches. | |
25 return (get_notification_id() == other.get_notification_id()); | |
26 } | |
27 | |
28 // Set the read state on the notification, returns true for success. | |
29 bool SyncedNotification::SetReadState( | |
30 SyncedNotificationCoalescedNotification_ReadState readState) { | |
31 if (!sync_data_.GetSpecifics().synced_notification(). | |
32 has_coalesced_notification()) | |
33 return false; | |
34 | |
35 // TODO(petewil): implement | |
36 return true; | |
37 } | |
38 | |
39 // Mark this notification as having been read locally. | |
40 void SyncedNotification::NotificationHasBeenRead() { | |
41 // We set the is_local_ flag to true since we modified it locally | |
42 // then we create a sync change object to pass back up. | |
43 has_local_changes_ = true; | |
44 | |
45 bool success = SetReadState( | |
46 SyncedNotificationCoalescedNotification_ReadState_READ); | |
47 DCHECK(success); | |
48 } | |
49 | |
50 // mark this notification as having been dismissed locally | |
51 void SyncedNotification::NotificationHasBeenDeleted() { | |
52 // We set the is_deleted_ 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 SyncedNotificationCoalescedNotification_ReadState_DISMISSED); | |
58 DCHECK(success); | |
59 } | |
60 | |
61 // TODO(petewil): Consider whether the repeated code below can be re-used. | |
62 // A first attempt to do so failed. | |
63 | |
64 const SyncedNotificationSpecifics* | |
65 SyncedNotification::GetSyncedNotificationSpecifics() { | |
66 return &(sync_data_.GetSpecifics().synced_notification()); | |
67 } | |
68 | |
69 const std::string SyncedNotification::get_first_external_id() const { | |
70 if (!sync_data_.GetSpecifics().synced_notification(). | |
71 has_coalesced_notification()) | |
72 return ""; | |
73 if (sync_data_.GetSpecifics().synced_notification(). | |
74 coalesced_notification().notification_size() < 1) | |
75 return ""; | |
76 if (!sync_data_.GetSpecifics().synced_notification(). | |
77 coalesced_notification().notification(0).has_external_id()) | |
78 return ""; | |
79 | |
80 return sync_data_.GetSpecifics().synced_notification(). | |
81 coalesced_notification().notification(0).external_id(); | |
82 } | |
83 | |
84 const std::string SyncedNotification::get_title() const { | |
85 if (!sync_data_.GetSpecifics().synced_notification(). | |
Nicolas Zea
2013/01/18 23:09:25
I think you might be able to get away with just ch
Pete Williamson
2013/01/23 01:45:55
Done.
| |
86 has_coalesced_notification()) | |
87 return ""; | |
88 if (!sync_data_.GetSpecifics().synced_notification(). | |
89 coalesced_notification().has_render_info()) | |
90 return ""; | |
91 if (!sync_data_.GetSpecifics().synced_notification(). | |
92 coalesced_notification().render_info().has_layout()) | |
93 return ""; | |
94 if (!sync_data_.GetSpecifics().synced_notification(). | |
95 coalesced_notification().render_info().layout().has_layout_type()) | |
96 return ""; | |
97 | |
98 const SyncedNotificationRenderInfo_Layout_LayoutType layout_type = | |
99 sync_data_.GetSpecifics().synced_notification(). | |
100 coalesced_notification().render_info().layout().layout_type(); | |
101 | |
102 // Depending on the layout type, get the proper title. | |
103 if (SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_SUBTEXT | |
dcheng
2013/01/18 21:56:13
switch () { }?
It may be helpful in other areas t
Pete Williamson
2013/01/23 01:45:55
changed here. In the other places I left the if s
| |
104 == layout_type) { | |
105 | |
106 // If we have title and subtext, get that title. | |
107 if (!sync_data_.GetSpecifics().synced_notification(). | |
108 coalesced_notification().render_info().layout(). | |
109 has_title_and_subtext_data()) | |
110 return ""; | |
111 if (!sync_data_.GetSpecifics().synced_notification(). | |
112 coalesced_notification().render_info().layout(). | |
113 title_and_subtext_data().has_title()) | |
114 return ""; | |
115 | |
116 // We have a title after all, return it. | |
117 return sync_data_.GetSpecifics().synced_notification(). | |
118 coalesced_notification().render_info().layout(). | |
119 title_and_subtext_data().title(); | |
120 | |
121 } else if (SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_IMAGE | |
122 == layout_type) { | |
123 | |
124 // If we have title and image, get that title. | |
125 if (!sync_data_.GetSpecifics().synced_notification(). | |
126 coalesced_notification().render_info().layout(). | |
127 has_title_and_image_data()) | |
128 return ""; | |
129 if (!sync_data_.GetSpecifics().synced_notification(). | |
130 coalesced_notification().render_info().layout(). | |
131 title_and_image_data().has_title()) | |
132 return ""; | |
133 | |
134 // We have a title after all, return it. | |
135 return sync_data_.GetSpecifics().synced_notification(). | |
136 coalesced_notification().render_info().layout(). | |
137 title_and_image_data().title(); | |
138 } else { | |
139 // This is an error case, we should never get here unless the protobuf | |
140 // is bad, or a new type is introduced and this code does not get updated. | |
141 DCHECK(false); | |
142 return ""; | |
143 } | |
144 } | |
145 | |
146 const std::string SyncedNotification::get_app_id() const { | |
147 if (!sync_data_.GetSpecifics().synced_notification(). | |
148 has_coalesced_notification()) | |
149 return ""; | |
150 if (!sync_data_.GetSpecifics().synced_notification(). | |
151 coalesced_notification().has_id()) | |
152 return ""; | |
153 if (!sync_data_.GetSpecifics().synced_notification(). | |
154 coalesced_notification().id(). | |
155 has_app_id()) | |
156 return ""; | |
157 return sync_data_.GetSpecifics().synced_notification(). | |
158 coalesced_notification().id().app_id(); | |
159 } | |
160 | |
161 const std::string SyncedNotification::get_coalescing_key() const { | |
162 | |
163 if (!sync_data_.GetSpecifics().synced_notification(). | |
164 has_coalesced_notification()) | |
165 return ""; | |
166 if (!sync_data_.GetSpecifics().synced_notification(). | |
167 coalesced_notification().has_id()) | |
168 return ""; | |
169 if (!sync_data_.GetSpecifics().synced_notification(). | |
170 coalesced_notification().id(). | |
171 has_coalescing_key()) | |
172 return ""; | |
173 return sync_data_.GetSpecifics().synced_notification(). | |
174 coalesced_notification().id().coalescing_key(); | |
175 } | |
176 | |
177 const std::string SyncedNotification::get_origin_url() const { | |
dcheng
2013/01/18 21:56:13
Return GURL?
Pete Williamson
2013/01/23 01:45:55
Done.
| |
178 std::string origin_url(kExtensionScheme); | |
179 origin_url += get_app_id(); | |
180 return origin_url; | |
181 } | |
182 | |
183 const std::string SyncedNotification::get_icon_url() const { | |
dcheng
2013/01/18 21:56:13
Return GURL?
Pete Williamson
2013/01/23 01:45:55
Done.
| |
184 if (!sync_data_.GetSpecifics().synced_notification(). | |
185 has_coalesced_notification()) | |
186 return ""; | |
187 if (!sync_data_.GetSpecifics().synced_notification(). | |
188 coalesced_notification().has_render_info()) | |
189 return ""; | |
190 if (!sync_data_.GetSpecifics().synced_notification(). | |
191 coalesced_notification().render_info().has_layout()) | |
192 return ""; | |
193 if (!sync_data_.GetSpecifics().synced_notification(). | |
194 coalesced_notification().render_info().layout().has_layout_type()) | |
195 return ""; | |
196 | |
197 const SyncedNotificationRenderInfo_Layout_LayoutType layout_type = | |
198 sync_data_.GetSpecifics().synced_notification(). | |
199 coalesced_notification().render_info().layout().layout_type(); | |
200 | |
201 // Depending on the layout type, get the icon. | |
202 if (SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_SUBTEXT | |
203 == layout_type) { | |
204 | |
205 // If we have title and subtext, get that icon. | |
206 if (!sync_data_.GetSpecifics().synced_notification(). | |
207 coalesced_notification().render_info().layout(). | |
208 has_title_and_subtext_data()) | |
209 return ""; | |
210 if (!sync_data_.GetSpecifics().synced_notification(). | |
211 coalesced_notification().render_info().layout(). | |
212 title_and_subtext_data().has_icon()) | |
213 return ""; | |
214 | |
215 // We have an icon after all, get the URL as a string. | |
dcheng
2013/01/18 21:56:13
Some of these comments seem misplaced and/or unnec
Pete Williamson
2013/01/23 01:45:55
Done.
| |
216 if (!sync_data_.GetSpecifics().synced_notification(). | |
217 coalesced_notification().render_info().layout(). | |
218 title_and_subtext_data().icon().has_url()) | |
219 return ""; | |
220 | |
221 return sync_data_.GetSpecifics().synced_notification(). | |
222 coalesced_notification().render_info().layout(). | |
223 title_and_subtext_data().icon().url(); | |
224 | |
225 } | |
226 return ""; | |
227 } | |
228 | |
229 const std::string SyncedNotification::get_body() const { | |
230 // If we have subtext data, concat the text lines and return it. | |
dcheng
2013/01/18 21:56:13
concatenate.
Pete Williamson
2013/01/23 01:45:55
Done.
| |
231 if (!sync_data_.GetSpecifics().synced_notification(). | |
232 has_coalesced_notification()) | |
233 return ""; | |
234 if (!sync_data_.GetSpecifics().synced_notification(). | |
235 coalesced_notification().has_render_info()) | |
236 return ""; | |
237 if (!sync_data_.GetSpecifics().synced_notification(). | |
238 coalesced_notification().render_info().has_layout()) | |
239 return ""; | |
240 if (!sync_data_.GetSpecifics().synced_notification(). | |
241 coalesced_notification().render_info().layout().has_layout_type()) | |
242 return ""; | |
243 | |
244 const SyncedNotificationRenderInfo_Layout_LayoutType layout_type = | |
245 sync_data_.GetSpecifics().synced_notification(). | |
246 coalesced_notification().render_info().layout().layout_type(); | |
247 | |
248 // Check if this layout type includes body text. | |
249 if (SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_SUBTEXT | |
250 == layout_type) { | |
251 | |
252 // If we have title and subtext, get the text. | |
253 if (!sync_data_.GetSpecifics().synced_notification(). | |
254 coalesced_notification().render_info().layout(). | |
255 has_title_and_subtext_data()) | |
256 return ""; | |
257 int subtext_lines = sync_data_.GetSpecifics().synced_notification(). | |
258 coalesced_notification().render_info().layout(). | |
259 title_and_subtext_data().subtext_size(); | |
260 if (subtext_lines < 1) | |
261 return ""; | |
262 | |
263 // We have a text after all, get it, separate with newlines. | |
264 std::string subtext; | |
265 for (int ii = 0; ii < subtext_lines; ++ii) { | |
266 subtext += sync_data_.GetSpecifics().synced_notification(). | |
267 coalesced_notification().render_info().layout(). | |
268 title_and_subtext_data().subtext(ii); | |
269 if ( ii < subtext_lines - 1) | |
270 subtext += '\n'; | |
271 } | |
272 return subtext; | |
273 } | |
274 return ""; | |
275 } | |
276 | |
277 const std::string SyncedNotification::get_notification_id() const { | |
278 // Append the coalescing key to the app id to get the unique id. | |
279 std::string id = get_app_id(); | |
280 id += "/"; | |
281 id += get_coalescing_key(); | |
282 | |
283 return id; | |
284 } | |
OLD | NEW |