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

Side by Side Diff: chrome/browser/download/chrome_download_manager_delegate.cc

Issue 55063002: Prefer opening PDF downloads in the browser. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Tests + rebase Created 7 years, 1 month 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) 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/basictypes.h"
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
11 #include "base/callback.h" 12 #include "base/callback.h"
12 #include "base/file_util.h" 13 #include "base/file_util.h"
13 #include "base/prefs/pref_member.h" 14 #include "base/prefs/pref_member.h"
14 #include "base/prefs/pref_service.h" 15 #include "base/prefs/pref_service.h"
15 #include "base/rand_util.h" 16 #include "base/rand_util.h"
16 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
18 #include "base/time/time.h" 19 #include "base/time/time.h"
19 #include "chrome/browser/browser_process.h" 20 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/chrome_notification_types.h" 21 #include "chrome/browser/chrome_notification_types.h"
21 #include "chrome/browser/download/download_completion_blocker.h" 22 #include "chrome/browser/download/download_completion_blocker.h"
22 #include "chrome/browser/download/download_crx_util.h" 23 #include "chrome/browser/download/download_crx_util.h"
23 #include "chrome/browser/download/download_file_picker.h" 24 #include "chrome/browser/download/download_file_picker.h"
24 #include "chrome/browser/download/download_history.h" 25 #include "chrome/browser/download/download_history.h"
26 #include "chrome/browser/download/download_item_model.h"
25 #include "chrome/browser/download/download_path_reservation_tracker.h" 27 #include "chrome/browser/download/download_path_reservation_tracker.h"
26 #include "chrome/browser/download/download_prefs.h" 28 #include "chrome/browser/download/download_prefs.h"
27 #include "chrome/browser/download/download_service.h" 29 #include "chrome/browser/download/download_service.h"
28 #include "chrome/browser/download/download_service_factory.h" 30 #include "chrome/browser/download/download_service_factory.h"
29 #include "chrome/browser/download/download_target_determiner.h" 31 #include "chrome/browser/download/download_target_determiner.h"
30 #include "chrome/browser/download/save_package_file_picker.h" 32 #include "chrome/browser/download/save_package_file_picker.h"
31 #include "chrome/browser/extensions/api/downloads/downloads_api.h" 33 #include "chrome/browser/extensions/api/downloads/downloads_api.h"
32 #include "chrome/browser/extensions/crx_installer.h" 34 #include "chrome/browser/extensions/crx_installer.h"
33 #include "chrome/browser/platform_util.h" 35 #include "chrome/browser/platform_util.h"
34 #include "chrome/browser/profiles/profile.h" 36 #include "chrome/browser/profiles/profile.h"
35 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 37 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
38 #include "chrome/browser/ui/browser.h"
39 #include "chrome/browser/ui/browser_finder.h"
36 #include "chrome/common/chrome_constants.h" 40 #include "chrome/common/chrome_constants.h"
37 #include "chrome/common/pref_names.h" 41 #include "chrome/common/pref_names.h"
38 #include "components/user_prefs/pref_registry_syncable.h" 42 #include "components/user_prefs/pref_registry_syncable.h"
39 #include "content/public/browser/download_item.h" 43 #include "content/public/browser/download_item.h"
40 #include "content/public/browser/download_manager.h" 44 #include "content/public/browser/download_manager.h"
41 #include "content/public/browser/notification_source.h" 45 #include "content/public/browser/notification_source.h"
46 #include "content/public/browser/page_navigator.h"
42 #include "extensions/common/constants.h" 47 #include "extensions/common/constants.h"
48 #include "net/base/mime_util.h"
49 #include "net/base/net_util.h"
43 50
44 #if defined(OS_CHROMEOS) 51 #if defined(OS_CHROMEOS)
45 #include "chrome/browser/chromeos/drive/download_handler.h" 52 #include "chrome/browser/chromeos/drive/download_handler.h"
46 #include "chrome/browser/chromeos/drive/file_system_util.h" 53 #include "chrome/browser/chromeos/drive/file_system_util.h"
47 #endif 54 #endif
48 55
49 using content::BrowserThread; 56 using content::BrowserThread;
50 using content::DownloadItem; 57 using content::DownloadItem;
51 using content::DownloadManager; 58 using content::DownloadManager;
52 using safe_browsing::DownloadProtectionService; 59 using safe_browsing::DownloadProtectionService;
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 } else { 161 } else {
155 // If the URL is malicious, we'll use that as the danger type. The results 162 // If the URL is malicious, we'll use that as the danger type. The results
156 // of the content check, if one is performed, will be ignored. 163 // of the content check, if one is performed, will be ignored.
157 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; 164 danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL;
158 } 165 }
159 callback.Run(danger_type); 166 callback.Run(danger_type);
160 } 167 }
161 168
162 #endif // FULL_SAFE_BROWSING 169 #endif // FULL_SAFE_BROWSING
163 170
171 // Called on the blocking pool to determine the MIME type for |path|.
172 void GetMimeTypeAndReplyOnUIThread(
173 const base::FilePath& path,
174 const base::Callback<void(const std::string&)>& callback) {
Randy Smith (Not in Mondays) 2013/11/04 19:53:26 Worth an assertion that we're on the IO thread?
asanka 2013/11/04 21:20:11 This is actually called on the blocking pool. The
175 std::string mime_type;
176 net::GetMimeTypeFromFile(path, &mime_type);
177 BrowserThread::PostTask(
178 BrowserThread::UI, FROM_HERE, base::Bind(callback, mime_type));
179 }
180
181 bool IsOpenInBrowserPreferreredForFile(const base::FilePath& path,
182 const std::string& mime_type) {
183 if (net::IsSupportedImageMimeType(mime_type))
184 return true;
185
186 #if ENABLE_PLUGINS
187 if (path.MatchesExtension(FILE_PATH_LITERAL(".pdf")))
Randy Smith (Not in Mondays) 2013/11/04 19:53:26 Doesn't this duplicate the plugin information as t
asanka 2013/11/04 21:20:11 I'll fiddle with the names to make this clearer, b
188 return true;
189 #endif
190 return false;
191 }
192
164 } // namespace 193 } // namespace
165 194
166 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile) 195 ChromeDownloadManagerDelegate::ChromeDownloadManagerDelegate(Profile* profile)
167 : profile_(profile), 196 : profile_(profile),
168 next_download_id_(content::DownloadItem::kInvalidId), 197 next_download_id_(content::DownloadItem::kInvalidId),
169 download_prefs_(new DownloadPrefs(profile)) { 198 download_prefs_(new DownloadPrefs(profile)) {
170 } 199 }
171 200
172 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() { 201 ChromeDownloadManagerDelegate::~ChromeDownloadManagerDelegate() {
173 } 202 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 const content::DownloadIdCallback& callback) { 242 const content::DownloadIdCallback& callback) {
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
215 DCHECK(!profile_->IsOffTheRecord()); 244 DCHECK(!profile_->IsOffTheRecord());
216 DCHECK_NE(content::DownloadItem::kInvalidId, next_download_id_); 245 DCHECK_NE(content::DownloadItem::kInvalidId, next_download_id_);
217 callback.Run(next_download_id_++); 246 callback.Run(next_download_id_++);
218 } 247 }
219 248
220 bool ChromeDownloadManagerDelegate::DetermineDownloadTarget( 249 bool ChromeDownloadManagerDelegate::DetermineDownloadTarget(
221 DownloadItem* download, 250 DownloadItem* download,
222 const content::DownloadTargetCallback& callback) { 251 const content::DownloadTargetCallback& callback) {
252 DownloadTargetDeterminer::CompletionCallback target_determined_callback =
253 base::Bind(&ChromeDownloadManagerDelegate::OnDownloadTargetDetermined,
254 this,
255 download->GetId(),
256 callback);
223 DownloadTargetDeterminer::Start( 257 DownloadTargetDeterminer::Start(
224 download, 258 download,
225 GetPlatformDownloadPath( 259 GetPlatformDownloadPath(profile_, download, PLATFORM_TARGET_PATH),
226 profile_, download, PLATFORM_TARGET_PATH),
227 download_prefs_.get(), 260 download_prefs_.get(),
228 this, 261 this,
229 callback); 262 target_determined_callback);
230 return true; 263 return true;
231 } 264 }
232 265
233 bool ChromeDownloadManagerDelegate::ShouldOpenFileBasedOnExtension( 266 bool ChromeDownloadManagerDelegate::ShouldOpenFileBasedOnExtension(
234 const base::FilePath& path) { 267 const base::FilePath& path) {
235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
236 if (path.Extension().empty()) 269 if (path.Extension().empty())
237 return false; 270 return false;
238 // TODO(asanka): This determination is done based on |path|, while 271 // TODO(asanka): This determination is done based on |path|, while
239 // ShouldOpenDownload() detects extension downloads based on the 272 // ShouldOpenDownload() detects extension downloads based on the
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 // Deletes itself. 397 // Deletes itself.
365 new SavePackageFilePicker( 398 new SavePackageFilePicker(
366 web_contents, 399 web_contents,
367 suggested_path, 400 suggested_path,
368 default_extension, 401 default_extension,
369 can_save_as_complete, 402 can_save_as_complete,
370 download_prefs_.get(), 403 download_prefs_.get(),
371 callback); 404 callback);
372 } 405 }
373 406
407 void ChromeDownloadManagerDelegate::OpenDownloadUsingPlatformHandler(
408 DownloadItem* download) {
409 base::FilePath platform_path(
410 GetPlatformDownloadPath(profile_, download, PLATFORM_TARGET_PATH));
411 DCHECK(!platform_path.empty());
412 platform_util::OpenItem(platform_path);
413 }
414
374 void ChromeDownloadManagerDelegate::OpenDownload(DownloadItem* download) { 415 void ChromeDownloadManagerDelegate::OpenDownload(DownloadItem* download) {
375 DCHECK_EQ(DownloadItem::COMPLETE, download->GetState()); 416 DCHECK_EQ(DownloadItem::COMPLETE, download->GetState());
376 if (!download->CanOpenDownload()) 417 if (!download->CanOpenDownload())
377 return; 418 return;
378 base::FilePath platform_path( 419 #if !defined(OS_ANDROID)
Randy Smith (Not in Mondays) 2013/11/04 19:53:26 Is there a reason not to hide the ifdef in the Sho
asanka 2013/11/04 21:20:11 Moved to IsOpenInBrowserPreferredForFile.
379 GetPlatformDownloadPath(profile_, download, PLATFORM_TARGET_PATH)); 420 if (DownloadItemModel(download).ShouldPreferOpeningInBrowser()) {
Randy Smith (Not in Mondays) 2013/11/04 19:53:26 Suggestion: I'm tempted to suggest reversing the t
asanka 2013/11/04 21:20:11 Done.
380 DCHECK(!platform_path.empty()); 421 content::WebContents* web_contents = download->GetWebContents();
381 platform_util::OpenItem(platform_path); 422 Browser* browser =
423 web_contents ? chrome::FindBrowserWithWebContents(web_contents) : NULL;
424 if (!browser ||
425 !browser->CanSupportWindowFeature(Browser::FEATURE_TABSTRIP))
426 browser = chrome::FindTabbedBrowser(
427 profile_, true, chrome::GetActiveDesktop());
428 if (!browser)
429 browser = chrome::FindOrCreateTabbedBrowser(
430 profile_, chrome::GetActiveDesktop());
431 if (browser) {
432 content::OpenURLParams params(
433 net::FilePathToFileURL(download->GetTargetFilePath()),
434 content::Referrer(),
435 NEW_FOREGROUND_TAB,
436 content::PAGE_TRANSITION_LINK,
437 false);
438 browser->OpenURL(params);
439 return;
440 }
441 }
442 #endif
443 OpenDownloadUsingPlatformHandler(download);
382 } 444 }
383 445
384 void ChromeDownloadManagerDelegate::ShowDownloadInShell( 446 void ChromeDownloadManagerDelegate::ShowDownloadInShell(
385 DownloadItem* download) { 447 DownloadItem* download) {
386 if (!download->CanShowInFolder()) 448 if (!download->CanShowInFolder())
387 return; 449 return;
388 base::FilePath platform_path( 450 base::FilePath platform_path(
389 GetPlatformDownloadPath(profile_, download, PLATFORM_CURRENT_PATH)); 451 GetPlatformDownloadPath(profile_, download, PLATFORM_CURRENT_PATH));
390 DCHECK(!platform_path.empty()); 452 DCHECK(!platform_path.empty());
391 platform_util::ShowItemInFolder(platform_path); 453 platform_util::ShowItemInFolder(platform_path);
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 service->CheckDownloadUrl(*download, 578 service->CheckDownloadUrl(*download,
517 base::Bind(&CheckDownloadUrlDone, 579 base::Bind(&CheckDownloadUrlDone,
518 callback, 580 callback,
519 is_content_check_supported)); 581 is_content_check_supported));
520 return; 582 return;
521 } 583 }
522 #endif 584 #endif
523 callback.Run(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 585 callback.Run(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
524 } 586 }
525 587
588 void ChromeDownloadManagerDelegate::GetFileMimeType(
589 const base::FilePath& path,
590 const GetFileMimeTypeCallback& callback) {
591 BrowserThread::PostBlockingPoolTask(
592 FROM_HERE,
593 base::Bind(&GetMimeTypeAndReplyOnUIThread, path, callback));
594 }
595
526 #if defined(FULL_SAFE_BROWSING) 596 #if defined(FULL_SAFE_BROWSING)
527 void ChromeDownloadManagerDelegate::CheckClientDownloadDone( 597 void ChromeDownloadManagerDelegate::CheckClientDownloadDone(
528 uint32 download_id, 598 uint32 download_id,
529 DownloadProtectionService::DownloadCheckResult result) { 599 DownloadProtectionService::DownloadCheckResult result) {
530 DownloadItem* item = download_manager_->GetDownload(download_id); 600 DownloadItem* item = download_manager_->GetDownload(download_id);
531 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS)) 601 if (!item || (item->GetState() != DownloadItem::IN_PROGRESS))
532 return; 602 return;
533 603
534 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false) 604 VLOG(2) << __FUNCTION__ << "() download = " << item->DebugString(false)
535 << " verdict = " << result; 605 << " verdict = " << result;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
579 chrome::NOTIFICATION_CRX_INSTALLER_DONE, 649 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
580 source); 650 source);
581 651
582 scoped_refptr<extensions::CrxInstaller> installer = 652 scoped_refptr<extensions::CrxInstaller> installer =
583 content::Source<extensions::CrxInstaller>(source).ptr(); 653 content::Source<extensions::CrxInstaller>(source).ptr();
584 content::DownloadOpenDelayedCallback callback = 654 content::DownloadOpenDelayedCallback callback =
585 crx_installers_[installer.get()]; 655 crx_installers_[installer.get()];
586 crx_installers_.erase(installer.get()); 656 crx_installers_.erase(installer.get());
587 callback.Run(installer->did_handle_successfully()); 657 callback.Run(installer->did_handle_successfully());
588 } 658 }
659
660 void ChromeDownloadManagerDelegate::OnDownloadTargetDetermined(
661 int32 download_id,
662 const content::DownloadTargetCallback& callback,
663 scoped_ptr<DownloadTargetInfo> target_info) {
664 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
665 DownloadItem* item = download_manager_->GetDownload(download_id);
666 if (!target_info->target_path.empty() && item &&
667 IsOpenInBrowserPreferreredForFile(target_info->target_path,
668 target_info->mime_type) &&
669 target_info->is_filetype_handled_securely)
670 DownloadItemModel(item).SetShouldPreferOpeningInBrowser(true);
671 callback.Run(target_info->target_path,
672 target_info->target_disposition,
673 target_info->danger_type,
674 target_info->intermediate_path);
675 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698