Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: chrome/browser/notifications/notification_platform_bridge_linux.cc

Issue 2814973002: Linux native notifications: Add support for custom notification buttons (Closed)
Patch Set: Rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/chrome_notification_types.h" 17 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/notifications/native_notification_display_service.h" 18 #include "chrome/browser/notifications/native_notification_display_service.h"
18 #include "chrome/browser/notifications/notification.h" 19 #include "chrome/browser/notifications/notification.h"
19 #include "chrome/browser/notifications/notification_display_service_factory.h" 20 #include "chrome/browser/notifications/notification_display_service_factory.h"
20 #include "chrome/browser/profiles/profile_manager.h" 21 #include "chrome/browser/profiles/profile_manager.h"
21 #include "content/public/browser/notification_service.h" 22 #include "content/public/browser/notification_service.h"
22 23
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 NotificationCommon::Type notification_type; 169 NotificationCommon::Type notification_type;
169 const std::string notification_id; 170 const std::string notification_id;
170 const std::string profile_id; 171 const std::string profile_id;
171 const bool is_incognito; 172 const bool is_incognito;
172 173
173 // A copy of the origin_url from the underlying 174 // A copy of the origin_url from the underlying
174 // message_center::Notification. Used to pass back to 175 // message_center::Notification. Used to pass back to
175 // NativeNotificationDisplayService. 176 // NativeNotificationDisplayService.
176 const GURL origin_url; 177 const GURL origin_url;
177 178
179 // Used to keep track of the IDs of the buttons currently displayed
180 // on this notification.
Lei Zhang 2017/04/14 01:19:45 Mention the valid range of action IDs is [action_s
Tom (Use chromium acct) 2017/04/14 18:19:01 Done.
181 size_t action_start = 0;
182 size_t action_end = 0;
183
178 // Temporary resource files associated with the notification that 184 // Temporary resource files associated with the notification that
179 // should be cleaned up when the notification is closed or on 185 // should be cleaned up when the notification is closed or on
180 // shutdown. 186 // shutdown.
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
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 349
344 if (data->dbus_id) 350 if (data->dbus_id)
345 DCHECK(!data->cancellable); 351 DCHECK(!data->cancellable);
346 352
347 data->ResetResourceFiles(); 353 data->ResetResourceFiles();
348 354
349 GVariantBuilder actions_builder; 355 GVariantBuilder actions_builder;
350 // Even-indexed elements in this array are action IDs passed back to 356 // Even-indexed elements in this array are action IDs passed back to
351 // us in GSignalReceiver. Odd-indexed ones contain the button text. 357 // us in GSignalReceiver. Odd-indexed ones contain the button text.
352 g_variant_builder_init(&actions_builder, G_VARIANT_TYPE("as")); 358 g_variant_builder_init(&actions_builder, G_VARIANT_TYPE("as"));
359 data->action_start = data->action_end;
360 for (const auto& button_info : notification.buttons()) {
361 // FDO notification buttons can contain either an icon or a label,
362 // but not both, and the type of all buttons must be the same (all
363 // labels or all icons), so always use labels.
364 std::string id = base::SizeTToString(data->action_end++);
365 const std::string label = base::UTF16ToUTF8(button_info.title);
366 AddActionToNotification(&actions_builder, id.c_str(), label.c_str());
367 }
353 if (notification.clickable()) { 368 if (notification.clickable()) {
354 // Special case: the pair ("default", "") will not add a button, 369 // Special case: the pair ("default", "") will not add a button,
355 // but instead makes the entire notification clickable. 370 // but instead makes the entire notification clickable.
356 AddActionToNotification(&actions_builder, "default", ""); 371 AddActionToNotification(&actions_builder, "default", "");
357 } 372 }
358 // Always add a settings button. 373 // Always add a settings button.
359 AddActionToNotification(&actions_builder, "settings", "Settings"); 374 AddActionToNotification(&actions_builder, "settings", "Settings");
360 375
361 GVariantBuilder hints_builder; 376 GVariantBuilder hints_builder;
362 g_variant_builder_init(&hints_builder, G_VARIANT_TYPE("a{sv}")); 377 g_variant_builder_init(&hints_builder, G_VARIANT_TYPE("a{sv}"));
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 ForwardNotificationOperation(dbus_id, NotificationCommon::CLOSE, -1); 465 ForwardNotificationOperation(dbus_id, NotificationCommon::CLOSE, -1);
451 // std::unordered_map::erase(nullptr) is safe here. 466 // std::unordered_map::erase(nullptr) is safe here.
452 notifications_.erase(FindNotificationData(dbus_id)); 467 notifications_.erase(FindNotificationData(dbus_id));
453 } else if (strcmp("ActionInvoked", sender_signal) == 0 && 468 } else if (strcmp("ActionInvoked", sender_signal) == 0 &&
454 g_variant_is_of_type(parameters, G_VARIANT_TYPE("(us)"))) { 469 g_variant_is_of_type(parameters, G_VARIANT_TYPE("(us)"))) {
455 const gchar* action = nullptr; 470 const gchar* action = nullptr;
456 g_variant_get(parameters, "(u&s)", &dbus_id, &action); 471 g_variant_get(parameters, "(u&s)", &dbus_id, &action);
457 DCHECK(action); 472 DCHECK(action);
458 473
459 if (strcmp(action, "default") == 0) { 474 if (strcmp(action, "default") == 0) {
460 ForwardNotificationOperation(dbus_id, NotificationCommon::CLICK, 0); 475 ForwardNotificationOperation(dbus_id, NotificationCommon::CLICK, -1);
461 } else if (strcmp(action, "settings") == 0) { 476 } else if (strcmp(action, "settings") == 0) {
462 ForwardNotificationOperation(dbus_id, NotificationCommon::SETTINGS, -1); 477 ForwardNotificationOperation(dbus_id, NotificationCommon::SETTINGS, -1);
463 } else { 478 } else {
464 NOTIMPLEMENTED() << "No custom buttons just yet!"; 479 size_t id;
480 if (!base::StringToSizeT(action, &id))
481 return;
482 NotificationData* data = FindNotificationData(dbus_id);
483 if (!data)
484 return;
485 size_t n_buttons = data->action_end - data->action_start;
486 if (id >= n_buttons)
Lei Zhang 2017/04/14 01:19:45 On line 360, when it gets the first time, it's usi
Tom (Use chromium acct) 2017/04/14 18:19:01 Nice catch! This should have been "id - data->act
487 return;
488 ForwardNotificationOperation(dbus_id, NotificationCommon::CLICK,
489 id - data->action_start);
465 } 490 }
466 } 491 }
467 } 492 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698