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

Unified Diff: chrome/browser/ui/cocoa/notifications/notification_response_builder_mac.mm

Issue 2065943002: Abstract notification clicks in its own dictionary (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/cocoa/notifications/notification_response_builder_mac.mm
diff --git a/chrome/browser/ui/cocoa/notifications/notification_response_builder_mac.mm b/chrome/browser/ui/cocoa/notifications/notification_response_builder_mac.mm
new file mode 100644
index 0000000000000000000000000000000000000000..cf4df014184f8df80027d21ff31fe51e698472df
--- /dev/null
+++ b/chrome/browser/ui/cocoa/notifications/notification_response_builder_mac.mm
@@ -0,0 +1,98 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import "chrome/browser/ui/cocoa/notifications/notification_response_builder_mac.h"
+
+#include "base/logging.h"
+#include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h"
+
+namespace {
+
+// Make sure this Obj-C enum is kept in sync with the
+// PlatformNotificationServiceImpl NotificationOperation enum.
+// The latter cannot be reused because the XPC service is not aware of
+// PlatformNotificationCenter.
+enum NotificationOperation {
+ NOTIFICATION_CLICK = 0,
+ NOTIFICATION_CLOSE = 1,
+ NOTIFICATION_SETTINGS = 2
+};
+} // namespace
+
+@implementation NotificationResponseBuilder
+
++ (NSDictionary*)buildDictionary:(NSUserNotification*)notification {
+ NSString* origin =
+ [[notification userInfo]
+ objectForKey:notification_constants::kNotificationOrigin]
+ ? [[notification userInfo]
+ objectForKey:notification_constants::kNotificationOrigin]
+ : @"";
+ DCHECK([[notification userInfo]
+ objectForKey:notification_constants::kNotificationId]);
+ NSString* notificationId = [[notification userInfo]
+ objectForKey:notification_constants::kNotificationId];
+
+ DCHECK([[notification userInfo]
+ objectForKey:notification_constants::kNotificationProfileId]);
+ NSString* profileId = [[notification userInfo]
+ objectForKey:notification_constants::kNotificationProfileId];
+
+ DCHECK([[notification userInfo]
+ objectForKey:notification_constants::kNotificationIncognito]);
+ NSNumber* incognito = [[notification userInfo]
+ objectForKey:notification_constants::kNotificationIncognito];
+
+ // Initialize operation and button index for the case where the
+ // notification itself was clicked.
+ NotificationOperation operation = NOTIFICATION_CLICK;
+ int buttonIndex = -1;
+
+ // Determine whether the user clicked on a button, and if they did, whether it
+ // was a developer-provided button or the mandatory Settings button.
+ if (notification.activationType ==
+ NSUserNotificationActivationTypeActionButtonClicked) {
+ NSArray* alternateButtons = @[];
+ if ([notification
+ respondsToSelector:@selector(_alternateActionButtonTitles)]) {
+ alternateButtons =
+ [notification valueForKey:@"_alternateActionButtonTitles"];
+ }
+
+ bool multipleButtons = (alternateButtons.count > 0);
+
+ // No developer actions, just the settings button.
+ if (!multipleButtons) {
+ operation = NOTIFICATION_SETTINGS;
+ buttonIndex = -1;
+ } else {
+ // 0 based array containing.
+ // Button 1
+ // Button 2 (optional)
+ // Settings
+ NSNumber* actionIndex =
+ [notification valueForKey:@"_alternateActionIndex"];
+ operation = (actionIndex.unsignedLongValue == alternateButtons.count - 1)
+ ? NOTIFICATION_SETTINGS
+ : NOTIFICATION_CLICK;
+ buttonIndex =
+ (actionIndex.unsignedLongValue == alternateButtons.count - 1)
+ ? -1
+ : actionIndex.intValue;
+ }
+ }
+
+ return @{
+ notification_constants::kNotificationOrigin : origin,
+ notification_constants::kNotificationId : notificationId,
+ notification_constants::kNotificationProfileId : profileId,
+ notification_constants::kNotificationIncognito : incognito,
+ notification_constants::kNotificationOperation :
+ [NSNumber numberWithInt:operation],
+ notification_constants::
+ kNotificationButtonIndex : [NSNumber numberWithInt:buttonIndex],
+ };
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698