Home | History | Annotate | Download | only in base
      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 NET_BASE_UPLOAD_FILE_ELEMENT_READER_H_
      6 #define NET_BASE_UPLOAD_FILE_ELEMENT_READER_H_
      7 
      8 #include "base/compiler_specific.h"
      9 #include "base/files/file_path.h"
     10 #include "base/gtest_prod_util.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/time/time.h"
     15 #include "net/base/upload_element_reader.h"
     16 
     17 namespace base {
     18 class TaskRunner;
     19 }
     20 
     21 namespace net {
     22 
     23 class FileStream;
     24 
     25 // An UploadElementReader implementation for file.
     26 class NET_EXPORT UploadFileElementReader : public UploadElementReader {
     27  public:
     28   // |task_runner| is used to perform file operations. It must not be NULL.
     29   UploadFileElementReader(base::TaskRunner* task_runner,
     30                           const base::FilePath& path,
     31                           uint64 range_offset,
     32                           uint64 range_length,
     33                           const base::Time& expected_modification_time);
     34   virtual ~UploadFileElementReader();
     35 
     36   const base::FilePath& path() const { return path_; }
     37   uint64 range_offset() const { return range_offset_; }
     38   uint64 range_length() const { return range_length_; }
     39   const base::Time& expected_modification_time() const {
     40     return expected_modification_time_;
     41   }
     42 
     43   // UploadElementReader overrides:
     44   virtual const UploadFileElementReader* AsFileReader() const OVERRIDE;
     45   virtual int Init(const CompletionCallback& callback) OVERRIDE;
     46   virtual uint64 GetContentLength() const OVERRIDE;
     47   virtual uint64 BytesRemaining() const OVERRIDE;
     48   virtual int Read(IOBuffer* buf,
     49                    int buf_length,
     50                    const CompletionCallback& callback) OVERRIDE;
     51 
     52  private:
     53   // Deletes FileStream with |task_runner| to avoid blocking the IO thread.
     54   // This class is used as a template argument of scoped_ptr.
     55   class FileStreamDeleter {
     56    public:
     57     explicit FileStreamDeleter(base::TaskRunner* task_runner);
     58     ~FileStreamDeleter();
     59     void operator() (FileStream* file_stream) const;
     60 
     61    private:
     62     scoped_refptr<base::TaskRunner> task_runner_;
     63   };
     64 
     65   typedef scoped_ptr<FileStream, FileStreamDeleter> ScopedFileStreamPtr;
     66 
     67   FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength);
     68   FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest,
     69                            UploadFileSmallerThanLength);
     70   FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy2Test,
     71                            UploadFileSmallerThanLength);
     72   FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy3Test,
     73                            UploadFileSmallerThanLength);
     74 
     75   // Resets this instance to the uninitialized state.
     76   void Reset();
     77 
     78   // This method is used to implement Init().
     79   void OnInitCompleted(ScopedFileStreamPtr* file_stream,
     80                        uint64* content_length,
     81                        const CompletionCallback& callback,
     82                        int result);
     83 
     84   // This method is used to implement Read().
     85   void OnReadCompleted(ScopedFileStreamPtr file_stream,
     86                        const CompletionCallback& callback,
     87                        int result);
     88 
     89   // Sets an value to override the result for GetContentLength().
     90   // Used for tests.
     91   struct NET_EXPORT_PRIVATE ScopedOverridingContentLengthForTests {
     92     ScopedOverridingContentLengthForTests(uint64 value);
     93     ~ScopedOverridingContentLengthForTests();
     94   };
     95 
     96   scoped_refptr<base::TaskRunner> task_runner_;
     97   const base::FilePath path_;
     98   const uint64 range_offset_;
     99   const uint64 range_length_;
    100   const base::Time expected_modification_time_;
    101   ScopedFileStreamPtr file_stream_;
    102   uint64 content_length_;
    103   uint64 bytes_remaining_;
    104   base::WeakPtrFactory<UploadFileElementReader> weak_ptr_factory_;
    105 
    106   DISALLOW_COPY_AND_ASSIGN(UploadFileElementReader);
    107 };
    108 
    109 // An UploadElementReader implementation for file which performs file operation
    110 // synchronously.
    111 // Use this class only if the thread is IO allowed.
    112 class NET_EXPORT UploadFileElementReaderSync : public UploadElementReader {
    113  public:
    114   UploadFileElementReaderSync(const base::FilePath& path,
    115                               uint64 range_offset,
    116                               uint64 range_length,
    117                               const base::Time& expected_modification_time);
    118   virtual ~UploadFileElementReaderSync();
    119 
    120   // UploadElementReader overrides:
    121   virtual int Init(const CompletionCallback& callback) OVERRIDE;
    122   virtual uint64 GetContentLength() const OVERRIDE;
    123   virtual uint64 BytesRemaining() const OVERRIDE;
    124   virtual int Read(IOBuffer* buf,
    125                    int buf_length,
    126                    const CompletionCallback& callback) OVERRIDE;
    127 
    128  private:
    129   const base::FilePath path_;
    130   const uint64 range_offset_;
    131   const uint64 range_length_;
    132   const base::Time expected_modification_time_;
    133   scoped_ptr<FileStream> file_stream_;
    134   uint64 content_length_;
    135   uint64 bytes_remaining_;
    136 
    137   DISALLOW_COPY_AND_ASSIGN(UploadFileElementReaderSync);
    138 };
    139 
    140 }  // namespace net
    141 
    142 #endif  // NET_BASE_UPLOAD_FILE_ELEMENT_READER_H_
    143