blob: ebb20af166c5a524c15750a958853e34a7998ac2 [file] [log] [blame]
[email protected]6db833d12012-01-21 00:45:191// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#ifndef NET_BASE_UPLOAD_DATA_STREAM_H_
6#define NET_BASE_UPLOAD_DATA_STREAM_H_
7
olli.raula6df48b2a2015-11-26 07:40:228#include <vector>
9
mmenkecbc2b712014-10-09 20:29:0710#include "base/basictypes.h"
11#include "base/macros.h"
olli.raula6df48b2a2015-11-26 07:40:2212#include "base/memory/scoped_ptr.h"
[email protected]df7adc62012-09-18 14:01:5313#include "net/base/completion_callback.h"
[email protected]172da1b2011-08-12 15:52:2614#include "net/base/net_export.h"
initial.commit586acc5fe2008-07-26 22:42:5215
16namespace net {
17
[email protected]5b768142012-10-17 10:15:1718class DrainableIOBuffer;
[email protected]597cf6e2009-05-29 09:43:2619class IOBuffer;
[email protected]d98961652012-09-11 20:27:2120class UploadElementReader;
[email protected]597cf6e2009-05-29 09:43:2621
mmenkecbc2b712014-10-09 20:29:0722// A class for retrieving all data to be sent as a request body. Supports both
23// chunked and non-chunked uploads.
[email protected]172da1b2011-08-12 15:52:2624class NET_EXPORT UploadDataStream {
initial.commit586acc5fe2008-07-26 22:42:5225 public:
mmenkecbc2b712014-10-09 20:29:0726 // |identifier| identifies a particular upload instance, which is used by the
27 // cache to formulate a cache key. This value should be unique across browser
28 // sessions. A value of 0 is used to indicate an unspecified identifier.
wtc69f8ea82015-06-04 00:08:1329 UploadDataStream(bool is_chunked, int64_t identifier);
[email protected]b2d26cfd2012-12-11 10:36:0630
mmenkecbc2b712014-10-09 20:29:0731 virtual ~UploadDataStream();
[email protected]f288ef02012-12-15 20:28:2832
[email protected]e5d477342012-11-02 15:18:4633 // Initializes the stream. This function must be called before calling any
34 // other method. It is not valid to call any method (other than the
mmenkecbc2b712014-10-09 20:29:0735 // destructor) if Init() fails. This method can be called multiple times.
36 // Calling this method after an Init() success results in resetting the
37 // state (i.e. the stream is rewound).
[email protected]49ed6cc2012-02-02 08:59:1638 //
[email protected]df7adc62012-09-18 14:01:5339 // Does the initialization synchronously and returns the result if possible,
40 // otherwise returns ERR_IO_PENDING and runs the callback with the result.
41 //
[email protected]49ed6cc2012-02-02 08:59:1642 // Returns OK on success. Returns ERR_UPLOAD_FILE_CHANGED if the expected
43 // file modification time is set (usually not set, but set for sliced
44 // files) and the target file is changed.
[email protected]df7adc62012-09-18 14:01:5345 int Init(const CompletionCallback& callback);
46
[email protected]5b768142012-10-17 10:15:1747 // When possible, reads up to |buf_len| bytes synchronously from the upload
48 // data stream to |buf| and returns the number of bytes read; otherwise,
49 // returns ERR_IO_PENDING and calls |callback| with the number of bytes read.
50 // Partial reads are allowed. Zero is returned on a call to Read when there
51 // are no remaining bytes in the stream, and IsEof() will return true
52 // hereafter.
[email protected]6db833d12012-01-21 00:45:1953 //
[email protected]0ab2e242012-02-08 06:07:0054 // If there's less data to read than we initially observed (i.e. the actual
55 // upload data is smaller than size()), zeros are padded to ensure that
56 // size() bytes can be read, which can happen for TYPE_FILE payloads.
mmenkecbc2b712014-10-09 20:29:0757 //
58 // Reads are currently not allowed to fail - they must either return
59 // a value >= 0 or ERR_IO_PENDING, and call OnReadCompleted with a
60 // value >= 0.
61 // TODO(mmenke): Investigate letting reads fail.
[email protected]5b768142012-10-17 10:15:1762 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
63
initial.commit586acc5fe2008-07-26 22:42:5264 // Returns the total size of the data stream and the current position.
mmenkecbc2b712014-10-09 20:29:0765 // When the data is chunked, always returns zero. Must always return the same
66 // value after each call to Initialize().
wtc69f8ea82015-06-04 00:08:1367 uint64_t size() const { return total_size_; }
68 uint64_t position() const { return current_position_; }
initial.commit586acc5fe2008-07-26 22:42:5269
mmenkecbc2b712014-10-09 20:29:0770 // See constructor for description.
wtc69f8ea82015-06-04 00:08:1371 int64_t identifier() const { return identifier_; }
[email protected]699efe602011-01-25 07:17:1172
mmenkecbc2b712014-10-09 20:29:0773 bool is_chunked() const { return is_chunked_; }
[email protected]0736d9e2012-11-28 19:50:4074
[email protected]0ab2e242012-02-08 06:07:0075 // Returns true if all data has been consumed from this upload data
mmenkecbc2b712014-10-09 20:29:0776 // stream. For chunked uploads, returns false until the first read attempt.
77 // This makes some state machines a little simpler.
[email protected]0ab2e242012-02-08 06:07:0078 bool IsEOF() const;
[email protected]0c9bf872011-03-04 17:53:2279
mmenkecbc2b712014-10-09 20:29:0780 // Cancels all pending callbacks, and resets state. Any IOBuffer currently
81 // being read to is not safe for future use, as it may be in use on another
82 // thread.
[email protected]e5d477342012-11-02 15:18:4683 void Reset();
84
mmenkecbc2b712014-10-09 20:29:0785 // Returns true if the upload data in the stream is entirely in memory, and
86 // all read requests will succeed synchronously. Expected to return false for
87 // chunked requests.
88 virtual bool IsInMemory() const;
89
90 // Returns a list of element readers owned by |this|, if it has any.
olli.raula6df48b2a2015-11-26 07:40:2291 virtual const std::vector<scoped_ptr<UploadElementReader>>*
92 GetElementReaders() const;
mmenkecbc2b712014-10-09 20:29:0793
94 protected:
95 // Must be called by subclasses when InitInternal and ReadInternal complete
96 // asynchronously.
97 void OnInitCompleted(int result);
98 void OnReadCompleted(int result);
99
100 // Must be called before InitInternal completes, for non-chunked uploads.
101 // Must not be called for chunked uploads.
wtc69f8ea82015-06-04 00:08:13102 void SetSize(uint64_t size);
mmenkecbc2b712014-10-09 20:29:07103
104 // Must be called for chunked uploads before the final ReadInternal call
105 // completes. Must not be called for non-chunked uploads.
106 void SetIsFinalChunk();
107
[email protected]02cad5d2013-10-02 08:14:03108 private:
mmenkecbc2b712014-10-09 20:29:07109 // See Init(). If it returns ERR_IO_PENDING, OnInitCompleted must be called
110 // once it completes. If the upload is not chunked, SetSize must be called
111 // before it completes.
112 virtual int InitInternal() = 0;
[email protected]e5d477342012-11-02 15:18:46113
mmenkecbc2b712014-10-09 20:29:07114 // See Read(). For chunked uploads, must call SetIsFinalChunk if this is the
115 // final chunk. For non-chunked uploads, the UploadDataStream determins which
116 // read is the last based on size. Must read 1 or more bytes on every call,
117 // though the final chunk may be 0 bytes, for chunked requests. If it returns
118 // ERR_IO_PENDING, OnInitCompleted must be called once it completes. Must not
119 // return any error, other than ERR_IO_PENDING.
120 virtual int ReadInternal(IOBuffer* buf, int buf_len) = 0;
[email protected]df7adc62012-09-18 14:01:53121
mmenkecbc2b712014-10-09 20:29:07122 // Resets state and cancels any pending callbacks. Guaranteed to be called
123 // before all but the first call to InitInternal.
124 virtual void ResetInternal() = 0;
[email protected]9fa94db2012-10-22 03:34:07125
wtc69f8ea82015-06-04 00:08:13126 uint64_t total_size_;
127 uint64_t current_position_;
initial.commit586acc5fe2008-07-26 22:42:52128
wtc69f8ea82015-06-04 00:08:13129 const int64_t identifier_;
[email protected]b2d26cfd2012-12-11 10:36:06130
131 const bool is_chunked_;
[email protected]fac16e22012-12-29 18:46:31132
[email protected]49ed6cc2012-02-02 08:59:16133 // True if the initialization was successful.
134 bool initialized_successfully_;
135
mmenkecbc2b712014-10-09 20:29:07136 bool is_eof_;
[email protected]9fa94db2012-10-22 03:34:07137
mmenkecbc2b712014-10-09 20:29:07138 CompletionCallback callback_;
[email protected]df7adc62012-09-18 14:01:53139
[email protected]73a797fb2010-06-07 02:10:18140 DISALLOW_COPY_AND_ASSIGN(UploadDataStream);
initial.commit586acc5fe2008-07-26 22:42:52141};
142
143} // namespace net
144
145#endif // NET_BASE_UPLOAD_DATA_STREAM_H_