PolyGerrit: Make plugins CANONICAL_PATH aware

Bug: Issue 6894
Change-Id: I71e221ca9442f36fb635ca210d1aa66afb083d75
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.html b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.html
index d6b2d11..f73f731 100644
--- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.html
+++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.html
@@ -14,6 +14,7 @@
 limitations under the License.
 -->
 <link rel="import" href="../../../bower_components/polymer/polymer.html">
+<link rel="import" href="../../../behaviors/base-url-behavior/base-url-behavior.html">
 <link rel="import" href="../../../behaviors/gr-patch-set-behavior/gr-patch-set-behavior.html">
 <link rel="import" href="../../core/gr-reporting/gr-reporting.html">
 <link rel="import" href="../../plugins/gr-dom-hooks/gr-dom-hooks.html">
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html
index 2a2fdef..1236ca4 100644
--- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html
+++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html
@@ -317,5 +317,22 @@
         assert.isTrue(console.warn.calledOnce);
       }
     });
+
+    suite('test plugin with base url', () => {
+      setup(() => {
+        sandbox.stub(Gerrit.BaseUrlBehavior, 'getBaseUrl').returns('/r');
+
+        Gerrit._setPluginsCount(1);
+        Gerrit.install(p => { plugin = p; }, '0.1',
+            'http://test.com/r/plugins/testplugin/static/test.js');
+      });
+
+      test('url', () => {
+        assert.notEqual(plugin.url(), 'http://test.com/plugins/testplugin/');
+        assert.equal(plugin.url(), 'http://test.com/r/plugins/testplugin/');
+        assert.equal(plugin.url('/static/test.js'),
+            'http://test.com/r/plugins/testplugin/static/test.js');
+      });
+    });
   });
 </script>
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
index 3b2d5c4..97b67ae 100644
--- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
+++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
@@ -64,13 +64,15 @@
       return;
     }
 
+    const base = Gerrit.BaseUrlBehavior.getBaseUrl();
+
     this._url = new URL(opt_url);
-    if (!this._url.pathname.startsWith('/plugins')) {
+    if (!this._url.pathname.startsWith(base + '/plugins')) {
       console.warn('Plugin not being loaded from /plugins base path:',
           this._url.href, '— Unable to determine name.');
       return;
     }
-    this._name = this._url.pathname.split('/')[2];
+    this._name = this._url.pathname.replace(base, '').split('/')[2];
   }
 
   Plugin._sharedAPIElement = document.createElement('gr-js-api-interface');
@@ -103,7 +105,9 @@
   };
 
   Plugin.prototype.url = function(opt_path) {
-    return this._url.origin + '/plugins/' + this._name + (opt_path || '/');
+    const base = Gerrit.BaseUrlBehavior.getBaseUrl();
+    return this._url.origin + base + '/plugins/' +
+        this._name + (opt_path || '/');
   };
 
   Plugin.prototype._send = function(method, url, opt_callback, opt_payload) {