Bug 1371879 - Add support for unregistering proxy scripts r?mixedpuppy draft
authorMatthew Wein <mwein@mozilla.com>
Mon, 26 Jun 2017 00:41:33 -0400
changeset 600371 f15228868d876f022298712e57eb1cc8c05dd63d
parent 599782 c03f906e6579fce0b61524c4f5c8445dfa76ffe0
child 634964 c061622da307471c5cfe7987a36749f6b2b5e1d0
push id65731
push usermwein@mozilla.com
push dateMon, 26 Jun 2017 04:55:06 +0000
reviewersmixedpuppy
bugs1371879
milestone56.0a1
Bug 1371879 - Add support for unregistering proxy scripts r?mixedpuppy This patch suggests deprecating proxy.registerProxyScript and replacing it with proxy.register since this API won't support registering anything other than proxy scripts. I normally would be hesitant to suggest making a name change after an API is released, but this API is one that is still in development, and for that reason I think changes like this should be somewhat expected until the API becomes more stable. MozReview-Commit-ID: 9UeuUjQ6OU5
toolkit/components/extensions/ext-proxy.js
toolkit/components/extensions/schemas/proxy.json
toolkit/components/extensions/test/mochitest/test_ext_proxy.html
--- a/toolkit/components/extensions/ext-proxy.js
+++ b/toolkit/components/extensions/ext-proxy.js
@@ -26,27 +26,35 @@ this.proxy = class extends ExtensionAPI 
       proxyScriptContextMap.delete(extension);
     }
   }
 
   getAPI(context) {
     let {extension} = context;
     return {
       proxy: {
-        registerProxyScript: (url) => {
+        register(url) {
+          this.unregister();
+
+          let proxyScriptContext = new ProxyScriptContext(extension, url);
+          if (proxyScriptContext.load()) {
+            proxyScriptContextMap.set(extension, proxyScriptContext);
+          }
+        },
+
+        unregister() {
           // Unload the current proxy script if one is loaded.
           if (proxyScriptContextMap.has(extension)) {
             proxyScriptContextMap.get(extension).unload();
             proxyScriptContextMap.delete(extension);
           }
+        },
 
-          let proxyScriptContext = new ProxyScriptContext(extension, url);
-          if (proxyScriptContext.load()) {
-            proxyScriptContextMap.set(extension, proxyScriptContext);
-          }
+        registerProxyScript(url) {
+          this.register(url);
         },
 
         onProxyError: new SingletonEventManager(context, "proxy.onProxyError", fire => {
           let listener = (name, error) => {
             fire.async(error);
           };
           extension.on("proxy-error", listener);
           return () => {
--- a/toolkit/components/extensions/schemas/proxy.json
+++ b/toolkit/components/extensions/schemas/proxy.json
@@ -14,19 +14,40 @@
     ]
   },
   {
     "namespace": "proxy",
     "description": "Use the browser.proxy API to register proxy scripts in Firefox. Proxy scripts in Firefox are proxy auto-config files with extra contextual information and support for additional return types.",
     "permissions": ["proxy"],
     "functions": [
       {
+        "name": "register",
+        "type": "function",
+        "description": "Registers the proxy script for the extension.",
+        "async": true,
+        "parameters": [
+          {
+            "name": "url",
+            "type": "string",
+            "format": "strictRelativeUrl"
+          }
+        ]
+      },
+      {
+        "name": "unregister",
+        "type": "function",
+        "description": "Unregisters the proxy script for the extension.",
+        "async": true,
+        "parameters": []
+      },
+      {
         "name": "registerProxyScript",
         "type": "function",
         "description": "Registers the proxy script for the extension.",
+        "deprecated": "Please use $(ref:proxy.register)",
         "async": true,
         "parameters": [
           {
             "name": "url",
             "type": "string",
             "format": "strictRelativeUrl"
           }
         ]
--- a/toolkit/components/extensions/test/mochitest/test_ext_proxy.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_proxy.html
@@ -21,38 +21,52 @@ async function testProxyScript(script, e
       let errorReceived = false;
       browser.proxy.onProxyError.addListener(error => {
         if (!errorReceived) {
           errorReceived = true;
           browser.test.sendMessage("proxy-error-received", error);
         }
       });
 
-      browser.proxy.registerProxyScript("proxy_script.js");
+      browser.proxy.register("proxy_script.js").then(() => {
+        browser.test.sendMessage("ready");
+      });
+
+      browser.test.onMessage.addListener(msg => {
+        if (msg === "unregister-proxy-script") {
+          browser.proxy.unregister().then(() => {
+            browser.test.notifyPass("proxy");
+          });
+        }
+      });
     },
     manifest: {
       "permissions": ["proxy"],
     },
     files: {
       "proxy_script.js": String(script).replace(/^.*?\{([^]*)\}$/, "$1"),
     },
   });
 
   await extension.startup();
+  await extension.awaitMessage("ready");
 
   let win = window.open("http://example.com/");
   let error = await extension.awaitMessage("proxy-error-received");
   is(error.message, expected.message, "Correct error message received");
 
   if (expected.errorInfo) {
     ok(error.fileName.includes("proxy_script.js"), "Error should include file name");
     is(error.lineNumber, 3, "Error should include line number");
     ok(error.stack.includes("proxy_script.js:3:9"), "Error should include stack trace");
   }
 
+  extension.sendMessage("unregister-proxy-script");
+  await extension.awaitFinish("proxy");
+
   win.close();
   await extension.unload();
 }
 
 add_task(async function test_invalid_FindProxyForURL_type() {
   await testProxyScript(
     () => { }, {
       message: "The proxy script must define FindProxyForURL as a function",