Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(337)

Side by Side Diff: chrome/browser/android/offline_pages/downloads/resource_throttle.cc

Issue 2841823004: Use DownloadManager to download non-html res when download icon pressed (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/android/offline_pages/downloads/resource_throttle.h" 5 #include "chrome/browser/android/offline_pages/downloads/resource_throttle.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "chrome/browser/android/offline_pages/offline_page_utils.h" 8 #include "chrome/browser/android/offline_pages/offline_page_utils.h"
9 #include "components/offline_pages/core/client_namespace_constants.h" 9 #include "components/offline_pages/core/client_namespace_constants.h"
10 #include "content/public/browser/browser_thread.h" 10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/resource_request_info.h" 11 #include "content/public/browser/resource_request_info.h"
12 #include "content/public/browser/web_contents.h" 12 #include "content/public/browser/web_contents.h"
13 #include "net/base/mime_util.h"
14 13
15 namespace { 14 namespace {
16 // Mime type of download resource that should trigger handoff to OfflinePages
17 // backend for full page load and snapshot.
18 bool CanDownloadAsOfflinePage(const std::string& contents_mime_type) {
19 return net::MatchesMimeType(contents_mime_type, "text/html") ||
20 net::MatchesMimeType(contents_mime_type, "application/xhtml+xml");
21 }
22
23 void WillStartOfflineRequestOnUIThread( 15 void WillStartOfflineRequestOnUIThread(
24 const GURL& url, 16 const GURL& url,
25 const content::ResourceRequestInfo::WebContentsGetter& contents_getter) { 17 const content::ResourceRequestInfo::WebContentsGetter& contents_getter) {
26 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 18 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
27 content::WebContents* web_contents = contents_getter.Run(); 19 content::WebContents* web_contents = contents_getter.Run();
28 if (!web_contents) 20 if (!web_contents)
29 return; 21 return;
30 offline_pages::OfflinePageUtils::ScheduleDownload( 22 offline_pages::OfflinePageUtils::ScheduleDownload(
31 web_contents, offline_pages::kDownloadNamespace, url, 23 web_contents, offline_pages::kDownloadNamespace, url,
32 offline_pages::OfflinePageUtils::DownloadUIActionFlags::ALL); 24 offline_pages::OfflinePageUtils::DownloadUIActionFlags::ALL);
33 } 25 }
34 } // namespace 26 } // namespace
35 27
36 namespace offline_pages { 28 namespace offline_pages {
37 namespace downloads { 29 namespace downloads {
38 30
39 ResourceThrottle::ResourceThrottle(const net::URLRequest* request) 31 ResourceThrottle::ResourceThrottle(const net::URLRequest* request)
40 : request_(request) { 32 : request_(request) {
41 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 33 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
42 } 34 }
43 35
44 ResourceThrottle::~ResourceThrottle() { 36 ResourceThrottle::~ResourceThrottle() {
45 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 37 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
46 } 38 }
47 39
48 void ResourceThrottle::WillProcessResponse(bool* defer) { 40 void ResourceThrottle::WillProcessResponse(bool* defer) {
49 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 41 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
50 std::string mime_type; 42 std::string mime_type;
51 request_->GetMimeType(&mime_type); 43 request_->GetMimeType(&mime_type);
52 if (CanDownloadAsOfflinePage(mime_type)) { 44 if (offline_pages::OfflinePageUtils::CanDownloadAsOfflinePage(mime_type)) {
53 const content::ResourceRequestInfo* info = 45 const content::ResourceRequestInfo* info =
54 content::ResourceRequestInfo::ForRequest(request_); 46 content::ResourceRequestInfo::ForRequest(request_);
55 if (!info) 47 if (!info)
56 return; 48 return;
57 content::BrowserThread::PostTask( 49 content::BrowserThread::PostTask(
58 content::BrowserThread::UI, FROM_HERE, 50 content::BrowserThread::UI, FROM_HERE,
59 base::Bind(&WillStartOfflineRequestOnUIThread, request_->url(), 51 base::Bind(&WillStartOfflineRequestOnUIThread, request_->url(),
60 info->GetWebContentsGetterForRequest())); 52 info->GetWebContentsGetterForRequest()));
61 Cancel(); 53 Cancel();
62 } 54 }
63 } 55 }
64 56
65 const char* ResourceThrottle::GetNameForLogging() const { 57 const char* ResourceThrottle::GetNameForLogging() const {
66 return "offline_pages::downloads::ResourceThrottle"; 58 return "offline_pages::downloads::ResourceThrottle";
67 } 59 }
68 60
69 } // namespace downloads 61 } // namespace downloads
70 } // namespace offline_pages 62 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698