| OLD | NEW |
| 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" | 13 #include "net/base/mime_util.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 // Mime type of download resource that should trigger handoff to OfflinePages | 16 // Check if the url and mime type of a download resource should trigger handoff |
| 17 // backend for full page load and snapshot. | 17 // to OfflinePages backend for full page load and snapshot. |
| 18 bool CanDownloadAsOfflinePage(const std::string& contents_mime_type) { | 18 bool CanDownloadAsOfflinePage( |
| 19 return net::MatchesMimeType(contents_mime_type, "text/html") || | 19 const GURL& url, const std::string& contents_mime_type) { |
| 20 net::MatchesMimeType(contents_mime_type, "application/xhtml+xml"); | 20 return url.SchemeIsHTTPOrHTTPS() && |
| 21 (net::MatchesMimeType(contents_mime_type, "text/html") || |
| 22 net::MatchesMimeType(contents_mime_type, "application/xhtml+xml")); |
| 21 } | 23 } |
| 22 | 24 |
| 23 void WillStartOfflineRequestOnUIThread( | 25 void WillStartOfflineRequestOnUIThread( |
| 24 const GURL& url, | 26 const GURL& url, |
| 25 const content::ResourceRequestInfo::WebContentsGetter& contents_getter) { | 27 const content::ResourceRequestInfo::WebContentsGetter& contents_getter) { |
| 26 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 28 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 27 content::WebContents* web_contents = contents_getter.Run(); | 29 content::WebContents* web_contents = contents_getter.Run(); |
| 28 if (!web_contents) | 30 if (!web_contents) |
| 29 return; | 31 return; |
| 30 offline_pages::OfflinePageUtils::ScheduleDownload( | 32 offline_pages::OfflinePageUtils::ScheduleDownload( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 42 } | 44 } |
| 43 | 45 |
| 44 ResourceThrottle::~ResourceThrottle() { | 46 ResourceThrottle::~ResourceThrottle() { |
| 45 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 47 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 46 } | 48 } |
| 47 | 49 |
| 48 void ResourceThrottle::WillProcessResponse(bool* defer) { | 50 void ResourceThrottle::WillProcessResponse(bool* defer) { |
| 49 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 51 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 50 std::string mime_type; | 52 std::string mime_type; |
| 51 request_->GetMimeType(&mime_type); | 53 request_->GetMimeType(&mime_type); |
| 52 if (CanDownloadAsOfflinePage(mime_type)) { | 54 |
| 55 if (CanDownloadAsOfflinePage(request_->url(), mime_type)) { |
| 53 const content::ResourceRequestInfo* info = | 56 const content::ResourceRequestInfo* info = |
| 54 content::ResourceRequestInfo::ForRequest(request_); | 57 content::ResourceRequestInfo::ForRequest(request_); |
| 55 if (!info) | 58 if (!info) |
| 56 return; | 59 return; |
| 57 content::BrowserThread::PostTask( | 60 content::BrowserThread::PostTask( |
| 58 content::BrowserThread::UI, FROM_HERE, | 61 content::BrowserThread::UI, FROM_HERE, |
| 59 base::Bind(&WillStartOfflineRequestOnUIThread, request_->url(), | 62 base::Bind(&WillStartOfflineRequestOnUIThread, request_->url(), |
| 60 info->GetWebContentsGetterForRequest())); | 63 info->GetWebContentsGetterForRequest())); |
| 61 Cancel(); | 64 Cancel(); |
| 62 } | 65 } |
| 63 } | 66 } |
| 64 | 67 |
| 65 const char* ResourceThrottle::GetNameForLogging() const { | 68 const char* ResourceThrottle::GetNameForLogging() const { |
| 66 return "offline_pages::downloads::ResourceThrottle"; | 69 return "offline_pages::downloads::ResourceThrottle"; |
| 67 } | 70 } |
| 68 | 71 |
| 69 } // namespace downloads | 72 } // namespace downloads |
| 70 } // namespace offline_pages | 73 } // namespace offline_pages |
| OLD | NEW |