Commit 03074588 authored by jianli's avatar jianli Committed by Commit bot

Use DownloadManager to download non-html res when download icon pressed

We check the content's MIME type. If it is not meant for HTML page,we
route to DownloadManager.

BUG=673635

Review-Url: https://codereview.chromium.org/2841823004
Cr-Commit-Position: refs/heads/master@{#467931}
parent a7273fae
......@@ -28,6 +28,9 @@
#include "components/offline_pages/core/offline_page_feature.h"
#include "components/offline_pages/core/offline_page_model.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/download_url_parameters.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "jni/OfflinePageDownloadBridge_jni.h"
#include "net/base/filename_util.h"
......@@ -337,10 +340,37 @@ void OfflinePageDownloadBridge::StartDownload(
return;
GURL url = web_contents->GetLastCommittedURL();
if (url.is_empty())
return;
GURL original_url =
offline_pages::OfflinePageUtils::GetOriginalURLFromWebContents(
web_contents);
// If the page is not a HTML page, route to DownloadManager.
if (!offline_pages::OfflinePageUtils::CanDownloadAsOfflinePage(
url, web_contents->GetContentsMimeType())) {
content::DownloadManager* dlm = content::BrowserContext::GetDownloadManager(
web_contents->GetBrowserContext());
std::unique_ptr<content::DownloadUrlParameters> dl_params(
content::DownloadUrlParameters::CreateForWebContentsMainFrame(
web_contents, url));
content::NavigationEntry* entry =
web_contents->GetController().GetLastCommittedEntry();
// |entry| should not be null since otherwise an empty URL is returned from
// calling GetLastCommittedURL and we should bail out earlier.
DCHECK(entry);
content::Referrer referrer =
content::Referrer::SanitizeForRequest(url, entry->GetReferrer());
dl_params->set_referrer(referrer);
dl_params->set_prefer_cache(true);
dl_params->set_prompt(false);
dlm->DownloadUrl(std::move(dl_params));
return;
}
ScopedJavaGlobalRef<jobject> j_tab_ref(env, j_tab);
OfflinePageUtils::CheckDuplicateDownloads(
......
......@@ -10,18 +10,8 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_request_info.h"
#include "content/public/browser/web_contents.h"
#include "net/base/mime_util.h"
namespace {
// Check if the url and mime type of a download resource should trigger handoff
// to OfflinePages backend for full page load and snapshot.
bool CanDownloadAsOfflinePage(
const GURL& url, const std::string& contents_mime_type) {
return url.SchemeIsHTTPOrHTTPS() &&
(net::MatchesMimeType(contents_mime_type, "text/html") ||
net::MatchesMimeType(contents_mime_type, "application/xhtml+xml"));
}
void WillStartOfflineRequestOnUIThread(
const GURL& url,
const content::ResourceRequestInfo::WebContentsGetter& contents_getter) {
......@@ -51,8 +41,8 @@ void ResourceThrottle::WillProcessResponse(bool* defer) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
std::string mime_type;
request_->GetMimeType(&mime_type);
if (CanDownloadAsOfflinePage(request_->url(), mime_type)) {
if (offline_pages::OfflinePageUtils::CanDownloadAsOfflinePage(request_->url(),
mime_type)) {
const content::ResourceRequestInfo* info =
content::ResourceRequestInfo::ForRequest(request_);
if (!info)
......
......@@ -29,6 +29,7 @@
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "net/base/mime_util.h"
namespace offline_pages {
namespace {
......@@ -298,4 +299,13 @@ void OfflinePageUtils::ScheduleDownload(content::WebContents* web_contents,
tab_helper->ScheduleDownloadHelper(web_contents, name_space, url, ui_action);
}
// static
bool OfflinePageUtils::CanDownloadAsOfflinePage(
const GURL& url,
const std::string& contents_mime_type) {
return url.SchemeIsHTTPOrHTTPS() &&
(net::MatchesMimeType(contents_mime_type, "text/html") ||
net::MatchesMimeType(contents_mime_type, "application/xhtml+xml"));
}
} // namespace offline_pages
......@@ -109,6 +109,11 @@ class OfflinePageUtils {
const std::string& name_space,
const GURL& url,
DownloadUIActionFlags ui_action);
// Determines if offline page download should be triggered based on MIME type
// of download resource.
static bool CanDownloadAsOfflinePage(const GURL& url,
const std::string& contents_mime_type);
};
} // namespace offline_pages
......
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