Pass referer on download requests

Bug: 6662557
Change-Id: Ib7fdc4b3f1e0f7370631d8a222565faaee550bfb
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index eefeffe..385d494 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -512,7 +512,7 @@
                             case R.id.save_link_context_menu_id:
                             case R.id.download_context_menu_id:
                                 DownloadHandler.onDownloadStartNoStream(
-                                        mActivity, url, null, null, null,
+                                        mActivity, url, null, null, null, null,
                                         view.isPrivateBrowsingEnabled());
                                 break;
                         }
@@ -1032,10 +1032,11 @@
 
     @Override
     public void onDownloadStart(Tab tab, String url, String userAgent,
-            String contentDisposition, String mimetype, long contentLength) {
+            String contentDisposition, String mimetype, String referer,
+            long contentLength) {
         WebView w = tab.getWebView();
         DownloadHandler.onDownloadStart(mActivity, url, userAgent,
-                contentDisposition, mimetype, w.isPrivateBrowsingEnabled());
+                contentDisposition, mimetype, referer, w.isPrivateBrowsingEnabled());
         if (w.copyBackForwardList().getSize() == 0) {
             // This Tab was opened for the sole purpose of downloading a
             // file. Remove it.
@@ -2174,7 +2175,7 @@
                 saveDataUri();
             } else {
                 DownloadHandler.onDownloadStartNoStream(mActivity, mText, null,
-                        null, null, mPrivateBrowsing);
+                        null, null, null, mPrivateBrowsing);
             }
             return true;
         }
diff --git a/src/com/android/browser/DownloadHandler.java b/src/com/android/browser/DownloadHandler.java
index 5517e59..dee10ae 100644
--- a/src/com/android/browser/DownloadHandler.java
+++ b/src/com/android/browser/DownloadHandler.java
@@ -21,7 +21,6 @@
 import android.app.DownloadManager;
 import android.content.ActivityNotFoundException;
 import android.content.ComponentName;
-import android.content.ContentValues;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
@@ -53,11 +52,12 @@
      * @param userAgent User agent of the downloading application.
      * @param contentDisposition Content-disposition http header, if present.
      * @param mimetype The mimetype of the content reported by the server
+     * @param referer The referer associated with the downloaded url
      * @param privateBrowsing If the request is coming from a private browsing tab.
      */
     public static void onDownloadStart(Activity activity, String url,
             String userAgent, String contentDisposition, String mimetype,
-            boolean privateBrowsing) {
+            String referer, boolean privateBrowsing) {
         // if we're dealing wih A/V content that's not explicitly marked
         //     for download, check if it's streamable.
         if (contentDisposition == null
@@ -95,7 +95,7 @@
             }
         }
         onDownloadStartNoStream(activity, url, userAgent, contentDisposition,
-                mimetype, privateBrowsing);
+                mimetype, referer, privateBrowsing);
     }
 
     // This is to work around the fact that java.net.URI throws Exceptions
@@ -136,11 +136,12 @@
      * @param userAgent User agent of the downloading application.
      * @param contentDisposition Content-disposition http header, if present.
      * @param mimetype The mimetype of the content reported by the server
+     * @param referer The referer associated with the downloaded url
      * @param privateBrowsing If the request is coming from a private browsing tab.
      */
     /*package */ static void onDownloadStartNoStream(Activity activity,
             String url, String userAgent, String contentDisposition,
-            String mimetype, boolean privateBrowsing) {
+            String mimetype, String referer, boolean privateBrowsing) {
 
         String filename = URLUtil.guessFileName(url,
                 contentDisposition, mimetype);
@@ -204,6 +205,7 @@
         String cookies = CookieManager.getInstance().getCookie(url, privateBrowsing);
         request.addRequestHeader("cookie", cookies);
         request.addRequestHeader("User-Agent", userAgent);
+        request.addRequestHeader("Referer", referer);
         request.setNotificationVisibility(
                 DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
         if (mimetype == null) {
diff --git a/src/com/android/browser/PreloadController.java b/src/com/android/browser/PreloadController.java
index a7feaee..881f19e 100644
--- a/src/com/android/browser/PreloadController.java
+++ b/src/com/android/browser/PreloadController.java
@@ -153,7 +153,7 @@
     @Override
     public void onDownloadStart(Tab tab, String url, String useragent,
                                     String contentDisposition, String mimeType,
-                                    long contentLength) {
+                                    String referer, long contentLength) {
         if (LOGD_ENABLED) Log.d(LOGTAG, "onDownloadStart()");
     }
 
diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java
index 484357e..b5000c2 100644
--- a/src/com/android/browser/Tab.java
+++ b/src/com/android/browser/Tab.java
@@ -46,9 +46,9 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewStub;
+import android.webkit.BrowserDownloadListener;
 import android.webkit.ClientCertRequestHandler;
 import android.webkit.ConsoleMessage;
-import android.webkit.DownloadListener;
 import android.webkit.GeolocationPermissions;
 import android.webkit.HttpAuthHandler;
 import android.webkit.SslErrorHandler;
@@ -172,7 +172,7 @@
     private ErrorConsoleView mErrorConsole;
     // The listener that gets invoked when a download is started from the
     // mMainView
-    private final DownloadListener mDownloadListener;
+    private final BrowserDownloadListener mDownloadListener;
     // Listener used to know when we move forward or back in the history list.
     private final WebBackForwardListClient mWebBackForwardListClient;
     private DataController mDataController;
@@ -1160,12 +1160,12 @@
         mInPageLoad = false;
         mInForeground = false;
 
-        mDownloadListener = new DownloadListener() {
+        mDownloadListener = new BrowserDownloadListener() {
             public void onDownloadStart(String url, String userAgent,
-                    String contentDisposition, String mimetype,
+                    String contentDisposition, String mimetype, String referer,
                     long contentLength) {
                 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
-                        mimetype, contentLength);
+                        mimetype, referer, contentLength);
             }
         };
         mWebBackForwardListClient = new WebBackForwardListClient() {
@@ -1354,12 +1354,12 @@
                     mWebChromeClient));
             // Set a different DownloadListener for the mSubView, since it will
             // just need to dismiss the mSubView, rather than close the Tab
-            mSubView.setDownloadListener(new DownloadListener() {
+            mSubView.setDownloadListener(new BrowserDownloadListener() {
                 public void onDownloadStart(String url, String userAgent,
-                        String contentDisposition, String mimetype,
+                        String contentDisposition, String mimetype, String referer,
                         long contentLength) {
                     mWebViewController.onDownloadStart(Tab.this, url, userAgent,
-                            contentDisposition, mimetype, contentLength);
+                            contentDisposition, mimetype, referer, contentLength);
                     if (mSubView.copyBackForwardList().getSize() == 0) {
                         // This subwindow was opened for the sole purpose of
                         // downloading a file. Remove it.
diff --git a/src/com/android/browser/WebViewController.java b/src/com/android/browser/WebViewController.java
index fef6964..e814ff9 100644
--- a/src/com/android/browser/WebViewController.java
+++ b/src/com/android/browser/WebViewController.java
@@ -73,7 +73,7 @@
             final String host, final String realm);
 
     void onDownloadStart(Tab tab, String url, String useragent, String contentDisposition,
-            String mimeType, long contentLength);
+            String mimeType, String referer, long contentLength);
 
     void showCustomView(Tab tab, View view, int requestedOrientation,
             WebChromeClient.CustomViewCallback callback);