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
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 "base/string16.h"
16#include "ui/gfx/native_widget_types.h"
17
18#if defined(TOOLKIT_VIEWS)
19#include "views/view.h"
20#endif
21
22namespace gfx {
23class Canvas;
24class Image;
25}
26
27class BaseDownloadItemModel;
28class DictionaryValue;
29class DownloadItem;
30class DownloadManager;
31class GURL;
32class Profile;
33class ResourceDispatcherHost;
34class SkBitmap;
35
36struct DownloadCreateInfo;
37struct DownloadSaveInfo;
38
39namespace net {
40class URLRequestContextGetter;
41}
42
43namespace download_util {
44
45// Download temporary file creation --------------------------------------------
46
47// Return the default download directory.
48const FilePath& GetDefaultDownloadDirectory();
49
50// Create a temporary file for a download in the user's default download
51// directory and return true if was successful in creating the file.
52bool CreateTemporaryFileForDownload(FilePath* path);
53
54// Return true if the |download_path| is dangerous path.
55bool DownloadPathIsDangerous(const FilePath& download_path);
56
57// Create an extension based on the file name and mime type.
58void GenerateExtension(const FilePath& file_name,
59                       const std::string& mime_type,
60                       FilePath::StringType* generated_extension);
61
62// Create a file name based on the response from the server.
63void GenerateFileNameFromInfo(DownloadCreateInfo* info,
64                              FilePath* generated_name);
65
66// Create a file name based on the response from the server.
67void GenerateFileName(const GURL& url,
68                      const std::string& content_disposition,
69                      const std::string& referrer_charset,
70                      const std::string& mime_type,
71                      FilePath* generated_name);
72
73// Used to make sure we have a safe file extension and filename for a
74// download.  |file_name| can either be just the file name or it can be a
75// full path to a file.
76void GenerateSafeFileName(const std::string& mime_type, FilePath* file_name);
77
78// Opens downloaded Chrome extension file (*.crx).
79void OpenChromeExtension(Profile* profile,
80                         DownloadManager* download_manager,
81                         const DownloadItem& download_item);
82
83// Download progress animations ------------------------------------------------
84
85// Arc sweep angle for use with downloads of unknown size
86const int kUnknownAngleDegrees = 50;
87
88// Rate of progress for use with downloads of unknown size
89const int kUnknownIncrementDegrees = 12;
90
91// Start angle for downloads with known size (midnight position)
92const int kStartAngleDegrees = -90;
93
94// A circle
95const int kMaxDegrees = 360;
96
97// Progress animation timer period, in milliseconds.
98const int kProgressRateMs = 150;
99
100// XP and Vista must support icons of this size.
101const int kSmallIconSize = 16;
102const int kBigIconSize = 32;
103
104// Our progress halo around the icon.
105int GetBigProgressIconSize();
106
107const int kSmallProgressIconSize = 39;
108const int kBigProgressIconSize = 52;
109
110// The offset required to center the icon in the progress bitmaps.
111int GetBigProgressIconOffset();
112
113const int kSmallProgressIconOffset =
114    (kSmallProgressIconSize - kSmallIconSize) / 2;
115
116enum PaintDownloadProgressSize {
117  SMALL = 0,
118  BIG
119};
120
121// We keep a count of how often various events occur in the
122// histogram "Download.Counts".
123enum DownloadCountTypes {
124  // The download was initiated by navigating to a URL (e.g. by user
125  // click).
126  INITIATED_BY_NAVIGATION_COUNT = 0,
127
128  // The download was initiated by invoking a context menu within a page.
129  INITIATED_BY_CONTEXT_MENU_COUNT,
130
131  // The download was initiated when the SavePackage system rejected
132  // a Save Page As ... by returning false from
133  // SavePackage::IsSaveableContents().
134  INITIATED_BY_SAVE_PACKAGE_FAILURE_COUNT,
135
136  // The download was initiated by a drag and drop from a drag-and-drop
137  // enabled web application.
138  INITIATED_BY_DRAG_N_DROP_COUNT,
139
140  // The download was initiated by explicit RPC from the renderer process
141  // (e.g. by Alt-click).
142  INITIATED_BY_RENDERER_COUNT,
143
144  // Downloads that made it to DownloadResourceHandler -- all of the
145  // above minus those blocked by DownloadThrottlingResourceHandler.
146  UNTHROTTLED_COUNT,
147
148  // Downloads that actually complete.
149  COMPLETED_COUNT,
150
151  // Downloads that are cancelled before completion (user action or error).
152  CANCELLED_COUNT,
153
154  DOWNLOAD_COUNT_TYPES_LAST_ENTRY
155};
156
157// Increment one of the above counts.
158void RecordDownloadCount(DownloadCountTypes type);
159
160// Paint the common download animation progress foreground and background,
161// clipping the foreground to 'percent' full. If percent is -1, then we don't
162// know the total size, so we just draw a rotating segment until we're done.
163//
164// |containing_view| is the View subclass within which the progress animation
165// is drawn (generally either DownloadItemTabView or DownloadItemView). We
166// require the containing View in addition to the canvas because if we are
167// drawing in a right-to-left locale, we need to mirror the position of the
168// progress animation within the containing View.
169void PaintDownloadProgress(gfx::Canvas* canvas,
170#if defined(TOOLKIT_VIEWS)
171                           views::View* containing_view,
172#endif
173                           int origin_x,
174                           int origin_y,
175                           int start_angle,
176                           int percent,
177                           PaintDownloadProgressSize size);
178
179void PaintDownloadComplete(gfx::Canvas* canvas,
180#if defined(TOOLKIT_VIEWS)
181                           views::View* containing_view,
182#endif
183                           int origin_x,
184                           int origin_y,
185                           double animation_progress,
186                           PaintDownloadProgressSize size);
187
188void PaintDownloadInterrupted(gfx::Canvas* canvas,
189#if defined(TOOLKIT_VIEWS)
190                              views::View* containing_view,
191#endif
192                              int origin_x,
193                              int origin_y,
194                              double animation_progress,
195                              PaintDownloadProgressSize size);
196
197// Drag support ----------------------------------------------------------------
198
199// Helper function for download views to use when acting as a drag source for a
200// DownloadItem. If |icon| is NULL, no image will be accompany the drag. |view|
201// is only required for Mac OS X, elsewhere it can be NULL.
202void DragDownload(const DownloadItem* download,
203                  gfx::Image* icon,
204                  gfx::NativeView view);
205
206// Helpers ---------------------------------------------------------------------
207
208// Creates a representation of a download in a format that the downloads
209// HTML page can understand.
210DictionaryValue* CreateDownloadItemValue(DownloadItem* download, int id);
211
212// Get the localized status text for an in-progress download.
213string16 GetProgressStatusText(DownloadItem* download);
214
215// Update the application icon to indicate overall download progress.
216// |download_count| is the number of downloads currently in progress. If
217// |progress_known| is false, then at least one download is of indeterminate
218// size and |progress| is invalid, otherwise |progress| indicates the overall
219// download progress (float value from 0..1).
220void UpdateAppIconDownloadProgress(int download_count,
221                                   bool progress_known,
222                                   float progress);
223
224// Appends the passed the number between parenthesis the path before the
225// extension.
226void AppendNumberToPath(FilePath* path, int number);
227
228// Attempts to find a number that can be appended to that path to make it
229// unique. If |path| does not exist, 0 is returned.  If it fails to find such
230// a number, -1 is returned.
231int GetUniquePathNumber(const FilePath& path);
232
233// Download the URL. Must be called on the IO thread.
234void DownloadUrl(const GURL& url,
235                 const GURL& referrer,
236                 const std::string& referrer_charset,
237                 const DownloadSaveInfo& save_info,
238                 ResourceDispatcherHost* rdh,
239                 int render_process_host_id,
240                 int render_view_id,
241                 net::URLRequestContextGetter* request_context_getter);
242
243// Tells the resource dispatcher host to cancel a download request.
244// Must be called on the IO thread.
245void CancelDownloadRequest(ResourceDispatcherHost* rdh,
246                           int render_process_id,
247                           int request_id);
248
249// Sends a notification on downloads being initiated
250// Must be called on the UI thread.
251void NotifyDownloadInitiated(int render_process_id, int render_view_id);
252
253// Same as GetUniquePathNumber, except that it also checks the existence
254// of its .crdownload intermediate path.
255// If |path| does not exist, 0 is returned.  If it fails to find such
256// a number, -1 is returned.
257int GetUniquePathNumberWithCrDownload(const FilePath& path);
258
259// Erases all downloaded files with the specified path and name prefix.
260// Used by download UI tests to clean up the download directory.
261void EraseUniqueDownloadFiles(const FilePath& path_prefix);
262
263// Returns a .crdownload intermediate path for the |suggested_path|.
264FilePath GetCrDownloadPath(const FilePath& suggested_path);
265
266// Returns true if this download should show the "dangerous file" warning.
267// Various factors are considered, such as the type of the file, whether a
268// user action initiated the download, and whether the user has explictly
269// marked the file type as "auto open".
270bool IsDangerous(DownloadCreateInfo* info, Profile* profile, bool auto_open);
271
272}  // namespace download_util
273
274#endif  // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_UTIL_H_
275