[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor-browser] 73/311: Bug 1750623 - Always set isTopLevel=true for ENV_EXTENSION senders. r=robwu, jonalmeida a=RyanVM



This is an automated email from the git hooks/post-receive script.

pierov pushed a commit to branch geckoview-99.0.1-11.0-1
in repository tor-browser.

commit 5af5e5e3811a98ff4daa4867d11dc99a6ad688ad
Author: Agi Sferro <agi@xxxxxxxxxx>
AuthorDate: Tue Feb 8 17:27:49 2022 +0000

    Bug 1750623 - Always set isTopLevel=true for ENV_EXTENSION senders. r=robwu,jonalmeida a=RyanVM
    
    isTopLevel is used for ENV_TYPE_CONTENT_SCRIPT to let embedders know if an
    iframe is sending messages to the app. For extension environments we don't need
    this extra check so we can always set the value to true.
    
    Differential Revision: https://phabricator.services.mozilla.com/D137776
---
 .../actions/test-popup-messaging.html              |  9 +++
 .../web_extensions/actions/test-popup-messaging.js | 24 ++++++
 .../mozilla/geckoview/test/ExtensionActionTest.kt  | 85 ++++++++++++++++++++++
 .../mozilla/geckoview/WebExtensionController.java  |  4 +-
 4 files changed, 120 insertions(+), 2 deletions(-)

diff --git a/mobile/android/geckoview/src/androidTest/assets/web_extensions/actions/test-popup-messaging.html b/mobile/android/geckoview/src/androidTest/assets/web_extensions/actions/test-popup-messaging.html
new file mode 100644
index 0000000000000..e83d7d8b7fabb
--- /dev/null
+++ b/mobile/android/geckoview/src/androidTest/assets/web_extensions/actions/test-popup-messaging.html
@@ -0,0 +1,9 @@
+<html>
+  <head>
+    <meta charset="utf-8">
+    <script src="test-popup-messaging.js"></script>
+  </head>
+  <body>
+    <h1> HELLO </h1>
+  </body>
+</html>
diff --git a/mobile/android/geckoview/src/androidTest/assets/web_extensions/actions/test-popup-messaging.js b/mobile/android/geckoview/src/androidTest/assets/web_extensions/actions/test-popup-messaging.js
new file mode 100644
index 0000000000000..479f957564e7e
--- /dev/null
+++ b/mobile/android/geckoview/src/androidTest/assets/web_extensions/actions/test-popup-messaging.js
@@ -0,0 +1,24 @@
+browser.runtime.sendNativeMessage("badNativeApi", "errorerrorerror");
+
+async function runTest() {
+  const response = await browser.runtime.sendNativeMessage(
+    "browser",
+    "testPopupMessage"
+  );
+
+  browser.runtime.sendNativeMessage("browser", `response: ${response}`);
+
+  const port = browser.runtime.connectNative("browser");
+  port.onMessage.addListener(response => {
+    if (response.action === "disconnect") {
+      port.disconnect();
+      return;
+    }
+
+    port.postMessage(`response: ${response.message}`);
+  });
+
+  port.postMessage("testPopupPortMessage");
+}
+
+runTest();
diff --git a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/ExtensionActionTest.kt b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/ExtensionActionTest.kt
index 47a1017f0a939..51e31ad350e97 100644
--- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/ExtensionActionTest.kt
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/ExtensionActionTest.kt
@@ -605,6 +605,91 @@ class ExtensionActionTest : BaseSessionTest() {
         sessionRule.waitForResult(onClicked)
     }
 
+    @Test
+    fun testPopupMessaging() {
+        val popupSession = sessionRule.createOpenSession()
+
+        val actionResult = GeckoResult<WebExtension.Action>()
+        testActionApi("""{
+           "action": "setPopup",
+           "popup": "test-popup-messaging.html"
+        }""") { action ->
+            assertEquals(action.title, "Test action default")
+            assertEquals(action.enabled, true)
+            actionResult.complete(action)
+        }
+
+        val messages = mutableListOf<String>()
+        val messageResult = GeckoResult<List<String>>()
+        val portResult = GeckoResult<WebExtension.Port>()
+        val messageDelegate = object : WebExtension.MessageDelegate {
+            override fun onMessage(
+                nativeApp: String,
+                message: Any,
+                sender: WebExtension.MessageSender
+            ): GeckoResult<Any>? {
+                assertEquals(extension!!.id, sender.webExtension.id)
+                assertEquals(WebExtension.MessageSender.ENV_TYPE_EXTENSION,
+                    sender.environmentType)
+                assertEquals(sender.isTopLevel, true)
+                assertEquals("${extension!!.metaData.baseUrl}test-popup-messaging.html",
+                    sender.url)
+                assertEquals(sender.session, popupSession)
+                messages.add(message as String)
+                if (messages.size == 2) {
+                    messageResult.complete(messages)
+                    return null
+                } else {
+                    return GeckoResult.fromValue("TEST_RESPONSE")
+                }
+            }
+
+            override fun onConnect(port: WebExtension.Port) {
+                assertEquals(extension!!.id, port.sender.webExtension.id)
+                assertEquals(WebExtension.MessageSender.ENV_TYPE_EXTENSION,
+                    port.sender.environmentType)
+                assertEquals(true, port.sender.isTopLevel)
+                assertEquals("${extension!!.metaData.baseUrl}test-popup-messaging.html",
+                    port.sender.url)
+                assertEquals(port.sender.session, popupSession)
+                portResult.complete(port)
+            }
+        }
+
+        popupSession.webExtensionController.setMessageDelegate(
+            extension!!, messageDelegate, "browser")
+
+        val action = sessionRule.waitForResult(actionResult)
+        extension!!.setActionDelegate(object : WebExtension.ActionDelegate {
+            override fun onTogglePopup(extension: WebExtension,
+                                       popupAction: WebExtension.Action): GeckoResult<GeckoSession>? {
+                assertEquals(extension, this@ExtensionActionTest.extension)
+                assertEquals(popupAction, action)
+                return GeckoResult.fromValue(popupSession)
+            }
+        })
+
+        action.click()
+
+        val message = sessionRule.waitForResult(messageResult)
+        assertThat("Message should match", message, equalTo(listOf(
+            "testPopupMessage", "response: TEST_RESPONSE")))
+
+        val port = sessionRule.waitForResult(portResult)
+        val portMessageResult = GeckoResult<String>()
+
+        port.setDelegate(object : WebExtension.PortDelegate {
+            override fun onPortMessage(message: Any, p: WebExtension.Port) {
+                assertEquals(port, p)
+                portMessageResult.complete(message as String)
+            }
+        })
+
+        val portMessage = sessionRule.waitForResult(portMessageResult)
+        assertThat("Message should match", portMessage,
+            equalTo("testPopupPortMessage"))
+    }
+
     @Test
     fun testPopupsCanCloseThemselves() {
         val onCloseRequestResult = GeckoResult<Void>()
diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/WebExtensionController.java b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/WebExtensionController.java
index 26b9a6b33e28b..7c691b6d54559 100644
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/WebExtensionController.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/WebExtensionController.java
@@ -1091,8 +1091,8 @@ public class WebExtensionController {
 
     final String url = sender.getString("url");
     final boolean isTopLevel;
-    if (session == null) {
-      // This message is coming from the background page
+    if (session == null || environmentType == WebExtension.MessageSender.ENV_TYPE_EXTENSION) {
+      // This message is coming from the background page, a popup, or an extension page
       isTopLevel = true;
     } else {
       // If session is present we are either receiving this message from a content script or

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits