Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/notifications/notification_platform_bridge_linux.h" | 5 #include "chrome/browser/notifications/notification_platform_bridge_linux.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/strings/nullable_string16.h" | |
| 11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/notifications/native_notification_display_service.h" | |
| 13 #include "chrome/browser/notifications/notification.h" | 15 #include "chrome/browser/notifications/notification.h" |
| 16 #include "chrome/browser/notifications/notification_display_service_factory.h" | |
| 17 #include "chrome/browser/profiles/profile_manager.h" | |
| 14 | 18 |
| 15 namespace { | 19 namespace { |
| 16 | 20 |
| 17 const char kFreedesktopNotificationsName[] = "org.freedesktop.Notifications"; | 21 const char kFreedesktopNotificationsName[] = "org.freedesktop.Notifications"; |
| 18 const char kFreedesktopNotificationsPath[] = "/org/freedesktop/Notifications"; | 22 const char kFreedesktopNotificationsPath[] = "/org/freedesktop/Notifications"; |
| 19 | 23 |
| 20 // Callback used by GLib when the "Notify" message completes for the | 24 // Callback used by GLib when the "Notify" message completes for the |
| 21 // first time. | 25 // first time. |
| 22 void NotifyCompleteReceiver(GObject* source_object, | 26 void NotifyCompleteReceiver(GObject* source_object, |
| 23 GAsyncResult* result, | 27 GAsyncResult* result, |
| 24 gpointer user_data) { | 28 gpointer user_data) { |
| 25 GDBusProxy* proxy = G_DBUS_PROXY(source_object); | 29 GDBusProxy* proxy = G_DBUS_PROXY(source_object); |
| 26 GVariant* value = g_dbus_proxy_call_finish(proxy, result, nullptr); | 30 GVariant* value = g_dbus_proxy_call_finish(proxy, result, nullptr); |
| 27 if (!value) { | 31 if (!value) { |
| 28 // The message might have been cancelled, in which case | 32 // The message might have been cancelled, in which case |
| 29 // |user_data| points to a destroyed NotificationData. | 33 // |user_data| points to a destroyed NotificationData. |
| 30 return; | 34 return; |
| 31 } | 35 } |
| 32 auto* platform_bridge_linux = static_cast<NotificationPlatformBridgeLinux*>( | 36 auto* platform_bridge_linux = static_cast<NotificationPlatformBridgeLinux*>( |
| 33 g_browser_process->notification_platform_bridge()); | 37 g_browser_process->notification_platform_bridge()); |
| 34 platform_bridge_linux->NotifyCompleteInternal(user_data, value); | 38 platform_bridge_linux->NotifyCompleteInternal(user_data, value); |
| 35 } | 39 } |
| 36 | 40 |
| 41 // Runs once the profile has been loaded in order to perform a given | |
| 42 // |operation| on a notification. | |
| 43 void ProfileLoadedCallback(NotificationCommon::Operation operation, | |
|
Peter Beverloo
2017/04/07 01:50:48
FYI: I've written down a task for me to remove the
Tom (Use chromium acct)
2017/04/07 18:59:51
Acknowledged.
| |
| 44 NotificationCommon::Type notification_type, | |
| 45 const std::string& origin, | |
| 46 const std::string& notification_id, | |
| 47 int action_index, | |
| 48 const base::NullableString16& reply, | |
| 49 Profile* profile) { | |
| 50 if (!profile) | |
| 51 return; | |
| 52 | |
| 53 NotificationDisplayService* display_service = | |
| 54 NotificationDisplayServiceFactory::GetForProfile(profile); | |
| 55 | |
| 56 static_cast<NativeNotificationDisplayService*>(display_service) | |
| 57 ->ProcessNotificationOperation(operation, notification_type, origin, | |
| 58 notification_id, action_index, reply); | |
| 59 } | |
| 60 | |
| 37 } // namespace | 61 } // namespace |
| 38 | 62 |
| 39 // static | 63 // static |
| 40 NotificationPlatformBridge* NotificationPlatformBridge::Create() { | 64 NotificationPlatformBridge* NotificationPlatformBridge::Create() { |
| 41 GDBusProxy* notification_proxy = g_dbus_proxy_new_for_bus_sync( | 65 GDBusProxy* notification_proxy = g_dbus_proxy_new_for_bus_sync( |
| 42 G_BUS_TYPE_SESSION, | 66 G_BUS_TYPE_SESSION, |
| 43 static_cast<GDBusProxyFlags>(G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | | 67 static_cast<GDBusProxyFlags>(G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | |
| 44 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS | | |
| 45 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START), | 68 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START), |
| 46 nullptr, kFreedesktopNotificationsName, kFreedesktopNotificationsPath, | 69 nullptr, kFreedesktopNotificationsName, kFreedesktopNotificationsPath, |
| 47 kFreedesktopNotificationsName, nullptr, nullptr); | 70 kFreedesktopNotificationsName, nullptr, nullptr); |
| 48 if (!notification_proxy) | 71 if (!notification_proxy) |
| 49 return nullptr; | 72 return nullptr; |
| 50 return new NotificationPlatformBridgeLinux(notification_proxy); | 73 return new NotificationPlatformBridgeLinux(notification_proxy); |
| 51 } | 74 } |
| 52 | 75 |
| 53 struct NotificationPlatformBridgeLinux::NotificationData { | 76 struct NotificationPlatformBridgeLinux::NotificationData { |
| 54 NotificationData(const std::string& notification_id, | 77 NotificationData(NotificationCommon::Type notification_type, |
| 78 const std::string& notification_id, | |
| 55 const std::string& profile_id, | 79 const std::string& profile_id, |
| 56 bool is_incognito) | 80 bool is_incognito, |
| 57 : notification_id(notification_id), | 81 const GURL& origin_url) |
| 82 : notification_type(notification_type), | |
| 83 notification_id(notification_id), | |
| 58 profile_id(profile_id), | 84 profile_id(profile_id), |
| 59 is_incognito(is_incognito) {} | 85 is_incognito(is_incognito), |
| 86 origin_url(origin_url) {} | |
| 60 | 87 |
| 61 ~NotificationData() { | 88 ~NotificationData() { |
| 62 if (cancellable) | 89 if (cancellable) |
| 63 g_cancellable_cancel(cancellable); | 90 g_cancellable_cancel(cancellable); |
| 64 } | 91 } |
| 65 | 92 |
| 66 // The ID used by the notification server. Will be 0 until the | 93 // The ID used by the notification server. Will be 0 until the |
| 67 // first "Notify" message completes. | 94 // first "Notify" message completes. |
| 68 uint32_t dbus_id = 0; | 95 uint32_t dbus_id = 0; |
| 69 | 96 |
| 70 // Same parameters used by NotificationPlatformBridge::Display(). | 97 // Same parameters used by NotificationPlatformBridge::Display(). |
| 98 const NotificationCommon::Type notification_type; | |
| 71 const std::string notification_id; | 99 const std::string notification_id; |
| 72 const std::string profile_id; | 100 const std::string profile_id; |
| 73 const bool is_incognito; | 101 const bool is_incognito; |
| 74 | 102 |
| 103 // A copy of the origin_url from the underlying | |
| 104 // message_center::Notification. Used to pass back to | |
| 105 // NativeNotificationDisplayService. | |
| 106 const GURL origin_url; | |
| 107 | |
| 75 // Used to cancel the initial "Notify" message so we don't call | 108 // Used to cancel the initial "Notify" message so we don't call |
| 76 // NotificationPlatformBridgeLinux::NotifyCompleteInternal() with a | 109 // NotificationPlatformBridgeLinux::NotifyCompleteInternal() with a |
| 77 // destroyed Notification. | 110 // destroyed Notification. |
| 78 ScopedGObject<GCancellable> cancellable; | 111 ScopedGObject<GCancellable> cancellable; |
| 79 | 112 |
| 80 // If not null, the data to update the notification with once | 113 // If not null, the data to update the notification with once |
| 81 // |dbus_id| becomes available. | 114 // |dbus_id| becomes available. |
| 82 std::unique_ptr<Notification> update_data; | 115 std::unique_ptr<Notification> update_data; |
| 83 NotificationCommon::Type update_type = NotificationCommon::TYPE_MAX; | |
| 84 | 116 |
| 85 // If true, indicates the notification should be closed once | 117 // If true, indicates the notification should be closed once |
| 86 // |dbus_id| becomes available. | 118 // |dbus_id| becomes available. |
| 87 bool should_close = false; | 119 bool should_close = false; |
| 88 }; | 120 }; |
| 89 | 121 |
| 90 NotificationPlatformBridgeLinux::NotificationPlatformBridgeLinux( | 122 NotificationPlatformBridgeLinux::NotificationPlatformBridgeLinux( |
| 91 GDBusProxy* notification_proxy) | 123 GDBusProxy* notification_proxy) |
| 92 : notification_proxy_(notification_proxy) {} | 124 : notification_proxy_(notification_proxy) { |
| 125 proxy_signal_handler_ = g_signal_connect( | |
| 126 notification_proxy_, "g-signal", G_CALLBACK(GSignalReceiverThunk), this); | |
| 127 } | |
| 93 | 128 |
| 94 NotificationPlatformBridgeLinux::~NotificationPlatformBridgeLinux() {} | 129 NotificationPlatformBridgeLinux::~NotificationPlatformBridgeLinux() { |
| 130 if (proxy_signal_handler_) | |
| 131 g_signal_handler_disconnect(notification_proxy_, proxy_signal_handler_); | |
| 132 } | |
| 95 | 133 |
| 96 void NotificationPlatformBridgeLinux::Display( | 134 void NotificationPlatformBridgeLinux::Display( |
| 97 NotificationCommon::Type notification_type, | 135 NotificationCommon::Type notification_type, |
| 98 const std::string& notification_id, | 136 const std::string& notification_id, |
| 99 const std::string& profile_id, | 137 const std::string& profile_id, |
| 100 bool is_incognito, | 138 bool is_incognito, |
| 101 const Notification& notification) { | 139 const Notification& notification) { |
| 102 NotificationData* data = FindNotificationData(notification_id, profile_id); | 140 NotificationData* data = FindNotificationData(notification_id, profile_id); |
| 103 if (data) { | 141 if (data) { |
| 104 // Update an existing notification. | 142 // Update an existing notification. |
| 143 DCHECK_EQ(data->notification_type, notification_type); | |
| 144 DCHECK_EQ(data->is_incognito, is_incognito); | |
|
Peter Beverloo
2017/04/07 01:50:47
I think it'd be possible for an extension to creat
Tom (Use chromium acct)
2017/04/07 18:59:51
Done. This changes the Close() impl though since
| |
| 105 if (data->dbus_id) { | 145 if (data->dbus_id) { |
| 106 NotifyNow(data->dbus_id, notification_type, notification, nullptr, | 146 NotifyNow(data->dbus_id, notification_type, notification, nullptr, |
| 107 nullptr, nullptr); | 147 nullptr, nullptr); |
| 108 } else { | 148 } else { |
| 109 data->update_type = notification_type; | |
| 110 data->update_data = base::MakeUnique<Notification>(notification); | 149 data->update_data = base::MakeUnique<Notification>(notification); |
| 111 } | 150 } |
| 112 } else { | 151 } else { |
| 113 // Send the notification for the first time. | 152 // Send the notification for the first time. |
| 114 data = new NotificationData(notification_id, profile_id, is_incognito); | 153 data = new NotificationData(notification_type, notification_id, profile_id, |
| 154 is_incognito, notification.origin_url()); | |
| 115 data->cancellable.reset(g_cancellable_new()); | 155 data->cancellable.reset(g_cancellable_new()); |
| 116 notifications_.emplace(data, base::WrapUnique(data)); | 156 notifications_.emplace(data, base::WrapUnique(data)); |
| 117 NotifyNow(0, notification_type, notification, data->cancellable, | 157 NotifyNow(0, notification_type, notification, data->cancellable, |
| 118 NotifyCompleteReceiver, data); | 158 NotifyCompleteReceiver, data); |
| 119 } | 159 } |
| 120 } | 160 } |
| 121 | 161 |
| 122 void NotificationPlatformBridgeLinux::Close( | 162 void NotificationPlatformBridgeLinux::Close( |
| 123 const std::string& profile_id, | 163 const std::string& profile_id, |
| 124 const std::string& notification_id) { | 164 const std::string& notification_id) { |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 149 if (value && g_variant_is_of_type(value, G_VARIANT_TYPE("(u)"))) | 189 if (value && g_variant_is_of_type(value, G_VARIANT_TYPE("(u)"))) |
| 150 g_variant_get(value, "(u)", &data->dbus_id); | 190 g_variant_get(value, "(u)", &data->dbus_id); |
| 151 | 191 |
| 152 if (!data->dbus_id) { | 192 if (!data->dbus_id) { |
| 153 // There was some sort of error with creating the notification. | 193 // There was some sort of error with creating the notification. |
| 154 notifications_.erase(data); | 194 notifications_.erase(data); |
| 155 } else if (data->should_close) { | 195 } else if (data->should_close) { |
| 156 CloseNow(data->dbus_id); | 196 CloseNow(data->dbus_id); |
| 157 notifications_.erase(data); | 197 notifications_.erase(data); |
| 158 } else if (data->update_data) { | 198 } else if (data->update_data) { |
| 159 NotifyNow(data->dbus_id, data->update_type, *data->update_data, nullptr, | 199 NotifyNow(data->dbus_id, data->notification_type, *data->update_data, |
| 160 nullptr, nullptr); | 200 nullptr, nullptr, nullptr); |
| 161 data->update_data.reset(); | 201 data->update_data.reset(); |
| 162 } | 202 } |
| 163 } | 203 } |
| 164 | 204 |
| 165 void NotificationPlatformBridgeLinux::NotifyNow( | 205 void NotificationPlatformBridgeLinux::NotifyNow( |
| 166 uint32_t dbus_id, | 206 uint32_t dbus_id, |
| 167 NotificationCommon::Type notification_type, | 207 NotificationCommon::Type notification_type, |
| 168 const Notification& notification, | 208 const Notification& notification, |
| 169 GCancellable* cancellable, | 209 GCancellable* cancellable, |
| 170 GAsyncReadyCallback callback, | 210 GAsyncReadyCallback callback, |
| 171 gpointer user_data) { | 211 gpointer user_data) { |
| 172 // TODO(thomasanderson): Add a complete implementation. | 212 // TODO(thomasanderson): Add a complete implementation. |
| 213 | |
| 214 // Even-indexed elements in this array are action IDs passed back to | |
| 215 // us in GSignalReceiver. Odd-indexed ones contain the button text. | |
| 216 GVariantBuilder actions_builder; | |
| 217 g_variant_builder_init(&actions_builder, G_VARIANT_TYPE("as")); | |
| 218 if (notification.clickable()) { | |
| 219 // Special case: the pair ("default", "") will not add a button, | |
| 220 // but instead makes the entire notification clickable. | |
| 221 g_variant_builder_add(&actions_builder, "s", "default"); | |
| 222 g_variant_builder_add(&actions_builder, "s", ""); | |
| 223 } | |
| 224 // Always add a settings button. | |
| 225 g_variant_builder_add(&actions_builder, "s", "settings"); | |
| 226 g_variant_builder_add(&actions_builder, "s", "Settings"); | |
|
Peter Beverloo
2017/04/07 01:50:48
Cool!
Peter Beverloo
2017/04/07 01:50:48
In the mid-term it might be interesting to split f
Tom (Use chromium acct)
2017/04/07 18:59:51
Done.
| |
| 227 | |
| 173 const std::string title = base::UTF16ToUTF8(notification.title()); | 228 const std::string title = base::UTF16ToUTF8(notification.title()); |
| 174 const std::string message = base::UTF16ToUTF8(notification.message()); | 229 const std::string message = base::UTF16ToUTF8(notification.message()); |
| 230 | |
| 175 GVariant* parameters = | 231 GVariant* parameters = |
| 176 g_variant_new("(susssasa{sv}i)", "", dbus_id, "", title.c_str(), | 232 g_variant_new("(susssasa{sv}i)", "", dbus_id, "", title.c_str(), |
| 177 message.c_str(), nullptr, nullptr, -1); | 233 message.c_str(), &actions_builder, nullptr, -1); |
| 178 g_dbus_proxy_call(notification_proxy_, "Notify", parameters, | 234 g_dbus_proxy_call(notification_proxy_, "Notify", parameters, |
| 179 G_DBUS_CALL_FLAGS_NONE, -1, cancellable, callback, | 235 G_DBUS_CALL_FLAGS_NONE, -1, cancellable, callback, |
| 180 user_data); | 236 user_data); |
| 181 } | 237 } |
| 182 | 238 |
| 183 void NotificationPlatformBridgeLinux::CloseNow(uint32_t dbus_id) { | 239 void NotificationPlatformBridgeLinux::CloseNow(uint32_t dbus_id) { |
| 184 g_dbus_proxy_call(notification_proxy_, "CloseNotification", | 240 g_dbus_proxy_call(notification_proxy_, "CloseNotification", |
| 185 g_variant_new("(u)", dbus_id), G_DBUS_CALL_FLAGS_NONE, -1, | 241 g_variant_new("(u)", dbus_id), G_DBUS_CALL_FLAGS_NONE, -1, |
| 186 nullptr, nullptr, nullptr); | 242 nullptr, nullptr, nullptr); |
| 187 } | 243 } |
| 188 | 244 |
| 189 NotificationPlatformBridgeLinux::NotificationData* | 245 NotificationPlatformBridgeLinux::NotificationData* |
| 190 NotificationPlatformBridgeLinux::FindNotificationData( | 246 NotificationPlatformBridgeLinux::FindNotificationData( |
| 191 const std::string& notification_id, | 247 const std::string& notification_id, |
| 192 const std::string& profile_id) { | 248 const std::string& profile_id) { |
| 193 for (const auto& pair : notifications_) { | 249 for (const auto& pair : notifications_) { |
| 194 NotificationData* data = pair.first; | 250 NotificationData* data = pair.first; |
| 195 if (data->notification_id == notification_id && | 251 if (data->notification_id == notification_id && |
| 196 data->profile_id == profile_id) { | 252 data->profile_id == profile_id) { |
| 197 return data; | 253 return data; |
| 198 } | 254 } |
| 199 } | 255 } |
| 200 | 256 |
| 201 return nullptr; | 257 return nullptr; |
| 202 } | 258 } |
| 259 | |
| 260 NotificationPlatformBridgeLinux::NotificationData* | |
| 261 NotificationPlatformBridgeLinux::FindNotificationData(uint32_t dbus_id) { | |
| 262 for (const auto& pair : notifications_) { | |
| 263 NotificationData* data = pair.first; | |
| 264 if (data->dbus_id == dbus_id) | |
| 265 return data; | |
| 266 } | |
| 267 | |
| 268 return nullptr; | |
| 269 } | |
| 270 | |
| 271 void NotificationPlatformBridgeLinux::ForwardNotificationOperation( | |
| 272 uint32_t dbus_id, | |
| 273 NotificationCommon::Operation operation, | |
| 274 int action_index) { | |
| 275 NotificationData* data = FindNotificationData(dbus_id); | |
| 276 if (!data) { | |
| 277 // This notification either belongs to a different app or we | |
|
Peter Beverloo
2017/04/07 01:50:48
We can get notified about changes to notifications
Tom (Use chromium acct)
2017/04/07 18:59:51
Yes, the linux system is anarchy :(
| |
| 278 // already removed the NotificationData after sending a | |
| 279 // "CloseNotification" message. | |
| 280 return; | |
| 281 } | |
| 282 | |
| 283 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 284 DCHECK(profile_manager); | |
| 285 | |
| 286 profile_manager->LoadProfile( | |
| 287 data->profile_id, data->is_incognito, | |
| 288 base::Bind(&ProfileLoadedCallback, operation, data->notification_type, | |
| 289 data->origin_url.spec(), data->notification_id, action_index, | |
| 290 base::NullableString16())); | |
| 291 } | |
| 292 | |
| 293 void NotificationPlatformBridgeLinux::GSignalReceiver(GDBusProxy* proxy, | |
| 294 const char* sender_name, | |
| 295 const char* sender_signal, | |
| 296 GVariant* parameters) { | |
| 297 uint32_t dbus_id = 0; | |
| 298 if (strcmp("NotificationClosed", sender_signal) == 0 && | |
| 299 g_variant_is_of_type(parameters, G_VARIANT_TYPE("(uu)"))) { | |
| 300 uint32_t reason; | |
| 301 g_variant_get(parameters, "(uu)", &dbus_id, &reason); | |
| 302 ForwardNotificationOperation(dbus_id, NotificationCommon::CLOSE, -1); | |
| 303 notifications_.erase(FindNotificationData(dbus_id)); | |
|
Peter Beverloo
2017/04/07 01:50:48
Mm. I guess std::unordered_map_.erase(nullptr) is
Tom (Use chromium acct)
2017/04/07 18:59:51
Done.
| |
| 304 } else if (strcmp("ActionInvoked", sender_signal) == 0 && | |
| 305 g_variant_is_of_type(parameters, G_VARIANT_TYPE("(us)"))) { | |
| 306 const gchar* action; | |
| 307 g_variant_get(parameters, "(u&s)", &dbus_id, &action); | |
|
Peter Beverloo
2017/04/07 01:50:48
Should we DCHECK(action) here, or is that me being
Tom (Use chromium acct)
2017/04/07 18:59:51
Done.
| |
| 308 | |
| 309 if (strcmp(action, "default") == 0) { | |
| 310 ForwardNotificationOperation(dbus_id, NotificationCommon::CLICK, 0); | |
| 311 } else if (strcmp(action, "settings") == 0) { | |
| 312 ForwardNotificationOperation(dbus_id, NotificationCommon::SETTINGS, -1); | |
| 313 } else { | |
| 314 NOTIMPLEMENTED() << "No custom buttons just yet!"; | |
| 315 } | |
| 316 } | |
| 317 } | |
| OLD | NEW |