OLD | NEW |
---|---|
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 "chrome/browser/download/download_item.h" | 5 #include "chrome/browser/download/download_item.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/i18n/case_conversion.h" | 10 #include "base/i18n/case_conversion.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
14 #include "base/timer.h" | 14 #include "base/timer.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
16 #include "net/base/net_util.h" | 16 #include "net/base/net_util.h" |
17 #include "chrome/browser/download/download_create_info.h" | |
17 #include "chrome/browser/download/download_extensions.h" | 18 #include "chrome/browser/download/download_extensions.h" |
18 #include "chrome/browser/download/download_file_manager.h" | 19 #include "chrome/browser/download/download_file_manager.h" |
19 #include "chrome/browser/download/download_history.h" | 20 #include "chrome/browser/download/download_history.h" |
20 #include "chrome/browser/download/download_manager.h" | 21 #include "chrome/browser/download/download_manager.h" |
21 #include "chrome/browser/download/download_prefs.h" | 22 #include "chrome/browser/download/download_prefs.h" |
22 #include "chrome/browser/download/download_util.h" | 23 #include "chrome/browser/download/download_util.h" |
23 #include "chrome/browser/history/download_create_info.h" | |
24 #include "chrome/browser/platform_util.h" | 24 #include "chrome/browser/platform_util.h" |
25 #include "chrome/browser/prefs/pref_service.h" | 25 #include "chrome/browser/prefs/pref_service.h" |
26 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
27 #include "chrome/common/extensions/extension.h" | 27 #include "chrome/common/extensions/extension.h" |
28 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
29 #include "content/browser/browser_thread.h" | 29 #include "content/browser/browser_thread.h" |
30 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
31 | 31 |
32 // A DownloadItem normally goes through the following states: | 32 // A DownloadItem normally goes through the following states: |
33 // * Created (when download starts) | 33 // * Created (when download starts) |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 if (dangerous_url) { | 105 if (dangerous_url) { |
106 // dangerous URL overweights dangerous file. We check dangerous URL first. | 106 // dangerous URL overweights dangerous file. We check dangerous URL first. |
107 return DownloadItem::DANGEROUS_URL; | 107 return DownloadItem::DANGEROUS_URL; |
108 } | 108 } |
109 return dangerous_file ? | 109 return dangerous_file ? |
110 DownloadItem::DANGEROUS_FILE : DownloadItem::NOT_DANGEROUS; | 110 DownloadItem::DANGEROUS_FILE : DownloadItem::NOT_DANGEROUS; |
111 } | 111 } |
112 | 112 |
113 } // namespace | 113 } // namespace |
114 | 114 |
115 DownloadItem::DownloadStateInfo::DownloadStateInfo() | |
116 : path_uniquifier(0), | |
Paweł Hajdan Jr.
2011/05/20 09:04:42
nit: Shouldn't this be indented +4?
ahendrickson
2011/05/20 18:31:25
Done.
| |
117 has_user_gesture(false), | |
118 prompt_user_for_save_location(false), | |
119 is_dangerous_file(false), | |
120 is_dangerous_url(false), | |
121 is_extension_install(false) { | |
122 } | |
123 | |
124 DownloadItem::DownloadStateInfo::DownloadStateInfo( | |
125 bool has_user_gesture, | |
126 bool prompt_user_for_save_location) | |
127 : path_uniquifier(0), | |
128 has_user_gesture(has_user_gesture), | |
129 prompt_user_for_save_location(prompt_user_for_save_location), | |
130 is_dangerous_file(false), | |
131 is_dangerous_url(false), | |
132 is_extension_install(false) { | |
133 } | |
134 | |
135 DownloadItem::DownloadStateInfo::DownloadStateInfo( | |
136 const FilePath& target, | |
137 const FilePath& forced_name, | |
138 bool has_user_gesture, | |
139 bool prompt_user_for_save_location, | |
140 int uniquifier, | |
141 bool dangerous_file, | |
142 bool dangerous_url, | |
143 bool extension_install) | |
144 : target_name(target), | |
145 path_uniquifier(uniquifier), | |
146 has_user_gesture(has_user_gesture), | |
147 prompt_user_for_save_location(prompt_user_for_save_location), | |
148 is_dangerous_file(dangerous_file), | |
149 is_dangerous_url(dangerous_url), | |
150 is_extension_install(extension_install), | |
151 force_file_name(forced_name) { | |
152 } | |
153 | |
154 bool DownloadItem::DownloadStateInfo::IsDangerous() const { | |
155 return is_dangerous_url || is_dangerous_file; | |
156 } | |
157 | |
115 // Constructor for reading from the history service. | 158 // Constructor for reading from the history service. |
116 DownloadItem::DownloadItem(DownloadManager* download_manager, | 159 DownloadItem::DownloadItem(DownloadManager* download_manager, |
117 const DownloadCreateInfo& info) | 160 const DownloadHistoryInfo& info) |
118 : id_(-1), | 161 : history_info_(info.path, info.url_chain, info.referrer_url, |
119 full_path_(info.path), | 162 info.start_time, info.received_bytes, info.total_bytes, |
120 path_uniquifier_(0), | 163 info.state, info.db_handle, info.download_id), |
121 url_chain_(info.url_chain), | |
122 referrer_url_(info.referrer_url), | |
123 mime_type_(info.mime_type), | |
124 original_mime_type_(info.original_mime_type), | |
125 total_bytes_(info.total_bytes), | |
126 received_bytes_(info.received_bytes), | |
127 start_tick_(base::TimeTicks()), | 164 start_tick_(base::TimeTicks()), |
128 state_(static_cast<DownloadState>(info.state)), | |
129 start_time_(info.start_time), | |
130 db_handle_(info.db_handle), | |
131 download_manager_(download_manager), | 165 download_manager_(download_manager), |
132 is_paused_(false), | 166 is_paused_(false), |
133 open_when_complete_(false), | 167 open_when_complete_(false), |
134 safety_state_(SAFE), | 168 safety_state_(SAFE), |
135 danger_type_(NOT_DANGEROUS), | |
136 auto_opened_(false), | 169 auto_opened_(false), |
137 target_name_(info.original_name), | |
138 save_as_(false), | |
139 is_otr_(false), | 170 is_otr_(false), |
140 is_extension_install_(info.is_extension_install), | |
141 is_temporary_(false), | 171 is_temporary_(false), |
142 all_data_saved_(false), | 172 all_data_saved_(false), |
143 opened_(false) { | 173 opened_(false) { |
144 if (IsInProgress()) | 174 if (IsInProgress()) |
145 state_ = CANCELLED; | 175 history_info_.state = CANCELLED; |
146 if (IsComplete()) | 176 if (IsComplete()) |
147 all_data_saved_ = true; | 177 all_data_saved_ = true; |
148 Init(false /* don't start progress timer */); | 178 Init(false /* don't start progress timer */); |
149 } | 179 } |
150 | 180 |
151 // Constructing for a regular download: | 181 // Constructing for a regular download: |
152 DownloadItem::DownloadItem(DownloadManager* download_manager, | 182 DownloadItem::DownloadItem(DownloadManager* download_manager, |
153 const DownloadCreateInfo& info, | 183 const DownloadCreateInfo& info, |
154 bool is_otr) | 184 bool is_otr) |
155 : id_(info.download_id), | 185 : history_info_(info.path, info.url_chain, info.referrer_url, |
156 full_path_(info.path), | 186 info.start_time, info.received_bytes, info.total_bytes, |
157 path_uniquifier_(info.path_uniquifier), | 187 IN_PROGRESS, DownloadHistory::kUninitializedHandle, |
158 url_chain_(info.url_chain), | 188 info.download_id), |
159 referrer_url_(info.referrer_url), | 189 manager_state_(info.original_name, info.save_info.file_path, |
190 info.has_user_gesture, info.prompt_user_for_save_location, | |
191 info.path_uniquifier, info.is_dangerous_file, | |
192 info.is_dangerous_url, info.is_extension_install), | |
193 process_handle_(info.process_handle), | |
194 content_disposition_(info.content_disposition), | |
160 mime_type_(info.mime_type), | 195 mime_type_(info.mime_type), |
161 original_mime_type_(info.original_mime_type), | 196 original_mime_type_(info.original_mime_type), |
162 total_bytes_(info.total_bytes), | 197 referrer_charset_(info.referrer_charset), |
163 received_bytes_(0), | |
164 last_os_error_(0), | 198 last_os_error_(0), |
165 start_tick_(base::TimeTicks::Now()), | 199 start_tick_(base::TimeTicks::Now()), |
166 state_(IN_PROGRESS), | |
167 start_time_(info.start_time), | |
168 db_handle_(DownloadHistory::kUninitializedHandle), | |
169 download_manager_(download_manager), | 200 download_manager_(download_manager), |
170 is_paused_(false), | 201 is_paused_(false), |
171 open_when_complete_(false), | 202 open_when_complete_(false), |
172 safety_state_(GetSafetyState(info.is_dangerous_file, | 203 safety_state_(GetSafetyState(info.is_dangerous_file, |
173 info.is_dangerous_url)), | 204 info.is_dangerous_url)), |
174 danger_type_(GetDangerType(info.is_dangerous_file, | |
175 info.is_dangerous_url)), | |
176 auto_opened_(false), | 205 auto_opened_(false), |
177 target_name_(info.original_name), | |
178 process_handle_(info.process_handle), | |
179 save_as_(info.prompt_user_for_save_location), | |
180 is_otr_(is_otr), | 206 is_otr_(is_otr), |
181 is_extension_install_(info.is_extension_install), | |
182 is_temporary_(!info.save_info.file_path.empty()), | 207 is_temporary_(!info.save_info.file_path.empty()), |
183 all_data_saved_(false), | 208 all_data_saved_(false), |
184 opened_(false) { | 209 opened_(false) { |
185 Init(true /* start progress timer */); | 210 Init(true /* start progress timer */); |
186 } | 211 } |
187 | 212 |
188 // Constructing for the "Save Page As..." feature: | 213 // Constructing for the "Save Page As..." feature: |
189 DownloadItem::DownloadItem(DownloadManager* download_manager, | 214 DownloadItem::DownloadItem(DownloadManager* download_manager, |
190 const FilePath& path, | 215 const FilePath& path, |
191 const GURL& url, | 216 const GURL& url, |
192 bool is_otr) | 217 bool is_otr) |
193 : id_(1), | 218 : history_info_(path, url, base::Time::Now(), 0, 0, IN_PROGRESS), |
194 full_path_(path), | |
195 path_uniquifier_(0), | |
196 url_chain_(1, url), | |
197 referrer_url_(GURL()), | |
198 mime_type_(std::string()), | |
199 original_mime_type_(std::string()), | |
200 total_bytes_(0), | |
201 received_bytes_(0), | |
202 last_os_error_(0), | 219 last_os_error_(0), |
203 start_tick_(base::TimeTicks::Now()), | 220 start_tick_(base::TimeTicks::Now()), |
204 state_(IN_PROGRESS), | |
205 start_time_(base::Time::Now()), | |
206 db_handle_(DownloadHistory::kUninitializedHandle), | |
207 download_manager_(download_manager), | 221 download_manager_(download_manager), |
208 is_paused_(false), | 222 is_paused_(false), |
209 open_when_complete_(false), | 223 open_when_complete_(false), |
210 safety_state_(SAFE), | 224 safety_state_(SAFE), |
211 danger_type_(NOT_DANGEROUS), | |
212 auto_opened_(false), | 225 auto_opened_(false), |
213 save_as_(false), | |
214 is_otr_(is_otr), | 226 is_otr_(is_otr), |
215 is_extension_install_(false), | |
216 is_temporary_(false), | 227 is_temporary_(false), |
217 all_data_saved_(false), | 228 all_data_saved_(false), |
218 opened_(false) { | 229 opened_(false) { |
219 Init(true /* start progress timer */); | 230 Init(true /* start progress timer */); |
220 } | 231 } |
221 | 232 |
222 DownloadItem::~DownloadItem() { | 233 DownloadItem::~DownloadItem() { |
223 state_ = REMOVING; | 234 history_info_.state = REMOVING; |
224 UpdateObservers(); | 235 UpdateObservers(); |
225 } | 236 } |
226 | 237 |
227 void DownloadItem::AddObserver(Observer* observer) { | 238 void DownloadItem::AddObserver(Observer* observer) { |
228 observers_.AddObserver(observer); | 239 observers_.AddObserver(observer); |
229 } | 240 } |
230 | 241 |
231 void DownloadItem::RemoveObserver(Observer* observer) { | 242 void DownloadItem::RemoveObserver(Observer* observer) { |
232 observers_.RemoveObserver(observer); | 243 observers_.RemoveObserver(observer); |
233 } | 244 } |
234 | 245 |
235 void DownloadItem::UpdateObservers() { | 246 void DownloadItem::UpdateObservers() { |
236 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this)); | 247 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this)); |
237 } | 248 } |
238 | 249 |
239 bool DownloadItem::CanOpenDownload() { | 250 bool DownloadItem::CanOpenDownload() { |
240 return !Extension::IsExtension(target_name_); | 251 return !Extension::IsExtension(manager_state_.target_name); |
241 } | 252 } |
242 | 253 |
243 bool DownloadItem::ShouldOpenFileBasedOnExtension() { | 254 bool DownloadItem::ShouldOpenFileBasedOnExtension() { |
244 return download_manager_->ShouldOpenFileBasedOnExtension( | 255 return download_manager_->ShouldOpenFileBasedOnExtension( |
245 GetUserVerifiedFilePath()); | 256 GetUserVerifiedFilePath()); |
246 } | 257 } |
247 | 258 |
248 void DownloadItem::OpenFilesBasedOnExtension(bool open) { | 259 void DownloadItem::OpenFilesBasedOnExtension(bool open) { |
249 DownloadPrefs* prefs = download_manager_->download_prefs(); | 260 DownloadPrefs* prefs = download_manager_->download_prefs(); |
250 if (open) | 261 if (open) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
283 #else | 294 #else |
284 BrowserThread::PostTask( | 295 BrowserThread::PostTask( |
285 BrowserThread::FILE, FROM_HERE, | 296 BrowserThread::FILE, FROM_HERE, |
286 NewRunnableFunction(&platform_util::ShowItemInFolder, | 297 NewRunnableFunction(&platform_util::ShowItemInFolder, |
287 full_path())); | 298 full_path())); |
288 #endif | 299 #endif |
289 } | 300 } |
290 | 301 |
291 void DownloadItem::DangerousDownloadValidated() { | 302 void DownloadItem::DangerousDownloadValidated() { |
292 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", | 303 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", |
293 danger_type_, | 304 CurrentDangerType(), |
294 DANGEROUS_TYPE_MAX); | 305 DANGEROUS_TYPE_MAX); |
295 download_manager_->DangerousDownloadValidated(this); | 306 download_manager_->DangerousDownloadValidated(this); |
296 } | 307 } |
297 | 308 |
298 void DownloadItem::UpdateSize(int64 bytes_so_far) { | 309 void DownloadItem::UpdateSize(int64 bytes_so_far) { |
299 received_bytes_ = bytes_so_far; | 310 history_info_.received_bytes = bytes_so_far; |
300 | 311 |
301 // If we've received more data than we were expecting (bad server info?), | 312 // If we've received more data than we were expecting (bad server info?), |
302 // revert to 'unknown size mode'. | 313 // revert to 'unknown size mode'. |
303 if (received_bytes_ > total_bytes_) | 314 if (history_info_.received_bytes > history_info_.total_bytes) |
304 total_bytes_ = 0; | 315 history_info_.total_bytes = 0; |
305 } | 316 } |
306 | 317 |
307 void DownloadItem::StartProgressTimer() { | 318 void DownloadItem::StartProgressTimer() { |
308 update_timer_.Start(base::TimeDelta::FromMilliseconds(kUpdateTimeMs), this, | 319 update_timer_.Start(base::TimeDelta::FromMilliseconds(kUpdateTimeMs), this, |
309 &DownloadItem::UpdateObservers); | 320 &DownloadItem::UpdateObservers); |
310 } | 321 } |
311 | 322 |
312 void DownloadItem::StopProgressTimer() { | 323 void DownloadItem::StopProgressTimer() { |
313 update_timer_.Stop(); | 324 update_timer_.Stop(); |
314 } | 325 } |
(...skipping 14 matching lines...) Expand all Loading... | |
329 void DownloadItem::Cancel(bool update_history) { | 340 void DownloadItem::Cancel(bool update_history) { |
330 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 341 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
331 if (!IsPartialDownload()) { | 342 if (!IsPartialDownload()) { |
332 // Small downloads might be complete before this method has | 343 // Small downloads might be complete before this method has |
333 // a chance to run. | 344 // a chance to run. |
334 return; | 345 return; |
335 } | 346 } |
336 | 347 |
337 download_util::RecordDownloadCount(download_util::CANCELLED_COUNT); | 348 download_util::RecordDownloadCount(download_util::CANCELLED_COUNT); |
338 | 349 |
339 state_ = CANCELLED; | 350 history_info_.state = CANCELLED; |
340 UpdateObservers(); | 351 UpdateObservers(); |
341 StopProgressTimer(); | 352 StopProgressTimer(); |
342 if (update_history) | 353 if (update_history) |
343 download_manager_->DownloadCancelled(id_); | 354 download_manager_->DownloadCancelled(history_info_.download_id); |
344 } | 355 } |
345 | 356 |
346 void DownloadItem::MarkAsComplete() { | 357 void DownloadItem::MarkAsComplete() { |
347 DCHECK(all_data_saved_); | 358 DCHECK(all_data_saved_); |
348 state_ = COMPLETE; | 359 history_info_.state = COMPLETE; |
349 UpdateObservers(); | 360 UpdateObservers(); |
350 } | 361 } |
351 | 362 |
352 void DownloadItem::OnAllDataSaved(int64 size) { | 363 void DownloadItem::OnAllDataSaved(int64 size) { |
353 DCHECK(!all_data_saved_); | 364 DCHECK(!all_data_saved_); |
354 all_data_saved_ = true; | 365 all_data_saved_ = true; |
355 UpdateSize(size); | 366 UpdateSize(size); |
356 StopProgressTimer(); | 367 StopProgressTimer(); |
357 } | 368 } |
358 | 369 |
(...skipping 14 matching lines...) Expand all Loading... | |
373 is_temporary()) { | 384 is_temporary()) { |
374 // If the download is temporary, like in drag-and-drop, do not open it but | 385 // If the download is temporary, like in drag-and-drop, do not open it but |
375 // we still need to set it auto-opened so that it can be removed from the | 386 // we still need to set it auto-opened so that it can be removed from the |
376 // download shelf. | 387 // download shelf. |
377 if (!is_temporary()) | 388 if (!is_temporary()) |
378 OpenDownload(); | 389 OpenDownload(); |
379 auto_opened_ = true; | 390 auto_opened_ = true; |
380 } | 391 } |
381 | 392 |
382 DCHECK(all_data_saved_); | 393 DCHECK(all_data_saved_); |
383 state_ = COMPLETE; | 394 history_info_.state = COMPLETE; |
384 UpdateObservers(); | 395 UpdateObservers(); |
385 download_manager_->DownloadCompleted(id()); | 396 download_manager_->DownloadCompleted(id()); |
386 } | 397 } |
387 | 398 |
388 void DownloadItem::Interrupted(int64 size, int os_error) { | 399 void DownloadItem::Interrupted(int64 size, int os_error) { |
389 if (!IsInProgress()) | 400 if (!IsInProgress()) |
390 return; | 401 return; |
391 state_ = INTERRUPTED; | 402 history_info_.state = INTERRUPTED; |
392 last_os_error_ = os_error; | 403 last_os_error_ = os_error; |
393 UpdateSize(size); | 404 UpdateSize(size); |
394 StopProgressTimer(); | 405 StopProgressTimer(); |
395 UpdateObservers(); | 406 UpdateObservers(); |
396 } | 407 } |
397 | 408 |
398 void DownloadItem::Delete(DeleteReason reason) { | 409 void DownloadItem::Delete(DeleteReason reason) { |
399 switch (reason) { | 410 switch (reason) { |
400 case DELETE_DUE_TO_USER_DISCARD: | 411 case DELETE_DUE_TO_USER_DISCARD: |
401 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", danger_type_, | 412 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", CurrentDangerType(), |
402 DANGEROUS_TYPE_MAX); | 413 DANGEROUS_TYPE_MAX); |
403 break; | 414 break; |
404 case DELETE_DUE_TO_BROWSER_SHUTDOWN: | 415 case DELETE_DUE_TO_BROWSER_SHUTDOWN: |
405 UMA_HISTOGRAM_ENUMERATION("Download.Discard", danger_type_, | 416 UMA_HISTOGRAM_ENUMERATION("Download.Discard", CurrentDangerType(), |
406 DANGEROUS_TYPE_MAX); | 417 DANGEROUS_TYPE_MAX); |
407 break; | 418 break; |
408 default: | 419 default: |
409 NOTREACHED(); | 420 NOTREACHED(); |
410 } | 421 } |
411 | 422 |
412 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 423 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
413 NewRunnableFunction(&DeleteDownloadedFile, full_path_)); | 424 NewRunnableFunction(&DeleteDownloadedFile, history_info_.path)); |
414 Remove(); | 425 Remove(); |
415 // We have now been deleted. | 426 // We have now been deleted. |
416 } | 427 } |
417 | 428 |
418 void DownloadItem::Remove() { | 429 void DownloadItem::Remove() { |
419 Cancel(true); | 430 Cancel(true); |
420 state_ = REMOVING; | 431 history_info_.state = REMOVING; |
421 download_manager_->RemoveDownload(db_handle_); | 432 download_manager_->RemoveDownload(history_info_.db_handle); |
422 // We have now been deleted. | 433 // We have now been deleted. |
423 } | 434 } |
424 | 435 |
425 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const { | 436 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const { |
426 if (total_bytes_ <= 0) | 437 if (history_info_.total_bytes <= 0) |
427 return false; // We never received the content_length for this download. | 438 return false; // We never received the content_length for this download. |
428 | 439 |
429 int64 speed = CurrentSpeed(); | 440 int64 speed = CurrentSpeed(); |
430 if (speed == 0) | 441 if (speed == 0) |
431 return false; | 442 return false; |
432 | 443 |
433 *remaining = | 444 *remaining = base::TimeDelta::FromSeconds( |
434 base::TimeDelta::FromSeconds((total_bytes_ - received_bytes_) / speed); | 445 (history_info_.total_bytes - history_info_.received_bytes) / speed); |
435 return true; | 446 return true; |
436 } | 447 } |
437 | 448 |
438 int64 DownloadItem::CurrentSpeed() const { | 449 int64 DownloadItem::CurrentSpeed() const { |
439 if (is_paused_) | 450 if (is_paused_) |
440 return 0; | 451 return 0; |
441 base::TimeDelta diff = base::TimeTicks::Now() - start_tick_; | 452 base::TimeDelta diff = base::TimeTicks::Now() - start_tick_; |
442 int64 diff_ms = diff.InMilliseconds(); | 453 int64 diff_ms = diff.InMilliseconds(); |
443 return diff_ms == 0 ? 0 : received_bytes_ * 1000 / diff_ms; | 454 return diff_ms == 0 ? 0 : history_info_.received_bytes * 1000 / diff_ms; |
444 } | 455 } |
445 | 456 |
446 int DownloadItem::PercentComplete() const { | 457 int DownloadItem::PercentComplete() const { |
447 return (total_bytes_ > 0) ? | 458 return (history_info_.total_bytes > 0) ? |
448 static_cast<int>(received_bytes_ * 100.0 / total_bytes_) : -1; | 459 static_cast<int>(history_info_.received_bytes * 100.0 / |
460 history_info_.total_bytes) : | |
461 -1; | |
449 } | 462 } |
450 | 463 |
451 void DownloadItem::Rename(const FilePath& full_path) { | 464 void DownloadItem::Rename(const FilePath& full_path) { |
452 VLOG(20) << __FUNCTION__ << "()" | 465 VLOG(20) << __FUNCTION__ << "()" |
453 << " full_path = \"" << full_path.value() << "\"" | 466 << " full_path = \"" << full_path.value() << "\"" |
454 << " " << DebugString(true); | 467 << " " << DebugString(true); |
455 DCHECK(!full_path.empty()); | 468 DCHECK(!full_path.empty()); |
456 full_path_ = full_path; | 469 history_info_.path = full_path; |
457 } | 470 } |
458 | 471 |
459 void DownloadItem::TogglePause() { | 472 void DownloadItem::TogglePause() { |
460 DCHECK(IsInProgress()); | 473 DCHECK(IsInProgress()); |
461 download_manager_->PauseDownload(id_, !is_paused_); | 474 download_manager_->PauseDownload(history_info_.download_id, !is_paused_); |
462 is_paused_ = !is_paused_; | 475 is_paused_ = !is_paused_; |
463 UpdateObservers(); | 476 UpdateObservers(); |
464 } | 477 } |
465 | 478 |
466 void DownloadItem::OnDownloadCompleting(DownloadFileManager* file_manager) { | 479 void DownloadItem::OnDownloadCompleting(DownloadFileManager* file_manager) { |
467 VLOG(20) << __FUNCTION__ << "()" | 480 VLOG(20) << __FUNCTION__ << "()" |
468 << " needs rename = " << NeedsRename() | 481 << " needs rename = " << NeedsRename() |
469 << " " << DebugString(true); | 482 << " " << DebugString(true); |
470 DCHECK_NE(DANGEROUS, safety_state()); | 483 DCHECK_NE(DANGEROUS, safety_state()); |
471 DCHECK(file_manager); | 484 DCHECK(file_manager); |
472 | 485 |
473 if (NeedsRename()) { | 486 if (NeedsRename()) { |
474 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 487 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
475 NewRunnableMethod(file_manager, | 488 NewRunnableMethod(file_manager, |
476 &DownloadFileManager::RenameCompletingDownloadFile, id(), | 489 &DownloadFileManager::RenameCompletingDownloadFile, id(), |
477 GetTargetFilePath(), safety_state() == SAFE)); | 490 GetTargetFilePath(), safety_state() == SAFE)); |
478 return; | 491 return; |
479 } | 492 } |
480 | 493 |
481 Completed(); | 494 Completed(); |
482 | 495 |
483 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 496 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
484 NewRunnableMethod(file_manager, &DownloadFileManager::CompleteDownload, | 497 NewRunnableMethod(file_manager, &DownloadFileManager::CompleteDownload, |
485 id())); | 498 id())); |
486 } | 499 } |
487 | 500 |
488 void DownloadItem::OnDownloadRenamedToFinalName(const FilePath& full_path) { | 501 void DownloadItem::OnDownloadRenamedToFinalName(const FilePath& full_path) { |
489 VLOG(20) << __FUNCTION__ << "()" | 502 VLOG(20) << __FUNCTION__ << "()" |
490 << " full_path = " << full_path.value() | 503 << " full_path = \"" << full_path.value() << "\"" |
491 << " needed rename = " << NeedsRename() | 504 << " needed rename = " << NeedsRename() |
492 << " " << DebugString(false); | 505 << " " << DebugString(false); |
493 DCHECK(NeedsRename()); | 506 DCHECK(NeedsRename()); |
494 | 507 |
495 Rename(full_path); | 508 Rename(full_path); |
496 | 509 |
497 Completed(); | 510 Completed(); |
498 } | 511 } |
499 | 512 |
500 bool DownloadItem::MatchesQuery(const string16& query) const { | 513 bool DownloadItem::MatchesQuery(const string16& query) const { |
501 if (query.empty()) | 514 if (query.empty()) |
502 return true; | 515 return true; |
503 | 516 |
504 DCHECK_EQ(query, base::i18n::ToLower(query)); | 517 DCHECK_EQ(query, base::i18n::ToLower(query)); |
505 | 518 |
506 string16 url_raw(base::i18n::ToLower(UTF8ToUTF16(url().spec()))); | 519 string16 url_raw(base::i18n::ToLower(UTF8ToUTF16(GetUrl().spec()))); |
507 if (url_raw.find(query) != string16::npos) | 520 if (url_raw.find(query) != string16::npos) |
508 return true; | 521 return true; |
509 | 522 |
510 // TODO(phajdan.jr): write a test case for the following code. | 523 // TODO(phajdan.jr): write a test case for the following code. |
511 // A good test case would be: | 524 // A good test case would be: |
512 // "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", | 525 // "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", |
513 // L"/\x4f60\x597d\x4f60\x597d", | 526 // L"/\x4f60\x597d\x4f60\x597d", |
514 // "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD" | 527 // "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD" |
515 PrefService* prefs = download_manager_->profile()->GetPrefs(); | 528 PrefService* prefs = download_manager_->profile()->GetPrefs(); |
516 std::string languages(prefs->GetString(prefs::kAcceptLanguages)); | 529 std::string languages(prefs->GetString(prefs::kAcceptLanguages)); |
517 string16 url_formatted(base::i18n::ToLower(net::FormatUrl(url(), languages))); | 530 string16 url_formatted( |
531 base::i18n::ToLower(net::FormatUrl(GetUrl(), languages))); | |
518 if (url_formatted.find(query) != string16::npos) | 532 if (url_formatted.find(query) != string16::npos) |
519 return true; | 533 return true; |
520 | 534 |
521 string16 path(base::i18n::ToLower(full_path().LossyDisplayName())); | 535 string16 path(base::i18n::ToLower(full_path().LossyDisplayName())); |
522 // This shouldn't just do a substring match; it is wrong for Unicode | 536 // This shouldn't just do a substring match; it is wrong for Unicode |
523 // due to normalization and we have a fancier search-query system | 537 // due to normalization and we have a fancier search-query system |
524 // used elsewhere. | 538 // used elsewhere. |
525 // http://code.google.com/p/chromium/issues/detail?id=71982 | 539 // http://code.google.com/p/chromium/issues/detail?id=71982 |
526 return (path.find(query) != string16::npos); | 540 return (path.find(query) != string16::npos); |
527 } | 541 } |
528 | 542 |
529 void DownloadItem::SetFileCheckResults(const FilePath& path, | 543 void DownloadItem::SetFileCheckResults(const DownloadStateInfo& state) { |
530 bool is_dangerous_file, | 544 VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true); |
531 bool is_dangerous_url, | 545 manager_state_ = state; |
532 int path_uniquifier, | 546 VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true); |
533 bool prompt, | |
534 bool is_extension_install, | |
535 const FilePath& original_name) { | |
536 VLOG(20) << " " << __FUNCTION__ << "()" | |
537 << " path = \"" << path.value() << "\"" | |
538 << " is_dangerous_file = " << is_dangerous_file | |
539 << " is_dangerous_url = " << is_dangerous_url | |
540 << " path_uniquifier = " << path_uniquifier | |
541 << " prompt = " << prompt | |
542 << " is_extension_install = " << is_extension_install | |
543 << " path = \"" << path.value() << "\"" | |
544 << " original_name = \"" << original_name.value() << "\"" | |
545 << " " << DebugString(true); | |
546 // Make sure the initial file name is set only once. | |
547 DCHECK(full_path_.empty()); | |
548 DCHECK(!path.empty()); | |
549 | 547 |
550 full_path_ = path; | 548 safety_state_ = GetSafetyState(manager_state_.is_dangerous_file, |
551 safety_state_ = GetSafetyState(is_dangerous_file, is_dangerous_url); | 549 manager_state_.is_dangerous_url); |
552 danger_type_ = GetDangerType(is_dangerous_file, is_dangerous_url); | 550 } |
553 path_uniquifier_ = path_uniquifier; | |
554 save_as_ = prompt; | |
555 is_extension_install_ = is_extension_install; | |
556 target_name_ = original_name; | |
557 | 551 |
558 if (target_name_.value().empty()) | 552 void DownloadItem::UpdateTarget() { |
559 target_name_ = full_path_.BaseName(); | 553 if (manager_state_.target_name.value().empty()) |
554 manager_state_.target_name = history_info_.path.BaseName(); | |
555 } | |
556 | |
557 DownloadItem::DangerType DownloadItem::CurrentDangerType() const { | |
558 return GetDangerType(manager_state_.is_dangerous_file, | |
559 manager_state_.is_dangerous_url); | |
560 } | |
561 | |
562 bool DownloadItem::IsDangerous() const { | |
563 return CurrentDangerType() != DownloadItem::NOT_DANGEROUS; | |
564 } | |
565 | |
566 void DownloadItem::MarkUrlDangerous() { | |
567 manager_state_.is_dangerous_url = true; | |
560 } | 568 } |
561 | 569 |
562 FilePath DownloadItem::GetTargetFilePath() const { | 570 FilePath DownloadItem::GetTargetFilePath() const { |
563 return full_path_.DirName().Append(target_name_); | 571 return history_info_.path.DirName().Append(manager_state_.target_name); |
564 } | 572 } |
565 | 573 |
566 FilePath DownloadItem::GetFileNameToReportUser() const { | 574 FilePath DownloadItem::GetFileNameToReportUser() const { |
567 if (path_uniquifier_ > 0) { | 575 if (manager_state_.path_uniquifier > 0) { |
568 FilePath name(target_name_); | 576 FilePath name(manager_state_.target_name); |
569 download_util::AppendNumberToPath(&name, path_uniquifier_); | 577 download_util::AppendNumberToPath(&name, manager_state_.path_uniquifier); |
570 return name; | 578 return name; |
571 } | 579 } |
572 return target_name_; | 580 return manager_state_.target_name; |
573 } | 581 } |
574 | 582 |
575 FilePath DownloadItem::GetUserVerifiedFilePath() const { | 583 FilePath DownloadItem::GetUserVerifiedFilePath() const { |
576 return (safety_state_ == DownloadItem::SAFE) ? | 584 return (safety_state_ == DownloadItem::SAFE) ? |
577 GetTargetFilePath() : full_path_; | 585 GetTargetFilePath() : history_info_.path; |
578 } | 586 } |
579 | 587 |
580 void DownloadItem::Init(bool start_timer) { | 588 void DownloadItem::Init(bool start_timer) { |
581 if (target_name_.value().empty()) | 589 UpdateTarget(); |
582 target_name_ = full_path_.BaseName(); | |
583 if (start_timer) | 590 if (start_timer) |
584 StartProgressTimer(); | 591 StartProgressTimer(); |
585 VLOG(20) << __FUNCTION__ << "() " << DebugString(true); | 592 VLOG(20) << __FUNCTION__ << "() " << DebugString(true); |
586 } | 593 } |
587 | 594 |
588 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to | 595 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to |
589 // |IsPartialDownload()|, when resuming interrupted downloads is implemented. | 596 // |IsPartialDownload()|, when resuming interrupted downloads is implemented. |
590 bool DownloadItem::IsPartialDownload() const { | 597 bool DownloadItem::IsPartialDownload() const { |
591 return (state_ == IN_PROGRESS); | 598 return (history_info_.state == IN_PROGRESS); |
592 } | 599 } |
593 | 600 |
594 bool DownloadItem::IsInProgress() const { | 601 bool DownloadItem::IsInProgress() const { |
595 return (state_ == IN_PROGRESS); | 602 return (history_info_.state == IN_PROGRESS); |
596 } | 603 } |
597 | 604 |
598 bool DownloadItem::IsCancelled() const { | 605 bool DownloadItem::IsCancelled() const { |
599 return (state_ == CANCELLED) || (state_ == INTERRUPTED); | 606 return (history_info_.state == CANCELLED) || |
607 (history_info_.state == INTERRUPTED); | |
600 } | 608 } |
601 | 609 |
602 bool DownloadItem::IsInterrupted() const { | 610 bool DownloadItem::IsInterrupted() const { |
603 return (state_ == INTERRUPTED); | 611 return (history_info_.state == INTERRUPTED); |
604 } | 612 } |
605 | 613 |
606 bool DownloadItem::IsComplete() const { | 614 bool DownloadItem::IsComplete() const { |
607 return (state_ == COMPLETE); | 615 return (history_info_.state == COMPLETE); |
616 } | |
617 | |
618 // This function converts history_info_.state (which is an int32) to a | |
619 // DownloadItem::DownloadState enum, and returns it. | |
620 // A DCHECK() will be fire if it has an illegal value. | |
Paweł Hajdan Jr.
2011/05/20 09:04:42
nit: This is an implementation comment, please rem
ahendrickson
2011/05/20 18:31:25
Done.
| |
621 DownloadItem::DownloadState DownloadItem::state() const { | |
622 if ((history_info_.state < 0) || | |
Paweł Hajdan Jr.
2011/05/20 09:04:42
To make sure it works, could you explicitly initia
ahendrickson
2011/05/20 18:31:25
Done.
| |
623 (history_info_.state > MAX_DOWNLOAD_STATE)) { | |
Paweł Hajdan Jr.
2011/05/20 09:04:42
nit: Shouldn't it be >= MAX_DOWNLOAD_STATE? I thin
ahendrickson
2011/05/20 18:31:25
Done.
| |
624 NOTREACHED() << " state = " << history_info_.state; | |
625 return IN_PROGRESS; | |
626 } | |
627 return static_cast<DownloadState>(history_info_.state); | |
628 } | |
629 | |
630 const GURL& DownloadItem::GetUrl() const { | |
631 return history_info_.url_chain.empty() ? | |
632 GURL::EmptyGURL() : history_info_.url_chain.back(); | |
608 } | 633 } |
609 | 634 |
610 std::string DownloadItem::DebugString(bool verbose) const { | 635 std::string DownloadItem::DebugString(bool verbose) const { |
611 std::string description = base::StringPrintf( | 636 std::string description = |
612 "{ id_ = %d state = %s", id_, DebugDownloadStateString(state())); | 637 base::StringPrintf("{ id = %d" |
638 " state = %s", | |
639 history_info_.download_id, | |
640 DebugDownloadStateString(state())); | |
613 | 641 |
614 // Construct a string of the URL chain. | 642 // Construct a string of the URL chain. |
615 std::string url_list("<none>"); | 643 std::string url_list("<none>"); |
616 if (!url_chain_.empty()) { | 644 if (!history_info_.url_chain.empty()) { |
617 std::vector<GURL>::const_iterator iter = url_chain_.begin(); | 645 std::vector<GURL>::const_iterator iter = history_info_.url_chain.begin(); |
618 std::vector<GURL>::const_iterator last = url_chain_.end(); | 646 std::vector<GURL>::const_iterator last = history_info_.url_chain.end(); |
619 url_list = (*iter).spec(); | 647 url_list = (*iter).spec(); |
620 ++iter; | 648 ++iter; |
621 for ( ; verbose && (iter != last); ++iter) { | 649 for ( ; verbose && (iter != last); ++iter) { |
622 url_list += " -> "; | 650 url_list += " ->\n\t"; |
623 const GURL& next_url = *iter; | 651 const GURL& next_url = *iter; |
624 url_list += next_url.spec(); | 652 url_list += next_url.spec(); |
625 } | 653 } |
626 } | 654 } |
627 | 655 |
628 if (verbose) { | 656 if (verbose) { |
629 description += base::StringPrintf( | 657 description += base::StringPrintf( |
630 " db_handle = %" PRId64 | 658 " db_handle = %" PRId64 |
631 " total_bytes = %" PRId64 | 659 " total_bytes = %" PRId64 |
632 " is_paused = " "%c" | 660 " is_paused = %c" |
633 " is_extension_install = " "%c" | 661 " is_extension_install = %c" |
634 " is_otr = " "%c" | 662 " is_otr = %c" |
635 " safety_state = " "%s" | 663 " safety_state = %s" |
636 " url_chain = " "\"%s\"" | 664 " url_chain = \n\t\"%s\"\n\t" |
637 " target_name_ = \"%" PRFilePath "\"" | 665 " target_name = \"%" PRFilePath "\"" |
638 " full_path = \"%" PRFilePath "\"", | 666 " full_path = \"%" PRFilePath "\"", |
639 db_handle(), | 667 db_handle(), |
640 total_bytes(), | 668 total_bytes(), |
641 is_paused() ? 'T' : 'F', | 669 is_paused() ? 'T' : 'F', |
642 is_extension_install() ? 'T' : 'F', | 670 is_extension_install() ? 'T' : 'F', |
643 is_otr() ? 'T' : 'F', | 671 is_otr() ? 'T' : 'F', |
644 DebugSafetyStateString(safety_state()), | 672 DebugSafetyStateString(safety_state()), |
645 url_list.c_str(), | 673 url_list.c_str(), |
646 target_name_.value().c_str(), | 674 manager_state_.target_name.value().c_str(), |
647 full_path().value().c_str()); | 675 full_path().value().c_str()); |
648 } else { | 676 } else { |
649 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 677 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
650 } | 678 } |
679 | |
680 description += " }"; | |
681 | |
651 return description; | 682 return description; |
652 } | 683 } |
OLD | NEW |