OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/offline_pages/downloads/resource_throttle.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "chrome/browser/android/offline_pages/offline_page_utils.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/resource_request_info.h" |
| 11 #include "content/public/browser/web_contents.h" |
| 12 #include "net/base/mime_util.h" |
| 13 |
| 14 namespace { |
| 15 // Mime type of download resource that should trigger handoff to OfflinePages |
| 16 // backend for full page load and snapshot. |
| 17 bool CanDownloadAsOfflinePage(const std::string& contents_mime_type) { |
| 18 return net::MatchesMimeType(contents_mime_type, "text/html") || |
| 19 net::MatchesMimeType(contents_mime_type, "application/xhtml+xml"); |
| 20 } |
| 21 |
| 22 void WillStartOfflineRequestOnUIThread( |
| 23 const GURL& url, |
| 24 const content::ResourceRequestInfo::WebContentsGetter& contents_getter) { |
| 25 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 26 content::WebContents* web_contents = contents_getter.Run(); |
| 27 if (!web_contents) |
| 28 return; |
| 29 offline_pages::OfflinePageUtils::StartOfflinePageDownload( |
| 30 web_contents->GetBrowserContext(), url); |
| 31 } |
| 32 } // namespace |
| 33 |
| 34 namespace offline_pages { |
| 35 namespace downloads { |
| 36 |
| 37 ResourceThrottle::ResourceThrottle(const net::URLRequest* request) |
| 38 : request_(request) { |
| 39 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 40 } |
| 41 |
| 42 ResourceThrottle::~ResourceThrottle() { |
| 43 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 44 } |
| 45 |
| 46 void ResourceThrottle::WillProcessResponse(bool* defer) { |
| 47 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 48 std::string mime_type; |
| 49 request_->GetMimeType(&mime_type); |
| 50 if (CanDownloadAsOfflinePage(mime_type)) { |
| 51 const content::ResourceRequestInfo* info = |
| 52 content::ResourceRequestInfo::ForRequest(request_); |
| 53 if (!info) |
| 54 return; |
| 55 content::BrowserThread::PostTask( |
| 56 content::BrowserThread::UI, FROM_HERE, |
| 57 base::Bind(&WillStartOfflineRequestOnUIThread, request_->url(), |
| 58 info->GetWebContentsGetterForRequest())); |
| 59 Cancel(); |
| 60 } |
| 61 } |
| 62 |
| 63 const char* ResourceThrottle::GetNameForLogging() const { |
| 64 return "offline_pages::downloads::ResourceThrottle"; |
| 65 } |
| 66 |
| 67 } // namespace downloads |
| 68 } // namespace offline_pages |
OLD | NEW |