diff options
author | leandrogracia@chromium.org <leandrogracia@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-28 15:36:42 +0000 |
---|---|---|
committer | leandrogracia@chromium.org <leandrogracia@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-28 15:36:42 +0000 |
commit | 8c18395581c5dd8db090244ba6a2d95d08e8cf5d (patch) | |
tree | d0522014bb7b7cbb498b1fbb0eb6410f2ae9968e /chrome/browser/status_icons | |
parent | 0703a1777f1925b54d141ea570e72c3c8219de08 (diff) | |
download | chromium_src-8c18395581c5dd8db090244ba6a2d95d08e8cf5d.zip chromium_src-8c18395581c5dd8db090244ba6a2d95d08e8cf5d.tar.gz chromium_src-8c18395581c5dd8db090244ba6a2d95d08e8cf5d.tar.bz2 |
Analyzed the problem and tested 30+ times the affected and related tests without problems. Test failure seems to be a non-related flake.
Revert 107727 - Revert 107722 - Implement notifications for the DisplayBalloon method on Linux and Mac.
BUG=74970
TEST=install background app on window, see desktop notification.
Review URL: http://codereview.chromium.org/8375027
TBR=leandrogracia@chromium.org
Review URL: http://codereview.chromium.org/8417027
TBR=rohitrao@chromium.org
Review URL: http://codereview.chromium.org/8414024
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@107738 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/status_icons')
-rw-r--r-- | chrome/browser/status_icons/desktop_notification_balloon.cc | 82 | ||||
-rw-r--r-- | chrome/browser/status_icons/desktop_notification_balloon.h | 37 |
2 files changed, 119 insertions, 0 deletions
diff --git a/chrome/browser/status_icons/desktop_notification_balloon.cc b/chrome/browser/status_icons/desktop_notification_balloon.cc new file mode 100644 index 0000000..4f1777a --- /dev/null +++ b/chrome/browser/status_icons/desktop_notification_balloon.cc @@ -0,0 +1,82 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/status_icons/desktop_notification_balloon.h" + +#include "base/bind.h" +#include "base/string_number_conversions.h" +#include "base/threading/thread_restrictions.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/notifications/desktop_notification_service.h" +#include "chrome/browser/notifications/notification.h" +#include "chrome/browser/notifications/notification_delegate.h" +#include "chrome/browser/notifications/notification_ui_manager.h" +#include "chrome/browser/profiles/profile_manager.h" +#include "chrome/browser/ui/webui/web_ui_util.h" +#include "third_party/skia/include/core/SkBitmap.h" + +namespace { + +void CloseBalloon(const std::string& id) { + g_browser_process->notification_ui_manager()->CancelById(id); +} + +// Prefix added to the notification ids. +const char kNotificationPrefix[] = "desktop_notification_balloon."; + +// Timeout for automatically dismissing the notification balloon. +const size_t kTimeoutMilliseconds = 6000; + +class DummyNotificationDelegate : public NotificationDelegate { + public: + explicit DummyNotificationDelegate(const std::string& id) + : id_(kNotificationPrefix + id) {} + virtual ~DummyNotificationDelegate() {} + + virtual void Display() OVERRIDE { + MessageLoop::current()->PostDelayedTask(FROM_HERE, + base::Bind(&CloseBalloon, id()), kTimeoutMilliseconds); + } + virtual void Error() OVERRIDE {} + virtual void Close(bool by_user) OVERRIDE {} + virtual void Click() OVERRIDE {} + virtual std::string id() const OVERRIDE { return id_; } + + private: + std::string id_; +}; + +} // anonymous namespace + +int DesktopNotificationBalloon::id_count_ = 1; + +DesktopNotificationBalloon::DesktopNotificationBalloon() { +} + +DesktopNotificationBalloon::~DesktopNotificationBalloon() { + if (notification_.get()) + CloseBalloon(notification_->notification_id()); +} + +void DesktopNotificationBalloon::DisplayBalloon(const SkBitmap& icon, + const string16& title, + const string16& contents) { + GURL icon_url; + if (!icon.empty()) + icon_url = GURL(web_ui_util::GetImageDataUrl(icon)); + + GURL content_url(DesktopNotificationService::CreateDataUrl( + icon_url, title, contents, WebKit::WebTextDirectionDefault)); + + notification_.reset(new Notification( + GURL(), content_url, string16(), string16(), + new DummyNotificationDelegate(base::IntToString(id_count_++)))); + + // Allowing IO access is required here to cover the corner case where + // there is no last used profile and the default one is loaded. + // IO access won't be required for normal uses. + base::ThreadRestrictions::ScopedAllowIO allow_io; + g_browser_process->notification_ui_manager()->Add( + *notification_.get(), ProfileManager::GetLastUsedProfile()); +} diff --git a/chrome/browser/status_icons/desktop_notification_balloon.h b/chrome/browser/status_icons/desktop_notification_balloon.h new file mode 100644 index 0000000..2a44f2b --- /dev/null +++ b/chrome/browser/status_icons/desktop_notification_balloon.h @@ -0,0 +1,37 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_STATUS_ICONS_DESKTOP_NOTIFICATION_BALLOON_H_ +#define CHROME_BROWSER_STATUS_ICONS_DESKTOP_NOTIFICATION_BALLOON_H_ +#pragma once + +#include "base/compiler_specific.h" +#include "base/memory/scoped_ptr.h" +#include "base/string16.h" + +class Notification; +class SkBitmap; + +// Provides the notification balloon functionality by using desktop +// notifications to platforms that don't have a specific native API. +class DesktopNotificationBalloon { + public: + DesktopNotificationBalloon(); + virtual ~DesktopNotificationBalloon(); + + void DisplayBalloon(const SkBitmap& icon, + const string16& title, + const string16& contents); + + private: + // Notification balloon. + scoped_ptr<Notification> notification_; + + // Counter to provide unique ids to notifications. + static int id_count_; + + DISALLOW_COPY_AND_ASSIGN(DesktopNotificationBalloon); +}; + +#endif // CHROME_BROWSER_STATUS_ICONS_DESKTOP_NOTIFICATION_BALLOON_H_ |