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_time().ToInternalValue() < | |
99 a2.get()->creation_time().ToInternalValue(); | |
asargent_no_longer_on_chrome
2011/11/17 23:40:14
nit: Time has operator< and operator> methods, so
elvin
2011/11/18 00:10:18
Done.
| |
100 } | |
101 | |
96 bool AppNotificationManager::Add(AppNotification* item) { | 102 bool AppNotificationManager::Add(AppNotification* item) { |
97 // Do this first since we own the incoming item and hence want to delete | 103 // Do this first since we own the incoming item and hence want to delete |
98 // it in error paths. | 104 // it in error paths. |
99 linked_ptr<AppNotification> linked_item(item); | 105 linked_ptr<AppNotification> linked_item(item); |
100 if (!loaded()) | 106 if (!loaded()) |
101 return false; | 107 return false; |
102 const std::string& extension_id = item->extension_id(); | 108 const std::string& extension_id = item->extension_id(); |
103 AppNotificationList& list = GetAllInternal(extension_id); | 109 AppNotificationList& list = GetAllInternal(extension_id); |
104 list.push_back(linked_item); | 110 list.push_back(linked_item); |
105 | 111 |
106 SyncAddChange(*linked_item); | 112 SyncAddChange(*linked_item); |
107 | 113 |
114 if (list.size() > notification_manager_constants::kMaxNotificationPerApp) { | |
115 AppNotification* removed = | |
116 std::min_element(list.begin(), list.end(), | |
117 AppNotificationSortPredicate)->get(); | |
118 SyncRemoveChange(*removed); | |
119 list.erase(list.begin()); | |
120 } | |
121 | |
108 if (storage_.get()) { | 122 if (storage_.get()) { |
109 BrowserThread::PostTask( | 123 BrowserThread::PostTask( |
110 BrowserThread::FILE, | 124 BrowserThread::FILE, |
111 FROM_HERE, | 125 FROM_HERE, |
112 base::Bind(&AppNotificationManager::SaveOnFileThread, | 126 base::Bind(&AppNotificationManager::SaveOnFileThread, |
113 this, extension_id, CopyAppNotificationList(list))); | 127 this, extension_id, CopyAppNotificationList(list))); |
114 } | 128 } |
115 | 129 |
116 content::NotificationService::current()->Notify( | 130 content::NotificationService::current()->Notify( |
117 chrome::NOTIFICATION_APP_NOTIFICATION_STATE_CHANGED, | 131 chrome::NOTIFICATION_APP_NOTIFICATION_STATE_CHANGED, |
(...skipping 26 matching lines...) Expand all Loading... | |
144 const AppNotification* AppNotificationManager::GetLast( | 158 const AppNotification* AppNotificationManager::GetLast( |
145 const std::string& extension_id) { | 159 const std::string& extension_id) { |
146 if (!loaded()) | 160 if (!loaded()) |
147 return NULL; | 161 return NULL; |
148 NotificationMap::iterator found = notifications_->find(extension_id); | 162 NotificationMap::iterator found = notifications_->find(extension_id); |
149 if (found == notifications_->end()) | 163 if (found == notifications_->end()) |
150 return NULL; | 164 return NULL; |
151 const AppNotificationList& list = found->second; | 165 const AppNotificationList& list = found->second; |
152 if (list.empty()) | 166 if (list.empty()) |
153 return NULL; | 167 return NULL; |
154 return list.rbegin()->get(); | 168 return std::max_element(list.begin(), list.end(), |
169 AppNotificationSortPredicate)->get(); | |
asargent_no_longer_on_chrome
2011/11/17 23:40:14
I guess it's a little strange that GetLast returns
| |
155 } | 170 } |
156 | 171 |
157 void AppNotificationManager::ClearAll(const std::string& extension_id) { | 172 void AppNotificationManager::ClearAll(const std::string& extension_id) { |
158 if (!loaded()) | 173 if (!loaded()) |
159 return; | 174 return; |
160 NotificationMap::iterator found = notifications_->find(extension_id); | 175 NotificationMap::iterator found = notifications_->find(extension_id); |
161 if (found != notifications_->end()) { | 176 if (found != notifications_->end()) { |
162 SyncClearAllChange(found->second); | 177 SyncClearAllChange(found->second); |
163 notifications_->erase(found); | 178 notifications_->erase(found); |
164 } | 179 } |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
405 return; | 420 return; |
406 | 421 |
407 // TODO(munjal): crbug.com/10059. Work with Lingesh/Antony to resolve. | 422 // TODO(munjal): crbug.com/10059. Work with Lingesh/Antony to resolve. |
408 | 423 |
409 SyncChangeList changes; | 424 SyncChangeList changes; |
410 SyncData sync_data = CreateSyncDataFromNotification(notif); | 425 SyncData sync_data = CreateSyncDataFromNotification(notif); |
411 changes.push_back(SyncChange(SyncChange::ACTION_ADD, sync_data)); | 426 changes.push_back(SyncChange(SyncChange::ACTION_ADD, sync_data)); |
412 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | 427 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); |
413 } | 428 } |
414 | 429 |
430 void AppNotificationManager::SyncRemoveChange(const AppNotification& notif) { | |
431 // Skip if either: | |
432 // - Sync is not enabled by user. | |
433 // - Change is generated from within the manager. | |
434 if (notif.is_local() || !models_associated_) { | |
435 return; | |
436 } | |
437 | |
438 SyncChangeList changes; | |
439 SyncData sync_data = CreateSyncDataFromNotification(notif); | |
440 changes.push_back(SyncChange(SyncChange::ACTION_DELETE, sync_data)); | |
441 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); | |
442 } | |
443 | |
415 void AppNotificationManager::SyncClearAllChange( | 444 void AppNotificationManager::SyncClearAllChange( |
416 const AppNotificationList& list) { | 445 const AppNotificationList& list) { |
417 // Skip if either: | 446 // Skip if either: |
418 // - Sync is not enabled by user. | 447 // - Sync is not enabled by user. |
419 // - Change is generated from within the manager. | 448 // - Change is generated from within the manager. |
420 if (!models_associated_ || processing_syncer_changes_) | 449 if (!models_associated_ || processing_syncer_changes_) |
421 return; | 450 return; |
422 | 451 |
423 SyncChangeList changes; | 452 SyncChangeList changes; |
424 for (AppNotificationList::const_iterator iter = list.begin(); | 453 for (AppNotificationList::const_iterator iter = list.begin(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
462 } | 491 } |
463 | 492 |
464 // static | 493 // static |
465 SyncData AppNotificationManager::CreateSyncDataFromNotification( | 494 SyncData AppNotificationManager::CreateSyncDataFromNotification( |
466 const AppNotification& notification) { | 495 const AppNotification& notification) { |
467 DCHECK(!notification.is_local()); | 496 DCHECK(!notification.is_local()); |
468 sync_pb::EntitySpecifics specifics; | 497 sync_pb::EntitySpecifics specifics; |
469 sync_pb::AppNotificationSpecifics* notif_specifics = | 498 sync_pb::AppNotificationSpecifics* notif_specifics = |
470 specifics.MutableExtension(sync_pb::app_notification); | 499 specifics.MutableExtension(sync_pb::app_notification); |
471 notif_specifics->set_app_id(notification.extension_id()); | 500 notif_specifics->set_app_id(notification.extension_id()); |
501 notif_specifics->set_creation_timestamp_ms( | |
502 notification.creation_time().ToInternalValue()); | |
472 notif_specifics->set_body_text(notification.body()); | 503 notif_specifics->set_body_text(notification.body()); |
473 notif_specifics->set_guid(notification.guid()); | 504 notif_specifics->set_guid(notification.guid()); |
474 notif_specifics->set_link_text(notification.link_text()); | 505 notif_specifics->set_link_text(notification.link_text()); |
475 notif_specifics->set_link_url(notification.link_url().spec()); | 506 notif_specifics->set_link_url(notification.link_url().spec()); |
476 notif_specifics->set_title(notification.title()); | 507 notif_specifics->set_title(notification.title()); |
477 | |
478 return SyncData::CreateLocalData( | 508 return SyncData::CreateLocalData( |
479 notif_specifics->guid(), notif_specifics->app_id(), specifics); | 509 notif_specifics->guid(), notif_specifics->app_id(), specifics); |
480 } | 510 } |
481 | 511 |
482 // static | 512 // static |
483 AppNotification* AppNotificationManager::CreateNotificationFromSyncData( | 513 AppNotification* AppNotificationManager::CreateNotificationFromSyncData( |
484 const SyncData& sync_data) { | 514 const SyncData& sync_data) { |
485 sync_pb::AppNotificationSpecifics specifics = | 515 sync_pb::AppNotificationSpecifics specifics = |
486 sync_data.GetSpecifics().GetExtension(sync_pb::app_notification); | 516 sync_data.GetSpecifics().GetExtension(sync_pb::app_notification); |
487 | 517 |
488 // Check for mandatory fields. | 518 // Check for mandatory fields. |
489 if (!specifics.has_app_id() || !specifics.has_guid() || | 519 if (!specifics.has_app_id() || !specifics.has_guid() || |
490 !specifics.has_title() || !specifics.has_body_text()) { | 520 !specifics.has_title() || !specifics.has_body_text() || |
521 !specifics.has_creation_timestamp_ms()) { | |
491 return NULL; | 522 return NULL; |
492 } | 523 } |
493 | 524 |
494 AppNotification* notification = new AppNotification( | 525 AppNotification* notification = new AppNotification( |
495 false, specifics.guid(), specifics.app_id(), | 526 false, base::Time::FromInternalValue(specifics.creation_timestamp_ms()), |
527 specifics.guid(), specifics.app_id(), | |
496 specifics.title(), specifics.body_text()); | 528 specifics.title(), specifics.body_text()); |
497 if (specifics.has_link_text()) | 529 if (specifics.has_link_text()) |
498 notification->set_link_text(specifics.link_text()); | 530 notification->set_link_text(specifics.link_text()); |
499 if (specifics.has_link_url()) | 531 if (specifics.has_link_url()) |
500 notification->set_link_url(GURL(specifics.link_url())); | 532 notification->set_link_url(GURL(specifics.link_url())); |
501 return notification; | 533 return notification; |
502 } | 534 } |
OLD | NEW |