Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(389)

Side by Side Diff: chrome/browser/notifications/sync_notifier/synced_notification.cc

Issue 12717010: Widen Data Pipes and newer protobufs (Closed) Base URL: http://git.chromium.org/chromium/src.git@newProtobufs
Patch Set: Synced Notifications - newer protobufs and wider data paths 2 Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/notifications/sync_notifier/synced_notification.h" 5 #include "chrome/browser/notifications/sync_notifier/synced_notification.h"
6 6
7 #include "sync/protocol/sync.pb.h" 7 #include "sync/protocol/sync.pb.h"
8 #include "sync/protocol/synced_notification_specifics.pb.h" 8 #include "sync/protocol/synced_notification_specifics.pb.h"
9 9
10 namespace { 10 namespace {
(...skipping 15 matching lines...) Expand all
26 sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED, 26 sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED,
27 local_enum_must_match_protobuf_enum); 27 local_enum_must_match_protobuf_enum);
28 28
29 SyncedNotification::SyncedNotification(const syncer::SyncData& sync_data) { 29 SyncedNotification::SyncedNotification(const syncer::SyncData& sync_data) {
30 Update(sync_data); 30 Update(sync_data);
31 } 31 }
32 32
33 SyncedNotification::~SyncedNotification() {} 33 SyncedNotification::~SyncedNotification() {}
34 34
35 void SyncedNotification::Update(const syncer::SyncData& sync_data) { 35 void SyncedNotification::Update(const syncer::SyncData& sync_data) {
36 // TODO(petewil): Let's add checking that the notification looks valid.
36 specifics_.CopyFrom(sync_data.GetSpecifics().synced_notification()); 37 specifics_.CopyFrom(sync_data.GetSpecifics().synced_notification());
37 } 38 }
38 39
39 sync_pb::EntitySpecifics SyncedNotification::GetEntitySpecifics() const { 40 sync_pb::EntitySpecifics SyncedNotification::GetEntitySpecifics() const {
40 sync_pb::EntitySpecifics entity_specifics; 41 sync_pb::EntitySpecifics entity_specifics;
41 entity_specifics.mutable_synced_notification()->CopyFrom(specifics_); 42 entity_specifics.mutable_synced_notification()->CopyFrom(specifics_);
42 return entity_specifics; 43 return entity_specifics;
43 } 44 }
44 45
45 std::string SyncedNotification::title() const { 46 std::string SyncedNotification::title() const {
(...skipping 21 matching lines...) Expand all
67 } 68 }
68 69
69 GURL SyncedNotification::app_icon_url() const { 70 GURL SyncedNotification::app_icon_url() const {
70 return ExtractAppIconUrl(); 71 return ExtractAppIconUrl();
71 } 72 }
72 73
73 GURL SyncedNotification::image_url() const { 74 GURL SyncedNotification::image_url() const {
74 return ExtractImageUrl(); 75 return ExtractImageUrl();
75 } 76 }
76 77
77 std::string SyncedNotification::first_external_id() const {
78 return ExtractFirstExternalId();
79 }
80
81 std::string SyncedNotification::notification_id() const {
82 return ExtractNotificationId();
83 }
84
85 std::string SyncedNotification::text() const { 78 std::string SyncedNotification::text() const {
86 return ExtractText(); 79 return ExtractText();
87 } 80 }
88 81
89 SyncedNotification::ReadState SyncedNotification::read_state() const { 82 SyncedNotification::ReadState SyncedNotification::read_state() const {
90 return ExtractReadState(); 83 return ExtractReadState();
84
dcheng 2013/03/26 07:48:50 Remove extra newline.
Pete Williamson 2013/03/26 17:18:30 Done.
85 }
86
87 double SyncedNotification::creation_time() const {
88 return ExtractCreationTime();
89 }
90
91 std::string SyncedNotification::default_destination_title() const {
92 return ExtractDefaultDestinationTitle();
93 }
94
95 std::string SyncedNotification::default_destination_icon_url() const {
96 return ExtractDefaultDestinationIconUrl();
97 }
98
99 std::string SyncedNotification::default_destination_url() const {
100 return ExtractDefaultDestinationUrl();
101 }
102
103 std::string SyncedNotification::button_one_title() const {
104 return ExtractButtonOneTitle();
105 }
106
107 std::string SyncedNotification::button_one_icon_url() const {
108 return ExtractButtonOneIconUrl();
109 }
110
111 std::string SyncedNotification::button_one_url() const {
112 return ExtractButtonOneUrl();
113 }
114
115 std::string SyncedNotification::button_two_title() const {
116 return ExtractButtonTwoTitle();
117 }
118
119 std::string SyncedNotification::button_two_icon_url() const {
120 return ExtractButtonTwoIconUrl();
121 }
122
123 std::string SyncedNotification::button_two_url() const {
124 return ExtractButtonTwoUrl();
125 }
126
127 int SyncedNotification::priority() const {
128 return ExtractPriority();
129 }
130
131 int SyncedNotification::button_count() const {
132 return ExtractButtonCount();
133 }
134
135 int SyncedNotification::notification_count() const {
136 return ExtractNotificationCount();
137 }
138
139 std::string SyncedNotification::contained_notification_title(int index) const {
140 return ExtractContainedNotificationTitle(index);
141 }
142
143 std::string SyncedNotification::contained_notification_message(
144 int index) const {
145 return ExtractContainedNotificationMessage(index);
91 } 146 }
92 147
93 // TODO(petewil): Consider the timestamp too once it gets added to the protobuf. 148 // TODO(petewil): Consider the timestamp too once it gets added to the protobuf.
149 // TODO: add more fields in here
94 bool SyncedNotification::EqualsIgnoringReadState( 150 bool SyncedNotification::EqualsIgnoringReadState(
95 const SyncedNotification& other) const { 151 const SyncedNotification& other) const {
96 return (title() == other.title() && 152 return (title() == other.title() &&
97 app_id() == other.app_id() && 153 app_id() == other.app_id() &&
98 key() == other.key() && 154 key() == other.key() &&
99 text() == other.text() && 155 text() == other.text() &&
100 origin_url() == other.origin_url() && 156 origin_url() == other.origin_url() &&
101 app_icon_url() == other.app_icon_url() && 157 app_icon_url() == other.app_icon_url() &&
102 image_url() == other.image_url() ); 158 image_url() == other.image_url() );
103 } 159 }
104 160
105 bool SyncedNotification::IdMatches(const SyncedNotification& other) const { 161 bool SyncedNotification::KeyMatches(const SyncedNotification& other) const {
106 // Two notifications have the same ID if the <appId/coalescingKey> pair 162 // Two notifications that have the same key match.
107 // matches. 163 return (key() == other.key());
108 return (notification_id() == other.notification_id());
109 } 164 }
110 165
111 // Set the read state on the notification, returns true for success. 166 // Set the read state on the notification, returns true for success.
112 void SyncedNotification::SetReadState(const ReadState& read_state) { 167 void SyncedNotification::SetReadState(const ReadState& read_state) {
113 168
114 // convert the read state to the protobuf type for read state 169 // convert the read state to the protobuf type for read state
115 if (kDismissed == read_state) 170 if (kDismissed == read_state)
116 specifics_.mutable_coalesced_notification()->set_read_state( 171 specifics_.mutable_coalesced_notification()->set_read_state(
117 sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED); 172 sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED);
118 else if (kUnread == read_state) 173 else if (kUnread == read_state)
119 specifics_.mutable_coalesced_notification()->set_read_state( 174 specifics_.mutable_coalesced_notification()->set_read_state(
120 sync_pb::CoalescedSyncedNotification_ReadState_UNREAD); 175 sync_pb::CoalescedSyncedNotification_ReadState_UNREAD);
121 else if (kRead == read_state) 176 else if (kRead == read_state)
122 specifics_.mutable_coalesced_notification()->set_read_state( 177 specifics_.mutable_coalesced_notification()->set_read_state(
123 sync_pb::CoalescedSyncedNotification_ReadState_READ); 178 sync_pb::CoalescedSyncedNotification_ReadState_READ);
124 else 179 else
125 NOTREACHED(); 180 NOTREACHED();
126 } 181 }
127 182
128 void SyncedNotification::NotificationHasBeenRead() {
129 SetReadState(kRead);
130 }
131
132 void SyncedNotification::NotificationHasBeenDismissed() { 183 void SyncedNotification::NotificationHasBeenDismissed() {
133 SetReadState(kDismissed); 184 SetReadState(kDismissed);
134 } 185 }
135 186
136 std::string SyncedNotification::ExtractFirstExternalId() const {
137 if (!specifics_.has_coalesced_notification() ||
138 specifics_.coalesced_notification().notification_size() < 1 ||
139 !specifics_.coalesced_notification().notification(0).has_external_id())
140 return std::string();
141
142 return specifics_.coalesced_notification().notification(0).external_id();
143 }
144
145 std::string SyncedNotification::ExtractTitle() const { 187 std::string SyncedNotification::ExtractTitle() const {
146 if (!specifics_.coalesced_notification().render_info().expanded_info(). 188 if (!specifics_.coalesced_notification().render_info().expanded_info().
147 simple_expanded_layout().has_title()) 189 simple_expanded_layout().has_title())
148 return ""; 190 return std::string();
149 191
150 return specifics_.coalesced_notification().render_info().expanded_info(). 192 return specifics_.coalesced_notification().render_info().expanded_info().
151 simple_expanded_layout().title(); 193 simple_expanded_layout().title();
152 } 194 }
153 195
154 std::string SyncedNotification::ExtractHeading() const { 196 std::string SyncedNotification::ExtractHeading() const {
155 if (!specifics_.coalesced_notification().render_info().collapsed_info(). 197 if (!specifics_.coalesced_notification().render_info().collapsed_info().
156 simple_collapsed_layout().has_heading()) 198 simple_collapsed_layout().has_heading())
157 return ""; 199 return std::string();
158 200
159 return specifics_.coalesced_notification().render_info().collapsed_info(). 201 return specifics_.coalesced_notification().render_info().collapsed_info().
160 simple_collapsed_layout().heading(); 202 simple_collapsed_layout().heading();
161 } 203 }
162 204
163 std::string SyncedNotification::ExtractDescription() const { 205 std::string SyncedNotification::ExtractDescription() const {
164 if (!specifics_.coalesced_notification().render_info().collapsed_info(). 206 if (!specifics_.coalesced_notification().render_info().collapsed_info().
165 simple_collapsed_layout().has_description()) 207 simple_collapsed_layout().has_description())
166 return ""; 208 return std::string();
167 209
168 return specifics_.coalesced_notification().render_info().collapsed_info(). 210 return specifics_.coalesced_notification().render_info().collapsed_info().
169 simple_collapsed_layout().description(); 211 simple_collapsed_layout().description();
170 } 212 }
171 213
172 std::string SyncedNotification::ExtractAppId() const { 214 std::string SyncedNotification::ExtractAppId() const {
173 if (!specifics_.coalesced_notification().has_app_id()) 215 if (!specifics_.coalesced_notification().has_app_id())
174 return ""; 216 return std::string();
175 return specifics_.coalesced_notification().app_id(); 217 return specifics_.coalesced_notification().app_id();
176 } 218 }
177 219
178 std::string SyncedNotification::ExtractKey() const { 220 std::string SyncedNotification::ExtractKey() const {
179 if (!specifics_.coalesced_notification().has_key()) 221 if (!specifics_.coalesced_notification().has_key())
180 return ""; 222 return std::string();
181 return specifics_.coalesced_notification().key(); 223 return specifics_.coalesced_notification().key();
182 } 224 }
183 225
184 GURL SyncedNotification::ExtractOriginUrl() const { 226 GURL SyncedNotification::ExtractOriginUrl() const {
185 std::string origin_url(kExtensionScheme); 227 std::string origin_url(kExtensionScheme);
186 origin_url += app_id(); 228 origin_url += app_id();
187 return GURL(origin_url); 229 return GURL(origin_url);
188 } 230 }
189 231
232 // TODO(petewil): This only returns the first icon. We should make all the
233 // icons available.
190 GURL SyncedNotification::ExtractAppIconUrl() const { 234 GURL SyncedNotification::ExtractAppIconUrl() const {
191 if (specifics_.coalesced_notification().render_info().expanded_info(). 235 if (specifics_.coalesced_notification().render_info().expanded_info().
192 collapsed_info_size() == 0) 236 collapsed_info_size() == 0)
193 return GURL(); 237 return GURL();
194 238
195 if (!specifics_.coalesced_notification().render_info().expanded_info(). 239 if (!specifics_.coalesced_notification().render_info().expanded_info().
196 collapsed_info(0).simple_collapsed_layout().has_app_icon()) 240 collapsed_info(0).simple_collapsed_layout().has_app_icon())
197 return GURL(); 241 return GURL();
198 242
199 return GURL(specifics_.coalesced_notification().render_info(). 243 return GURL(specifics_.coalesced_notification().render_info().
(...skipping 12 matching lines...) Expand all
212 simple_expanded_layout().media(0).image().has_url()) 256 simple_expanded_layout().media(0).image().has_url())
213 return GURL(); 257 return GURL();
214 258
215 return GURL(specifics_.coalesced_notification().render_info(). 259 return GURL(specifics_.coalesced_notification().render_info().
216 expanded_info().simple_expanded_layout().media(0).image().url()); 260 expanded_info().simple_expanded_layout().media(0).image().url());
217 } 261 }
218 262
219 std::string SyncedNotification::ExtractText() const { 263 std::string SyncedNotification::ExtractText() const {
220 if (!specifics_.coalesced_notification().render_info().expanded_info(). 264 if (!specifics_.coalesced_notification().render_info().expanded_info().
221 simple_expanded_layout().has_text()) 265 simple_expanded_layout().has_text())
222 return ""; 266 return std::string();
223 267
224 return specifics_.coalesced_notification().render_info().expanded_info(). 268 return specifics_.coalesced_notification().render_info().expanded_info().
225 simple_expanded_layout().text(); 269 simple_expanded_layout().text();
226 } 270 }
227 271
228 SyncedNotification::ReadState SyncedNotification::ExtractReadState() const { 272 SyncedNotification::ReadState SyncedNotification::ExtractReadState() const {
229 DCHECK(specifics_.coalesced_notification().has_read_state()); 273 DCHECK(specifics_.coalesced_notification().has_read_state());
230 274
231 sync_pb::CoalescedSyncedNotification_ReadState found_read_state = 275 sync_pb::CoalescedSyncedNotification_ReadState found_read_state =
232 specifics_.coalesced_notification().read_state(); 276 specifics_.coalesced_notification().read_state();
233 277
234 if (found_read_state == 278 if (found_read_state ==
235 sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED) { 279 sync_pb::CoalescedSyncedNotification_ReadState_DISMISSED) {
236 return kDismissed; 280 return kDismissed;
237 } else if (found_read_state == 281 } else if (found_read_state ==
238 sync_pb::CoalescedSyncedNotification_ReadState_UNREAD) { 282 sync_pb::CoalescedSyncedNotification_ReadState_UNREAD) {
239 return kUnread; 283 return kUnread;
240 } else if (found_read_state == 284 } else if (found_read_state ==
241 sync_pb::CoalescedSyncedNotification_ReadState_READ) { 285 sync_pb::CoalescedSyncedNotification_ReadState_READ) {
242 return kRead; 286 return kRead;
243 } else { 287 } else {
244 NOTREACHED(); 288 NOTREACHED();
245 return static_cast<SyncedNotification::ReadState>(found_read_state); 289 return static_cast<SyncedNotification::ReadState>(found_read_state);
246 } 290 }
247 } 291 }
248 292
249 std::string SyncedNotification::ExtractNotificationId() const { 293 // Time in milliseconds since the unix epoch, or 0.0 if not available.
250 return key(); 294 double SyncedNotification::ExtractCreationTime() const {
dcheng 2013/03/26 07:48:50 It'd probably be better to return a uint64 here fo
Pete Williamson 2013/03/26 17:18:30 Done.
295 uint64 creation_time_milliseconds;
296
297 if (!specifics_.coalesced_notification().has_creation_time_msec())
298 return 0.0;
299
300 creation_time_milliseconds = specifics_.coalesced_notification().
301 creation_time_msec();
302
303 return static_cast<double>(creation_time_milliseconds);
304 }
305
306 int SyncedNotification::ExtractPriority() const {
307 if (!specifics_.coalesced_notification().has_priority())
308 return 0;
309 return static_cast<int>(specifics_.coalesced_notification().
310 priority());
311 }
312
313 int SyncedNotification::ExtractNotificationCount() const {
314 return specifics_.coalesced_notification().render_info().
315 expanded_info().collapsed_info_size();
316 }
317
318 int SyncedNotification::ExtractButtonCount() const {
319 return specifics_.coalesced_notification().render_info().collapsed_info().
320 target_size();
321 }
322
323 std::string SyncedNotification::ExtractDefaultDestinationTitle() const {
324 if (!specifics_.coalesced_notification().render_info().collapsed_info().
325 default_destination().icon().has_alt_text()) {
326 return std::string();
327 }
328 return specifics_.coalesced_notification().render_info().collapsed_info().
329 default_destination().icon().alt_text();
330 }
331
332 std::string SyncedNotification::ExtractDefaultDestinationIconUrl() const {
333 if (!specifics_.coalesced_notification().render_info().collapsed_info().
334 default_destination().icon().has_url()) {
335 return std::string();
336 }
337 return specifics_.coalesced_notification().render_info().collapsed_info().
338 default_destination().icon().url();
339 }
340
341 std::string SyncedNotification::ExtractDefaultDestinationUrl() const {
342 if (!specifics_.coalesced_notification().render_info().collapsed_info().
343 default_destination().has_url()) {
344 return std::string();
345 }
346 return specifics_.coalesced_notification().render_info().collapsed_info().
347 default_destination().url();
348 }
349
350 std::string SyncedNotification::ExtractButtonOneTitle() const {
351 // Must ensure that we have a target before trying to access it.
352 if (ExtractButtonCount() < 1)
353 return std::string();
354 if (!specifics_.coalesced_notification().render_info().collapsed_info().
355 target(0).action().icon().has_alt_text()) {
356 return std::string();
357 }
358 return specifics_.coalesced_notification().render_info().collapsed_info().
359 target(0).action().icon().alt_text();
360 }
361
362 std::string SyncedNotification::ExtractButtonOneIconUrl() const {
363 // Must ensure that we have a target before trying to access it.
364 if (ExtractButtonCount() < 1)
365 return std::string();
366 if (!specifics_.coalesced_notification().render_info().collapsed_info().
367 target(0).action().icon().has_url()) {
368 return std::string();
369 }
370 return specifics_.coalesced_notification().render_info().collapsed_info().
371 target(0).action().icon().url();
372 }
373
374 std::string SyncedNotification::ExtractButtonOneUrl() const {
375 // Must ensure that we have a target before trying to access it.
376 if (ExtractButtonCount() < 1)
377 return std::string();
378 if (!specifics_.coalesced_notification().render_info().collapsed_info().
379 target(0).action().has_url()) {
380 return std::string();
381 }
382 return specifics_.coalesced_notification().render_info().collapsed_info().
383 target(0).action().url();
384 }
385
386 std::string SyncedNotification::ExtractButtonTwoTitle() const {
387 // Must ensure that we have a target before trying to access it.
388 if (ExtractButtonCount() < 2)
389 return std::string();
390 if (!specifics_.coalesced_notification().render_info().collapsed_info().
391 target(1).action().icon().has_alt_text()) {
392 return std::string();
393 }
394 return specifics_.coalesced_notification().render_info().collapsed_info().
395 target(1).action().icon().alt_text();
396 }
397
398 std::string SyncedNotification::ExtractButtonTwoIconUrl() const {
399 // Must ensure that we have a target before trying to access it.
400 if (ExtractButtonCount() < 2)
401 return std::string();
402 if (!specifics_.coalesced_notification().render_info().collapsed_info().
403 target(1).action().icon().has_url()) {
404 return std::string();
405 }
406 return specifics_.coalesced_notification().render_info().collapsed_info().
407 target(1).action().icon().url();
408 }
409
410 std::string SyncedNotification::ExtractButtonTwoUrl() const {
411 // Must ensure that we have a target before trying to access it.
412 if (ExtractButtonCount() < 2)
413 return std::string();
414 if (!specifics_.coalesced_notification().render_info().collapsed_info().
415 target(1).action().has_url()) {
416 return std::string();
417 }
418 return specifics_.coalesced_notification().render_info().collapsed_info().
419 target(1).action().url();
420 }
421
422 std::string SyncedNotification::ExtractContainedNotificationTitle(
423 int index) const {
424 if (specifics_.coalesced_notification().render_info().expanded_info().
425 collapsed_info_size() < index + 1)
426 return std::string();
427
428 return specifics_.coalesced_notification().render_info().expanded_info().
429 collapsed_info(index).simple_collapsed_layout().heading();
430 }
431
432 std::string SyncedNotification::ExtractContainedNotificationMessage(
433 int index) const {
434 if (specifics_.coalesced_notification().render_info().expanded_info().
435 collapsed_info_size() < index + 1)
436 return std::string();
437
438 return specifics_.coalesced_notification().render_info().expanded_info().
439 collapsed_info(index).simple_collapsed_layout().description();
251 } 440 }
252 441
253 } // namespace notifier 442 } // namespace notifier
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698