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_IMPL_H_ 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ 7 8 #include "content/browser/download/download_file.h" 9 10 #include "base/memory/ref_counted.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/weak_ptr.h" 13 #include "base/time/time.h" 14 #include "base/timer/timer.h" 15 #include "content/browser/byte_stream.h" 16 #include "content/browser/download/base_file.h" 17 #include "content/browser/download/rate_estimator.h" 18 #include "content/public/browser/download_save_info.h" 19 #include "net/base/net_log.h" 20 21 namespace content { 22 class ByteStreamReader; 23 class DownloadDestinationObserver; 24 class DownloadManager; 25 struct DownloadCreateInfo; 26 27 class CONTENT_EXPORT DownloadFileImpl : virtual public DownloadFile { 28 public: 29 // Takes ownership of the object pointed to by |request_handle|. 30 // |bound_net_log| will be used for logging the download file's events. 31 // May be constructed on any thread. All methods besides the constructor 32 // (including destruction) must occur on the FILE thread. 33 // 34 // Note that the DownloadFileImpl automatically reads from the passed in 35 // stream, and sends updates and status of those reads to the 36 // DownloadDestinationObserver. 37 DownloadFileImpl( 38 scoped_ptr<DownloadSaveInfo> save_info, 39 const base::FilePath& default_downloads_directory, 40 const GURL& url, 41 const GURL& referrer_url, 42 bool calculate_hash, 43 scoped_ptr<ByteStreamReader> stream, 44 const net::BoundNetLog& bound_net_log, 45 base::WeakPtr<DownloadDestinationObserver> observer); 46 47 virtual ~DownloadFileImpl(); 48 49 // DownloadFile functions. 50 virtual void Initialize(const InitializeCallback& callback) OVERRIDE; 51 virtual void RenameAndUniquify( 52 const base::FilePath& full_path, 53 const RenameCompletionCallback& callback) OVERRIDE; 54 virtual void RenameAndAnnotate( 55 const base::FilePath& full_path, 56 const RenameCompletionCallback& callback) OVERRIDE; 57 virtual void Detach() OVERRIDE; 58 virtual void Cancel() OVERRIDE; 59 virtual base::FilePath FullPath() const OVERRIDE; 60 virtual bool InProgress() const OVERRIDE; 61 virtual int64 CurrentSpeed() const OVERRIDE; 62 virtual bool GetHash(std::string* hash) OVERRIDE; 63 virtual std::string GetHashState() OVERRIDE; 64 virtual void SetClientGuid(const std::string& guid) OVERRIDE; 65 66 protected: 67 // For test class overrides. 68 virtual DownloadInterruptReason AppendDataToFile( 69 const char* data, size_t data_len); 70 71 private: 72 // Send an update on our progress. 73 void SendUpdate(); 74 75 // Called when there's some activity on stream_reader_ that needs to be 76 // handled. 77 void StreamActive(); 78 79 // The base file instance. 80 BaseFile file_; 81 82 // The default directory for creating the download file. 83 base::FilePath default_download_directory_; 84 85 // The stream through which data comes. 86 // TODO(rdsmith): Move this into BaseFile; requires using the same 87 // stream semantics in SavePackage. Alternatively, replace SaveFile 88 // with DownloadFile and get rid of BaseFile. 89 scoped_ptr<ByteStreamReader> stream_reader_; 90 91 // Used to trigger progress updates. 92 scoped_ptr<base::RepeatingTimer<DownloadFileImpl> > update_timer_; 93 94 // Statistics 95 size_t bytes_seen_; 96 base::TimeDelta disk_writes_time_; 97 base::TimeTicks download_start_; 98 RateEstimator rate_estimator_; 99 100 net::BoundNetLog bound_net_log_; 101 102 base::WeakPtr<DownloadDestinationObserver> observer_; 103 104 base::WeakPtrFactory<DownloadFileImpl> weak_factory_; 105 106 DISALLOW_COPY_AND_ASSIGN(DownloadFileImpl); 107 }; 108 109 } // namespace content 110 111 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_FILE_IMPL_H_ 112