Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: chrome/browser/notifier/chrome_notifier_service.cc

Issue 11745024: Synced Notification Sync Change Processor (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Lint fixes Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/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/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(), static_cast<size_t>(0));
dcheng 2013/01/28 23:35:09 FYI: 0U probably works.
Pete Williamson 2013/01/29 00:11:43 Done.
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));
dcheng 2013/01/28 23:35:09 Nit: remove extra parens.
Pete Williamson 2013/01/29 00:11:43 Done.
107 }
108
109 return merge_result;
110 }
111
112 void ChromeNotifierService::StopSyncing(syncer::ModelType type) {
113 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, type);
dcheng 2013/01/28 23:35:09 Would be nice to have a TODO or a comment stating
Pete Williamson 2013/01/29 00:11:43 Done.
114 }
115
116 syncer::SyncDataList ChromeNotifierService::GetAllSyncData(
117 syncer::ModelType type) const {
118 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, type);
119 syncer::SyncDataList sync_data;
120
121 // Copy our native format data into a SyncDataList format.
122 for (std::vector<SyncedNotification*>::const_iterator it =
123 notification_data_.begin();
124 it != notification_data_.end();
125 ++it) {
126 sync_data.push_back(CreateSyncDataFromNotification(**it));
127 }
128
129 return sync_data;
130 }
131
132 // This method is called when there is an incoming sync change from the server.
133 syncer::SyncError ChromeNotifierService::ProcessSyncChanges(
134 const tracked_objects::Location& from_here,
135 const syncer::SyncChangeList& change_list) {
136 // TODO(petewil): add a check that we are called on the thread we expect.
137 syncer::SyncError error;
138
139 for (syncer::SyncChangeList::const_iterator it = change_list.begin();
140 it != change_list.end(); ++it) {
141 syncer::SyncData sync_data = it->sync_data();
142 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, sync_data.GetDataType());
143 syncer::SyncChange::SyncChangeType change_type = it->change_type();
144
145 scoped_ptr<SyncedNotification> new_notification(
146 CreateNotificationFromSyncData(sync_data));
147 if (!new_notification.get()) {
148 NOTREACHED() << "Failed to read notification.";
149 continue;
150 }
151
152 switch (change_type) {
153 case syncer::SyncChange::ACTION_ADD:
154 // TODO(petewil): Update the notification if it already exists
155 // as opposed to adding it.
156 Add(new_notification.Pass());
157 break;
158 // TODO(petewil): Implement code to add delete and update actions.
159
160 default:
161 break;
162 }
163 }
164
165 return error;
166 }
167
168 // Support functions for data type conversion.
169
170 // Static method. Get to the sync data in our internal format.
171 syncer::SyncData ChromeNotifierService::CreateSyncDataFromNotification(
172 const SyncedNotification& notification) {
173 return *notification.sync_data();
174 }
175
176 // Static Method. Convert from SyncData to our internal format.
177 scoped_ptr<SyncedNotification>
178 ChromeNotifierService::CreateNotificationFromSyncData(
179 const syncer::SyncData& sync_data) {
180 // Get a pointer to our data within the sync_data object.
181 sync_pb::SyncedNotificationSpecifics specifics =
182 sync_data.GetSpecifics().synced_notification();
183
184 // Check for mandatory fields in the sync_data object.
185 if (!specifics.has_coalesced_notification() ||
186 !specifics.coalesced_notification().has_id() ||
187 !specifics.coalesced_notification().id().has_app_id() ||
188 specifics.coalesced_notification().id().app_id().empty() ||
189 !specifics.coalesced_notification().has_render_info() ||
190 !specifics.coalesced_notification().has_creation_time_msec()) {
191 return NULL;
192 }
193
194 // Create a new notification object based on the supplied sync_data.
195 scoped_ptr<SyncedNotification> notification(
196 new SyncedNotification(sync_data));
197
198 return notification.Pass();
199 }
200
201 // This returns a pointer into a vector that we own. Caller must not free it.
202 // Returns NULL if no match is found.
203 // This uses the <app_id/coalescing_key> pair as a key.
204 SyncedNotification* ChromeNotifierService::FindNotificationById(
205 const std::string& id) {
206 // TODO(petewil): We can make a performance trade off here.
207 // While the vector has good locality of reference, a map has faster lookup.
208 // Based on how bit we expect this to get, maybe change this to a map.
209 for (std::vector<SyncedNotification*>::const_iterator it =
210 notification_data_.begin();
211 it != notification_data_.end();
212 ++it) {
213 SyncedNotification* notification = *it;
214 if (id == notification->notification_id())
215 return *it;
216 }
217
218 return NULL;
219 }
220
221 // Add a new notification to our data structure. This takes ownership
222 // of the passed in pointer.
223 void ChromeNotifierService::Add(scoped_ptr<SyncedNotification> notification) {
224 SyncedNotification* notification_copy = notification.get();
225 // Take ownership of the object and put it into our local storage.
226 notification_data_.push_back(notification.release());
227
228 // Show it to the user.
229 Show(notification_copy);
230 }
231
232 // Send the notification to the NotificationUIManager to show to the user.
233 void ChromeNotifierService::Show(SyncedNotification* notification) {
234 // Set up the fields we need to send and create a Notification object.
235 GURL origin_url(notification->origin_url());
236 GURL icon_url(notification->icon_url());
237 string16 title = UTF8ToUTF16(notification->title());
238 string16 body = UTF8ToUTF16(notification->body());
239 // TODO(petewil): What goes in the display source, is empty OK?
240 string16 display_source;
241 string16 replace_id = UTF8ToUTF16(notification->notification_id());
242 // The delegate will eventually catch calls that the notification
243 // was read or deleted, and send the changes back to the server.
244 scoped_refptr<NotificationDelegate> delegate =
245 new ChromeNotifierDelegate(notification->notification_id());
246
247 Notification ui_notification(origin_url, icon_url, title, body,
248 WebKit::WebTextDirectionDefault,
249 display_source, replace_id, delegate);
250
251 notification_manager_->Add(ui_notification, profile_);
252
253 DVLOG(1) << "Synced Notification arrived! " << title << " " << body
254 << " " << icon_url << " " << replace_id;
255
256 return;
257 }
258
259 } // namespace notifier
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698