This source file includes following definitions.
- stats_collector_
- Add
- Update
- FindById
- CancelById
- GetAllIdsByProfileAndSourceOrigin
- CancelAllBySourceOrigin
- CancelAllByProfile
- CancelAll
- OnNotificationRemoved
- OnCenterVisibilityChanged
- OnNotificationUpdated
- EnsureMessageCenterClosed
- SetMessageCenterTrayDelegateForTest
- Observe
- observer_
- StartDownloads
- StartDownloadWithImage
- DownloadComplete
- AddPendingDownload
- PendingDownloadCompleted
- downloads_
- StartDownloads
- OnDownloadsCompleted
- GetExtensionId
- AddProfileNotification
- RemoveProfileNotification
- FindProfileNotification
#include "chrome/browser/notifications/message_center_notification_manager.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/notifications/desktop_notification_service.h"
#include "chrome/browser/notifications/desktop_notification_service_factory.h"
#include "chrome/browser/notifications/fullscreen_notification_blocker.h"
#include "chrome/browser/notifications/message_center_settings_controller.h"
#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/notifications/screen_lock_notification_blocker.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/host_desktop.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/info_map.h"
#include "extensions/common/extension_set.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/message_center/message_center_style.h"
#include "ui/message_center/message_center_tray.h"
#include "ui/message_center/message_center_types.h"
#include "ui/message_center/notifier_settings.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/notifications/login_state_notification_blocker_chromeos.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
#endif
#if defined(USE_ASH)
#include "ash/shell.h"
#include "ash/system/web_notification/web_notification_tray.h"
#endif
#if defined(OS_WIN)
const int kFirstRunIdleDelaySeconds = 1;
#endif
MessageCenterNotificationManager::MessageCenterNotificationManager(
message_center::MessageCenter* message_center,
PrefService* local_state,
scoped_ptr<message_center::NotifierSettingsProvider> settings_provider)
: message_center_(message_center),
#if defined(OS_WIN)
first_run_idle_timeout_(
base::TimeDelta::FromSeconds(kFirstRunIdleDelaySeconds)),
weak_factory_(this),
#endif
settings_provider_(settings_provider.Pass()),
system_observer_(this),
stats_collector_(message_center) {
#if defined(OS_WIN)
first_run_pref_.Init(prefs::kMessageCenterShowedFirstRunBalloon, local_state);
#endif
message_center_->AddObserver(this);
message_center_->SetNotifierSettingsProvider(settings_provider_.get());
#if defined(OS_CHROMEOS)
blockers_.push_back(
new LoginStateNotificationBlockerChromeOS(message_center));
#else
blockers_.push_back(new ScreenLockNotificationBlocker(message_center));
#endif
blockers_.push_back(new FullscreenNotificationBlocker(message_center));
#if defined(OS_WIN) || defined(OS_MACOSX) \
|| (defined(USE_AURA) && !defined(USE_ASH))
tray_.reset(message_center::CreateMessageCenterTray());
#endif
registrar_.Add(this,
chrome::NOTIFICATION_FULLSCREEN_CHANGED,
content::NotificationService::AllSources());
}
MessageCenterNotificationManager::~MessageCenterNotificationManager() {
message_center_->RemoveObserver(this);
}
void MessageCenterNotificationManager::Add(const Notification& notification,
Profile* profile) {
if (Update(notification, profile))
return;
DesktopNotificationServiceFactory::GetForProfile(profile)->
ShowWelcomeNotificationIfNecessary(notification);
AddProfileNotification(
new ProfileNotification(profile, notification, message_center_));
}
bool MessageCenterNotificationManager::Update(const Notification& notification,
Profile* profile) {
const base::string16& replace_id = notification.replace_id();
if (replace_id.empty())
return false;
const GURL origin_url = notification.origin_url();
DCHECK(origin_url.is_valid());
for (NotificationMap::iterator iter = profile_notifications_.begin();
iter != profile_notifications_.end(); ++iter) {
ProfileNotification* old_notification = (*iter).second;
if (old_notification->notification().replace_id() == replace_id &&
old_notification->notification().origin_url() == origin_url &&
old_notification->profile() == profile) {
std::string old_id =
old_notification->notification().notification_id();
DCHECK(message_center_->HasNotification(old_id));
delete old_notification;
profile_notifications_.erase(old_id);
ProfileNotification* new_notification =
new ProfileNotification(profile, notification, message_center_);
profile_notifications_[notification.notification_id()] = new_notification;
scoped_ptr<message_center::Notification> message_center_notification(
make_scoped_ptr(new message_center::Notification(notification)));
message_center_->UpdateNotification(old_id,
message_center_notification.Pass());
new_notification->StartDownloads();
return true;
}
}
return false;
}
const Notification* MessageCenterNotificationManager::FindById(
const std::string& id) const {
NotificationMap::const_iterator iter = profile_notifications_.find(id);
if (iter == profile_notifications_.end())
return NULL;
return &(iter->second->notification());
}
bool MessageCenterNotificationManager::CancelById(const std::string& id) {
NotificationMap::iterator iter = profile_notifications_.find(id);
if (iter == profile_notifications_.end())
return false;
RemoveProfileNotification(iter->second);
message_center_->RemoveNotification(id, false);
return true;
}
std::set<std::string>
MessageCenterNotificationManager::GetAllIdsByProfileAndSourceOrigin(
Profile* profile,
const GURL& source) {
std::set<std::string> notification_ids;
for (NotificationMap::iterator iter = profile_notifications_.begin();
iter != profile_notifications_.end(); iter++) {
if ((*iter).second->notification().origin_url() == source &&
profile == (*iter).second->profile()) {
notification_ids.insert(iter->first);
}
}
return notification_ids;
}
bool MessageCenterNotificationManager::CancelAllBySourceOrigin(
const GURL& source) {
bool removed = false;
for (NotificationMap::iterator loopiter = profile_notifications_.begin();
loopiter != profile_notifications_.end(); ) {
NotificationMap::iterator curiter = loopiter++;
if ((*curiter).second->notification().origin_url() == source) {
const std::string id = curiter->first;
RemoveProfileNotification(curiter->second);
message_center_->RemoveNotification(id, false);
removed = true;
}
}
return removed;
}
bool MessageCenterNotificationManager::CancelAllByProfile(Profile* profile) {
bool removed = false;
for (NotificationMap::iterator loopiter = profile_notifications_.begin();
loopiter != profile_notifications_.end(); ) {
NotificationMap::iterator curiter = loopiter++;
if (profile == (*curiter).second->profile()) {
const std::string id = curiter->first;
RemoveProfileNotification(curiter->second);
message_center_->RemoveNotification(id, false);
removed = true;
}
}
return removed;
}
void MessageCenterNotificationManager::CancelAll() {
message_center_->RemoveAllNotifications( false);
}
void MessageCenterNotificationManager::OnNotificationRemoved(
const std::string& notification_id,
bool by_user) {
NotificationMap::const_iterator iter =
profile_notifications_.find(notification_id);
if (iter != profile_notifications_.end())
RemoveProfileNotification(iter->second);
#if defined(OS_WIN)
CheckFirstRunTimer();
#endif
}
void MessageCenterNotificationManager::OnCenterVisibilityChanged(
message_center::Visibility visibility) {
#if defined(OS_WIN)
if (visibility == message_center::VISIBILITY_TRANSIENT)
CheckFirstRunTimer();
#endif
}
void MessageCenterNotificationManager::OnNotificationUpdated(
const std::string& notification_id) {
#if defined(OS_WIN)
CheckFirstRunTimer();
#endif
}
void MessageCenterNotificationManager::EnsureMessageCenterClosed() {
if (tray_.get())
tray_->GetMessageCenterTray()->HideMessageCenterBubble();
#if defined(USE_ASH)
if (ash::Shell::HasInstance()) {
ash::WebNotificationTray* tray =
ash::Shell::GetInstance()->GetWebNotificationTray();
if (tray)
tray->GetMessageCenterTray()->HideMessageCenterBubble();
}
#endif
}
void MessageCenterNotificationManager::SetMessageCenterTrayDelegateForTest(
message_center::MessageCenterTrayDelegate* delegate) {
tray_.reset(delegate);
}
void MessageCenterNotificationManager::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
if (type == chrome::NOTIFICATION_FULLSCREEN_CHANGED) {
const bool is_fullscreen = *content::Details<bool>(details).ptr();
if (is_fullscreen && tray_.get() && tray_->GetMessageCenterTray())
tray_->GetMessageCenterTray()->HidePopupBubble();
}
}
MessageCenterNotificationManager::ImageDownloads::ImageDownloads(
message_center::MessageCenter* message_center,
ImageDownloadsObserver* observer)
: message_center_(message_center),
pending_downloads_(0),
observer_(observer) {
}
MessageCenterNotificationManager::ImageDownloads::~ImageDownloads() { }
void MessageCenterNotificationManager::ImageDownloads::StartDownloads(
const Notification& notification) {
AddPendingDownload();
StartDownloadWithImage(
notification,
¬ification.icon(),
notification.icon_url(),
base::Bind(&message_center::MessageCenter::SetNotificationIcon,
base::Unretained(message_center_),
notification.notification_id()));
StartDownloadWithImage(
notification,
NULL,
notification.image_url(),
base::Bind(&message_center::MessageCenter::SetNotificationImage,
base::Unretained(message_center_),
notification.notification_id()));
StartDownloadWithImage(
notification,
NULL,
notification.button_one_icon_url(),
base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon,
base::Unretained(message_center_),
notification.notification_id(),
0));
StartDownloadWithImage(
notification,
NULL,
notification.button_two_icon_url(),
base::Bind(&message_center::MessageCenter::SetNotificationButtonIcon,
base::Unretained(message_center_),
notification.notification_id(),
1));
PendingDownloadCompleted();
}
void MessageCenterNotificationManager::ImageDownloads::StartDownloadWithImage(
const Notification& notification,
const gfx::Image* image,
const GURL& url,
const SetImageCallback& callback) {
if (image && !image->IsEmpty()) {
callback.Run(*image);
return;
}
if (url.is_empty())
return;
content::RenderViewHost* host = notification.GetRenderViewHost();
if (!host) {
LOG(WARNING) << "Notification needs an image but has no RenderViewHost";
return;
}
content::WebContents* contents =
content::WebContents::FromRenderViewHost(host);
if (!contents) {
LOG(WARNING) << "Notification needs an image but has no WebContents";
return;
}
AddPendingDownload();
contents->DownloadImage(
url,
false,
0,
base::Bind(
&MessageCenterNotificationManager::ImageDownloads::DownloadComplete,
AsWeakPtr(),
callback));
}
void MessageCenterNotificationManager::ImageDownloads::DownloadComplete(
const SetImageCallback& callback,
int download_id,
int http_status_code,
const GURL& image_url,
const std::vector<SkBitmap>& bitmaps,
const std::vector<gfx::Size>& original_bitmap_sizes) {
PendingDownloadCompleted();
if (bitmaps.empty())
return;
gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmaps[0]);
callback.Run(image);
}
void MessageCenterNotificationManager::ImageDownloads::AddPendingDownload() {
++pending_downloads_;
}
void
MessageCenterNotificationManager::ImageDownloads::PendingDownloadCompleted() {
DCHECK(pending_downloads_ > 0);
if (--pending_downloads_ == 0 && observer_)
observer_->OnDownloadsCompleted();
}
MessageCenterNotificationManager::ProfileNotification::ProfileNotification(
Profile* profile,
const Notification& notification,
message_center::MessageCenter* message_center)
: profile_(profile),
notification_(notification),
downloads_(new ImageDownloads(message_center, this)) {
DCHECK(profile);
#if defined(OS_CHROMEOS)
notification_.set_profile_id(multi_user_util::GetUserIDFromProfile(profile));
#endif
}
MessageCenterNotificationManager::ProfileNotification::~ProfileNotification() {
}
void MessageCenterNotificationManager::ProfileNotification::StartDownloads() {
downloads_->StartDownloads(notification_);
}
void
MessageCenterNotificationManager::ProfileNotification::OnDownloadsCompleted() {
notification_.DoneRendering();
}
std::string
MessageCenterNotificationManager::ProfileNotification::GetExtensionId() {
extensions::InfoMap* extension_info_map =
extensions::ExtensionSystem::Get(profile())->info_map();
extensions::ExtensionSet extensions;
extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin(
notification().origin_url(), notification().process_id(),
extensions::APIPermission::kNotification, &extensions);
DesktopNotificationService* desktop_service =
DesktopNotificationServiceFactory::GetForProfile(profile());
for (extensions::ExtensionSet::const_iterator iter = extensions.begin();
iter != extensions.end(); ++iter) {
if (desktop_service->IsNotifierEnabled(message_center::NotifierId(
message_center::NotifierId::APPLICATION, (*iter)->id()))) {
return (*iter)->id();
}
}
return std::string();
}
void MessageCenterNotificationManager::AddProfileNotification(
ProfileNotification* profile_notification) {
const Notification& notification = profile_notification->notification();
std::string id = notification.notification_id();
DCHECK(profile_notifications_.find(id) == profile_notifications_.end());
profile_notifications_[id] = profile_notification;
scoped_ptr<message_center::Notification> message_center_notification(
new message_center::Notification(notification));
message_center_->AddNotification(message_center_notification.Pass());
profile_notification->StartDownloads();
}
void MessageCenterNotificationManager::RemoveProfileNotification(
ProfileNotification* profile_notification) {
std::string id = profile_notification->notification().notification_id();
profile_notifications_.erase(id);
delete profile_notification;
}
MessageCenterNotificationManager::ProfileNotification*
MessageCenterNotificationManager::FindProfileNotification(
const std::string& id) const {
NotificationMap::const_iterator iter = profile_notifications_.find(id);
if (iter == profile_notifications_.end())
return NULL;
return (*iter).second;
}