Chromium Code Reviews| Index: chrome/browser/status_icons/desktop_notification_balloon_impl.cc |
| diff --git a/chrome/browser/status_icons/desktop_notification_balloon_impl.cc b/chrome/browser/status_icons/desktop_notification_balloon_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d4a6df96c3e4bf2ef459c1720cbca710f7eccc4d |
| --- /dev/null |
| +++ b/chrome/browser/status_icons/desktop_notification_balloon_impl.cc |
| @@ -0,0 +1,77 @@ |
| +// 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_impl.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 { |
| + |
| +// Prefix added to the notification ids. |
| +const char kNotificationPrefix[] = "desktop_notification_balloon."; |
| + |
| +void CloseBalloon(const std::string id) { |
|
Andrew T Wilson (Slow)
2011/10/26 01:21:18
Is there a reason why this is broken out rather th
Leandro Graciá Gil
2011/10/26 13:52:00
This comes from a previous testing version that im
|
| + g_browser_process->notification_ui_manager()->CancelById(id); |
| +} |
| + |
| +class DummyNotificationDelegate : public NotificationDelegate { |
| + public: |
| + explicit DummyNotificationDelegate(const std::string& id) : id_(id) {} |
| + virtual ~DummyNotificationDelegate() {} |
| + |
| + virtual void Display() OVERRIDE {} |
| + virtual void Error() OVERRIDE {} |
| + virtual void Close(bool by_user) OVERRIDE {} |
| + virtual void Click() OVERRIDE {} |
| + virtual std::string id() const OVERRIDE { return kNotificationPrefix + id_; } |
| + |
| + private: |
| + std::string id_; |
| +}; |
| + |
| +} // anonymous namespace |
| + |
| +int DesktopNotificationBalloonImpl::id_count_ = 1; |
| + |
| +DesktopNotificationBalloonImpl::DesktopNotificationBalloonImpl() { |
| +} |
| + |
| +DesktopNotificationBalloonImpl::~DesktopNotificationBalloonImpl() { |
| + CloseBalloon(notification_->notification_id()); |
| +} |
| + |
| +void DesktopNotificationBalloonImpl::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_++)))); |
| + |
| + // Allow IO access here to get the default profile since a profile is |
| + // required for the rendering of the notification UI. No specific profile |
| + // can be required by this API because of design reasons (e.g. this is also |
| + // used by the background manager which doesn't know about profiles). |
| + // The FILE thread cannot be used here as it will raise an invalid thread |
| + // assertion when trying to add the notification. |
| + // The only option left is to allow IO as an exception. |
| + base::ThreadRestrictions::ScopedAllowIO allow_io; |
| + g_browser_process->notification_ui_manager()->Add( |
|
Andrew T Wilson (Slow)
2011/10/26 01:21:18
This makes me somewhat nervous - can you have mira
Leandro Graciá Gil
2011/10/26 13:52:00
Done.
|
| + *notification_.get(), ProfileManager::GetDefaultProfile()); |
| +} |