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/notification.h"
15 #include "chrome/browser/notifications/notification_delegate.h"
16 #include "chrome/browser/notifications/notification_ui_manager.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "ui/gfx/image/image_skia.h"
23 void CloseBalloon(const std::string
& id
, ProfileID profile_id
) {
24 // The browser process may have gone away during shutting down, in this case
25 // notification_ui_manager() will close the balloon in its destructor.
26 if (!g_browser_process
)
29 g_browser_process
->notification_ui_manager()->CancelById(id
, profile_id
);
32 // Prefix added to the notification ids.
33 const char kNotificationPrefix
[] = "desktop_notification_balloon.";
35 // Timeout for automatically dismissing the notification balloon.
36 const size_t kTimeoutSeconds
= 6;
38 class DummyNotificationDelegate
: public NotificationDelegate
{
40 explicit DummyNotificationDelegate(const std::string
& id
, Profile
* profile
)
41 : id_(kNotificationPrefix
+ id
), profile_(profile
) {}
43 void Display() override
{
44 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
45 FROM_HERE
, base::Bind(&CloseBalloon
, id(),
46 NotificationUIManager::GetProfileID(profile_
)),
47 base::TimeDelta::FromSeconds(kTimeoutSeconds
));
49 std::string
id() const override
{ return id_
; }
52 ~DummyNotificationDelegate() override
{}
58 } // anonymous namespace
60 int DesktopNotificationBalloon::id_count_
= 1;
62 DesktopNotificationBalloon::DesktopNotificationBalloon() : profile_(NULL
) {
65 DesktopNotificationBalloon::~DesktopNotificationBalloon() {
66 if (!notification_id_
.empty())
67 CloseBalloon(notification_id_
,
68 NotificationUIManager::GetProfileID(profile_
));
71 void DesktopNotificationBalloon::DisplayBalloon(
72 const gfx::ImageSkia
& icon
,
73 const base::string16
& title
,
74 const base::string16
& contents
) {
75 // Allowing IO access is required here to cover the corner case where
76 // there is no last used profile and the default one is loaded.
77 // IO access won't be required for normal uses.
80 base::ThreadRestrictions::ScopedAllowIO allow_io
;
81 profile
= ProfileManager::GetLastUsedProfile();
84 NotificationDelegate
* delegate
=
85 new DummyNotificationDelegate(base::IntToString(id_count_
++), profile_
);
86 Notification
notification(GURL(), title
, contents
, gfx::Image(icon
),
87 base::string16(), std::string(), delegate
);
89 g_browser_process
->notification_ui_manager()->Add(notification
, profile
);
91 notification_id_
= notification
.delegate_id();