| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/notifications/system_notification.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "chrome/browser/browser_process.h" | |
| 9 #include "chrome/browser/chromeos/notifications/system_notification_factory.h" | |
| 10 #include "chrome/browser/notifications/notification.h" | |
| 11 #include "chrome/browser/notifications/notification_ui_manager.h" | |
| 12 #include "chrome/browser/ui/views/ash/balloon_collection_impl_ash.h" | |
| 13 #include "chrome/browser/ui/webui/web_ui_util.h" | |
| 14 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 15 | |
| 16 namespace chromeos { | |
| 17 | |
| 18 void SystemNotification::Init(int icon_resource_id) { | |
| 19 DBusThreadManager::Get()->GetSessionManagerClient()->AddObserver(this); | |
| 20 collection_ = static_cast<BalloonCollectionImplAsh*>( | |
| 21 g_browser_process->notification_ui_manager()->balloon_collection()); | |
| 22 std::string url = web_ui_util::GetImageDataUrlFromResource(icon_resource_id); | |
| 23 DCHECK(!url.empty()); | |
| 24 GURL tmp_gurl(url); | |
| 25 icon_.Swap(&tmp_gurl); | |
| 26 } | |
| 27 | |
| 28 SystemNotification::SystemNotification(Profile* profile, | |
| 29 NotificationDelegate* delegate, | |
| 30 int icon_resource_id, | |
| 31 const string16& title) | |
| 32 : profile_(profile), | |
| 33 collection_(NULL), | |
| 34 delegate_(delegate), | |
| 35 title_(title), | |
| 36 visible_(false), | |
| 37 sticky_(false), | |
| 38 urgent_(false), | |
| 39 show_on_unlock_(false) { | |
| 40 Init(icon_resource_id); | |
| 41 } | |
| 42 | |
| 43 SystemNotification::SystemNotification(Profile* profile, | |
| 44 const std::string& id, | |
| 45 int icon_resource_id, | |
| 46 const string16& title) | |
| 47 : profile_(profile), | |
| 48 collection_(NULL), | |
| 49 delegate_(new Delegate(id)), | |
| 50 title_(title), | |
| 51 visible_(false), | |
| 52 sticky_(false), | |
| 53 urgent_(false), | |
| 54 show_on_unlock_(false) { | |
| 55 Init(icon_resource_id); | |
| 56 } | |
| 57 | |
| 58 SystemNotification::~SystemNotification() { | |
| 59 DBusThreadManager::Get()->GetSessionManagerClient()->RemoveObserver(this); | |
| 60 } | |
| 61 | |
| 62 void SystemNotification::UnlockScreen() { | |
| 63 if (show_on_unlock_) { | |
| 64 DCHECK(!visible_); | |
| 65 Notification notify = SystemNotificationFactory::Create( | |
| 66 icon_, title_, message_, link_, delegate_.get()); | |
| 67 ShowNotification(notify); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 void SystemNotification::Show(const string16& message, | |
| 72 bool urgent, | |
| 73 bool sticky) { | |
| 74 Show(message, string16(), BalloonViewHost::MessageCallback(), urgent, sticky); | |
| 75 } | |
| 76 | |
| 77 void SystemNotification::Show(const string16& message, | |
| 78 const string16& link, | |
| 79 const BalloonViewHost::MessageCallback& callback, | |
| 80 bool urgent, | |
| 81 bool sticky) { | |
| 82 message_ = message; | |
| 83 link_ = link; | |
| 84 callback_ = callback; | |
| 85 sticky_ = sticky; | |
| 86 | |
| 87 if (DBusThreadManager::Get()->GetSessionManagerClient()-> | |
| 88 GetIsScreenLocked()) { | |
| 89 if (visible_ && urgent && !urgent_) { | |
| 90 // Hide the notification so that we show/update it on unlock. | |
| 91 Hide(); | |
| 92 urgent_ = true; | |
| 93 } | |
| 94 if (!visible_) | |
| 95 show_on_unlock_ = true; | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 Notification notify = SystemNotificationFactory::Create( | |
| 100 icon_, title_, message_, link_, delegate_.get()); | |
| 101 if (visible_) { | |
| 102 // Force showing a user hidden notification on an urgent transition. | |
| 103 if (urgent && !urgent_) { | |
| 104 if (!collection_->UpdateAndShowNotification(notify)) | |
| 105 visible_ = false; // re-show | |
| 106 } else { | |
| 107 collection_->UpdateNotification(notify); | |
| 108 } | |
| 109 } | |
| 110 if (!visible_) | |
| 111 ShowNotification(notify); | |
| 112 urgent_ = urgent; | |
| 113 } | |
| 114 | |
| 115 void SystemNotification::ShowNotification(const Notification& notify) { | |
| 116 collection_->AddSystemNotification(notify, profile_, sticky_); | |
| 117 collection_->AddWebUIMessageCallback(notify, "link", callback_); | |
| 118 visible_ = true; | |
| 119 } | |
| 120 | |
| 121 void SystemNotification::Hide() { | |
| 122 if (visible_) { | |
| 123 collection_->RemoveById(delegate_->id()); | |
| 124 visible_ = false; | |
| 125 urgent_ = false; | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 //////////////////////////////////////////////////////////////////////////////// | |
| 130 // SystemNotification::Delegate | |
| 131 | |
| 132 SystemNotification::Delegate::Delegate(const std::string& id) | |
| 133 : id_(id) { | |
| 134 } | |
| 135 | |
| 136 std::string SystemNotification::Delegate::id() const { | |
| 137 return id_; | |
| 138 } | |
| 139 | |
| 140 content::RenderViewHost* | |
| 141 SystemNotification::Delegate::GetRenderViewHost() const { | |
| 142 return NULL; | |
| 143 } | |
| 144 | |
| 145 SystemNotification::Delegate::~Delegate() {} | |
| 146 | |
| 147 } // namespace chromeos | |
| OLD | NEW |