Skip to content

Remove macOS changes for "Fix user earns while native Windows and macOS notifications are suppressed" #3907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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