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

Side by Side Diff: content/browser/download/download_item_impl.cc

Issue 8697006: DownloadManager intereface refactoring to allow cleaner DownloadItem unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added CONTENT_EXPORT to delegate. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "content/browser/download/download_item_impl.h" 5 #include "content/browser/download/download_item_impl.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/format_macros.h" 12 #include "base/format_macros.h"
13 #include "base/i18n/case_conversion.h" 13 #include "base/i18n/case_conversion.h"
14 #include "base/i18n/string_search.h" 14 #include "base/i18n/string_search.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/metrics/histogram.h" 16 #include "base/metrics/histogram.h"
17 #include "base/stringprintf.h" 17 #include "base/stringprintf.h"
18 #include "base/utf_string_conversions.h" 18 #include "base/utf_string_conversions.h"
19 #include "content/browser/download/download_create_info.h" 19 #include "content/browser/download/download_create_info.h"
20 #include "content/browser/download/download_file.h" 20 #include "content/browser/download/download_file.h"
21 #include "content/browser/download/download_file_manager.h" 21 #include "content/browser/download/download_file_manager.h"
22 #include "content/browser/download/download_id.h" 22 #include "content/browser/download/download_id.h"
23 #include "content/browser/download/download_manager.h"
24 #include "content/browser/download/download_persistent_store_info.h" 23 #include "content/browser/download/download_persistent_store_info.h"
25 #include "content/browser/download/download_request_handle.h" 24 #include "content/browser/download/download_request_handle.h"
26 #include "content/browser/download/download_stats.h" 25 #include "content/browser/download/download_stats.h"
27 #include "content/browser/download/interrupt_reasons.h" 26 #include "content/browser/download/interrupt_reasons.h"
28 #include "content/browser/tab_contents/tab_contents.h" 27 #include "content/browser/tab_contents/tab_contents.h"
29 #include "content/public/browser/browser_thread.h" 28 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/content_browser_client.h" 29 #include "content/public/browser/content_browser_client.h"
31 #include "content/public/browser/download_manager_delegate.h"
32 #include "net/base/net_util.h" 30 #include "net/base/net_util.h"
33 31
34 using content::BrowserThread; 32 using content::BrowserThread;
35 33
36 // A DownloadItem normally goes through the following states: 34 // A DownloadItem normally goes through the following states:
37 // * Created (when download starts) 35 // * Created (when download starts)
38 // * Made visible to consumers (e.g. Javascript) after the 36 // * Made visible to consumers (e.g. Javascript) after the
39 // destination file has been determined. 37 // destination file has been determined.
40 // * Entered into the history database. 38 // * Entered into the history database.
41 // * Made visible in the download shelf. 39 // * Made visible in the download shelf.
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 virtual void PauseRequest() const OVERRIDE {} 109 virtual void PauseRequest() const OVERRIDE {}
112 virtual void ResumeRequest() const OVERRIDE {} 110 virtual void ResumeRequest() const OVERRIDE {}
113 virtual void CancelRequest() const OVERRIDE {} 111 virtual void CancelRequest() const OVERRIDE {}
114 virtual std::string DebugString() const OVERRIDE { 112 virtual std::string DebugString() const OVERRIDE {
115 return "Null DownloadRequestHandle"; 113 return "Null DownloadRequestHandle";
116 } 114 }
117 }; 115 };
118 116
119 } // namespace 117 } // namespace
120 118
119 // Infrastructure in DownloadItemImpl::Delegate to assert invariant that
120 // delegate always outlives all attached DownloadItemImpls.
121 DownloadItemImpl::Delegate::Delegate()
122 : count_(0) {}
123
124 DownloadItemImpl::Delegate::~Delegate() {
125 DCHECK_EQ(0, count_);
126 }
127
128 void DownloadItemImpl::Delegate::Attach() {
129 ++count_;
130 }
131
132 void DownloadItemImpl::Delegate::Detach() {
133 DCHECK_LT(0, count_);
134 --count_;
135 }
136
121 // Our download table ID starts at 1, so we use 0 to represent a download that 137 // Our download table ID starts at 1, so we use 0 to represent a download that
122 // has started, but has not yet had its data persisted in the table. We use fake 138 // has started, but has not yet had its data persisted in the table. We use fake
123 // database handles in incognito mode starting at -1 and progressively getting 139 // database handles in incognito mode starting at -1 and progressively getting
124 // more negative. 140 // more negative.
125 141
126 // Constructor for reading from the history service. 142 // Constructor for reading from the history service.
127 DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, 143 DownloadItemImpl::DownloadItemImpl(Delegate* delegate,
144 DownloadId download_id,
128 const DownloadPersistentStoreInfo& info) 145 const DownloadPersistentStoreInfo& info)
129 : download_id_(download_manager->GetNextId()), 146 : download_id_(download_id),
130 full_path_(info.path), 147 full_path_(info.path),
131 url_chain_(1, info.url), 148 url_chain_(1, info.url),
132 referrer_url_(info.referrer_url), 149 referrer_url_(info.referrer_url),
133 total_bytes_(info.total_bytes), 150 total_bytes_(info.total_bytes),
134 received_bytes_(info.received_bytes), 151 received_bytes_(info.received_bytes),
135 bytes_per_sec_(0), 152 bytes_per_sec_(0),
136 start_tick_(base::TimeTicks()), 153 start_tick_(base::TimeTicks()),
137 state_(static_cast<DownloadState>(info.state)), 154 state_(static_cast<DownloadState>(info.state)),
138 start_time_(info.start_time), 155 start_time_(info.start_time),
139 end_time_(info.end_time), 156 end_time_(info.end_time),
140 db_handle_(info.db_handle), 157 db_handle_(info.db_handle),
141 download_manager_(download_manager), 158 delegate_(delegate),
142 is_paused_(false), 159 is_paused_(false),
143 open_when_complete_(false), 160 open_when_complete_(false),
144 file_externally_removed_(false), 161 file_externally_removed_(false),
145 safety_state_(SAFE), 162 safety_state_(SAFE),
146 auto_opened_(false), 163 auto_opened_(false),
147 is_otr_(false), 164 is_otr_(false),
148 is_temporary_(false), 165 is_temporary_(false),
149 all_data_saved_(false), 166 all_data_saved_(false),
150 opened_(info.opened), 167 opened_(info.opened),
151 open_enabled_(true), 168 open_enabled_(true),
152 delegate_delayed_complete_(false) { 169 delegate_delayed_complete_(false) {
170 delegate_->Attach();
153 if (IsInProgress()) 171 if (IsInProgress())
154 state_ = CANCELLED; 172 state_ = CANCELLED;
155 if (IsComplete()) 173 if (IsComplete())
156 all_data_saved_ = true; 174 all_data_saved_ = true;
157 Init(false /* not actively downloading */); 175 Init(false /* not actively downloading */);
158 } 176 }
159 177
160 // Constructing for a regular download: 178 // Constructing for a regular download:
161 DownloadItemImpl::DownloadItemImpl( 179 DownloadItemImpl::DownloadItemImpl(
162 DownloadManager* download_manager, 180 Delegate* delegate,
163 const DownloadCreateInfo& info, 181 const DownloadCreateInfo& info,
164 DownloadRequestHandleInterface* request_handle, 182 DownloadRequestHandleInterface* request_handle,
165 bool is_otr) 183 bool is_otr)
166 : state_info_(info.original_name, info.save_info.file_path, 184 : state_info_(info.original_name, info.save_info.file_path,
167 info.has_user_gesture, info.transition_type, 185 info.has_user_gesture, info.transition_type,
168 info.prompt_user_for_save_location, info.path_uniquifier, 186 info.prompt_user_for_save_location, info.path_uniquifier,
169 DownloadStateInfo::NOT_DANGEROUS), 187 DownloadStateInfo::NOT_DANGEROUS),
170 request_handle_(request_handle), 188 request_handle_(request_handle),
171 download_id_(info.download_id), 189 download_id_(info.download_id),
172 full_path_(info.path), 190 full_path_(info.path),
173 url_chain_(info.url_chain), 191 url_chain_(info.url_chain),
174 referrer_url_(info.referrer_url), 192 referrer_url_(info.referrer_url),
175 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), 193 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)),
176 content_disposition_(info.content_disposition), 194 content_disposition_(info.content_disposition),
177 mime_type_(info.mime_type), 195 mime_type_(info.mime_type),
178 original_mime_type_(info.original_mime_type), 196 original_mime_type_(info.original_mime_type),
179 referrer_charset_(info.referrer_charset), 197 referrer_charset_(info.referrer_charset),
180 remote_address_(info.remote_address), 198 remote_address_(info.remote_address),
181 total_bytes_(info.total_bytes), 199 total_bytes_(info.total_bytes),
182 received_bytes_(0), 200 received_bytes_(0),
183 bytes_per_sec_(0), 201 bytes_per_sec_(0),
184 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), 202 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE),
185 start_tick_(base::TimeTicks::Now()), 203 start_tick_(base::TimeTicks::Now()),
186 state_(IN_PROGRESS), 204 state_(IN_PROGRESS),
187 start_time_(info.start_time), 205 start_time_(info.start_time),
188 db_handle_(DownloadItem::kUninitializedHandle), 206 db_handle_(DownloadItem::kUninitializedHandle),
189 download_manager_(download_manager), 207 delegate_(delegate),
190 is_paused_(false), 208 is_paused_(false),
191 open_when_complete_(false), 209 open_when_complete_(false),
192 file_externally_removed_(false), 210 file_externally_removed_(false),
193 safety_state_(SAFE), 211 safety_state_(SAFE),
194 auto_opened_(false), 212 auto_opened_(false),
195 is_otr_(is_otr), 213 is_otr_(is_otr),
196 is_temporary_(!info.save_info.file_path.empty()), 214 is_temporary_(!info.save_info.file_path.empty()),
197 all_data_saved_(false), 215 all_data_saved_(false),
198 opened_(false), 216 opened_(false),
199 open_enabled_(true), 217 open_enabled_(true),
200 delegate_delayed_complete_(false) { 218 delegate_delayed_complete_(false) {
219 delegate_->Attach();
201 Init(true /* actively downloading */); 220 Init(true /* actively downloading */);
202 } 221 }
203 222
204 // Constructing for the "Save Page As..." feature: 223 // Constructing for the "Save Page As..." feature:
205 DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, 224 DownloadItemImpl::DownloadItemImpl(Delegate* delegate,
206 const FilePath& path, 225 const FilePath& path,
207 const GURL& url, 226 const GURL& url,
208 bool is_otr, 227 bool is_otr,
209 DownloadId download_id) 228 DownloadId download_id)
210 : request_handle_(new NullDownloadRequestHandle()), 229 : request_handle_(new NullDownloadRequestHandle()),
211 download_id_(download_id), 230 download_id_(download_id),
212 full_path_(path), 231 full_path_(path),
213 url_chain_(1, url), 232 url_chain_(1, url),
214 referrer_url_(GURL()), 233 referrer_url_(GURL()),
215 total_bytes_(0), 234 total_bytes_(0),
216 received_bytes_(0), 235 received_bytes_(0),
217 bytes_per_sec_(0), 236 bytes_per_sec_(0),
218 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), 237 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE),
219 start_tick_(base::TimeTicks::Now()), 238 start_tick_(base::TimeTicks::Now()),
220 state_(IN_PROGRESS), 239 state_(IN_PROGRESS),
221 start_time_(base::Time::Now()), 240 start_time_(base::Time::Now()),
222 db_handle_(DownloadItem::kUninitializedHandle), 241 db_handle_(DownloadItem::kUninitializedHandle),
223 download_manager_(download_manager), 242 delegate_(delegate),
224 is_paused_(false), 243 is_paused_(false),
225 open_when_complete_(false), 244 open_when_complete_(false),
226 file_externally_removed_(false), 245 file_externally_removed_(false),
227 safety_state_(SAFE), 246 safety_state_(SAFE),
228 auto_opened_(false), 247 auto_opened_(false),
229 is_otr_(is_otr), 248 is_otr_(is_otr),
230 is_temporary_(false), 249 is_temporary_(false),
231 all_data_saved_(false), 250 all_data_saved_(false),
232 opened_(false), 251 opened_(false),
233 open_enabled_(true), 252 open_enabled_(true),
234 delegate_delayed_complete_(false) { 253 delegate_delayed_complete_(false) {
254 delegate_->Attach();
235 Init(true /* actively downloading */); 255 Init(true /* actively downloading */);
236 } 256 }
237 257
238 DownloadItemImpl::~DownloadItemImpl() { 258 DownloadItemImpl::~DownloadItemImpl() {
239 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 259 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
240 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 260 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
241 261
242 TransitionTo(REMOVING); 262 TransitionTo(REMOVING);
243 download_manager_->AssertQueueStateConsistent(this); 263 delegate_->AssertStateConsistent(this);
264 delegate_->Detach();
244 } 265 }
245 266
246 void DownloadItemImpl::AddObserver(Observer* observer) { 267 void DownloadItemImpl::AddObserver(Observer* observer) {
247 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 268 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
248 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 269 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
249 270
250 observers_.AddObserver(observer); 271 observers_.AddObserver(observer);
251 } 272 }
252 273
253 void DownloadItemImpl::RemoveObserver(Observer* observer) { 274 void DownloadItemImpl::RemoveObserver(Observer* observer) {
(...skipping 12 matching lines...) Expand all
266 287
267 bool DownloadItemImpl::CanShowInFolder() { 288 bool DownloadItemImpl::CanShowInFolder() {
268 return !IsCancelled() && !file_externally_removed_; 289 return !IsCancelled() && !file_externally_removed_;
269 } 290 }
270 291
271 bool DownloadItemImpl::CanOpenDownload() { 292 bool DownloadItemImpl::CanOpenDownload() {
272 return !file_externally_removed_; 293 return !file_externally_removed_;
273 } 294 }
274 295
275 bool DownloadItemImpl::ShouldOpenFileBasedOnExtension() { 296 bool DownloadItemImpl::ShouldOpenFileBasedOnExtension() {
276 return download_manager_->delegate()->ShouldOpenFileBasedOnExtension( 297 return delegate_->ShouldOpenFileBasedOnExtension(GetUserVerifiedFilePath());
277 GetUserVerifiedFilePath());
278 } 298 }
279 299
280 void DownloadItemImpl::OpenDownload() { 300 void DownloadItemImpl::OpenDownload() {
281 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 301 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
282 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 302 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
283 303
284 if (IsPartialDownload()) { 304 if (IsPartialDownload()) {
285 open_when_complete_ = !open_when_complete_; 305 open_when_complete_ = !open_when_complete_;
286 return; 306 return;
287 } 307 }
288 308
289 if (!IsComplete() || file_externally_removed_) 309 if (!IsComplete() || file_externally_removed_)
290 return; 310 return;
291 311
292 // Ideally, we want to detect errors in opening and report them, but we 312 // Ideally, we want to detect errors in opening and report them, but we
293 // don't generally have the proper interface for that to the external 313 // don't generally have the proper interface for that to the external
294 // program that opens the file. So instead we spawn a check to update 314 // program that opens the file. So instead we spawn a check to update
295 // the UI if the file has been deleted in parallel with the open. 315 // the UI if the file has been deleted in parallel with the open.
296 download_manager_->CheckForFileRemoval(this); 316 delegate_->CheckForFileRemoval(this);
297 download_stats::RecordOpen(GetEndTime(), !GetOpened()); 317 download_stats::RecordOpen(GetEndTime(), !GetOpened());
298 opened_ = true; 318 opened_ = true;
299 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); 319 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this));
300 download_manager_->MarkDownloadOpened(this); 320 delegate_->DownloadOpened(this);
301 321
302 // For testing: If download opening is disabled on this item, 322 // For testing: If download opening is disabled on this item,
303 // make the rest of the routine a no-op. 323 // make the rest of the routine a no-op.
304 if (!open_enabled_) 324 if (!open_enabled_)
305 return; 325 return;
306 326
307 content::GetContentClient()->browser()->OpenItem(GetFullPath()); 327 content::GetContentClient()->browser()->OpenItem(GetFullPath());
308 } 328 }
309 329
310 void DownloadItemImpl::ShowDownloadInShell() { 330 void DownloadItemImpl::ShowDownloadInShell() {
311 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 331 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
312 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 332 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
313 333
314 content::GetContentClient()->browser()->ShowItemInFolder(GetFullPath()); 334 content::GetContentClient()->browser()->ShowItemInFolder(GetFullPath());
315 } 335 }
316 336
317 void DownloadItemImpl::DangerousDownloadValidated() { 337 void DownloadItemImpl::DangerousDownloadValidated() {
318 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 338 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
319 DCHECK_EQ(DANGEROUS, GetSafetyState()); 339 DCHECK_EQ(DANGEROUS, GetSafetyState());
320 340
321 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", 341 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated",
322 GetDangerType(), 342 GetDangerType(),
323 DownloadStateInfo::DANGEROUS_TYPE_MAX); 343 DownloadStateInfo::DANGEROUS_TYPE_MAX);
324 344
325 safety_state_ = DANGEROUS_BUT_VALIDATED; 345 safety_state_ = DANGEROUS_BUT_VALIDATED;
326 UpdateObservers(); 346 UpdateObservers();
327 347
328 download_manager_->MaybeCompleteDownload(this); 348 delegate_->MaybeCompleteDownload(this);
329 } 349 }
330 350
331 void DownloadItemImpl::UpdateSize(int64 bytes_so_far) { 351 void DownloadItemImpl::UpdateSize(int64 bytes_so_far) {
332 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 352 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
333 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 353 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
334 354
335 received_bytes_ = bytes_so_far; 355 received_bytes_ = bytes_so_far;
336 356
337 // If we've received more data than we were expecting (bad server info?), 357 // If we've received more data than we were expecting (bad server info?),
338 // revert to 'unknown size mode'. 358 // revert to 'unknown size mode'.
(...skipping 30 matching lines...) Expand all
369 if (!IsPartialDownload()) { 389 if (!IsPartialDownload()) {
370 // Small downloads might be complete before this method has 390 // Small downloads might be complete before this method has
371 // a chance to run. 391 // a chance to run.
372 return; 392 return;
373 } 393 }
374 394
375 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); 395 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT);
376 396
377 TransitionTo(CANCELLED); 397 TransitionTo(CANCELLED);
378 if (user_cancel) 398 if (user_cancel)
379 download_manager_->DownloadCancelledInternal(this); 399 delegate_->DownloadCancelled(this);
380 } 400 }
381 401
382 void DownloadItemImpl::MarkAsComplete() { 402 void DownloadItemImpl::MarkAsComplete() {
383 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 403 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
384 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 404 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
385 405
386 DCHECK(all_data_saved_); 406 DCHECK(all_data_saved_);
387 end_time_ = base::Time::Now(); 407 end_time_ = base::Time::Now();
388 TransitionTo(COMPLETE); 408 TransitionTo(COMPLETE);
389 } 409 }
(...skipping 12 matching lines...) Expand all
402 all_data_saved_ = true; 422 all_data_saved_ = true;
403 UpdateSize(size); 423 UpdateSize(size);
404 hash_ = final_hash; 424 hash_ = final_hash;
405 } 425 }
406 426
407 void DownloadItemImpl::OnDownloadedFileRemoved() { 427 void DownloadItemImpl::OnDownloadedFileRemoved() {
408 file_externally_removed_ = true; 428 file_externally_removed_ = true;
409 UpdateObservers(); 429 UpdateObservers();
410 } 430 }
411 431
432 void DownloadItemImpl::MaybeCompleteDownload() {
433 // TODO(rdsmith): Move logic for this function here.
434 delegate_->MaybeCompleteDownload(this);
435 }
436
412 void DownloadItemImpl::Completed() { 437 void DownloadItemImpl::Completed() {
413 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 438 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
414 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 439 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
415 440
416 VLOG(20) << __FUNCTION__ << "() " << DebugString(false); 441 VLOG(20) << __FUNCTION__ << "() " << DebugString(false);
417 442
418 DCHECK(all_data_saved_); 443 DCHECK(all_data_saved_);
419 end_time_ = base::Time::Now(); 444 end_time_ = base::Time::Now();
420 TransitionTo(COMPLETE); 445 TransitionTo(COMPLETE);
421 download_manager_->DownloadCompleted(GetId()); 446 delegate_->DownloadCompleted(this);
422 download_stats::RecordDownloadCompleted(start_tick_, received_bytes_); 447 download_stats::RecordDownloadCompleted(start_tick_, received_bytes_);
423 448
424 if (auto_opened_) { 449 if (auto_opened_) {
425 // If it was already handled by the delegate, do nothing. 450 // If it was already handled by the delegate, do nothing.
426 } else if (GetOpenWhenComplete() || 451 } else if (GetOpenWhenComplete() ||
427 ShouldOpenFileBasedOnExtension() || 452 ShouldOpenFileBasedOnExtension() ||
428 IsTemporary()) { 453 IsTemporary()) {
429 // If the download is temporary, like in drag-and-drop, do not open it but 454 // If the download is temporary, like in drag-and-drop, do not open it but
430 // we still need to set it auto-opened so that it can be removed from the 455 // we still need to set it auto-opened so that it can be removed from the
431 // download shelf. 456 // download shelf.
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 522 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
498 base::Bind(&DeleteDownloadedFile, full_path_)); 523 base::Bind(&DeleteDownloadedFile, full_path_));
499 Remove(); 524 Remove();
500 // We have now been deleted. 525 // We have now been deleted.
501 } 526 }
502 527
503 void DownloadItemImpl::Remove() { 528 void DownloadItemImpl::Remove() {
504 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 529 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
505 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 530 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
506 531
507 download_manager_->AssertQueueStateConsistent(this); 532 delegate_->AssertStateConsistent(this);
508 Cancel(true); 533 Cancel(true);
509 download_manager_->AssertQueueStateConsistent(this); 534 delegate_->AssertStateConsistent(this);
510 535
511 TransitionTo(REMOVING); 536 TransitionTo(REMOVING);
512 download_manager_->RemoveDownload(db_handle_); 537 delegate_->DownloadRemoved(this);
513 // We have now been deleted. 538 // We have now been deleted.
514 } 539 }
515 540
516 bool DownloadItemImpl::TimeRemaining(base::TimeDelta* remaining) const { 541 bool DownloadItemImpl::TimeRemaining(base::TimeDelta* remaining) const {
517 if (total_bytes_ <= 0) 542 if (total_bytes_ <= 0)
518 return false; // We never received the content_length for this download. 543 return false; // We never received the content_length for this download.
519 544
520 int64 speed = CurrentSpeed(); 545 int64 speed = CurrentSpeed();
521 if (speed == 0) 546 if (speed == 0)
522 return false; 547 return false;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 601
577 VLOG(20) << __FUNCTION__ << "()" 602 VLOG(20) << __FUNCTION__ << "()"
578 << " needs rename = " << NeedsRename() 603 << " needs rename = " << NeedsRename()
579 << " " << DebugString(true); 604 << " " << DebugString(true);
580 DCHECK_NE(DANGEROUS, GetSafetyState()); 605 DCHECK_NE(DANGEROUS, GetSafetyState());
581 DCHECK(file_manager); 606 DCHECK(file_manager);
582 607
583 if (NeedsRename()) { 608 if (NeedsRename()) {
584 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 609 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
585 base::Bind(&DownloadFileManager::RenameCompletingDownloadFile, 610 base::Bind(&DownloadFileManager::RenameCompletingDownloadFile,
586 file_manager, GetGlobalId(), 611 file_manager, download_id_,
587 GetTargetFilePath(), GetSafetyState() == SAFE)); 612 GetTargetFilePath(), GetSafetyState() == SAFE));
588 return; 613 return;
589 } 614 }
590 615
591 Completed(); 616 Completed();
592 617
593 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 618 BrowserThread::PostTask(
594 base::Bind(&DownloadFileManager::CompleteDownload, 619 BrowserThread::FILE, FROM_HERE,
595 file_manager, GetGlobalId())); 620 base::Bind(&DownloadFileManager::CompleteDownload,
621 file_manager, download_id_));
596 } 622 }
597 623
598 void DownloadItemImpl::OnDownloadRenamedToFinalName(const FilePath& full_path) { 624 void DownloadItemImpl::OnDownloadRenamedToFinalName(const FilePath& full_path) {
599 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 625 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
600 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 626 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
601 627
602 VLOG(20) << __FUNCTION__ << "()" 628 VLOG(20) << __FUNCTION__ << "()"
603 << " full_path = \"" << full_path.value() << "\"" 629 << " full_path = \"" << full_path.value() << "\""
604 << " needed rename = " << NeedsRename() 630 << " needed rename = " << NeedsRename()
605 << " " << DebugString(false); 631 << " " << DebugString(false);
606 DCHECK(NeedsRename()); 632 DCHECK(NeedsRename());
607 633
608 Rename(full_path); 634 Rename(full_path);
609 635
610 if (download_manager_->delegate()->ShouldOpenDownload(this)) { 636 if (delegate_->ShouldOpenDownload(this)) {
611 Completed(); 637 Completed();
612 } else { 638 } else {
613 delegate_delayed_complete_ = true; 639 delegate_delayed_complete_ = true;
614 } 640 }
615 } 641 }
616 642
617 bool DownloadItemImpl::MatchesQuery(const string16& query) const { 643 bool DownloadItemImpl::MatchesQuery(const string16& query) const {
618 if (query.empty()) 644 if (query.empty())
619 return true; 645 return true;
620 646
621 DCHECK_EQ(query, base::i18n::ToLower(query)); 647 DCHECK_EQ(query, base::i18n::ToLower(query));
622 648
623 string16 url_raw(UTF8ToUTF16(GetURL().spec())); 649 string16 url_raw(UTF8ToUTF16(GetURL().spec()));
624 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_raw)) 650 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_raw))
625 return true; 651 return true;
626 652
627 // TODO(phajdan.jr): write a test case for the following code. 653 // TODO(phajdan.jr): write a test case for the following code.
628 // A good test case would be: 654 // A good test case would be:
629 // "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", 655 // "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd",
630 // L"/\x4f60\x597d\x4f60\x597d", 656 // L"/\x4f60\x597d\x4f60\x597d",
631 // "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD" 657 // "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD"
632 std::string languages; 658 std::string languages;
633 TabContents* tab = GetTabContents(); 659 languages = content::GetContentClient()->browser()->GetAcceptLangs(
634 if (tab) { 660 BrowserContext());
635 languages = content::GetContentClient()->browser()->GetAcceptLangs(
636 tab->browser_context());
637 }
638 string16 url_formatted(net::FormatUrl(GetURL(), languages)); 661 string16 url_formatted(net::FormatUrl(GetURL(), languages));
639 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_formatted)) 662 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_formatted))
640 return true; 663 return true;
641 664
642 string16 path(GetFullPath().LossyDisplayName()); 665 string16 path(GetFullPath().LossyDisplayName());
643 return base::i18n::StringSearchIgnoringCaseAndAccents(query, path); 666 return base::i18n::StringSearchIgnoringCaseAndAccents(query, path);
644 } 667 }
645 668
646 void DownloadItemImpl::SetFileCheckResults(const DownloadStateInfo& state) { 669 void DownloadItemImpl::SetFileCheckResults(const DownloadStateInfo& state) {
647 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 670 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
699 TabContents* DownloadItemImpl::GetTabContents() const { 722 TabContents* DownloadItemImpl::GetTabContents() const {
700 // TODO(rdsmith): Remove null check after removing GetTabContents() from 723 // TODO(rdsmith): Remove null check after removing GetTabContents() from
701 // paths that might be used by DownloadItems created from history import. 724 // paths that might be used by DownloadItems created from history import.
702 // Currently such items have null request_handle_s, where other items 725 // Currently such items have null request_handle_s, where other items
703 // (regular and SavePackage downloads) have actual objects off the pointer. 726 // (regular and SavePackage downloads) have actual objects off the pointer.
704 if (request_handle_.get()) 727 if (request_handle_.get())
705 return request_handle_->GetTabContents(); 728 return request_handle_->GetTabContents();
706 return NULL; 729 return NULL;
707 } 730 }
708 731
732 content::BrowserContext* DownloadItemImpl::BrowserContext() const {
733 return delegate_->BrowserContext();
734 }
735
709 FilePath DownloadItemImpl::GetTargetFilePath() const { 736 FilePath DownloadItemImpl::GetTargetFilePath() const {
710 return full_path_.DirName().Append(state_info_.target_name); 737 return full_path_.DirName().Append(state_info_.target_name);
711 } 738 }
712 739
713 FilePath DownloadItemImpl::GetFileNameToReportUser() const { 740 FilePath DownloadItemImpl::GetFileNameToReportUser() const {
714 if (state_info_.path_uniquifier > 0) { 741 if (state_info_.path_uniquifier > 0) {
715 FilePath name(state_info_.target_name); 742 FilePath name(state_info_.target_name);
716 DownloadFile::AppendNumberToPath(&name, state_info_.path_uniquifier); 743 DownloadFile::AppendNumberToPath(&name, state_info_.path_uniquifier);
717 return name; 744 return name;
718 } 745 }
719 return state_info_.target_name; 746 return state_info_.target_name;
720 } 747 }
721 748
722 FilePath DownloadItemImpl::GetUserVerifiedFilePath() const { 749 FilePath DownloadItemImpl::GetUserVerifiedFilePath() const {
723 return (safety_state_ == DownloadItem::SAFE) ? 750 return (safety_state_ == DownloadItem::SAFE) ?
724 GetTargetFilePath() : full_path_; 751 GetTargetFilePath() : full_path_;
725 } 752 }
726 753
727 void DownloadItemImpl::OffThreadCancel(DownloadFileManager* file_manager) { 754 void DownloadItemImpl::OffThreadCancel(DownloadFileManager* file_manager) {
728 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 755 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
729 request_handle_->CancelRequest(); 756 request_handle_->CancelRequest();
730 757
731 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 758 BrowserThread::PostTask(
732 base::Bind(&DownloadFileManager::CancelDownload, 759 BrowserThread::FILE, FROM_HERE,
733 file_manager, GetGlobalId())); 760 base::Bind(&DownloadFileManager::CancelDownload,
761 file_manager, download_id_));
734 } 762 }
735 763
736 void DownloadItemImpl::Init(bool active) { 764 void DownloadItemImpl::Init(bool active) {
737 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. 765 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved.
738 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 766 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
739 767
740 UpdateTarget(); 768 UpdateTarget();
741 if (active) { 769 if (active) {
742 download_stats::RecordDownloadCount(download_stats::START_COUNT); 770 download_stats::RecordDownloadCount(download_stats::START_COUNT);
743 } 771 }
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
858 total_bytes_ = total_bytes; 886 total_bytes_ = total_bytes;
859 } 887 }
860 const std::string& DownloadItemImpl::GetHash() const { return hash_; } 888 const std::string& DownloadItemImpl::GetHash() const { return hash_; }
861 int64 DownloadItemImpl::GetReceivedBytes() const { return received_bytes_; } 889 int64 DownloadItemImpl::GetReceivedBytes() const { return received_bytes_; }
862 int32 DownloadItemImpl::GetId() const { return download_id_.local(); } 890 int32 DownloadItemImpl::GetId() const { return download_id_.local(); }
863 DownloadId DownloadItemImpl::GetGlobalId() const { return download_id_; } 891 DownloadId DownloadItemImpl::GetGlobalId() const { return download_id_; }
864 base::Time DownloadItemImpl::GetStartTime() const { return start_time_; } 892 base::Time DownloadItemImpl::GetStartTime() const { return start_time_; }
865 base::Time DownloadItemImpl::GetEndTime() const { return end_time_; } 893 base::Time DownloadItemImpl::GetEndTime() const { return end_time_; }
866 void DownloadItemImpl::SetDbHandle(int64 handle) { db_handle_ = handle; } 894 void DownloadItemImpl::SetDbHandle(int64 handle) { db_handle_ = handle; }
867 int64 DownloadItemImpl::GetDbHandle() const { return db_handle_; } 895 int64 DownloadItemImpl::GetDbHandle() const { return db_handle_; }
868 DownloadManager* DownloadItemImpl::GetDownloadManager() {
869 return download_manager_;
870 }
871 bool DownloadItemImpl::IsPaused() const { return is_paused_; } 896 bool DownloadItemImpl::IsPaused() const { return is_paused_; }
872 bool DownloadItemImpl::GetOpenWhenComplete() const { 897 bool DownloadItemImpl::GetOpenWhenComplete() const {
873 return open_when_complete_; 898 return open_when_complete_;
874 } 899 }
875 void DownloadItemImpl::SetOpenWhenComplete(bool open) { 900 void DownloadItemImpl::SetOpenWhenComplete(bool open) {
876 open_when_complete_ = open; 901 open_when_complete_ = open;
877 } 902 }
878 bool DownloadItemImpl::GetFileExternallyRemoved() const { 903 bool DownloadItemImpl::GetFileExternallyRemoved() const {
879 return file_externally_removed_; 904 return file_externally_removed_;
880 } 905 }
(...skipping 15 matching lines...) Expand all
896 void DownloadItemImpl::SetOpened(bool opened) { opened_ = opened; } 921 void DownloadItemImpl::SetOpened(bool opened) { opened_ = opened; }
897 bool DownloadItemImpl::GetOpened() const { return opened_; } 922 bool DownloadItemImpl::GetOpened() const { return opened_; }
898 InterruptReason DownloadItemImpl::GetLastReason() const { 923 InterruptReason DownloadItemImpl::GetLastReason() const {
899 return last_reason_; 924 return last_reason_;
900 } 925 }
901 DownloadStateInfo DownloadItemImpl::GetStateInfo() const { return state_info_; } 926 DownloadStateInfo DownloadItemImpl::GetStateInfo() const { return state_info_; }
902 bool DownloadItemImpl::NeedsRename() const { 927 bool DownloadItemImpl::NeedsRename() const {
903 return state_info_.target_name != full_path_.BaseName(); 928 return state_info_.target_name != full_path_.BaseName();
904 } 929 }
905 void DownloadItemImpl::MockDownloadOpenForTesting() { open_enabled_ = false; } 930 void DownloadItemImpl::MockDownloadOpenForTesting() { open_enabled_ = false; }
OLDNEW
« no previous file with comments | « content/browser/download/download_item_impl.h ('k') | content/browser/download/download_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698