message_center_notification_manager.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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"
102385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch#include "chrome/browser/chrome_notification_types.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/desktop_notification_service.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/desktop_notification_service_factory.h"
1358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/notifications/fullscreen_notification_blocker.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/message_center_settings_controller.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/notification.h"
1658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/notifications/screen_lock_notification_blocker.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/chrome_pages.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/host_desktop.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/pref_names.h"
222385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch#include "content/public/browser/notification_service.h"
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents.h"
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/common/url_constants.h"
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/extension_system.h"
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/browser/info_map.h"
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/common/extension_set.h"
28424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ui/gfx/image/image_skia.h"
2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/message_center/message_center_style.h"
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/message_center/message_center_tray.h"
3158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "ui/message_center/message_center_types.h"
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/message_center/notifier_settings.h"
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_CHROMEOS)
3558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/notifications/login_state_notification_blocker_chromeos.h"
36a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
3758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif
3858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(USE_ASH)
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ash/shell.h"
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ash/system/web_notification/web_notification_tray.h"
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#if defined(OS_WIN)
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// The first-run balloon will be shown |kFirstRunIdleDelaySeconds| after all
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// popups go away and the user has notifications in the message center.
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochconst int kFirstRunIdleDelaySeconds = 1;
4868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#endif
49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::MessageCenterNotificationManager(
51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    message_center::MessageCenter* message_center,
52bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    PrefService* local_state,
53bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    scoped_ptr<message_center::NotifierSettingsProvider> settings_provider)
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : message_center_(message_center),
55eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
56eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      first_run_idle_timeout_(
57eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          base::TimeDelta::FromSeconds(kFirstRunIdleDelaySeconds)),
58eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      weak_factory_(this),
59eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
6058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      settings_provider_(settings_provider.Pass()),
6168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      system_observer_(this),
6268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      stats_collector_(message_center) {
63eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  first_run_pref_.Init(prefs::kMessageCenterShowedFirstRunBalloon, local_state);
65eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
66eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  message_center_->AddObserver(this);
68bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  message_center_->SetNotifierSettingsProvider(settings_provider_.get());
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_CHROMEOS)
7158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blockers_.push_back(
7258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      new LoginStateNotificationBlockerChromeOS(message_center));
7358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#else
7458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blockers_.push_back(new ScreenLockNotificationBlocker(message_center));
7558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif
7658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blockers_.push_back(new FullscreenNotificationBlocker(message_center));
7758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#if defined(OS_WIN) || defined(OS_MACOSX) \
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  || (defined(USE_AURA) && !defined(USE_ASH))
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // On Windows, Linux and Mac, the notification manager owns the tray icon and
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // views.Other platforms have global ownership and Create will return NULL.
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  tray_.reset(message_center::CreateMessageCenterTray());
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
842385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  registrar_.Add(this,
852385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch                 chrome::NOTIFICATION_FULLSCREEN_CHANGED,
862385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch                 content::NotificationService::AllSources());
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::~MessageCenterNotificationManager() {
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  message_center_->RemoveObserver(this);
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// NotificationUIManager
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void MessageCenterNotificationManager::Add(const Notification& notification,
9758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                           Profile* profile) {
9858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (Update(notification, profile))
9958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return;
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DesktopNotificationServiceFactory::GetForProfile(profile)->
1028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      ShowWelcomeNotificationIfNecessary(notification);
1038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AddProfileNotification(
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new ProfileNotification(profile, notification, message_center_));
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool MessageCenterNotificationManager::Update(const Notification& notification,
10958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                              Profile* profile) {
110a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const base::string16& replace_id = notification.replace_id();
111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (replace_id.empty())
112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const GURL origin_url = notification.origin_url();
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(origin_url.is_valid());
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Since replace_id is provided by arbitrary JS, we need to use origin_url
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // (which is an app url in case of app/extension) to scope the replace ids
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // in the given profile.
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (NotificationMap::iterator iter = profile_notifications_.begin();
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       iter != profile_notifications_.end(); ++iter) {
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ProfileNotification* old_notification = (*iter).second;
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (old_notification->notification().replace_id() == replace_id &&
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        old_notification->notification().origin_url() == origin_url &&
125eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        old_notification->profile() == profile) {
126a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      // Changing the type from non-progress to progress does not count towards
127a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      // the immediate update allowed in the message center.
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      std::string old_id =
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          old_notification->notification().notification_id();
130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DCHECK(message_center_->HasNotification(old_id));
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Add/remove notification in the local list but just update the same
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // one in MessageCenter.
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      delete old_notification;
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profile_notifications_.erase(old_id);
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ProfileNotification* new_notification =
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          new ProfileNotification(profile, notification, message_center_);
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profile_notifications_[notification.notification_id()] = new_notification;
139868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
140868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // Now pass a copy to message center.
141868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      scoped_ptr<message_center::Notification> message_center_notification(
142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          make_scoped_ptr(new message_center::Notification(notification)));
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      message_center_->UpdateNotification(old_id,
144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                          message_center_notification.Pass());
145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new_notification->StartDownloads();
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return true;
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)const Notification* MessageCenterNotificationManager::FindById(
15458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const std::string& id) const {
15558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  NotificationMap::const_iterator iter = profile_notifications_.find(id);
15658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (iter == profile_notifications_.end())
15758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return NULL;
15858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return &(iter->second->notification());
15958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool MessageCenterNotificationManager::CancelById(const std::string& id) {
16258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // See if this ID hasn't been shown yet.
16358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // If it has been shown, remove it.
16458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  NotificationMap::iterator iter = profile_notifications_.find(id);
16558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (iter == profile_notifications_.end())
16658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return false;
16758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
16868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RemoveProfileNotification(iter->second);
16958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  message_center_->RemoveNotification(id, /* by_user */ false);
17058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return true;
17158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
17258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
17358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)std::set<std::string>
17458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)MessageCenterNotificationManager::GetAllIdsByProfileAndSourceOrigin(
17558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    Profile* profile,
17658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const GURL& source) {
17758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
17858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::set<std::string> notification_ids;
17958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (NotificationMap::iterator iter = profile_notifications_.begin();
18058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)       iter != profile_notifications_.end(); iter++) {
18158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if ((*iter).second->notification().origin_url() == source &&
18258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        profile == (*iter).second->profile()) {
18358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      notification_ids.insert(iter->first);
18458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return notification_ids;
18758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool MessageCenterNotificationManager::CancelAllBySourceOrigin(
19058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const GURL& source) {
19158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Same pattern as CancelById, but more complicated than the above
19258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // because there may be multiple notifications from the same source.
19358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool removed = false;
194c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
19558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (NotificationMap::iterator loopiter = profile_notifications_.begin();
19658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)       loopiter != profile_notifications_.end(); ) {
19758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    NotificationMap::iterator curiter = loopiter++;
19858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if ((*curiter).second->notification().origin_url() == source) {
19968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      const std::string id = curiter->first;
20068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      RemoveProfileNotification(curiter->second);
20168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      message_center_->RemoveNotification(id, /* by_user */ false);
20258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      removed = true;
20358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
20458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
20558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return removed;
20658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
20758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
20858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool MessageCenterNotificationManager::CancelAllByProfile(Profile* profile) {
20958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Same pattern as CancelAllBySourceOrigin.
21058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool removed = false;
21158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
21258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (NotificationMap::iterator loopiter = profile_notifications_.begin();
21358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)       loopiter != profile_notifications_.end(); ) {
21458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    NotificationMap::iterator curiter = loopiter++;
21558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (profile == (*curiter).second->profile()) {
21668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      const std::string id = curiter->first;
21768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      RemoveProfileNotification(curiter->second);
21868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      message_center_->RemoveNotification(id, /* by_user */ false);
21958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      removed = true;
22058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
22158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
22258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return removed;
22358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
22458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
22558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void MessageCenterNotificationManager::CancelAll() {
22658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  message_center_->RemoveAllNotifications(/* by_user */ false);
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
229c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
230c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// MessageCenter::Observer
231c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void MessageCenterNotificationManager::OnNotificationRemoved(
232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string& notification_id,
233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bool by_user) {
234c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NotificationMap::const_iterator iter =
235c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      profile_notifications_.find(notification_id);
236c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (iter != profile_notifications_.end())
237ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    RemoveProfileNotification(iter->second);
238eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
239eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
240eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  CheckFirstRunTimer();
241eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
242c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
243c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
24458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void MessageCenterNotificationManager::OnCenterVisibilityChanged(
24558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    message_center::Visibility visibility) {
246eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
24768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  if (visibility == message_center::VISIBILITY_TRANSIENT)
24868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    CheckFirstRunTimer();
249eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
250eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
251eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
252eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid MessageCenterNotificationManager::OnNotificationUpdated(
253eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const std::string& notification_id) {
254eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
255eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  CheckFirstRunTimer();
256eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
257eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
258eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void MessageCenterNotificationManager::EnsureMessageCenterClosed() {
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (tray_.get())
2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    tray_->GetMessageCenterTray()->HideMessageCenterBubble();
2625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(USE_ASH)
2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (ash::Shell::HasInstance()) {
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ash::WebNotificationTray* tray =
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        ash::Shell::GetInstance()->GetWebNotificationTray();
2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (tray)
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      tray->GetMessageCenterTray()->HideMessageCenterBubble();
2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
273eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid MessageCenterNotificationManager::SetMessageCenterTrayDelegateForTest(
274eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    message_center::MessageCenterTrayDelegate* delegate) {
275eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  tray_.reset(delegate);
276868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
277868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2782385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochvoid MessageCenterNotificationManager::Observe(
2792385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    int type,
2802385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    const content::NotificationSource& source,
2812385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    const content::NotificationDetails& details) {
2822385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  if (type == chrome::NOTIFICATION_FULLSCREEN_CHANGED) {
2832385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    const bool is_fullscreen = *content::Details<bool>(details).ptr();
2842385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
2852385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    if (is_fullscreen && tray_.get() && tray_->GetMessageCenterTray())
2862385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      tray_->GetMessageCenterTray()->HidePopupBubble();
2872385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  }
2882385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch}
2892385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ImageDownloads
2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::ImageDownloads(
294c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    message_center::MessageCenter* message_center,
295c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ImageDownloadsObserver* observer)
296c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    : message_center_(message_center),
297c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      pending_downloads_(0),
298c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      observer_(observer) {
2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::~ImageDownloads() { }
3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::StartDownloads(
3042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification) {
305c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // In case all downloads are synchronous, assume a pending download.
306c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AddPendingDownload();
307c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification primary icon.
3092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StartDownloadWithImage(
3102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification,
3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &notification.icon(),
3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification.icon_url(),
3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationIcon,
3142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
3152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 notification.notification_id()));
3162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification image.
318868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  StartDownloadWithImage(
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification,
320868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NULL,
321868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification.image_url(),
3222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationImage,
3232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
3242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 notification.notification_id()));
3252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification button icons.
327868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  StartDownloadWithImage(
3282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification,
329868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NULL,
330868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification.button_one_icon_url(),
3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon,
3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
333868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 notification.notification_id(),
334868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 0));
335868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  StartDownloadWithImage(
336868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification,
337868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NULL,
338868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification.button_two_icon_url(),
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon,
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
341868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 notification.notification_id(),
342868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 1));
343c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
344c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // This should tell the observer we're done if everything was synchronous.
345c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PendingDownloadCompleted();
3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::StartDownloadWithImage(
3492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification,
3502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Image* image,
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& url,
3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SetImageCallback& callback) {
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Set the image directly if we have it.
3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (image && !image->IsEmpty()) {
3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    callback.Run(*image);
3562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Leave the image null if there's no URL.
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (url.is_empty())
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::RenderViewHost* host = notification.GetRenderViewHost();
3642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!host) {
3652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LOG(WARNING) << "Notification needs an image but has no RenderViewHost";
3662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::WebContents* contents =
3702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      content::WebContents::FromRenderViewHost(host);
3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!contents) {
3722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LOG(WARNING) << "Notification needs an image but has no WebContents";
3732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
376c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AddPendingDownload();
377c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
378c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  contents->DownloadImage(
3792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      url,
380eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      false,  // Not a favicon
381eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      0,  // No maximum size
3822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(
3832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          &MessageCenterNotificationManager::ImageDownloads::DownloadComplete,
3842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          AsWeakPtr(),
3852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          callback));
3862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::DownloadComplete(
3892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SetImageCallback& callback,
3902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int download_id,
39190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    int http_status_code,
3922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& image_url,
393d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const std::vector<SkBitmap>& bitmaps,
394d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const std::vector<gfx::Size>& original_bitmap_sizes) {
395c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PendingDownloadCompleted();
396c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (bitmaps.empty())
3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmaps[0]);
4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  callback.Run(image);
4012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
403c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Private methods.
404c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
405c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::AddPendingDownload() {
406c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ++pending_downloads_;
407c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
408c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
409c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void
410c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::PendingDownloadCompleted() {
411c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(pending_downloads_ > 0);
412c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (--pending_downloads_ == 0 && observer_)
413c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    observer_->OnDownloadsCompleted();
414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
415c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
4172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ProfileNotification
4182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::ProfileNotification(
4202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Profile* profile,
4212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification,
4222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    message_center::MessageCenter* message_center)
4232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : profile_(profile),
4242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification_(notification),
425c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      downloads_(new ImageDownloads(message_center, this)) {
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile);
427a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#if defined(OS_CHROMEOS)
428a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  notification_.set_profile_id(multi_user_util::GetUserIDFromProfile(profile));
429a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#endif
4302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::~ProfileNotification() {
4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ProfileNotification::StartDownloads() {
4362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  downloads_->StartDownloads(notification_);
4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
439c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void
440c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::OnDownloadsCompleted() {
441c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  notification_.DoneRendering();
442c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
443c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string
4452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MessageCenterNotificationManager::ProfileNotification::GetExtensionId() {
446f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  extensions::InfoMap* extension_info_map =
447c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      extensions::ExtensionSystem::Get(profile())->info_map();
4485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  extensions::ExtensionSet extensions;
449c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin(
450c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      notification().origin_url(), notification().process_id(),
451c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      extensions::APIPermission::kNotification, &extensions);
452c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
453c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DesktopNotificationService* desktop_service =
454c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DesktopNotificationServiceFactory::GetForProfile(profile());
4555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (extensions::ExtensionSet::const_iterator iter = extensions.begin();
456c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)       iter != extensions.end(); ++iter) {
457eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (desktop_service->IsNotifierEnabled(message_center::NotifierId(
458eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            message_center::NotifierId::APPLICATION, (*iter)->id()))) {
459c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return (*iter)->id();
460eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
461c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
462c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return std::string();
4632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
4662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// private
4672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::AddProfileNotification(
4692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ProfileNotification* profile_notification) {
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const Notification& notification = profile_notification->notification();
4712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string id = notification.notification_id();
4722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification ids should be unique.
4732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile_notifications_.find(id) == profile_notifications_.end());
4742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notifications_[id] = profile_notification;
4752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
476868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Create the copy for message center, and ensure the extension ID is correct.
477868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<message_center::Notification> message_center_notification(
478868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      new message_center::Notification(notification));
479868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  message_center_->AddNotification(message_center_notification.Pass());
480868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
4812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notification->StartDownloads();
4822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::RemoveProfileNotification(
485ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    ProfileNotification* profile_notification) {
4862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string id = profile_notification->notification().notification_id();
4872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notifications_.erase(id);
4882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  delete profile_notification;
4892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification*
4922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MessageCenterNotificationManager::FindProfileNotification(
4932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        const std::string& id) const {
4942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NotificationMap::const_iterator iter = profile_notifications_.find(id);
495c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (iter == profile_notifications_.end())
496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return NULL;
497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return (*iter).second;
4992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
500