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.
5 #ifndef NET_BASE_UPLOAD_FILE_ELEMENT_READER_H_
6 #define NET_BASE_UPLOAD_FILE_ELEMENT_READER_H_
8 #include "base/compiler_specific.h"
9 #include "base/files/file.h"
10 #include "base/files/file_path.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/time/time.h"
16 #include "net/base/net_export.h"
17 #include "net/base/upload_element_reader.h"
27 // An UploadElementReader implementation for file.
28 class NET_EXPORT UploadFileElementReader
: public UploadElementReader
{
30 // |task_runner| is used to perform file operations. It must not be NULL.
31 UploadFileElementReader(base::TaskRunner
* task_runner
,
32 const base::FilePath
& path
,
33 uint64_t range_offset
,
34 uint64_t range_length
,
35 const base::Time
& expected_modification_time
);
36 ~UploadFileElementReader() override
;
38 const base::FilePath
& path() const { return path_
; }
39 uint64_t range_offset() const { return range_offset_
; }
40 uint64_t range_length() const { return range_length_
; }
41 const base::Time
& expected_modification_time() const {
42 return expected_modification_time_
;
45 // UploadElementReader overrides:
46 const UploadFileElementReader
* AsFileReader() const override
;
47 int Init(const CompletionCallback
& callback
) override
;
48 uint64_t GetContentLength() const override
;
49 uint64_t BytesRemaining() const override
;
50 int Read(IOBuffer
* buf
,
52 const CompletionCallback
& callback
) override
;
55 FRIEND_TEST_ALL_PREFIXES(ElementsUploadDataStreamTest
, FileSmallerThanLength
);
56 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest
,
57 UploadFileSmallerThanLength
);
59 // Resets this instance to the uninitialized state.
62 // These methods are used to implement Init().
63 void OnOpenCompleted(const CompletionCallback
& callback
, int result
);
64 void OnSeekCompleted(const CompletionCallback
& callback
, int64_t result
);
65 void OnGetFileInfoCompleted(const CompletionCallback
& callback
,
66 base::File::Info
* file_info
,
69 // This method is used to implement Read().
70 int OnReadCompleted(const CompletionCallback
& callback
, int result
);
72 // Sets an value to override the result for GetContentLength().
74 struct NET_EXPORT_PRIVATE ScopedOverridingContentLengthForTests
{
75 explicit ScopedOverridingContentLengthForTests(uint64_t value
);
76 ~ScopedOverridingContentLengthForTests();
79 scoped_refptr
<base::TaskRunner
> task_runner_
;
80 const base::FilePath path_
;
81 const uint64_t range_offset_
;
82 const uint64_t range_length_
;
83 const base::Time expected_modification_time_
;
84 scoped_ptr
<FileStream
> file_stream_
;
85 uint64_t content_length_
;
86 uint64_t bytes_remaining_
;
87 base::WeakPtrFactory
<UploadFileElementReader
> weak_ptr_factory_
;
89 DISALLOW_COPY_AND_ASSIGN(UploadFileElementReader
);
94 #endif // NET_BASE_UPLOAD_FILE_ELEMENT_READER_H_