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

Side by Side Diff: chrome/browser/download/download_manager.h

Issue 351029: Support dragging a virtual file out of the browser. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 11 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_file.cc ('k') | chrome/browser/download/download_manager.cc » ('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) 2006-2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2009 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 // The DownloadManager object manages the process of downloading, including 5 // The DownloadManager object manages the process of downloading, including
6 // updates to the history system and providing the information for displaying 6 // updates to the history system and providing the information for displaying
7 // the downloads view in the Destinations tab. There is one DownloadManager per 7 // the downloads view in the Destinations tab. There is one DownloadManager per
8 // active profile in Chrome. 8 // active profile in Chrome.
9 // 9 //
10 // Each download is represented by a DownloadItem, and all DownloadItems 10 // Each download is represented by a DownloadItem, and all DownloadItems
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 DANGEROUS, 88 DANGEROUS,
89 DANGEROUS_BUT_VALIDATED // Dangerous but the user confirmed the download. 89 DANGEROUS_BUT_VALIDATED // Dangerous but the user confirmed the download.
90 }; 90 };
91 91
92 // Interface that observers of a particular download must implement in order 92 // Interface that observers of a particular download must implement in order
93 // to receive updates to the download's status. 93 // to receive updates to the download's status.
94 class Observer { 94 class Observer {
95 public: 95 public:
96 virtual void OnDownloadUpdated(DownloadItem* download) = 0; 96 virtual void OnDownloadUpdated(DownloadItem* download) = 0;
97 97
98 // Called when a downloaded file has been completed.
99 virtual void OnDownloadFileCompleted(DownloadItem* download) = 0;
100
98 // Called when a downloaded file has been opened. 101 // Called when a downloaded file has been opened.
99 virtual void OnDownloadOpened(DownloadItem* download) = 0; 102 virtual void OnDownloadOpened(DownloadItem* download) = 0;
100 103
101 protected: 104 protected:
102 virtual ~Observer() {} 105 virtual ~Observer() {}
103 }; 106 };
104 107
105 // Constructing from persistent store: 108 // Constructing from persistent store:
106 explicit DownloadItem(const DownloadCreateInfo& info); 109 explicit DownloadItem(const DownloadCreateInfo& info);
107 110
108 // Constructing from user action: 111 // Constructing from user action:
109 DownloadItem(int32 download_id, 112 DownloadItem(int32 download_id,
110 const FilePath& path, 113 const FilePath& path,
111 int path_uniquifier, 114 int path_uniquifier,
112 const GURL& url, 115 const GURL& url,
113 const GURL& referrer_url, 116 const GURL& referrer_url,
114 const std::string& mime_type, 117 const std::string& mime_type,
115 const FilePath& original_name, 118 const FilePath& original_name,
116 const base::Time start_time, 119 const base::Time start_time,
117 int64 download_size, 120 int64 download_size,
118 int render_process_id, 121 int render_process_id,
119 int request_id, 122 int request_id,
120 bool is_dangerous, 123 bool is_dangerous,
121 bool save_as, 124 bool save_as,
122 bool is_extension_install); 125 bool is_extension_install,
126 bool is_temporary);
123 127
124 ~DownloadItem(); 128 ~DownloadItem();
125 129
126 void Init(bool start_timer); 130 void Init(bool start_timer);
127 131
128 // Public API 132 // Public API
129 133
130 void AddObserver(Observer* observer); 134 void AddObserver(Observer* observer);
131 void RemoveObserver(Observer* observer); 135 void RemoveObserver(Observer* observer);
132 136
133 // Notifies our observers periodically. 137 // Notifies our observers periodically.
134 void UpdateObservers(); 138 void UpdateObservers();
135 139
140 // Notifies our observers the downloaded file has been completed.
141 void NotifyObserversDownloadFileCompleted();
142
136 // Notifies our observers the downloaded file has been opened. 143 // Notifies our observers the downloaded file has been opened.
137 void NotifyObserversDownloadOpened(); 144 void NotifyObserversDownloadOpened();
138 145
139 // Received a new chunk of data 146 // Received a new chunk of data
140 void Update(int64 bytes_so_far); 147 void Update(int64 bytes_so_far);
141 148
142 // Cancel the download operation. We need to distinguish between cancels at 149 // Cancel the download operation. We need to distinguish between cancels at
143 // exit (DownloadManager destructor) from user interface initiated cancels 150 // exit (DownloadManager destructor) from user interface initiated cancels
144 // because at exit, the history system may not exist, and any updates to it 151 // because at exit, the history system may not exist, and any updates to it
145 // require AddRef'ing the DownloadManager in the destructor which results in 152 // require AddRef'ing the DownloadManager in the destructor which results in
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 SafetyState safety_state() const { return safety_state_; } 217 SafetyState safety_state() const { return safety_state_; }
211 void set_safety_state(SafetyState safety_state) { 218 void set_safety_state(SafetyState safety_state) {
212 safety_state_ = safety_state; 219 safety_state_ = safety_state;
213 } 220 }
214 bool auto_opened() { return auto_opened_; } 221 bool auto_opened() { return auto_opened_; }
215 void set_auto_opened(bool auto_opened) { auto_opened_ = auto_opened; } 222 void set_auto_opened(bool auto_opened) { auto_opened_ = auto_opened; }
216 FilePath original_name() const { return original_name_; } 223 FilePath original_name() const { return original_name_; }
217 void set_original_name(const FilePath& name) { original_name_ = name; } 224 void set_original_name(const FilePath& name) { original_name_ = name; }
218 bool save_as() const { return save_as_; } 225 bool save_as() const { return save_as_; }
219 bool is_extension_install() const { return is_extension_install_; } 226 bool is_extension_install() const { return is_extension_install_; }
227 bool name_finalized() const { return name_finalized_; }
228 void set_name_finalized(bool name_finalized) {
229 name_finalized_ = name_finalized;
230 }
231 bool is_temporary() const { return is_temporary_; }
232 void set_is_temporary(bool is_temporary) { is_temporary_ = is_temporary; }
220 233
221 // Returns the file-name that should be reported to the user, which is 234 // Returns the file-name that should be reported to the user, which is
222 // file_name_ for safe downloads and original_name_ for dangerous ones with 235 // file_name_ for safe downloads and original_name_ for dangerous ones with
223 // the uniquifier number. 236 // the uniquifier number.
224 FilePath GetFileName() const; 237 FilePath GetFileName() const;
225 238
226 private: 239 private:
227 // Internal helper for maintaining consistent received and total sizes. 240 // Internal helper for maintaining consistent received and total sizes.
228 void UpdateSize(int64 size); 241 void UpdateSize(int64 size);
229 242
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 // For canceling or pausing requests. 311 // For canceling or pausing requests.
299 int render_process_id_; 312 int render_process_id_;
300 int request_id_; 313 int request_id_;
301 314
302 // True if the item was downloaded as a result of 'save as...' 315 // True if the item was downloaded as a result of 'save as...'
303 bool save_as_; 316 bool save_as_;
304 317
305 // True if the item was downloaded for an extension installation. 318 // True if the item was downloaded for an extension installation.
306 bool is_extension_install_; 319 bool is_extension_install_;
307 320
321 // True if the filename is finalized.
322 bool name_finalized_;
323
324 // True if the item was downloaded temporarily.
325 bool is_temporary_;
326
308 DISALLOW_COPY_AND_ASSIGN(DownloadItem); 327 DISALLOW_COPY_AND_ASSIGN(DownloadItem);
309 }; 328 };
310 329
311 330
312 // DownloadManager ------------------------------------------------------------- 331 // DownloadManager -------------------------------------------------------------
313 332
314 // Browser's download manager: manages all downloads and destination view. 333 // Browser's download manager: manages all downloads and destination view.
315 class DownloadManager : public base::RefCountedThreadSafe<DownloadManager>, 334 class DownloadManager : public base::RefCountedThreadSafe<DownloadManager>,
316 public SelectFileDialog::Listener { 335 public SelectFileDialog::Listener {
317 // For testing. 336 // For testing.
(...skipping 22 matching lines...) Expand all
340 }; 359 };
341 360
342 // Public API 361 // Public API
343 362
344 // Begin a search for all downloads matching 'search_text'. If 'search_text' 363 // Begin a search for all downloads matching 'search_text'. If 'search_text'
345 // is empty, return all known downloads. The results are returned in the 364 // is empty, return all known downloads. The results are returned in the
346 // 'SetDownloads' observer callback. 365 // 'SetDownloads' observer callback.
347 void GetDownloads(Observer* observer, 366 void GetDownloads(Observer* observer,
348 const std::wstring& search_text); 367 const std::wstring& search_text);
349 368
369 // Return all temporary downloads that reside in the specified directory.
370 void GetTemporaryDownloads(Observer* observer,
371 const FilePath& dir_path);
372
350 // Returns true if initialized properly. 373 // Returns true if initialized properly.
351 bool Init(Profile* profile); 374 bool Init(Profile* profile);
352 375
353 // Schedule a query of the history service to retrieve all downloads. 376 // Schedule a query of the history service to retrieve all downloads.
354 void QueryHistoryForDownloads(); 377 void QueryHistoryForDownloads();
355 378
356 // Notifications sent from the download thread to the UI thread 379 // Notifications sent from the download thread to the UI thread
357 void StartDownload(DownloadCreateInfo* info); 380 void StartDownload(DownloadCreateInfo* info);
358 void UpdateDownload(int32 download_id, int64 size); 381 void UpdateDownload(int32 download_id, int64 size);
359 void DownloadFinished(int32 download_id, int64 size); 382 void DownloadFinished(int32 download_id, int64 size);
(...skipping 20 matching lines...) Expand all
380 // Remove all downloads will delete all downloads. The number of downloads 403 // Remove all downloads will delete all downloads. The number of downloads
381 // deleted is returned back to the caller. 404 // deleted is returned back to the caller.
382 int RemoveAllDownloads(); 405 int RemoveAllDownloads();
383 406
384 // Download the object at the URL. Used in cases such as "Save Link As..." 407 // Download the object at the URL. Used in cases such as "Save Link As..."
385 void DownloadUrl(const GURL& url, 408 void DownloadUrl(const GURL& url,
386 const GURL& referrer, 409 const GURL& referrer,
387 const std::string& referrer_encoding, 410 const std::string& referrer_encoding,
388 TabContents* tab_contents); 411 TabContents* tab_contents);
389 412
413 // Download the object at the URL and save it to the specified path. The
414 // download is treated as the temporary download and thus will not appear
415 // in the download history. Used in cases such as drag and drop.
416 void DownloadUrlToFile(const GURL& url,
417 const GURL& referrer,
418 const std::string& referrer_encoding,
419 const FilePath& save_file_path,
420 TabContents* tab_contents);
421
390 // Allow objects to observe the download creation process. 422 // Allow objects to observe the download creation process.
391 void AddObserver(Observer* observer); 423 void AddObserver(Observer* observer);
392 424
393 // Remove a download observer from ourself. 425 // Remove a download observer from ourself.
394 void RemoveObserver(Observer* observer); 426 void RemoveObserver(Observer* observer);
395 427
396 // Methods called on completion of a query sent to the history system. 428 // Methods called on completion of a query sent to the history system.
397 void OnQueryDownloadEntriesComplete( 429 void OnQueryDownloadEntriesComplete(
398 std::vector<DownloadCreateInfo>* entries); 430 std::vector<DownloadCreateInfo>* entries);
399 void OnCreateDownloadEntryComplete(DownloadCreateInfo info, int64 db_handle); 431 void OnCreateDownloadEntryComplete(DownloadCreateInfo info, int64 db_handle);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 // Tests if a file type should be opened automatically. 465 // Tests if a file type should be opened automatically.
434 bool ShouldOpenFileBasedOnExtension(const FilePath& path) const; 466 bool ShouldOpenFileBasedOnExtension(const FilePath& path) const;
435 467
436 // Tests if we think the server means for this mime_type to be executable. 468 // Tests if we think the server means for this mime_type to be executable.
437 static bool IsExecutableMimeType(const std::string& mime_type); 469 static bool IsExecutableMimeType(const std::string& mime_type);
438 470
439 // Tests if a file is considered executable, based on its type. 471 // Tests if a file is considered executable, based on its type.
440 bool IsExecutableFile(const FilePath& path) const; 472 bool IsExecutableFile(const FilePath& path) const;
441 473
442 // Tests if a file type is considered executable. 474 // Tests if a file type is considered executable.
443 bool IsExecutableExtension(const FilePath::StringType& extension) const; 475 static bool IsExecutableExtension(const FilePath::StringType& extension);
444 476
445 // Resets the automatic open preference. 477 // Resets the automatic open preference.
446 void ResetAutoOpenFiles(); 478 void ResetAutoOpenFiles();
447 479
448 // Returns true if there are automatic handlers registered for any file 480 // Returns true if there are automatic handlers registered for any file
449 // types. 481 // types.
450 bool HasAutoOpenFileTypesRegistered() const; 482 bool HasAutoOpenFileTypesRegistered() const;
451 483
452 // Overridden from SelectFileDialog::Listener: 484 // Overridden from SelectFileDialog::Listener:
453 virtual void FileSelected(const FilePath& path, int index, void* params); 485 virtual void FileSelected(const FilePath& path, int index, void* params);
454 virtual void FileSelectionCanceled(void* params); 486 virtual void FileSelectionCanceled(void* params);
455 487
456 // Deletes the specified path on the file thread. 488 // Deletes the specified path on the file thread.
457 void DeleteDownload(const FilePath& path); 489 void DeleteDownload(const FilePath& path);
458 490
459 // Called when the user has validated the donwload of a dangerous file. 491 // Called when the user has validated the donwload of a dangerous file.
460 void DangerousDownloadValidated(DownloadItem* download); 492 void DangerousDownloadValidated(DownloadItem* download);
461 493
462 // Used to make sure we have a safe file extension and filename for a 494 // Used to make sure we have a safe file extension and filename for a
463 // download. |file_name| can either be just the file name or it can be a 495 // download. |file_name| can either be just the file name or it can be a
464 // full path to a file. 496 // full path to a file.
465 void GenerateSafeFilename(const std::string& mime_type, 497 static void GenerateSafeFileName(const std::string& mime_type,
466 FilePath* file_name); 498 FilePath* file_name);
467 499
468 // Runs the network cancel. Must be called on the IO thread. 500 // Runs the network cancel. Must be called on the IO thread.
469 static void OnCancelDownloadRequest(ResourceDispatcherHost* rdh, 501 static void OnCancelDownloadRequest(ResourceDispatcherHost* rdh,
470 int render_process_id, 502 int render_process_id,
471 int request_id); 503 int request_id);
472 504
505 // Create a file name based on the response from the server.
506 static void GenerateFileName(const GURL& url,
507 const std::string& content_disposition,
508 const std::string& referrer_charset,
509 const std::string& mime_type,
510 FilePath* generated_name);
511
473 private: 512 private:
474 friend class base::RefCountedThreadSafe<DownloadManager>; 513 friend class base::RefCountedThreadSafe<DownloadManager>;
475 514
476 ~DownloadManager(); 515 ~DownloadManager();
477 516
478 // Opens a download via the Windows shell. 517 // Opens a download via the Windows shell.
479 void OpenDownloadInShell(const DownloadItem* download, 518 void OpenDownloadInShell(const DownloadItem* download,
480 gfx::NativeView parent_window); 519 gfx::NativeView parent_window);
481 520
482 // Opens downloaded Chrome extension file (*.crx). 521 // Opens downloaded Chrome extension file (*.crx).
(...skipping 18 matching lines...) Expand all
501 void ContinueStartDownload(DownloadCreateInfo* info, 540 void ContinueStartDownload(DownloadCreateInfo* info,
502 const FilePath& target_path); 541 const FilePath& target_path);
503 542
504 // Update the history service for a particular download. 543 // Update the history service for a particular download.
505 void UpdateHistoryForDownload(DownloadItem* download); 544 void UpdateHistoryForDownload(DownloadItem* download);
506 void RemoveDownloadFromHistory(DownloadItem* download); 545 void RemoveDownloadFromHistory(DownloadItem* download);
507 void RemoveDownloadsFromHistoryBetween(const base::Time remove_begin, 546 void RemoveDownloadsFromHistoryBetween(const base::Time remove_begin,
508 const base::Time remove_before); 547 const base::Time remove_before);
509 548
510 // Create an extension based on the file name and mime type. 549 // Create an extension based on the file name and mime type.
511 void GenerateExtension(const FilePath& file_name, 550 static void GenerateExtension(const FilePath& file_name,
512 const std::string& mime_type, 551 const std::string& mime_type,
513 FilePath::StringType* generated_extension); 552 FilePath::StringType* generated_extension);
514 553
515 // Create a file name based on the response from the server. 554 // Create a file name based on the response from the server.
516 void GenerateFilename(DownloadCreateInfo* info, FilePath* generated_name); 555 static void GenerateFileNameFromInfo(DownloadCreateInfo* info,
556 FilePath* generated_name);
517 557
518 // Persist the automatic opening preference. 558 // Persist the automatic opening preference.
519 void SaveAutoOpens(); 559 void SaveAutoOpens();
520 560
521 // Download cancel helper function. 561 // Download cancel helper function.
522 void DownloadCancelledInternal(int download_id, 562 void DownloadCancelledInternal(int download_id,
523 int render_process_id, 563 int render_process_id,
524 int request_id); 564 int request_id);
525 565
526 // Runs the pause on the IO thread. 566 // Runs the pause on the IO thread.
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 // Set of file extensions to open at download completion. 652 // Set of file extensions to open at download completion.
613 struct AutoOpenCompareFunctor { 653 struct AutoOpenCompareFunctor {
614 inline bool operator()(const FilePath::StringType& a, 654 inline bool operator()(const FilePath::StringType& a,
615 const FilePath::StringType& b) const { 655 const FilePath::StringType& b) const {
616 return FilePath::CompareLessIgnoreCase(a, b); 656 return FilePath::CompareLessIgnoreCase(a, b);
617 } 657 }
618 }; 658 };
619 typedef std::set<FilePath::StringType, AutoOpenCompareFunctor> AutoOpenSet; 659 typedef std::set<FilePath::StringType, AutoOpenCompareFunctor> AutoOpenSet;
620 AutoOpenSet auto_open_; 660 AutoOpenSet auto_open_;
621 661
622 // Set of file extensions that are executables and shouldn't be auto opened.
623 std::set<std::string> exe_types_;
624
625 // Keep track of downloads that are completed before the user selects the 662 // Keep track of downloads that are completed before the user selects the
626 // destination, so that observers are appropriately notified of completion 663 // destination, so that observers are appropriately notified of completion
627 // after this determination is made. 664 // after this determination is made.
628 // The map is of download_id->remaining size (bytes), both of which are 665 // The map is of download_id->remaining size (bytes), both of which are
629 // required when calling DownloadFinished. 666 // required when calling DownloadFinished.
630 typedef std::map<int32, int64> PendingFinishedMap; 667 typedef std::map<int32, int64> PendingFinishedMap;
631 PendingFinishedMap pending_finished_downloads_; 668 PendingFinishedMap pending_finished_downloads_;
632 669
633 // The "Save As" dialog box used to ask the user where a file should be 670 // The "Save As" dialog box used to ask the user where a file should be
634 // saved. 671 // saved.
635 scoped_refptr<SelectFileDialog> select_file_dialog_; 672 scoped_refptr<SelectFileDialog> select_file_dialog_;
636 673
637 DISALLOW_COPY_AND_ASSIGN(DownloadManager); 674 DISALLOW_COPY_AND_ASSIGN(DownloadManager);
638 }; 675 };
639 676
640 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_ 677 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_
OLDNEW
« no previous file with comments | « chrome/browser/download/download_file.cc ('k') | chrome/browser/download/download_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698