[email protected] | 0afff03 | 2012-01-06 20:55:00 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 5 | #include "content/browser/download/download_manager_impl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | e7557f17 | 2011-08-19 23:42:01 | [diff] [blame] | 7 | #include <iterator> |
| 8 | |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 10 | #include "base/callback.h" |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 11 | #include "base/debug/alias.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | #include "base/file_util.h" |
[email protected] | 503d0387 | 2011-05-06 08:36:26 | [diff] [blame] | 13 | #include "base/i18n/case_conversion.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "base/logging.h" |
[email protected] | f167520 | 2012-07-09 15:18:00 | [diff] [blame] | 15 | #include "base/message_loop.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 16 | #include "base/stl_util.h" |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 17 | #include "base/stringprintf.h" |
[email protected] | d59d40c | 2012-08-08 18:18:37 | [diff] [blame] | 18 | #include "base/supports_user_data.h" |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 19 | #include "base/synchronization/lock.h" |
| 20 | #include "base/sys_string_conversions.h" |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 21 | #include "build/build_config.h" |
[email protected] | 2bddd207 | 2012-06-18 16:16:51 | [diff] [blame] | 22 | #include "content/browser/download/byte_stream.h" |
[email protected] | 71bf3f5e | 2011-08-15 21:05:22 | [diff] [blame] | 23 | #include "content/browser/download/download_create_info.h" |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 24 | #include "content/browser/download/download_file_manager.h" |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 25 | #include "content/browser/download/download_item_impl.h" |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 26 | #include "content/browser/download/download_stats.h" |
[email protected] | b3c41c0b | 2012-03-06 15:48:32 | [diff] [blame] | 27 | #include "content/browser/renderer_host/render_view_host_impl.h" |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 28 | #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
[email protected] | 93ddb3c | 2012-04-11 21:44:29 | [diff] [blame] | 29 | #include "content/browser/web_contents/web_contents_impl.h" |
[email protected] | ccb79730 | 2011-12-15 16:55:11 | [diff] [blame] | 30 | #include "content/public/browser/browser_context.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 31 | #include "content/public/browser/browser_thread.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 32 | #include "content/public/browser/content_browser_client.h" |
[email protected] | bf3b08a | 2012-03-08 01:52:34 | [diff] [blame] | 33 | #include "content/public/browser/download_interrupt_reasons.h" |
[email protected] | 1bd0ef1 | 2011-10-20 05:24:17 | [diff] [blame] | 34 | #include "content/public/browser/download_manager_delegate.h" |
[email protected] | 8da82ea | 2012-03-11 22:16:52 | [diff] [blame] | 35 | #include "content/public/browser/download_persistent_store_info.h" |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 36 | #include "content/public/browser/download_url_parameters.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 37 | #include "content/public/browser/notification_service.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 38 | #include "content/public/browser/notification_types.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 39 | #include "content/public/browser/render_process_host.h" |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 40 | #include "content/public/browser/resource_context.h" |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 41 | #include "content/public/browser/web_contents_delegate.h" |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 42 | #include "net/base/load_flags.h" |
[email protected] | 27678b2a | 2012-02-04 22:09:14 | [diff] [blame] | 43 | #include "net/base/upload_data.h" |
[email protected] | eb79563 | 2012-09-01 00:19:31 | [diff] [blame] | 44 | #include "net/url_request/url_request_context.h" |
[email protected] | f859eba | 2012-05-30 17:22:49 | [diff] [blame] | 45 | #include "webkit/glue/webkit_glue.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 47 | using content::BrowserThread; |
[email protected] | 98e81406 | 2012-01-27 00:35:42 | [diff] [blame] | 48 | using content::DownloadId; |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 49 | using content::DownloadItem; |
[email protected] | 8da82ea | 2012-03-11 22:16:52 | [diff] [blame] | 50 | using content::DownloadPersistentStoreInfo; |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 51 | using content::ResourceDispatcherHostImpl; |
[email protected] | 2a6bc3e | 2011-12-28 23:51:33 | [diff] [blame] | 52 | using content::WebContents; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 53 | |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 54 | namespace { |
| 55 | |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 56 | // This is just used to remember which DownloadItems come from SavePage. |
| 57 | class SavePageData : public base::SupportsUserData::Data { |
| 58 | public: |
| 59 | // A spoonful of syntactic sugar. |
| 60 | static bool Get(DownloadItem* item) { |
| 61 | return item->GetUserData(kKey) != NULL; |
| 62 | } |
| 63 | |
| 64 | explicit SavePageData(DownloadItem* item) { |
| 65 | item->SetUserData(kKey, this); |
| 66 | } |
| 67 | |
| 68 | virtual ~SavePageData() {} |
| 69 | |
| 70 | private: |
| 71 | static const char kKey[]; |
| 72 | |
| 73 | DISALLOW_COPY_AND_ASSIGN(SavePageData); |
| 74 | }; |
| 75 | |
| 76 | const char SavePageData::kKey[] = "DownloadItem SavePageData"; |
| 77 | |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 78 | void BeginDownload(content::DownloadUrlParameters* params) { |
| 79 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 80 | // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and |
| 81 | // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so |
| 82 | // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. |
[email protected] | eb79563 | 2012-09-01 00:19:31 | [diff] [blame] | 83 | scoped_ptr<net::URLRequest> request( |
| 84 | params->resource_context()->GetRequestContext()->CreateRequest( |
| 85 | params->url(), NULL)); |
[email protected] | f859eba | 2012-05-30 17:22:49 | [diff] [blame] | 86 | request->set_referrer(params->referrer().url.spec()); |
| 87 | webkit_glue::ConfigureURLRequestForReferrerPolicy( |
| 88 | request.get(), params->referrer().policy); |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 89 | request->set_load_flags(request->load_flags() | params->load_flags()); |
| 90 | request->set_method(params->method()); |
| 91 | if (!params->post_body().empty()) |
| 92 | request->AppendBytesToUpload(params->post_body().data(), |
| 93 | params->post_body().size()); |
| 94 | if (params->post_id() >= 0) { |
[email protected] | 27678b2a | 2012-02-04 22:09:14 | [diff] [blame] | 95 | // The POST in this case does not have an actual body, and only works |
| 96 | // when retrieving data from cache. This is done because we don't want |
| 97 | // to do a re-POST without user consent, and currently don't have a good |
| 98 | // plan on how to display the UI for that. |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 99 | DCHECK(params->prefer_cache()); |
| 100 | DCHECK(params->method() == "POST"); |
[email protected] | 27678b2a | 2012-02-04 22:09:14 | [diff] [blame] | 101 | scoped_refptr<net::UploadData> upload_data = new net::UploadData(); |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 102 | upload_data->set_identifier(params->post_id()); |
[email protected] | 27678b2a | 2012-02-04 22:09:14 | [diff] [blame] | 103 | request->set_upload(upload_data); |
| 104 | } |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 105 | for (content::DownloadUrlParameters::RequestHeadersType::const_iterator iter |
| 106 | = params->request_headers_begin(); |
| 107 | iter != params->request_headers_end(); |
| 108 | ++iter) { |
| 109 | request->SetExtraRequestHeaderByName( |
| 110 | iter->first, iter->second, false/*overwrite*/); |
| 111 | } |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 112 | params->resource_dispatcher_host()->BeginDownload( |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 113 | request.Pass(), |
[email protected] | a53e2f9 | 2012-05-15 15:27:06 | [diff] [blame] | 114 | params->content_initiated(), |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 115 | params->resource_context(), |
| 116 | params->render_process_host_id(), |
| 117 | params->render_view_host_routing_id(), |
| 118 | params->prefer_cache(), |
| 119 | params->save_info(), |
| 120 | params->callback()); |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | 33d2210 | 2012-01-25 17:46:53 | [diff] [blame] | 123 | class MapValueIteratorAdapter { |
| 124 | public: |
| 125 | explicit MapValueIteratorAdapter( |
| 126 | base::hash_map<int64, DownloadItem*>::const_iterator iter) |
| 127 | : iter_(iter) { |
| 128 | } |
| 129 | ~MapValueIteratorAdapter() {} |
| 130 | |
| 131 | DownloadItem* operator*() { return iter_->second; } |
| 132 | |
| 133 | MapValueIteratorAdapter& operator++() { |
| 134 | ++iter_; |
| 135 | return *this; |
| 136 | } |
| 137 | |
| 138 | bool operator!=(const MapValueIteratorAdapter& that) const { |
| 139 | return iter_ != that.iter_; |
| 140 | } |
| 141 | |
| 142 | private: |
| 143 | base::hash_map<int64, DownloadItem*>::const_iterator iter_; |
| 144 | // Allow copy and assign. |
| 145 | }; |
| 146 | |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 147 | void EnsureNoPendingDownloadsOnFile(scoped_refptr<DownloadFileManager> dfm, |
| 148 | bool* result) { |
| 149 | if (dfm->NumberOfActiveDownloads()) |
| 150 | *result = false; |
[email protected] | 5948e1a | 2012-03-10 00:19:18 | [diff] [blame] | 151 | BrowserThread::PostTask( |
| 152 | BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
| 153 | } |
| 154 | |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 155 | void EnsureNoPendingDownloadJobsOnIO(bool* result) { |
| 156 | scoped_refptr<DownloadFileManager> download_file_manager = |
| 157 | ResourceDispatcherHostImpl::Get()->download_file_manager(); |
| 158 | BrowserThread::PostTask( |
| 159 | BrowserThread::FILE, FROM_HERE, |
| 160 | base::Bind(&EnsureNoPendingDownloadsOnFile, |
| 161 | download_file_manager, result)); |
| 162 | } |
| 163 | |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 164 | class DownloadItemFactoryImpl : public content::DownloadItemFactory { |
| 165 | public: |
| 166 | DownloadItemFactoryImpl() {} |
| 167 | virtual ~DownloadItemFactoryImpl() {} |
| 168 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 169 | virtual DownloadItemImpl* CreatePersistedItem( |
| 170 | DownloadItemImplDelegate* delegate, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 171 | content::DownloadId download_id, |
| 172 | const content::DownloadPersistentStoreInfo& info, |
| 173 | const net::BoundNetLog& bound_net_log) OVERRIDE { |
| 174 | return new DownloadItemImpl(delegate, download_id, info, bound_net_log); |
| 175 | } |
| 176 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 177 | virtual DownloadItemImpl* CreateActiveItem( |
| 178 | DownloadItemImplDelegate* delegate, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 179 | const DownloadCreateInfo& info, |
[email protected] | 94d841cf | 2012-06-12 20:58:20 | [diff] [blame] | 180 | scoped_ptr<DownloadRequestHandleInterface> request_handle, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 181 | const net::BoundNetLog& bound_net_log) OVERRIDE { |
[email protected] | 94d841cf | 2012-06-12 20:58:20 | [diff] [blame] | 182 | return new DownloadItemImpl(delegate, info, request_handle.Pass(), |
[email protected] | 66521d0 | 2012-08-14 17:51:54 | [diff] [blame] | 183 | bound_net_log); |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 186 | virtual DownloadItemImpl* CreateSavePageItem( |
| 187 | DownloadItemImplDelegate* delegate, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 188 | const FilePath& path, |
| 189 | const GURL& url, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 190 | content::DownloadId download_id, |
| 191 | const std::string& mime_type, |
| 192 | const net::BoundNetLog& bound_net_log) OVERRIDE { |
[email protected] | 66521d0 | 2012-08-14 17:51:54 | [diff] [blame] | 193 | return new DownloadItemImpl(delegate, path, url, download_id, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 194 | mime_type, bound_net_log); |
| 195 | } |
| 196 | }; |
| 197 | |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 198 | } // namespace |
| 199 | |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 200 | namespace content { |
| 201 | |
[email protected] | 5948e1a | 2012-03-10 00:19:18 | [diff] [blame] | 202 | bool DownloadManager::EnsureNoPendingDownloadsForTesting() { |
| 203 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 204 | bool result = true; |
| 205 | BrowserThread::PostTask( |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 206 | BrowserThread::IO, FROM_HERE, |
| 207 | base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); |
[email protected] | 5948e1a | 2012-03-10 00:19:18 | [diff] [blame] | 208 | MessageLoop::current()->Run(); |
| 209 | return result; |
| 210 | } |
| 211 | |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 212 | } // namespace content |
| 213 | |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 214 | DownloadManagerImpl::DownloadManagerImpl( |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 215 | DownloadFileManager* file_manager, |
| 216 | scoped_ptr<content::DownloadItemFactory> factory, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 217 | net::NetLog* net_log) |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 218 | : factory_(factory.Pass()), |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 219 | history_size_(0), |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 220 | shutdown_needed_(false), |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 221 | browser_context_(NULL), |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 222 | file_manager_(file_manager), |
[email protected] | 33c20cd9 | 2012-06-14 22:02:50 | [diff] [blame] | 223 | delegate_(NULL), |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 224 | net_log_(net_log) { |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 225 | DCHECK(file_manager); |
| 226 | if (!factory_.get()) |
| 227 | factory_.reset(new DownloadItemFactoryImpl()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 228 | } |
| 229 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 230 | DownloadManagerImpl::~DownloadManagerImpl() { |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 231 | DCHECK(!shutdown_needed_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | } |
| 233 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 234 | DownloadId DownloadManagerImpl::GetNextId() { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 235 | DownloadId id; |
| 236 | if (delegate_) |
| 237 | id = delegate_->GetNextId(); |
| 238 | if (!id.IsValid()) { |
| 239 | static int next_id; |
| 240 | id = DownloadId(browser_context_, ++next_id); |
| 241 | } |
| 242 | |
| 243 | return id; |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 244 | } |
| 245 | |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 246 | DownloadFileManager* DownloadManagerImpl::GetDownloadFileManager() { |
| 247 | return file_manager_; |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 250 | bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 251 | if (!delegate_) |
| 252 | return true; |
| 253 | |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 254 | return delegate_->ShouldOpenDownload(item); |
| 255 | } |
| 256 | |
| 257 | bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 258 | if (!delegate_) |
| 259 | return false; |
| 260 | |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 261 | return delegate_->ShouldOpenFileBasedOnExtension(path); |
| 262 | } |
| 263 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 264 | void DownloadManagerImpl::SetDelegate( |
| 265 | content::DownloadManagerDelegate* delegate) { |
| 266 | delegate_ = delegate; |
| 267 | } |
| 268 | |
[email protected] | b488b5a5 | 2012-06-06 17:01:28 | [diff] [blame] | 269 | content::DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const { |
| 270 | return delegate_; |
| 271 | } |
| 272 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 273 | void DownloadManagerImpl::Shutdown() { |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 274 | VLOG(20) << __FUNCTION__ << "()" |
| 275 | << " shutdown_needed_ = " << shutdown_needed_; |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 276 | if (!shutdown_needed_) |
| 277 | return; |
| 278 | shutdown_needed_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | |
[email protected] | 75e51b5 | 2012-02-04 16:57:54 | [diff] [blame] | 280 | FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); |
[email protected] | fb4f8d90 | 2011-09-16 06:07:08 | [diff] [blame] | 281 | // TODO(benjhayden): Consider clearing observers_. |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 282 | |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 283 | DCHECK(file_manager_); |
| 284 | BrowserThread::PostTask( |
| 285 | BrowserThread::FILE, FROM_HERE, |
| 286 | base::Bind(&DownloadFileManager::OnDownloadManagerShutdown, |
| 287 | file_manager_, make_scoped_refptr(this))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 288 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 289 | AssertContainersConsistent(); |
| 290 | |
| 291 | // Go through all downloads in downloads_. Dangerous ones we need to |
| 292 | // remove on disk, and in progress ones we need to cancel. |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 293 | for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 294 | DownloadItemImpl* download = it->second; |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 295 | |
| 296 | // Save iterator from potential erases in this set done by called code. |
| 297 | // Iterators after an erasure point are still valid for lists and |
| 298 | // associative containers such as sets. |
| 299 | it++; |
| 300 | |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 301 | if (download->GetSafetyState() == DownloadItem::DANGEROUS && |
[email protected] | 4883796 | 2011-04-19 17:03:29 | [diff] [blame] | 302 | download->IsPartialDownload()) { |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 303 | // The user hasn't accepted it, so we need to remove it |
| 304 | // from the disk. This may or may not result in it being |
| 305 | // removed from the DownloadManager queues and deleted |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 306 | // (specifically, DownloadManager::DownloadRemoved only |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 307 | // removes and deletes it if it's known to the history service) |
| 308 | // so the only thing we know after calling this function is that |
| 309 | // the download was deleted if-and-only-if it was removed |
| 310 | // from all queues. |
[email protected] | 30307700 | 2011-04-19 23:21:01 | [diff] [blame] | 311 | download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 312 | } else if (download->IsPartialDownload()) { |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 313 | download->Cancel(false); |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 314 | if (delegate_) |
| 315 | delegate_->UpdateItemInPersistentStore(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 319 | // At this point, all dangerous downloads have had their files removed |
| 320 | // and all in progress downloads have been cancelled. We can now delete |
| 321 | // anything left. |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 322 | |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 323 | active_downloads_.clear(); |
[email protected] | 7e834f0 | 2012-08-09 20:38:56 | [diff] [blame] | 324 | STLDeleteValues(&downloads_); |
| 325 | downloads_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 326 | |
[email protected] | 41f558fb | 2012-01-09 22:59:58 | [diff] [blame] | 327 | // We'll have nothing more to report to the observers after this point. |
| 328 | observers_.Clear(); |
| 329 | |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 330 | file_manager_ = NULL; |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 331 | if (delegate_) |
| 332 | delegate_->Shutdown(); |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 333 | delegate_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 334 | } |
| 335 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 336 | void DownloadManagerImpl::GetTemporaryDownloads( |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 337 | const FilePath& dir_path, DownloadVector* result) { |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 338 | DCHECK(result); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 339 | |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 340 | for (DownloadMap::iterator it = downloads_.begin(); |
| 341 | it != downloads_.end(); ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 342 | DownloadItemImpl* item = it->second; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 343 | // TODO(benjhayden): Don't check IsPersisted(). |
| 344 | if (item->IsTemporary() && |
| 345 | item->IsPersisted() && |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 346 | (dir_path.empty() || |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 347 | item->GetTargetFilePath().DirName() == dir_path)) |
| 348 | result->push_back(item); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 349 | } |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 350 | } |
| 351 | |
[email protected] | 55b174fd | 2012-09-07 22:02:29 | [diff] [blame] | 352 | void DownloadManagerImpl::GetAllDownloads( |
| 353 | const FilePath& dir_path, DownloadVector* result) { |
| 354 | DCHECK(result); |
| 355 | |
| 356 | for (DownloadMap::iterator it = downloads_.begin(); |
| 357 | it != downloads_.end(); ++it) { |
| 358 | DownloadItemImpl* item = it->second; |
| 359 | // TODO(benjhayden): Don't check IsPersisted(). |
| 360 | if (!item->IsTemporary() && |
| 361 | item->IsPersisted() && |
| 362 | (dir_path.empty() || |
| 363 | item->GetTargetFilePath().DirName() == dir_path)) |
| 364 | result->push_back(item); |
| 365 | } |
| 366 | } |
| 367 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 368 | void DownloadManagerImpl::SearchDownloads(const string16& query, |
| 369 | DownloadVector* result) { |
[email protected] | 503d0387 | 2011-05-06 08:36:26 | [diff] [blame] | 370 | string16 query_lower(base::i18n::ToLower(query)); |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 371 | |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 372 | for (DownloadMap::iterator it = downloads_.begin(); |
| 373 | it != downloads_.end(); ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 374 | DownloadItemImpl* download_item = it->second; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 375 | // TODO(benjhayden): Don't check IsPersisted(). |
| 376 | if (!download_item->IsTemporary() && |
| 377 | download_item->IsPersisted() && |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 378 | download_item->MatchesQuery(query_lower)) { |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 379 | result->push_back(download_item); |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 380 | } |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 381 | } |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 382 | } |
| 383 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 384 | bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { |
[email protected] | 6d0c9fb | 2011-08-22 19:26:03 | [diff] [blame] | 385 | DCHECK(browser_context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 386 | DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 387 | shutdown_needed_ = true; |
| 388 | |
[email protected] | 6d0c9fb | 2011-08-22 19:26:03 | [diff] [blame] | 389 | browser_context_ = browser_context; |
[email protected] | 024f2f0 | 2010-04-30 22:51:46 | [diff] [blame] | 390 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 391 | return true; |
| 392 | } |
| 393 | |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 394 | // We have received a message from DownloadFileManager about a new download. |
[email protected] | 2bddd207 | 2012-06-18 16:16:51 | [diff] [blame] | 395 | content::DownloadId DownloadManagerImpl::StartDownload( |
| 396 | scoped_ptr<DownloadCreateInfo> info, |
| 397 | scoped_ptr<content::ByteStreamReader> stream) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 398 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 399 | |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 400 | // |bound_net_log| will be used for logging both the download item's and |
| 401 | // the download file's events. |
| 402 | net::BoundNetLog bound_net_log = CreateDownloadItem(info.get()); |
[email protected] | 2bddd207 | 2012-06-18 16:16:51 | [diff] [blame] | 403 | |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 404 | // If info->download_id was unknown on entry to this function, it was |
| 405 | // assigned in CreateDownloadItem. |
| 406 | DownloadId download_id = info->download_id; |
[email protected] | 2bddd207 | 2012-06-18 16:16:51 | [diff] [blame] | 407 | |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 408 | DownloadFileManager::CreateDownloadFileCallback callback( |
| 409 | base::Bind(&DownloadManagerImpl::OnDownloadFileCreated, |
| 410 | this, download_id.local())); |
| 411 | |
| 412 | BrowserThread::PostTask( |
| 413 | BrowserThread::FILE, FROM_HERE, |
| 414 | base::Bind(&DownloadFileManager::CreateDownloadFile, |
| 415 | file_manager_, base::Passed(info.Pass()), |
| 416 | base::Passed(stream.Pass()), |
| 417 | make_scoped_refptr(this), |
| 418 | GenerateFileHash(), bound_net_log, |
| 419 | callback)); |
| 420 | |
| 421 | return download_id; |
| 422 | } |
| 423 | |
| 424 | void DownloadManagerImpl::OnDownloadFileCreated( |
| 425 | int32 download_id, content::DownloadInterruptReason reason) { |
| 426 | if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 427 | OnDownloadInterrupted(download_id, reason); |
| 428 | // TODO(rdsmith): It makes no sense to continue along the |
| 429 | // regular download path after we've gotten an error. But it's |
| 430 | // the way the code has historically worked, and this allows us |
| 431 | // to get the download persisted and observers of the download manager |
| 432 | // notified, so tests work. When we execute all side effects of cancel |
| 433 | // (including queue removal) immedately rather than waiting for |
| 434 | // persistence we should replace this comment with a "return;". |
| 435 | } |
| 436 | |
| 437 | DownloadMap::iterator download_iter = active_downloads_.find(download_id); |
| 438 | if (download_iter == active_downloads_.end()) |
| 439 | return; |
| 440 | |
| 441 | DownloadItemImpl* download = download_iter->second; |
| 442 | content::DownloadTargetCallback callback = |
| 443 | base::Bind(&DownloadManagerImpl::OnDownloadTargetDetermined, |
| 444 | this, download_id); |
| 445 | if (!delegate_ || !delegate_->DetermineDownloadTarget(download, callback)) { |
| 446 | FilePath target_path = download->GetForcedFilePath(); |
| 447 | // TODO(asanka): Determine a useful path if |target_path| is empty. |
| 448 | callback.Run(target_path, |
| 449 | DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 450 | content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 451 | target_path); |
| 452 | } |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | void DownloadManagerImpl::OnDownloadTargetDetermined( |
| 456 | int32 download_id, |
| 457 | const FilePath& target_path, |
| 458 | DownloadItem::TargetDisposition disposition, |
| 459 | content::DownloadDangerType danger_type, |
| 460 | const FilePath& intermediate_path) { |
| 461 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 462 | DownloadMap::iterator download_iter = active_downloads_.find(download_id); |
| 463 | if (download_iter != active_downloads_.end()) { |
| 464 | // Once DownloadItem::OnDownloadTargetDetermined() is called, we expect a |
| 465 | // DownloadRenamedToIntermediateName() callback. This is necessary for the |
| 466 | // download to proceed. |
| 467 | download_iter->second->OnDownloadTargetDetermined( |
| 468 | target_path, disposition, danger_type, intermediate_path); |
| 469 | } |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 470 | } |
| 471 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 472 | void DownloadManagerImpl::CheckForHistoryFilesRemoval() { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 473 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 474 | for (DownloadMap::iterator it = downloads_.begin(); |
| 475 | it != downloads_.end(); ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 476 | DownloadItemImpl* item = it->second; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 477 | if (item->IsPersisted()) |
| 478 | CheckForFileRemoval(item); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 482 | void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 483 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 484 | if (download_item->IsComplete() && |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 485 | !download_item->GetFileExternallyRemoved()) { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 486 | BrowserThread::PostTask( |
| 487 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 488 | base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread, |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 489 | this, download_item->GetId(), |
[email protected] | fabf36d2 | 2011-10-28 21:50:17 | [diff] [blame] | 490 | download_item->GetTargetFilePath())); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 494 | void DownloadManagerImpl::CheckForFileRemovalOnFileThread( |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 495 | int32 download_id, const FilePath& path) { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 496 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 497 | if (!file_util::PathExists(path)) { |
| 498 | BrowserThread::PostTask( |
| 499 | BrowserThread::UI, FROM_HERE, |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 500 | base::Bind(&DownloadManagerImpl::OnFileRemovalDetected, |
| 501 | this, |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 502 | download_id)); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 506 | void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 507 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 508 | if (ContainsKey(downloads_, download_id)) |
| 509 | downloads_[download_id]->OnDownloadedFileRemoved(); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 510 | } |
| 511 | |
[email protected] | 37757c6 | 2011-12-20 20:07:12 | [diff] [blame] | 512 | content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 513 | return browser_context_; |
| 514 | } |
| 515 | |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 516 | net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( |
| 517 | DownloadCreateInfo* info) { |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 518 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 519 | |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 520 | net::BoundNetLog bound_net_log = |
| 521 | net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 522 | if (!info->download_id.IsValid()) |
| 523 | info->download_id = GetNextId(); |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 524 | DownloadItemImpl* download = factory_->CreateActiveItem( |
[email protected] | 2bddd207 | 2012-06-18 16:16:51 | [diff] [blame] | 525 | this, *info, |
| 526 | scoped_ptr<DownloadRequestHandleInterface>( |
| 527 | new DownloadRequestHandle(info->request_handle)).Pass(), |
[email protected] | 66521d0 | 2012-08-14 17:51:54 | [diff] [blame] | 528 | bound_net_log); |
[email protected] | d8472d9 | 2011-08-26 20:15:20 | [diff] [blame] | 529 | |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 530 | DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 531 | downloads_[download->GetId()] = download; |
| 532 | DCHECK(!ContainsKey(active_downloads_, download->GetId())); |
| 533 | active_downloads_[download->GetId()] = download; |
[email protected] | 6a1a59a1 | 2012-07-26 17:26:40 | [diff] [blame] | 534 | FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 535 | |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 536 | return bound_net_log; |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 537 | } |
| 538 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 539 | DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 540 | const FilePath& main_file_path, |
| 541 | const GURL& page_url, |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 542 | const std::string& mime_type, |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 543 | DownloadItem::Observer* observer) { |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 544 | net::BoundNetLog bound_net_log = |
| 545 | net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 546 | DownloadItemImpl* download = factory_->CreateSavePageItem( |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 547 | this, |
| 548 | main_file_path, |
| 549 | page_url, |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 550 | GetNextId(), |
| 551 | mime_type, |
| 552 | bound_net_log); |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 553 | |
| 554 | download->AddObserver(observer); |
| 555 | |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 556 | DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 557 | downloads_[download->GetId()] = download; |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 558 | DCHECK(!SavePageData::Get(download)); |
| 559 | new SavePageData(download); |
| 560 | DCHECK(SavePageData::Get(download)); |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 561 | |
[email protected] | 8a5ccda | 2012-08-22 23:55:41 | [diff] [blame] | 562 | FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
[email protected] | 6a1a59a1 | 2012-07-26 17:26:40 | [diff] [blame] | 563 | |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 564 | // Will notify the observer in the callback. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 565 | if (delegate_) |
| 566 | delegate_->AddItemToPersistentStore(download); |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 567 | |
| 568 | return download; |
| 569 | } |
| 570 | |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 571 | void DownloadManagerImpl::UpdateDownload(int32 download_id, |
| 572 | int64 bytes_so_far, |
| 573 | int64 bytes_per_sec, |
| 574 | const std::string& hash_state) { |
| 575 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 576 | DownloadMap::iterator it = active_downloads_.find(download_id); |
| 577 | if (it != active_downloads_.end()) { |
| 578 | DownloadItemImpl* download = it->second; |
| 579 | if (download->IsInProgress()) { |
| 580 | download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state); |
| 581 | if (delegate_) |
| 582 | delegate_->UpdateItemInPersistentStore(download); |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | void DownloadManagerImpl::OnResponseCompleted(int32 download_id, |
| 588 | int64 size, |
| 589 | const std::string& hash) { |
| 590 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 591 | VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
| 592 | << " size = " << size; |
| 593 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 594 | |
| 595 | // If it's not in active_downloads_, that means it was cancelled; just |
| 596 | // ignore the notification. |
| 597 | if (active_downloads_.count(download_id) == 0) |
| 598 | return; |
| 599 | |
| 600 | DownloadItemImpl* download = active_downloads_[download_id]; |
| 601 | download->OnAllDataSaved(size, hash); |
| 602 | MaybeCompleteDownload(download); |
| 603 | } |
| 604 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 605 | void DownloadManagerImpl::AssertStateConsistent( |
| 606 | DownloadItemImpl* download) const { |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 607 | CHECK(ContainsKey(downloads_, download->GetId())); |
[email protected] | 7d41311 | 2011-06-16 18:50:17 | [diff] [blame] | 608 | |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 609 | int64 state = download->GetState(); |
[email protected] | 61b75a5 | 2011-08-29 16:34:34 | [diff] [blame] | 610 | base::debug::Alias(&state); |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 611 | if (ContainsKey(active_downloads_, download->GetId())) { |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 612 | if (download->IsPersisted()) |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 613 | CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| 614 | if (DownloadItem::IN_PROGRESS != download->GetState()) |
| 615 | CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle()); |
[email protected] | f9a2997f | 2011-09-23 16:54:07 | [diff] [blame] | 616 | } |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 617 | if (DownloadItem::IN_PROGRESS == download->GetState()) |
| 618 | CHECK(ContainsKey(active_downloads_, download->GetId())); |
[email protected] | 5cd11b6e | 2011-06-10 20:30:59 | [diff] [blame] | 619 | } |
| 620 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 621 | bool DownloadManagerImpl::IsDownloadReadyForCompletion( |
| 622 | DownloadItemImpl* download) { |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 623 | // If we don't have all the data, the download is not ready for |
| 624 | // completion. |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 625 | if (!download->AllDataSaved()) |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 626 | return false; |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 627 | |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame] | 628 | // If the download is dangerous, but not yet validated, it's not ready for |
| 629 | // completion. |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 630 | if (download->GetSafetyState() == DownloadItem::DANGEROUS) |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame] | 631 | return false; |
| 632 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 633 | // If the download isn't active (e.g. has been cancelled) it's not |
| 634 | // ready for completion. |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 635 | if (active_downloads_.count(download->GetId()) == 0) |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 636 | return false; |
| 637 | |
| 638 | // If the download hasn't been inserted into the history system |
| 639 | // (which occurs strictly after file name determination, intermediate |
| 640 | // file rename, and UI display) then it's not ready for completion. |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 641 | if (!download->IsPersisted()) |
[email protected] | 7054b59 | 2011-06-22 14:46:42 | [diff] [blame] | 642 | return false; |
| 643 | |
| 644 | return true; |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 645 | } |
| 646 | |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 647 | // When SavePackage downloads MHTML to GData (see |
| 648 | // SavePackageFilePickerChromeOS), GData calls MaybeCompleteDownload() like it |
| 649 | // does for non-SavePackage downloads, but SavePackage downloads never satisfy |
| 650 | // IsDownloadReadyForCompletion(). GDataDownloadObserver manually calls |
| 651 | // DownloadItem::UpdateObservers() when the upload completes so that SavePackage |
| 652 | // notices that the upload has completed and runs its normal Finish() pathway. |
| 653 | // MaybeCompleteDownload() is never the mechanism by which SavePackage completes |
| 654 | // downloads. SavePackage always uses its own Finish() to mark downloads |
| 655 | // complete. |
| 656 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 657 | void DownloadManagerImpl::MaybeCompleteDownload( |
| 658 | DownloadItemImpl* download) { |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 659 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 660 | VLOG(20) << __FUNCTION__ << "()" << " download = " |
| 661 | << download->DebugString(false); |
| 662 | |
| 663 | if (!IsDownloadReadyForCompletion(download)) |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 664 | return; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 665 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 666 | // TODO(rdsmith): DCHECK that we only pass through this point |
| 667 | // once per download. The natural way to do this is by a state |
| 668 | // transition on the DownloadItem. |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 669 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 670 | // Confirm we're in the proper set of states to be here; |
[email protected] | 550520f | 2012-05-14 20:58:48 | [diff] [blame] | 671 | // have all data, have a history handle, (validated or safe). |
| 672 | DCHECK(download->IsInProgress()); |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 673 | DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState()); |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 674 | DCHECK(download->AllDataSaved()); |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 675 | DCHECK(download->IsPersisted()); |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 676 | |
[email protected] | da4cd426 | 2012-05-18 20:42:35 | [diff] [blame] | 677 | // Give the delegate a chance to override. It's ok to keep re-setting the |
| 678 | // delegate's |complete_callback| cb as long as there isn't another call-point |
| 679 | // trying to set it to a different cb. TODO(benjhayden): Change the callback |
| 680 | // to point directly to the item instead of |this| when DownloadItem supports |
| 681 | // weak-ptrs. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 682 | if (delegate_ && !delegate_->ShouldCompleteDownload(download, base::Bind( |
[email protected] | da4cd426 | 2012-05-18 20:42:35 | [diff] [blame] | 683 | &DownloadManagerImpl::MaybeCompleteDownloadById, |
| 684 | this, download->GetId()))) |
[email protected] | c291865 | 2011-11-01 18:50:23 | [diff] [blame] | 685 | return; |
| 686 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 687 | VLOG(20) << __FUNCTION__ << "()" << " executing: download = " |
| 688 | << download->DebugString(false); |
| 689 | |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 690 | if (delegate_) |
| 691 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 692 | download->OnDownloadCompleting(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 693 | } |
| 694 | |
[email protected] | da4cd426 | 2012-05-18 20:42:35 | [diff] [blame] | 695 | void DownloadManagerImpl::MaybeCompleteDownloadById(int download_id) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 696 | if (ContainsKey(active_downloads_, download_id)) |
| 697 | MaybeCompleteDownload(active_downloads_[download_id]); |
[email protected] | da4cd426 | 2012-05-18 20:42:35 | [diff] [blame] | 698 | } |
| 699 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 700 | void DownloadManagerImpl::DownloadCompleted(DownloadItemImpl* download) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 701 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | cc3c7c09 | 2011-05-09 18:40:21 | [diff] [blame] | 702 | DCHECK(download); |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 703 | if (delegate_) |
| 704 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 705 | active_downloads_.erase(download->GetId()); |
| 706 | AssertStateConsistent(download); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 707 | } |
| 708 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 709 | void DownloadManagerImpl::CancelDownload(int32 download_id) { |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 710 | // A cancel at the right time could remove the download from the |
| 711 | // |active_downloads_| map before we get here. |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 712 | if (ContainsKey(active_downloads_, download_id)) |
| 713 | active_downloads_[download_id]->Cancel(true); |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 714 | } |
| 715 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 716 | void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { |
[email protected] | d8472d9 | 2011-08-26 20:15:20 | [diff] [blame] | 717 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | d8472d9 | 2011-08-26 20:15:20 | [diff] [blame] | 718 | |
| 719 | VLOG(20) << __FUNCTION__ << "()" |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 720 | << " download = " << download->DebugString(true); |
| 721 | |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 722 | RemoveFromActiveList(download); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 723 | // This function is called from the DownloadItem, so DI state |
| 724 | // should already have been updated. |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 725 | AssertStateConsistent(download); |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 726 | |
| 727 | DCHECK(file_manager_); |
| 728 | download->OffThreadCancel(); |
| 729 | } |
| 730 | |
| 731 | void DownloadManagerImpl::OnDownloadInterrupted( |
| 732 | int32 download_id, |
| 733 | content::DownloadInterruptReason reason) { |
| 734 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 735 | |
| 736 | if (!ContainsKey(active_downloads_, download_id)) |
| 737 | return; |
| 738 | active_downloads_[download_id]->Interrupt(reason); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 739 | } |
| 740 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 741 | void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 742 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 743 | DCHECK(download); |
| 744 | |
| 745 | // Clean up will happen when the history system create callback runs if we |
| 746 | // don't have a valid db_handle yet. |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 747 | if (download->IsPersisted()) { |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 748 | active_downloads_.erase(download->GetId()); |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 749 | if (delegate_) |
| 750 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
[email protected] | fd3a8283 | 2012-01-19 20:35:12 | [diff] [blame] | 754 | bool DownloadManagerImpl::GenerateFileHash() { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 755 | return delegate_ && delegate_->GenerateFileHash(); |
[email protected] | fd3a8283 | 2012-01-19 20:35:12 | [diff] [blame] | 756 | } |
| 757 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 758 | int DownloadManagerImpl::RemoveDownloadItems( |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 759 | const DownloadItemImplVector& pending_deletes) { |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 760 | if (pending_deletes.empty()) |
| 761 | return 0; |
| 762 | |
| 763 | // Delete from internal maps. |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 764 | for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); |
[email protected] | 7e834f0 | 2012-08-09 20:38:56 | [diff] [blame] | 765 | it != pending_deletes.end(); |
| 766 | ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 767 | DownloadItemImpl* download = *it; |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 768 | DCHECK(download); |
[email protected] | 7e834f0 | 2012-08-09 20:38:56 | [diff] [blame] | 769 | int32 download_id = download->GetId(); |
| 770 | delete download; |
| 771 | downloads_.erase(download_id); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 772 | } |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 773 | NotifyModelChanged(); |
[email protected] | 7e834f0 | 2012-08-09 20:38:56 | [diff] [blame] | 774 | return static_cast<int>(pending_deletes.size()); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 775 | } |
| 776 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 777 | void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 778 | if (!download || |
| 779 | downloads_.find(download->GetId()) == downloads_.end()) |
| 780 | return; |
| 781 | |
| 782 | // TODO(benjhayden,rdsmith): Remove this. |
| 783 | if (!download->IsPersisted()) |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 784 | return; |
| 785 | |
| 786 | // Make history update. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 787 | if (delegate_) |
| 788 | delegate_->RemoveItemFromPersistentStore(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 789 | |
| 790 | // Remove from our tables and delete. |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 791 | int downloads_count = |
| 792 | RemoveDownloadItems(DownloadItemImplVector(1, download)); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 793 | DCHECK_EQ(1, downloads_count); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 794 | } |
| 795 | |
[email protected] | fd3a8283 | 2012-01-19 20:35:12 | [diff] [blame] | 796 | int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, |
| 797 | base::Time remove_end) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 798 | if (delegate_) |
| 799 | delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 800 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 801 | DownloadItemImplVector pending_deletes; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 802 | for (DownloadMap::const_iterator it = downloads_.begin(); |
| 803 | it != downloads_.end(); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 804 | ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 805 | DownloadItemImpl* download = it->second; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 806 | if (download->IsPersisted() && |
| 807 | download->GetStartTime() >= remove_begin && |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 808 | (remove_end.is_null() || download->GetStartTime() < remove_end) && |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 809 | (download->IsComplete() || download->IsCancelled())) { |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 810 | AssertStateConsistent(download); |
[email protected] | 7e834f0 | 2012-08-09 20:38:56 | [diff] [blame] | 811 | download->NotifyRemoved(); |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 812 | pending_deletes.push_back(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 813 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 814 | } |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 815 | return RemoveDownloadItems(pending_deletes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 816 | } |
| 817 | |
[email protected] | fd3a8283 | 2012-01-19 20:35:12 | [diff] [blame] | 818 | int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) { |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 819 | return RemoveDownloadsBetween(remove_begin, base::Time()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 820 | } |
| 821 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 822 | int DownloadManagerImpl::RemoveAllDownloads() { |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 823 | // The null times make the date range unbounded. |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 824 | int num_deleted = RemoveDownloadsBetween(base::Time(), base::Time()); |
| 825 | download_stats::RecordClearAllSize(num_deleted); |
| 826 | return num_deleted; |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 827 | } |
| 828 | |
[email protected] | 0d4e30c | 2012-01-28 00:47:53 | [diff] [blame] | 829 | void DownloadManagerImpl::DownloadUrl( |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 830 | scoped_ptr<content::DownloadUrlParameters> params) { |
| 831 | if (params->post_id() >= 0) { |
| 832 | // Check this here so that the traceback is more useful. |
| 833 | DCHECK(params->prefer_cache()); |
| 834 | DCHECK(params->method() == "POST"); |
| 835 | } |
| 836 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
| 837 | &BeginDownload, base::Owned(params.release()))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 838 | } |
| 839 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 840 | void DownloadManagerImpl::AddObserver(Observer* observer) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 841 | observers_.AddObserver(observer); |
[email protected] | a1e41e7 | 2012-02-22 17:41:25 | [diff] [blame] | 842 | // TODO: It is the responsibility of the observers to query the |
| 843 | // DownloadManager. Remove the following call from here and update all |
| 844 | // observers. |
[email protected] | 75e51b5 | 2012-02-04 16:57:54 | [diff] [blame] | 845 | observer->ModelChanged(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 846 | } |
| 847 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 848 | void DownloadManagerImpl::RemoveObserver(Observer* observer) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 849 | observers_.RemoveObserver(observer); |
| 850 | } |
| 851 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 852 | // Operations posted to us from the history service ---------------------------- |
| 853 | |
| 854 | // The history service has retrieved all download entries. 'entries' contains |
[email protected] | bb1a421 | 2011-08-22 22:38:25 | [diff] [blame] | 855 | // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 856 | void DownloadManagerImpl::OnPersistentStoreQueryComplete( |
[email protected] | bb1a421 | 2011-08-22 22:38:25 | [diff] [blame] | 857 | std::vector<DownloadPersistentStoreInfo>* entries) { |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 858 | history_size_ = entries->size(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 859 | for (size_t i = 0; i < entries->size(); ++i) { |
[email protected] | deb4083 | 2012-02-23 15:41:37 | [diff] [blame] | 860 | int64 db_handle = entries->at(i).db_handle; |
| 861 | base::debug::Alias(&db_handle); |
[email protected] | deb4083 | 2012-02-23 15:41:37 | [diff] [blame] | 862 | |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 863 | net::BoundNetLog bound_net_log = |
| 864 | net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 865 | DownloadItemImpl* download = factory_->CreatePersistedItem( |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 866 | this, GetNextId(), entries->at(i), bound_net_log); |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 867 | DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 868 | downloads_[download->GetId()] = download; |
[email protected] | 6a1a59a1 | 2012-07-26 17:26:40 | [diff] [blame] | 869 | FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 870 | VLOG(20) << __FUNCTION__ << "()" << i << ">" |
| 871 | << " download = " << download->DebugString(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 872 | } |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 873 | NotifyModelChanged(); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 874 | CheckForHistoryFilesRemoval(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 875 | } |
| 876 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 877 | void DownloadManagerImpl::AddDownloadItemToHistory(DownloadItemImpl* download, |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 878 | int64 db_handle) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 879 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 0634626a | 2012-05-03 19:04:26 | [diff] [blame] | 880 | DCHECK_NE(DownloadItem::kUninitializedHandle, db_handle); |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 881 | DCHECK(!download->IsPersisted()); |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 882 | download->SetDbHandle(db_handle); |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 883 | download->SetIsPersisted(); |
[email protected] | 5bcd73eb | 2011-03-23 21:14:02 | [diff] [blame] | 884 | |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 885 | download_stats::RecordHistorySize(history_size_); |
| 886 | // Not counting |download|. |
| 887 | ++history_size_; |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 888 | |
| 889 | // Show in the appropriate browser UI. |
| 890 | // This includes buttons to save or cancel, for a dangerous download. |
| 891 | ShowDownloadInBrowser(download); |
| 892 | |
| 893 | // Inform interested objects about the new download. |
| 894 | NotifyModelChanged(); |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 895 | } |
| 896 | |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 897 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 898 | void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, |
| 899 | int64 db_handle) { |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 900 | // It's valid that we don't find a matching item, i.e. on shutdown. |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 901 | if (!ContainsKey(downloads_, download_id)) |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 902 | return; |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 903 | |
| 904 | DownloadItemImpl* item = downloads_[download_id]; |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 905 | AddDownloadItemToHistory(item, db_handle); |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 906 | if (SavePageData::Get(item)) { |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 907 | OnSavePageItemAddedToPersistentStore(item); |
| 908 | } else { |
| 909 | OnDownloadItemAddedToPersistentStore(item); |
| 910 | } |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 911 | } |
| 912 | |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 913 | // Once the new DownloadItem has been committed to the persistent store, |
| 914 | // associate it with its db_handle (TODO(benjhayden) merge db_handle with id), |
| 915 | // show it in the browser (TODO(benjhayden) the ui should observe us instead), |
| 916 | // and notify observers (TODO(benjhayden) observers should be able to see the |
| 917 | // item when it's created so they can observe it directly. Are there any |
| 918 | // clients that actually need to know when the item is added to the history?). |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 919 | void DownloadManagerImpl::OnDownloadItemAddedToPersistentStore( |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 920 | DownloadItemImpl* item) { |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 921 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 922 | VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << item->GetDbHandle() |
| 923 | << " download_id = " << item->GetId() |
| 924 | << " download = " << item->DebugString(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 925 | |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 926 | // If the download is still in progress, try to complete it. |
| 927 | // |
| 928 | // Otherwise, download has been cancelled or interrupted before we've |
| 929 | // received the DB handle. We post one final message to the history |
| 930 | // service so that it can be properly in sync with the DownloadItem's |
| 931 | // completion status, and also inform any observers so that they get |
| 932 | // more than just the start notification. |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 933 | if (item->IsInProgress()) { |
| 934 | MaybeCompleteDownload(item); |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 935 | } else { |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 936 | DCHECK(item->IsCancelled()); |
| 937 | active_downloads_.erase(item->GetId()); |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 938 | if (delegate_) |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 939 | delegate_->UpdateItemInPersistentStore(item); |
| 940 | item->UpdateObservers(); |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 941 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 942 | } |
| 943 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 944 | void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItemImpl* download) { |
[email protected] | a29e4f2 | 2012-04-12 21:22:03 | [diff] [blame] | 945 | // The 'contents' may no longer exist if the user closed the contents before |
| 946 | // we get this start completion event. |
[email protected] | a62d4290 | 2012-01-24 17:24:38 | [diff] [blame] | 947 | WebContents* content = download->GetWebContents(); |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 948 | |
| 949 | // If the contents no longer exists, we ask the embedder to suggest another |
[email protected] | a29e4f2 | 2012-04-12 21:22:03 | [diff] [blame] | 950 | // contents. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 951 | if (!content && delegate_) |
[email protected] | ef9572e | 2012-01-04 22:14:12 | [diff] [blame] | 952 | content = delegate_->GetAlternativeWebContentsToNotifyForDownload(); |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 953 | |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 954 | if (content && content->GetDelegate()) |
| 955 | content->GetDelegate()->OnStartDownload(content, download); |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 956 | } |
| 957 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 958 | int DownloadManagerImpl::InProgressCount() const { |
[email protected] | 550520f | 2012-05-14 20:58:48 | [diff] [blame] | 959 | // Don't use active_downloads_.count() because Cancel() leaves items in |
| 960 | // active_downloads_ if they haven't made it into the persistent store yet. |
[email protected] | 007e741 | 2012-03-13 20:10:56 | [diff] [blame] | 961 | // Need to actually look at each item's state. |
| 962 | int count = 0; |
[email protected] | 550520f | 2012-05-14 20:58:48 | [diff] [blame] | 963 | for (DownloadMap::const_iterator it = active_downloads_.begin(); |
| 964 | it != active_downloads_.end(); ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 965 | DownloadItemImpl* item = it->second; |
[email protected] | 007e741 | 2012-03-13 20:10:56 | [diff] [blame] | 966 | if (item->IsInProgress()) |
| 967 | ++count; |
| 968 | } |
| 969 | return count; |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 970 | } |
| 971 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 972 | void DownloadManagerImpl::NotifyModelChanged() { |
[email protected] | 75e51b5 | 2012-02-04 16:57:54 | [diff] [blame] | 973 | FOR_EACH_OBSERVER(Observer, observers_, ModelChanged(this)); |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 974 | } |
| 975 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 976 | DownloadItem* DownloadManagerImpl::GetDownloadItem(int download_id) { |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 977 | DownloadItem* download = GetDownload(download_id); |
| 978 | return (download && download->IsPersisted()) ? download : NULL; |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 979 | } |
| 980 | |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 981 | DownloadItem* DownloadManagerImpl::GetDownload(int download_id) { |
| 982 | return ContainsKey(downloads_, download_id) ? downloads_[download_id] : NULL; |
| 983 | } |
| 984 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 985 | DownloadItem* DownloadManagerImpl::GetActiveDownloadItem(int download_id) { |
[email protected] | 5d3e8364 | 2011-12-16 01:14:36 | [diff] [blame] | 986 | if (ContainsKey(active_downloads_, download_id)) |
| 987 | return active_downloads_[download_id]; |
| 988 | return NULL; |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 989 | } |
| 990 | |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 991 | // Confirm that everything in all maps is also in |downloads_|, and that |
| 992 | // everything in |downloads_| is also in some other map. |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 993 | void DownloadManagerImpl::AssertContainersConsistent() const { |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 994 | #if !defined(NDEBUG) |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 995 | // Turn everything into sets. |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 996 | const DownloadMap* input_maps[] = {&active_downloads_}; |
| 997 | DownloadSet active_set; |
| 998 | DownloadSet* all_sets[] = {&active_set}; |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 999 | DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(all_sets)); |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1000 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) { |
| 1001 | for (DownloadMap::const_iterator it = input_maps[i]->begin(); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1002 | it != input_maps[i]->end(); ++it) { |
| 1003 | all_sets[i]->insert(&*it->second); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 1004 | } |
| 1005 | } |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1006 | |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 1007 | DownloadSet all_downloads; |
| 1008 | for (DownloadMap::const_iterator it = downloads_.begin(); |
| 1009 | it != downloads_.end(); ++it) { |
| 1010 | all_downloads.insert(it->second); |
| 1011 | } |
| 1012 | |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1013 | // Check if each set is fully present in downloads, and create a union. |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1014 | for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) { |
| 1015 | DownloadSet remainder; |
| 1016 | std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin()); |
| 1017 | std::set_difference(all_sets[i]->begin(), all_sets[i]->end(), |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 1018 | all_downloads.begin(), all_downloads.end(), |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1019 | insert_it); |
| 1020 | DCHECK(remainder.empty()); |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1021 | } |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 1022 | #endif |
| 1023 | } |
| 1024 | |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1025 | // SavePackage will call SavePageDownloadFinished upon completion/cancellation. |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 1026 | // The history callback will call OnSavePageItemAddedToPersistentStore. |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1027 | // If the download finishes before the history callback, |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 1028 | // OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring |
| 1029 | // that the history event is update regardless of the order in which these two |
| 1030 | // events complete. |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1031 | // If something removes the download item from the download manager (Remove, |
| 1032 | // Shutdown) the result will be that the SavePage system will not be able to |
| 1033 | // properly update the download item (which no longer exists) or the download |
| 1034 | // history, but the action will complete properly anyway. This may lead to the |
| 1035 | // history entry being wrong on a reload of chrome (specifically in the case of |
| 1036 | // Initiation -> History Callback -> Removal -> Completion), but there's no way |
| 1037 | // to solve that without canceling on Remove (which would then update the DB). |
| 1038 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 1039 | void DownloadManagerImpl::OnSavePageItemAddedToPersistentStore( |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 1040 | DownloadItemImpl* item) { |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1041 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1042 | |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1043 | // Finalize this download if it finished before the history callback. |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 1044 | if (!item->IsInProgress()) |
| 1045 | SavePageDownloadFinished(item); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1046 | } |
| 1047 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 1048 | void DownloadManagerImpl::SavePageDownloadFinished( |
| 1049 | content::DownloadItem* download) { |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 1050 | if (download->IsPersisted()) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 1051 | if (delegate_) |
| 1052 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 1053 | if (download->IsComplete()) |
| 1054 | content::NotificationService::current()->Notify( |
| 1055 | content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
| 1056 | content::Source<DownloadManager>(this), |
| 1057 | content::Details<DownloadItem>(download)); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1058 | } |
| 1059 | } |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 1060 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 1061 | void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 1062 | if (delegate_) |
| 1063 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 1064 | int num_unopened = 0; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 1065 | for (DownloadMap::iterator it = downloads_.begin(); |
| 1066 | it != downloads_.end(); ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 1067 | DownloadItemImpl* item = it->second; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 1068 | if (item->IsComplete() && |
| 1069 | !item->GetOpened()) |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 1070 | ++num_unopened; |
| 1071 | } |
| 1072 | download_stats::RecordOpensOutstanding(num_unopened); |
| 1073 | } |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 1074 | |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 1075 | void DownloadManagerImpl::DownloadRenamedToIntermediateName( |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 1076 | DownloadItemImpl* download) { |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 1077 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 1078 | // download->GetFullPath() is only expected to be meaningful after this |
| 1079 | // callback is received. Therefore we can now add the download to a persistent |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 1080 | // store. If the rename failed, we receive an OnDownloadInterrupted() call |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 1081 | // before we receive the DownloadRenamedToIntermediateName() call. |
| 1082 | if (delegate_) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 1083 | delegate_->AddItemToPersistentStore(download); |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 1084 | } else { |
| 1085 | OnItemAddedToPersistentStore(download->GetId(), |
| 1086 | DownloadItem::kUninitializedHandle); |
| 1087 | } |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | void DownloadManagerImpl::DownloadRenamedToFinalName( |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 1091 | DownloadItemImpl* download) { |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 1092 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 1093 | // If the rename failed, we receive an OnDownloadInterrupted() call before we |
| 1094 | // receive the DownloadRenamedToFinalName() call. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 1095 | if (delegate_) { |
| 1096 | delegate_->UpdatePathForItemInPersistentStore( |
| 1097 | download, download->GetFullPath()); |
| 1098 | } |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 1099 | } |