message_center_notification_manager.cc revision 558790d6acca3451cf3a6b497803a5f07d0bec58
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)#include "chrome/browser/notifications/message_center_notification_manager.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/logging.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/extensions/extension_info_map.h"
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/extensions/extension_system.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/desktop_notification_service.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/desktop_notification_service_factory.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/message_center_settings_controller.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/notification.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/chrome_pages.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/host_desktop.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/extensions/extension_set.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/pref_names.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents.h"
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/common/url_constants.h"
2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/message_center/message_center_style.h"
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/message_center/message_center_tray.h"
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/message_center/notifier_settings.h"
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace {
29eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// The first-run balloon will be shown |kFirstRunIdleDelaySeconds| after all
30eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// popups go away and the user has notifications in the message center.
31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochconst int kFirstRunIdleDelaySeconds = 1;
32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}  // namespace
33eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::MessageCenterNotificationManager(
35eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    message_center::MessageCenter* message_center,
36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    PrefService* local_state)
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : message_center_(message_center),
38eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
39eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      first_run_idle_timeout_(
40eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          base::TimeDelta::FromSeconds(kFirstRunIdleDelaySeconds)),
41eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      weak_factory_(this),
42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      settings_controller_(new MessageCenterSettingsController) {
44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  first_run_pref_.Init(prefs::kMessageCenterShowedFirstRunBalloon, local_state);
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  message_center_->SetDelegate(this);
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  message_center_->AddObserver(this);
50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  message_center_->SetNotifierSettingsProvider(settings_controller_.get());
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#if defined(OS_WIN) || defined(OS_MACOSX) \
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  || (defined(USE_AURA) && !defined(USE_ASH))
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // On Windows, Linux and Mac, the notification manager owns the tray icon and
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // views.Other platforms have global ownership and Create will return NULL.
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  tray_.reset(message_center::CreateMessageCenterTray());
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::~MessageCenterNotificationManager() {
61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  message_center_->RemoveObserver(this);
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// NotificationUIManager
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
67558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdochconst Notification* MessageCenterNotificationManager::FindById(
68558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    const std::string& id) const {
69558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  const Notification* notification = NotificationUIManagerImpl::FindById(id);
70558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  if (notification)
71558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    return notification;
72558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  NotificationMap::const_iterator iter = profile_notifications_.find(id);
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (iter == profile_notifications_.end())
74558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    return NULL;
75558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  return &(iter->second->notification());
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool MessageCenterNotificationManager::CancelById(const std::string& id) {
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // See if this ID hasn't been shown yet.
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (NotificationUIManagerImpl::CancelById(id))
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return true;
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If it has been shown, remove it.
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NotificationMap::iterator iter = profile_notifications_.find(id);
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (iter == profile_notifications_.end())
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  message_center_->RemoveNotification(id, /* by_user */ false);
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return true;
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)std::set<std::string>
93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)MessageCenterNotificationManager::GetAllIdsByProfileAndSourceOrigin(
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    Profile* profile,
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const GURL& source) {
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  std::set<std::string> notification_ids =
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NotificationUIManagerImpl::GetAllIdsByProfileAndSourceOrigin(profile,
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                                                   source);
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  for (NotificationMap::iterator iter = profile_notifications_.begin();
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)       iter != profile_notifications_.end(); iter++) {
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if ((*iter).second->notification().origin_url() == source &&
104eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        profile == (*iter).second->profile()) {
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification_ids.insert(iter->first);
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return notification_ids;
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool MessageCenterNotificationManager::CancelAllBySourceOrigin(
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& source) {
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Same pattern as CancelById, but more complicated than the above
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // because there may be multiple notifications from the same source.
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool removed = NotificationUIManagerImpl::CancelAllBySourceOrigin(source);
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (NotificationMap::iterator loopiter = profile_notifications_.begin();
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       loopiter != profile_notifications_.end(); ) {
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    NotificationMap::iterator curiter = loopiter++;
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if ((*curiter).second->notification().origin_url() == source) {
121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      message_center_->RemoveNotification(curiter->first, /* by_user */ false);
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      removed = true;
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return removed;
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool MessageCenterNotificationManager::CancelAllByProfile(Profile* profile) {
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Same pattern as CancelAllBySourceOrigin.
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool removed = NotificationUIManagerImpl::CancelAllByProfile(profile);
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (NotificationMap::iterator loopiter = profile_notifications_.begin();
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       loopiter != profile_notifications_.end(); ) {
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    NotificationMap::iterator curiter = loopiter++;
135eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (profile == (*curiter).second->profile()) {
136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      message_center_->RemoveNotification(curiter->first, /* by_user */ false);
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      removed = true;
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return removed;
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::CancelAll() {
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NotificationUIManagerImpl::CancelAll();
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
146c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  message_center_->RemoveAllNotifications(/* by_user */ false);
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// NotificationUIManagerImpl
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool MessageCenterNotificationManager::ShowNotification(
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification, Profile* profile) {
154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (message_center_->IsMessageCenterVisible())
155868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
156868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
157868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (UpdateNotification(notification, profile))
158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return true;
159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AddProfileNotification(
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new ProfileNotification(profile, notification, message_center_));
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return true;
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool MessageCenterNotificationManager::UpdateNotification(
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification, Profile* profile) {
167868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (message_center_->IsMessageCenterVisible())
168868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
169868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const string16& replace_id = notification.replace_id();
171868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (replace_id.empty())
172868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const GURL origin_url = notification.origin_url();
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(origin_url.is_valid());
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Since replace_id is provided by arbitrary JS, we need to use origin_url
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // (which is an app url in case of app/extension) to scope the replace ids
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // in the given profile.
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (NotificationMap::iterator iter = profile_notifications_.begin();
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       iter != profile_notifications_.end(); ++iter) {
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ProfileNotification* old_notification = (*iter).second;
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (old_notification->notification().replace_id() == replace_id &&
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        old_notification->notification().origin_url() == origin_url &&
185eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        old_notification->profile() == profile) {
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      std::string old_id =
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          old_notification->notification().notification_id();
188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DCHECK(message_center_->HasNotification(old_id));
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Add/remove notification in the local list but just update the same
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // one in MessageCenter.
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      old_notification->notification().Close(false); // Not by user.
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      delete old_notification;
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profile_notifications_.erase(old_id);
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ProfileNotification* new_notification =
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          new ProfileNotification(profile, notification, message_center_);
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profile_notifications_[notification.notification_id()] = new_notification;
198868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
199868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // Now pass a copy to message center.
200868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<message_center::Notification> message_center_notification(
201868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          make_scoped_ptr(new message_center::Notification(notification)));
202868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      message_center_notification->set_extension_id(
203868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new_notification->GetExtensionId());
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      message_center_->UpdateNotification(old_id,
205868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                          message_center_notification.Pass());
206868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new_notification->StartDownloads();
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return true;
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// MessageCenter::Delegate
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::DisableExtension(
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& notification_id) {
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ProfileNotification* profile_notification =
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FindProfileNotification(notification_id);
221c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!profile_notification)
222c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
223c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string extension_id = profile_notification->GetExtensionId();
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!extension_id.empty());  // or UI should not have enabled the command.
226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DesktopNotificationService* service =
227c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DesktopNotificationServiceFactory::GetForProfile(
228c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          profile_notification->profile());
229eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  message_center::NotifierId notifier_id(
230eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      message_center::NotifierId::APPLICATION, extension_id);
231eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  service->SetNotifierEnabled(notifier_id, false);
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::DisableNotificationsFromSource(
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& notification_id) {
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ProfileNotification* profile_notification =
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FindProfileNotification(notification_id);
238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!profile_notification)
239c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
240c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // UI should not have enabled the command if there is no valid source.
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile_notification->notification().origin_url().is_valid());
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DesktopNotificationService* service =
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DesktopNotificationServiceFactory::GetForProfile(
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          profile_notification->profile());
246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (profile_notification->notification().origin_url().scheme() ==
247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      chrome::kChromeUIScheme) {
248c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string name =
249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        profile_notification->notification().origin_url().host();
250eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    message_center::NotifierId notifier_id(
251eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        message_center::ParseSystemComponentName(name));
252eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    service->SetNotifierEnabled(notifier_id, false);
253c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
254c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    service->DenyPermission(profile_notification->notification().origin_url());
255c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ShowSettings(
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& notification_id) {
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The per-message-center Settings button passes an empty string.
2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (notification_id.empty()) {
2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    NOTIMPLEMENTED();
2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ProfileNotification* profile_notification =
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FindProfileNotification(notification_id);
268c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!profile_notification)
269c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
270c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser* browser =
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      chrome::FindOrCreateTabbedBrowser(profile_notification->profile(),
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        chrome::HOST_DESKTOP_TYPE_NATIVE);
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (profile_notification->GetExtensionId().empty())
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    chrome::ShowContentSettings(browser, CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  else
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    chrome::ShowExtensions(browser, std::string());
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
280c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
281c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// MessageCenter::Observer
282c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void MessageCenterNotificationManager::OnNotificationRemoved(
283c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string& notification_id,
284c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bool by_user) {
285c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Do not call FindProfileNotification(). Some tests create notifications
286c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // directly to MessageCenter, but this method will be called for the removals
287c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // of such notifications.
288c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NotificationMap::const_iterator iter =
289c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      profile_notifications_.find(notification_id);
290c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (iter != profile_notifications_.end())
291c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    RemoveProfileNotification(iter->second, by_user);
292eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
293eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
294eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  CheckFirstRunTimer();
295eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
296c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
297c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
298868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void MessageCenterNotificationManager::OnNotificationCenterClosed() {
299868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // When the center is open it halts all notifications, so we need to listen
300868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // for events indicating it's been closed.
301868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  CheckAndShowNotifications();
302eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
303eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  CheckFirstRunTimer();
304eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
305eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
306eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
307eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid MessageCenterNotificationManager::OnNotificationUpdated(
308eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const std::string& notification_id) {
309eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
310eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  CheckFirstRunTimer();
311eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
312eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
313eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
314eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid MessageCenterNotificationManager::SetMessageCenterTrayDelegateForTest(
315eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    message_center::MessageCenterTrayDelegate* delegate) {
316eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  tray_.reset(delegate);
317868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
318868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
3202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ImageDownloads
3212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::ImageDownloads(
323c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    message_center::MessageCenter* message_center,
324c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ImageDownloadsObserver* observer)
325c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    : message_center_(message_center),
326c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      pending_downloads_(0),
327c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      observer_(observer) {
3282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::~ImageDownloads() { }
3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::StartDownloads(
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification) {
334c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // In case all downloads are synchronous, assume a pending download.
335c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AddPendingDownload();
336c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification primary icon.
3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StartDownloadWithImage(
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification,
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &notification.icon(),
3412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification.icon_url(),
3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      message_center::kNotificationIconSize,
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationIcon,
3442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 notification.notification_id()));
3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification image.
348868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  StartDownloadWithImage(
3492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification,
350868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NULL,
351868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification.image_url(),
3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      message_center::kNotificationPreferredImageSize,
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationImage,
3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 notification.notification_id()));
3562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification button icons.
358868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  StartDownloadWithImage(
3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification,
360868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NULL,
361868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification.button_one_icon_url(),
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      message_center::kNotificationButtonIconSize,
3632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon,
3642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
365868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 notification.notification_id(),
366868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 0));
367868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  StartDownloadWithImage(
368868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification,
369868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NULL,
370868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification.button_two_icon_url(),
3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      message_center::kNotificationButtonIconSize,
3722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon,
3732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
374868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 notification.notification_id(),
375868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 1));
376c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
377c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // This should tell the observer we're done if everything was synchronous.
378c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PendingDownloadCompleted();
3792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::StartDownloadWithImage(
3822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification,
3832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Image* image,
3842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& url,
3852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int size,
3862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SetImageCallback& callback) {
3872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Set the image directly if we have it.
3882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (image && !image->IsEmpty()) {
3892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    callback.Run(*image);
3902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Leave the image null if there's no URL.
3942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (url.is_empty())
3952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::RenderViewHost* host = notification.GetRenderViewHost();
3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!host) {
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LOG(WARNING) << "Notification needs an image but has no RenderViewHost";
4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
4012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::WebContents* contents =
4042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      content::WebContents::FromRenderViewHost(host);
4052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!contents) {
4062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LOG(WARNING) << "Notification needs an image but has no WebContents";
4072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
4082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
410c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AddPendingDownload();
411c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
412c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  contents->DownloadImage(
4132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      url,
414eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      false,  // Not a favicon
415eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      size,  // Preferred size
416eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      0,  // No maximum size
4172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(
4182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          &MessageCenterNotificationManager::ImageDownloads::DownloadComplete,
4192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          AsWeakPtr(),
4202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          callback));
4212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::DownloadComplete(
4242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SetImageCallback& callback,
4252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int download_id,
42690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    int http_status_code,
4272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& image_url,
4282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int requested_size,
4292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::vector<SkBitmap>& bitmaps) {
430c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PendingDownloadCompleted();
431c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (bitmaps.empty())
4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
4342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmaps[0]);
4352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  callback.Run(image);
4362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
438c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Private methods.
439c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
440c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::AddPendingDownload() {
441c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ++pending_downloads_;
442c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
443c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
444c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void
445c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::PendingDownloadCompleted() {
446c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(pending_downloads_ > 0);
447c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (--pending_downloads_ == 0 && observer_)
448c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    observer_->OnDownloadsCompleted();
449c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
450c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
4522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ProfileNotification
4532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::ProfileNotification(
4552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Profile* profile,
4562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification,
4572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    message_center::MessageCenter* message_center)
4582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : profile_(profile),
4592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification_(notification),
460c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      downloads_(new ImageDownloads(message_center, this)) {
4612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile);
4622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::~ProfileNotification() {
4652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ProfileNotification::StartDownloads() {
4682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  downloads_->StartDownloads(notification_);
4692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
471c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void
472c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::OnDownloadsCompleted() {
473c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  notification_.DoneRendering();
474c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
475c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MessageCenterNotificationManager::ProfileNotification::GetExtensionId() {
478c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ExtensionInfoMap* extension_info_map =
479c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      extensions::ExtensionSystem::Get(profile())->info_map();
480c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ExtensionSet extensions;
481c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin(
482c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      notification().origin_url(), notification().process_id(),
483c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      extensions::APIPermission::kNotification, &extensions);
484c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
485c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DesktopNotificationService* desktop_service =
486c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DesktopNotificationServiceFactory::GetForProfile(profile());
487c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (ExtensionSet::const_iterator iter = extensions.begin();
488c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)       iter != extensions.end(); ++iter) {
489eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (desktop_service->IsNotifierEnabled(message_center::NotifierId(
490eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            message_center::NotifierId::APPLICATION, (*iter)->id()))) {
491c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return (*iter)->id();
492eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
493c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
494c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return std::string();
4952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
4982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// private
4992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::AddProfileNotification(
5012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ProfileNotification* profile_notification) {
5022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const Notification& notification = profile_notification->notification();
5032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string id = notification.notification_id();
5042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification ids should be unique.
5052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile_notifications_.find(id) == profile_notifications_.end());
5062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notifications_[id] = profile_notification;
5072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
508868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Create the copy for message center, and ensure the extension ID is correct.
509868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<message_center::Notification> message_center_notification(
510868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      new message_center::Notification(notification));
511868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  message_center_notification->set_extension_id(
512868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      profile_notification->GetExtensionId());
513868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  message_center_->AddNotification(message_center_notification.Pass());
514868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notification->StartDownloads();
5162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::RemoveProfileNotification(
5192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ProfileNotification* profile_notification,
5202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool by_user) {
5212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notification->notification().Close(by_user);
5222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string id = profile_notification->notification().notification_id();
5232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notifications_.erase(id);
5242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  delete profile_notification;
5252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification*
5282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MessageCenterNotificationManager::FindProfileNotification(
5292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        const std::string& id) const {
5302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NotificationMap::const_iterator iter = profile_notifications_.find(id);
531c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (iter == profile_notifications_.end())
532c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return NULL;
533c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return (*iter).second;
5352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
536