desktop_notification_service.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/notifications/desktop_notification_service.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/histogram.h"
81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/prefs/scoped_user_pref_update.h"
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/thread.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/browser_process.h"
127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/content_settings/content_settings_details.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/content_settings/content_settings_provider.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/content_settings/host_content_settings_map.h"
161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/extensions/api/notifications/notifications_api.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/extension_service.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/extensions/extension_system.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/infobars/confirm_infobar_delegate.h"
20a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/browser/infobars/infobar.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/infobars/infobar_service.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/notifications/desktop_notification_service_factory.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/notifications/notification.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/notifications/notification_object_proxy.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/notifications/notification_ui_manager.h"
26eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h"
27eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/notifications/sync_notifier/chrome_notifier_service_factory.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/content_settings.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/content_settings_pattern.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h"
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/user_prefs/pref_registry_syncable.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_service.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/render_view_host.h"
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/common/show_desktop_notification_params.h"
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/browser/event_router.h"
41f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/browser/info_map.h"
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "extensions/common/constants.h"
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "grit/browser_resources.h"
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "grit/chromium_strings.h"
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "grit/generated_resources.h"
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "grit/theme_resources.h"
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/escape.h"
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "ui/base/webui/web_ui_util.h"
51a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "ui/message_center/message_center_util.h"
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/message_center/notifier_settings.h"
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserThread;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::RenderViewHost;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::WebContents;
57eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochusing message_center::NotifierId;
58f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)using blink::WebTextDirection;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NotificationPermissionInfoBarDelegate --------------------------------------
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The delegate for the infobar shown when an origin requests notification
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// permissions.
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NotificationPermissionInfoBarDelegate : public ConfirmInfoBarDelegate {
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
67a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Creates a notification permission infobar and delegate and adds the infobar
68a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // to |infobar_service|.
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static void Create(InfoBarService* infobar_service,
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     DesktopNotificationService* notification_service,
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     const GURL& origin,
72a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                     const base::string16& display_name,
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     int process_id,
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     int route_id,
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     int callback_context);
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NotificationPermissionInfoBarDelegate(
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DesktopNotificationService* notification_service,
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const GURL& origin,
81a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      const base::string16& display_name,
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int process_id,
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int route_id,
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int callback_context);
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~NotificationPermissionInfoBarDelegate();
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ConfirmInfoBarDelegate:
8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual int GetIconID() const OVERRIDE;
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual Type GetInfoBarType() const OVERRIDE;
90a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual base::string16 GetMessageText() const OVERRIDE;
91a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual base::string16 GetButtonLabel(InfoBarButton button) const OVERRIDE;
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Accept() OVERRIDE;
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Cancel() OVERRIDE;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The origin we are asking for permissions on.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL origin_;
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The display name for the origin to be displayed.  Will be different from
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // origin_ for extensions.
100a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 display_name_;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The notification service to be used.
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DesktopNotificationService* notification_service_;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The callback information that tells us how to respond to javascript via
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the correct RenderView.
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int process_id_;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int route_id_;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int callback_context_;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether the user clicked one of the buttons.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool action_taken_;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(NotificationPermissionInfoBarDelegate);
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void NotificationPermissionInfoBarDelegate::Create(
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    InfoBarService* infobar_service,
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DesktopNotificationService* notification_service,
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& origin,
122a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& display_name,
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int process_id,
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int route_id,
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int callback_context) {
126a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
127a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      scoped_ptr<ConfirmInfoBarDelegate>(
128a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          new NotificationPermissionInfoBarDelegate(
129a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)              notification_service, origin, display_name, process_id, route_id,
130a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)              callback_context))));
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NotificationPermissionInfoBarDelegate::NotificationPermissionInfoBarDelegate(
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DesktopNotificationService* notification_service,
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& origin,
136a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& display_name,
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int process_id,
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int route_id,
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int callback_context)
140a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    : ConfirmInfoBarDelegate(),
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      origin_(origin),
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      display_name_(display_name),
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      notification_service_(notification_service),
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      process_id_(process_id),
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      route_id_(route_id),
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      callback_context_(callback_context),
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      action_taken_(false) {
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NotificationPermissionInfoBarDelegate::
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ~NotificationPermissionInfoBarDelegate() {
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!action_taken_)
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    UMA_HISTOGRAM_COUNTS("NotificationPermissionRequest.Ignored", 1);
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RenderViewHost* host = RenderViewHost::FromID(process_id_, route_id_);
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (host)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    host->DesktopNotificationPermissionRequestDone(callback_context_);
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
16090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)int NotificationPermissionInfoBarDelegate::GetIconID() const {
16190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return IDR_INFOBAR_DESKTOP_NOTIFICATIONS;
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)InfoBarDelegate::Type
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NotificationPermissionInfoBarDelegate::GetInfoBarType() const {
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return PAGE_ACTION_TYPE;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
169a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 NotificationPermissionInfoBarDelegate::GetMessageText() const {
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return l10n_util::GetStringFUTF16(IDS_NOTIFICATION_PERMISSIONS,
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    display_name_);
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
174a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 NotificationPermissionInfoBarDelegate::GetButtonLabel(
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    InfoBarButton button) const {
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      IDS_NOTIFICATION_PERMISSION_YES : IDS_NOTIFICATION_PERMISSION_NO);
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool NotificationPermissionInfoBarDelegate::Accept() {
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UMA_HISTOGRAM_COUNTS("NotificationPermissionRequest.Allowed", 1);
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  notification_service_->GrantPermission(origin_);
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  action_taken_ = true;
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool NotificationPermissionInfoBarDelegate::Cancel() {
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UMA_HISTOGRAM_COUNTS("NotificationPermissionRequest.Denied", 1);
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  notification_service_->DenyPermission(origin_);
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  action_taken_ = true;
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DesktopNotificationService -------------------------------------------------
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
1987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid DesktopNotificationService::RegisterProfilePrefs(
199c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
2004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  registry->RegisterListPref(
2014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      prefs::kMessageCenterDisabledExtensionIds,
2024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
2034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  registry->RegisterListPref(
2044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      prefs::kMessageCenterDisabledSystemComponentIds,
2054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
2064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  registry->RegisterListPref(
2074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      prefs::kMessageCenterEnabledSyncNotifierIds,
2084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
2098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  WelcomeNotification::RegisterProfilePrefs(registry);
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
213a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 DesktopNotificationService::CreateDataUrl(
214a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const GURL& icon_url,
215a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& title,
216a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& body,
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebTextDirection dir) {
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int resource;
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<std::string> subst;
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (icon_url.is_valid()) {
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    resource = IDR_NOTIFICATION_ICON_HTML;
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    subst.push_back(icon_url.spec());
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    subst.push_back(net::EscapeForHTML(UTF16ToUTF8(title)));
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    subst.push_back(net::EscapeForHTML(UTF16ToUTF8(body)));
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // icon float position
226f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    subst.push_back(dir == blink::WebTextDirectionRightToLeft ?
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    "right" : "left");
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (title.empty() || body.empty()) {
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    resource = IDR_NOTIFICATION_1LINE_HTML;
230a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::string16 line = title.empty() ? body : title;
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Strings are div names in the template file.
232a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::string16 line_name = title.empty() ? ASCIIToUTF16("description")
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       : ASCIIToUTF16("title");
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    subst.push_back(net::EscapeForHTML(UTF16ToUTF8(line_name)));
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    subst.push_back(net::EscapeForHTML(UTF16ToUTF8(line)));
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    resource = IDR_NOTIFICATION_2LINE_HTML;
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    subst.push_back(net::EscapeForHTML(UTF16ToUTF8(title)));
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    subst.push_back(net::EscapeForHTML(UTF16ToUTF8(body)));
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // body text direction
242f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  subst.push_back(dir == blink::WebTextDirectionRightToLeft ?
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  "rtl" : "ltr");
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return CreateDataUrl(resource, subst);
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
249a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 DesktopNotificationService::CreateDataUrl(
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int resource, const std::vector<std::string>& subst) {
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const base::StringPiece template_html(
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ResourceBundle::GetSharedInstance().GetRawDataResource(
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          resource));
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (template_html.empty()) {
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED() << "unable to load template. ID: " << resource;
257a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return base::string16();
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string data = ReplaceStringPlaceholders(template_html, subst, NULL);
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return UTF8ToUTF16("data:text/html;charset=utf-8," +
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      net::EscapeQueryParamValue(data, false));
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string DesktopNotificationService::AddNotification(
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& origin_url,
268a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& title,
269a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& message,
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& icon_url,
271a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& replace_id,
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NotificationDelegate* delegate,
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* profile) {
274a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (message_center::IsRichNotificationEnabled()) {
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // For message center create a non-HTML notification with |icon_url|.
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Notification notification(origin_url, icon_url, title, message,
277f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                              blink::WebTextDirectionDefault,
278a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                              base::string16(), replace_id, delegate);
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    g_browser_process->notification_ui_manager()->Add(notification, profile);
2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return notification.notification_id();
2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Generate a data URL embedding the icon URL, title, and message.
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL content_url(CreateDataUrl(
285f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      icon_url, title, message, blink::WebTextDirectionDefault));
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Notification notification(
287a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      GURL(), content_url, base::string16(), replace_id, delegate);
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  g_browser_process->notification_ui_manager()->Add(notification, profile);
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return notification.notification_id();
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string DesktopNotificationService::AddIconNotification(
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& origin_url,
295a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& title,
296a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& message,
2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Image& icon,
298a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& replace_id,
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NotificationDelegate* delegate,
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* profile) {
301a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (message_center::IsRichNotificationEnabled()) {
3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // For message center create a non-HTML notification with |icon|.
3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Notification notification(origin_url, icon, title, message,
304f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                              blink::WebTextDirectionDefault,
305a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                              base::string16(), replace_id, delegate);
3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    g_browser_process->notification_ui_manager()->Add(notification, profile);
3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return notification.notification_id();
3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL icon_url;
3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!icon.IsEmpty())
3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    icon_url = GURL(webui::GetBitmapDataUrl(*icon.ToSkBitmap()));
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return AddNotification(
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      origin_url, title, message, icon_url, replace_id, delegate, profile);
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DesktopNotificationService::RemoveNotification(
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& notification_id) {
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    g_browser_process->notification_ui_manager()->CancelById(notification_id);
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)DesktopNotificationService::DesktopNotificationService(
3242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Profile* profile,
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NotificationUIManager* ui_manager)
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : profile_(profile),
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ui_manager_(ui_manager) {
328eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  OnStringListPrefChanged(
329eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      prefs::kMessageCenterDisabledExtensionIds, &disabled_extension_ids_);
330eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  OnStringListPrefChanged(
331eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      prefs::kMessageCenterDisabledSystemComponentIds,
332eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      &disabled_system_component_ids_);
333eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  OnStringListPrefChanged(
334eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      prefs::kMessageCenterEnabledSyncNotifierIds, &enabled_sync_notifier_ids_);
3352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  disabled_extension_id_pref_.Init(
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      prefs::kMessageCenterDisabledExtensionIds,
3372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profile_->GetPrefs(),
3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(
339eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          &DesktopNotificationService::OnStringListPrefChanged,
340eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          base::Unretained(this),
341eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          base::Unretained(prefs::kMessageCenterDisabledExtensionIds),
342eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          base::Unretained(&disabled_extension_ids_)));
343c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  disabled_system_component_id_pref_.Init(
344c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      prefs::kMessageCenterDisabledSystemComponentIds,
345c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      profile_->GetPrefs(),
346c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Bind(
347eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          &DesktopNotificationService::OnStringListPrefChanged,
348eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          base::Unretained(this),
349eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          base::Unretained(prefs::kMessageCenterDisabledSystemComponentIds),
350eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          base::Unretained(&disabled_system_component_ids_)));
351eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  enabled_sync_notifier_id_pref_.Init(
352eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      prefs::kMessageCenterEnabledSyncNotifierIds,
353eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      profile_->GetPrefs(),
354eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      base::Bind(
355eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          &DesktopNotificationService::OnStringListPrefChanged,
356eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          base::Unretained(this),
357eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          base::Unretained(prefs::kMessageCenterEnabledSyncNotifierIds),
358eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          base::Unretained(&enabled_sync_notifier_ids_)));
35990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
36090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                 content::Source<Profile>(profile_));
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DesktopNotificationService::~DesktopNotificationService() {
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DesktopNotificationService::GrantPermission(const GURL& origin) {
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ContentSettingsPattern primary_pattern =
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ContentSettingsPattern::FromURLNoWildcard(origin);
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  profile_->GetHostContentSettingsMap()->SetContentSetting(
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      primary_pattern,
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ContentSettingsPattern::Wildcard(),
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NO_RESOURCE_IDENTIFIER,
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CONTENT_SETTING_ALLOW);
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DesktopNotificationService::DenyPermission(const GURL& origin) {
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ContentSettingsPattern primary_pattern =
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ContentSettingsPattern::FromURLNoWildcard(origin);
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  profile_->GetHostContentSettingsMap()->SetContentSetting(
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      primary_pattern,
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ContentSettingsPattern::Wildcard(),
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NO_RESOURCE_IDENTIFIER,
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CONTENT_SETTING_BLOCK);
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ContentSetting DesktopNotificationService::GetDefaultContentSetting(
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string* provider_id) {
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return profile_->GetHostContentSettingsMap()->GetDefaultContentSetting(
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CONTENT_SETTINGS_TYPE_NOTIFICATIONS, provider_id);
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DesktopNotificationService::SetDefaultContentSetting(
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ContentSetting setting) {
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  profile_->GetHostContentSettingsMap()->SetDefaultContentSetting(
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CONTENT_SETTINGS_TYPE_NOTIFICATIONS, setting);
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DesktopNotificationService::ResetToDefaultContentSetting() {
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  profile_->GetHostContentSettingsMap()->SetDefaultContentSetting(
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_DEFAULT);
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DesktopNotificationService::GetNotificationsSettings(
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ContentSettingsForOneType* settings) {
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  profile_->GetHostContentSettingsMap()->GetSettingsForOneType(
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NO_RESOURCE_IDENTIFIER,
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      settings);
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DesktopNotificationService::ClearSetting(
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ContentSettingsPattern& pattern) {
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  profile_->GetHostContentSettingsMap()->SetContentSetting(
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pattern,
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ContentSettingsPattern::Wildcard(),
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NO_RESOURCE_IDENTIFIER,
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CONTENT_SETTING_DEFAULT);
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DesktopNotificationService::ResetAllOrigins() {
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  profile_->GetHostContentSettingsMap()->ClearSettingsForOneType(
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ContentSetting DesktopNotificationService::GetContentSetting(
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& origin) {
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return profile_->GetHostContentSettingsMap()->GetContentSetting(
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      origin,
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      origin,
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NO_RESOURCE_IDENTIFIER);
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DesktopNotificationService::RequestPermission(
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& origin, int process_id, int route_id, int callback_context,
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WebContents* contents) {
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |origin| hasn't been seen before and the default content setting for
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // notifications is "ask", show an infobar.
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The cache can only answer queries on the IO thread once it's initialized,
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // so don't ask the cache.
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ContentSetting setting = GetContentSetting(origin);
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (setting == CONTENT_SETTING_ASK) {
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Show an info bar requesting permission.
4472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    InfoBarService* infobar_service =
4482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        InfoBarService::FromWebContents(contents);
4492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // |infobar_service| may be NULL, e.g., if this request originated in a
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // browser action popup, extension background page, or any HTML that runs
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // outside of a tab.
4522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (infobar_service) {
4532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      NotificationPermissionInfoBarDelegate::Create(
4542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          infobar_service,
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          DesktopNotificationServiceFactory::GetForProfile(
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              Profile::FromBrowserContext(contents->GetBrowserContext())),
4577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch          origin, DisplayNameForOriginInProcessId(origin, process_id),
4587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch          process_id, route_id, callback_context);
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Notify renderer immediately.
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RenderViewHost* host = RenderViewHost::FromID(process_id, route_id);
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (host)
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    host->DesktopNotificationPermissionRequestDone(callback_context);
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(OS_WIN)
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DesktopNotificationService::ShowNotification(
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const Notification& notification) {
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetUIManager()->Add(notification, profile_);
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool DesktopNotificationService::CancelDesktopNotification(
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int process_id, int route_id, int notification_id) {
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<NotificationObjectProxy> proxy(
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new NotificationObjectProxy(process_id, route_id, notification_id,
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  false));
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GetUIManager()->CancelById(proxy->id());
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // OS_WIN
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool DesktopNotificationService::ShowDesktopNotification(
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const content::ShowDesktopNotificationHostMsgParams& params,
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int process_id, int route_id, DesktopNotificationSource source) {
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const GURL& origin = params.origin;
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NotificationObjectProxy* proxy =
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new NotificationObjectProxy(process_id, route_id,
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  params.notification_id,
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  source == WorkerNotification);
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
494a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 display_source =
495a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DisplayNameForOriginInProcessId(origin, process_id);
496a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  Notification notification(origin, params.icon_url, params.title,
497a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      params.body, params.direction, display_source, params.replace_id,
498a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      proxy);
499a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
500a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // The webkit notification doesn't timeout.
501a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  notification.set_never_timeout(true);
502a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
503a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ShowNotification(notification);
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
507a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 DesktopNotificationService::DisplayNameForOriginInProcessId(
508c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const GURL& origin, int process_id) {
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the source is an extension, lookup the display name.
510c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Message center prefers to use extension name if the notification
511c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // is allowed by an extension.
512c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (NotificationUIManager::DelegatesToMessageCenter() ||
513c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      origin.SchemeIs(extensions::kExtensionScheme)) {
514f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    extensions::InfoMap* extension_info_map =
515c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        extensions::ExtensionSystem::Get(profile_)->info_map();
516c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (extension_info_map) {
517c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ExtensionSet extensions;
518c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin(
519c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          origin, process_id, extensions::APIPermission::kNotification,
520c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          &extensions);
521c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      for (ExtensionSet::const_iterator iter = extensions.begin();
522c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)           iter != extensions.end(); ++iter) {
523eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        NotifierId notifier_id(NotifierId::APPLICATION, (*iter)->id());
524eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        if (IsNotifierEnabled(notifier_id))
525c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          return UTF8ToUTF16((*iter)->name());
526c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return UTF8ToUTF16(origin.host());
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DesktopNotificationService::NotifySettingsChange() {
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::NotificationService::current()->Notify(
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      chrome::NOTIFICATION_DESKTOP_NOTIFICATION_SETTINGS_CHANGED,
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::Source<DesktopNotificationService>(this),
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::NotificationService::NoDetails());
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NotificationUIManager* DesktopNotificationService::GetUIManager() {
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We defer setting ui_manager_ to the global singleton until we need it
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in order to avoid UI dependent construction during startup.
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!ui_manager_)
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ui_manager_ = g_browser_process->notification_ui_manager();
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return ui_manager_;
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
547eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool DesktopNotificationService::IsNotifierEnabled(
548eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const NotifierId& notifier_id) {
549eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  switch (notifier_id.type) {
550eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    case NotifierId::APPLICATION:
551eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      return disabled_extension_ids_.find(notifier_id.id) ==
552eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          disabled_extension_ids_.end();
553eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    case NotifierId::WEB_PAGE:
554eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      return GetContentSetting(notifier_id.url) == CONTENT_SETTING_ALLOW;
555eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    case NotifierId::SYSTEM_COMPONENT:
556424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#if defined(OS_CHROMEOS)
557a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      return disabled_system_component_ids_.find(notifier_id.id) ==
558a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          disabled_system_component_ids_.end();
559424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#else
560a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      // We do not disable system component notifications.
561a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      return true;
562424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#endif
563eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    case NotifierId::SYNCED_NOTIFICATION_SERVICE:
564eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      return enabled_sync_notifier_ids_.find(notifier_id.id) !=
565eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          enabled_sync_notifier_ids_.end();
566eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
567c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
568eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  NOTREACHED();
569eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return false;
570eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
571eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
572eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid DesktopNotificationService::SetNotifierEnabled(
573eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const NotifierId& notifier_id,
574eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    bool enabled) {
575eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DCHECK_NE(NotifierId::WEB_PAGE, notifier_id.type);
576eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
577eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  bool add_new_item = false;
578eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  const char* pref_name = NULL;
579eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  scoped_ptr<base::StringValue> id;
580eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  switch (notifier_id.type) {
581eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    case NotifierId::APPLICATION:
582eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      pref_name = prefs::kMessageCenterDisabledExtensionIds;
583eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      add_new_item = !enabled;
584eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      id.reset(new base::StringValue(notifier_id.id));
5851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      FirePermissionLevelChangedEvent(notifier_id, enabled);
586eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      break;
587eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    case NotifierId::SYSTEM_COMPONENT:
588424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#if defined(OS_CHROMEOS)
589eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      pref_name = prefs::kMessageCenterDisabledSystemComponentIds;
590eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      add_new_item = !enabled;
591a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      id.reset(new base::StringValue(notifier_id.id));
592424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#else
593424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      return;
594424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#endif
595eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      break;
596eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    case NotifierId::SYNCED_NOTIFICATION_SERVICE:
597eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      pref_name = prefs::kMessageCenterEnabledSyncNotifierIds;
598eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      // Adding a new item if |enabled| == true, since synced notification
599eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      // services are opt-in.
600eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      add_new_item = enabled;
601eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      id.reset(new base::StringValue(notifier_id.id));
602eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      break;
603eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    default:
604eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      NOTREACHED();
605eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
606eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DCHECK(pref_name != NULL);
607eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
6087dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  ListPrefUpdate update(profile_->GetPrefs(), pref_name);
6097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  base::ListValue* const list = update.Get();
6107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (add_new_item) {
6117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // AppendIfNotPresent will delete |adding_value| when the same value
6127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // already exists.
6137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    list->AppendIfNotPresent(id.release());
6147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  } else {
6157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    list->Remove(*id, NULL);
616eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
617c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
618c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
6198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DesktopNotificationService::ShowWelcomeNotificationIfNecessary(
6208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const Notification& notification) {
6218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!welcome_notification && message_center::IsRichNotificationEnabled()) {
6228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    welcome_notification.reset(
6238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        new WelcomeNotification(profile_, g_browser_process->message_center()));
6248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
6258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
6268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (welcome_notification)
6278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    welcome_notification->ShowWelcomeNotificationIfNecessary(notification);
6288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
6298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
630eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid DesktopNotificationService::OnStringListPrefChanged(
631eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const char* pref_name, std::set<std::string>* ids_field) {
632eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ids_field->clear();
633eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  const base::ListValue* pref_list = profile_->GetPrefs()->GetList(pref_name);
634eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (size_t i = 0; i < pref_list->GetSize(); ++i) {
635eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    std::string element;
636eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (pref_list->GetString(i, &element) && !element.empty())
637eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      ids_field->insert(element);
638eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    else
639eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      LOG(WARNING) << i << "-th element is not a string for " << pref_name;
640eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
6412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
64390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void DesktopNotificationService::Observe(
64490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    int type,
64590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const content::NotificationSource& source,
64690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const content::NotificationDetails& details) {
64790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UNINSTALLED, type);
64890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
64990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  extensions::Extension* extension =
65090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      content::Details<extensions::Extension>(details).ptr();
651eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  NotifierId notifier_id(NotifierId::APPLICATION, extension->id());
652eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (IsNotifierEnabled(notifier_id))
65390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return;
65490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
655eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SetNotifierEnabled(notifier_id, true);
65690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
6571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
6581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void DesktopNotificationService::FirePermissionLevelChangedEvent(
6591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const NotifierId& notifier_id, bool enabled) {
6601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK_EQ(NotifierId::APPLICATION, notifier_id.type);
6611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  extensions::api::notifications::PermissionLevel permission =
6621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      enabled ? extensions::api::notifications::PERMISSION_LEVEL_GRANTED
6631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)              : extensions::api::notifications::PERMISSION_LEVEL_DENIED;
6641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  scoped_ptr<base::ListValue> args(new base::ListValue());
6651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  args->Append(new base::StringValue(
6661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      extensions::api::notifications::ToString(permission)));
6671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  scoped_ptr<extensions::Event> event(new extensions::Event(
6681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      extensions::api::notifications::OnPermissionLevelChanged::kEventName,
6691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      args.Pass()));
6701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  extensions::ExtensionSystem::Get(profile_)->event_router()->
6711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      DispatchEventToExtension(notifier_id.id, event.Pass());
672f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
673f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Tell the IO thread that this extension's permission for notifications
674f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // has changed.
675f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  extensions::InfoMap* extension_info_map =
676f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      extensions::ExtensionSystem::Get(profile_)->info_map();
677f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  BrowserThread::PostTask(
678f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      BrowserThread::IO, FROM_HERE,
679f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      base::Bind(&extensions::InfoMap::SetNotificationsDisabled,
680f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                 extension_info_map, notifier_id.id, !enabled));
681f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
6821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
683