| OLD | NEW |
| (Empty) |
| 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_EXTENSIONS_APP_NOTIFICATION_MANAGER_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_APP_NOTIFICATION_MANAGER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 | |
| 10 #include "base/compiler_specific.h" | |
| 11 #include "base/gtest_prod_util.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/sequenced_task_runner_helpers.h" | |
| 15 #include "base/synchronization/waitable_event.h" | |
| 16 #include "chrome/browser/extensions/app_notification.h" | |
| 17 #include "chrome/browser/extensions/app_notification_storage.h" | |
| 18 #include "content/public/browser/browser_thread.h" | |
| 19 #include "content/public/browser/notification_details.h" | |
| 20 #include "content/public/browser/notification_observer.h" | |
| 21 #include "content/public/browser/notification_registrar.h" | |
| 22 #include "sync/api/sync_change.h" | |
| 23 #include "sync/api/syncable_service.h" | |
| 24 | |
| 25 class PerfTimer; | |
| 26 class Profile; | |
| 27 | |
| 28 namespace syncer { | |
| 29 class SyncErrorFactory; | |
| 30 } | |
| 31 | |
| 32 namespace extensions { | |
| 33 | |
| 34 // This class keeps track of notifications for installed apps. | |
| 35 class AppNotificationManager | |
| 36 : public base::RefCountedThreadSafe< | |
| 37 AppNotificationManager, | |
| 38 content::BrowserThread::DeleteOnUIThread>, | |
| 39 public content::NotificationObserver, | |
| 40 public syncer::SyncableService { | |
| 41 public: | |
| 42 static const unsigned int kMaxNotificationPerApp; | |
| 43 explicit AppNotificationManager(Profile* profile); | |
| 44 | |
| 45 // Starts up the process of reading from persistent storage. | |
| 46 void Init(); | |
| 47 | |
| 48 // Returns whether add was succcessful. | |
| 49 // Takes ownership of |item| in all cases. | |
| 50 bool Add(AppNotification* item); | |
| 51 | |
| 52 const AppNotificationList* GetAll(const std::string& extension_id) const; | |
| 53 | |
| 54 // Returns the most recently added notification for |extension_id| if there | |
| 55 // are any, or NULL otherwise. | |
| 56 const AppNotification* GetLast(const std::string& extension_id); | |
| 57 | |
| 58 // Clears all notifications for |extension_id|. | |
| 59 void ClearAll(const std::string& extension_id); | |
| 60 | |
| 61 // Implementing content::NotificationObserver interface. | |
| 62 virtual void Observe(int type, | |
| 63 const content::NotificationSource& source, | |
| 64 const content::NotificationDetails& details) OVERRIDE; | |
| 65 | |
| 66 bool loaded() const { return notifications_.get() != NULL; } | |
| 67 | |
| 68 // syncer::SyncableService implementation. | |
| 69 | |
| 70 // Returns all syncable notifications from this model as syncer::SyncData. | |
| 71 virtual syncer::SyncDataList GetAllSyncData( | |
| 72 syncer::ModelType type) const OVERRIDE; | |
| 73 // Process notifications related changes from Sync, merging them into | |
| 74 // our model. | |
| 75 virtual syncer::SyncError ProcessSyncChanges( | |
| 76 const tracked_objects::Location& from_here, | |
| 77 const syncer::SyncChangeList& change_list) OVERRIDE; | |
| 78 // Associate and merge sync data model with our data model. | |
| 79 virtual syncer::SyncMergeResult MergeDataAndStartSyncing( | |
| 80 syncer::ModelType type, | |
| 81 const syncer::SyncDataList& initial_sync_data, | |
| 82 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | |
| 83 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) OVERRIDE; | |
| 84 virtual void StopSyncing(syncer::ModelType type) OVERRIDE; | |
| 85 | |
| 86 private: | |
| 87 friend class AppNotificationManagerSyncTest; | |
| 88 friend class base::DeleteHelper<AppNotificationManager>; | |
| 89 friend struct content::BrowserThread::DeleteOnThread< | |
| 90 content::BrowserThread::UI>; | |
| 91 FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest, | |
| 92 NotificationToSyncDataToNotification); | |
| 93 FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest, | |
| 94 GetAllSyncDataNoLocal); | |
| 95 FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest, | |
| 96 GetAllSyncDataSomeLocal); | |
| 97 FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest, | |
| 98 ModelAssocModelEmpty); | |
| 99 FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest, | |
| 100 ModelAssocBothNonEmptyNoOverlap); | |
| 101 FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest, | |
| 102 ModelAssocBothNonEmptySomeOverlap); | |
| 103 FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest, | |
| 104 ModelAssocBothNonEmptyTitleMismatch); | |
| 105 FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest, | |
| 106 ModelAssocBothNonEmptyMatchesLocal); | |
| 107 FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest, | |
| 108 ProcessSyncChangesErrorModelAssocNotDone); | |
| 109 FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest, | |
| 110 ProcessSyncChangesEmptyModel); | |
| 111 FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest, | |
| 112 StopSyncing); | |
| 113 FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest, | |
| 114 ClearAllGetsSynced); | |
| 115 | |
| 116 // Maps extension id to a list of notifications for that extension. | |
| 117 typedef std::map<std::string, AppNotificationList> NotificationMap; | |
| 118 | |
| 119 virtual ~AppNotificationManager(); | |
| 120 | |
| 121 // Starts loading storage_ using |storage_path|. | |
| 122 void LoadOnFileThread(const base::FilePath& storage_path); | |
| 123 | |
| 124 // Called on the UI thread to handle the loaded results from storage_. | |
| 125 void HandleLoadResults(NotificationMap* map); | |
| 126 | |
| 127 // Saves the contents of |list| to storage. | |
| 128 // Ownership of |list| is transferred here. | |
| 129 void SaveOnFileThread(const std::string& extension_id, | |
| 130 AppNotificationList* list); | |
| 131 | |
| 132 void DeleteOnFileThread(const std::string& extension_id); | |
| 133 | |
| 134 // Gets notifications for a given extension id. | |
| 135 AppNotificationList& GetAllInternal(const std::string& extension_id); | |
| 136 | |
| 137 // Removes the notification with given extension id and given guid. | |
| 138 void Remove(const std::string& extension_id, const std::string& guid); | |
| 139 | |
| 140 // Returns the notification for the given |extension_id| and |guid|, | |
| 141 // NULL if no such notification exists. | |
| 142 const AppNotification* GetNotification(const std::string& extension_id, | |
| 143 const std::string& guid); | |
| 144 | |
| 145 // Sends a change to syncer to add the given notification. | |
| 146 void SyncAddChange(const AppNotification& notif); | |
| 147 | |
| 148 // Sends a change to syncer to remove the given notification. | |
| 149 void SyncRemoveChange(const AppNotification& notif); | |
| 150 | |
| 151 // Sends changes to syncer to remove all notifications in the given list. | |
| 152 void SyncClearAllChange(const AppNotificationList& list); | |
| 153 | |
| 154 // Converters from AppNotification to syncer::SyncData and vice versa. | |
| 155 static syncer::SyncData CreateSyncDataFromNotification( | |
| 156 const AppNotification& notification); | |
| 157 static AppNotification* CreateNotificationFromSyncData( | |
| 158 const syncer::SyncData& sync_data); | |
| 159 | |
| 160 Profile* profile_; | |
| 161 content::NotificationRegistrar registrar_; | |
| 162 scoped_ptr<NotificationMap> notifications_; | |
| 163 | |
| 164 // This should only be used on the FILE thread. | |
| 165 scoped_ptr<AppNotificationStorage> storage_; | |
| 166 | |
| 167 // Sync change processor we use to push all our changes. | |
| 168 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; | |
| 169 | |
| 170 // Sync error handler that we use to create errors from. | |
| 171 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_; | |
| 172 | |
| 173 // Whether the sync model is associated with the local model. | |
| 174 // In other words, whether we are ready to apply sync changes. | |
| 175 bool models_associated_; | |
| 176 // Whether syncer changes are being processed right now. | |
| 177 bool processing_syncer_changes_; | |
| 178 | |
| 179 // Used for a histogram of load time. | |
| 180 scoped_ptr<PerfTimer> load_timer_; | |
| 181 | |
| 182 DISALLOW_COPY_AND_ASSIGN(AppNotificationManager); | |
| 183 }; | |
| 184 | |
| 185 } // namespace extensions | |
| 186 | |
| 187 #endif // CHROME_BROWSER_EXTENSIONS_APP_NOTIFICATION_MANAGER_H_ | |
| OLD | NEW |