Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Aug 2, 2019
1 parent 827e4c6 commit 30ddb4f
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 5 deletions.
3 changes: 2 additions & 1 deletion components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "brave/components/brave_rewards/browser/rewards_notification_service.h"
#include "brave/components/services/bat_ads/public/cpp/ads_client_mojo_bridge.h"
#include "brave/components/services/bat_ads/public/interfaces/bat_ads.mojom.h"
#include "brave/components/brave_ads/browser/notification_helper.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/notifications/notification_display_service_impl.h"
Expand Down Expand Up @@ -1220,7 +1221,7 @@ bool AdsServiceImpl::IsNotificationsAvailable() const {
#if BUILDFLAG(ENABLE_NATIVE_NOTIFICATIONS)
return NotificationHelper::GetInstance()->IsNotificationsAvailable();
#else
return false;
return true;
#endif
}

Expand Down
1 change: 1 addition & 0 deletions components/brave_ads/browser/notification_helper_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ NotificationHelperLinux::~NotificationHelperLinux() = default;
bool NotificationHelperLinux::IsNotificationsAvailable() const {
// Unable to find a way of detecting if notifications are enabled within the
// operating system so always return true

return true;
}

Expand Down
31 changes: 31 additions & 0 deletions components/brave_ads/browser/notification_helper_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,37 @@ NotificationHelperMac::NotificationHelperMac() = default;
NotificationHelperMac::~NotificationHelperMac() = default;

bool NotificationHelperMac::IsNotificationsAvailable() const {
// macOS 10.13 and older do not support native notifications. Chromium will
// fall back to the Chromium Message Center if native notifications are not
// supported

// Until we can build the browser with |mac_deployment_target| set to
// |10.14| or above in |src/build/config/mac/mac_sdk.gni| it is not possible
// detect the status of notification using the following code:

// UNUserNotificationCenter *notificationCenter =
// [UNUserNotificationCenter currentNotificationCenter];
// [notificationCenter getNotificationSettingsWithCompletionHandler:
// ^(UNNotificationSettings * _Nonnull settings) {
// switch (settings.authorizationStatus) {
// case UNAuthorizationStatusNotDetermined: {
// return false;
// }

// case UNAuthorizationStatusDenied: {
// return false;
// }

// case UNAuthorizationStatusAuthorized: {
// return true;
// }

// case UNAuthorizationStatusProvisional: {
// return true;
// }
// }
// }];

return true;
}

Expand Down
79 changes: 78 additions & 1 deletion components/brave_ads/browser/notification_helper_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,93 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <windows.ui.notifications.h>
#include <wrl/event.h>

#include "brave/components/brave_ads/browser/notification_helper_win.h"

#include "base/win/windows_version.h"
#include "base/win/core_winrt_util.h"
#include "base/win/scoped_hstring.h"
#include "base/win/scoped_winrt_initializer.h"
#include "base/feature_list.h"

namespace mswr = Microsoft::WRL;
namespace winui = ABI::Windows::UI;

namespace brave_ads {

NotificationHelperWin::NotificationHelperWin() = default;

NotificationHelperWin::~NotificationHelperWin() = default;

bool NotificationHelperWin::IsNotificationsAvailable() const {
return true;
// There was a Microsoft bug in Windows 10 prior to build 17134 (i.e.
// VERSION_WIN10_RS4) causing endless loops in displaying notifications. It
// significantly amplified the memory and CPU usage. Therefore, Windows 10
// native notifications are only enabled for build 17134 and later

if (base::win::GetVersion() < base::win::VERSION_WIN10_RS4) {
return true;
}

if (base::FeatureList::IsEnabled(features::kNativeNotifications)) {
mswr::ComPtr<ABI::Windows::UI::Notifications::IToastNotifier> notifier_;

if (!notifier_.Get() && FAILED(InitializeToastNotifier())) {
return false;
}

winui::Notifications::IToastNotifier* notifier = notifier_.Get();

winui::Notifications::NotificationSetting setting;
HRESULT hr = notifier->get_Setting(&setting);
if (!SUCCEEDED(hr)) {
return false;
}

switch (setting) {
case winui::Notifications::NotificationSetting_Enabled: {
return true;
}
case winui::Notifications::NotificationSetting_DisabledForApplication: {
return false;
}
case winui::Notifications::NotificationSetting_DisabledForUser: {
return false;
}
case winui::Notifications::NotificationSetting_DisabledByGroupPolicy: {
return false;
}
case winui::Notifications::NotificationSetting_DisabledByManifest: {
return false;
}
}

return false;
}

return false;
}

HRESULT NotificationHelperWin::InitializeToastNotifier() {
mswr::ComPtr<winui::Notifications::IToastNotificationManagerStatics>
toast_manager;
HRESULT hr = CreateActivationFactory(
RuntimeClass_Windows_UI_Notifications_ToastNotificationManager,
toast_manager.GetAddressOf());
if (FAILED(hr)) {
return hr;
}

ScopedHString application_id = ScopedHString::Create(GetAppId());
hr = toast_manager->CreateToastNotifierWithId(application_id.get(),
&notifier_);
if (FAILED(hr)) {
return hr;
}

return hr;
}

bool NotificationHelperWin::ShowMyFirstAdNotification() const {
Expand Down
2 changes: 1 addition & 1 deletion vendor/bat-native-ads/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void LoadUserModelForLocale(
bool IsForeground() const
```

`IsNotificationsAvailable` should return `true` if the operating system supports notifications otherwise returns `false`
`IsNotificationsAvailable` should return `true` if notifications are enabled otherwise returns `false`
```
bool IsNotificationsAvailable() const
```
Expand Down
3 changes: 1 addition & 2 deletions vendor/bat-native-ads/include/bat/ads/ads_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ class ADS_EXPORT AdsClient {
// false
virtual bool IsForeground() const = 0;

// Should return true if the operating system supports notifications otherwise
// returns false
// Should return true if notifications are enabled otherwise returns false
virtual bool IsNotificationsAvailable() const = 0;

// Should show a notification
Expand Down

0 comments on commit 30ddb4f

Please sign in to comment.