OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/status_icons/desktop_notification_balloon.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/string_number_conversions.h" |
| 9 #include "base/threading/thread_restrictions.h" |
| 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/notifications/desktop_notification_service.h" |
| 12 #include "chrome/browser/notifications/notification.h" |
| 13 #include "chrome/browser/notifications/notification_delegate.h" |
| 14 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 15 #include "chrome/browser/profiles/profile_manager.h" |
| 16 #include "chrome/browser/ui/webui/web_ui_util.h" |
| 17 #include "third_party/skia/include/core/SkBitmap.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 void CloseBalloon(const std::string& id) { |
| 22 g_browser_process->notification_ui_manager()->CancelById(id); |
| 23 } |
| 24 |
| 25 // Prefix added to the notification ids. |
| 26 const char kNotificationPrefix[] = "desktop_notification_balloon."; |
| 27 |
| 28 // Timeout for automatically dismissing the notification balloon. |
| 29 const size_t kTimeoutMilliseconds = 6000; |
| 30 |
| 31 class DummyNotificationDelegate : public NotificationDelegate { |
| 32 public: |
| 33 explicit DummyNotificationDelegate(const std::string& id) |
| 34 : id_(kNotificationPrefix + id) {} |
| 35 virtual ~DummyNotificationDelegate() {} |
| 36 |
| 37 virtual void Display() OVERRIDE { |
| 38 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 39 base::Bind(&CloseBalloon, id()), kTimeoutMilliseconds); |
| 40 } |
| 41 virtual void Error() OVERRIDE {} |
| 42 virtual void Close(bool by_user) OVERRIDE {} |
| 43 virtual void Click() OVERRIDE {} |
| 44 virtual std::string id() const OVERRIDE { return id_; } |
| 45 |
| 46 private: |
| 47 std::string id_; |
| 48 }; |
| 49 |
| 50 } // anonymous namespace |
| 51 |
| 52 int DesktopNotificationBalloon::id_count_ = 1; |
| 53 |
| 54 DesktopNotificationBalloon::DesktopNotificationBalloon() { |
| 55 } |
| 56 |
| 57 DesktopNotificationBalloon::~DesktopNotificationBalloon() { |
| 58 if (notification_.get()) |
| 59 CloseBalloon(notification_->notification_id()); |
| 60 } |
| 61 |
| 62 void DesktopNotificationBalloon::DisplayBalloon(const SkBitmap& icon, |
| 63 const string16& title, |
| 64 const string16& contents) { |
| 65 GURL icon_url; |
| 66 if (!icon.empty()) |
| 67 icon_url = GURL(web_ui_util::GetImageDataUrl(icon)); |
| 68 |
| 69 GURL content_url(DesktopNotificationService::CreateDataUrl( |
| 70 icon_url, title, contents, WebKit::WebTextDirectionDefault)); |
| 71 |
| 72 notification_.reset(new Notification( |
| 73 GURL(), content_url, string16(), string16(), |
| 74 new DummyNotificationDelegate(base::IntToString(id_count_++)))); |
| 75 |
| 76 // Allowing IO access is required here to cover the corner case where |
| 77 // there is no last used profile and the default one is loaded. |
| 78 // IO access won't be required for normal uses. |
| 79 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 80 g_browser_process->notification_ui_manager()->Add( |
| 81 *notification_.get(), ProfileManager::GetLastUsedProfile()); |
| 82 } |
OLD | NEW |