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/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/strings/nullable_string16.h" | 12 #include "base/strings/nullable_string16.h" |
13 #include "base/strings/string_number_conversions.h" | |
13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
14 #include "base/task_scheduler/post_task.h" | 15 #include "base/task_scheduler/post_task.h" |
15 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/notifications/native_notification_display_service.h" | 17 #include "chrome/browser/notifications/native_notification_display_service.h" |
17 #include "chrome/browser/notifications/notification.h" | 18 #include "chrome/browser/notifications/notification.h" |
18 #include "chrome/browser/notifications/notification_display_service_factory.h" | 19 #include "chrome/browser/notifications/notification_display_service_factory.h" |
19 #include "chrome/browser/profiles/profile_manager.h" | 20 #include "chrome/browser/profiles/profile_manager.h" |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
169 NotificationCommon::Type notification_type; | 170 NotificationCommon::Type notification_type; |
170 const std::string notification_id; | 171 const std::string notification_id; |
171 const std::string profile_id; | 172 const std::string profile_id; |
172 const bool is_incognito; | 173 const bool is_incognito; |
173 | 174 |
174 // A copy of the origin_url from the underlying | 175 // A copy of the origin_url from the underlying |
175 // message_center::Notification. Used to pass back to | 176 // message_center::Notification. Used to pass back to |
176 // NativeNotificationDisplayService. | 177 // NativeNotificationDisplayService. |
177 const GURL origin_url; | 178 const GURL origin_url; |
178 | 179 |
180 // Used to keep track of the IDs of the buttons currently displayed | |
181 // on this notification. | |
182 size_t action_start = 0; | |
183 size_t action_end = 0; | |
184 | |
179 // Temporary resource files associated with the notification that | 185 // Temporary resource files associated with the notification that |
180 // should be cleaned up when the notification is closed. | 186 // should be cleaned up when the notification is closed. |
181 std::vector<base::FilePath> resource_files; | 187 std::vector<base::FilePath> resource_files; |
182 | 188 |
183 // Used to cancel the initial "Notify" message so we don't call | 189 // Used to cancel the initial "Notify" message so we don't call |
184 // NotificationPlatformBridgeLinux::NotifyCompleteInternal() with a | 190 // NotificationPlatformBridgeLinux::NotifyCompleteInternal() with a |
185 // destroyed Notification. | 191 // destroyed Notification. |
186 ScopedGObject<GCancellable> cancellable; | 192 ScopedGObject<GCancellable> cancellable; |
187 | 193 |
188 // If not null, the data to update the notification with once | 194 // If not null, the data to update the notification with once |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
324 | 330 |
325 if (data->dbus_id) | 331 if (data->dbus_id) |
326 DCHECK(!data->cancellable); | 332 DCHECK(!data->cancellable); |
327 | 333 |
328 data->ResetResourceFiles(); | 334 data->ResetResourceFiles(); |
329 | 335 |
330 GVariantBuilder actions_builder; | 336 GVariantBuilder actions_builder; |
331 // Even-indexed elements in this array are action IDs passed back to | 337 // Even-indexed elements in this array are action IDs passed back to |
332 // us in GSignalReceiver. Odd-indexed ones contain the button text. | 338 // us in GSignalReceiver. Odd-indexed ones contain the button text. |
333 g_variant_builder_init(&actions_builder, G_VARIANT_TYPE("as")); | 339 g_variant_builder_init(&actions_builder, G_VARIANT_TYPE("as")); |
340 data->action_start = data->action_end; | |
341 for (const auto& button_info : notification.buttons()) { | |
342 // FDO notification buttons can contain either an icon or a label, | |
343 // but not both, and the type of all buttons must be the same (all | |
344 // labels or all icons), so always use labels. | |
345 std::string id = base::SizeTToString(data->action_end++); | |
346 const std::string label = base::UTF16ToUTF8(button_info.title); | |
347 AddActionToNotification(&actions_builder, id.c_str(), label.c_str()); | |
Peter Beverloo
2017/04/12 00:44:22
Are we concerned about the OS passing us invalid b
Tom (Use chromium acct)
2017/04/13 02:23:34
No, but I wanted to handle this case:
1. Notificat
| |
348 } | |
334 if (notification.clickable()) { | 349 if (notification.clickable()) { |
335 // Special case: the pair ("default", "") will not add a button, | 350 // Special case: the pair ("default", "") will not add a button, |
336 // but instead makes the entire notification clickable. | 351 // but instead makes the entire notification clickable. |
337 AddActionToNotification(&actions_builder, "default", ""); | 352 AddActionToNotification(&actions_builder, "default", ""); |
338 } | 353 } |
339 // Always add a settings button. | 354 // Always add a settings button. |
340 AddActionToNotification(&actions_builder, "settings", "Settings"); | 355 AddActionToNotification(&actions_builder, "settings", "Settings"); |
341 | 356 |
342 GVariantBuilder hints_builder; | 357 GVariantBuilder hints_builder; |
343 g_variant_builder_init(&hints_builder, G_VARIANT_TYPE("a{sv}")); | 358 g_variant_builder_init(&hints_builder, G_VARIANT_TYPE("a{sv}")); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
431 ForwardNotificationOperation(dbus_id, NotificationCommon::CLOSE, -1); | 446 ForwardNotificationOperation(dbus_id, NotificationCommon::CLOSE, -1); |
432 // std::unordered_map::erase(nullptr) is safe here. | 447 // std::unordered_map::erase(nullptr) is safe here. |
433 notifications_.erase(FindNotificationData(dbus_id)); | 448 notifications_.erase(FindNotificationData(dbus_id)); |
434 } else if (strcmp("ActionInvoked", sender_signal) == 0 && | 449 } else if (strcmp("ActionInvoked", sender_signal) == 0 && |
435 g_variant_is_of_type(parameters, G_VARIANT_TYPE("(us)"))) { | 450 g_variant_is_of_type(parameters, G_VARIANT_TYPE("(us)"))) { |
436 const gchar* action = nullptr; | 451 const gchar* action = nullptr; |
437 g_variant_get(parameters, "(u&s)", &dbus_id, &action); | 452 g_variant_get(parameters, "(u&s)", &dbus_id, &action); |
438 DCHECK(action); | 453 DCHECK(action); |
439 | 454 |
440 if (strcmp(action, "default") == 0) { | 455 if (strcmp(action, "default") == 0) { |
441 ForwardNotificationOperation(dbus_id, NotificationCommon::CLICK, 0); | 456 ForwardNotificationOperation(dbus_id, NotificationCommon::CLICK, -1); |
442 } else if (strcmp(action, "settings") == 0) { | 457 } else if (strcmp(action, "settings") == 0) { |
443 ForwardNotificationOperation(dbus_id, NotificationCommon::SETTINGS, -1); | 458 ForwardNotificationOperation(dbus_id, NotificationCommon::SETTINGS, -1); |
444 } else { | 459 } else { |
445 NOTIMPLEMENTED() << "No custom buttons just yet!"; | 460 size_t id; |
461 if (!base::StringToSizeT(action, &id)) | |
462 return; | |
463 NotificationData* data = FindNotificationData(dbus_id); | |
464 if (!data) | |
465 return; | |
466 size_t n_buttons = data->action_end - data->action_start; | |
467 if (id >= n_buttons) | |
468 return; | |
469 ForwardNotificationOperation(dbus_id, NotificationCommon::CLICK, | |
470 id - data->action_start); | |
446 } | 471 } |
447 } | 472 } |
448 } | 473 } |
OLD | NEW |