12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The ChromeNotifierService works together with sync to maintain the state of
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// user notifications, which can then be presented in the notification center,
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// via the Notification UI Manager.
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/browser/chrome_notification_types.h"
12f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/extensions/api/synced_notifications_private/synced_notifications_shim.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "content/public/browser/notification_service.h"
15f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "extensions/browser/event_router.h"
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace notifier {
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
19116680a4aac90f2aa7413d9095a592090648e557Ben MurdochChromeNotifierService::ChromeNotifierService(Profile* profile)
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : profile_(profile),
21f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      weak_ptr_factory_(this) {
22f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  synced_notifications_shim_.reset(new SyncedNotificationsShim(
23f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      base::Bind(&ChromeNotifierService::FireSyncJSEvent,
245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                 weak_ptr_factory_.GetWeakPtr()),
255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      base::Bind(&ChromeNotifierService::NotifyRefreshNeeded,
26f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                 weak_ptr_factory_.GetWeakPtr())));
27a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch}
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
29effb81e5f8246d0db0270817048dc992db66e9fbBen MurdochChromeNotifierService::~ChromeNotifierService() {
30effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Methods from KeyedService.
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void ChromeNotifierService::Shutdown() {}
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
35f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)SyncedNotificationsShim* ChromeNotifierService::GetSyncedNotificationsShim() {
36f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return synced_notifications_shim_.get();
37f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
38f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
39f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void ChromeNotifierService::FireSyncJSEvent(
40f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    scoped_ptr<extensions::Event> event) {
41f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  event->restrict_to_browser_context = profile_;
42f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // TODO(synced notifications): consider broadcasting to a specific extension
43f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // id.
44f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  extensions::EventRouter::Get(profile_)->BroadcastEvent(event.Pass());
45f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
46f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void ChromeNotifierService::NotifyRefreshNeeded() {
485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  const syncer::ModelTypeSet types(syncer::SYNCED_NOTIFICATIONS);
495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  content::NotificationService::current()->Notify(
505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      chrome::NOTIFICATION_SYNC_REFRESH_LOCAL,
515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      content::Source<Profile>(profile_),
525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      content::Details<const syncer::ModelTypeSet>(&types));
535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace notifier
56