message_center_notification_manager.cc revision a02191e04bc25c4935f804f2c080ae28663d096d
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"
9a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "base/prefs/pref_registry_simple.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
112385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch#include "chrome/browser/chrome_notification_types.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/desktop_notification_service.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/desktop_notification_service_factory.h"
1458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/notifications/fullscreen_notification_blocker.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/message_center_settings_controller.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/notification.h"
1758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/notifications/screen_lock_notification_blocker.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/chrome_pages.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/host_desktop.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/pref_names.h"
232385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch#include "content/public/browser/notification_service.h"
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents.h"
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/common/url_constants.h"
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/extension_system.h"
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/browser/info_map.h"
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/common/extension_set.h"
29424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ui/gfx/image/image_skia.h"
3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/message_center/message_center_style.h"
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/message_center/message_center_tray.h"
3258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "ui/message_center/message_center_types.h"
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/message_center/notifier_settings.h"
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_CHROMEOS)
3658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/notifications/login_state_notification_blocker_chromeos.h"
37a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
3858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif
3958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(USE_ASH)
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ash/shell.h"
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ash/system/web_notification/web_notification_tray.h"
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#if defined(OS_WIN)
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// The first-run balloon will be shown |kFirstRunIdleDelaySeconds| after all
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// popups go away and the user has notifications in the message center.
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochconst int kFirstRunIdleDelaySeconds = 1;
4968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#endif
50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::MessageCenterNotificationManager(
52eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    message_center::MessageCenter* message_center,
53bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    PrefService* local_state,
54bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    scoped_ptr<message_center::NotifierSettingsProvider> settings_provider)
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : message_center_(message_center),
56eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
57eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      first_run_idle_timeout_(
58eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          base::TimeDelta::FromSeconds(kFirstRunIdleDelaySeconds)),
59eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      weak_factory_(this),
60eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
6158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      settings_provider_(settings_provider.Pass()),
6268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      system_observer_(this),
6368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      stats_collector_(message_center) {
64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
65eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  first_run_pref_.Init(prefs::kMessageCenterShowedFirstRunBalloon, local_state);
66eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
67eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  message_center_->AddObserver(this);
69bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  message_center_->SetNotifierSettingsProvider(settings_provider_.get());
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_CHROMEOS)
7258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blockers_.push_back(
7358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      new LoginStateNotificationBlockerChromeOS(message_center));
7458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#else
7558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blockers_.push_back(new ScreenLockNotificationBlocker(message_center));
7658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif
7758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blockers_.push_back(new FullscreenNotificationBlocker(message_center));
7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#if defined(OS_WIN) || defined(OS_MACOSX) \
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  || (defined(USE_AURA) && !defined(USE_ASH))
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // On Windows, Linux and Mac, the notification manager owns the tray icon and
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // views.Other platforms have global ownership and Create will return NULL.
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  tray_.reset(message_center::CreateMessageCenterTray());
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
852385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  registrar_.Add(this,
862385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch                 chrome::NOTIFICATION_FULLSCREEN_CHANGED,
872385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch                 content::NotificationService::AllSources());
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::~MessageCenterNotificationManager() {
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  message_center_->RemoveObserver(this);
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
94a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochvoid MessageCenterNotificationManager::RegisterPrefs(
95a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    PrefRegistrySimple* registry) {
96a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  registry->RegisterBooleanPref(prefs::kMessageCenterShowedFirstRunBalloon,
97a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                false);
98a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch}
99a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// NotificationUIManager
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void MessageCenterNotificationManager::Add(const Notification& notification,
10458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                           Profile* profile) {
10558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (Update(notification, profile))
10658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return;
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DesktopNotificationServiceFactory::GetForProfile(profile)->
1098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      ShowWelcomeNotificationIfNecessary(notification);
1108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AddProfileNotification(
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new ProfileNotification(profile, notification, message_center_));
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool MessageCenterNotificationManager::Update(const Notification& notification,
11658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                              Profile* profile) {
117a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const base::string16& replace_id = notification.replace_id();
118868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (replace_id.empty())
119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const GURL origin_url = notification.origin_url();
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(origin_url.is_valid());
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Since replace_id is provided by arbitrary JS, we need to use origin_url
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // (which is an app url in case of app/extension) to scope the replace ids
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // in the given profile.
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (NotificationMap::iterator iter = profile_notifications_.begin();
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       iter != profile_notifications_.end(); ++iter) {
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ProfileNotification* old_notification = (*iter).second;
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (old_notification->notification().replace_id() == replace_id &&
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        old_notification->notification().origin_url() == origin_url &&
132eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        old_notification->profile() == profile) {
133a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      // Changing the type from non-progress to progress does not count towards
134a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      // the immediate update allowed in the message center.
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      std::string old_id =
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          old_notification->notification().notification_id();
137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DCHECK(message_center_->HasNotification(old_id));
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Add/remove notification in the local list but just update the same
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // one in MessageCenter.
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      delete old_notification;
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profile_notifications_.erase(old_id);
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ProfileNotification* new_notification =
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          new ProfileNotification(profile, notification, message_center_);
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profile_notifications_[notification.notification_id()] = new_notification;
146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // Now pass a copy to message center.
148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<message_center::Notification> message_center_notification(
149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          make_scoped_ptr(new message_center::Notification(notification)));
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      message_center_->UpdateNotification(old_id,
151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                          message_center_notification.Pass());
152868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new_notification->StartDownloads();
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return true;
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)const Notification* MessageCenterNotificationManager::FindById(
16158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const std::string& id) const {
16258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  NotificationMap::const_iterator iter = profile_notifications_.find(id);
16358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (iter == profile_notifications_.end())
16458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return NULL;
16558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return &(iter->second->notification());
16658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool MessageCenterNotificationManager::CancelById(const std::string& id) {
16958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // See if this ID hasn't been shown yet.
17058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // If it has been shown, remove it.
17158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  NotificationMap::iterator iter = profile_notifications_.find(id);
17258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (iter == profile_notifications_.end())
17358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return false;
17458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
17568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RemoveProfileNotification(iter->second);
17658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  message_center_->RemoveNotification(id, /* by_user */ false);
17758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return true;
17858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
17958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
18058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)std::set<std::string>
18158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)MessageCenterNotificationManager::GetAllIdsByProfileAndSourceOrigin(
18258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    Profile* profile,
18358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const GURL& source) {
18458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
18558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::set<std::string> notification_ids;
18658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (NotificationMap::iterator iter = profile_notifications_.begin();
18758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)       iter != profile_notifications_.end(); iter++) {
18858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if ((*iter).second->notification().origin_url() == source &&
18958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        profile == (*iter).second->profile()) {
19058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      notification_ids.insert(iter->first);
19158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
19358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return notification_ids;
19458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool MessageCenterNotificationManager::CancelAllBySourceOrigin(
19758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const GURL& source) {
19858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Same pattern as CancelById, but more complicated than the above
19958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // because there may be multiple notifications from the same source.
20058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool removed = false;
201c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
20258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (NotificationMap::iterator loopiter = profile_notifications_.begin();
20358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)       loopiter != profile_notifications_.end(); ) {
20458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    NotificationMap::iterator curiter = loopiter++;
20558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if ((*curiter).second->notification().origin_url() == source) {
20668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      const std::string id = curiter->first;
20768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      RemoveProfileNotification(curiter->second);
20868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      message_center_->RemoveNotification(id, /* by_user */ false);
20958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      removed = true;
21058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
21158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
21258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return removed;
21358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
21458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
21558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool MessageCenterNotificationManager::CancelAllByProfile(Profile* profile) {
21658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Same pattern as CancelAllBySourceOrigin.
21758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool removed = false;
21858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
21958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (NotificationMap::iterator loopiter = profile_notifications_.begin();
22058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)       loopiter != profile_notifications_.end(); ) {
22158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    NotificationMap::iterator curiter = loopiter++;
22258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (profile == (*curiter).second->profile()) {
22368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      const std::string id = curiter->first;
22468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      RemoveProfileNotification(curiter->second);
22568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      message_center_->RemoveNotification(id, /* by_user */ false);
22658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      removed = true;
22758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
22858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
22958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return removed;
23058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
23158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
23258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void MessageCenterNotificationManager::CancelAll() {
23358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  message_center_->RemoveAllNotifications(/* by_user */ false);
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
236c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
237c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// MessageCenter::Observer
238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void MessageCenterNotificationManager::OnNotificationRemoved(
239c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string& notification_id,
240c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bool by_user) {
241c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NotificationMap::const_iterator iter =
242c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      profile_notifications_.find(notification_id);
243c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (iter != profile_notifications_.end())
244ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    RemoveProfileNotification(iter->second);
245eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
246eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
247eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  CheckFirstRunTimer();
248eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
25158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void MessageCenterNotificationManager::OnCenterVisibilityChanged(
25258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    message_center::Visibility visibility) {
253eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
25468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  if (visibility == message_center::VISIBILITY_TRANSIENT)
25568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    CheckFirstRunTimer();
256eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
257eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
258eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
259eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid MessageCenterNotificationManager::OnNotificationUpdated(
260eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const std::string& notification_id) {
261eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
262eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  CheckFirstRunTimer();
263eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
264eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
265eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void MessageCenterNotificationManager::EnsureMessageCenterClosed() {
2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (tray_.get())
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    tray_->GetMessageCenterTray()->HideMessageCenterBubble();
2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(USE_ASH)
2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (ash::Shell::HasInstance()) {
2725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ash::WebNotificationTray* tray =
2735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        ash::Shell::GetInstance()->GetWebNotificationTray();
2745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (tray)
2755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      tray->GetMessageCenterTray()->HideMessageCenterBubble();
2765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
2785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
280eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid MessageCenterNotificationManager::SetMessageCenterTrayDelegateForTest(
281eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    message_center::MessageCenterTrayDelegate* delegate) {
282eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  tray_.reset(delegate);
283868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
284868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2852385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochvoid MessageCenterNotificationManager::Observe(
2862385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    int type,
2872385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    const content::NotificationSource& source,
2882385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    const content::NotificationDetails& details) {
2892385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  if (type == chrome::NOTIFICATION_FULLSCREEN_CHANGED) {
2902385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    const bool is_fullscreen = *content::Details<bool>(details).ptr();
2912385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
2922385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    if (is_fullscreen && tray_.get() && tray_->GetMessageCenterTray())
2932385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      tray_->GetMessageCenterTray()->HidePopupBubble();
2942385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  }
2952385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch}
2962385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ImageDownloads
2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::ImageDownloads(
301c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    message_center::MessageCenter* message_center,
302c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ImageDownloadsObserver* observer)
303c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    : message_center_(message_center),
304c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      pending_downloads_(0),
305c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      observer_(observer) {
3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::~ImageDownloads() { }
3092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::StartDownloads(
3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification) {
312c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // In case all downloads are synchronous, assume a pending download.
313c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AddPendingDownload();
314c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification primary icon.
3162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StartDownloadWithImage(
3172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification,
3182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &notification.icon(),
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification.icon_url(),
3202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationIcon,
3212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
3222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 notification.notification_id()));
3232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification image.
325868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  StartDownloadWithImage(
3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification,
327868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NULL,
328868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification.image_url(),
3292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationImage,
3302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 notification.notification_id()));
3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification button icons.
334868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  StartDownloadWithImage(
3352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification,
336868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NULL,
337868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification.button_one_icon_url(),
3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon,
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
340868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 notification.notification_id(),
341868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 0));
342868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  StartDownloadWithImage(
343868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification,
344868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NULL,
345868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification.button_two_icon_url(),
3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon,
3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
348868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 notification.notification_id(),
349868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 1));
350c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
351c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // This should tell the observer we're done if everything was synchronous.
352c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PendingDownloadCompleted();
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::StartDownloadWithImage(
3562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification,
3572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Image* image,
3582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& url,
3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SetImageCallback& callback) {
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Set the image directly if we have it.
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (image && !image->IsEmpty()) {
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    callback.Run(*image);
3632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Leave the image null if there's no URL.
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (url.is_empty())
3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::RenderViewHost* host = notification.GetRenderViewHost();
3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!host) {
3722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LOG(WARNING) << "Notification needs an image but has no RenderViewHost";
3732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::WebContents* contents =
3772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      content::WebContents::FromRenderViewHost(host);
3782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!contents) {
3792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LOG(WARNING) << "Notification needs an image but has no WebContents";
3802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
383c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AddPendingDownload();
384c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
385c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  contents->DownloadImage(
3862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      url,
387eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      false,  // Not a favicon
388eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      0,  // No maximum size
3892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(
3902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          &MessageCenterNotificationManager::ImageDownloads::DownloadComplete,
3912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          AsWeakPtr(),
3922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          callback));
3932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::DownloadComplete(
3962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SetImageCallback& callback,
3972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int download_id,
39890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    int http_status_code,
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& image_url,
400d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const std::vector<SkBitmap>& bitmaps,
401d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const std::vector<gfx::Size>& original_bitmap_sizes) {
402c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PendingDownloadCompleted();
403c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (bitmaps.empty())
4052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
4062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmaps[0]);
4072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  callback.Run(image);
4082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
410c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Private methods.
411c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
412c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::AddPendingDownload() {
413c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ++pending_downloads_;
414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
415c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
416c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void
417c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::PendingDownloadCompleted() {
418c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(pending_downloads_ > 0);
419c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (--pending_downloads_ == 0 && observer_)
420c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    observer_->OnDownloadsCompleted();
421c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
422c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
4242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ProfileNotification
4252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::ProfileNotification(
4272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Profile* profile,
4282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification,
4292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    message_center::MessageCenter* message_center)
4302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : profile_(profile),
4312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification_(notification),
432c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      downloads_(new ImageDownloads(message_center, this)) {
4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile);
434a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#if defined(OS_CHROMEOS)
435a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  notification_.set_profile_id(multi_user_util::GetUserIDFromProfile(profile));
436a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#endif
4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::~ProfileNotification() {
4402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ProfileNotification::StartDownloads() {
4432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  downloads_->StartDownloads(notification_);
4442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
446c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void
447c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::OnDownloadsCompleted() {
448c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  notification_.DoneRendering();
449c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
450c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string
4522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MessageCenterNotificationManager::ProfileNotification::GetExtensionId() {
453f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  extensions::InfoMap* extension_info_map =
454c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      extensions::ExtensionSystem::Get(profile())->info_map();
4555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  extensions::ExtensionSet extensions;
456c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin(
457c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      notification().origin_url(), notification().process_id(),
458c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      extensions::APIPermission::kNotification, &extensions);
459c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
460c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DesktopNotificationService* desktop_service =
461c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DesktopNotificationServiceFactory::GetForProfile(profile());
4625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (extensions::ExtensionSet::const_iterator iter = extensions.begin();
463c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)       iter != extensions.end(); ++iter) {
464eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (desktop_service->IsNotifierEnabled(message_center::NotifierId(
465eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            message_center::NotifierId::APPLICATION, (*iter)->id()))) {
466c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return (*iter)->id();
467eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
468c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
469c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return std::string();
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
4732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// private
4742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::AddProfileNotification(
4762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ProfileNotification* profile_notification) {
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const Notification& notification = profile_notification->notification();
4782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string id = notification.notification_id();
4792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification ids should be unique.
4802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile_notifications_.find(id) == profile_notifications_.end());
4812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notifications_[id] = profile_notification;
4822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
483868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Create the copy for message center, and ensure the extension ID is correct.
484868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<message_center::Notification> message_center_notification(
485868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      new message_center::Notification(notification));
486868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  message_center_->AddNotification(message_center_notification.Pass());
487868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
4882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notification->StartDownloads();
4892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::RemoveProfileNotification(
492ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    ProfileNotification* profile_notification) {
4932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string id = profile_notification->notification().notification_id();
4942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notifications_.erase(id);
4952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  delete profile_notification;
4962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification*
4992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MessageCenterNotificationManager::FindProfileNotification(
5002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        const std::string& id) const {
5012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NotificationMap::const_iterator iter = profile_notifications_.find(id);
502c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (iter == profile_notifications_.end())
503c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return NULL;
504c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return (*iter).second;
5062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
507