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 // The ChromeNotifierService works together with sync to maintain the state of |
| 6 // user notifications, which can then be presented in the notification center, |
| 7 // via the Notification UI Manager. |
| 8 |
| 9 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h" |
| 10 |
| 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/notifications/notification.h" |
| 14 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 15 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_delegate.h" |
| 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "googleurl/src/gurl.h" |
| 18 #include "sync/api/sync_change.h" |
| 19 #include "sync/api/sync_change_processor.h" |
| 20 #include "sync/api/sync_error_factory.h" |
| 21 #include "sync/protocol/sync.pb.h" |
| 22 #include "sync/protocol/synced_notification_specifics.pb.h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextDirection.h" |
| 24 |
| 25 namespace notifier { |
| 26 |
| 27 ChromeNotifierService::ChromeNotifierService(Profile* profile, |
| 28 NotificationUIManager* manager) |
| 29 : profile_(profile), notification_manager_(manager) {} |
| 30 ChromeNotifierService::~ChromeNotifierService() {} |
| 31 |
| 32 // Methods from ProfileKeyedService. |
| 33 void ChromeNotifierService::Shutdown() { |
| 34 } |
| 35 |
| 36 // syncer::SyncableService implementation. |
| 37 |
| 38 // This is called at startup to sync with the server. |
| 39 // This code is not thread safe. |
| 40 syncer::SyncMergeResult ChromeNotifierService::MergeDataAndStartSyncing( |
| 41 syncer::ModelType type, |
| 42 const syncer::SyncDataList& initial_sync_data, |
| 43 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
| 44 scoped_ptr<syncer::SyncErrorFactory> error_handler) { |
| 45 // TODO(petewil): After I add the infrastructure for the test, add a check |
| 46 // that we are currently on the UI thread here. |
| 47 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, type); |
| 48 syncer::SyncMergeResult merge_result(syncer::SYNCED_NOTIFICATIONS); |
| 49 |
| 50 // Mark all data we have now as local. New incoming data will clear |
| 51 // the local flag, and merged data will clear the local flag. We use |
| 52 // this to know what has to go back up to the server. |
| 53 for (std::vector<SyncedNotification*>::iterator it = |
| 54 notification_data_.begin(); |
| 55 it != notification_data_.end(); |
| 56 ++it) { |
| 57 (*it)->set_has_local_changes(true); |
| 58 } |
| 59 |
| 60 for (syncer::SyncDataList::const_iterator it = initial_sync_data.begin(); |
| 61 it != initial_sync_data.end(); ++it) { |
| 62 const syncer::SyncData& sync_data = *it; |
| 63 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, sync_data.GetDataType()); |
| 64 |
| 65 // Build a local notification object from the sync data. |
| 66 scoped_ptr<SyncedNotification> incoming(CreateNotificationFromSyncData( |
| 67 sync_data)); |
| 68 DCHECK(incoming.get()); |
| 69 |
| 70 // Process each incoming remote notification. |
| 71 const std::string& id = incoming->notification_id(); |
| 72 DCHECK_GT(id.length(), 0U); |
| 73 SyncedNotification* found = FindNotificationById(id); |
| 74 |
| 75 if (NULL == found) { |
| 76 // If there are no conflicts, copy in the data from remote. |
| 77 Add(incoming.Pass()); |
| 78 } else { |
| 79 // If the remote and local data conflict (the read or deleted flag), |
| 80 // resolve the conflict. |
| 81 // TODO(petewil): Implement. |
| 82 } |
| 83 } |
| 84 |
| 85 // TODO(petewil): Implement the code that does the actual merging. |
| 86 // See chrome/browser/history/history.cc for an example to follow. |
| 87 |
| 88 // Make a list of local changes to send up to the sync server. |
| 89 syncer::SyncChangeList new_changes; |
| 90 for (std::vector<SyncedNotification*>::iterator it = |
| 91 notification_data_.begin(); |
| 92 it != notification_data_.end(); |
| 93 ++it) { |
| 94 if ((*it)->has_local_changes()) { |
| 95 syncer::SyncData sync_data = CreateSyncDataFromNotification(**it); |
| 96 new_changes.push_back( |
| 97 syncer::SyncChange(FROM_HERE, |
| 98 syncer::SyncChange::ACTION_ADD, |
| 99 sync_data)); |
| 100 } |
| 101 } |
| 102 |
| 103 // Send up the changes that were made locally. |
| 104 if (new_changes.size() > 0) { |
| 105 merge_result.set_error( |
| 106 sync_processor.get()->ProcessSyncChanges(FROM_HERE, new_changes)); |
| 107 } |
| 108 |
| 109 return merge_result; |
| 110 } |
| 111 |
| 112 void ChromeNotifierService::StopSyncing(syncer::ModelType type) { |
| 113 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, type); |
| 114 // TODO(petewil): implement |
| 115 } |
| 116 |
| 117 syncer::SyncDataList ChromeNotifierService::GetAllSyncData( |
| 118 syncer::ModelType type) const { |
| 119 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, type); |
| 120 syncer::SyncDataList sync_data; |
| 121 |
| 122 // Copy our native format data into a SyncDataList format. |
| 123 for (std::vector<SyncedNotification*>::const_iterator it = |
| 124 notification_data_.begin(); |
| 125 it != notification_data_.end(); |
| 126 ++it) { |
| 127 sync_data.push_back(CreateSyncDataFromNotification(**it)); |
| 128 } |
| 129 |
| 130 return sync_data; |
| 131 } |
| 132 |
| 133 // This method is called when there is an incoming sync change from the server. |
| 134 syncer::SyncError ChromeNotifierService::ProcessSyncChanges( |
| 135 const tracked_objects::Location& from_here, |
| 136 const syncer::SyncChangeList& change_list) { |
| 137 // TODO(petewil): add a check that we are called on the thread we expect. |
| 138 syncer::SyncError error; |
| 139 |
| 140 for (syncer::SyncChangeList::const_iterator it = change_list.begin(); |
| 141 it != change_list.end(); ++it) { |
| 142 syncer::SyncData sync_data = it->sync_data(); |
| 143 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, sync_data.GetDataType()); |
| 144 syncer::SyncChange::SyncChangeType change_type = it->change_type(); |
| 145 |
| 146 scoped_ptr<SyncedNotification> new_notification( |
| 147 CreateNotificationFromSyncData(sync_data)); |
| 148 if (!new_notification.get()) { |
| 149 NOTREACHED() << "Failed to read notification."; |
| 150 continue; |
| 151 } |
| 152 |
| 153 switch (change_type) { |
| 154 case syncer::SyncChange::ACTION_ADD: |
| 155 // TODO(petewil): Update the notification if it already exists |
| 156 // as opposed to adding it. |
| 157 Add(new_notification.Pass()); |
| 158 break; |
| 159 // TODO(petewil): Implement code to add delete and update actions. |
| 160 |
| 161 default: |
| 162 break; |
| 163 } |
| 164 } |
| 165 |
| 166 return error; |
| 167 } |
| 168 |
| 169 // Support functions for data type conversion. |
| 170 |
| 171 // Static method. Get to the sync data in our internal format. |
| 172 syncer::SyncData ChromeNotifierService::CreateSyncDataFromNotification( |
| 173 const SyncedNotification& notification) { |
| 174 return *notification.sync_data(); |
| 175 } |
| 176 |
| 177 // Static Method. Convert from SyncData to our internal format. |
| 178 scoped_ptr<SyncedNotification> |
| 179 ChromeNotifierService::CreateNotificationFromSyncData( |
| 180 const syncer::SyncData& sync_data) { |
| 181 // Get a pointer to our data within the sync_data object. |
| 182 sync_pb::SyncedNotificationSpecifics specifics = |
| 183 sync_data.GetSpecifics().synced_notification(); |
| 184 |
| 185 // Check for mandatory fields in the sync_data object. |
| 186 if (!specifics.has_coalesced_notification() || |
| 187 !specifics.coalesced_notification().has_id() || |
| 188 !specifics.coalesced_notification().id().has_app_id() || |
| 189 specifics.coalesced_notification().id().app_id().empty() || |
| 190 !specifics.coalesced_notification().has_render_info() || |
| 191 !specifics.coalesced_notification().has_creation_time_msec()) { |
| 192 return scoped_ptr<SyncedNotification>(); |
| 193 } |
| 194 |
| 195 // Create a new notification object based on the supplied sync_data. |
| 196 scoped_ptr<SyncedNotification> notification( |
| 197 new SyncedNotification(sync_data)); |
| 198 |
| 199 return notification.Pass(); |
| 200 } |
| 201 |
| 202 // This returns a pointer into a vector that we own. Caller must not free it. |
| 203 // Returns NULL if no match is found. |
| 204 // This uses the <app_id/coalescing_key> pair as a key. |
| 205 SyncedNotification* ChromeNotifierService::FindNotificationById( |
| 206 const std::string& id) { |
| 207 // TODO(petewil): We can make a performance trade off here. |
| 208 // While the vector has good locality of reference, a map has faster lookup. |
| 209 // Based on how bit we expect this to get, maybe change this to a map. |
| 210 for (std::vector<SyncedNotification*>::const_iterator it = |
| 211 notification_data_.begin(); |
| 212 it != notification_data_.end(); |
| 213 ++it) { |
| 214 SyncedNotification* notification = *it; |
| 215 if (id == notification->notification_id()) |
| 216 return *it; |
| 217 } |
| 218 |
| 219 return NULL; |
| 220 } |
| 221 |
| 222 // Add a new notification to our data structure. This takes ownership |
| 223 // of the passed in pointer. |
| 224 void ChromeNotifierService::Add(scoped_ptr<SyncedNotification> notification) { |
| 225 SyncedNotification* notification_copy = notification.get(); |
| 226 // Take ownership of the object and put it into our local storage. |
| 227 notification_data_.push_back(notification.release()); |
| 228 |
| 229 // Show it to the user. |
| 230 Show(notification_copy); |
| 231 } |
| 232 |
| 233 // Send the notification to the NotificationUIManager to show to the user. |
| 234 void ChromeNotifierService::Show(SyncedNotification* notification) { |
| 235 // Set up the fields we need to send and create a Notification object. |
| 236 GURL origin_url(notification->origin_url()); |
| 237 GURL icon_url(notification->icon_url()); |
| 238 string16 title = UTF8ToUTF16(notification->title()); |
| 239 string16 body = UTF8ToUTF16(notification->body()); |
| 240 // TODO(petewil): What goes in the display source, is empty OK? |
| 241 string16 display_source; |
| 242 string16 replace_id = UTF8ToUTF16(notification->notification_id()); |
| 243 // The delegate will eventually catch calls that the notification |
| 244 // was read or deleted, and send the changes back to the server. |
| 245 scoped_refptr<NotificationDelegate> delegate = |
| 246 new ChromeNotifierDelegate(notification->notification_id()); |
| 247 |
| 248 Notification ui_notification(origin_url, icon_url, title, body, |
| 249 WebKit::WebTextDirectionDefault, |
| 250 display_source, replace_id, delegate); |
| 251 |
| 252 notification_manager_->Add(ui_notification, profile_); |
| 253 |
| 254 DVLOG(1) << "Synced Notification arrived! " << title << " " << body |
| 255 << " " << icon_url << " " << replace_id; |
| 256 |
| 257 return; |
| 258 } |
| 259 |
| 260 } // namespace notifier |
OLD | NEW |