| 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 #ifndef CHROME_BROWSER_EXTENSIONS_APP_NOTIFICATION_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_APP_NOTIFICATION_MANAGER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_APP_NOTIFICATION_MANAGER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_APP_NOTIFICATION_MANAGER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/sequenced_task_runner_helpers.h" | 15 #include "base/sequenced_task_runner_helpers.h" |
| 16 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| 17 #include "chrome/browser/extensions/app_notification.h" | 17 #include "chrome/browser/extensions/app_notification.h" |
| 18 #include "chrome/browser/extensions/app_notification_storage.h" | 18 #include "chrome/browser/extensions/app_notification_storage.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/notification_details.h" | 20 #include "content/public/browser/notification_details.h" |
| 21 #include "content/public/browser/notification_observer.h" | 21 #include "content/public/browser/notification_observer.h" |
| 22 #include "content/public/browser/notification_registrar.h" | 22 #include "content/public/browser/notification_registrar.h" |
| 23 #include "sync/api/sync_change.h" | 23 #include "sync/api/sync_change.h" |
| 24 #include "sync/api/syncable_service.h" | 24 #include "sync/api/syncable_service.h" |
| 25 | 25 |
| 26 class PerfTimer; | 26 class PerfTimer; |
| 27 class Profile; | 27 class Profile; |
| 28 |
| 29 namespace csync { |
| 28 class SyncErrorFactory; | 30 class SyncErrorFactory; |
| 31 } |
| 29 | 32 |
| 30 // This class keeps track of notifications for installed apps. | 33 // This class keeps track of notifications for installed apps. |
| 31 class AppNotificationManager | 34 class AppNotificationManager |
| 32 : public base::RefCountedThreadSafe< | 35 : public base::RefCountedThreadSafe< |
| 33 AppNotificationManager, | 36 AppNotificationManager, |
| 34 content::BrowserThread::DeleteOnUIThread>, | 37 content::BrowserThread::DeleteOnUIThread>, |
| 35 public content::NotificationObserver, | 38 public content::NotificationObserver, |
| 36 public SyncableService { | 39 public csync::SyncableService { |
| 37 public: | 40 public: |
| 38 static const unsigned int kMaxNotificationPerApp; | 41 static const unsigned int kMaxNotificationPerApp; |
| 39 explicit AppNotificationManager(Profile* profile); | 42 explicit AppNotificationManager(Profile* profile); |
| 40 | 43 |
| 41 // Starts up the process of reading from persistent storage. | 44 // Starts up the process of reading from persistent storage. |
| 42 void Init(); | 45 void Init(); |
| 43 | 46 |
| 44 // Returns whether add was succcessful. | 47 // Returns whether add was succcessful. |
| 45 // Takes ownership of |item| in all cases. | 48 // Takes ownership of |item| in all cases. |
| 46 bool Add(AppNotification* item); | 49 bool Add(AppNotification* item); |
| 47 | 50 |
| 48 const AppNotificationList* GetAll(const std::string& extension_id) const; | 51 const AppNotificationList* GetAll(const std::string& extension_id) const; |
| 49 | 52 |
| 50 // Returns the most recently added notification for |extension_id| if there | 53 // Returns the most recently added notification for |extension_id| if there |
| 51 // are any, or NULL otherwise. | 54 // are any, or NULL otherwise. |
| 52 const AppNotification* GetLast(const std::string& extension_id); | 55 const AppNotification* GetLast(const std::string& extension_id); |
| 53 | 56 |
| 54 // Clears all notifications for |extension_id|. | 57 // Clears all notifications for |extension_id|. |
| 55 void ClearAll(const std::string& extension_id); | 58 void ClearAll(const std::string& extension_id); |
| 56 | 59 |
| 57 // Implementing content::NotificationObserver interface. | 60 // Implementing content::NotificationObserver interface. |
| 58 virtual void Observe(int type, | 61 virtual void Observe(int type, |
| 59 const content::NotificationSource& source, | 62 const content::NotificationSource& source, |
| 60 const content::NotificationDetails& details) OVERRIDE; | 63 const content::NotificationDetails& details) OVERRIDE; |
| 61 | 64 |
| 62 bool loaded() const { return notifications_.get() != NULL; } | 65 bool loaded() const { return notifications_.get() != NULL; } |
| 63 | 66 |
| 64 // SyncableService implementation. | 67 // csync::SyncableService implementation. |
| 65 | 68 |
| 66 // Returns all syncable notifications from this model as SyncData. | 69 // Returns all syncable notifications from this model as csync::SyncData. |
| 67 virtual SyncDataList GetAllSyncData(syncable::ModelType type) const OVERRIDE; | 70 virtual csync::SyncDataList GetAllSyncData( |
| 71 syncable::ModelType type) const OVERRIDE; |
| 68 // Process notifications related changes from Sync, merging them into | 72 // Process notifications related changes from Sync, merging them into |
| 69 // our model. | 73 // our model. |
| 70 virtual SyncError ProcessSyncChanges( | 74 virtual csync::SyncError ProcessSyncChanges( |
| 71 const tracked_objects::Location& from_here, | 75 const tracked_objects::Location& from_here, |
| 72 const SyncChangeList& change_list) OVERRIDE; | 76 const csync::SyncChangeList& change_list) OVERRIDE; |
| 73 // Associate and merge sync data model with our data model. | 77 // Associate and merge sync data model with our data model. |
| 74 virtual SyncError MergeDataAndStartSyncing( | 78 virtual csync::SyncError MergeDataAndStartSyncing( |
| 75 syncable::ModelType type, | 79 syncable::ModelType type, |
| 76 const SyncDataList& initial_sync_data, | 80 const csync::SyncDataList& initial_sync_data, |
| 77 scoped_ptr<SyncChangeProcessor> sync_processor, | 81 scoped_ptr<csync::SyncChangeProcessor> sync_processor, |
| 78 scoped_ptr<SyncErrorFactory> sync_error_factory) OVERRIDE; | 82 scoped_ptr<csync::SyncErrorFactory> sync_error_factory) OVERRIDE; |
| 79 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; | 83 virtual void StopSyncing(syncable::ModelType type) OVERRIDE; |
| 80 | 84 |
| 81 private: | 85 private: |
| 82 friend class AppNotificationManagerSyncTest; | 86 friend class AppNotificationManagerSyncTest; |
| 83 friend class base::DeleteHelper<AppNotificationManager>; | 87 friend class base::DeleteHelper<AppNotificationManager>; |
| 84 friend struct content::BrowserThread::DeleteOnThread< | 88 friend struct content::BrowserThread::DeleteOnThread< |
| 85 content::BrowserThread::UI>; | 89 content::BrowserThread::UI>; |
| 86 FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest, | 90 FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest, |
| 87 NotificationToSyncDataToNotification); | 91 NotificationToSyncDataToNotification); |
| 88 FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest, | 92 FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 | 143 |
| 140 // Sends a change to syncer to add the given notification. | 144 // Sends a change to syncer to add the given notification. |
| 141 void SyncAddChange(const AppNotification& notif); | 145 void SyncAddChange(const AppNotification& notif); |
| 142 | 146 |
| 143 // Sends a change to syncer to remove the given notification. | 147 // Sends a change to syncer to remove the given notification. |
| 144 void SyncRemoveChange(const AppNotification& notif); | 148 void SyncRemoveChange(const AppNotification& notif); |
| 145 | 149 |
| 146 // Sends changes to syncer to remove all notifications in the given list. | 150 // Sends changes to syncer to remove all notifications in the given list. |
| 147 void SyncClearAllChange(const AppNotificationList& list); | 151 void SyncClearAllChange(const AppNotificationList& list); |
| 148 | 152 |
| 149 // Converters from AppNotification to SyncData and vice versa. | 153 // Converters from AppNotification to csync::SyncData and vice versa. |
| 150 static SyncData CreateSyncDataFromNotification( | 154 static csync::SyncData CreateSyncDataFromNotification( |
| 151 const AppNotification& notification); | 155 const AppNotification& notification); |
| 152 static AppNotification* CreateNotificationFromSyncData( | 156 static AppNotification* CreateNotificationFromSyncData( |
| 153 const SyncData& sync_data); | 157 const csync::SyncData& sync_data); |
| 154 | 158 |
| 155 Profile* profile_; | 159 Profile* profile_; |
| 156 content::NotificationRegistrar registrar_; | 160 content::NotificationRegistrar registrar_; |
| 157 scoped_ptr<NotificationMap> notifications_; | 161 scoped_ptr<NotificationMap> notifications_; |
| 158 | 162 |
| 159 // This should only be used on the FILE thread. | 163 // This should only be used on the FILE thread. |
| 160 scoped_ptr<AppNotificationStorage> storage_; | 164 scoped_ptr<AppNotificationStorage> storage_; |
| 161 | 165 |
| 162 // Sync change processor we use to push all our changes. | 166 // Sync change processor we use to push all our changes. |
| 163 scoped_ptr<SyncChangeProcessor> sync_processor_; | 167 scoped_ptr<csync::SyncChangeProcessor> sync_processor_; |
| 164 | 168 |
| 165 // Sync error handler that we use to create errors from. | 169 // Sync error handler that we use to create errors from. |
| 166 scoped_ptr<SyncErrorFactory> sync_error_factory_; | 170 scoped_ptr<csync::SyncErrorFactory> sync_error_factory_; |
| 167 | 171 |
| 168 // Whether the sync model is associated with the local model. | 172 // Whether the sync model is associated with the local model. |
| 169 // In other words, whether we are ready to apply sync changes. | 173 // In other words, whether we are ready to apply sync changes. |
| 170 bool models_associated_; | 174 bool models_associated_; |
| 171 // Whether syncer changes are being processed right now. | 175 // Whether syncer changes are being processed right now. |
| 172 bool processing_syncer_changes_; | 176 bool processing_syncer_changes_; |
| 173 | 177 |
| 174 // Used for a histogram of load time. | 178 // Used for a histogram of load time. |
| 175 scoped_ptr<PerfTimer> load_timer_; | 179 scoped_ptr<PerfTimer> load_timer_; |
| 176 | 180 |
| 177 DISALLOW_COPY_AND_ASSIGN(AppNotificationManager); | 181 DISALLOW_COPY_AND_ASSIGN(AppNotificationManager); |
| 178 }; | 182 }; |
| 179 | 183 |
| 180 #endif // CHROME_BROWSER_EXTENSIONS_APP_NOTIFICATION_MANAGER_H_ | 184 #endif // CHROME_BROWSER_EXTENSIONS_APP_NOTIFICATION_MANAGER_H_ |
| OLD | NEW |