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

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

Issue 6969009: Reduced the lifetime of DownloadCreateInfo. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Stupid clang! Created 9 years, 7 months 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
« no previous file with comments | « chrome/browser/download/download_util.h ('k') | chrome/browser/history/download_database.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // Download utility implementation 5 // Download utility implementation
6 6
7 #include "chrome/browser/download/download_util.h" 7 #include "chrome/browser/download/download_util.h"
8 8
9 #if defined(OS_WIN) 9 #if defined(OS_WIN)
10 #include <shobjidl.h> 10 #include <shobjidl.h>
11 #endif 11 #endif
12 #include <string> 12 #include <string>
13 13
14 #include "base/file_util.h" 14 #include "base/file_util.h"
15 #include "base/i18n/rtl.h" 15 #include "base/i18n/rtl.h"
16 #include "base/i18n/time_formatting.h" 16 #include "base/i18n/time_formatting.h"
17 #include "base/lazy_instance.h" 17 #include "base/lazy_instance.h"
18 #include "base/metrics/histogram.h" 18 #include "base/metrics/histogram.h"
19 #include "base/path_service.h" 19 #include "base/path_service.h"
20 #include "base/string16.h" 20 #include "base/string16.h"
21 #include "base/string_number_conversions.h" 21 #include "base/string_number_conversions.h"
22 #include "base/stringprintf.h" 22 #include "base/stringprintf.h"
23 #include "base/sys_string_conversions.h" 23 #include "base/sys_string_conversions.h"
24 #include "base/threading/thread_restrictions.h" 24 #include "base/threading/thread_restrictions.h"
25 #include "base/utf_string_conversions.h" 25 #include "base/utf_string_conversions.h"
26 #include "base/value_conversions.h" 26 #include "base/value_conversions.h"
27 #include "base/values.h" 27 #include "base/values.h"
28 #include "base/win/windows_version.h" 28 #include "base/win/windows_version.h"
29 #include "chrome/browser/download/download_create_info.h"
29 #include "chrome/browser/download/download_extensions.h" 30 #include "chrome/browser/download/download_extensions.h"
30 #include "chrome/browser/download/download_item.h" 31 #include "chrome/browser/download/download_item.h"
31 #include "chrome/browser/download/download_item_model.h" 32 #include "chrome/browser/download/download_item_model.h"
32 #include "chrome/browser/download/download_manager.h" 33 #include "chrome/browser/download/download_manager.h"
33 #include "chrome/browser/download/download_types.h" 34 #include "chrome/browser/download/download_types.h"
34 #include "chrome/browser/extensions/crx_installer.h" 35 #include "chrome/browser/extensions/crx_installer.h"
35 #include "chrome/browser/extensions/extension_install_ui.h" 36 #include "chrome/browser/extensions/extension_install_ui.h"
36 #include "chrome/browser/extensions/extension_service.h" 37 #include "chrome/browser/extensions/extension_service.h"
37 #include "chrome/browser/history/download_create_info.h"
38 #include "chrome/browser/profiles/profile.h" 38 #include "chrome/browser/profiles/profile.h"
39 #include "chrome/browser/ui/browser.h" 39 #include "chrome/browser/ui/browser.h"
40 #include "chrome/common/chrome_paths.h" 40 #include "chrome/common/chrome_paths.h"
41 #include "chrome/common/time_format.h" 41 #include "chrome/common/time_format.h"
42 #include "content/browser/browser_thread.h" 42 #include "content/browser/browser_thread.h"
43 #include "content/browser/renderer_host/render_view_host.h" 43 #include "content/browser/renderer_host/render_view_host.h"
44 #include "content/browser/renderer_host/resource_dispatcher_host.h" 44 #include "content/browser/renderer_host/resource_dispatcher_host.h"
45 #include "content/browser/tab_contents/tab_contents.h" 45 #include "content/browser/tab_contents/tab_contents.h"
46 #include "content/common/notification_service.h" 46 #include "content/common/notification_service.h"
47 #include "grit/generated_resources.h" 47 #include "grit/generated_resources.h"
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 // The GetPreferredExtensionForMimeType call will end up going to disk. Do 274 // The GetPreferredExtensionForMimeType call will end up going to disk. Do
275 // this on another thread to avoid slowing the IO thread. 275 // this on another thread to avoid slowing the IO thread.
276 // http://crbug.com/61827 276 // http://crbug.com/61827
277 base::ThreadRestrictions::ScopedAllowIO allow_io; 277 base::ThreadRestrictions::ScopedAllowIO allow_io;
278 net::GetPreferredExtensionForMimeType(mime_type, &extension); 278 net::GetPreferredExtensionForMimeType(mime_type, &extension);
279 } 279 }
280 280
281 generated_extension->swap(extension); 281 generated_extension->swap(extension);
282 } 282 }
283 283
284 void GenerateFileNameFromInfo(DownloadCreateInfo* info, 284 void GenerateFileNameFromRequest(const GURL& url,
285 FilePath* generated_name) { 285 const std::string& content_disposition,
286 GenerateFileNameInternal(GURL(info->url()), info->content_disposition, 286 const std::string& referrer_charset,
287 info->referrer_charset, std::string(), 287 const std::string& mime_type,
288 info->mime_type, generated_name); 288 FilePath* generated_name) {
289 GenerateFileNameInternal(url,
290 content_disposition,
291 referrer_charset,
292 std::string(),
293 mime_type,
294 generated_name);
289 } 295 }
290 296
291 void GenerateFileNameFromSuggestedName(const GURL& url, 297 void GenerateFileNameFromSuggestedName(const GURL& url,
292 const std::string& suggested_name, 298 const std::string& suggested_name,
293 const std::string& mime_type, 299 const std::string& mime_type,
294 FilePath* generated_name) { 300 FilePath* generated_name) {
295 GenerateFileNameInternal(url, std::string(), std::string(), 301 GenerateFileNameInternal(url, std::string(), std::string(),
296 suggested_name, mime_type, generated_name); 302 suggested_name, mime_type, generated_name);
297 } 303 }
298 304
(...skipping 30 matching lines...) Expand all
329 335
330 void OpenChromeExtension(Profile* profile, 336 void OpenChromeExtension(Profile* profile,
331 DownloadManager* download_manager, 337 DownloadManager* download_manager,
332 const DownloadItem& download_item) { 338 const DownloadItem& download_item) {
333 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
334 DCHECK(download_item.is_extension_install()); 340 DCHECK(download_item.is_extension_install());
335 341
336 ExtensionService* service = profile->GetExtensionService(); 342 ExtensionService* service = profile->GetExtensionService();
337 CHECK(service); 343 CHECK(service);
338 NotificationService* nservice = NotificationService::current(); 344 NotificationService* nservice = NotificationService::current();
339 GURL nonconst_download_url = download_item.url(); 345 GURL nonconst_download_url = download_item.GetURL();
340 nservice->Notify(NotificationType::EXTENSION_READY_FOR_INSTALL, 346 nservice->Notify(NotificationType::EXTENSION_READY_FOR_INSTALL,
341 Source<DownloadManager>(download_manager), 347 Source<DownloadManager>(download_manager),
342 Details<GURL>(&nonconst_download_url)); 348 Details<GURL>(&nonconst_download_url));
343 349
344 scoped_refptr<CrxInstaller> installer( 350 scoped_refptr<CrxInstaller> installer(
345 service->MakeCrxInstaller(new ExtensionInstallUI(profile))); 351 service->MakeCrxInstaller(new ExtensionInstallUI(profile)));
346 installer->set_delete_source(true); 352 installer->set_delete_source(true);
347 353
348 if (UserScript::IsURLUserScript(download_item.url(), 354 if (UserScript::IsURLUserScript(download_item.GetURL(),
349 download_item.mime_type())) { 355 download_item.mime_type())) {
350 installer->InstallUserScript(download_item.full_path(), 356 installer->InstallUserScript(download_item.full_path(),
351 download_item.url()); 357 download_item.GetURL());
352 return; 358 return;
353 } 359 }
354 360
355 bool is_gallery_download = service->IsDownloadFromGallery( 361 bool is_gallery_download = service->IsDownloadFromGallery(
356 download_item.url(), download_item.referrer_url()); 362 download_item.GetURL(), download_item.referrer_url());
357 installer->set_original_mime_type(download_item.original_mime_type()); 363 installer->set_original_mime_type(download_item.original_mime_type());
358 installer->set_apps_require_extension_mime_type(true); 364 installer->set_apps_require_extension_mime_type(true);
359 installer->set_original_url(download_item.url()); 365 installer->set_original_url(download_item.GetURL());
360 installer->set_is_gallery_install(is_gallery_download); 366 installer->set_is_gallery_install(is_gallery_download);
361 installer->set_allow_silent_install(is_gallery_download); 367 installer->set_allow_silent_install(is_gallery_download);
362 installer->set_install_cause(extension_misc::INSTALL_CAUSE_USER_DOWNLOAD); 368 installer->set_install_cause(extension_misc::INSTALL_CAUSE_USER_DOWNLOAD);
363 installer->InstallCrx(download_item.full_path()); 369 installer->InstallCrx(download_item.full_path());
364 } 370 }
365 371
366 void RecordDownloadCount(DownloadCountTypes type) { 372 void RecordDownloadCount(DownloadCountTypes type) {
367 UMA_HISTOGRAM_ENUMERATION( 373 UMA_HISTOGRAM_ENUMERATION(
368 "Download.Counts", type, DOWNLOAD_COUNT_TYPES_LAST_ENTRY); 374 "Download.Counts", type, DOWNLOAD_COUNT_TYPES_LAST_ENTRY);
369 } 375 }
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
635 TimeFormat::RelativeDate(download->start_time(), NULL)); 641 TimeFormat::RelativeDate(download->start_time(), NULL));
636 file_value->SetString("date_string", 642 file_value->SetString("date_string",
637 base::TimeFormatShortDate(download->start_time())); 643 base::TimeFormatShortDate(download->start_time()));
638 file_value->SetInteger("id", id); 644 file_value->SetInteger("id", id);
639 file_value->Set("file_path", 645 file_value->Set("file_path",
640 base::CreateFilePathValue(download->GetTargetFilePath())); 646 base::CreateFilePathValue(download->GetTargetFilePath()));
641 // Keep file names as LTR. 647 // Keep file names as LTR.
642 string16 file_name = download->GetFileNameToReportUser().LossyDisplayName(); 648 string16 file_name = download->GetFileNameToReportUser().LossyDisplayName();
643 file_name = base::i18n::GetDisplayStringInLTRDirectionality(file_name); 649 file_name = base::i18n::GetDisplayStringInLTRDirectionality(file_name);
644 file_value->SetString("file_name", file_name); 650 file_value->SetString("file_name", file_name);
645 file_value->SetString("url", download->url().spec()); 651 file_value->SetString("url", download->GetURL().spec());
646 file_value->SetBoolean("otr", download->is_otr()); 652 file_value->SetBoolean("otr", download->is_otr());
647 653
648 if (download->IsInProgress()) { 654 if (download->IsInProgress()) {
649 if (download->safety_state() == DownloadItem::DANGEROUS) { 655 if (download->safety_state() == DownloadItem::DANGEROUS) {
650 file_value->SetString("state", "DANGEROUS"); 656 file_value->SetString("state", "DANGEROUS");
651 DCHECK(download->danger_type() == DownloadItem::DANGEROUS_FILE || 657 DCHECK(download->GetDangerType() == DownloadItem::DANGEROUS_FILE ||
652 download->danger_type() == DownloadItem::DANGEROUS_URL); 658 download->GetDangerType() == DownloadItem::DANGEROUS_URL);
653 const char* danger_type_value = 659 const char* danger_type_value =
654 download->danger_type() == DownloadItem::DANGEROUS_FILE ? 660 download->GetDangerType() == DownloadItem::DANGEROUS_FILE ?
655 "DANGEROUS_FILE" : "DANGEROUS_URL"; 661 "DANGEROUS_FILE" : "DANGEROUS_URL";
656 file_value->SetString("danger_type", danger_type_value); 662 file_value->SetString("danger_type", danger_type_value);
657 } else if (download->is_paused()) { 663 } else if (download->is_paused()) {
658 file_value->SetString("state", "PAUSED"); 664 file_value->SetString("state", "PAUSED");
659 } else { 665 } else {
660 file_value->SetString("state", "IN_PROGRESS"); 666 file_value->SetString("state", "IN_PROGRESS");
661 } 667 }
662 668
663 file_value->SetString("progress_status_text", 669 file_value->SetString("progress_status_text",
664 GetProgressStatusText(download)); 670 GetProgressStatusText(download));
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
900 906
901 FilePath GetCrDownloadPath(const FilePath& suggested_path) { 907 FilePath GetCrDownloadPath(const FilePath& suggested_path) {
902 FilePath::StringType file_name; 908 FilePath::StringType file_name;
903 base::SStringPrintf( 909 base::SStringPrintf(
904 &file_name, 910 &file_name,
905 PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"), 911 PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"),
906 suggested_path.value().c_str()); 912 suggested_path.value().c_str());
907 return FilePath(file_name); 913 return FilePath(file_name);
908 } 914 }
909 915
910 // TODO(erikkay,phajdan.jr): This is apparently not being exercised in tests.
911 bool IsDangerous(DownloadCreateInfo* info, Profile* profile, bool auto_open) {
912 DownloadDangerLevel danger_level = GetFileDangerLevel(
913 info->suggested_path.BaseName());
914 if (danger_level == Dangerous)
915 return !(auto_open && info->has_user_gesture);
916 if (danger_level == AllowOnUserGesture && !info->has_user_gesture)
917 return true;
918 if (info->is_extension_install) {
919 // Extensions that are not from the gallery are considered dangerous.
920 ExtensionService* service = profile->GetExtensionService();
921 if (!service ||
922 !service->IsDownloadFromGallery(info->url(), info->referrer_url))
923 return true;
924 }
925 return false;
926 }
927
928 } // namespace download_util 916 } // namespace download_util
OLDNEW
« no previous file with comments | « chrome/browser/download/download_util.h ('k') | chrome/browser/history/download_database.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698