Home | History | Annotate | Download | only in download
      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 // The DownloadFileManager owns a set of DownloadFile objects, each of which
      6 // represent one in progress download and performs the disk IO for that
      7 // download. The DownloadFileManager itself is a singleton object owned by the
      8 // ResourceDispatcherHost.
      9 //
     10 // The DownloadFileManager uses the file_thread for performing file write
     11 // operations, in order to avoid disk activity on either the IO (network) thread
     12 // and the UI thread. It coordinates the notifications from the network and UI.
     13 //
     14 // A typical download operation involves multiple threads:
     15 //
     16 // Updating an in progress download
     17 // io_thread
     18 //      |----> data ---->|
     19 //                     file_thread (writes to disk)
     20 //                              |----> stats ---->|
     21 //                                              ui_thread (feedback for user and
     22 //                                                         updates to history)
     23 //
     24 // Cancel operations perform the inverse order when triggered by a user action:
     25 // ui_thread (user click)
     26 //    |----> cancel command ---->|
     27 //                          file_thread (close file)
     28 //                                 |----> cancel command ---->|
     29 //                                                    io_thread (stops net IO
     30 //                                                               for download)
     31 //
     32 // The DownloadFileManager tracks download requests, mapping from a download
     33 // ID (unique integer created in the IO thread) to the DownloadManager for the
     34 // tab (profile) where the download was initiated. In the event of a tab closure
     35 // during a download, the DownloadFileManager will continue to route data to the
     36 // appropriate DownloadManager. In progress downloads are cancelled for a
     37 // DownloadManager that exits (such as when closing a profile).
     38 
     39 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_FILE_MANAGER_H_
     40 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_FILE_MANAGER_H_
     41 #pragma once
     42 
     43 #include <map>
     44 
     45 #include "base/basictypes.h"
     46 #include "base/gtest_prod_util.h"
     47 #include "base/hash_tables.h"
     48 #include "base/memory/ref_counted.h"
     49 #include "base/timer.h"
     50 #include "ui/gfx/native_widget_types.h"
     51 
     52 struct DownloadBuffer;
     53 struct DownloadCreateInfo;
     54 struct DownloadSaveInfo;
     55 class DownloadFile;
     56 class DownloadManager;
     57 class FilePath;
     58 class GURL;
     59 class ResourceDispatcherHost;
     60 
     61 namespace net {
     62 class URLRequestContextGetter;
     63 }
     64 
     65 // Manages all in progress downloads.
     66 class DownloadFileManager
     67     : public base::RefCountedThreadSafe<DownloadFileManager> {
     68  public:
     69   explicit DownloadFileManager(ResourceDispatcherHost* rdh);
     70 
     71   // Called on shutdown on the UI thread.
     72   void Shutdown();
     73 
     74   // Called on the IO thread
     75   int GetNextId();
     76 
     77   // Called on UI thread to make DownloadFileManager start the download.
     78   void StartDownload(DownloadCreateInfo* info);
     79 
     80   // Handlers for notifications sent from the IO thread and run on the
     81   // FILE thread.
     82   void UpdateDownload(int id, DownloadBuffer* buffer);
     83   // |os_error| is 0 for normal completions, and non-0 for errors.
     84   // |security_info| contains SSL information (cert_id, cert_status,
     85   // security_bits, ssl_connection_status), which can be used to
     86   // fine-tune the error message.  It is empty if the transaction
     87   // was not performed securely.
     88   void OnResponseCompleted(int id,
     89                            DownloadBuffer* buffer,
     90                            int os_error,
     91                            const std::string& security_info);
     92 
     93   // Handlers for notifications sent from the UI thread and run on the
     94   // FILE thread.  These are both terminal actions with respect to the
     95   // download file, as far as the DownloadFileManager is concerned -- if
     96   // anything happens to the download file after they are called, it will
     97   // be ignored.
     98   void CancelDownload(int id);
     99   void CompleteDownload(int id);
    100 
    101   // Called on FILE thread by DownloadManager at the beginning of its shutdown.
    102   void OnDownloadManagerShutdown(DownloadManager* manager);
    103 
    104   // The DownloadManager in the UI thread has provided an intermediate
    105   // .crdownload name for the download specified by |id|.
    106   void RenameInProgressDownloadFile(int id, const FilePath& full_path);
    107 
    108   // The DownloadManager in the UI thread has provided a final name for the
    109   // download specified by |id|.
    110   // |overwrite_existing_file| prevents uniquification, and is used for SAFE
    111   // downloads, as the user may have decided to overwrite the file.
    112   // Sent from the UI thread and run on the FILE thread.
    113   void RenameCompletingDownloadFile(int id,
    114                                     const FilePath& full_path,
    115                                     bool overwrite_existing_file);
    116 
    117   // The number of downloads currently active on the DownloadFileManager.
    118   // Primarily for testing.
    119   int NumberOfActiveDownloads() const {
    120     return downloads_.size();
    121   }
    122 
    123  private:
    124   friend class base::RefCountedThreadSafe<DownloadFileManager>;
    125   friend class DownloadManagerTest;
    126   FRIEND_TEST_ALL_PREFIXES(DownloadManagerTest, StartDownload);
    127 
    128   ~DownloadFileManager();
    129 
    130   // Timer helpers for updating the UI about the current progress of a download.
    131   void StartUpdateTimer();
    132   void StopUpdateTimer();
    133   void UpdateInProgressDownloads();
    134 
    135   // Clean up helper that runs on the download thread.
    136   void OnShutdown();
    137 
    138   // Creates DownloadFile on FILE thread and continues starting the download
    139   // process.
    140   void CreateDownloadFile(DownloadCreateInfo* info,
    141                           DownloadManager* download_manager,
    142                           bool hash_needed);
    143 
    144   // Tells the ResourceDispatcherHost to resume a download request
    145   // that was paused to wait for the on-disk file to be created.
    146   void ResumeDownloadRequest(int child_id, int request_id);
    147 
    148   // Called only on the download thread.
    149   DownloadFile* GetDownloadFile(int id);
    150 
    151   // Called only from RenameInProgressDownloadFile and
    152   // RenameCompletingDownloadFile on the FILE thread.
    153   void CancelDownloadOnRename(int id);
    154 
    155   // Erases the download file with the given the download |id| and removes
    156   // it from the maps.
    157   void EraseDownload(int id);
    158 
    159   // Unique ID for each DownloadFile.
    160   int next_id_;
    161 
    162   typedef base::hash_map<int, DownloadFile*> DownloadFileMap;
    163 
    164   // A map of all in progress downloads.  It owns the download files.
    165   DownloadFileMap downloads_;
    166 
    167   // Schedule periodic updates of the download progress. This timer
    168   // is controlled from the FILE thread, and posts updates to the UI thread.
    169   base::RepeatingTimer<DownloadFileManager> update_timer_;
    170 
    171   ResourceDispatcherHost* resource_dispatcher_host_;
    172 
    173   DISALLOW_COPY_AND_ASSIGN(DownloadFileManager);
    174 };
    175 
    176 #endif  // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_FILE_MANAGER_H_
    177