Title: [208974] trunk
Revision
208974
Author
[email protected]
Date
2016-11-24 12:10:25 -0800 (Thu, 24 Nov 2016)

Log Message

[GTK] Notifications API does not expose or respect the "tag" attribute
https://bugs.webkit.org/show_bug.cgi?id=164771

Reviewed by Gustavo Noronha Silva.

Source/WebKit2:

Expose a tag property on WebKitNotification. Ensure that any previous notification with the
same tag is closed when showing a new notification with that tag.

* UIProcess/API/gtk/WebKitNotification.cpp:
(webkit_notification_class_init):
(webkitNotificationCreate):
(webkit_notification_get_tag):
* UIProcess/API/gtk/WebKitNotification.h:
* UIProcess/API/gtk/WebKitNotificationProvider.cpp:
(WebKitNotificationProvider::withdrawAnyPreviousNotificationMatchingTag):
(WebKitNotificationProvider::show):
* UIProcess/API/gtk/WebKitNotificationProvider.h:
* UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt:

Tools:

Verify that showing a notification with the same tag as another notification closes the
previous notification before the new notification is shown.

* TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitWebView.cpp:
(testWebViewNotification):

Modified Paths

Diff

Modified: trunk/Source/WebKit2/ChangeLog (208973 => 208974)


--- trunk/Source/WebKit2/ChangeLog	2016-11-24 15:08:11 UTC (rev 208973)
+++ trunk/Source/WebKit2/ChangeLog	2016-11-24 20:10:25 UTC (rev 208974)
@@ -1,3 +1,24 @@
+2016-11-24  Michael Catanzaro  <[email protected]>
+
+        [GTK] Notifications API does not expose or respect the "tag" attribute
+        https://bugs.webkit.org/show_bug.cgi?id=164771
+
+        Reviewed by Gustavo Noronha Silva.
+
+        Expose a tag property on WebKitNotification. Ensure that any previous notification with the
+        same tag is closed when showing a new notification with that tag.
+
+        * UIProcess/API/gtk/WebKitNotification.cpp:
+        (webkit_notification_class_init):
+        (webkitNotificationCreate):
+        (webkit_notification_get_tag):
+        * UIProcess/API/gtk/WebKitNotification.h:
+        * UIProcess/API/gtk/WebKitNotificationProvider.cpp:
+        (WebKitNotificationProvider::withdrawAnyPreviousNotificationMatchingTag):
+        (WebKitNotificationProvider::show):
+        * UIProcess/API/gtk/WebKitNotificationProvider.h:
+        * UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt:
+
 2016-11-22  Sergio Villar Senin  <[email protected]>
 
         [GTK] Fix introspection warning

Modified: trunk/Source/WebKit2/UIProcess/API/gtk/WebKitNotification.cpp (208973 => 208974)


--- trunk/Source/WebKit2/UIProcess/API/gtk/WebKitNotification.cpp	2016-11-24 15:08:11 UTC (rev 208973)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/WebKitNotification.cpp	2016-11-24 20:10:25 UTC (rev 208974)
@@ -39,7 +39,8 @@
 
     PROP_ID,
     PROP_TITLE,
