OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 ChromeNotifierService works together with sync to maintain the state of | 5 // The ChromeNotifierService works together with sync to maintain the state of |
6 // user notifications, which can then be presented in the notification center, | 6 // user notifications, which can then be presented in the notification center, |
7 // via the Notification UI Manager. | 7 // via the Notification UI Manager. |
8 | 8 |
9 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h" | 9 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h" |
10 | 10 |
| 11 #include <string> |
| 12 #include <vector> |
| 13 |
11 #include "chrome/browser/notifications/desktop_notification_service.h" | 14 #include "chrome/browser/notifications/desktop_notification_service.h" |
12 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | 15 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
13 #include "chrome/browser/notifications/notification.h" | 16 #include "chrome/browser/notifications/notification.h" |
14 #include "chrome/browser/notifications/notification_ui_manager.h" | 17 #include "chrome/browser/notifications/notification_ui_manager.h" |
15 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
16 #include "grit/ui_strings.h" | 19 #include "grit/generated_resources.h" |
| 20 #include "grit/theme_resources.h" |
17 #include "sync/api/sync_change.h" | 21 #include "sync/api/sync_change.h" |
18 #include "sync/api/sync_change_processor.h" | 22 #include "sync/api/sync_change_processor.h" |
19 #include "sync/api/sync_error_factory.h" | 23 #include "sync/api/sync_error_factory.h" |
20 #include "sync/protocol/sync.pb.h" | 24 #include "sync/protocol/sync.pb.h" |
21 #include "sync/protocol/synced_notification_specifics.pb.h" | 25 #include "sync/protocol/synced_notification_specifics.pb.h" |
22 #include "third_party/WebKit/public/web/WebTextDirection.h" | 26 #include "third_party/WebKit/public/web/WebTextDirection.h" |
23 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
| 28 #include "ui/base/resource/resource_bundle.h" |
24 #include "ui/message_center/notifier_settings.h" | 29 #include "ui/message_center/notifier_settings.h" |
25 #include "url/gurl.h" | 30 #include "url/gurl.h" |
26 | 31 |
27 namespace notifier { | 32 namespace notifier { |
28 namespace { | 33 namespace { |
29 | 34 |
30 const char kSampleSyncedNotificationServiceId[] = "sample-synced-service"; | 35 const char kFirstSyncedNotificationServiceId[] = "Google+"; |
31 | 36 |
32 } | 37 } |
33 | 38 |
34 bool ChromeNotifierService::avoid_bitmap_fetching_for_test_ = false; | 39 bool ChromeNotifierService::avoid_bitmap_fetching_for_test_ = false; |
35 | 40 |
36 ChromeNotifierService::ChromeNotifierService(Profile* profile, | 41 ChromeNotifierService::ChromeNotifierService(Profile* profile, |
37 NotificationUIManager* manager) | 42 NotificationUIManager* manager) |
38 : profile_(profile), notification_manager_(manager) {} | 43 : profile_(profile), notification_manager_(manager) { |
| 44 } |
39 ChromeNotifierService::~ChromeNotifierService() {} | 45 ChromeNotifierService::~ChromeNotifierService() {} |
40 | 46 |
41 // Methods from BrowserContextKeyedService. | 47 // Methods from BrowserContextKeyedService. |
42 void ChromeNotifierService::Shutdown() {} | 48 void ChromeNotifierService::Shutdown() {} |
43 | 49 |
44 // syncer::SyncableService implementation. | 50 // syncer::SyncableService implementation. |
45 | 51 |
46 // This is called at startup to sync with the server. | 52 // This is called at startup to sync with the server. |
47 // This code is not thread safe. | 53 // This code is not thread safe. |
48 syncer::SyncMergeResult ChromeNotifierService::MergeDataAndStartSyncing( | 54 syncer::SyncMergeResult ChromeNotifierService::MergeDataAndStartSyncing( |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 } | 269 } |
264 | 270 |
265 return NULL; | 271 return NULL; |
266 } | 272 } |
267 | 273 |
268 void ChromeNotifierService::GetSyncedNotificationServices( | 274 void ChromeNotifierService::GetSyncedNotificationServices( |
269 std::vector<message_center::Notifier*>* notifiers) { | 275 std::vector<message_center::Notifier*>* notifiers) { |
270 // TODO(mukai|petewil): Check the profile's eligibility before adding the | 276 // TODO(mukai|petewil): Check the profile's eligibility before adding the |
271 // sample app. | 277 // sample app. |
272 | 278 |
273 // Currently we just use kSampleSyncedNotificationServiceId as a place holder. | 279 // TODO(petewil): Really obtain the list of synced notification sending |
274 // TODO(petewil): Really obtain the list of apps from the server and create | 280 // services from the server and create the list of ids here. Until then, we |
275 // the list of ids here. | 281 // are hardcoding the service names. Once that is done, remove this |
| 282 // hardcoding. |
| 283 // crbug.com/248337 |
276 DesktopNotificationService* desktop_notification_service = | 284 DesktopNotificationService* desktop_notification_service = |
277 DesktopNotificationServiceFactory::GetForProfile(profile_); | 285 DesktopNotificationServiceFactory::GetForProfile(profile_); |
278 message_center::NotifierId notifier_id( | 286 message_center::NotifierId notifier_id( |
279 message_center::NotifierId::SYNCED_NOTIFICATION_SERVICE, | 287 message_center::NotifierId::SYNCED_NOTIFICATION_SERVICE, |
280 kSampleSyncedNotificationServiceId); | 288 kFirstSyncedNotificationServiceId); |
281 notifiers->push_back(new message_center::Notifier( | 289 message_center::Notifier* notifier_service = new message_center::Notifier( |
282 notifier_id, | 290 notifier_id, |
283 l10n_util::GetStringUTF16( | 291 l10n_util::GetStringUTF16( |
284 IDS_MESSAGE_CENTER_SAMPLE_SYNCED_NOTIFICATION_SERVICE_NAME), | 292 IDS_FIRST_SYNCED_NOTIFICATION_SERVICE_NAME), |
285 desktop_notification_service->IsNotifierEnabled(notifier_id))); | 293 desktop_notification_service->IsNotifierEnabled(notifier_id)); |
286 // TODO(mukai): Add icon for the sample app. | 294 |
| 295 // Add icons for our sending services. |
| 296 // TODO(petewil): Replace this temporary hardcoding with a new sync datatype |
| 297 // to dynamically get the name and icon for each synced notification sending |
| 298 // service. Until then, we use hardcoded service icons for all services. |
| 299 // crbug.com/248337 |
| 300 notifier_service->icon = ui::ResourceBundle::GetSharedInstance(). |
| 301 GetImageNamed(IDR_TEMPORARY_GOOGLE_PLUS_ICON); |
| 302 |
| 303 notifiers->push_back(notifier_service); |
287 } | 304 } |
288 | 305 |
289 void ChromeNotifierService::MarkNotificationAsDismissed( | 306 void ChromeNotifierService::MarkNotificationAsDismissed( |
290 const std::string& key) { | 307 const std::string& key) { |
291 SyncedNotification* notification = FindNotificationById(key); | 308 SyncedNotification* notification = FindNotificationById(key); |
292 CHECK(notification != NULL); | 309 CHECK(notification != NULL); |
293 | 310 |
294 notification->NotificationHasBeenDismissed(); | 311 notification->NotificationHasBeenDismissed(); |
295 syncer::SyncChangeList new_changes; | 312 syncer::SyncChangeList new_changes; |
296 | 313 |
297 syncer::SyncData sync_data = CreateSyncDataFromNotification(*notification); | 314 syncer::SyncData sync_data = CreateSyncDataFromNotification(*notification); |
298 new_changes.push_back( | 315 new_changes.push_back( |
299 syncer::SyncChange(FROM_HERE, | 316 syncer::SyncChange(FROM_HERE, |
300 syncer::SyncChange::ACTION_UPDATE, | 317 syncer::SyncChange::ACTION_UPDATE, |
301 sync_data)); | 318 sync_data)); |
302 | 319 |
303 // Send up the changes that were made locally. | 320 // Send up the changes that were made locally. |
304 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); | 321 sync_processor_->ProcessSyncChanges(FROM_HERE, new_changes); |
305 } | 322 } |
306 | 323 |
307 // Add a new notification to our data structure. This takes ownership | 324 // Add a new notification to our data structure. This takes ownership |
308 // of the passed in pointer. | 325 // of the passed in pointer. |
309 void ChromeNotifierService::Add(scoped_ptr<SyncedNotification> notification) { | 326 void ChromeNotifierService::Add(scoped_ptr<SyncedNotification> notification) { |
310 SyncedNotification* notification_copy = notification.get(); | 327 SyncedNotification* notification_copy = notification.get(); |
311 // Take ownership of the object and put it into our local storage. | 328 // Take ownership of the object and put it into our local storage. |
312 notification_data_.push_back(notification.release()); | 329 notification_data_.push_back(notification.release()); |
313 | 330 |
| 331 // If the user is not interested in this type of notification, ignore it. |
| 332 std::vector<std::string>::iterator iter = |
| 333 find(enabled_sending_services_.begin(), |
| 334 enabled_sending_services_.end(), |
| 335 notification_copy->GetSendingServiceId()); |
| 336 if (iter == enabled_sending_services_.end()) { |
| 337 return; |
| 338 } |
| 339 |
314 Display(notification_copy); | 340 Display(notification_copy); |
315 } | 341 } |
316 | 342 |
317 void ChromeNotifierService::AddForTest( | 343 void ChromeNotifierService::AddForTest( |
318 scoped_ptr<notifier::SyncedNotification> notification) { | 344 scoped_ptr<notifier::SyncedNotification> notification) { |
319 notification_data_.push_back(notification.release()); | 345 notification_data_.push_back(notification.release()); |
320 } | 346 } |
321 | 347 |
322 void ChromeNotifierService::Display(SyncedNotification* notification) { | 348 void ChromeNotifierService::Display(SyncedNotification* notification) { |
| 349 |
323 // Set up to fetch the bitmaps. | 350 // Set up to fetch the bitmaps. |
324 notification->QueueBitmapFetchJobs(notification_manager_, | 351 notification->QueueBitmapFetchJobs(notification_manager_, |
325 this, | 352 this, |
326 profile_); | 353 profile_); |
327 | 354 |
328 // Our tests cannot use the network for reliability reasons. | 355 // Our tests cannot use the network for reliability reasons. |
329 if (avoid_bitmap_fetching_for_test_) { | 356 if (avoid_bitmap_fetching_for_test_) { |
330 return; | 357 return; |
331 } | 358 } |
332 | 359 |
333 // Start the bitmap fetching, Show() will be called when the last bitmap | 360 // Start the bitmap fetching, Show() will be called when the last bitmap |
334 // either arrives or times out. | 361 // either arrives or times out. |
335 notification->StartBitmapFetch(); | 362 notification->StartBitmapFetch(); |
336 } | 363 } |
337 | 364 |
338 void ChromeNotifierService::OnSyncedNotificationServiceEnabled( | 365 void ChromeNotifierService::OnSyncedNotificationServiceEnabled( |
339 const std::string& notifier_id, bool enabled) { | 366 const std::string& notifier_id, bool enabled) { |
340 // TODO(petewil): start/stop syncing | 367 std::vector<std::string>::iterator iter; |
| 368 |
| 369 iter = find(enabled_sending_services_.begin(), |
| 370 enabled_sending_services_.end(), |
| 371 notifier_id); |
| 372 |
| 373 // Add the notifier_id if it is enabled and not already there. |
| 374 if (iter == enabled_sending_services_.end() && enabled) { |
| 375 enabled_sending_services_.push_back(notifier_id); |
| 376 // TODO(petewil) Check now for any outstanding notifications. |
| 377 // Remove the notifier_id if it is disabled and present. |
| 378 } else if (iter != enabled_sending_services_.end() && !enabled) { |
| 379 enabled_sending_services_.erase(iter); |
| 380 } |
| 381 |
| 382 // Otherwise, nothing to do, we can exit. |
| 383 return; |
341 } | 384 } |
342 | 385 |
343 } // namespace notifier | 386 } // namespace notifier |
OLD | NEW |