Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / status_icons / desktop_notification_balloon.cc
blob528df5822670e56c76e122cc7b9793afec88a0e6
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"
7 #include "base/bind.h"
8 #include "base/strings/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 "third_party/skia/include/core/SkBitmap.h"
17 #include "ui/gfx/image/image_skia.h"
19 namespace {
21 void CloseBalloon(const std::string& id, ProfileID profile_id) {
22 // The browser process may have gone away during shutting down, in this case
23 // notification_ui_manager() will close the balloon in its destructor.
24 if (!g_browser_process)
25 return;
27 g_browser_process->notification_ui_manager()->CancelById(id, profile_id);
30 // Prefix added to the notification ids.
31 const char kNotificationPrefix[] = "desktop_notification_balloon.";
33 // Timeout for automatically dismissing the notification balloon.
34 const size_t kTimeoutSeconds = 6;
36 class DummyNotificationDelegate : public NotificationDelegate {
37 public:
38 explicit DummyNotificationDelegate(const std::string& id, Profile* profile)
39 : id_(kNotificationPrefix + id), profile_(profile) {}
41 virtual void Display() override {
42 base::MessageLoop::current()->PostDelayedTask(
43 FROM_HERE,
44 base::Bind(
45 &CloseBalloon, id(), NotificationUIManager::GetProfileID(profile_)),
46 base::TimeDelta::FromSeconds(kTimeoutSeconds));
48 virtual void Error() override {}
49 virtual void Close(bool by_user) override {}
50 virtual void Click() override {}
51 virtual std::string id() const override { return id_; }
53 private:
54 virtual ~DummyNotificationDelegate() {}
56 std::string id_;
57 Profile* profile_;
60 } // anonymous namespace
62 int DesktopNotificationBalloon::id_count_ = 1;
64 DesktopNotificationBalloon::DesktopNotificationBalloon() : profile_(NULL) {
67 DesktopNotificationBalloon::~DesktopNotificationBalloon() {
68 if (!notification_id_.empty())
69 CloseBalloon(notification_id_,
70 NotificationUIManager::GetProfileID(profile_));
73 void DesktopNotificationBalloon::DisplayBalloon(
74 const gfx::ImageSkia& icon,
75 const base::string16& title,
76 const base::string16& contents) {
77 // Allowing IO access is required here to cover the corner case where
78 // there is no last used profile and the default one is loaded.
79 // IO access won't be required for normal uses.
80 Profile* profile;
82 base::ThreadRestrictions::ScopedAllowIO allow_io;
83 profile = ProfileManager::GetLastUsedProfile();
85 notification_id_ = DesktopNotificationService::AddIconNotification(
86 GURL(),
87 title,
88 contents,
89 gfx::Image(icon),
90 base::string16(),
91 new DummyNotificationDelegate(base::IntToString(id_count_++), profile_),
92 profile);
93 profile_ = profile;