Bug 1288821 - Convert callObserver/removeObserver to native method; r=snorp

Combine the callObserver and removeObserver methods used for notifying
alert listeners into the native method
GeckoAppShell.notifyAlertListener. Keep track of the listener and the
alert cookie on the native side so that we don't need
GeckoAppShell.ALERT_COOKIES anymore.
This commit is contained in:
Jim Chen
2016-08-04 09:17:54 -04:00
parent 923069e07d
commit d7e3d4d04f
4 changed files with 70 additions and 16 deletions

View File

@@ -6,13 +6,14 @@
#include "AndroidAlerts.h"
#include "GeneratedJNIWrappers.h"
#include "nsAlertsUtils.h"
#include "nsAppShell.h"
namespace mozilla {
namespace widget {
NS_IMPL_ISUPPORTS(AndroidAlerts, nsIAlertsService)
StaticAutoPtr<AndroidAlerts::AlertInfoMap> AndroidAlerts::sAlertInfoMap;
NS_IMETHODIMP
AndroidAlerts::ShowAlertNotification(const nsAString & aImageUrl,
const nsAString & aAlertTitle,
@@ -76,8 +77,11 @@ AndroidAlerts::ShowPersistentNotification(const nsAString& aPersistentData,
nsAlertsUtils::GetSourceHostPort(principal, host);
if (aPersistentData.IsEmpty() && aAlertListener) {
// This will remove any observers already registered for this id
nsAppShell::PostEvent(AndroidGeckoEvent::MakeAddObserver(name, aAlertListener));
if (!sAlertInfoMap) {
sAlertInfoMap = new AlertInfoMap();
}
// This will remove any observers already registered for this name.
sAlertInfoMap->Put(name, new AlertInfo{aAlertListener, cookie});
}
java::GeckoAppShell::ShowAlertNotification(
@@ -91,9 +95,33 @@ NS_IMETHODIMP
AndroidAlerts::CloseAlert(const nsAString& aAlertName,
nsIPrincipal* aPrincipal)
{
// We delete the entry in sAlertInfoMap later, when CloseNotification calls
// NotifyListener.
java::GeckoAppShell::CloseNotification(aAlertName);
return NS_OK;
}
void
AndroidAlerts::NotifyListener(const nsAString& aName, const char* aTopic)
{
if (!sAlertInfoMap) {
return;
}
const auto pAlertInfo = sAlertInfoMap->Get(aName);
if (!pAlertInfo) {
return;
}
if (pAlertInfo->listener) {
pAlertInfo->listener->Observe(
nullptr, aTopic, pAlertInfo->cookie.get());
}
if (NS_LITERAL_CSTRING("alertfinished").Equals(aTopic)) {
sAlertInfoMap->Remove(aName);
}
}
} // namespace widget
} // namespace mozilla