1 // Copyright (c) 2012 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.
5 #include "chrome/browser/status_icons/desktop_notification_balloon.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/threading/thread_restrictions.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/notifications/desktop_notification_service.h"
15 #include "chrome/browser/notifications/notification.h"
16 #include "chrome/browser/notifications/notification_delegate.h"
17 #include "chrome/browser/notifications/notification_ui_manager.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "third_party/skia/include/core/SkBitmap.h"
20 #include "ui/gfx/image/image_skia.h"
24 void CloseBalloon(const std::string
& id
, ProfileID profile_id
) {
25 // The browser process may have gone away during shutting down, in this case
26 // notification_ui_manager() will close the balloon in its destructor.
27 if (!g_browser_process
)
30 g_browser_process
->notification_ui_manager()->CancelById(id
, profile_id
);
33 // Prefix added to the notification ids.
34 const char kNotificationPrefix
[] = "desktop_notification_balloon.";
36 // Timeout for automatically dismissing the notification balloon.
37 const size_t kTimeoutSeconds
= 6;
39 class DummyNotificationDelegate
: public NotificationDelegate
{
41 explicit DummyNotificationDelegate(const std::string
& id
, Profile
* profile
)
42 : id_(kNotificationPrefix
+ id
), profile_(profile
) {}
44 void Display() override
{
45 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
46 FROM_HERE
, base::Bind(&CloseBalloon
, id(),
47 NotificationUIManager::GetProfileID(profile_
)),
48 base::TimeDelta::FromSeconds(kTimeoutSeconds
));
50 std::string
id() const override
{ return id_
; }
53 ~DummyNotificationDelegate() override
{}
59 } // anonymous namespace
61 int DesktopNotificationBalloon::id_count_
= 1;
63 DesktopNotificationBalloon::DesktopNotificationBalloon() : profile_(NULL
) {
66 DesktopNotificationBalloon::~DesktopNotificationBalloon() {
67 if (!notification_id_
.empty())
68 CloseBalloon(notification_id_
,
69 NotificationUIManager::GetProfileID(profile_
));
72 void DesktopNotificationBalloon::DisplayBalloon(
73 const gfx::ImageSkia
& icon
,
74 const base::string16
& title
,
75 const base::string16
& contents
) {
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.
81 base::ThreadRestrictions::ScopedAllowIO allow_io
;
82 profile
= ProfileManager::GetLastUsedProfile();
85 NotificationDelegate
* delegate
=
86 new DummyNotificationDelegate(base::IntToString(id_count_
++), profile_
);
87 Notification
notification(GURL(), title
, contents
, gfx::Image(icon
),
88 base::string16(), std::string(), delegate
);
90 g_browser_process
->notification_ui_manager()->Add(notification
, profile
);
92 notification_id_
= notification
.delegate_id();