OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/chrome_download_manager_delegate.h" | 5 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 29 matching lines...) Expand all Loading... | |
40 #include "content/public/browser/download_item.h" | 40 #include "content/public/browser/download_item.h" |
41 #include "content/public/browser/download_manager.h" | 41 #include "content/public/browser/download_manager.h" |
42 #include "content/public/browser/notification_source.h" | 42 #include "content/public/browser/notification_source.h" |
43 #include "content/public/browser/web_contents.h" | 43 #include "content/public/browser/web_contents.h" |
44 #include "content/public/browser/web_contents_delegate.h" | 44 #include "content/public/browser/web_contents_delegate.h" |
45 #include "content/public/browser/web_intents_dispatcher.h" | 45 #include "content/public/browser/web_intents_dispatcher.h" |
46 #include "grit/generated_resources.h" | 46 #include "grit/generated_resources.h" |
47 #include "net/base/net_util.h" | 47 #include "net/base/net_util.h" |
48 #include "ui/base/l10n/l10n_util.h" | 48 #include "ui/base/l10n/l10n_util.h" |
49 #include "webkit/glue/web_intent_data.h" | 49 #include "webkit/glue/web_intent_data.h" |
50 #include "webkit/glue/web_intent_reply_data.h" | |
50 | 51 |
51 #if !defined(OS_ANDROID) | 52 #if !defined(OS_ANDROID) |
52 #include "chrome/browser/ui/browser.h" | 53 #include "chrome/browser/ui/browser.h" |
53 #include "chrome/browser/ui/browser_finder.h" | 54 #include "chrome/browser/ui/browser_finder.h" |
54 #endif | 55 #endif |
55 | 56 |
56 #if defined(OS_CHROMEOS) | 57 #if defined(OS_CHROMEOS) |
57 #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" | 58 #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" |
58 #include "chrome/browser/chromeos/gdata/gdata_util.h" | 59 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
59 #include "chrome/browser/download/download_file_picker_chromeos.h" | 60 #include "chrome/browser/download/download_file_picker_chromeos.h" |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
365 return false; | 366 return false; |
366 } | 367 } |
367 | 368 |
368 return true; | 369 return true; |
369 } | 370 } |
370 | 371 |
371 bool ChromeDownloadManagerDelegate::ShouldOpenWithWebIntents( | 372 bool ChromeDownloadManagerDelegate::ShouldOpenWithWebIntents( |
372 const DownloadItem* item) { | 373 const DownloadItem* item) { |
373 if (!item->GetWebContents() || !item->GetWebContents()->GetDelegate()) | 374 if (!item->GetWebContents() || !item->GetWebContents()->GetDelegate()) |
374 return false; | 375 return false; |
376 if (!item->GetForcedFilePath().empty()) | |
377 return false; | |
375 | 378 |
376 std::string mime_type = item->GetMimeType(); | 379 std::string mime_type = item->GetMimeType(); |
377 if (mime_type == "application/rss+xml" || | 380 if (mime_type == "application/rss+xml" || |
378 mime_type == "application/atom+xml") { | 381 mime_type == "application/atom+xml") { |
379 return true; | 382 return true; |
380 } | 383 } |
381 | 384 |
382 #if defined(OS_CHROMEOS) | 385 #if defined(OS_CHROMEOS) |
383 if (mime_type == "application/msword" || | 386 if (mime_type == "application/msword" || |
384 mime_type == "application/vnd.ms-powerpoint" || | 387 mime_type == "application/vnd.ms-powerpoint" || |
385 mime_type == "application/vnd.ms-excel" || | 388 mime_type == "application/vnd.ms-excel" || |
386 mime_type == "application/vnd.openxmlformats-officedocument." | 389 mime_type == "application/vnd.openxmlformats-officedocument." |
387 "wordprocessingml.document" || | 390 "wordprocessingml.document" || |
388 mime_type == "application/vnd.openxmlformats-officedocument." | 391 mime_type == "application/vnd.openxmlformats-officedocument." |
389 "presentationml.presentation" || | 392 "presentationml.presentation" || |
390 mime_type == "application/vnd.openxmlformats-officedocument." | 393 mime_type == "application/vnd.openxmlformats-officedocument." |
391 "spreadsheetml.sheet") { | 394 "spreadsheetml.sheet") { |
392 return true; | 395 return true; |
393 } | 396 } |
394 #endif // defined(OS_CHROMEOS) | 397 #endif // defined(OS_CHROMEOS) |
395 | 398 |
396 return false; | 399 return false; |
397 } | 400 } |
398 | 401 |
402 // Needed to give PostTask a void closure in OnWebIntentDispatchCompleted. | |
403 void DeleteFile(const FilePath& file_path) { | |
asanka
2012/08/16 18:27:25
Nit: Move to anonymous namespace.
Greg Billock
2012/08/16 21:29:51
Done.
| |
404 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
405 file_util::Delete(file_path, false); | |
406 } | |
407 | |
408 // Called when the web intents dispatch has completed. Deletes the |file_path|. | |
409 void OnWebIntentDispatchCompleted( | |
asanka
2012/08/16 18:27:25
Nit: This as well.
Greg Billock
2012/08/16 21:29:51
Done.
| |
410 const FilePath& file_path, | |
411 webkit_glue::WebIntentReplyType intent_reply) { | |
412 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
413 base::Bind(&DeleteFile, file_path)); | |
414 } | |
415 | |
399 void ChromeDownloadManagerDelegate::OpenWithWebIntent( | 416 void ChromeDownloadManagerDelegate::OpenWithWebIntent( |
400 const DownloadItem* item) { | 417 const DownloadItem* item) { |
401 webkit_glue::WebIntentData intent_data( | 418 webkit_glue::WebIntentData intent_data( |
402 ASCIIToUTF16("http://webintents.org/view"), | 419 ASCIIToUTF16("http://webintents.org/view"), |
403 ASCIIToUTF16(item->GetMimeType()), | 420 ASCIIToUTF16(item->GetMimeType()), |
404 item->GetFullPath(), | 421 item->GetFullPath(), |
405 item->GetReceivedBytes()); | 422 item->GetReceivedBytes()); |
406 | 423 |
407 // RCH specifies that the receiver gets the url, but with Web Intents | 424 // RCH specifies that the receiver gets the url, but with Web Intents |
408 // it isn't really needed. | 425 // it isn't really needed. |
409 intent_data.extra_data.insert(make_pair( | 426 intent_data.extra_data.insert(make_pair( |
410 ASCIIToUTF16("url"), ASCIIToUTF16(item->GetURL().spec()))); | 427 ASCIIToUTF16("url"), ASCIIToUTF16(item->GetURL().spec()))); |
411 | 428 |
412 // Pass the downloaded filename to the service app as the name hint. | 429 // Pass the downloaded filename to the service app as the name hint. |
413 intent_data.extra_data.insert( | 430 string16 display_name = item->GetFileNameToReportUser().LossyDisplayName(); |
414 make_pair(ASCIIToUTF16("filename"), | 431 ReplaceFirstSubstringAfterOffset(&display_name, display_name.length() - 11, |
asanka
2012/08/16 18:27:25
See comment below about display names.
Greg Billock
2012/08/16 21:29:51
Done.
| |
415 item->GetFileNameToReportUser().LossyDisplayName())); | 432 ASCIIToUTF16(".webintents"), string16()); |
433 intent_data.extra_data.insert(make_pair(ASCIIToUTF16("filename"), | |
434 display_name)); | |
416 | 435 |
417 content::WebIntentsDispatcher* dispatcher = | 436 content::WebIntentsDispatcher* dispatcher = |
418 content::WebIntentsDispatcher::Create(intent_data); | 437 content::WebIntentsDispatcher::Create(intent_data); |
438 dispatcher->RegisterReplyNotification( | |
439 base::Bind(&OnWebIntentDispatchCompleted, item->GetFullPath())); | |
419 // TODO(gbillock): try to get this to be able to delegate to the Browser | 440 // TODO(gbillock): try to get this to be able to delegate to the Browser |
420 // object directly, passing a NULL WebContents? | 441 // object directly, passing a NULL WebContents? |
421 item->GetWebContents()->GetDelegate()->WebIntentDispatch( | 442 item->GetWebContents()->GetDelegate()->WebIntentDispatch( |
422 item->GetWebContents(), dispatcher); | 443 item->GetWebContents(), dispatcher); |
423 } | 444 } |
424 | 445 |
425 bool ChromeDownloadManagerDelegate::GenerateFileHash() { | 446 bool ChromeDownloadManagerDelegate::GenerateFileHash() { |
426 #if defined(ENABLE_SAFE_BROWSING) | 447 #if defined(ENABLE_SAFE_BROWSING) |
427 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && | 448 return profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled) && |
428 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); | 449 g_browser_process->safe_browsing_service()->DownloadBinHashNeeded(); |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
690 if (should_prompt && !last_download_path_.empty()) | 711 if (should_prompt && !last_download_path_.empty()) |
691 target_directory = last_download_path_; | 712 target_directory = last_download_path_; |
692 else | 713 else |
693 target_directory = download_prefs_->DownloadPath(); | 714 target_directory = download_prefs_->DownloadPath(); |
694 suggested_path = target_directory.Append(generated_name); | 715 suggested_path = target_directory.Append(generated_name); |
695 } else { | 716 } else { |
696 DCHECK(!should_prompt); | 717 DCHECK(!should_prompt); |
697 suggested_path = download->GetForcedFilePath(); | 718 suggested_path = download->GetForcedFilePath(); |
698 } | 719 } |
699 | 720 |
721 // If we will open the file with a web intents dispatch, | |
722 // give it a name that will not allow the OS to open it using usual | |
723 // associated apps. | |
724 if (ShouldOpenWithWebIntents(download)) { | |
725 suggested_path = suggested_path.AddExtension(".webintents"); | |
asanka
2012/08/16 18:27:25
I suggest calling download->SetDisplayName(suggest
asanka
2012/08/16 18:27:25
Also, consider using a named constant for the ".we
Greg Billock
2012/08/16 21:29:51
Done.
Greg Billock
2012/08/16 21:29:51
Got it. Yes. That's very nice.
On 2012/08/16 18:2
| |
726 } | |
727 | |
700 // If the download hasn't already been marked dangerous (could be | 728 // If the download hasn't already been marked dangerous (could be |
701 // DANGEROUS_URL), check if it is a dangerous file. | 729 // DANGEROUS_URL), check if it is a dangerous file. |
702 if (danger_type == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { | 730 if (danger_type == content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { |
703 if (!should_prompt && !is_forced_path && | 731 if (!should_prompt && !is_forced_path && |
704 IsDangerousFile(*download, suggested_path, visited_referrer_before)) { | 732 IsDangerousFile(*download, suggested_path, visited_referrer_before)) { |
705 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; | 733 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; |
706 } | 734 } |
707 | 735 |
708 #if defined(ENABLE_SAFE_BROWSING) | 736 #if defined(ENABLE_SAFE_BROWSING) |
709 DownloadProtectionService* service = GetDownloadProtectionService(); | 737 DownloadProtectionService* service = GetDownloadProtectionService(); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
829 int32 download_id, int64 db_handle) { | 857 int32 download_id, int64 db_handle) { |
830 // It's not immediately obvious, but HistoryBackend::CreateDownload() can | 858 // It's not immediately obvious, but HistoryBackend::CreateDownload() can |
831 // call this function with an invalid |db_handle|. For instance, this can | 859 // call this function with an invalid |db_handle|. For instance, this can |
832 // happen when the history database is offline. We cannot have multiple | 860 // happen when the history database is offline. We cannot have multiple |
833 // DownloadItems with the same invalid db_handle, so we need to assign a | 861 // DownloadItems with the same invalid db_handle, so we need to assign a |
834 // unique |db_handle| here. | 862 // unique |db_handle| here. |
835 if (db_handle == DownloadItem::kUninitializedHandle) | 863 if (db_handle == DownloadItem::kUninitializedHandle) |
836 db_handle = download_history_->GetNextFakeDbHandle(); | 864 db_handle = download_history_->GetNextFakeDbHandle(); |
837 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); | 865 download_manager_->OnItemAddedToPersistentStore(download_id, db_handle); |
838 } | 866 } |
OLD | NEW |