Bug 973489 - Part 2. Fix memory leak. r=jimm

This commit is contained in:
Makoto Kato 2014-02-20 11:29:16 +09:00
Родитель ea965699b4
Коммит 640fed18f9
3 изменённых файлов: 42 добавлений и 30 удалений

Просмотреть файл

@ -18,7 +18,7 @@ using namespace ABI::Windows::UI::Notifications;
typedef __FITypedEventHandler_2_Windows__CUI__CNotifications__CToastNotification_IInspectable_t ToastActivationHandler;
typedef __FITypedEventHandler_2_Windows__CUI__CNotifications__CToastNotification_Windows__CUI__CNotifications__CToastDismissedEventArgs ToastDismissHandler;
void
bool
ToastNotificationHandler::DisplayNotification(HSTRING title,
HSTRING msg,
HSTRING imagePath,
@ -39,22 +39,22 @@ ToastNotificationHandler::DisplayNotification(HSTRING title,
toastXml->GetElementsByTagName(textNodeStr, &toastTextElements);
toastXml->GetElementsByTagName(imageNodeStr, &toastImageElements);
AssertHRESULT(toastTextElements->Item(0, &titleTextNodeRoot));
AssertHRESULT(toastTextElements->Item(1, &msgTextNodeRoot));
AssertHRESULT(toastImageElements->Item(0, &imageNodeRoot));
AssertRetHRESULT(toastTextElements->Item(0, &titleTextNodeRoot), false);
AssertRetHRESULT(toastTextElements->Item(1, &msgTextNodeRoot), false);
AssertRetHRESULT(toastImageElements->Item(0, &imageNodeRoot), false);
ComPtr<IXmlNamedNodeMap> attributes;
AssertHRESULT(imageNodeRoot->get_Attributes(&attributes));
AssertHRESULT(attributes->GetNamedItem(srcNodeStr, &srcAttribute));
AssertRetHRESULT(imageNodeRoot->get_Attributes(&attributes), false);
AssertRetHRESULT(attributes->GetNamedItem(srcNodeStr, &srcAttribute), false);
SetNodeValueString(title, titleTextNodeRoot.Get(), toastXml.Get());
SetNodeValueString(msg, msgTextNodeRoot.Get(), toastXml.Get());
SetNodeValueString(imagePath, srcAttribute.Get(), toastXml.Get());
CreateWindowsNotificationFromXml(toastXml.Get(), aAppId);
return CreateWindowsNotificationFromXml(toastXml.Get(), aAppId);
}
void
bool
ToastNotificationHandler::DisplayTextNotification(HSTRING title,
HSTRING msg,
const nsAString& aCookie,
@ -71,13 +71,13 @@ ToastNotificationHandler::DisplayTextNotification(HSTRING title,
WindowsCreateStringReference(L"text", 4, &textHeader, &textNodeStr);
toastXml->GetElementsByTagName(textNodeStr, &toastTextElements);
AssertHRESULT(toastTextElements->Item(0, &titleTextNodeRoot));
AssertHRESULT(toastTextElements->Item(1, &msgTextNodeRoot));
AssertRetHRESULT(toastTextElements->Item(0, &titleTextNodeRoot), false);
AssertRetHRESULT(toastTextElements->Item(1, &msgTextNodeRoot), false);
SetNodeValueString(title, titleTextNodeRoot.Get(), toastXml.Get());
SetNodeValueString(msg, msgTextNodeRoot.Get(), toastXml.Get());
CreateWindowsNotificationFromXml(toastXml.Get(), aAppId);
return CreateWindowsNotificationFromXml(toastXml.Get(), aAppId);
}
ComPtr<IXmlDocument>
@ -92,35 +92,38 @@ ToastNotificationHandler::InitializeXmlForTemplate(ToastTemplateType templateTyp
return toastXml;
}
void
bool
ToastNotificationHandler::CreateWindowsNotificationFromXml(IXmlDocument *toastXml,
const nsAString& aAppId)
{
ComPtr<IToastNotification> notification;
ComPtr<IToastNotificationFactory> factory;
AssertHRESULT(GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_Notifications_ToastNotification).Get(),
factory.GetAddressOf()));
AssertHRESULT(factory->CreateToastNotification(toastXml, &notification));
AssertRetHRESULT(GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_Notifications_ToastNotification).Get(),
factory.GetAddressOf()), false);
AssertRetHRESULT(factory->CreateToastNotification(toastXml, &notification),
false);
EventRegistrationToken activatedToken;
AssertHRESULT(notification->add_Activated(Callback<ToastActivationHandler>(this,
&ToastNotificationHandler::OnActivate).Get(), &activatedToken));
AssertRetHRESULT(notification->add_Activated(Callback<ToastActivationHandler>(this,
&ToastNotificationHandler::OnActivate).Get(), &activatedToken), false);
EventRegistrationToken dismissedToken;
AssertHRESULT(notification->add_Dismissed(Callback<ToastDismissHandler>(this,
&ToastNotificationHandler::OnDismiss).Get(), &dismissedToken));
AssertRetHRESULT(notification->add_Dismissed(Callback<ToastDismissHandler>(this,
&ToastNotificationHandler::OnDismiss).Get(), &dismissedToken), false);
ComPtr<IToastNotifier> notifier;
if (aAppId.IsEmpty()) {
AssertHRESULT(mToastNotificationManagerStatics->CreateToastNotifier(
&notifier));
AssertRetHRESULT(mToastNotificationManagerStatics->CreateToastNotifier(
&notifier), false);
} else {
AssertHRESULT(mToastNotificationManagerStatics->CreateToastNotifierWithId(
AssertRetHRESULT(mToastNotificationManagerStatics->CreateToastNotifierWithId(
HStringReference(PromiseFlatString(aAppId).get()).Get(),
&notifier));
&notifier), false);
}
notifier->Show(notification.Get());
AssertRetHRESULT(notifier->Show(notification.Get()), false);
MetroUtils::FireObserver("metro_native_toast_shown", mCookie.get());
return true;
}
void ToastNotificationHandler::SetNodeValueString(HSTRING inputString, ComPtr<IXmlNode> node, ComPtr<IXmlDocument> xml) {
@ -142,5 +145,6 @@ ToastNotificationHandler::OnDismiss(IToastNotification *notification,
IToastDismissedEventArgs* aArgs)
{
MetroUtils::FireObserver("metro_native_toast_dismissed", mCookie.get());
delete this;
return S_OK;
}

Просмотреть файл

@ -25,9 +25,9 @@ class ToastNotificationHandler {
ToastNotificationHandler() {};
~ToastNotificationHandler() {};
void DisplayNotification(HSTRING title, HSTRING msg, HSTRING imagePath,
bool DisplayNotification(HSTRING title, HSTRING msg, HSTRING imagePath,
const nsAString& aCookie, const nsAString& aAppId);
void DisplayTextNotification(HSTRING title, HSTRING msg,
bool DisplayTextNotification(HSTRING title, HSTRING msg,
const nsAString& aCookie,
const nsAString& aAppId);
HRESULT OnActivate(IToastNotification *notification, IInspectable *inspectable);
@ -38,7 +38,7 @@ class ToastNotificationHandler {
nsString mCookie;
ComPtr<IToastNotificationManagerStatics> mToastNotificationManagerStatics;
void CreateWindowsNotificationFromXml(IXmlDocument *toastXml,
bool CreateWindowsNotificationFromXml(IXmlDocument *toastXml,
const nsAString& aAppId);
ComPtr<IXmlDocument> InitializeXmlForTemplate(ToastTemplateType templateType);
};

Просмотреть файл

@ -208,12 +208,20 @@ nsWinMetroUtils::ShowNativeToast(const nsAString &aTitle,
HSTRING title = HStringReference(aTitle.BeginReading()).Get();
HSTRING msg = HStringReference(aMessage.BeginReading()).Get();
bool ret;
if (anImage.Length() > 0) {
HSTRING imagePath = HStringReference(anImage.BeginReading()).Get();
notification_handler->DisplayNotification(title, msg, imagePath, aCookie,
aAppId);
ret = notification_handler->DisplayNotification(title, msg, imagePath,
aCookie,
aAppId);
} else {
notification_handler->DisplayTextNotification(title, msg, aCookie, aAppId);
ret = notification_handler->DisplayTextNotification(title, msg, aCookie,
aAppId);
}
if (!ret) {
delete notification_handler;
return NS_ERROR_FAILURE;
}
return NS_OK;