OLD | NEW |
---|---|
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 // The ChromeNotifierService works together with sync to maintain the state of | 5 // The ChromeNotifierService works together with sync to maintain the state of |
6 // user notifications, which can then be presented in the notification center, | 6 // user notifications, which can then be presented in the notification center, |
7 // via the Notification UI Manager. | 7 // via the Notification UI Manager. |
8 | 8 |
9 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h" | 9 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h" |
10 | 10 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 // If local state changed, notify Notification UI Manager. | 120 // If local state changed, notify Notification UI Manager. |
121 } | 121 } |
122 // For any other conflict besides read state, treat it as an update. | 122 // For any other conflict besides read state, treat it as an update. |
123 } else { | 123 } else { |
124 // If different, just replace the local with the remote. | 124 // If different, just replace the local with the remote. |
125 // TODO(petewil): Someday we may allow changes from the client to | 125 // TODO(petewil): Someday we may allow changes from the client to |
126 // flow upwards, when we do, we will need better merge resolution. | 126 // flow upwards, when we do, we will need better merge resolution. |
127 found->Update(sync_data); | 127 found->Update(sync_data); |
128 | 128 |
129 // Tell the notification manager to update the notification. | 129 // Tell the notification manager to update the notification. |
130 Display(found); | 130 UpdateInMessageCenter(found); |
131 } | 131 } |
132 } | 132 } |
133 } | 133 } |
134 | 134 |
135 // Send up the changes that were made locally. | 135 // Send up the changes that were made locally. |
136 if (new_changes.size() > 0) { | 136 if (new_changes.size() > 0) { |
137 merge_result.set_error(sync_processor_->ProcessSyncChanges( | 137 merge_result.set_error(sync_processor_->ProcessSyncChanges( |
138 FROM_HERE, new_changes)); | 138 FROM_HERE, new_changes)); |
139 } | 139 } |
140 | 140 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, sync_data.GetDataType()); | 175 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, sync_data.GetDataType()); |
176 syncer::SyncChange::SyncChangeType change_type = it->change_type(); | 176 syncer::SyncChange::SyncChangeType change_type = it->change_type(); |
177 | 177 |
178 scoped_ptr<SyncedNotification> new_notification( | 178 scoped_ptr<SyncedNotification> new_notification( |
179 CreateNotificationFromSyncData(sync_data)); | 179 CreateNotificationFromSyncData(sync_data)); |
180 if (!new_notification.get()) { | 180 if (!new_notification.get()) { |
181 NOTREACHED() << "Failed to read notification."; | 181 NOTREACHED() << "Failed to read notification."; |
182 continue; | 182 continue; |
183 } | 183 } |
184 | 184 |
185 const std::string& key = new_notification->GetKey(); | |
186 DCHECK_GT(key.length(), 0U); | |
187 SyncedNotification* found = FindNotificationById(key); | |
188 | |
185 switch (change_type) { | 189 switch (change_type) { |
186 case syncer::SyncChange::ACTION_ADD: | 190 case syncer::SyncChange::ACTION_ADD: |
187 // TODO(petewil): Update the notification if it already exists | 191 if (found != NULL) { |
188 // as opposed to adding it. | 192 found->Update(sync_data); |
193 UpdateInMessageCenter(found); | |
194 break; | |
195 } | |
189 Add(new_notification.Pass()); | 196 Add(new_notification.Pass()); |
190 break; | 197 break; |
191 // TODO(petewil): Implement code to add delete and update actions. | 198 |
199 case syncer::SyncChange::ACTION_UPDATE: | |
200 if (found == NULL) { | |
201 Add(new_notification.Pass()); | |
202 break; | |
203 } | |
204 // Update it in our store. | |
205 found->Update(sync_data); | |
206 // Tell the notification manager to update the notification. | |
207 UpdateInMessageCenter(found); | |
208 break; | |
Nicolas Zea
2013/08/09 20:04:13
it seems to me this is the same as the code above,
Pete Williamson
2013/08/09 21:00:18
Done. I've heard before that it is worse to fall
| |
209 | |
210 case syncer::SyncChange::ACTION_DELETE: | |
211 if (found == NULL) { | |
212 break; | |
213 } | |
214 // Remove it from our store. | |
215 FreeNotificationById(key); | |
216 // Remove it from the message center. | |
217 UpdateInMessageCenter(new_notification.get()); | |
218 // TODO(petewil): Do I need to remember that it was deleted in case the | |
219 // add arrives after the delete? If so, how long do I need to remember? | |
220 break; | |
192 | 221 |
193 default: | 222 default: |
223 NOTREACHED(); | |
dewittj
2013/08/09 19:40:28
You don't handle ACTION_INVALID, yet you have a NO
Nicolas Zea
2013/08/09 20:04:13
The syncer will never return ACTION_INVALID in a v
Pete Williamson
2013/08/09 21:00:18
Left as is.
Pete Williamson
2013/08/09 21:00:18
Left as is per Zea@'s suggestion (below)
| |
194 break; | 224 break; |
195 } | 225 } |
196 } | 226 } |
197 | 227 |
198 return error; | 228 return error; |
199 } | 229 } |
200 | 230 |
201 // Support functions for data type conversion. | 231 // Support functions for data type conversion. |
202 | 232 |
203 // Static method. Get to the sync data in our internal format. | 233 // Static method. Get to the sync data in our internal format. |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 it != notification_data_.end(); | 308 it != notification_data_.end(); |
279 ++it) { | 309 ++it) { |
280 SyncedNotification* notification = *it; | 310 SyncedNotification* notification = *it; |
281 if (notification_id == notification->GetKey()) | 311 if (notification_id == notification->GetKey()) |
282 return *it; | 312 return *it; |
283 } | 313 } |
284 | 314 |
285 return NULL; | 315 return NULL; |
286 } | 316 } |
287 | 317 |
318 void ChromeNotifierService::FreeNotificationById( | |
319 const std::string& notification_id) { | |
320 for (std::vector<SyncedNotification*>::iterator it = | |
321 notification_data_.begin(); | |
dewittj
2013/08/09 19:40:28
This indentation is really weird here. Can I reco
Pete Williamson
2013/08/09 21:00:18
Done.
| |
322 it != notification_data_.end(); | |
323 ++it) { | |
324 SyncedNotification* notification = *it; | |
325 if (notification_id == notification->GetKey()) { | |
326 notification_data_.erase(it); | |
327 return; | |
328 } | |
329 } | |
330 } | |
331 | |
288 void ChromeNotifierService::GetSyncedNotificationServices( | 332 void ChromeNotifierService::GetSyncedNotificationServices( |
289 std::vector<message_center::Notifier*>* notifiers) { | 333 std::vector<message_center::Notifier*>* notifiers) { |
290 // TODO(mukai|petewil): Check the profile's eligibility before adding the | 334 // TODO(mukai|petewil): Check the profile's eligibility before adding the |
291 // sample app. | 335 // sample app. |
292 | 336 |
293 // TODO(petewil): Really obtain the list of synced notification sending | 337 // TODO(petewil): Really obtain the list of synced notification sending |
294 // services from the server and create the list of ids here. Until then, we | 338 // services from the server and create the list of ids here. Until then, we |
295 // are hardcoding the service names. Once that is done, remove this | 339 // are hardcoding the service names. Once that is done, remove this |
296 // hardcoding. | 340 // hardcoding. |
297 // crbug.com/248337 | 341 // crbug.com/248337 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
345 | 389 |
346 // If the user is not interested in this type of notification, ignore it. | 390 // If the user is not interested in this type of notification, ignore it. |
347 std::vector<std::string>::iterator iter = | 391 std::vector<std::string>::iterator iter = |
348 find(enabled_sending_services_.begin(), | 392 find(enabled_sending_services_.begin(), |
349 enabled_sending_services_.end(), | 393 enabled_sending_services_.end(), |
350 notification_copy->GetSendingServiceId()); | 394 notification_copy->GetSendingServiceId()); |
351 if (iter == enabled_sending_services_.end()) { | 395 if (iter == enabled_sending_services_.end()) { |
352 return; | 396 return; |
353 } | 397 } |
354 | 398 |
355 Display(notification_copy); | 399 UpdateInMessageCenter(notification_copy); |
356 } | 400 } |
357 | 401 |
358 void ChromeNotifierService::AddForTest( | 402 void ChromeNotifierService::AddForTest( |
359 scoped_ptr<notifier::SyncedNotification> notification) { | 403 scoped_ptr<notifier::SyncedNotification> notification) { |
360 notification_data_.push_back(notification.release()); | 404 notification_data_.push_back(notification.release()); |
361 } | 405 } |
362 | 406 |
363 void ChromeNotifierService::Display(SyncedNotification* notification) { | 407 void ChromeNotifierService::UpdateInMessageCenter( |
408 SyncedNotification* notification) { | |
364 // If the feature is disabled, exit now. | 409 // If the feature is disabled, exit now. |
365 if (!notifier::ChromeNotifierServiceFactory::UseSyncedNotifications( | 410 if (!notifier::ChromeNotifierServiceFactory::UseSyncedNotifications( |
366 CommandLine::ForCurrentProcess())) | 411 CommandLine::ForCurrentProcess())) |
367 return; | 412 return; |
368 | 413 |
369 notification->LogNotification(); | 414 notification->LogNotification(); |
370 | 415 |
416 if (notification->GetReadState() == SyncedNotification::kUnread) { | |
417 // If the message is unread, update it. | |
418 Display(notification); | |
419 } else { | |
420 // If the message is read or deleted, dismiss it from the center. | |
421 // We intentionally ignore errors if it is not in the center. | |
422 notification_manager_->CancelById(notification->GetKey()); | |
423 } | |
424 } | |
425 | |
426 void ChromeNotifierService::Display(SyncedNotification* notification) { | |
371 // Set up to fetch the bitmaps. | 427 // Set up to fetch the bitmaps. |
372 notification->QueueBitmapFetchJobs(notification_manager_, | 428 notification->QueueBitmapFetchJobs(notification_manager_, |
373 this, | 429 this, |
374 profile_); | 430 profile_); |
375 | 431 |
376 // Our tests cannot use the network for reliability reasons. | 432 // Our tests cannot use the network for reliability reasons. |
377 if (avoid_bitmap_fetching_for_test_) { | 433 if (avoid_bitmap_fetching_for_test_) { |
378 return; | 434 return; |
379 } | 435 } |
380 | 436 |
(...skipping 17 matching lines...) Expand all Loading... | |
398 // Remove the notifier_id if it is disabled and present. | 454 // Remove the notifier_id if it is disabled and present. |
399 } else if (iter != enabled_sending_services_.end() && !enabled) { | 455 } else if (iter != enabled_sending_services_.end() && !enabled) { |
400 enabled_sending_services_.erase(iter); | 456 enabled_sending_services_.erase(iter); |
401 } | 457 } |
402 | 458 |
403 // Otherwise, nothing to do, we can exit. | 459 // Otherwise, nothing to do, we can exit. |
404 return; | 460 return; |
405 } | 461 } |
406 | 462 |
407 } // namespace notifier | 463 } // namespace notifier |
OLD | NEW |