Bug 1497426 - Add source.label support for native Toast Notification. r=aklotz
☠☠ backed out by 6231a4da0ebe ☠ ☠
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Tue, 09 Oct 2018 16:26:42 +0900
changeset 444542 91d5d2c4b1b9a11f09a1ca26f946956eb7eee29e
parent 444541 2c369f37574ac8be128b853f9f4f466dbfe6cd76
child 444543 6231a4da0ebe72edd019cac34d57b5c386070cba
push id34997
push userrgurzau@mozilla.com
push dateTue, 06 Nov 2018 09:59:47 +0000
treeherdermozilla-central@957a743c4ca2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaklotz
bugs1497426
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1497426 - Add source.label support for native Toast Notification. r=aklotz XUL version of notification and macOS's notification center show source label ("via <URL>" to show origin URL). Windows's toast notification should show it like XUL version. Windows 10 anniversary update or later has placement=attribution attribute for this usages. https://blogs.msdn.microsoft.com/tiles_and_toasts/2016/05/23/notificationsextensions-updated-for-anniversary-update-of-windows-10/#toastattribution is more detail about attribution text, so we should use it. As example, screenshots of this change are attached in this bug. Differential Revision: https://phabricator.services.mozilla.com/D8196
widget/windows/ToastNotificationHandler.cpp
--- a/widget/windows/ToastNotificationHandler.cpp
+++ b/widget/windows/ToastNotificationHandler.cpp
@@ -3,16 +3,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * 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 "ToastNotificationHandler.h"
 
 #include "imgIRequest.h"
 #include "mozilla/gfx/2D.h"
+#include "mozilla/WindowsVersion.h"
 #include "nsDirectoryServiceDefs.h"
 #include "nsIStringBundle.h"
 #include "nsIURI.h"
 #include "nsIUUIDGenerator.h"
 #include "nsNetUtil.h"
 #include "nsProxyRelease.h"
 #include "WinTaskbar.h"
 #include "WinUtils.h"
@@ -176,22 +177,28 @@ ToastNotificationHandler::InitAlertAsync
 
 bool
 ToastNotificationHandler::ShowAlert()
 {
   if (!mBackend->IsActiveHandler(mName, this)) {
     return true;
   }
 
-  ComPtr<IXmlDocument> toastXml =
-    InitializeXmlForTemplate(
-      !mHasImage ?
-        ToastTemplateType::ToastTemplateType_ToastText03 :
-        ToastTemplateType::ToastTemplateType_ToastImageAndText03);
+  ToastTemplateType toastTemplate;
+  if (mHostPort.IsEmpty()) {
+    toastTemplate = mHasImage ?
+      ToastTemplateType::ToastTemplateType_ToastImageAndText03 :
+      ToastTemplateType::ToastTemplateType_ToastText03;
+  } else {
+    toastTemplate = !mHasImage ?
+      ToastTemplateType::ToastTemplateType_ToastImageAndText04 :
+      ToastTemplateType::ToastTemplateType_ToastText04;
+  }
 
+  ComPtr<IXmlDocument> toastXml = InitializeXmlForTemplate(toastTemplate);
   if (!toastXml) {
     return false;
   }
 
   HRESULT hr;
 
   if (mHasImage) {
     ComPtr<IXmlNodeList> toastImageElements;
@@ -278,18 +285,46 @@ ToastNotificationHandler::ShowAlert()
   nsCOMPtr<nsIStringBundle> bundle;
   sbs->CreateBundle("chrome://alerts/locale/alert.properties",
                     getter_AddRefs(bundle));
   if (NS_WARN_IF(!bundle)) {
     return false;
   }
 
   if (!mHostPort.IsEmpty()) {
+    const char16_t* formatStrings[] = { mHostPort.get() };
+
+    ComPtr<IXmlNode> urlTextNodeRoot;
+    hr = toastTextElements->Item(2, &urlTextNodeRoot);
+    if (NS_WARN_IF(FAILED(hr))) {
+      return false;
+    }
+
+    nsAutoString urlReference;
+    bundle->FormatStringFromName("source.label",
+                                 formatStrings,
+                                 ArrayLength(formatStrings),
+                                 urlReference);
+
+    if (NS_WARN_IF(!SetNodeValueString(urlReference, urlTextNodeRoot.Get(),
+                                       toastXml.Get()))) {
+      return false;
+    }
+
+    if (IsWin10AnniversaryUpdateOrLater()) {
+      ComPtr<IXmlElement> placementText;
+      hr = urlTextNodeRoot.As(&placementText);
+      if (SUCCEEDED(hr)) {
+        // placement is supported on Windows 10 Anniversary Update or later
+        SetAttribute(placementText.Get(), HStringReference(L"placement").Get(),
+                     NS_LITERAL_STRING("attribution"));
+      }
+    }
+
     nsAutoString disableButtonTitle;
-    const char16_t* formatStrings[] = { mHostPort.get() };
     bundle->FormatStringFromName("webActions.disableForOrigin.label",
                                  formatStrings,
                                  ArrayLength(formatStrings),
                                  disableButtonTitle);
 
     AddActionNode(toastXml.Get(), actionsNode.Get(), disableButtonTitle,
                   NS_LITERAL_STRING("snooze"));
   }