Skip to content

Commit

Permalink
Merge pull request #3907 from /issues/6818
Browse files Browse the repository at this point in the history
Remove macOS changes for "Fix user earns while native Windows and macOS notifications are suppressed"
  • Loading branch information
tmancey authored Nov 7, 2019
2 parents 16a085b + a44238b commit 8dc603c
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 96 deletions.
6 changes: 0 additions & 6 deletions components/brave_ads/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@ source_set("browser") {
"//ui/message_center/public/cpp",
]

if (is_mac) {
libs = [
"UserNotifications.framework",
]
}

if (is_win) {
deps += [
"//ui/views",
Expand Down
194 changes: 104 additions & 90 deletions components/brave_ads/browser/notification_helper_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#import <Cocoa/Cocoa.h>
#import <UserNotifications/UserNotifications.h>

// TODO(https://github.com/brave/brave-browser/issues/5541): Uncomment below
// code when notification_platform_bridge_mac.mm has been updated to use
// UNUserNotificationCenter
// #import <UserNotifications/UserNotifications.h>

#include "base/mac/mac_util.h"
#include "base/feature_list.h"
Expand Down Expand Up @@ -70,111 +74,121 @@
///////////////////////////////////////////////////////////////////////////////

bool NotificationHelperMac::IsAuthorized() const {
#if !defined(OFFICIAL_BUILD)
LOG(WARNING) << "Unable to detect the status of native notifications on non"
" official builds as the app is not code signed";
// TODO(https://github.com/brave/brave-browser/issues/5541): Uncomment below
// code when notification_platform_bridge_mac.mm has been updated to use
// UNUserNotificationCenter
return true;
#else
// TODO(https://openradar.appspot.com/27768556): We must mock this function
// using NotificationHelperMock as a workaround to UNUserNotificationCenter
// throwing an exception during tests

if (@available(macOS 10.14, *)) {
__block bool is_authorized = false;
// #if !defined(OFFICIAL_BUILD)
// LOG(WARNING) << "Unable to detect the status of native notifications on non"
// " official builds as the app is not code signed";
// return true;
// #else
// // TODO(https://openradar.appspot.com/27768556): We must mock this function
// // using NotificationHelperMock as a workaround to UNUserNotificationCenter
// // throwing an exception during tests

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
// if (@available(macOS 10.14, *)) {
// __block bool is_authorized = false;

UNUserNotificationCenter *notificationCenter =
[UNUserNotificationCenter currentNotificationCenter];
// dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

[notificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionAlert
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
LOG(INFO) << "User granted authorization to show notifications";
} else {
LOG(WARNING) << "User denied authorization to show notifications";
}
// UNUserNotificationCenter *notificationCenter =
// [UNUserNotificationCenter currentNotificationCenter];

is_authorized = granted;
// [notificationCenter requestAuthorizationWithOptions:UNAuthorizationOptionAlert
// completionHandler:^(BOOL granted, NSError * _Nullable error) {
// if (granted) {
// LOG(INFO) << "User granted authorization to show notifications";
// } else {
// LOG(WARNING) << "User denied authorization to show notifications";
// }

dispatch_semaphore_signal(semaphore);
}];
// is_authorized = granted;

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_release(semaphore);
// dispatch_semaphore_signal(semaphore);
// }];

if (!is_authorized) {
LOG(WARNING) << "User is not authorized to show notifications";
}
// dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
// dispatch_release(semaphore);

return is_authorized;
}
// if (!is_authorized) {
// LOG(WARNING) << "User is not authorized to show notifications";
// }

return true;
#endif
// return is_authorized;
// }

// return true;
// #endif
}

bool NotificationHelperMac::IsEnabled() const {
#if !defined(OFFICIAL_BUILD)
LOG(WARNING) << "Unable to detect the status of native notifications on non"
" official builds as the app is not code signed";
// TODO(https://github.com/brave/brave-browser/issues/5541): Uncomment below
// code when notification_platform_bridge_mac.mm has been updated to use
// UNUserNotificationCenter
return true;
#else
// TODO(https://openradar.appspot.com/27768556): We must mock this function
// using NotificationHelperMock as a workaround to UNUserNotificationCenter
// throwing an exception during tests

if (@available(macOS 10.14, *)) {
__block bool is_authorized = false;

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

UNUserNotificationCenter *notificationCenter =
[UNUserNotificationCenter currentNotificationCenter];

[notificationCenter getNotificationSettingsWithCompletionHandler:
^(UNNotificationSettings * _Nonnull settings) {
switch (settings.authorizationStatus) {
case UNAuthorizationStatusDenied: {
LOG(WARNING) << "Notification authorization status denied";
is_authorized = false;
break;
}

case UNAuthorizationStatusNotDetermined: {
LOG(INFO) << "Notification authorization status not determined";
is_authorized = true;
break;
}

case UNAuthorizationStatusAuthorized: {
LOG(INFO) << "Notification authorization status authorized";
is_authorized = true;
break;
}

case UNAuthorizationStatusProvisional: {
LOG(INFO) << "Notification authorization status provisional";
is_authorized = true;
break;
}
}

dispatch_semaphore_signal(semaphore);
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_release(semaphore);

if (!is_authorized) {
LOG(WARNING) << "Notifications not authorized";
}

return is_authorized;
}

return true;
#endif
// #if !defined(OFFICIAL_BUILD)
// LOG(WARNING) << "Unable to detect the status of native notifications on non"
// " official builds as the app is not code signed";
// return true;
// #else
// // TODO(https://openradar.appspot.com/27768556): We must mock this function
// // using NotificationHelperMock as a workaround to UNUserNotificationCenter
// // throwing an exception during tests

// if (@available(macOS 10.14, *)) {
// __block bool is_authorized = false;

// dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

// UNUserNotificationCenter *notificationCenter =
// [UNUserNotificationCenter currentNotificationCenter];

// [notificationCenter getNotificationSettingsWithCompletionHandler:
// ^(UNNotificationSettings * _Nonnull settings) {
// switch (settings.authorizationStatus) {
// case UNAuthorizationStatusDenied: {
// LOG(WARNING) << "Notification authorization status denied";
// is_authorized = false;
// break;
// }

// case UNAuthorizationStatusNotDetermined: {
// LOG(INFO) << "Notification authorization status not determined";
// is_authorized = true;
// break;
// }

// case UNAuthorizationStatusAuthorized: {
// LOG(INFO) << "Notification authorization status authorized";
// is_authorized = true;
// break;
// }

// case UNAuthorizationStatusProvisional: {
// LOG(INFO) << "Notification authorization status provisional";
// is_authorized = true;
// break;
// }
// }

// dispatch_semaphore_signal(semaphore);
// }];

// dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
// dispatch_release(semaphore);

// if (!is_authorized) {
// LOG(WARNING) << "Notifications not authorized";
// }

// return is_authorized;
// }

// return true;
// #endif
}

} // namespace brave_ads

0 comments on commit 8dc603c

Please sign in to comment.