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 #ifndef CHROME_BROWSER_NOTIFICATIONS_SYNC_NOTIFIER_CHROME_NOTIFIER_SERVICE_H_ 6 #define CHROME_BROWSER_NOTIFICATIONS_SYNC_NOTIFIER_CHROME_NOTIFIER_SERVICE_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "base/memory/scoped_vector.h" 12 #include "base/threading/non_thread_safe.h" 13 #include "chrome/browser/notifications/sync_notifier/synced_notification.h" 14 #include "components/browser_context_keyed_service/browser_context_keyed_service.h" 15 #include "sync/api/syncable_service.h" 16 17 class NotificationUIManager; 18 class Profile; 19 20 namespace message_center { 21 struct Notifier; 22 } 23 24 namespace notifier { 25 26 // The ChromeNotifierService holds notifications which represent the state of 27 // delivered notifications for chrome. These are obtained from the sync service 28 // and kept up to date. 29 class ChromeNotifierService : public syncer::SyncableService, 30 public BrowserContextKeyedService { 31 public: 32 ChromeNotifierService(Profile* profile, NotificationUIManager* manager); 33 virtual ~ChromeNotifierService(); 34 35 // Methods from BrowserContextKeyedService. 36 virtual void Shutdown() OVERRIDE; 37 38 // syncer::SyncableService implementation. 39 virtual syncer::SyncMergeResult MergeDataAndStartSyncing( 40 syncer::ModelType type, 41 const syncer::SyncDataList& initial_sync_data, 42 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, 43 scoped_ptr<syncer::SyncErrorFactory> error_handler) OVERRIDE; 44 virtual void StopSyncing(syncer::ModelType type) OVERRIDE; 45 virtual syncer::SyncDataList GetAllSyncData( 46 syncer::ModelType type) const OVERRIDE; 47 virtual syncer::SyncError ProcessSyncChanges( 48 const tracked_objects::Location& from_here, 49 const syncer::SyncChangeList& change_list) OVERRIDE; 50 51 // Convert from internal representation to SyncData representation. 52 static syncer::SyncData CreateSyncDataFromNotification( 53 const SyncedNotification& notification); 54 55 // Convert from SyncData representation to internal representation. 56 static scoped_ptr<SyncedNotification> CreateNotificationFromSyncData( 57 const syncer::SyncData& sync_data); 58 59 // Get a pointer to a notification. ChromeNotifierService owns this pointer. 60 virtual notifier::SyncedNotification* FindNotificationById( 61 const std::string& notification_id); 62 63 // Get the list of synced notification services and fill their meta data to 64 // |notifiers|. 65 void GetSyncedNotificationServices( 66 std::vector<message_center::Notifier*>* notifiers); 67 68 // Called when we dismiss a notification. This is virtual so that test 69 // subclasses can override it. 70 virtual void MarkNotificationAsRead(const std::string& id); 71 72 // Called when a notier is enabled or disabled. 73 void OnSyncedNotificationServiceEnabled( 74 const std::string& notifier_id, 75 bool enabled); 76 77 Profile* profile() const { return profile_; } 78 79 // Functions for test. 80 void AddForTest(scoped_ptr<notifier::SyncedNotification> notification); 81 82 // If we allow the tests to do bitmap fetching, they will attempt to fetch 83 // a URL from the web, which will fail. We can already test the majority 84 // of what we want without also trying to fetch bitmaps. Other tests will 85 // cover bitmap fetching. 86 static void set_avoid_bitmap_fetching_for_test(bool avoid) { 87 avoid_bitmap_fetching_for_test_ = avoid; 88 } 89 90 private: 91 // Add a notification to our list. This takes ownership of the pointer. 92 void Add(scoped_ptr<notifier::SyncedNotification> notification); 93 94 // Display this notification in the notification center, or remove it. 95 void UpdateInMessageCenter(notifier::SyncedNotification* notification); 96 97 // Display a notification in the notification center (eventually). 98 void Display(notifier::SyncedNotification* notification); 99 100 // Remove a notification from our store. 101 void FreeNotificationById(const std::string& notification_id); 102 103 // Back pointer to the owning profile. 104 Profile* const profile_; 105 NotificationUIManager* const notification_manager_; 106 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; 107 std::vector<std::string> enabled_sending_services_; 108 static bool avoid_bitmap_fetching_for_test_; 109 110 // TODO(petewil): Consider whether a map would better suit our data. 111 // If there are many entries, lookup time may trump locality of reference. 112 ScopedVector<notifier::SyncedNotification> notification_data_; 113 114 friend class ChromeNotifierServiceTest; 115 116 DISALLOW_COPY_AND_ASSIGN(ChromeNotifierService); 117 }; 118 119 } // namespace notifier 120 121 #endif // CHROME_BROWSER_NOTIFICATIONS_SYNC_NOTIFIER_CHROME_NOTIFIER_SERVICE_H_ 122