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/string_number_conversions.h" | |
8 #include "base/threading/thread_restrictions.h" | |
9 #include "chrome/browser/browser_process.h" | |
10 #include "chrome/browser/notifications/desktop_notification_service.h" | |
11 #include "chrome/browser/notifications/notification.h" | |
12 #include "chrome/browser/notifications/notification_delegate.h" | |
13 #include "chrome/browser/notifications/notification_ui_manager.h" | |
14 #include "chrome/browser/profiles/profile_manager.h" | |
15 #include "chrome/browser/ui/webui/web_ui_util.h" | |
16 #include "third_party/skia/include/core/SkBitmap.h" | |
17 | |
18 namespace { | |
19 | |
20 // Prefix added to the notification ids. | |
21 const char kNotificationPrefix[] = "desktop_notification_balloon."; | |
22 | |
23 class DummyNotificationDelegate : public NotificationDelegate { | |
24 public: | |
25 explicit DummyNotificationDelegate(const std::string& id) : id_(id) {} | |
26 virtual ~DummyNotificationDelegate() {} | |
27 | |
28 virtual void Display() OVERRIDE {} | |
29 virtual void Error() OVERRIDE {} | |
30 virtual void Close(bool by_user) OVERRIDE {} | |
31 virtual void Click() OVERRIDE {} | |
32 virtual std::string id() const OVERRIDE { return kNotificationPrefix + id_; } | |
Andrew T Wilson (Slow)
2011/10/26 22:20:26
Rather than having to regenerate a new string ever
Leandro GraciĆ” Gil
2011/10/26 23:10:02
Done.
| |
33 | |
34 private: | |
35 std::string id_; | |
36 }; | |
37 | |
38 } // anonymous namespace | |
39 | |
40 int DesktopNotificationBalloon::id_count_ = 1; | |
41 | |
42 DesktopNotificationBalloon::DesktopNotificationBalloon() { | |
43 } | |
44 | |
45 DesktopNotificationBalloon::~DesktopNotificationBalloon() { | |
46 if (notification_.get()) { | |
47 g_browser_process->notification_ui_manager()->CancelById( | |
48 notification_->notification_id()); | |
49 } | |
50 } | |
51 | |
52 void DesktopNotificationBalloon::DisplayBalloon(const SkBitmap& icon, | |
53 const string16& title, | |
54 const string16& contents) { | |
55 GURL icon_url; | |
56 if (!icon.empty()) | |
57 icon_url = GURL(web_ui_util::GetImageDataUrl(icon)); | |
58 | |
59 GURL content_url(DesktopNotificationService::CreateDataUrl( | |
60 icon_url, title, contents, WebKit::WebTextDirectionDefault)); | |
61 | |
62 notification_.reset(new Notification( | |
63 GURL(), content_url, string16(), string16(), | |
64 new DummyNotificationDelegate(base::IntToString(id_count_++)))); | |
65 | |
66 // Allowing IO access is required here to cover the corner case where | |
67 // there is no last used profile and the default one is loaded. | |
68 // IO access won't be required for normal uses. | |
69 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
70 g_browser_process->notification_ui_manager()->Add( | |
71 *notification_.get(), ProfileManager::GetLastUsedProfile()); | |
72 } | |
OLD | NEW |