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/download/download_controller.h" | 5 #include "chrome/browser/android/download/download_controller.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/android/context_utils.h" | 10 #include "base/android/context_utils.h" |
11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
18 #include "chrome/browser/android/download/chrome_download_delegate.h" | 18 #include "chrome/browser/android/download/chrome_download_delegate.h" |
19 #include "chrome/browser/android/download/dangerous_download_infobar_delegate.h" | 19 #include "chrome/browser/android/download/dangerous_download_infobar_delegate.h" |
20 #include "chrome/browser/android/download/download_manager_service.h" | 20 #include "chrome/browser/android/download/download_manager_service.h" |
| 21 #include "chrome/browser/android/offline_pages/offline_page_utils.h" |
21 #include "chrome/browser/infobars/infobar_service.h" | 22 #include "chrome/browser/infobars/infobar_service.h" |
22 #include "chrome/browser/ui/android/view_android_helper.h" | 23 #include "chrome/browser/ui/android/view_android_helper.h" |
23 #include "content/public/browser/browser_context.h" | 24 #include "content/public/browser/browser_context.h" |
24 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 26 #include "content/public/browser/download_interrupt_reasons.h" |
25 #include "content/public/browser/download_manager.h" | 27 #include "content/public/browser/download_manager.h" |
26 #include "content/public/browser/download_url_parameters.h" | 28 #include "content/public/browser/download_url_parameters.h" |
27 #include "content/public/browser/render_process_host.h" | 29 #include "content/public/browser/render_process_host.h" |
28 #include "content/public/browser/render_view_host.h" | 30 #include "content/public/browser/render_view_host.h" |
29 #include "content/public/common/referrer.h" | 31 #include "content/public/common/referrer.h" |
30 #include "jni/DownloadController_jni.h" | 32 #include "jni/DownloadController_jni.h" |
31 #include "net/base/filename_util.h" | 33 #include "net/base/filename_util.h" |
32 #include "ui/android/view_android.h" | 34 #include "ui/android/view_android.h" |
33 #include "ui/android/window_android.h" | 35 #include "ui/android/window_android.h" |
34 #include "ui/base/page_transition_types.h" | 36 #include "ui/base/page_transition_types.h" |
(...skipping 15 matching lines...) Expand all Loading... |
50 WebContents* GetWebContents(int render_process_id, int render_view_id) { | 52 WebContents* GetWebContents(int render_process_id, int render_view_id) { |
51 content::RenderViewHost* render_view_host = | 53 content::RenderViewHost* render_view_host = |
52 content::RenderViewHost::FromID(render_process_id, render_view_id); | 54 content::RenderViewHost::FromID(render_process_id, render_view_id); |
53 | 55 |
54 if (!render_view_host) | 56 if (!render_view_host) |
55 return nullptr; | 57 return nullptr; |
56 | 58 |
57 return WebContents::FromRenderViewHost(render_view_host); | 59 return WebContents::FromRenderViewHost(render_view_host); |
58 } | 60 } |
59 | 61 |
| 62 void DownloadItemCreationCallback( |
| 63 BrowserContext* context, |
| 64 const GURL& url, |
| 65 DownloadItem* item, |
| 66 content::DownloadInterruptReason interrupt_reason) { |
| 67 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 68 if (interrupt_reason != |
| 69 content::DOWNLOAD_INTERRUPT_REASON_PAGE_DOWNLOAD_HANDOFF) { |
| 70 return; |
| 71 } |
| 72 offline_pages::OfflinePageUtils::CreateOfflinePageDownload(context, url); |
| 73 } |
| 74 |
60 void CreateContextMenuDownload(int render_process_id, | 75 void CreateContextMenuDownload(int render_process_id, |
61 int render_view_id, | 76 int render_view_id, |
62 const content::ContextMenuParams& params, | 77 const content::ContextMenuParams& params, |
63 bool is_link, | 78 bool is_link, |
64 const std::string& extra_headers, | 79 const std::string& extra_headers, |
65 bool granted) { | 80 bool granted) { |
66 if (!granted) | 81 if (!granted) |
67 return; | 82 return; |
68 | 83 |
69 content::WebContents* web_contents = | 84 content::WebContents* web_contents = |
(...skipping 16 matching lines...) Expand all Loading... |
86 dl_params->set_referrer(referrer); | 101 dl_params->set_referrer(referrer); |
87 if (is_link) | 102 if (is_link) |
88 dl_params->set_referrer_encoding(params.frame_charset); | 103 dl_params->set_referrer_encoding(params.frame_charset); |
89 net::HttpRequestHeaders headers; | 104 net::HttpRequestHeaders headers; |
90 headers.AddHeadersFromString(extra_headers); | 105 headers.AddHeadersFromString(extra_headers); |
91 for (net::HttpRequestHeaders::Iterator it(headers); it.GetNext();) | 106 for (net::HttpRequestHeaders::Iterator it(headers); it.GetNext();) |
92 dl_params->add_request_header(it.name(), it.value()); | 107 dl_params->add_request_header(it.name(), it.value()); |
93 if (!is_link && extra_headers.empty()) | 108 if (!is_link && extra_headers.empty()) |
94 dl_params->set_prefer_cache(true); | 109 dl_params->set_prefer_cache(true); |
95 dl_params->set_prompt(false); | 110 dl_params->set_prompt(false); |
| 111 dl_params->set_callback(base::Bind(DownloadItemCreationCallback, |
| 112 web_contents->GetBrowserContext(), |
| 113 dl_params->url())); |
96 dlm->DownloadUrl(std::move(dl_params)); | 114 dlm->DownloadUrl(std::move(dl_params)); |
97 } | 115 } |
98 | 116 |
99 // Check if an interrupted download item can be auto resumed. | 117 // Check if an interrupted download item can be auto resumed. |
100 bool IsInterruptedDownloadAutoResumable(content::DownloadItem* download_item) { | 118 bool IsInterruptedDownloadAutoResumable(content::DownloadItem* download_item) { |
101 int interrupt_reason = download_item->GetLastReason(); | 119 int interrupt_reason = download_item->GetLastReason(); |
102 DCHECK_NE(interrupt_reason, content::DOWNLOAD_INTERRUPT_REASON_NONE); | 120 DCHECK_NE(interrupt_reason, content::DOWNLOAD_INTERRUPT_REASON_NONE); |
103 return | 121 return |
104 interrupt_reason == content::DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT || | 122 interrupt_reason == content::DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT || |
105 interrupt_reason == content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED || | 123 interrupt_reason == content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED || |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 void DownloadController::StartContextMenuDownload( | 345 void DownloadController::StartContextMenuDownload( |
328 const ContextMenuParams& params, WebContents* web_contents, bool is_link, | 346 const ContextMenuParams& params, WebContents* web_contents, bool is_link, |
329 const std::string& extra_headers) { | 347 const std::string& extra_headers) { |
330 int process_id = web_contents->GetRenderProcessHost()->GetID(); | 348 int process_id = web_contents->GetRenderProcessHost()->GetID(); |
331 int routing_id = web_contents->GetRoutingID(); | 349 int routing_id = web_contents->GetRoutingID(); |
332 AcquireFileAccessPermission( | 350 AcquireFileAccessPermission( |
333 web_contents, base::Bind(&CreateContextMenuDownload, process_id, | 351 web_contents, base::Bind(&CreateContextMenuDownload, process_id, |
334 routing_id, params, is_link, extra_headers)); | 352 routing_id, params, is_link, extra_headers)); |
335 } | 353 } |
336 | 354 |
OLD | NEW |