message_center_notification_manager.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/stl_util.h"
122385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch#include "chrome/browser/chrome_notification_types.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/desktop_notification_service.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/desktop_notification_service_factory.h"
1558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/notifications/fullscreen_notification_blocker.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/message_center_settings_controller.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/notification.h"
1858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/notifications/screen_lock_notification_blocker.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/chrome_pages.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/host_desktop.h"
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/pref_names.h"
242385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch#include "content/public/browser/notification_service.h"
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents.h"
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/common/url_constants.h"
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/extension_system.h"
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/browser/info_map.h"
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/common/extension_set.h"
30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "ui/gfx/image/image_skia.h"
3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/message_center/message_center_style.h"
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/message_center/message_center_tray.h"
3358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "ui/message_center/message_center_types.h"
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/message_center/notifier_settings.h"
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_CHROMEOS)
3758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/notifications/login_state_notification_blocker_chromeos.h"
38a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
3958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif
4058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(USE_ASH)
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ash/shell.h"
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ash/system/web_notification/web_notification_tray.h"
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#if defined(OS_WIN)
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// The first-run balloon will be shown |kFirstRunIdleDelaySeconds| after all
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// popups go away and the user has notifications in the message center.
49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochconst int kFirstRunIdleDelaySeconds = 1;
5068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#endif
51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::MessageCenterNotificationManager(
53eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    message_center::MessageCenter* message_center,
54bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    PrefService* local_state,
55bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    scoped_ptr<message_center::NotifierSettingsProvider> settings_provider)
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : message_center_(message_center),
57eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
58eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      first_run_idle_timeout_(
59eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          base::TimeDelta::FromSeconds(kFirstRunIdleDelaySeconds)),
60eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      weak_factory_(this),
61eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
6258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      settings_provider_(settings_provider.Pass()),
6368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      system_observer_(this),
64010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      stats_collector_(message_center),
65010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      google_now_stats_collector_(message_center) {
66eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
67eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  first_run_pref_.Init(prefs::kMessageCenterShowedFirstRunBalloon, local_state);
68eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
69eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  message_center_->AddObserver(this);
71bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  message_center_->SetNotifierSettingsProvider(settings_provider_.get());
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_CHROMEOS)
7458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blockers_.push_back(
7558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      new LoginStateNotificationBlockerChromeOS(message_center));
7658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#else
7758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blockers_.push_back(new ScreenLockNotificationBlocker(message_center));
7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif
7958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  blockers_.push_back(new FullscreenNotificationBlocker(message_center));
8058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#if defined(OS_WIN) || defined(OS_MACOSX) \
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // On Windows, Linux and Mac, the notification manager owns the tray icon and
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // views.Other platforms have global ownership and Create will return NULL.
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  tray_.reset(message_center::CreateMessageCenterTray());
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
872385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  registrar_.Add(this,
882385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch                 chrome::NOTIFICATION_FULLSCREEN_CHANGED,
892385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch                 content::NotificationService::AllSources());
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::~MessageCenterNotificationManager() {
93cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  message_center_->SetNotifierSettingsProvider(NULL);
94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  message_center_->RemoveObserver(this);
95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  STLDeleteContainerPairSecondPointers(profile_notifications_.begin(),
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                       profile_notifications_.end());
98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  profile_notifications_.clear();
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
101a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochvoid MessageCenterNotificationManager::RegisterPrefs(
102a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    PrefRegistrySimple* registry) {
103a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  registry->RegisterBooleanPref(prefs::kMessageCenterShowedFirstRunBalloon,
104a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                false);
1055c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  registry->RegisterBooleanPref(prefs::kMessageCenterShowIcon, true);
1065c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  registry->RegisterBooleanPref(prefs::kMessageCenterForcedOnTaskbar, false);
107a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch}
108a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// NotificationUIManager
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void MessageCenterNotificationManager::Add(const Notification& notification,
11358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                           Profile* profile) {
11458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (Update(notification, profile))
11558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return;
116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DesktopNotificationServiceFactory::GetForProfile(profile)->
1188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      ShowWelcomeNotificationIfNecessary(notification);
1198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
120116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // WARNING: You MUST use AddProfileNotification or update the message center
121116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // via the notification within a ProfileNotification object or the profile ID
122116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // will not be correctly set for ChromeOS.
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AddProfileNotification(
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new ProfileNotification(profile, notification, message_center_));
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
12758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool MessageCenterNotificationManager::Update(const Notification& notification,
12858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                              Profile* profile) {
129a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const base::string16& replace_id = notification.replace_id();
130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (replace_id.empty())
131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return false;
132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const GURL origin_url = notification.origin_url();
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(origin_url.is_valid());
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Since replace_id is provided by arbitrary JS, we need to use origin_url
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // (which is an app url in case of app/extension) to scope the replace ids
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // in the given profile.
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (NotificationMap::iterator iter = profile_notifications_.begin();
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       iter != profile_notifications_.end(); ++iter) {
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ProfileNotification* old_notification = (*iter).second;
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (old_notification->notification().replace_id() == replace_id &&
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        old_notification->notification().origin_url() == origin_url &&
144eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        old_notification->profile() == profile) {
145a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      // Changing the type from non-progress to progress does not count towards
146a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      // the immediate update allowed in the message center.
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      std::string old_id =
1486d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)          old_notification->notification().delegate_id();
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Add/remove notification in the local list but just update the same
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // one in MessageCenter.
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      delete old_notification;
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profile_notifications_.erase(old_id);
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ProfileNotification* new_notification =
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          new ProfileNotification(profile, notification, message_center_);
1566d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      profile_notifications_[notification.delegate_id()] = new_notification;
157868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
158116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      // WARNING: You MUST use AddProfileNotification or update the message
159116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      // center via the notification within a ProfileNotification object or the
160116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      // profile ID will not be correctly set for ChromeOS.
161116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      message_center_->UpdateNotification(
162116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          old_id,
163116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          make_scoped_ptr(new message_center::Notification(
164116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch              new_notification->notification())));
165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new_notification->StartDownloads();
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return true;
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)const Notification* MessageCenterNotificationManager::FindById(
17458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const std::string& id) const {
17558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  NotificationMap::const_iterator iter = profile_notifications_.find(id);
17658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (iter == profile_notifications_.end())
17758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return NULL;
17858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return &(iter->second->notification());
17958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool MessageCenterNotificationManager::CancelById(const std::string& id) {
18258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // See if this ID hasn't been shown yet.
18358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // If it has been shown, remove it.
18458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  NotificationMap::iterator iter = profile_notifications_.find(id);
18558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (iter == profile_notifications_.end())
18658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return false;
18758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
18868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RemoveProfileNotification(iter->second);
18958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  message_center_->RemoveNotification(id, /* by_user */ false);
19058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return true;
19158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
19258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
19358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)std::set<std::string>
19458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)MessageCenterNotificationManager::GetAllIdsByProfileAndSourceOrigin(
19558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    Profile* profile,
19658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const GURL& source) {
19758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
19858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::set<std::string> notification_ids;
19958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (NotificationMap::iterator iter = profile_notifications_.begin();
20058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)       iter != profile_notifications_.end(); iter++) {
20158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if ((*iter).second->notification().origin_url() == source &&
20258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        profile == (*iter).second->profile()) {
20358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      notification_ids.insert(iter->first);
20458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
20658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return notification_ids;
20758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool MessageCenterNotificationManager::CancelAllBySourceOrigin(
21058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    const GURL& source) {
21158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Same pattern as CancelById, but more complicated than the above
21258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // because there may be multiple notifications from the same source.
21358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool removed = false;
214c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
21558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (NotificationMap::iterator loopiter = profile_notifications_.begin();
21658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)       loopiter != profile_notifications_.end(); ) {
21758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    NotificationMap::iterator curiter = loopiter++;
21858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if ((*curiter).second->notification().origin_url() == source) {
21968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      const std::string id = curiter->first;
22068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      RemoveProfileNotification(curiter->second);
22168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      message_center_->RemoveNotification(id, /* by_user */ false);
22258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      removed = true;
22358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
22458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
22558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return removed;
22658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
22758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
22858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)bool MessageCenterNotificationManager::CancelAllByProfile(Profile* profile) {
22958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Same pattern as CancelAllBySourceOrigin.
23058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool removed = false;
23158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
23258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  for (NotificationMap::iterator loopiter = profile_notifications_.begin();
23358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)       loopiter != profile_notifications_.end(); ) {
23458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    NotificationMap::iterator curiter = loopiter++;
23558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (profile == (*curiter).second->profile()) {
23668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      const std::string id = curiter->first;
23768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      RemoveProfileNotification(curiter->second);
23868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      message_center_->RemoveNotification(id, /* by_user */ false);
23958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      removed = true;
24058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
24158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
24258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return removed;
24358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
24458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
24558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void MessageCenterNotificationManager::CancelAll() {
24658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  message_center_->RemoveAllNotifications(/* by_user */ false);
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// MessageCenter::Observer
251c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void MessageCenterNotificationManager::OnNotificationRemoved(
252c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string& notification_id,
253c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bool by_user) {
254c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  NotificationMap::const_iterator iter =
255c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      profile_notifications_.find(notification_id);
256c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (iter != profile_notifications_.end())
257ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    RemoveProfileNotification(iter->second);
258eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
259eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
260eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  CheckFirstRunTimer();
261eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
262c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
263c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
26458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void MessageCenterNotificationManager::OnCenterVisibilityChanged(
26558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    message_center::Visibility visibility) {
266eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
26768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  if (visibility == message_center::VISIBILITY_TRANSIENT)
26868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    CheckFirstRunTimer();
269eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
270eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
271eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
272eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid MessageCenterNotificationManager::OnNotificationUpdated(
273eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const std::string& notification_id) {
274eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN)
275eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  CheckFirstRunTimer();
276eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif
277eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
278eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void MessageCenterNotificationManager::EnsureMessageCenterClosed() {
2805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (tray_.get())
2815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    tray_->GetMessageCenterTray()->HideMessageCenterBubble();
2825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(USE_ASH)
2845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (ash::Shell::HasInstance()) {
2855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ash::WebNotificationTray* tray =
2865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        ash::Shell::GetInstance()->GetWebNotificationTray();
2875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (tray)
2885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      tray->GetMessageCenterTray()->HideMessageCenterBubble();
2895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
2915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
293eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid MessageCenterNotificationManager::SetMessageCenterTrayDelegateForTest(
294eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    message_center::MessageCenterTrayDelegate* delegate) {
295eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  tray_.reset(delegate);
296868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
297868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2982385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochvoid MessageCenterNotificationManager::Observe(
2992385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    int type,
3002385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    const content::NotificationSource& source,
3012385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    const content::NotificationDetails& details) {
3022385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  if (type == chrome::NOTIFICATION_FULLSCREEN_CHANGED) {
3032385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    const bool is_fullscreen = *content::Details<bool>(details).ptr();
3042385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
3052385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    if (is_fullscreen && tray_.get() && tray_->GetMessageCenterTray())
3062385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      tray_->GetMessageCenterTray()->HidePopupBubble();
3072385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  }
3082385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch}
3092385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
3102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ImageDownloads
3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::ImageDownloads(
314c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    message_center::MessageCenter* message_center,
315c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ImageDownloadsObserver* observer)
316c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    : message_center_(message_center),
317c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      pending_downloads_(0),
318c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      observer_(observer) {
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::~ImageDownloads() { }
3222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::StartDownloads(
3242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification) {
325c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // In case all downloads are synchronous, assume a pending download.
326c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AddPendingDownload();
327c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification primary icon.
3292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StartDownloadWithImage(
3302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification,
3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &notification.icon(),
3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification.icon_url(),
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationIcon,
3342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
3356d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                 notification.delegate_id()));
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification image.
338868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  StartDownloadWithImage(
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification,
340868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NULL,
341868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification.image_url(),
3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationImage,
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
3446d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                 notification.delegate_id()));
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification button icons.
347868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  StartDownloadWithImage(
3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification,
349868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NULL,
350868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification.button_one_icon_url(),
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon,
3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
3536d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                 notification.delegate_id(),
354868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 0));
355868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  StartDownloadWithImage(
356868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification,
357868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      NULL,
358868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      notification.button_two_icon_url(),
3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon,
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 base::Unretained(message_center_),
3616d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                 notification.delegate_id(),
362868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                 1));
363c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
364c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // This should tell the observer we're done if everything was synchronous.
365c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PendingDownloadCompleted();
3662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::StartDownloadWithImage(
3692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification,
3702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Image* image,
3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& url,
3722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SetImageCallback& callback) {
3732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Set the image directly if we have it.
3742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (image && !image->IsEmpty()) {
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    callback.Run(*image);
3762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Leave the image null if there's no URL.
3802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (url.is_empty())
3812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  content::WebContents* contents = notification.GetWebContents();
3842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!contents) {
3852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LOG(WARNING) << "Notification needs an image but has no WebContents";
3862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
3872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
389c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  AddPendingDownload();
390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
391c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  contents->DownloadImage(
3922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      url,
393eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      false,  // Not a favicon
394eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      0,  // No maximum size
3952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::Bind(
3962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          &MessageCenterNotificationManager::ImageDownloads::DownloadComplete,
3972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          AsWeakPtr(),
3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          callback));
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::DownloadComplete(
4022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const SetImageCallback& callback,
4032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int download_id,
40490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    int http_status_code,
4052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const GURL& image_url,
406d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const std::vector<SkBitmap>& bitmaps,
407d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    const std::vector<gfx::Size>& original_bitmap_sizes) {
408c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PendingDownloadCompleted();
409c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (bitmaps.empty())
4112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
4122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmaps[0]);
4132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  callback.Run(image);
4142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
416c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Private methods.
417c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
418c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::AddPendingDownload() {
419c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ++pending_downloads_;
420c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
421c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
422c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void
423c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::PendingDownloadCompleted() {
424c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(pending_downloads_ > 0);
425c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (--pending_downloads_ == 0 && observer_)
426c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    observer_->OnDownloadsCompleted();
427c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
428c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
4302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ProfileNotification
4312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::ProfileNotification(
4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Profile* profile,
4342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const Notification& notification,
4352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    message_center::MessageCenter* message_center)
4362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : profile_(profile),
4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      notification_(notification),
438c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      downloads_(new ImageDownloads(message_center, this)) {
4392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile);
440a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#if defined(OS_CHROMEOS)
441a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  notification_.set_profile_id(multi_user_util::GetUserIDFromProfile(profile));
442a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#endif
4432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::~ProfileNotification() {
4462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ProfileNotification::StartDownloads() {
4492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  downloads_->StartDownloads(notification_);
4502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
452c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void
453c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::OnDownloadsCompleted() {
454c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  notification_.DoneRendering();
455c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
456c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string
4582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MessageCenterNotificationManager::ProfileNotification::GetExtensionId() {
459f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  extensions::InfoMap* extension_info_map =
460c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      extensions::ExtensionSystem::Get(profile())->info_map();
4615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  extensions::ExtensionSet extensions;
462c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin(
463c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      notification().origin_url(), notification().process_id(),
464c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      extensions::APIPermission::kNotification, &extensions);
465c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
466c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DesktopNotificationService* desktop_service =
467c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      DesktopNotificationServiceFactory::GetForProfile(profile());
4685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (extensions::ExtensionSet::const_iterator iter = extensions.begin();
469c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)       iter != extensions.end(); ++iter) {
470eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (desktop_service->IsNotifierEnabled(message_center::NotifierId(
471eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            message_center::NotifierId::APPLICATION, (*iter)->id()))) {
472c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return (*iter)->id();
473eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
474c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
475c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return std::string();
4762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
4792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// private
4802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::AddProfileNotification(
4822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ProfileNotification* profile_notification) {
4832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const Notification& notification = profile_notification->notification();
4846d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  std::string id = notification.delegate_id();
4852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification ids should be unique.
4862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(profile_notifications_.find(id) == profile_notifications_.end());
4872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notifications_[id] = profile_notification;
4882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
489868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Create the copy for message center, and ensure the extension ID is correct.
490868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  scoped_ptr<message_center::Notification> message_center_notification(
491868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      new message_center::Notification(notification));
492868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  message_center_->AddNotification(message_center_notification.Pass());
493868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
4942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notification->StartDownloads();
4952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::RemoveProfileNotification(
498ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    ProfileNotification* profile_notification) {
4996d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  std::string id = profile_notification->notification().delegate_id();
5002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  profile_notifications_.erase(id);
5012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  delete profile_notification;
5022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification*
5052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MessageCenterNotificationManager::FindProfileNotification(
5062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        const std::string& id) const {
5072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NotificationMap::const_iterator iter = profile_notifications_.find(id);
508c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (iter == profile_notifications_.end())
509c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return NULL;
510c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return (*iter).second;
5122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
513