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

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: Created 7 years, 11 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 "sync/api/sync_change.h"
12 #include "sync/api/sync_change_processor.h"
13 #include "sync/protocol/sync.pb.h"
14 #include "sync/protocol/synced_notification_specifics.pb.h"
15
16 using namespace sync_pb;
dcheng 2013/01/07 20:42:10 It's much more common to just wrap it in namespace
Pete Williamson 2013/01/18 18:58:39 This now uses a different type of using statement,
17
18 ChromeNotifierService::ChromeNotifierService() {}
19 ChromeNotifierService::~ChromeNotifierService() {}
20
21 // Methods from ProfileKeyedService.
22 void ChromeNotifierService::Shutdown() {
23 }
24
25 // syncer::SyncableService implementation.
26
27 // This is called at startup to sync with the server.
28 // I expect this to always get called on the same thread, so no need
29 // to worry about re-entrancy.
Nicolas Zea 2013/01/05 01:18:48 Re-entrancy doesn't really relate to thread safety
Pete Williamson 2013/01/18 18:58:39 Done.
30 syncer::SyncMergeResult ChromeNotifierService::MergeDataAndStartSyncing(
31 syncer::ModelType type,
32 const syncer::SyncDataList& initial_sync_data,
33 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
34 scoped_ptr<syncer::SyncErrorFactory> error_handler) {
35 // TODO(petewil):After I add the infrastructure for the test, add a check
36 // that we are currently on the UI thread here.
37 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, type);
38 syncer::SyncMergeResult merge_result(syncer::SYNCED_NOTIFICATIONS);
39
40 // Mark all data we have now as local. New incoming data will clear
41 // the local flag, and merged data will clear the local flag. We use
42 // this to know what has to go back up to the server.
43 for (std::vector<SyncedNotification*>::iterator iter = our_data_.begin();
dcheng 2013/01/07 20:42:10 Nit: "it" is much more common than "iter".
Pete Williamson 2013/01/18 18:58:39 Done.
44 iter != our_data_.end();
45 ++iter) {
46 (*iter)->set_has_local_changes(true);
47 }
48
49 for (syncer::SyncDataList::const_iterator iter = initial_sync_data.begin();
50 iter != initial_sync_data.end(); ++iter) {
51 const syncer::SyncData& sync_data = *iter;
52 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, sync_data.GetDataType());
53
54 // Build a local notification object from the sync data.
55 scoped_ptr<SyncedNotification> incoming(CreateNotificationFromSyncData(
56 sync_data));
57 CHECK(incoming.get());
dcheng 2013/01/07 20:42:10 Do we really want to CHECK() here? Is it possible
Pete Williamson 2013/01/18 18:58:39 After I looked at the other code I originally copi
58
59 std::string external_id;
60 external_id = incoming->get_first_external_id();
dcheng 2013/01/07 20:42:10 const std::string& external_id =
Pete Williamson 2013/01/18 18:58:39 Done.
61 CHECK(external_id.length() > 0);
62
63 // Process each incoming remote notification.
64 // TODO(petewil): this should really get a unique id, not message text.
65 std::string title = incoming->title();
dcheng 2013/01/07 20:42:10 const std::string& title
Pete Williamson 2013/01/18 18:58:39 Done.
66 CHECK(title.length() > 0);
67 SyncedNotification* found = GetNotification(title);
68
69 if (NULL == found) {
70 // If there are no conflicts, copy in the data from remote.
71 Add(incoming.release());
72 } else {
73 // If the remote and local data conflict (the read or deleted flag),
74 // resolve the conflict.
75 // TODO(petewil): Implement.
76 }
77 }
78
79 // TODO(petewil): Implement the code that does the actual merging.
80
81 // Make a list of local changes to send up to the sync server.
82 syncer::SyncChangeList new_changes;
83 for (std::vector<SyncedNotification*>::iterator iter = our_data_.begin();
84 iter != our_data_.end();
85 ++iter) {
86 if ((*iter)->has_local_changes()) {
87 DCHECK(NULL != *iter);
dcheng 2013/01/07 20:42:10 Isn't it a little late to DCHECK() that *iter is n
Pete Williamson 2013/01/18 18:58:39 Done.
88 syncer::SyncData sync_data = CreateSyncDataFromNotification(**iter);
89 new_changes.push_back(
90 syncer::SyncChange(FROM_HERE,
91 syncer::SyncChange::ACTION_ADD,
92 sync_data));
93 }
94 }
95
96 // Send up the changes that were made locally.
97 if (new_changes.size() > 0) {
98 merge_result.set_error(
99 (sync_processor.Pass())->ProcessSyncChanges(FROM_HERE, new_changes));
dcheng 2013/01/07 20:42:10 I don't understand why you call Pass() here. Usual
Pete Williamson 2013/01/18 18:58:39 Oops, good catch, converted to get()
100 }
101
102 return merge_result;
103 }
104
105 void ChromeNotifierService::StopSyncing(syncer::ModelType type) {
106 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, type);
107 }
108
109 syncer::SyncDataList ChromeNotifierService::GetAllSyncData(
110 syncer::ModelType type) const {
111 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, type);
112 syncer::SyncDataList our_sync_data;
113
114 // Copy our native format data into a SyncDataList format.
115 for (std::vector<SyncedNotification*>::const_iterator iter =
116 our_data_.begin();
117 iter != our_data_.end();
118 ++iter) {
119 our_sync_data.push_back(CreateSyncDataFromNotification(**iter));
120 }
121
122 return our_sync_data;
123 }
124
125 // This method is called when there is an incoming sync change from the server.
126 syncer::SyncError ChromeNotifierService::ProcessSyncChanges(
127 const tracked_objects::Location& from_here,
128 const syncer::SyncChangeList& change_list) {
129 // TODO(petewil): add a check that we are called on the thread we expect.
130 syncer::SyncError error;
131
132 for (syncer::SyncChangeList::const_iterator iter = change_list.begin();
133 iter != change_list.end(); ++iter) {
134 syncer::SyncData sync_data = iter->sync_data();
135 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, sync_data.GetDataType());
136 syncer::SyncChange::SyncChangeType change_type = iter->change_type();
137
138 scoped_ptr<SyncedNotification> new_notification(
139 CreateNotificationFromSyncData(sync_data));
140 if (!new_notification.get()) {
141 NOTREACHED() << "Failed to read notification.";
142 continue;
143 }
144
145 switch (change_type) {
146 case syncer::SyncChange::ACTION_ADD:
147 // TODO(petewil): Don't add the notification if it already exists.
148 Add(new_notification.release());
149 break;
150 // TODO(petewil): Implement code to add delete and update actions.
151 // case syncer::SyncChange::ACTION_DELETE:
152
153 default:
154 break;
155 }
156 }
157
158 return error;
159 }
160
161 // Support functions for data type conversion.
162
163 // Static method. Get to the sync data in our internal format.
164 syncer::SyncData ChromeNotifierService::CreateSyncDataFromNotification(
165 SyncedNotification& notification) {
166 return *notification.sync_data();
167 }
168
169 // Static Method. Convert from SyncData to our internal format.
170 SyncedNotification* ChromeNotifierService::CreateNotificationFromSyncData(
171 const syncer::SyncData& sync_data) {
172
173 // Get a pointer to our data within the sync_data object.
174 sync_pb::SyncedNotificationSpecifics specifics =
175 sync_data.GetSpecifics().synced_notification();
176
177 // Check for mandatory fields in the sync_data object
178 // TODO(petewil): Consider adding check for a title in one of the
179 // title and subtext or title and image objects.
180 if (!specifics.has_coalesced_notification() ||
181 !specifics.coalesced_notification().has_id() ||
182 !specifics.coalesced_notification().id().has_app_id() ||
183 specifics.coalesced_notification().id().app_id().empty() ||
184 !specifics.coalesced_notification().has_render_info() ||
185 !specifics.coalesced_notification().has_creation_time_msec()) {
186 return NULL;
187 }
188
189 // Create a new notification object based on the supplied sync_data.
190 SyncedNotification* notification = new SyncedNotification(sync_data);
191
192 return notification;
193 }
194
195 // This returns a pointer into a vector that we own. Caller must not free it.
196 // This returns NULL if no match is found.
197 // TODO(petewil): Use the <app_id/coalescing_key> pair instead of title to
198 // check for uniqueness.
199 SyncedNotification* ChromeNotifierService::GetNotification(
dcheng 2013/01/07 20:42:10 Perhaps FindNotificationWithTitle() would be a mor
Pete Williamson 2013/01/18 18:58:39 Renamed to FindNotifictionById()
200 const std::string& value) {
dcheng 2013/01/07 20:42:10 "value" is not a meaningful name. I would call ren
Pete Williamson 2013/01/18 18:58:39 Renamed to "id" since we now find by ID instead of
201
202 // TODO(petewil): Convert this to a map if the expected number
203 // of notifications on the device is high enough to justify the worsened
204 // locality of reference.
205 for (std::vector<SyncedNotification*>::const_iterator iter =
206 our_data_.begin();
207 iter != our_data_.end();
208 ++iter) {
209 SyncedNotification* notification = *iter;
210 if (value == notification->title())
211 return *iter;
212 }
213
214 return NULL;
215 }
216
217 // Add a new notification to our data structure. This takes ownership
218 // of the passed in pointer.
219 bool ChromeNotifierService::Add(SyncedNotification* notification) {
dcheng 2013/01/07 20:42:10 scoped_ptr<SyncedNotification>
Pete Williamson 2013/01/18 18:58:39 Done.
220
221 our_data_.push_back(notification);
222
223 // TODO(petewil): Implement code to send the notification.
224
225 return true;
226 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698