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/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 using sync_pb::SyncedNotification; | |
26 | |
27 | |
28 ChromeNotifierService::ChromeNotifierService(Profile* profile, | |
29 NotificationUIManager* manager) : | |
dcheng
2013/01/18 21:56:13
Incorrect formatting. : should be on the next line
Pete Williamson
2013/01/23 01:45:55
Done.
| |
30 profile_(profile), notification_manager_(manager) {} | |
31 ChromeNotifierService::~ChromeNotifierService() {} | |
32 | |
33 // Methods from ProfileKeyedService. | |
34 void ChromeNotifierService::Shutdown() { | |
35 } | |
36 | |
37 // syncer::SyncableService implementation. | |
38 | |
39 // This is called at startup to sync with the server. | |
40 // This code is not thread safe. | |
41 syncer::SyncMergeResult ChromeNotifierService::MergeDataAndStartSyncing( | |
42 syncer::ModelType type, | |
43 const syncer::SyncDataList& initial_sync_data, | |
44 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | |
45 scoped_ptr<syncer::SyncErrorFactory> error_handler) { | |
46 // TODO(petewil):After I add the infrastructure for the test, add a check | |
47 // that we are currently on the UI thread here. | |
48 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, type); | |
49 syncer::SyncMergeResult merge_result(syncer::SYNCED_NOTIFICATIONS); | |
50 | |
51 // Mark all data we have now as local. New incoming data will clear | |
52 // the local flag, and merged data will clear the local flag. We use | |
53 // this to know what has to go back up to the server. | |
54 for (std::vector<SyncedNotification*>::iterator it = | |
55 notification_data_.begin(); | |
56 it != notification_data_.end(); | |
57 ++it) { | |
58 (*it)->set_has_local_changes(true); | |
59 } | |
60 | |
61 for (syncer::SyncDataList::const_iterator it = initial_sync_data.begin(); | |
62 it != initial_sync_data.end(); ++it) { | |
63 const syncer::SyncData& sync_data = *it; | |
64 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, sync_data.GetDataType()); | |
65 | |
66 // Build a local notification object from the sync data. | |
67 scoped_ptr<SyncedNotification> incoming(CreateNotificationFromSyncData( | |
68 sync_data)); | |
69 DCHECK(incoming.get()); | |
70 | |
71 // Process each incoming remote notification. | |
72 const std::string& id = incoming->get_notification_id(); | |
73 DCHECK(id.length() > 0); | |
74 SyncedNotification* found = FindNotificationById(id); | |
75 | |
76 if (NULL == found) { | |
77 // If there are no conflicts, copy in the data from remote. | |
78 Add(incoming.Pass()); | |
79 } else { | |
80 // If the remote and local data conflict (the read or deleted flag), | |
81 // resolve the conflict. | |
82 // TODO(petewil): Implement. | |
83 } | |
84 } | |
85 | |
86 // TODO(petewil): Implement the code that does the actual merging. | |
87 // See chrome/browser/history/history.cc for an example to follow. | |
88 | |
89 // Make a list of local changes to send up to the sync server. | |
90 syncer::SyncChangeList new_changes; | |
91 for (std::vector<SyncedNotification*>::iterator it = | |
92 notification_data_.begin(); | |
93 it != notification_data_.end(); | |
94 ++it) { | |
95 if ((*it)->has_local_changes()) { | |
96 syncer::SyncData sync_data = CreateSyncDataFromNotification(**it); | |
97 new_changes.push_back( | |
98 syncer::SyncChange(FROM_HERE, | |
99 syncer::SyncChange::ACTION_ADD, | |
100 sync_data)); | |
101 } | |
102 } | |
103 | |
104 // Send up the changes that were made locally. | |
105 if (new_changes.size() > 0) { | |
106 merge_result.set_error( | |
107 (sync_processor.get())->ProcessSyncChanges(FROM_HERE, new_changes)); | |
108 } | |
109 | |
110 return merge_result; | |
111 } | |
112 | |
113 void ChromeNotifierService::StopSyncing(syncer::ModelType type) { | |
114 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, type); | |
115 } | |
116 | |
117 syncer::SyncDataList ChromeNotifierService::GetAllSyncData( | |
118 syncer::ModelType type) const { | |
119 DCHECK_EQ(syncer::SYNCED_NOTIFICATIONS, type); | |
120 syncer::SyncDataList local_sync_data; | |
dcheng
2013/01/18 21:56:13
Just call this sync_data?
Pete Williamson
2013/01/23 01:45:55
Done.
| |
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 local_sync_data.push_back(CreateSyncDataFromNotification(**it)); | |
128 } | |
129 | |
130 return local_sync_data; | |
131 } | |
dcheng
2013/01/18 21:56:13
Indent.
Pete Williamson
2013/01/23 01:45:55
Done.
| |
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()); | |
dcheng
2013/01/18 21:56:13
Indent.
Pete Williamson
2013/01/23 01:45:55
Done.
| |
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 SyncedNotification& notification) { | |
174 return *notification.mutable_sync_data(); | |
175 } | |
176 | |
177 // Static Method. Convert from SyncData to our internal format. | |
178 SyncedNotification* ChromeNotifierService::CreateNotificationFromSyncData( | |
179 const syncer::SyncData& sync_data) { | |
180 | |
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 NULL; | |
193 } | |
194 | |
195 // Create a new notification object based on the supplied sync_data. | |
196 SyncedNotification* notification = new SyncedNotification(sync_data); | |
197 | |
198 return notification; | |
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 | |
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->get_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 | |
226 // Logically take ownership of the object. Since we still need the pointer | |
227 // below for show, we don't do the release here, but further below. | |
228 notification_data_.push_back(notification.get()); | |
229 | |
230 // Show it to the user (and complete taking ownership of the pointer). | |
231 Show(notification.release()); | |
232 } | |
233 | |
234 // Send the notification to the NotificationUIManager to show to the user. | |
235 void ChromeNotifierService::Show(SyncedNotification* notification) { | |
236 | |
dcheng
2013/01/18 21:56:13
Empty newlines here and elsewhere at the beginning
Pete Williamson
2013/01/23 01:45:55
Done.
| |
237 // Set up the fields we need to send and create a Notification object. | |
238 GURL origin_url(notification->get_origin_url()); | |
239 GURL icon_url(notification->get_icon_url()); | |
240 std::string title = notification->get_title(); | |
241 string16 title16 = UTF8ToWide(title); | |
dcheng
2013/01/18 21:56:13
UTF8ToUTF16.
Despite the fact that wstring happen
dcheng
2013/01/18 21:56:13
Why not string16 title = UTF8ToUTF16(notification-
Pete Williamson
2013/01/23 01:45:55
Done.
Pete Williamson
2013/01/23 01:45:55
I expect the compiler to be smart and not allocate
dcheng
2013/01/23 18:39:43
I personally find it harder to read a block of cod
Pete Williamson
2013/01/24 01:48:11
Done.
| |
242 std::string body = notification->get_body(); | |
243 string16 body16 = UTF8ToWide(body); | |
244 // TODO(petewil):what goes in the display source, is empty OK? | |
245 string16 display_source16; | |
246 std::string notification_id = notification->get_notification_id(); | |
247 string16 replace_id16 = UTF8ToWide(notification_id); | |
248 // The delegate will eventually catch calls that the notification | |
249 // was read or deleted, and send the changes back to the server. | |
250 // TODO(petewil): who should free this? | |
251 NotificationDelegate* delegate = new ChromeNotifierDelegate(notification_id); | |
dcheng
2013/01/18 21:56:13
Use scoped_refptr. Not sure this comment is needed
Pete Williamson
2013/01/23 01:45:55
Done.
I removed the TODO, but kept the rest of the
| |
252 | |
253 Notification ui_notification(origin_url, icon_url, title16, body16, | |
254 WebKit::WebTextDirectionDefault, | |
255 display_source16, replace_id16, delegate); | |
256 | |
257 notification_manager_->Add(ui_notification, profile_); | |
258 | |
259 DVLOG(1) << "Synced Notification arrived! " << title << " " << body | |
260 << " " << icon_url << " " << notification_id; | |
dcheng
2013/01/18 21:56:13
Formatting.
Pete Williamson
2013/01/23 01:45:55
Done.
| |
261 | |
262 return; | |
263 } | |
OLD | NEW |