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_DATA_STREAM_H_
6 #define NET_BASE_UPLOAD_DATA_STREAM_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "net/base/completion_once_callback.h"
14 #include "net/base/net_export.h"
15 #include "net/base/upload_progress.h"
16 #include "net/log/net_log_with_source.h"
17 
18 namespace net {
19 
20 class IOBuffer;
21 class UploadElementReader;
22 
23 // A class for retrieving all data to be sent as a request body. Supports both
24 // chunked and non-chunked uploads.
25 class NET_EXPORT UploadDataStream {
26  public:
27   // |identifier| identifies a particular upload instance, which is used by the
28   // cache to formulate a cache key. This value should be unique across browser
29   // sessions. A value of 0 is used to indicate an unspecified identifier.
30   UploadDataStream(bool is_chunked, int64_t identifier);
31 
32   UploadDataStream(const UploadDataStream&) = delete;
33   UploadDataStream& operator=(const UploadDataStream&) = delete;
34 
35   virtual ~UploadDataStream();
36 
37   // Initializes the stream. This function must be called before calling any
38   // other method. It is not valid to call any method (other than the
39   // destructor) if Init() fails. This method can be called multiple times.
40   // Calling this method after an Init() success results in resetting the
41   // state (i.e. the stream is rewound).
42   //
43   // Does the initialization synchronously and returns the result if possible,
44   // otherwise returns ERR_IO_PENDING and runs the callback with the result.
45   //
46   // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected
47   // file modification time is set (usually not set, but set for sliced
48   // files) and the target file is changed.
49   int Init(CompletionOnceCallback callback, const NetLogWithSource& net_log);
50 
51   // When possible, reads up to |buf_len| bytes synchronously from the upload
52   // data stream to |buf| and returns the number of bytes read; otherwise,
53   // returns ERR_IO_PENDING and calls |callback| with the number of bytes read.
54   // Partial reads are allowed. Zero is returned on a call to Read when there
55   // are no remaining bytes in the stream, and IsEof() will return true
56   // hereafter.
57   //
58   // If there's less data to read than we initially observed (i.e. the actual
59   // upload data is smaller than size()), zeros are padded to ensure that
60   // size() bytes can be read, which can happen for TYPE_FILE payloads.
61   //
62   // TODO(mmenke):  Investigate letting reads fail.
63   int Read(IOBuffer* buf, int buf_len, CompletionOnceCallback callback);
64 
65   // Returns the total size of the data stream and the current position.
66   // When the data is chunked, always returns zero. Must always return the same
67   // value after each call to Initialize().
size()68   uint64_t size() const { return total_size_; }
position()69   uint64_t position() const { return current_position_; }
70 
71   // See constructor for description.
identifier()72   int64_t identifier() const { return identifier_; }
73 
is_chunked()74   bool is_chunked() const { return is_chunked_; }
75 
76   // Returns true if all data has been consumed from this upload data
77   // stream. For chunked uploads, returns false until the first read attempt.
78   // This makes some state machines a little simpler.
79   bool IsEOF() const;
80 
81   // Cancels all pending callbacks, and resets state. Any IOBuffer currently
82   // being read to is not safe for future use, as it may be in use on another
83   // thread.
84   void Reset();
85 
86   // Returns true if the upload data in the stream is entirely in memory, and
87   // all read requests will succeed synchronously. Expected to return false for
88   // chunked requests.
89   virtual bool IsInMemory() const;
90 
91   // Returns a list of element readers owned by |this|, if it has any.
92   virtual const std::vector<std::unique_ptr<UploadElementReader>>*
93   GetElementReaders() const;
94 
95   // Returns the upload progress. If the stream was not initialized
96   // successfully, or has been reset and not yet re-initialized, returns an
97   // empty UploadProgress.
98   virtual UploadProgress GetUploadProgress() const;
99 
100   // Indicates whether fetch upload streaming is allowed/rejected over H/1.
101   // Even if this is false but there is a QUIC/H2 stream, the upload is allowed.
102   virtual bool AllowHTTP1() const;
103 
104  protected:
105   // Must be called by subclasses when InitInternal and ReadInternal complete
106   // asynchronously.
107   void OnInitCompleted(int result);
108   void OnReadCompleted(int result);
109 
110   // Must be called before InitInternal completes, for non-chunked uploads.
111   // Must not be called for chunked uploads.
112   void SetSize(uint64_t size);
113 
114   // Must be called for chunked uploads before the final ReadInternal call
115   // completes. Must not be called for non-chunked uploads.
116   void SetIsFinalChunk();
117 
118  private:
119   // See Init(). If it returns ERR_IO_PENDING, OnInitCompleted must be called
120   // once it completes. If the upload is not chunked, SetSize must be called
121   // before it completes.
122   virtual int InitInternal(const NetLogWithSource& net_log) = 0;
123 
124   // See Read(). For chunked uploads, must call SetIsFinalChunk if this is the
125   // final chunk. For non-chunked uploads, the UploadDataStream determins which
126   // read is the last based on size. Must read 1 or more bytes on every call,
127   // though the final chunk may be 0 bytes, for chunked requests. If it returns
128   // ERR_IO_PENDING, OnInitCompleted must be called once it completes. Must not
129   // return any error, other than ERR_IO_PENDING.
130   virtual int ReadInternal(IOBuffer* buf, int buf_len) = 0;
131 
132   // Resets state and cancels any pending callbacks. Guaranteed to be called
133   // at least once before every call to InitInternal.
134   virtual void ResetInternal() = 0;
135 
136   uint64_t total_size_;
137   uint64_t current_position_;
138 
139   const int64_t identifier_;
140 
141   const bool is_chunked_;
142 
143   // True if the initialization was successful.
144   bool initialized_successfully_;
145 
146   bool is_eof_;
147 
148   CompletionOnceCallback callback_;
149 
150   NetLogWithSource net_log_;
151 };
152 
153 }  // namespace net
154 
155 #endif  // NET_BASE_UPLOAD_DATA_STREAM_H_
156