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 #ifndef CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_
      6 #define CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_
      7 #pragma once
      8 
      9 #include <string>
     10 
     11 #include "base/file_path.h"
     12 #include "base/memory/linked_ptr.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "chrome/browser/power_save_blocker.h"
     15 #include "googleurl/src/gurl.h"
     16 
     17 namespace crypto {
     18 class SecureHash;
     19 }
     20 namespace net {
     21 class FileStream;
     22 }
     23 
     24 // File being downloaded and saved to disk. This is a base class
     25 // for DownloadFile and SaveFile, which keep more state information.
     26 class BaseFile {
     27  public:
     28   BaseFile(const FilePath& full_path,
     29            const GURL& source_url,
     30            const GURL& referrer_url,
     31            int64 received_bytes,
     32            const linked_ptr<net::FileStream>& file_stream);
     33   ~BaseFile();
     34 
     35   // If calculate_hash is true, sha256 hash will be calculated.
     36   bool Initialize(bool calculate_hash);
     37 
     38   // Write a new chunk of data to the file. Returns true on success (all bytes
     39   // written to the file).
     40   bool AppendDataToFile(const char* data, size_t data_len);
     41 
     42   // Rename the download file. Returns true on success.
     43   virtual bool Rename(const FilePath& full_path);
     44 
     45   // Detach the file so it is not deleted on destruction.
     46   virtual void Detach();
     47 
     48   // Abort the download and automatically close the file.
     49   void Cancel();
     50 
     51   // Indicate that the download has finished. No new data will be received.
     52   void Finish();
     53 
     54   // Informs the OS that this file came from the internet.
     55   void AnnotateWithSourceInformation();
     56 
     57   FilePath full_path() const { return full_path_; }
     58   bool in_progress() const { return file_stream_ != NULL; }
     59   int64 bytes_so_far() const { return bytes_so_far_; }
     60 
     61   // Set |hash| with sha256 digest for the file.
     62   // Returns true if digest is successfully calculated.
     63   virtual bool GetSha256Hash(std::string* hash);
     64 
     65   virtual std::string DebugString() const;
     66 
     67  protected:
     68   bool Open();
     69   void Close();
     70 
     71   // Full path to the file including the file name.
     72   FilePath full_path_;
     73 
     74  private:
     75   static const size_t kSha256HashLen = 32;
     76 
     77   // Source URL for the file being downloaded.
     78   GURL source_url_;
     79 
     80   // The URL where the download was initiated.
     81   GURL referrer_url_;
     82 
     83   // OS file stream for writing
     84   linked_ptr<net::FileStream> file_stream_;
     85 
     86   // Amount of data received up so far, in bytes.
     87   int64 bytes_so_far_;
     88 
     89   // RAII handle to keep the system from sleeping while we're downloading.
     90   PowerSaveBlocker power_save_blocker_;
     91 
     92   // Indicates if sha256 hash should be calculated for the file.
     93   bool calculate_hash_;
     94 
     95   // Used to calculate sha256 hash for the file when calculate_hash_
     96   // is set.
     97   scoped_ptr<crypto::SecureHash> secure_hash_;
     98 
     99   unsigned char sha256_hash_[kSha256HashLen];
    100 
    101   // Indicates that this class no longer owns the associated file, and so
    102   // won't delete it on destruction.
    103   bool detached_;
    104 
    105   DISALLOW_COPY_AND_ASSIGN(BaseFile);
    106 };
    107 
    108 #endif  // CHROME_BROWSER_DOWNLOAD_BASE_FILE_H_
    109