-    PROP_BODY
+    PROP_BODY,
+    PROP_TAG
 };
 
 enum {
@@ -52,6 +53,7 @@
 struct _WebKitNotificationPrivate {
     CString title;
     CString body;
+    CString tag;
     guint64 id;
 
     WebKitWebView* webView;
@@ -75,6 +77,9 @@
     case PROP_BODY:
         g_value_set_string(value, webkit_notification_get_body(notification));
         break;
+    case PROP_TAG:
+        g_value_set_string(value, webkit_notification_get_tag(notification));
+        break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
     }
@@ -131,6 +136,21 @@
             WEBKIT_PARAM_READABLE));
 
     /**
+     * WebKitNotification:tag:
+     *
+     * The tag identifier for the notification.
+     *
+     * Since: 2.16
+     */
+    g_object_class_install_property(objectClass,
+        PROP_TAG,
+        g_param_spec_string("tag",
+            _("Tag"),
+            _("The tag identifier for the notification"),
+            nullptr,
+            WEBKIT_PARAM_READABLE));
+
+    /**
      * WebKitNotification::closed:
      * @notification: the #WebKitNotification on which the signal is emitted
      *
@@ -176,6 +196,7 @@
     notification->priv->id = webNotification.notificationID();
     notification->priv->title = webNotification.title().utf8();
     notification->priv->body = webNotification.body().utf8();
+    notification->priv->tag = webNotification.tag().utf8();
     notification->priv->webView = webView;
     return notification;
 }
@@ -237,6 +258,24 @@
 }
 
 /**
+ * webkit_notification_get_tag:
+ * @notification: a #WebKitNotification
+ *
+ * Obtains the tag identifier for the notification.
+ *
+ * Returns: (allow-none): the tag for the notification
+ *
+ * Since: 2.16
+ */
+const gchar* webkit_notification_get_tag(WebKitNotification* notification)
+{
+    g_return_val_if_fail(WEBKIT_IS_NOTIFICATION(notification), nullptr);
+
+    const gchar* tag = notification->priv->tag.data();
+    return strlen(tag) > 0 ? tag : nullptr;
+}
+
+/**
  * webkit_notification_close:
  * @notification: a #WebKitNotification
  *

Modified: trunk/Source/WebKit2/UIProcess/API/gtk/WebKitNotification.h (208973 => 208974)


--- trunk/Source/WebKit2/UIProcess/API/gtk/WebKitNotification.h	2016-11-24 15:08:11 UTC (rev 208973)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/WebKitNotification.h	2016-11-24 20:10:25 UTC (rev 208974)
@@ -70,6 +70,9 @@
 WEBKIT_API const gchar *
 webkit_notification_get_body                 (WebKitNotification *notification);
 
+WEBKIT_API const gchar *
+webkit_notification_get_tag                  (WebKitNotification *notification);
+
 WEBKIT_API void
 webkit_notification_close                    (WebKitNotification *notification);
 

Modified: trunk/Source/WebKit2/UIProcess/API/gtk/WebKitNotificationProvider.cpp (208973 => 208974)


--- trunk/Source/WebKit2/UIProcess/API/gtk/WebKitNotificationProvider.cpp	2016-11-24 15:08:11 UTC (rev 208973)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/WebKitNotificationProvider.cpp	2016-11-24 20:10:25 UTC (rev 208974)
@@ -102,11 +102,30 @@
     provider->m_notificationManager->providerDidClickNotification(webkit_notification_get_id(notification));
 }
 
+void WebKitNotificationProvider::withdrawAnyPreviousNotificationMatchingTag(const String& tag)
+{
+    if (tag.isEmpty())
+        return;
+
+    for (auto& notification : m_notifications.values()) {
+        if (tag == webkit_notification_get_tag(notification.get())) {
+            webkit_notification_close(notification.get());
+            break;
+        }
+    }
+
+#ifndef NDEBUG
+    for (auto& notification : m_notifications.values())
+        ASSERT(tag != webkit_notification_get_tag(notification.get()));
+#endif
+}
+
 void WebKitNotificationProvider::show(WebPageProxy* page, const WebNotification& webNotification)
 {
     GRefPtr<WebKitNotification> notification = m_notifications.get(webNotification.notificationID());
 
     if (!notification) {
+        withdrawAnyPreviousNotificationMatchingTag(webNotification.tag());
         notification = adoptGRef(webkitNotificationCreate(WEBKIT_WEB_VIEW(page->viewWidget()), webNotification));
         g_signal_connect(notification.get(), "closed", G_CALLBACK(notificationCloseCallback), this);
         g_signal_connect(notification.get(), "clicked", G_CALLBACK(notificationClickedCallback), this);

Modified: trunk/Source/WebKit2/UIProcess/API/gtk/WebKitNotificationProvider.h (208973 => 208974)


--- trunk/Source/WebKit2/UIProcess/API/gtk/WebKitNotificationProvider.h	2016-11-24 15:08:11 UTC (rev 208973)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/WebKitNotificationProvider.h	2016-11-24 20:10:25 UTC (rev 208974)
@@ -48,6 +48,8 @@
     static void notificationCloseCallback(WebKitNotification*, WebKitNotificationProvider*);
     static void notificationClickedCallback(WebKitNotification*, WebKitNotificationProvider*);
 
+    void withdrawAnyPreviousNotificationMatchingTag(const String&);
+
     RefPtr<WebNotificationManagerProxy> m_notificationManager;
     HashMap<uint64_t, GRefPtr<WebKitNotification>> m_notifications;
 };

Modified: trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt (208973 => 208974)


--- trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt	2016-11-24 15:08:11 UTC (rev 208973)
+++ trunk/Source/WebKit2/UIProcess/API/gtk/docs/webkit2gtk-4.0-sections.txt	2016-11-24 20:10:25 UTC (rev 208974)
@@ -670,6 +670,7 @@
 webkit_notification_get_id
 webkit_notification_get_title
 webkit_notification_get_body
+webkit_notification_get_tag
 webkit_notification_close
 webkit_notification_clicked
 

Modified: trunk/Tools/ChangeLog (208973 => 208974)


--- trunk/Tools/ChangeLog	2016-11-24 15:08:11 UTC (rev 208973)
+++ trunk/Tools/ChangeLog	2016-11-24 20:10:25 UTC (rev 208974)
@@ -1,3 +1,16 @@
+2016-11-24  Michael Catanzaro  <[email protected]>
+
+        [GTK] Notifications API does not expose or respect the "tag" attribute
+        https://bugs.webkit.org/show_bug.cgi?id=164771
+
+        Reviewed by Gustavo Noronha Silva.
+
+        Verify that showing a notification with the same tag as another notification closes the
+        previous notification before the new notification is shown.
+
+        * TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitWebView.cpp:
+        (testWebViewNotification):
+
 2016-11-24  Carlos Garcia Campos  <[email protected]>
 
         Unreviewed. Fix GTK+ test /webkit2/WebKitWebContext/get-plugins after r208429.

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitWebView.cpp (208973 => 208974)


--- trunk/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitWebView.cpp	2016-11-24 15:08:11 UTC (rev 208973)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKit2Gtk/TestWebKitWebView.cpp	2016-11-24 20:10:25 UTC (rev 208974)
@@ -677,6 +677,7 @@
 
     static gboolean showNotificationCallback(WebKitWebView*, WebKitNotification* notification, NotificationWebViewTest* test)
     {
+        g_assert(!test->m_notification);
         test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(notification));
         test->m_notification = notification;
         g_signal_connect(notification, "closed", G_CALLBACK(notificationClosedCallback), test);
@@ -732,6 +733,16 @@
         g_main_loop_run(m_mainLoop);
     }
 
+    void requestNotificationAndWaitUntilShown(const char* title, const char* body, const char* tag)
+    {
+        m_event = None;
+
+        GUniquePtr<char> jscode(g_strdup_printf("n = new Notification('%s', { body: '%s', tag: '%s'});", title, body, tag));
+        webkit_web_view_run_javascript(m_webView, jscode.get(), nullptr, nullptr, nullptr);
+
+        g_main_loop_run(m_mainLoop);
+    }
+
     void clickNotificationAndWaitUntilClicked()
     {
         m_event = None;
@@ -773,12 +784,14 @@
 
     static const char* title = "This is a notification";
     static const char* body = "This is the body.";
-    test->requestNotificationAndWaitUntilShown(title, body);
+    static const char* tag = "This is the tag.";
+    test->requestNotificationAndWaitUntilShown(title, body, tag);
 
     g_assert(test->m_event == NotificationWebViewTest::Shown);
     g_assert(test->m_notification);
     g_assert_cmpstr(webkit_notification_get_title(test->m_notification), ==, title);
     g_assert_cmpstr(webkit_notification_get_body(test->m_notification), ==, body);
+    g_assert_cmpstr(webkit_notification_get_tag(test->m_notification), ==, tag);
 
     test->clickNotificationAndWaitUntilClicked();
     g_assert(test->m_event == NotificationWebViewTest::OnClicked);
@@ -788,13 +801,18 @@
 
     test->requestNotificationAndWaitUntilShown(title, body);
     g_assert(test->m_event == NotificationWebViewTest::Shown);
+    g_assert_cmpstr(webkit_notification_get_tag(test->m_notification), ==, nullptr);
 
     test->closeNotificationAndWaitUntilOnClosed();
     g_assert(test->m_event == NotificationWebViewTest::OnClosed);
 
-    test->requestNotificationAndWaitUntilShown(title, body);
+    // The first notification should be closed automatically because the tag is
+    // the same. It will crash in showNotificationCallback on failure.
+    test->requestNotificationAndWaitUntilShown(title, body, tag);
+    test->requestNotificationAndWaitUntilShown(title, body, tag);
     g_assert(test->m_event == NotificationWebViewTest::Shown);
 
+    // Notification should be closed when navigating to a different webpage.
     test->loadURI(gServer->getURIForPath("/").data());
     test->waitUntilLoadFinished();
     g_assert(test->m_event == NotificationWebViewTest::Closed);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to