OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/extensions/app_notification_manager.h" | 5 #include "chrome/browser/extensions/app_notification_manager.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 | 86 |
87 void AppNotificationManager::Init() { | 87 void AppNotificationManager::Init() { |
88 FilePath storage_path = profile_->GetPath().AppendASCII("App Notifications"); | 88 FilePath storage_path = profile_->GetPath().AppendASCII("App Notifications"); |
89 BrowserThread::PostTask( | 89 BrowserThread::PostTask( |
90 BrowserThread::FILE, | 90 BrowserThread::FILE, |
91 FROM_HERE, | 91 FROM_HERE, |
92 base::Bind(&AppNotificationManager::LoadOnFileThread, | 92 base::Bind(&AppNotificationManager::LoadOnFileThread, |
93 this, storage_path)); | 93 this, storage_path)); |
94 } | 94 } |
95 | 95 |
96 bool AppNotificationSortPredicate(const linked_ptr<AppNotification> a1, | |
97 const linked_ptr<AppNotification> a2) { | |
98 return a1.get()->creation_timestamp_ms() < a2.get()->creation_timestamp_ms(); | |
99 } | |
100 | |
96 bool AppNotificationManager::Add(AppNotification* item) { | 101 bool AppNotificationManager::Add(AppNotification* item) { |
97 // Do this first since we own the incoming item and hence want to delete | 102 // Do this first since we own the incoming item and hence want to delete |
98 // it in error paths. | 103 // it in error paths. |
99 linked_ptr<AppNotification> linked_item(item); | 104 linked_ptr<AppNotification> linked_item(item); |
100 if (!loaded()) | 105 if (!loaded()) |
101 return false; | 106 return false; |
102 const std::string& extension_id = item->extension_id(); | 107 const std::string& extension_id = item->extension_id(); |
103 AppNotificationList& list = GetAllInternal(extension_id); | 108 AppNotificationList& list = GetAllInternal(extension_id); |
104 list.push_back(linked_item); | 109 list.push_back(linked_item); |
105 | 110 |
106 SyncAddChange(*linked_item); | 111 SyncAddChange(*linked_item); |
107 | 112 |
113 if (list.size() > notification_manager_constants::kMaxNotificationPerApp) { | |
114 AppNotification* removed = | |
115 std::min_element(list.begin(), list.end(), | |
116 AppNotificationSortPredicate)->get(); | |
117 SyncRemoveChange(*removed, true); | |
118 list.erase(list.begin()); | |
119 } | |
Munjal (Google)
2011/11/17 19:45:32
Actually, I am wondering if we should add in a sor
asargent_no_longer_on_chrome
2011/11/17 22:36:24
+1 - keeping them sorted is a good idea
| |
120 | |
108 if (storage_.get()) { | 121 if (storage_.get()) { |
109 BrowserThread::PostTask( | 122 BrowserThread::PostTask( |
110 BrowserThread::FILE, | 123 BrowserThread::FILE, |
111 FROM_HERE, | 124 FROM_HERE, |
112 base::Bind(&AppNotificationManager::SaveOnFileThread, | 125 base::Bind(&AppNotificationManager::SaveOnFileThread, |
113 this, extension_id, CopyAppNotificationList(list))); | 126 this, extension_id, CopyAppNotificationList(list))); |
114 } | 127 } |
115 | 128 |
116 content::NotificationService::current()->Notify( | 129 content::NotificationService::current()->Notify( |
117 chrome::NOTIFICATION_APP_NOTIFICATION_STATE_CHANGED, | 130 chrome::NOTIFICATION_APP_NOTIFICATION_STATE_CHANGED, |
(...skipping 26 matching lines...) Expand all Loading... | |
144 const AppNotification* AppNotificationManager::GetLast( | 157 const AppNotification* AppNotificationManager::GetLast( |
145 const std::string& extension_id) { | 158 const std::string& extension_id) { |
146 if (!loaded()) | 159 if (!loaded()) |
147 return NULL; | 160 return NULL; |
148 NotificationMap::iterator found = notifications_->find(extension_id); | 161 NotificationMap::iterator found = notifications_->find(extension_id); |
149 if (found == notifications_->end()) | 162 if (found == notifications_->end()) |
150 return NULL; | 163 return NULL; |
151 const AppNotificationList& list = found->second; | 164 const AppNotificationList& list = found->second; |
152 if (list.empty()) | 165 if (list.empty()) |
153 return NULL; | 166 return NULL; |
154 return list.rbegin()->get(); | 167 return std::max_element(list.begin(), list.end(), |
168 AppNotificationSortPredicate)->get(); | |
155 } | 169 } |
156 | 170 |
157 void AppNotificationManager::ClearAll(const std::string& extension_id) { | 171 void AppNotificationManager::ClearAll(const std::string& extension_id) { |
158 if (!loaded()) | 172 if (!loaded()) |
159 return; | 173 return; |
160 NotificationMap::iterator found = notifications_->find(extension_id); | 174 NotificationMap::iterator found = notifications_->find(extension_id); |
161 if (found != notifications_->end()) { | 175 if (found != notifications_->end()) { |
162 SyncClearAllChange(found->second); | 176 SyncClearAllChange(found->second); |
163 notifications_->erase(found); | 177 notifications_->erase(found); |
164 } | 178 } |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
405 return; | 419 return; |
406 | 420 |
407 // TODO(munjal): crbug.com/10059. Work with Lingesh/Antony to resolve. | 421 // TODO(munjal): crbug.com/10059. Work with Lingesh/Antony to resolve. |
408 | 422 |
409 SyncChangeList changes; | 423 SyncChangeList changes; |
410 SyncData sync_data = CreateSyncDataFromNotification(notif); | 424 SyncData sync_data = CreateSyncDataFromNotification(notif); |
411 changes.push_back(SyncChange(SyncChange::ACTION_ADD, sync_data)); | 425 changes.push_back(SyncChange(SyncChange::ACTION_ADD, sync_data)); |
412 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 426 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
413 } | 427 } |
414 | 428 |
429 void AppNotificationManager::SyncRemoveChange(const AppNotification& notif, | |
430 bool shouldProcessSyncerChanges) { | |
Munjal (Google)
2011/11/17 19:45:32
Actually seems like you call this with true for se
elvin
2011/11/17 22:28:17
Done.
| |
431 // Skip if either: | |
432 // - Sync is not enabled by user. | |
433 // - Change is generated from within the manager. | |
434 if (notif.is_local() || | |
435 ((!models_associated_ || processing_syncer_changes_) | |
436 && !shouldProcessSyncerChanges)) | |
Munjal (Google)
2011/11/17 19:45:32
Don't we want this to happen even when we are proc
elvin
2011/11/17 22:28:17
Done.
| |
437 return; | |
Munjal (Google)
2011/11/17 19:45:32
If your if is multiple lines you need braces even
elvin
2011/11/17 22:28:17
Done.
| |
438 | |
439 // TODO(munjal): crbug.com/10059. Work with Lingesh/Antony to resolve. | |
Munjal (Google)
2011/11/17 19:45:32
This TODO is not relevant here I think.
elvin
2011/11/17 22:28:17
Done.
| |
440 | |
441 SyncChangeList changes; | |
442 SyncData sync_data = CreateSyncDataFromNotification(notif); | |
443 changes.push_back(SyncChange(SyncChange::ACTION_DELETE, sync_data)); | |
444 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | |
445 } | |
446 | |
415 void AppNotificationManager::SyncClearAllChange( | 447 void AppNotificationManager::SyncClearAllChange( |
416 const AppNotificationList& list) { | 448 const AppNotificationList& list) { |
417 // Skip if either: | 449 // Skip if either: |
418 // - Sync is not enabled by user. | 450 // - Sync is not enabled by user. |
419 // - Change is generated from within the manager. | 451 // - Change is generated from within the manager. |
420 if (!models_associated_ || processing_syncer_changes_) | 452 if (!models_associated_ || processing_syncer_changes_) |
421 return; | 453 return; |
422 | 454 |
423 SyncChangeList changes; | 455 SyncChangeList changes; |
424 for (AppNotificationList::const_iterator iter = list.begin(); | 456 for (AppNotificationList::const_iterator iter = list.begin(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
462 } | 494 } |
463 | 495 |
464 // static | 496 // static |
465 SyncData AppNotificationManager::CreateSyncDataFromNotification( | 497 SyncData AppNotificationManager::CreateSyncDataFromNotification( |
466 const AppNotification& notification) { | 498 const AppNotification& notification) { |
467 DCHECK(!notification.is_local()); | 499 DCHECK(!notification.is_local()); |
468 sync_pb::EntitySpecifics specifics; | 500 sync_pb::EntitySpecifics specifics; |
469 sync_pb::AppNotificationSpecifics* notif_specifics = | 501 sync_pb::AppNotificationSpecifics* notif_specifics = |
470 specifics.MutableExtension(sync_pb::app_notification); | 502 specifics.MutableExtension(sync_pb::app_notification); |
471 notif_specifics->set_app_id(notification.extension_id()); | 503 notif_specifics->set_app_id(notification.extension_id()); |
504 notif_specifics->set_creation_timestamp_ms( | |
505 notification.creation_timestamp_ms()); | |
472 notif_specifics->set_body_text(notification.body()); | 506 notif_specifics->set_body_text(notification.body()); |
473 notif_specifics->set_guid(notification.guid()); | 507 notif_specifics->set_guid(notification.guid()); |
474 notif_specifics->set_link_text(notification.link_text()); | 508 notif_specifics->set_link_text(notification.link_text()); |
475 notif_specifics->set_link_url(notification.link_url().spec()); | 509 notif_specifics->set_link_url(notification.link_url().spec()); |
476 notif_specifics->set_title(notification.title()); | 510 notif_specifics->set_title(notification.title()); |
477 | |
478 return SyncData::CreateLocalData( | 511 return SyncData::CreateLocalData( |
479 notif_specifics->guid(), notif_specifics->app_id(), specifics); | 512 notif_specifics->guid(), notif_specifics->app_id(), specifics); |
480 } | 513 } |
481 | 514 |
482 // static | 515 // static |
483 AppNotification* AppNotificationManager::CreateNotificationFromSyncData( | 516 AppNotification* AppNotificationManager::CreateNotificationFromSyncData( |
484 const SyncData& sync_data) { | 517 const SyncData& sync_data) { |
485 sync_pb::AppNotificationSpecifics specifics = | 518 sync_pb::AppNotificationSpecifics specifics = |
486 sync_data.GetSpecifics().GetExtension(sync_pb::app_notification); | 519 sync_data.GetSpecifics().GetExtension(sync_pb::app_notification); |
487 | 520 |
488 // Check for mandatory fields. | 521 // Check for mandatory fields. |
489 if (!specifics.has_app_id() || !specifics.has_guid() || | 522 if (!specifics.has_app_id() || !specifics.has_guid() || |
490 !specifics.has_title() || !specifics.has_body_text()) { | 523 !specifics.has_title() || !specifics.has_body_text() || |
524 !specifics.has_creation_timestamp_ms()) { | |
491 return NULL; | 525 return NULL; |
492 } | 526 } |
493 | 527 |
494 AppNotification* notification = new AppNotification( | 528 AppNotification* notification = new AppNotification( |
495 false, specifics.guid(), specifics.app_id(), | 529 false, specifics.creation_timestamp_ms(), |
530 specifics.guid(), specifics.app_id(), | |
496 specifics.title(), specifics.body_text()); | 531 specifics.title(), specifics.body_text()); |
497 if (specifics.has_link_text()) | 532 if (specifics.has_link_text()) |
498 notification->set_link_text(specifics.link_text()); | 533 notification->set_link_text(specifics.link_text()); |
499 if (specifics.has_link_url()) | 534 if (specifics.has_link_url()) |
500 notification->set_link_url(GURL(specifics.link_url())); | 535 notification->set_link_url(GURL(specifics.link_url())); |
501 return notification; | 536 return notification; |
502 } | 537 } |
OLD | NEW |