Home | History | Annotate | Download | only in download
      1 // Copyright (c) 2012 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 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_
      6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/callback_forward.h"
     12 #include "base/files/file_path.h"
     13 #include "content/common/content_export.h"
     14 #include "content/public/browser/download_interrupt_reasons.h"
     15 
     16 namespace content {
     17 
     18 class DownloadManager;
     19 
     20 // These objects live exclusively on the file thread and handle the writing
     21 // operations for one download. These objects live only for the duration that
     22 // the download is 'in progress': once the download has been completed or
     23 // cancelled, the DownloadFile is destroyed.
     24 class CONTENT_EXPORT DownloadFile {
     25  public:
     26   // Callback used with Initialize.  On a successful initialize, |reason| will
     27   // be DOWNLOAD_INTERRUPT_REASON_NONE; on a failed initialize, it will be
     28   // set to the reason for the failure.
     29   typedef base::Callback<void(DownloadInterruptReason reason)>
     30       InitializeCallback;
     31 
     32   // Callback used with Rename*().  On a successful rename |reason| will be
     33   // DOWNLOAD_INTERRUPT_REASON_NONE and |path| the path the rename
     34   // was done to.  On a failed rename, |reason| will contain the
     35   // error.
     36   typedef base::Callback<void(DownloadInterruptReason reason,
     37                               const base::FilePath& path)>
     38       RenameCompletionCallback;
     39 
     40   virtual ~DownloadFile() {}
     41 
     42   // Upon completion, |callback| will be called on the UI
     43   // thread as per the comment above, passing DOWNLOAD_INTERRUPT_REASON_NONE
     44   // on success, or a network download interrupt reason on failure.
     45   virtual void Initialize(const InitializeCallback& callback) = 0;
     46 
     47   // Rename the download file to |full_path|.  If that file exists
     48   // |full_path| will be uniquified by suffixing " (<number>)" to the
     49   // file name before the extension.
     50   virtual void RenameAndUniquify(const base::FilePath& full_path,
     51                                  const RenameCompletionCallback& callback) = 0;
     52 
     53   // Rename the download file to |full_path| and annotate it with
     54   // "Mark of the Web" information about its source.  No uniquification
     55   // will be performed.
     56   virtual void RenameAndAnnotate(const base::FilePath& full_path,
     57                                  const RenameCompletionCallback& callback) = 0;
     58 
     59   // Detach the file so it is not deleted on destruction.
     60   virtual void Detach() = 0;
     61 
     62   // Abort the download and automatically close the file.
     63   virtual void Cancel() = 0;
     64 
     65   virtual base::FilePath FullPath() const = 0;
     66   virtual bool InProgress() const = 0;
     67   virtual int64 CurrentSpeed() const = 0;
     68 
     69   // Set |hash| with sha256 digest for the file.
     70   // Returns true if digest is successfully calculated.
     71   virtual bool GetHash(std::string* hash) = 0;
     72 
     73   // Returns the current (intermediate) state of the hash as a byte string.
     74   virtual std::string GetHashState() = 0;
     75 
     76   // Set the application GUID to be used to identify the app to the
     77   // system AV function when scanning downloaded files. Should be called
     78   // before RenameAndAnnotate() to take effect.
     79   virtual void SetClientGuid(const std::string& guid) = 0;
     80 
     81   // For testing.  Must be called on FILE thread.
     82   // TODO(rdsmith): Replace use of EnsureNoPendingDownloads()
     83   // on the DownloadManager with a test-specific DownloadFileFactory
     84   // which keeps track of the number of DownloadFiles.
     85   static int GetNumberOfDownloadFiles();
     86 
     87  protected:
     88   static int number_active_objects_;
     89 };
     90 
     91 }  // namespace content
     92 
     93 #endif  // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_H_
     94