OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // The SyncedNotificationAppInfoService brings down read only metadata from the | 5 // The SyncedNotificationAppInfoService brings down read only metadata from the |
6 // sync server with information about the services sending synced notifications. | 6 // sync server with information about the services sending synced notifications. |
7 | 7 |
8 #include "chrome/browser/notifications/sync_notifier/synced_notification_app_inf o_service.h" | 8 #include "chrome/browser/notifications/sync_notifier/synced_notification_app_inf o_service.h" |
9 | 9 |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h" | |
12 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service_fac tory.h" | |
11 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
12 #include "sync/api/sync_change.h" | 14 #include "sync/api/sync_change.h" |
13 #include "sync/api/sync_change_processor.h" | 15 #include "sync/api/sync_change_processor.h" |
14 #include "sync/api/sync_error_factory.h" | 16 #include "sync/api/sync_error_factory.h" |
15 #include "sync/protocol/sync.pb.h" | 17 #include "sync/protocol/sync.pb.h" |
16 #include "sync/protocol/synced_notification_app_info_specifics.pb.h" | 18 #include "sync/protocol/synced_notification_app_info_specifics.pb.h" |
17 #include "url/gurl.h" | 19 #include "url/gurl.h" |
18 | 20 |
19 namespace notifier { | 21 namespace notifier { |
20 | 22 |
23 bool SyncedNotificationAppInfoService::avoid_bitmap_fetching_for_test_ = false; | |
dewittj
2014/03/17 21:43:43
maybe a better way to do this is to have a bitmapf
Pete Williamson
2014/03/21 01:22:31
In my opinion the simplicity of having a flag is b
| |
24 | |
21 SyncedNotificationAppInfoService::SyncedNotificationAppInfoService( | 25 SyncedNotificationAppInfoService::SyncedNotificationAppInfoService( |
22 Profile* profile) | 26 Profile* profile) |
23 : profile_(profile) {} | 27 : profile_(profile), chrome_notifier_service_(NULL) {} |
24 | 28 |
25 SyncedNotificationAppInfoService::~SyncedNotificationAppInfoService() {} | 29 SyncedNotificationAppInfoService::~SyncedNotificationAppInfoService() {} |
26 | 30 |
27 // Methods from BrowserContextKeyedService. | 31 // Methods from BrowserContextKeyedService. |
28 void SyncedNotificationAppInfoService::Shutdown() {} | 32 void SyncedNotificationAppInfoService::Shutdown() {} |
29 | 33 |
30 // syncer::SyncableService implementation. | 34 // syncer::SyncableService implementation. |
31 | 35 |
32 // This is called at startup to sync with the sync data. This code is not thread | 36 // This is called at startup to sync with the sync data. This code is not thread |
33 // safe, it should only be called by sync on the browser thread. | 37 // safe, it should only be called by sync on the browser thread. |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 // Process each incoming app_info protobuf. | 153 // Process each incoming app_info protobuf. |
150 const std::string& name = incoming->settings_display_name(); | 154 const std::string& name = incoming->settings_display_name(); |
151 DCHECK_GT(name.length(), 0U); | 155 DCHECK_GT(name.length(), 0U); |
152 if (name.length() == 0) { | 156 if (name.length() == 0) { |
153 // If there is no unique id (name), there is nothing we can do. | 157 // If there is no unique id (name), there is nothing we can do. |
154 return; | 158 return; |
155 } | 159 } |
156 | 160 |
157 SyncedNotificationAppInfo* found = FindSyncedNotificationAppInfoByName(name); | 161 SyncedNotificationAppInfo* found = FindSyncedNotificationAppInfoByName(name); |
158 | 162 |
159 if (NULL != found) { | 163 std::vector<std::string> old_app_ids; |
164 std::vector<std::string> new_app_ids; | |
165 std::vector<std::string> added_app_ids; | |
166 std::vector<std::string> removed_app_ids; | |
167 | |
168 incoming->GetAppIdList(&new_app_ids); | |
169 | |
170 if (NULL == found) { | |
171 added_app_ids = new_app_ids; | |
172 } else { | |
160 // When we have an update, some app id types may be added or removed. | 173 // When we have an update, some app id types may be added or removed. |
161 // Append to lists of added and removed types. | 174 // Append to lists of added and removed types. |
175 found->GetAppIdList(&old_app_ids); | |
176 incoming->GetAppIdList(&new_app_ids); | |
162 FreeSyncedNotificationAppInfoByName(name); | 177 FreeSyncedNotificationAppInfoByName(name); |
178 | |
179 // Pre-reserve enough space in the output vectors because set_difference | |
180 // uses assignment to array elements, not push_back. | |
dewittj
2014/03/17 21:43:43
Consider using std::back_inserter here, like this:
Pete Williamson
2014/03/21 01:22:31
Done. Good catch, I'd forgotten about back_insert
| |
181 size_t max_added_size = new_app_ids.size(); | |
182 size_t max_removed_size = old_app_ids.size(); | |
183 for (size_t ii = 0; ii < max_added_size; ++ii) { | |
184 added_app_ids.push_back(std::string()); | |
185 } | |
186 for (size_t jj = 0; jj < max_removed_size; ++jj) { | |
187 removed_app_ids.push_back(std::string()); | |
188 } | |
189 | |
190 // Set up for a set difference by sorting the lists. | |
191 std::sort(old_app_ids.begin(), old_app_ids.end()); | |
192 std::sort(new_app_ids.begin(), new_app_ids.end()); | |
193 | |
194 // Calculate which app ids are removed (in old, but not in new app ids). | |
195 std::set_difference(old_app_ids.begin(), | |
196 old_app_ids.end(), | |
197 new_app_ids.begin(), | |
198 new_app_ids.end(), | |
199 removed_app_ids.begin()); | |
200 | |
201 // Calculate which app_ids are added (in new, but not in old app ids). | |
202 std::set_difference(new_app_ids.begin(), | |
203 new_app_ids.end(), | |
204 old_app_ids.begin(), | |
205 old_app_ids.end(), | |
206 added_app_ids.begin()); | |
207 } | |
208 | |
209 // Put these lists into the app_info object. | |
210 incoming->SetAddedAppIds(added_app_ids); | |
211 incoming->SetRemovedAppIds(removed_app_ids); | |
212 | |
213 // Start bitmap fetch. | |
214 if (!avoid_bitmap_fetching_for_test_) { | |
215 incoming->QueueBitmapFetchJobs(); | |
216 incoming->StartBitmapFetch(); | |
217 } else { | |
218 OnBitmapFetchesDone(incoming->added_app_ids(), incoming->removed_app_ids()); | |
163 } | 219 } |
164 | 220 |
165 sending_service_infos_.push_back(incoming.release()); | 221 sending_service_infos_.push_back(incoming.release()); |
222 } | |
166 | 223 |
224 void SyncedNotificationAppInfoService::OnBitmapFetchesDone( | |
225 std::vector<std::string> added_app_ids, | |
226 std::vector<std::string> removed_app_ids) { | |
167 // Tell the Chrome Notifier Service so it can show any notifications that were | 227 // Tell the Chrome Notifier Service so it can show any notifications that were |
168 // waiting for the app id to arrive, and to remave any notifications that are | 228 // waiting for the app id to arrive, and to remave any notifications that are |
169 // no longer supported. | 229 // no longer supported. |
170 // TODO(petewil): Notify CNS of added ids | 230 if (chrome_notifier_service_ != NULL) { |
171 // TODO(petewil): Notify CNS of deleted ids. | 231 chrome_notifier_service_->SetAddedAppIds(added_app_ids); |
232 chrome_notifier_service_->SetRemovedAppIds(removed_app_ids); | |
233 } | |
172 } | 234 } |
173 | 235 |
174 // Static Method. Convert from a server protobuf to our internal format. | 236 // Static Method. Convert from a server protobuf to our internal format. |
175 scoped_ptr<SyncedNotificationAppInfo> | 237 scoped_ptr<SyncedNotificationAppInfo> |
176 SyncedNotificationAppInfoService::CreateSyncedNotificationAppInfoFromProtobuf( | 238 SyncedNotificationAppInfoService::CreateSyncedNotificationAppInfoFromProtobuf( |
177 const sync_pb::SyncedNotificationAppInfo& server_app_info) { | 239 const sync_pb::SyncedNotificationAppInfo& server_app_info) { |
178 | 240 |
179 // Check for mandatory fields in the sync_data object. | 241 // Check for mandatory fields in the sync_data object. |
180 std::string display_name; | 242 std::string display_name; |
181 if (server_app_info.has_settings_display_name()) { | 243 if (server_app_info.has_settings_display_name()) { |
182 display_name = server_app_info.settings_display_name(); | 244 display_name = server_app_info.settings_display_name(); |
183 } | 245 } |
184 scoped_ptr<SyncedNotificationAppInfo> app_info; | 246 scoped_ptr<SyncedNotificationAppInfo> app_info; |
185 if (display_name.length() == 0) | 247 if (display_name.length() == 0) |
186 return app_info.Pass(); | 248 return app_info.Pass(); |
187 | 249 |
188 // Create a new app info object based on the supplied protobuf. | 250 // Create a new app info object based on the supplied protobuf. |
189 app_info.reset(new SyncedNotificationAppInfo(display_name)); | 251 app_info.reset(new SyncedNotificationAppInfo(profile_, display_name, this)); |
190 | 252 |
191 // TODO(petewil): Eventually we will add the monochrome icon here, and we may | 253 // TODO(petewil): Eventually we will add the monochrome icon here, and we may |
192 // need to fetch the correct url for the current DPI. | 254 // need to fetch the correct url for the current DPI. |
193 // Add the icon URL, if any. | 255 // Add the icon URL, if any. |
194 if (server_app_info.has_icon()) { | 256 if (server_app_info.has_icon()) { |
195 std::string icon_url = server_app_info.icon().url(); | 257 std::string icon_url = server_app_info.icon().url(); |
196 app_info->SetSettingsIcon(GURL(icon_url)); | 258 app_info->SetSettingsIcon(GURL(icon_url)); |
197 } | 259 } |
198 | 260 |
199 // Add all the AppIds from the protobuf. | 261 // Add all the AppIds from the protobuf. |
(...skipping 15 matching lines...) Expand all Loading... | |
215 it != sending_service_infos_.end(); | 277 it != sending_service_infos_.end(); |
216 ++it) { | 278 ++it) { |
217 SyncedNotificationAppInfo* app_info = *it; | 279 SyncedNotificationAppInfo* app_info = *it; |
218 if (name == app_info->settings_display_name()) | 280 if (name == app_info->settings_display_name()) |
219 return *it; | 281 return *it; |
220 } | 282 } |
221 | 283 |
222 return NULL; | 284 return NULL; |
223 } | 285 } |
224 | 286 |
287 // This returns a pointer into a vector that we own. Caller must not free it. | |
288 // Returns NULL if no match is found. | |
289 notifier::SyncedNotificationAppInfo* | |
290 SyncedNotificationAppInfoService::FindSyncedNotificationAppInfoByAppId( | |
291 const std::string& app_id) { | |
292 for (ScopedVector<SyncedNotificationAppInfo>::const_iterator it = | |
293 sending_service_infos_.begin(); | |
294 it != sending_service_infos_.end(); | |
295 ++it) { | |
296 SyncedNotificationAppInfo* app_info = *it; | |
297 if (app_info->HasAppId(app_id)) | |
298 return *it; | |
299 } | |
300 | |
301 return NULL; | |
302 } | |
303 | |
304 // Lookup the sending service name for a given app id. | |
305 std::string SyncedNotificationAppInfoService::FindSendingServiceNameFromAppId( | |
306 const std::string app_id) { | |
307 for (ScopedVector<SyncedNotificationAppInfo>::const_iterator it = | |
308 sending_service_infos_.begin(); | |
309 it != sending_service_infos_.end(); | |
310 ++it) { | |
311 SyncedNotificationAppInfo* app_info = *it; | |
312 if (app_info->HasAppId(app_id)) | |
313 return app_info->settings_display_name(); | |
314 } | |
315 | |
316 return std::string(); | |
317 } | |
318 | |
225 void SyncedNotificationAppInfoService::FreeSyncedNotificationAppInfoByName( | 319 void SyncedNotificationAppInfoService::FreeSyncedNotificationAppInfoByName( |
226 const std::string& name) { | 320 const std::string& name) { |
227 ScopedVector<SyncedNotificationAppInfo>::iterator it = | 321 ScopedVector<SyncedNotificationAppInfo>::iterator it = |
228 sending_service_infos_.begin(); | 322 sending_service_infos_.begin(); |
229 for (; it != sending_service_infos_.end(); ++it) { | 323 for (; it != sending_service_infos_.end(); ++it) { |
230 SyncedNotificationAppInfo* app_info = *it; | 324 SyncedNotificationAppInfo* app_info = *it; |
231 if (name == app_info->settings_display_name()) { | 325 if (name == app_info->settings_display_name()) { |
232 sending_service_infos_.erase(it); | 326 sending_service_infos_.erase(it); |
233 return; | 327 return; |
234 } | 328 } |
235 } | 329 } |
236 } | 330 } |
237 | 331 |
332 std::vector<std::string> | |
333 SyncedNotificationAppInfoService::GetAllSendingServiceNames() { | |
334 std::vector<std::string> names; | |
335 ScopedVector<SyncedNotificationAppInfo>::iterator it = | |
336 sending_service_infos_.begin(); | |
337 | |
338 for (; it != sending_service_infos_.end(); ++it) { | |
339 SyncedNotificationAppInfo* app_info = *it; | |
340 names.push_back(app_info->settings_display_name()); | |
341 } | |
342 | |
343 return names; | |
344 } | |
345 | |
238 // Add a new app info to our data structure. This takes ownership | 346 // Add a new app info to our data structure. This takes ownership |
239 // of the passed in pointer. | 347 // of the passed in pointer. |
240 void SyncedNotificationAppInfoService::Add( | 348 void SyncedNotificationAppInfoService::Add( |
241 scoped_ptr<SyncedNotificationAppInfo> app_info) { | 349 scoped_ptr<SyncedNotificationAppInfo> app_info) { |
242 sending_service_infos_.push_back(app_info.release()); | 350 sending_service_infos_.push_back(app_info.release()); |
243 } | 351 } |
244 | 352 |
245 } // namespace notifier | 353 } // namespace notifier |
OLD | NEW |