message_center_notification_manager.cc revision ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16
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" 11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/extensions/extension_info_map.h" 12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/extensions/extension_system.h" 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/desktop_notification_service.h" 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/desktop_notification_service_factory.h" 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/message_center_settings_controller.h" 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/notifications/notification.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/extensions/extension_set.h" 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/pref_names.h" 232385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch#include "content/public/browser/notification_service.h" 242385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch#include "content/public/browser/user_metrics.h" 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/web_contents.h" 26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/common/url_constants.h" 2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/message_center/message_center_style.h" 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/message_center/message_center_tray.h" 29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/message_center/notifier_settings.h" 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace { 32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// The first-run balloon will be shown |kFirstRunIdleDelaySeconds| after all 33eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// popups go away and the user has notifications in the message center. 34eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochconst int kFirstRunIdleDelaySeconds = 1; 35eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} // namespace 36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::MessageCenterNotificationManager( 38eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch message_center::MessageCenter* message_center, 39bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch PrefService* local_state, 40bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch scoped_ptr<message_center::NotifierSettingsProvider> settings_provider) 41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) : message_center_(message_center), 42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN) 43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch first_run_idle_timeout_( 44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::TimeDelta::FromSeconds(kFirstRunIdleDelaySeconds)), 45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch weak_factory_(this), 46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif 47bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch settings_provider_(settings_provider.Pass()) { 48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN) 49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch first_run_pref_.Init(prefs::kMessageCenterShowedFirstRunBalloon, local_state); 50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif 51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) message_center_->SetDelegate(this); 53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) message_center_->AddObserver(this); 54bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch message_center_->SetNotifierSettingsProvider(settings_provider_.get()); 552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#if defined(OS_WIN) || defined(OS_MACOSX) \ 57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) || (defined(USE_AURA) && !defined(USE_ASH)) 58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // On Windows, Linux and Mac, the notification manager owns the tray icon and 59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // views.Other platforms have global ownership and Create will return NULL. 602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) tray_.reset(message_center::CreateMessageCenterTray()); 612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 622385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch registrar_.Add(this, 632385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch chrome::NOTIFICATION_FULLSCREEN_CHANGED, 642385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch content::NotificationService::AllSources()); 652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::~MessageCenterNotificationManager() { 68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) message_center_->RemoveObserver(this); 692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//////////////////////////////////////////////////////////////////////////////// 722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// NotificationUIManager 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 74558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdochconst Notification* MessageCenterNotificationManager::FindById( 75558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch const std::string& id) const { 76558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch const Notification* notification = NotificationUIManagerImpl::FindById(id); 77558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch if (notification) 78558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch return notification; 79558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch NotificationMap::const_iterator iter = profile_notifications_.find(id); 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (iter == profile_notifications_.end()) 81558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch return NULL; 82558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch return &(iter->second->notification()); 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool MessageCenterNotificationManager::CancelById(const std::string& id) { 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // See if this ID hasn't been shown yet. 872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (NotificationUIManagerImpl::CancelById(id)) 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return true; 892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // If it has been shown, remove it. 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) NotificationMap::iterator iter = profile_notifications_.find(id); 922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (iter == profile_notifications_.end()) 932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return false; 942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) message_center_->RemoveNotification(id, /* by_user */ false); 962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return true; 972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)std::set<std::string> 100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)MessageCenterNotificationManager::GetAllIdsByProfileAndSourceOrigin( 101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Profile* profile, 102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const GURL& source) { 103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::set<std::string> notification_ids = 105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) NotificationUIManagerImpl::GetAllIdsByProfileAndSourceOrigin(profile, 106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) source); 107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) for (NotificationMap::iterator iter = profile_notifications_.begin(); 109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) iter != profile_notifications_.end(); iter++) { 110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if ((*iter).second->notification().origin_url() == source && 111eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch profile == (*iter).second->profile()) { 112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) notification_ids.insert(iter->first); 113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return notification_ids; 116868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 117868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool MessageCenterNotificationManager::CancelAllBySourceOrigin( 1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GURL& source) { 1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Same pattern as CancelById, but more complicated than the above 1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // because there may be multiple notifications from the same source. 1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool removed = NotificationUIManagerImpl::CancelAllBySourceOrigin(source); 1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) for (NotificationMap::iterator loopiter = profile_notifications_.begin(); 1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) loopiter != profile_notifications_.end(); ) { 1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) NotificationMap::iterator curiter = loopiter++; 1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if ((*curiter).second->notification().origin_url() == source) { 128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) message_center_->RemoveNotification(curiter->first, /* by_user */ false); 1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) removed = true; 1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return removed; 1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool MessageCenterNotificationManager::CancelAllByProfile(Profile* profile) { 1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Same pattern as CancelAllBySourceOrigin. 1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool removed = NotificationUIManagerImpl::CancelAllByProfile(profile); 1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) for (NotificationMap::iterator loopiter = profile_notifications_.begin(); 1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) loopiter != profile_notifications_.end(); ) { 1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) NotificationMap::iterator curiter = loopiter++; 142eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (profile == (*curiter).second->profile()) { 143c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) message_center_->RemoveNotification(curiter->first, /* by_user */ false); 1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) removed = true; 1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return removed; 1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::CancelAll() { 1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) NotificationUIManagerImpl::CancelAll(); 1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 153c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) message_center_->RemoveAllNotifications(/* by_user */ false); 1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//////////////////////////////////////////////////////////////////////////////// 1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// NotificationUIManagerImpl 1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool MessageCenterNotificationManager::ShowNotification( 1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const Notification& notification, Profile* profile) { 161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (message_center_->IsMessageCenterVisible()) 162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return false; 163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (UpdateNotification(notification, profile)) 165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return true; 166868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) AddProfileNotification( 1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) new ProfileNotification(profile, notification, message_center_)); 1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return true; 1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool MessageCenterNotificationManager::UpdateNotification( 1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const Notification& notification, Profile* profile) { 174a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // Only progress notification update can be reflected immediately in the 175a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // message center. 176a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) bool update_progress_notification = 177a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) notification.type() == message_center::NOTIFICATION_TYPE_PROGRESS; 178a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) bool is_message_center_visible = message_center_->IsMessageCenterVisible(); 179a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) if (!update_progress_notification && is_message_center_visible) 180868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return false; 181868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const string16& replace_id = notification.replace_id(); 183868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (replace_id.empty()) 184868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return false; 185868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GURL origin_url = notification.origin_url(); 1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(origin_url.is_valid()); 1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Since replace_id is provided by arbitrary JS, we need to use origin_url 1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // (which is an app url in case of app/extension) to scope the replace ids 1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // in the given profile. 1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) for (NotificationMap::iterator iter = profile_notifications_.begin(); 1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) iter != profile_notifications_.end(); ++iter) { 1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ProfileNotification* old_notification = (*iter).second; 1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (old_notification->notification().replace_id() == replace_id && 1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) old_notification->notification().origin_url() == origin_url && 197eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch old_notification->profile() == profile) { 198a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // Changing the type from non-progress to progress does not count towards 199a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) // the immediate update allowed in the message center. 200a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) if (update_progress_notification && is_message_center_visible && 201a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) old_notification->notification().type() != 202a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) message_center::NOTIFICATION_TYPE_PROGRESS) { 203a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) return false; 204a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) } 205a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) 2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::string old_id = 2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) old_notification->notification().notification_id(); 208c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DCHECK(message_center_->HasNotification(old_id)); 2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Add/remove notification in the local list but just update the same 2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // one in MessageCenter. 2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) delete old_notification; 2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) profile_notifications_.erase(old_id); 2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ProfileNotification* new_notification = 2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) new ProfileNotification(profile, notification, message_center_); 2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) profile_notifications_[notification.notification_id()] = new_notification; 217868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 218868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Now pass a copy to message center. 219868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) scoped_ptr<message_center::Notification> message_center_notification( 220868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) make_scoped_ptr(new message_center::Notification(notification))); 221868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) message_center_notification->set_extension_id( 222868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) new_notification->GetExtensionId()); 2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) message_center_->UpdateNotification(old_id, 224868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) message_center_notification.Pass()); 225868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) new_notification->StartDownloads(); 2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return true; 2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return false; 2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//////////////////////////////////////////////////////////////////////////////// 2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// MessageCenter::Delegate 2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::DisableExtension( 2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& notification_id) { 2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ProfileNotification* profile_notification = 2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) FindProfileNotification(notification_id); 240c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (!profile_notification) 241c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return; 242c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::string extension_id = profile_notification->GetExtensionId(); 2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!extension_id.empty()); // or UI should not have enabled the command. 245c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DesktopNotificationService* service = 246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DesktopNotificationServiceFactory::GetForProfile( 247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) profile_notification->profile()); 248eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch message_center::NotifierId notifier_id( 249eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch message_center::NotifierId::APPLICATION, extension_id); 250eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch service->SetNotifierEnabled(notifier_id, false); 2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::DisableNotificationsFromSource( 2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& notification_id) { 2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ProfileNotification* profile_notification = 2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) FindProfileNotification(notification_id); 257c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (!profile_notification) 258c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return; 259c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // UI should not have enabled the command if there is no valid source. 2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(profile_notification->notification().origin_url().is_valid()); 2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DesktopNotificationService* service = 2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DesktopNotificationServiceFactory::GetForProfile( 2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) profile_notification->profile()); 265c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (profile_notification->notification().origin_url().scheme() == 266c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) chrome::kChromeUIScheme) { 267c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const std::string name = 268c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) profile_notification->notification().origin_url().host(); 269eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch message_center::NotifierId notifier_id( 270eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch message_center::ParseSystemComponentName(name)); 271eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch service->SetNotifierEnabled(notifier_id, false); 272c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } else { 273c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) service->DenyPermission(profile_notification->notification().origin_url()); 274c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ShowSettings( 2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& notification_id) { 2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // The per-message-center Settings button passes an empty string. 2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (notification_id.empty()) { 2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) NOTIMPLEMENTED(); 2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return; 2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ProfileNotification* profile_notification = 2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) FindProfileNotification(notification_id); 287c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (!profile_notification) 288c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return; 289c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Browser* browser = 2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) chrome::FindOrCreateTabbedBrowser(profile_notification->profile(), 2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) chrome::HOST_DESKTOP_TYPE_NATIVE); 2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (profile_notification->GetExtensionId().empty()) 2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) chrome::ShowContentSettings(browser, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); 2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) else 2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) chrome::ShowExtensions(browser, std::string()); 2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 299c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//////////////////////////////////////////////////////////////////////////////// 300c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// MessageCenter::Observer 301c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void MessageCenterNotificationManager::OnNotificationRemoved( 302c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const std::string& notification_id, 303c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) bool by_user) { 304c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Do not call FindProfileNotification(). Some tests create notifications 305c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // directly to MessageCenter, but this method will be called for the removals 306c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // of such notifications. 307c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) NotificationMap::const_iterator iter = 308c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) profile_notifications_.find(notification_id); 309c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (iter != profile_notifications_.end()) 310ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch RemoveProfileNotification(iter->second); 311eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 312eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN) 313eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch CheckFirstRunTimer(); 314eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif 315c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 316c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 317868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void MessageCenterNotificationManager::OnNotificationCenterClosed() { 318868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // When the center is open it halts all notifications, so we need to listen 319868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // for events indicating it's been closed. 320868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) CheckAndShowNotifications(); 321eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN) 322eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch CheckFirstRunTimer(); 323eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif 324eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 325eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 326eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid MessageCenterNotificationManager::OnNotificationUpdated( 327eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const std::string& notification_id) { 328eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#if defined(OS_WIN) 329eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch CheckFirstRunTimer(); 330eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif 331eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 332eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 333eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid MessageCenterNotificationManager::SetMessageCenterTrayDelegateForTest( 334eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch message_center::MessageCenterTrayDelegate* delegate) { 335eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch tray_.reset(delegate); 336868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 337868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 3382385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochvoid MessageCenterNotificationManager::Observe( 3392385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch int type, 3402385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch const content::NotificationSource& source, 3412385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch const content::NotificationDetails& details) { 3422385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch if (type == chrome::NOTIFICATION_FULLSCREEN_CHANGED) { 3432385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch const bool is_fullscreen = *content::Details<bool>(details).ptr(); 3442385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch 3452385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch if (is_fullscreen && tray_.get() && tray_->GetMessageCenterTray()) 3462385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch tray_->GetMessageCenterTray()->HidePopupBubble(); 3472385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch } else { 3482385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch NotificationUIManagerImpl::Observe(type, source, details); 3492385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch } 3502385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch} 3512385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch 3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//////////////////////////////////////////////////////////////////////////////// 3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ImageDownloads 3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::ImageDownloads( 356c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) message_center::MessageCenter* message_center, 357c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ImageDownloadsObserver* observer) 358c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) : message_center_(message_center), 359c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) pending_downloads_(0), 360c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) observer_(observer) { 3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::~ImageDownloads() { } 3642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::StartDownloads( 3662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const Notification& notification) { 367c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // In case all downloads are synchronous, assume a pending download. 368c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) AddPendingDownload(); 369c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 3702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Notification primary icon. 3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) StartDownloadWithImage( 3722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) notification, 3732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ¬ification.icon(), 3742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) notification.icon_url(), 3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) message_center::kNotificationIconSize, 3762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::Bind(&message_center::MessageCenter::SetNotificationIcon, 3772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::Unretained(message_center_), 3782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) notification.notification_id())); 3792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Notification image. 381868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) StartDownloadWithImage( 3822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) notification, 383868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) NULL, 384868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) notification.image_url(), 3852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) message_center::kNotificationPreferredImageSize, 3862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::Bind(&message_center::MessageCenter::SetNotificationImage, 3872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::Unretained(message_center_), 3882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) notification.notification_id())); 3892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 3902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Notification button icons. 391868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) StartDownloadWithImage( 3922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) notification, 393868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) NULL, 394868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) notification.button_one_icon_url(), 3952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) message_center::kNotificationButtonIconSize, 3962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon, 3972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::Unretained(message_center_), 398868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) notification.notification_id(), 399868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 0)); 400868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) StartDownloadWithImage( 401868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) notification, 402868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) NULL, 403868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) notification.button_two_icon_url(), 4042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) message_center::kNotificationButtonIconSize, 4052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon, 4062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::Unretained(message_center_), 407868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) notification.notification_id(), 408868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 1)); 409c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 410c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // This should tell the observer we're done if everything was synchronous. 411c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) PendingDownloadCompleted(); 4122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 4132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 4142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::StartDownloadWithImage( 4152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const Notification& notification, 4162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const gfx::Image* image, 4172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GURL& url, 4182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int size, 4192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const SetImageCallback& callback) { 4202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Set the image directly if we have it. 4212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (image && !image->IsEmpty()) { 4222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) callback.Run(*image); 4232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return; 4242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 4252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Leave the image null if there's no URL. 4272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (url.is_empty()) 4282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return; 4292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 4302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content::RenderViewHost* host = notification.GetRenderViewHost(); 4312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (!host) { 4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) LOG(WARNING) << "Notification needs an image but has no RenderViewHost"; 4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return; 4342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 4352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 4362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content::WebContents* contents = 4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) content::WebContents::FromRenderViewHost(host); 4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (!contents) { 4392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) LOG(WARNING) << "Notification needs an image but has no WebContents"; 4402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return; 4412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 4422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 443c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) AddPendingDownload(); 444c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 445c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) contents->DownloadImage( 4462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) url, 447eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch false, // Not a favicon 448eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch size, // Preferred size 449eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 0, // No maximum size 4502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::Bind( 4512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) &MessageCenterNotificationManager::ImageDownloads::DownloadComplete, 4522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) AsWeakPtr(), 4532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) callback)); 4542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 4552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 4562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::DownloadComplete( 4572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const SetImageCallback& callback, 4582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int download_id, 45990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) int http_status_code, 4602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GURL& image_url, 4612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int requested_size, 4622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::vector<SkBitmap>& bitmaps) { 463c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) PendingDownloadCompleted(); 464c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 4652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (bitmaps.empty()) 4662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return; 4672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmaps[0]); 4682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) callback.Run(image); 4692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 471c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Private methods. 472c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 473c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void MessageCenterNotificationManager::ImageDownloads::AddPendingDownload() { 474c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ++pending_downloads_; 475c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 476c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 477c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void 478c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)MessageCenterNotificationManager::ImageDownloads::PendingDownloadCompleted() { 479c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DCHECK(pending_downloads_ > 0); 480c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (--pending_downloads_ == 0 && observer_) 481c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) observer_->OnDownloadsCompleted(); 482c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 483c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 4842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//////////////////////////////////////////////////////////////////////////////// 4852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ProfileNotification 4862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 4872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::ProfileNotification( 4882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Profile* profile, 4892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const Notification& notification, 4902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) message_center::MessageCenter* message_center) 4912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) : profile_(profile), 4922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) notification_(notification), 493c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) downloads_(new ImageDownloads(message_center, this)) { 4942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(profile); 4952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 4962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 4972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::~ProfileNotification() { 4982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 4992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::ProfileNotification::StartDownloads() { 5012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) downloads_->StartDownloads(notification_); 5022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 5032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 504c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void 505c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification::OnDownloadsCompleted() { 506c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) notification_.DoneRendering(); 507c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 508c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 5092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)std::string 5102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) MessageCenterNotificationManager::ProfileNotification::GetExtensionId() { 511c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ExtensionInfoMap* extension_info_map = 512c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) extensions::ExtensionSystem::Get(profile())->info_map(); 513c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ExtensionSet extensions; 514c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( 515c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) notification().origin_url(), notification().process_id(), 516c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) extensions::APIPermission::kNotification, &extensions); 517c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 518c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DesktopNotificationService* desktop_service = 519c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DesktopNotificationServiceFactory::GetForProfile(profile()); 520c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) for (ExtensionSet::const_iterator iter = extensions.begin(); 521c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) iter != extensions.end(); ++iter) { 522eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (desktop_service->IsNotifierEnabled(message_center::NotifierId( 523eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch message_center::NotifierId::APPLICATION, (*iter)->id()))) { 524c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return (*iter)->id(); 525eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 526c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 527c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return std::string(); 5282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 5292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//////////////////////////////////////////////////////////////////////////////// 5312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// private 5322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::AddProfileNotification( 5342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ProfileNotification* profile_notification) { 5352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const Notification& notification = profile_notification->notification(); 5362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::string id = notification.notification_id(); 5372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Notification ids should be unique. 5382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(profile_notifications_.find(id) == profile_notifications_.end()); 5392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) profile_notifications_[id] = profile_notification; 5402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 541868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Create the copy for message center, and ensure the extension ID is correct. 542868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) scoped_ptr<message_center::Notification> message_center_notification( 543868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) new message_center::Notification(notification)); 544868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) message_center_notification->set_extension_id( 545868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) profile_notification->GetExtensionId()); 546868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) message_center_->AddNotification(message_center_notification.Pass()); 547868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 5482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) profile_notification->StartDownloads(); 5492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 5502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void MessageCenterNotificationManager::RemoveProfileNotification( 552ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch ProfileNotification* profile_notification) { 5532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::string id = profile_notification->notification().notification_id(); 5542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) profile_notifications_.erase(id); 5552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) delete profile_notification; 5562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 5572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)MessageCenterNotificationManager::ProfileNotification* 5592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) MessageCenterNotificationManager::FindProfileNotification( 5602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string& id) const { 5612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) NotificationMap::const_iterator iter = profile_notifications_.find(id); 562c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (iter == profile_notifications_.end()) 563c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return NULL; 564c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 5652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return (*iter).second; 5662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 567