Commit 079c096d authored by Tim Volodine's avatar Tim Volodine Committed by Commit Bot

[AW NS] provide proper user_agent in onDownloadStart callback and add tests

The user agent is not necessarily set in the request headers
during InterceptDownload (e.g. because they are not initialized yet).

In this patch:
- make sure a user agent is set in the case of uninitialized headers.
- make sure the user agent override is used when it is set by using
WebSetting.setUserAgentString().
- add download related instrumentation tests for the user agent
setting.

Tests:
- AwContentsTest.testDownload
- AwContentsTest.testDownloaWithCustomUserAgent

BUG=893568,841556

Cq-Include-Trybots: master.tryserver.chromium.android:android_mojo
Change-Id: Ifc432fd93dd68ffb7416a25b6d6d1b5d1021e327
Reviewed-on: https://chromium-review.googlesource.com/c/1477606
Commit-Queue: Tim Volodine <timvolodine@chromium.org>
Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#634890}
parent 929f77d4
...@@ -4,14 +4,15 @@ ...@@ -4,14 +4,15 @@
#include "android_webview/browser/aw_download_manager_delegate.h" #include "android_webview/browser/aw_download_manager_delegate.h"
#include "android_webview/browser/aw_content_browser_client.h"
#include "android_webview/browser/aw_contents_client_bridge.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/task/post_task.h"
#include "components/download/public/common/download_danger_type.h" #include "components/download/public/common/download_danger_type.h"
#include "components/download/public/common/download_item.h" #include "components/download/public/common/download_item.h"
#include "android_webview/browser/aw_contents_client_bridge.h"
#include "base/task/post_task.h"
#include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
namespace android_webview { namespace android_webview {
...@@ -70,10 +71,16 @@ bool AwDownloadManagerDelegate::InterceptDownloadIfApplicable( ...@@ -70,10 +71,16 @@ bool AwDownloadManagerDelegate::InterceptDownloadIfApplicable(
const std::string& request_origin, const std::string& request_origin,
int64_t content_length, int64_t content_length,
content::WebContents* web_contents) { content::WebContents* web_contents) {
std::string aw_user_agent = web_contents->GetUserAgentOverride();
if (aw_user_agent.empty()) {
// use default user agent if nothing is provided
aw_user_agent = user_agent.empty() ? GetUserAgent() : user_agent;
}
base::PostTaskWithTraits( base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::UI}, FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(&DownloadStartingOnUIThread, web_contents, url, user_agent, base::BindOnce(&DownloadStartingOnUIThread, web_contents, url,
content_disposition, mime_type, content_length)); aw_user_agent, content_disposition, mime_type,
content_length));
return true; return true;
} }
......
...@@ -10,6 +10,12 @@ ...@@ -10,6 +10,12 @@
#include "base/supports_user_data.h" #include "base/supports_user_data.h"
#include "content/public/browser/download_manager_delegate.h" #include "content/public/browser/download_manager_delegate.h"
namespace content {
class WebContents;
} // namespace content
namespace android_webview { namespace android_webview {
// Android WebView does not use Chromium downloads, so implement methods here to // Android WebView does not use Chromium downloads, so implement methods here to
......
...@@ -343,10 +343,26 @@ public class AwContentsTest { ...@@ -343,10 +343,26 @@ public class AwContentsTest {
@Feature({"AndroidWebView", "Downloads"}) @Feature({"AndroidWebView", "Downloads"})
@SmallTest @SmallTest
public void testDownload() throws Throwable { public void testDownload() throws Throwable {
downloadAndCheck(null);
}
@Test
@Feature({"AndroidWebView", "Downloads"})
@SmallTest
public void testDownloadWithCustomUserAgent() throws Throwable {
downloadAndCheck("Custom User Agent");
}
private void downloadAndCheck(String customUserAgent) throws Throwable {
AwTestContainerView testView = AwTestContainerView testView =
mActivityTestRule.createAwTestContainerViewOnMainSync(mContentsClient); mActivityTestRule.createAwTestContainerViewOnMainSync(mContentsClient);
AwContents awContents = testView.getAwContents(); AwContents awContents = testView.getAwContents();
if (customUserAgent != null) {
AwSettings awSettings = mActivityTestRule.getAwSettingsOnUiThread(awContents);
awSettings.setUserAgentString(customUserAgent);
}
final String data = "download data"; final String data = "download data";
final String contentDisposition = "attachment;filename=\"download.txt\""; final String contentDisposition = "attachment;filename=\"download.txt\"";
final String mimeType = "text/plain"; final String mimeType = "text/plain";
...@@ -370,6 +386,13 @@ public class AwContentsTest { ...@@ -370,6 +386,13 @@ public class AwContentsTest {
Assert.assertEquals(contentDisposition, downloadStartHelper.getContentDisposition()); Assert.assertEquals(contentDisposition, downloadStartHelper.getContentDisposition());
Assert.assertEquals(mimeType, downloadStartHelper.getMimeType()); Assert.assertEquals(mimeType, downloadStartHelper.getMimeType());
Assert.assertEquals(data.length(), downloadStartHelper.getContentLength()); Assert.assertEquals(data.length(), downloadStartHelper.getContentLength());
Assert.assertFalse(downloadStartHelper.getUserAgent().isEmpty());
if (customUserAgent != null) {
Assert.assertEquals(customUserAgent, downloadStartHelper.getUserAgent());
} else {
Assert.assertEquals(
downloadStartHelper.getUserAgent(), AwSettings.getDefaultUserAgent());
}
} finally { } finally {
webServer.shutdown(); webServer.shutdown();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment