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/notifications/sync_notifier/chrome_notifier_service.h"
10
11#include "chrome/browser/chrome_notification_types.h"
12#include "chrome/browser/extensions/api/synced_notifications_private/synced_notifications_shim.h"
13#include "chrome/browser/profiles/profile.h"
14#include "content/public/browser/notification_service.h"
15#include "extensions/browser/event_router.h"
16
17namespace notifier {
18
19ChromeNotifierService::ChromeNotifierService(Profile* profile)
20    : profile_(profile),
21      weak_ptr_factory_(this) {
22  synced_notifications_shim_.reset(new SyncedNotificationsShim(
23      base::Bind(&ChromeNotifierService::FireSyncJSEvent,
24                 weak_ptr_factory_.GetWeakPtr()),
25      base::Bind(&ChromeNotifierService::NotifyRefreshNeeded,
26                 weak_ptr_factory_.GetWeakPtr())));
27}
28
29ChromeNotifierService::~ChromeNotifierService() {
30}
31
32// Methods from KeyedService.
33void ChromeNotifierService::Shutdown() {}
34
35SyncedNotificationsShim* ChromeNotifierService::GetSyncedNotificationsShim() {
36  return synced_notifications_shim_.get();
37}
38
39void ChromeNotifierService::FireSyncJSEvent(
40    scoped_ptr<extensions::Event> event) {
41  event->restrict_to_browser_context = profile_;
42  // TODO(synced notifications): consider broadcasting to a specific extension
43  // id.
44  extensions::EventRouter::Get(profile_)->BroadcastEvent(event.Pass());
45}
46
47void ChromeNotifierService::NotifyRefreshNeeded() {
48  const syncer::ModelTypeSet types(syncer::SYNCED_NOTIFICATIONS);
49  content::NotificationService::current()->Notify(
50      chrome::NOTIFICATION_SYNC_REFRESH_LOCAL,
51      content::Source<Profile>(profile_),
52      content::Details<const syncer::ModelTypeSet>(&types));
53}
54
55}  // namespace notifier
56