message_center_notification_manager.cc revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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) {
167a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Only progress notification update can be reflected immediately in the
168a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // message center.
169a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  bool update_progress_notification =
170a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      notification.type() == message_center::NOTIFICATION_TYPE_PROGRESS;
171a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  bool is_message_center_visible = message_center_->IsMessageCenterVisible();
172a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (!update_progress_notification && is_message_center_visible)
173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
174868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const string16& replace_id = notification.replace_id();
176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (replace_id.empty())
177868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
178868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const GURL origin_url = notification.origin_url();
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(origin_url.is_valid());
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Since replace_id is provided by arbitrary JS, we need to use origin_url
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // (which is an app url in case of app/extension) to scope the replace ids
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // in the given profile.
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (NotificationMap::iterator iter = profile_notifications_.begin();
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       iter != profile_notifications_.end(); ++iter) {
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ProfileNotification* old_notification = (*iter).second;
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (old_notification->notification().replace_id() == replace_id &&
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        old_notification->notification().origin_url() == origin_url &&
190eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        old_notification->profile() == profile) {
191a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      // Changing the type from non-progress to progress does not count towards
192a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      // the immediate update allowed in the message center.
193a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      if (update_progress_notification && is_message_center_visible &&
194a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)          old_notification->notification().type() !=
195a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)              message_center::NOTIFICATION_TYPE_PROGRESS) {
196a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        return false;
197a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      }
198a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      std::string old_id =
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          old_notification->notification().notification_id();
201c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DCHECK(message_center_->HasNotification(old_id));
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Add/remove notification in the local list but just update the same
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // one in MessageCenter.
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      delete old_notification;
2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profile_notifications_.erase(old_id);
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ProfileNotification* new_notification =
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          new ProfileNotification(profile, notification, message_center_);
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profile_notifications_[notification.notification_id()] = new_notification;
210868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
211868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // Now pass a copy to message center.
212868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<message_center::Notification> message_center_notification(
213868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          make_scoped_ptr(new message_center::Notification(notification)));
214868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      message_center_notification->set_extension_id(
215868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          new_notification->GetExtensionId());
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      message_center_->UpdateNotification(old_id,
217868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                          message_center_notification.Pass());
218868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new_notification->StartDownloads();
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return true;
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// MessageCenter::Delegate
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::DisableExtension(
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& notification_id) {
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ProfileNotification* profile_notification =
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FindProfileNotification(notification_id);
233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!profile_notification)
234c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
235c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string extension_id = profile_notification->GetExtensionId();
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!extension_id.empty());  // or UI should not have enabled the command.
238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DesktopNotificationService* service =
239c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DesktopNotificationServiceFactory::GetForProfile(
240c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          profile_notification->profile());
241eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  message_center::NotifierId notifier_id(
242eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      message_center::NotifierId::APPLICATION, extension_id);
243eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  service->SetNotifierEnabled(notifier_id, false);
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::DisableNotificationsFromSource(
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& notification_id) {
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ProfileNotification* profile_notification =
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FindProfileNotification(notification_id);
250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!profile_notification)
251c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
252c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // UI should not have enabled the command if there is no valid source.
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile_notification->notification().origin_url().is_valid());
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DesktopNotificationService* service =
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DesktopNotificationServiceFactory::GetForProfile(
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          profile_notification->profile());
258c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (profile_notification->notification().origin_url().scheme() ==
259c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      chrome::kChromeUIScheme) {
260c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string name =
261c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        profile_notification->notification().origin_url().host();
262eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    message_center::NotifierId notifier_id(
263eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        message_center::ParseSystemComponentName(name));
264eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    service->SetNotifierEnabled(notifier_id, false);
265c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
266c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    service->DenyPermission(profile_notification->notification().origin_url());
267c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ShowSettings(
2712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& notification_id) {
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The per-message-center Settings button passes an empty string.
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (notification_id.empty()) {
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    NOTIMPLEMENTED();
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ProfileNotification* profile_notification =
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FindProfileNotification(notification_id);
280c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!profile_notification)
281c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
282c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Browser* browser =
2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      chrome::FindOrCreateTabbedBrowser(profile_notification->profile(),
2852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        chrome::HOST_DESKTOP_TYPE_NATIVE);
2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (profile_notification->GetExtensionId().empty())
2872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    chrome::ShowContentSettings(browser, CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  else
2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    chrome::ShowExtensions(browser, std::string());
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
292c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
293c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// MessageCenter::Observer
294c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void MessageCenterNotificationManager::OnNotificationRemoved(
295c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string& notification_id,
296c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bool by_user) {
297c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Do not call FindProfileNotification(). Some tests create notifications
298c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // directly to MessageCenter, but this method will be called for the removals
299c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // of such notifications.
300c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NotificationMap::const_iterator iter =
301c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      profile_notifications_.find(notification_id);
302c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (iter != profile_notifications_.end())
303c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    RemoveProfileNotification(iter->second, by_user);
304eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
305eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
306eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  CheckFirstRunTimer();
307eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
308c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
309c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
310868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void MessageCenterNotificationManager::OnNotificationCenterClosed() {
311868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // When the center is open it halts all notifications, so we need to listen
312868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // for events indicating it's been closed.
313868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  CheckAndShowNotifications();
314eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
315eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  CheckFirstRunTimer();
316eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
317eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
318eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
319eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid MessageCenterNotificationManager::OnNotificationUpdated(
320eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const std::string& notification_id) {
321eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
322eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  CheckFirstRunTimer();
323eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
324eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
325eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
326eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid MessageCenterNotificationManager::SetMessageCenterTrayDelegateForTest(
327eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    message_center::MessageCenterTrayDelegate* delegate) {
328eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  tray_.reset(delegate);
329868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
330868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ImageDownloads
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::ImageDownloads(
335c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    message_center::MessageCenter* message_center,
336c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ImageDownloadsObserver* observer)
337c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    : message_center_(message_center),
338c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      pending_downloads_(0),
339c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      observer_(observer) {
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::~ImageDownloads() { }
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::StartDownloads(
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification) {
346c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // In case all downloads are synchronous, assume a pending download.
347c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AddPendingDownload();
348c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification primary icon.
3502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StartDownloadWithImage(
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification,
3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &notification.icon(),
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification.icon_url(),
3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      message_center::kNotificationIconSize,
3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationIcon,
3562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
3572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 notification.notification_id()));
3582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification image.
360868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  StartDownloadWithImage(
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification,
362868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NULL,
363868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification.image_url(),
3642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      message_center::kNotificationPreferredImageSize,
3652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationImage,
3662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 notification.notification_id()));
3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification button icons.
370868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  StartDownloadWithImage(
3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification,
372868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NULL,
373868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification.button_one_icon_url(),
3742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      message_center::kNotificationButtonIconSize,
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon,
3762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
377868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 notification.notification_id(),
378868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 0));
379868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  StartDownloadWithImage(
380868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification,
381868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NULL,
382868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification.button_two_icon_url(),
3832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      message_center::kNotificationButtonIconSize,
3842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon,
3852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
386868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 notification.notification_id(),
387868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 1));
388c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
389c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // This should tell the observer we're done if everything was synchronous.
390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PendingDownloadCompleted();
3912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::StartDownloadWithImage(
3942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification,
3952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Image* image,
3962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& url,
3972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int size,
3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SetImageCallback& callback) {
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Set the image directly if we have it.
4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (image && !image->IsEmpty()) {
4012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    callback.Run(*image);
4022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
4032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Leave the image null if there's no URL.
4062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (url.is_empty())
4072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
4082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::RenderViewHost* host = notification.GetRenderViewHost();
4102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!host) {
4112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LOG(WARNING) << "Notification needs an image but has no RenderViewHost";
4122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
4132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::WebContents* contents =
4162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      content::WebContents::FromRenderViewHost(host);
4172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!contents) {
4182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LOG(WARNING) << "Notification needs an image but has no WebContents";
4192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
4202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
422c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AddPendingDownload();
423c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
424c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  contents->DownloadImage(
4252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      url,
426eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      false,  // Not a favicon
427eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      size,  // Preferred size
428eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      0,  // No maximum size
4292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(
4302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          &MessageCenterNotificationManager::ImageDownloads::DownloadComplete,
4312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          AsWeakPtr(),
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          callback));
4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::DownloadComplete(
4362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SetImageCallback& callback,
4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int download_id,
43890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    int http_status_code,
4392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& image_url,
4402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int requested_size,
4412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::vector<SkBitmap>& bitmaps) {
442c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PendingDownloadCompleted();
443c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (bitmaps.empty())
4452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
4462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmaps[0]);
4472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  callback.Run(image);
4482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
450c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Private methods.
451c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
452c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::AddPendingDownload() {
453c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ++pending_downloads_;
454c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
455c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
456c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void
457c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::PendingDownloadCompleted() {
458c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(pending_downloads_ > 0);
459c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (--pending_downloads_ == 0 && observer_)
460c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    observer_->OnDownloadsCompleted();
461c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
462c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
4642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ProfileNotification
4652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::ProfileNotification(
4672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Profile* profile,
4682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification,
4692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    message_center::MessageCenter* message_center)
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : profile_(profile),
4712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification_(notification),
472c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      downloads_(new ImageDownloads(message_center, this)) {
4732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile);
4742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::~ProfileNotification() {
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ProfileNotification::StartDownloads() {
4802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  downloads_->StartDownloads(notification_);
4812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
483c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void
484c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::OnDownloadsCompleted() {
485c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  notification_.DoneRendering();
486c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
487c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string
4892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MessageCenterNotificationManager::ProfileNotification::GetExtensionId() {
490c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ExtensionInfoMap* extension_info_map =
491c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      extensions::ExtensionSystem::Get(profile())->info_map();
492c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ExtensionSet extensions;
493c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin(
494c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      notification().origin_url(), notification().process_id(),
495c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      extensions::APIPermission::kNotification, &extensions);
496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DesktopNotificationService* desktop_service =
498c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DesktopNotificationServiceFactory::GetForProfile(profile());
499c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (ExtensionSet::const_iterator iter = extensions.begin();
500c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)       iter != extensions.end(); ++iter) {
501eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (desktop_service->IsNotifierEnabled(message_center::NotifierId(
502eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            message_center::NotifierId::APPLICATION, (*iter)->id()))) {
503c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return (*iter)->id();
504eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
505c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
506c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return std::string();
5072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
5102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// private
5112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::AddProfileNotification(
5132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ProfileNotification* profile_notification) {
5142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const Notification& notification = profile_notification->notification();
5152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string id = notification.notification_id();
5162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification ids should be unique.
5172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile_notifications_.find(id) == profile_notifications_.end());
5182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notifications_[id] = profile_notification;
5192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
520868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Create the copy for message center, and ensure the extension ID is correct.
521868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<message_center::Notification> message_center_notification(
522868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      new message_center::Notification(notification));
523868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  message_center_notification->set_extension_id(
524868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      profile_notification->GetExtensionId());
525868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  message_center_->AddNotification(message_center_notification.Pass());
526868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notification->StartDownloads();
5282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::RemoveProfileNotification(
5312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ProfileNotification* profile_notification,
5322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool by_user) {
5332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notification->notification().Close(by_user);
5342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string id = profile_notification->notification().notification_id();
5352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notifications_.erase(id);
5362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  delete profile_notification;
5372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification*
5402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MessageCenterNotificationManager::FindProfileNotification(
5412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        const std::string& id) const {
5422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NotificationMap::const_iterator iter = profile_notifications_.find(id);
543c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (iter == profile_notifications_.end())
544c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return NULL;
545c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return (*iter).second;
5472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
548