download_util.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4// 5// Download utilities. 6 7#ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_UTIL_H_ 8#define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_UTIL_H_ 9#pragma once 10 11#include <string> 12 13#include "base/basictypes.h" 14#include "base/file_path.h" 15#include "gfx/native_widget_types.h" 16 17#if defined(TOOLKIT_VIEWS) 18#include "views/view.h" 19#endif 20 21namespace gfx { 22class Canvas; 23} 24 25class BaseDownloadItemModel; 26class DictionaryValue; 27class DownloadItem; 28class DownloadManager; 29class GURL; 30class Profile; 31class ResourceDispatcherHost; 32class SkBitmap; 33class URLRequestContextGetter; 34 35struct DownloadCreateInfo; 36struct DownloadSaveInfo; 37 38namespace download_util { 39 40// Download temporary file creation -------------------------------------------- 41 42// Return the default download directory. 43const FilePath& GetDefaultDownloadDirectory(); 44 45// Create a temporary file for a download in the user's default download 46// directory and return true if was successful in creating the file. 47bool CreateTemporaryFileForDownload(FilePath* path); 48 49// Return true if the |download_path| is dangerous path. 50bool DownloadPathIsDangerous(const FilePath& download_path); 51 52// Create an extension based on the file name and mime type. 53void GenerateExtension(const FilePath& file_name, 54 const std::string& mime_type, 55 FilePath::StringType* generated_extension); 56 57// Create a file name based on the response from the server. 58void GenerateFileNameFromInfo(DownloadCreateInfo* info, 59 FilePath* generated_name); 60 61// Create a file name based on the response from the server. 62void GenerateFileName(const GURL& url, 63 const std::string& content_disposition, 64 const std::string& referrer_charset, 65 const std::string& mime_type, 66 FilePath* generated_name); 67 68// Used to make sure we have a safe file extension and filename for a 69// download. |file_name| can either be just the file name or it can be a 70// full path to a file. 71void GenerateSafeFileName(const std::string& mime_type, FilePath* file_name); 72 73// Opens downloaded Chrome extension file (*.crx). 74void OpenChromeExtension(Profile* profile, 75 DownloadManager* download_manager, 76 const DownloadItem& download_item); 77 78// Download progress animations ------------------------------------------------ 79 80// Arc sweep angle for use with downloads of unknown size 81const int kUnknownAngleDegrees = 50; 82 83// Rate of progress for use with downloads of unknown size 84const int kUnknownIncrementDegrees = 12; 85 86// Start angle for downloads with known size (midnight position) 87const int kStartAngleDegrees = -90; 88 89// A circle 90const int kMaxDegrees = 360; 91 92// Progress animation timer period, in milliseconds. 93const int kProgressRateMs = 150; 94 95// XP and Vista must support icons of this size. 96const int kSmallIconSize = 16; 97const int kBigIconSize = 32; 98 99// Our progress halo around the icon. 100int GetBigProgressIconSize(); 101 102const int kSmallProgressIconSize = 39; 103const int kBigProgressIconSize = 52; 104 105// The offset required to center the icon in the progress bitmaps. 106int GetBigProgressIconOffset(); 107 108const int kSmallProgressIconOffset = 109 (kSmallProgressIconSize - kSmallIconSize) / 2; 110 111enum PaintDownloadProgressSize { 112 SMALL = 0, 113 BIG 114}; 115 116// Paint the common download animation progress foreground and background, 117// clipping the foreground to 'percent' full. If percent is -1, then we don't 118// know the total size, so we just draw a rotating segment until we're done. 119// 120// |containing_view| is the View subclass within which the progress animation 121// is drawn (generally either DownloadItemTabView or DownloadItemView). We 122// require the containing View in addition to the canvas because if we are 123// drawing in a right-to-left locale, we need to mirror the position of the 124// progress animation within the containing View. 125void PaintDownloadProgress(gfx::Canvas* canvas, 126#if defined(TOOLKIT_VIEWS) 127 views::View* containing_view, 128#endif 129 int origin_x, 130 int origin_y, 131 int start_angle, 132 int percent, 133 PaintDownloadProgressSize size); 134 135void PaintDownloadComplete(gfx::Canvas* canvas, 136#if defined(TOOLKIT_VIEWS) 137 views::View* containing_view, 138#endif 139 int origin_x, 140 int origin_y, 141 double animation_progress, 142 PaintDownloadProgressSize size); 143 144// Drag support ---------------------------------------------------------------- 145 146// Helper function for download views to use when acting as a drag source for a 147// DownloadItem. If |icon| is NULL, no image will be accompany the drag. |view| 148// is only required for Mac OS X, elsewhere it can be NULL. 149void DragDownload(const DownloadItem* download, 150 SkBitmap* icon, 151 gfx::NativeView view); 152 153// Helpers --------------------------------------------------------------------- 154 155// Creates a representation of a download in a format that the downloads 156// HTML page can understand. 157DictionaryValue* CreateDownloadItemValue(DownloadItem* download, int id); 158 159// Get the localized status text for an in-progress download. 160std::wstring GetProgressStatusText(DownloadItem* download); 161 162// Update the application icon to indicate overall download progress. 163// |download_count| is the number of downloads currently in progress. If 164// |progress_known| is false, then at least one download is of indeterminate 165// size and |progress| is invalid, otherwise |progress| indicates the overall 166// download progress (float value from 0..1). 167void UpdateAppIconDownloadProgress(int download_count, 168 bool progress_known, 169 float progress); 170 171// Appends the passed the number between parenthesis the path before the 172// extension. 173void AppendNumberToPath(FilePath* path, int number); 174 175// Attempts to find a number that can be appended to that path to make it 176// unique. If |path| does not exist, 0 is returned. If it fails to find such 177// a number, -1 is returned. 178int GetUniquePathNumber(const FilePath& path); 179 180// Download the URL. Must be called on the IO thread. 181void DownloadUrl(const GURL& url, 182 const GURL& referrer, 183 const std::string& referrer_charset, 184 const DownloadSaveInfo& save_info, 185 ResourceDispatcherHost* rdh, 186 int render_process_host_id, 187 int render_view_id, 188 URLRequestContextGetter* request_context_getter); 189 190// Tells the resource dispatcher host to cancel a download request. 191// Must be called on the IO thread. 192void CancelDownloadRequest(ResourceDispatcherHost* rdh, 193 int render_process_id, 194 int request_id); 195 196// Same as GetUniquePathNumber, except that it also checks the existence 197// of its .crdownload intermediate path. 198// If |path| does not exist, 0 is returned. If it fails to find such 199// a number, -1 is returned. 200int GetUniquePathNumberWithCrDownload(const FilePath& path); 201 202// Erases all downloaded files with the specified path and name prefix. 203// Used by download UI tests to clean up the download directory. 204void EraseUniqueDownloadFiles(const FilePath& path_prefix); 205 206// Returns a .crdownload intermediate path for the |suggested_path|. 207FilePath GetCrDownloadPath(const FilePath& suggested_path); 208 209// Whether a given download should be considered potentially dangerous. 210bool IsDangerous(DownloadCreateInfo *info, Profile* profile); 211 212} // namespace download_util 213 214#endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_UTIL_H_ 215