Bug 1393699: Add referrerURI to Tabs.loadUrl flows. r=liuche draft
authorMichael Comella <michael.l.comella@gmail.com>
Wed, 30 Aug 2017 15:26:03 -0700
changeset 656300 52c616720fb1735e593f02330d1dd02db45f501f
parent 656299 07d83ea9d48074de04fc01e137a37396beacb1ba
child 656301 68d17e59231fb5e7fc4232d4256b925e6e4fce7e
push id77157
push usermichael.l.comella@gmail.com
push dateThu, 31 Aug 2017 00:16:18 +0000
reviewersliuche
bugs1393699
milestone57.0a1
Bug 1393699: Add referrerURI to Tabs.loadUrl flows. r=liuche This will allow us to call Tabs.loadUrl with a referrer URI from the Pocket top stories. MozReview-Commit-ID: IGdoTo80SGG
mobile/android/base/java/org/mozilla/gecko/Tabs.java
mobile/android/chrome/content/browser.js
--- a/mobile/android/base/java/org/mozilla/gecko/Tabs.java
+++ b/mobile/android/base/java/org/mozilla/gecko/Tabs.java
@@ -905,50 +905,51 @@ public class Tabs implements BundleEvent
      *
      * @param url   URL of page to load
      * @param flags flags used to load tab
      *
      * @return      the Tab if a new one was created; null otherwise
      */
     @RobocopTarget
     public Tab loadUrl(String url, int flags) {
-        return loadUrl(url, null, INVALID_TAB_ID, null, flags);
+        return loadUrl(url, null, null, INVALID_TAB_ID, null, flags);
     }
 
     public Tab loadUrlWithIntentExtras(final String url, final SafeIntent intent, final int flags) {
         // We can't directly create a listener to tell when the user taps on the "What's new"
         // notification, so we use this intent handling as a signal that they tapped the notification.
         if (intent.getBooleanExtra(WhatsNewReceiver.EXTRA_WHATSNEW_NOTIFICATION, false)) {
             Telemetry.sendUIEvent(TelemetryContract.Event.ACTION, TelemetryContract.Method.NOTIFICATION,
                     WhatsNewReceiver.EXTRA_WHATSNEW_NOTIFICATION);
         }
 
         // Note: we don't get the URL from the intent so the calling
         // method has the opportunity to change the URL if applicable.
-        return loadUrl(url, null, INVALID_TAB_ID, intent, flags);
+        return loadUrl(url, null, null, INVALID_TAB_ID, intent, flags);
     }
 
     public Tab loadUrl(final String url, final String searchEngine, final int parentId, final int flags) {
-        return loadUrl(url, searchEngine, parentId, null, flags);
+        return loadUrl(url, searchEngine, null, parentId, null, flags);
     }
 
     /**
      * Loads a tab with the given URL.
      *
      * @param url          URL of page to load, or search term used if searchEngine is given
      * @param searchEngine if given, the search engine with this name is used
      *                     to search for the url string; if null, the URL is loaded directly
+     * @param referrerUri  the URI which referred this page load, or null if there is none.
      * @param parentId     ID of this tab's parent, or INVALID_TAB_ID (-1) if it has no parent
      * @param intent       an intent whose extras are used to modify the request
      * @param flags        flags used to load tab
      *
      * @return             the Tab if a new one was created; null otherwise
      */
-    public Tab loadUrl(final String url, final String searchEngine, final int parentId,
-                   final SafeIntent intent, final int flags) {
+    public Tab loadUrl(final String url, final String searchEngine, @Nullable final String referrerUri,
+            final int parentId, @Nullable final SafeIntent intent, final int flags) {
         final GeckoBundle data = new GeckoBundle();
         Tab tabToSelect = null;
         boolean delayLoad = (flags & LOADURL_DELAY_LOAD) != 0;
 
         // delayLoad implies background tab
         boolean background = delayLoad || (flags & LOADURL_BACKGROUND) != 0;
 
         boolean isPrivate = (flags & LOADURL_PRIVATE) != 0 || (intent != null && intent.getBooleanExtra(PRIVATE_TAB_INTENT_EXTRA, false));
@@ -959,16 +960,17 @@ public class Tabs implements BundleEvent
 
         data.putString("url", url);
         data.putString("engine", searchEngine);
         data.putInt("parentId", parentId);
         data.putBoolean("userEntered", userEntered);
         data.putBoolean("isPrivate", isPrivate);
         data.putBoolean("pinned", (flags & LOADURL_PINNED) != 0);
         data.putBoolean("desktopMode", desktopMode);
+        data.putString("referrerURI", referrerUri);
 
         final boolean needsNewTab;
         final String applicationId = (intent == null) ? null :
                 intent.getStringExtra(Browser.EXTRA_APPLICATION_ID);
         if (applicationId == null) {
             needsNewTab = (flags & LOADURL_NEW_TAB) != 0;
         } else {
             // If you modify this code, be careful that intent != null.
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -1892,16 +1892,25 @@ var BrowserApp = {
           isPrivate: (data.isPrivate === true),
           pinned: (data.pinned === true),
           delayLoad: (delayLoad === true),
           desktopMode: (data.desktopMode === true)
         };
 
         params.userRequested = url;
 
+        if (data.referrerURI) {
+          try {
+            params.referrerURI = Services.io.newURI(data.referrerURI);
+          } catch (e) {
+            console.warn("Tab:Load referrerURI is invalid - ignoring."); // don't log exception to avoid leaking urls.
+            params.referrerURI = null;
+          }
+        }
+
         if (data.engine) {
           let engine = Services.search.getEngineByName(data.engine);
           if (engine) {
             let submission = engine.getSubmission(url);
             url = submission.uri.spec;
             params.postData = submission.postData;
             params.isSearch = true;
           }